/** * Choose the best matching VAT type for given product category and country. * * We calculate priority for each VAT type and then choose * the VAT type having the highest priority * (or first of those having the highest priority). * * VAT type priority is calculated from county match and category match as following: * * CountryMatch = 0 * CategoryMatch = 1 * * if ( there is exact match on country ) * CountryMatch = 2 * elseif ( there is weak match on country ) * CountryMatch = 1 * * if ( there is exact match on product category ) * CategoryMatch = 2 * elseif ( there is weak match on product category ) * CategoryMatch = 1 * * if ( there is match on both country and category ) * VatTypePriority = CountryMatch * 2 + CategoryMatch - 2 * else * VatTypePriority = 0 * * \private * \static */ function chooseVatType( $productCategory, $country ) { $vatRules = eZVatRule::fetchList(); $catID = $productCategory->attribute( 'id' ); $vatPriorities = array(); foreach ( $vatRules as $rule ) { $ruleCountry = $rule->attribute( 'country_code' ); $ruleCatIDs = $rule->attribute( 'product_categories_ids' ); $ruleVatID = $rule->attribute( 'vat_type' ); $categoryMatch = 0; $countryMatch = 0; if ( $ruleCountry == '*' ) $countryMatch = 1; elseif ( $ruleCountry == $country ) $countryMatch = 2; if ( !$ruleCatIDs ) $categoryMatch = 1; elseif ( in_array( $catID, $ruleCatIDs ) ) $categoryMatch = 2; if ( $countryMatch && $categoryMatch ) $vatPriority = $countryMatch * 2 + $categoryMatch - 2; else $vatPriority = 0; if ( !isset( $vatPriorities[$vatPriority] ) ) $vatPriorities[$vatPriority] = $ruleVatID; } krsort( $vatPriorities, SORT_NUMERIC ); $bestPriority = 0; if ( $vatPriorities ) { $tmpKeys = array_keys( $vatPriorities ); $bestPriority = array_shift( $tmpKeys ); } if ( $bestPriority == 0 ) { eZDebug::writeError( "Cannot find a suitable VAT type " . "for country '" . $country . "'" . " and category '" . $productCategory->attribute( 'name' ). "'." ); return new eZVatType( array( "id" => 0, "name" => ezpI18n::tr( 'kernel/shop', 'None' ), "percentage" => 0.0 ) ); } $bestVatTypeID = array_shift( $vatPriorities ); $bestVatType = eZVatType::fetch( $bestVatTypeID ); eZDebug::writeDebug( sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)", $country, $productCategory->attribute( 'name' ), $bestVatType->attribute( 'name' ), $bestVatType->attribute( 'percentage' ) ) ); return $bestVatType; }
$db->begin(); foreach ($ruleIDList as $ruleID) { eZVatRule::removeVatRule($ruleID); } $db->commit(); } if ($http->hasPostVariable("SaveCategoriesButton")) { $db = eZDB::instance(); $db->begin(); foreach ($productCategories as $cat) { $id = $cat->attribute('id'); if (!$http->hasPostVariable("category_name_" . $id)) { continue; } $name = $http->postVariable("category_name_" . $id); $cat->setAttribute('name', $name); $cat->store(); } $db->commit(); return $module->redirectTo($module->functionURI("productcategories")); } $vatRules = eZVatRule::fetchList(); $errors = findErrors($vatRules); usort($vatRules, 'compareVatRules'); $tpl->setVariable('rules', $vatRules); $tpl->setVariable('errors', $errors); $path = array(); $path[] = array('text' => ezpI18n::tr('kernel/shop/vatrules', 'VAT rules'), 'url' => false); $Result = array(); $Result['path'] = $path; $Result['content'] = $tpl->fetch("design:shop/vatrules.tpl");
/** * Check entered data. * * \return list of errors found, or false if the data is ok. */ function checkEnteredData($country, $categories, $vatType, $productCategories, $ruleID) { $errors = false; $errorHeader = ''; /* * Check if the data was entered correctly. */ if (!$country || !is_numeric($vatType)) { $errors = array(); $errorHeader = ezpI18n::tr('kernel/shop/editvatrule', 'Invalid data entered'); if (!$country) { $errors[] = ezpI18n::tr('kernel/shop/editvatrule', 'Choose a country.'); } if (!is_numeric($vatType)) { $errors[] = ezpI18n::tr('kernel/shop/editvatrule', 'Choose a VAT type.'); } return array($errorHeader, $errors); } /* * Check if the rule we're about to create * conflicts with existing ones. */ $errorHeader = ezpI18n::tr('kernel/shop/editvatrule', 'Conflicting rule'); $vatRules = eZVatRule::fetchList(); // If the rule is default one if (!$categories) { // check if default rule already exists. foreach ($vatRules as $i) { if ($i->attribute('id') == $ruleID || $i->attribute('country_code') != $country || $i->attribute('product_categories')) { continue; } if ($country == '*') { $errors[] = ezpI18n::tr('kernel/shop/editvatrule', 'Default rule for any country already exists.'); } else { $errorMessage = "Default rule for country '%1' already exists."; $errors[] = ezpI18n::tr('kernel/shop/editvatrule', $errorMessage, null, array($country)); } break; } } else { // check if there are already rules defined for the same country // containing some of the categories. foreach ($vatRules as $i) { if ($i->attribute('id') == $ruleID || $i->attribute('country_code') != $country || !$i->attribute('product_categories')) { continue; } $intersection = array_intersect($categories, $i->attribute('product_categories_ids')); if (!$intersection) { continue; } // Construct string containing name of all the conflicting categories. $intersectingCategories = array(); foreach ($productCategories as $cat) { if (array_search($cat->attribute('id'), $intersection) !== false) { $intersectingCategories[] = $cat->attribute('name'); } } $intersectingCategories = join(', ', $intersectingCategories); if ($country == '*') { $errorMessage = "There is already a rule defined for any country containing the following categories: %2."; } else { $errorMessage = "There is already a rule defined for country '%1' containing the following categories: %2."; } $errors[] = ezpI18n::tr('kernel/shop/editvatrule', $errorMessage, null, array($country, $intersectingCategories)); } } return array($errorHeader, $errors); }