コード例 #1
0
 protected function _installTaxes($xml)
 {
     if (isset($xml->taxes->tax)) {
         $available_behavior = array(PS_PRODUCT_TAX, PS_STATE_TAX, PS_BOTH_TAX);
         $assoc_taxes = array();
         foreach ($xml->taxes->tax as $taxData) {
             $attributes = $taxData->attributes();
             if (Tax::getTaxIdByName($attributes['name'])) {
                 continue;
             }
             $tax = new Tax();
             $tax->name[(int) Configuration::get('PS_LANG_DEFAULT')] = strval($attributes['name']);
             $tax->rate = (double) $attributes['rate'];
             $tax->active = 1;
             if (!$tax->validateFields()) {
                 $this->_errors[] = Tools::displayError('Invalid tax properties.');
                 return false;
             }
             if (!$tax->add()) {
                 $this->_errors[] = Tools::displayError('An error occurred while importing the tax: ') . strval($attributes['name']);
                 return false;
             }
             $assoc_taxes[(int) $attributes['id']] = $tax->id;
         }
         foreach ($xml->taxes->taxRulesGroup as $group) {
             $group_attributes = $group->attributes();
             if (!Validate::isGenericName($group_attributes['name'])) {
                 continue;
             }
             if (TaxRulesGroup::getIdByName($group['name'])) {
                 continue;
             }
             $trg = new TaxRulesGroup();
             $trg->name = $group['name'];
             $trg->active = 1;
             if (!$trg->save()) {
                 $this->_errors = Tools::displayError('This tax rule cannot be saved.');
                 return false;
             }
             foreach ($group->taxRule as $rule) {
                 $rule_attributes = $rule->attributes();
                 // Validation
                 if (!isset($rule_attributes['iso_code_country'])) {
                     continue;
                 }
                 $id_country = Country::getByIso(strtoupper($rule_attributes['iso_code_country']));
                 if (!$id_country) {
                     continue;
                 }
                 if (!isset($rule_attributes['id_tax']) || !array_key_exists(strval($rule_attributes['id_tax']), $assoc_taxes)) {
                     continue;
                 }
                 // Default values
                 $id_state = (int) isset($rule_attributes['iso_code_state']) ? State::getIdByIso(strtoupper($rule_attributes['iso_code_state'])) : 0;
                 $id_county = 0;
                 $state_behavior = 0;
                 $county_behavior = 0;
                 if ($id_state) {
                     if (isset($rule_attributes['state_behavior']) && in_array($rule_attributes['state_behavior'], $available_behavior)) {
                         $state_behavior = (int) $rule_attributes['state_behavior'];
                     }
                     if (isset($rule_attributes['county_name'])) {
                         $id_county = County::getIdCountyByNameAndIdState($rule_attributes['county_name'], (int) $id_state);
                         if (!$id_county) {
                             continue;
                         }
                     }
                     if (isset($rule_attributes['county_behavior']) && in_array($rule_attributes['state_behavior'], $available_behavior)) {
                         $county_behavior = (int) $rule_attributes['county_behavior'];
                     }
                 }
                 // Creation
                 $tr = new TaxRule();
                 $tr->id_tax_rules_group = $trg->id;
                 $tr->id_country = $id_country;
                 $tr->id_state = $id_state;
                 $tr->id_county = $id_county;
                 $tr->state_behavior = $state_behavior;
                 $tr->county_behavior = $county_behavior;
                 $tr->id_tax = $assoc_taxes[strval($rule_attributes['id_tax'])];
                 $tr->save();
             }
         }
     }
     return true;
 }