/**
  * Remove the given category and all references to it.
  *
  * \public
  * \static
  */
 static function removeByID($id)
 {
     $id = (int) $id;
     $db = eZDB::instance();
     $db->begin();
     // Delete references to the category from VAT charging rules.
     eZVatRule::removeReferencesToProductCategory($id);
     // Reset product category attribute for all products
     // that have been referencing the category.
     $ini = eZINI::instance('shop.ini');
     if ($ini->hasVariable('VATSettings', 'ProductCategoryAttribute') && ($categoryAttrName = $ini->variable('VATSettings', 'ProductCategoryAttribute'))) {
         $categoryAttrName = $db->escapeString($categoryAttrName);
         $query = "SELECT coa.id FROM ezcontentobject_attribute coa, ezcontentclass_attribute cca, ezcontentobject co " . "WHERE " . " cca.id=coa.contentclassattribute_id " . " AND coa.contentobject_id=co.id " . " AND cca.data_type_string='ezproductcategory' " . " AND cca.identifier='{$categoryAttrName}' " . " AND coa.version=co.current_version " . " AND coa.data_int={$id}";
         $rows = $db->arrayQuery($query);
         foreach ($rows as $row) {
             $query = "UPDATE ezcontentobject_attribute SET data_int=0, sort_key_int=0 WHERE id=" . (int) $row['id'];
             $db->query($query);
         }
     }
     // Remove the category itself.
     eZPersistentObject::removeObject(eZProductCategory::definition(), array("id" => $id));
     $db->commit();
 }
 /**
  * Remove given VAT charging rule.
  */
 static function removeVatRule($id)
 {
     $db = eZDB::instance();
     $db->begin();
     // Remove product categories associated with the rule.
     eZVatRule::removeProductCategories($id);
     // Remove the rule itself.
     eZPersistentObject::removeObject(eZVatRule::definition(), array("id" => $id));
     $db->commit();
 }
 $errors = applyChanges($module, $http);
 if (!$module->hasActionParameter('CategoryIDList')) {
     $catIDList = array();
 } else {
     $catIDList = $module->actionParameter('CategoryIDList');
 }
 if ($catIDList) {
     // Find dependencies for the categories being removed.
     $deps = array();
     foreach ($catIDList as $catID) {
         $category = eZProductCategory::fetch($catID);
         if (!is_object($category)) {
             continue;
         }
         $catName = $category->attribute('name');
         $dependantRulesCount = eZVatRule::fetchCountByCategory($catID);
         $dependantProductsCount = eZProductCategory::fetchProductCountByCategory($catID);
         $deps[$catID] = array('name' => $catName, 'affected_rules_count' => $dependantRulesCount, 'affected_products_count' => $dependantProductsCount);
     }
     // Skip the confirmation dialog if the categories
     // being removed have no conflicts.
     $haveDeps = false;
     foreach ($deps as $dep) {
         if ($dep['affected_rules_count'] > 0 || $dep['affected_products_count'] > 0) {
             $haveDeps = true;
         }
     }
     // Show the confirmation dialog.
     if ($haveDeps) {
         $tpl->setVariable('dependencies', $deps);
         $tpl->setVariable('category_ids', join(',', $catIDList));
Beispiel #4
0
    $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");
Beispiel #5
0
 /**
  * Remove given VAT type and all references to it.
  *
  * Drops VAT charging rules referencing the VAT type.
  * Resets VAT type in associated products to its default value for a product class.
  *
  * \param $vatID id of VAT type to remove.
  * \public
  * \static
  */
 function removeThis()
 {
     $vatID = $this->ID;
     $db = eZDB::instance();
     $db->begin();
     // remove dependent VAT rules
     $dependentRules = eZVatRule::fetchByVatType($vatID);
     foreach ($dependentRules as $rule) {
         eZVatRule::removeVatRule($rule->attribute('id'));
     }
     // replace VAT type in dependent products.
     eZVatType::resetToDefaultInProducts($vatID);
     // Remove the VAT type itself.
     eZPersistentObject::removeObject(eZVatType::definition(), array("id" => $vatID));
     $db->commit();
 }
    /**
     * 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;
    }
/**
 * Determine dependent VAT rules and products for the given VAT types.
 *
 * \private
 */
function findDependencies( $vatTypeIDList, &$deps, &$haveDeps, &$canRemove )
{
    // Find dependencies (products and/or VAT rules).
    $deps = array();
    $haveDeps = false;
    $canRemove = true;
    foreach ( $vatTypeIDList as $vatID )
    {
        $vatType = eZVatType::fetch( $vatID );
        $vatName = $vatType->attribute( 'name' );

        // Find dependent VAT rules.
        $nRules = eZVatRule::fetchCountByVatType( $vatID );

        // Find dependent products.
        $nProducts = eZVatType::fetchDependentProductsCount( $vatID );

        // Find product classes having this VAT type set as default.
        $nClasses = eZVatType::fetchDependentClassesCount( $vatID );

        if ( $nClasses )
            $canRemove = false;

        $deps[$vatID] = array( 'name' => $vatName,
                               'affected_rules_count' => $nRules,
                               'affected_products_count' => $nProducts,
                               'affected_classes_count' => $nClasses );

        if ( !$haveDeps && ( $nRules > 0 || $nProducts > 0 ) )
            $haveDeps = true;
    }
}
/**
 * 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);
}