public function delete() { $id = $this->id; parent::delete(); // remove associated zip codes & tax rule return County::deleteZipCodeByIdCounty($id) and TaxRule::deleteTaxRuleByIdCounty($id); }
protected function _onStatusChange() { if (!$this->active) { return TaxRule::deleteTaxRuleByIdTax($this->id); } return true; }
/** * Fetch the template for action enable * * @param string $token * @param int $id * @param int $value state enabled or not * @param string $active status * @param int $id_category * @param int $id_product */ public function displayEnableLink($token, $id, $value, $active, $id_category = null, $id_product = null) { if ($value && TaxRule::isTaxInUse($id)) { $confirm = $this->l('This tax is currently in use as a tax rule. If you continue, this tax will be removed from the tax rule. Are you sure you\'d like to continue?', null, true, false); } $tpl_enable = $this->context->smarty->createTemplate('helpers/list/list_action_enable.tpl'); $tpl_enable->assign(array('enabled' => (bool) $value, 'url_enable' => self::$currentIndex . '&' . $this->identifier . '=' . (int) $id . '&' . $active . $this->table . ((int) $id_category && (int) $id_product ? '&id_category=' . (int) $id_category : '') . '&token=' . ($token != null ? $token : $this->token), 'confirm' => isset($confirm) ? $confirm : null)); return $tpl_enable->fetch(); }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME, * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. * Defaults to TableMap::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['CriteriaSearchCategoryTaxRule'][$this->getPrimaryKey()])) { return '*RECURSION*'; } $alreadyDumpedObjects['CriteriaSearchCategoryTaxRule'][$this->getPrimaryKey()] = true; $keys = CriteriaSearchCategoryTaxRuleTableMap::getFieldNames($keyType); $result = array($keys[0] => $this->getId(), $keys[1] => $this->getCategoryId(), $keys[2] => $this->getTaxRuleId()); $virtualColumns = $this->virtualColumns; foreach ($virtualColumns as $key => $virtualColumn) { $result[$key] = $virtualColumn; } if ($includeForeignObjects) { if (null !== $this->aCategory) { $result['Category'] = $this->aCategory->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aTaxRule) { $result['TaxRule'] = $this->aTaxRule->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
/** * Check if the tax rule could be added in the database * * @param TaxRule $tr * * @return array */ protected function validateTaxRule(TaxRule $tr) { // @TODO: check if the rule already exists return $tr->validateController(); }
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; }
private function generateEuropeTaxRule() { $euro_vat_array = self::$europe_vat_array; $euro_tax_rule_grp_id = TaxRulesGroup::getIdByName((string) $this->european_vat_name); if (!$euro_tax_rule_grp_id) { // Create it $trg = new TaxRulesGroup(); $trg->name = (string) $this->european_vat_name; $trg->active = 1; if (!$trg->save()) { $this->_errors[] = Tools::displayError('Tax rule cannot be saved.'); return false; } $euro_tax_rule_grp_id = (int) $trg->id; } $tax_rules_euro_group = TaxRule::getTaxRulesByGroupId((int) Context::getContext()->language->id, (int) $euro_tax_rule_grp_id); $euro_group_taxes_rules = array(); foreach ($tax_rules_euro_group as $tax_rule) { $euro_group_taxes_rules[] = $tax_rule; } foreach ($euro_vat_array as $euro_vat_name => $euro_vat_details) { $posted_euro_vat = 'euro_vat_' . (string) $euro_vat_details['iso_country']; $posted_available_vat = 'available_vat_' . (string) $euro_vat_details['iso_country']; $country_id = Country::getByIso((string) $euro_vat_details['iso_country']); if (Tools::isSubmit($posted_euro_vat)) { if (!Tools::isSubmit($posted_available_vat)) { $id_tax_found = Tax::getTaxIdByName((string) $euro_vat_name); if ($id_tax_found !== false) { $tax = new Tax((int) $id_tax_found); } else { $tax = new Tax(); } $tax->name[(int) Configuration::get('PS_LANG_DEFAULT')] = (string) $euro_vat_name; $tax->rate = (double) $euro_vat_details['rate']; $tax->active = 1; if ($tax->validateFields(false, true) !== true || $tax->validateFieldsLang(false, true) !== true) { $this->_errors[] = Tools::displayError('Invalid tax properties.'); continue; } if (!$tax->save()) { $this->_errors[] = Tools::displayError('An error occurred while saving the tax: '); continue; } $id_tax_rule = $this->getTaxRuleIdFromUnique($euro_tax_rule_grp_id, $country_id, $id_tax_found); if ($id_tax_rule !== false) { $tr = new TaxRule((int) $id_tax_rule); } else { $tr = new TaxRule(); } $tr->id_tax_rules_group = (int) $euro_tax_rule_grp_id; $tr->id_country = (int) $country_id; $tr->id_state = 0; $tr->id_county = 0; $tr->zipcode_from = 0; $tr->zipcode_to = 0; $tr->behavior = 0; $tr->description = ''; $tr->id_tax = (int) $tax->id; $tr->save(); } else { $assoc_id_tax = (int) Tools::getValue($posted_available_vat); $id_tax_rule = $this->getTaxRuleIdFromUnique($euro_tax_rule_grp_id, $country_id, $assoc_id_tax); if ($id_tax_rule !== false) { $tr = new TaxRule((int) $id_tax_rule); } else { $tr = new TaxRule(); } $tr->id_tax_rules_group = (int) $euro_tax_rule_grp_id; $tr->id_country = (int) $country_id; $tr->id_state = 0; $tr->id_county = 0; $tr->zipcode_from = 0; $tr->zipcode_to = 0; $tr->behavior = 0; $tr->description = ''; $tr->id_tax = (int) $assoc_id_tax; $tr->save(); } } else { $this->_errors[] = Tools::displayError('Invalid parameters received'); } } }
public function getById($id) { return TaxRule::findOrFail($id); }
private function createParams() { $product_types = array(); $public_token = Tools::getValue('TAXAMOEUVAT_TOKENPUBLIC', Configuration::get('TAXAMOEUVAT_TOKENPUBLIC')); $res_api_product_types = $this->getResApi('https://api.taxamo.com/api/v1/dictionaries/product_types', 'GET', array('public_token' => $public_token)); if ($res_api_product_types && isset($res_api_product_types['dictionary'])) { $res_product_types_dictionary = $res_api_product_types['dictionary']; foreach ($res_product_types_dictionary as $product_type) { if (isset($product_type['code'])) { $product_types[] = $product_type['code']; } } if (count($product_types) <= 0) { $this->_html .= $this->displayError($this->l('Error In Product Types Dictionary')); return false; } } else { return false; } @set_time_limit(0); $generic_name = Tools::getValue('TAXAMOEUVAT_GENERICNAME', Configuration::get('TAXAMOEUVAT_GENERICNAME')); $params_tax = array(); $res_api_countries = $this->getResApi('https://api.taxamo.com/api/v1/dictionaries/countries', 'GET', array('tax_supported' => 'true', 'public_token' => $public_token)); if ($res_api_countries && isset($res_api_countries['dictionary'])) { $res_countries_dictionary = $res_api_countries['dictionary']; foreach ($res_countries_dictionary as $country) { if (isset($country['tax_supported'], $country['cca2']) && (bool) $country['tax_supported']) { $id_country = Country::getByIso($country['cca2']); if (!$id_country) { continue; } foreach ($product_types as $product_type) { $res_api_calculate = $this->getResApi('https://api.taxamo.com/api/v1/tax/calculate', 'GET', array('public_token' => $public_token, 'currency_code' => 'EUR', 'force_country_code' => $country['cca2'], 'buyer_tax_number' => null, 'product_type' => $product_type, 'amount' => 100)); if ($res_api_calculate && isset($res_api_calculate['transaction']['transaction_lines'][0])) { $res_tax = $res_api_calculate['transaction']['transaction_lines'][0]; $tax_name = $generic_name . ' ' . $country['cca2'] . ' ' . $product_type . ' ' . trim((string) $res_tax['tax_rate']) . '%'; $params_tax[] = array('name' => $tax_name, 'rate' => $res_tax['tax_rate'], 'active' => 1, 'product_type' => $product_type, 'id_country' => $id_country); } } } } } foreach ($params_tax as $key => $tax_values) { if (!Validate::isGenericName($tax_values['name'])) { continue; } if ($id_tax = Tax::getTaxIdByName($tax_values['name'])) { $params_tax[$key]['id_tax'] = $id_tax; continue; } $tax = new Tax(); $tax->name[(int) Configuration::get('PS_LANG_DEFAULT')] = (string) $tax_values['name']; $tax->rate = (double) $tax_values['rate']; $tax->active = 1; if (($error = $tax->validateFields(false, true)) !== true || ($error = $tax->validateFieldsLang(false, true)) !== true) { $this->_html .= $this->displayError('Invalid tax properties.') . ' ' . $error; return false; } if (!$tax->add()) { $this->_html .= $this->displayError('An error occurred while importing the tax: ') . (string) $tax_values['name']; return false; } $params_tax[$key]['id_tax'] = $tax->id; } foreach ($product_types as $product_type) { $tax_group_name = $generic_name . ' - ' . $product_type; if (!Validate::isGenericName($tax_group_name)) { continue; } if (TaxRulesGroup::getIdByName($tax_group_name)) { $this->_html .= $this->displayError('This tax rule group cannot be saved, exists.'); return false; } $trg = new TaxRulesGroup(); $trg->name = $tax_group_name; $trg->active = 1; if (!$trg->save()) { $this->_html .= $this->displayError('This tax rule group cannot be saved.'); return false; } foreach ($params_tax as $tax_values) { if ($tax_values['product_type'] == $product_type) { // Creation $tr = new TaxRule(); $tr->id_tax_rules_group = $trg->id; $tr->id_country = $tax_values['id_country']; $tr->id_state = 0; $tr->id_county = 0; $tr->zipcode_from = 0; $tr->zipcode_to = 0; $tr->behavior = 0; $tr->description = ''; $tr->id_tax = $tax_values['id_tax']; $tr->save(); } } } return true; }
public static function taxamoCreateParams() { $res_craete_params = array('error' => null, 'success' => false); $product_types = array(); $public_token = Tools::getValue('TAXAMOEUVAT_TOKENPUBLIC', Configuration::get('TAXAMOEUVAT_TOKENPUBLIC')); $res_api_product_types = self::getResApi('https://api.taxamo.com/api/v1/dictionaries/product_types', 'GET', array('public_token' => $public_token)); if ($res_api_product_types && isset($res_api_product_types['dictionary'])) { $res_product_types_dictionary = $res_api_product_types['dictionary']; foreach ($res_product_types_dictionary as $product_type) { if (isset($product_type['code'])) { $product_types[] = $product_type['code']; } } if (count($product_types) <= 0) { $res_craete_params['error'] = 'Error In Product Types Dictionary'; return $res_craete_params; } } else { $res_craete_params['error'] = 'Error In API Product Types Dictionary'; return $res_craete_params; } @set_time_limit(0); $generic_name = Tools::getValue('TAXAMOEUVAT_GENERICNAME', Configuration::get('TAXAMOEUVAT_GENERICNAME')); $params_tax = array(); $res_api_countries = self::getResApi('https://api.taxamo.com/api/v1/dictionaries/countries', 'GET', array('tax_supported' => 'true', 'public_token' => $public_token)); if ($res_api_countries && isset($res_api_countries['dictionary'])) { $res_countries_dictionary = $res_api_countries['dictionary']; foreach ($res_countries_dictionary as $country) { if (isset($country['tax_supported'], $country['cca2']) && (bool) $country['tax_supported']) { $id_country = Country::getByIso($country['cca2']); if (!$id_country) { // continue; $res_craete_params['error'] = 'Error In Procedure Countries'; return $res_craete_params; } foreach ($product_types as $product_type) { $res_api_calculate = self::getResApi('https://api.taxamo.com/api/v1/tax/calculate', 'GET', array('public_token' => $public_token, 'currency_code' => 'EUR', 'force_country_code' => $country['cca2'], 'buyer_tax_number' => null, 'product_type' => $product_type, 'amount' => 100)); if ($res_api_calculate && isset($res_api_calculate['transaction']['transaction_lines'][0])) { $res_tax = $res_api_calculate['transaction']['transaction_lines'][0]; $tax_name = $generic_name . ' ' . $country['cca2'] . ' ' . $product_type . ' ' . trim((string) $res_tax['tax_rate']) . '%'; $params_tax[] = array('name' => $tax_name, 'rate' => $res_tax['tax_rate'], 'active' => 1, 'product_type' => $product_type, 'id_country' => $id_country); } else { $res_craete_params['error'] = 'Error In API Tax Calculate'; return $res_craete_params; } } } } if (count($params_tax) <= 0) { $res_craete_params['error'] = 'Error In Countries Dictionary'; return $res_craete_params; } } else { $res_craete_params['error'] = 'Error In API Countries Dictionary'; return $res_craete_params; } foreach ($params_tax as $key => $tax_values) { if (!Validate::isGenericName($tax_values['name'])) { // continue; $res_craete_params['error'] = 'Error In Procedure Tax'; return $res_craete_params; } if (!($id_tax = Tax::getTaxIdByName($tax_values['name'], 1))) { $id_tax = Tax::getTaxIdByName($tax_values['name'], 0); } if ($id_tax) { $tax = new Tax($id_tax); if ($tax->rate != (double) $tax_values['rate'] || $tax->active != 1) { $tax->rate = (double) $tax_values['rate']; $tax->active = 1; if (($error = $tax->validateFields(false, true)) !== true || ($error = $tax->validateFieldsLang(false, true)) !== true) { $res_craete_params['error'] = 'Invalid tax properties (update). ' . $error; return $res_craete_params; } if (!$tax->update()) { $res_craete_params['error'] = 'An error occurred while updating the tax: ' . (string) $tax_values['name']; return $res_craete_params; } } $params_tax[$key]['id_tax'] = $id_tax; } else { $tax = new Tax(); $tax->name[(int) Configuration::get('PS_LANG_DEFAULT')] = (string) $tax_values['name']; $tax->rate = (double) $tax_values['rate']; $tax->active = 1; if (($error = $tax->validateFields(false, true)) !== true || ($error = $tax->validateFieldsLang(false, true)) !== true) { $res_craete_params['error'] = 'Invalid tax properties (add). ' . $error; return $res_craete_params; } if (!$tax->add()) { $res_craete_params['error'] = 'An error occurred while importing the tax: ' . (string) $tax_values['name']; return $res_craete_params; } $params_tax[$key]['id_tax'] = $tax->id; } } foreach ($product_types as $product_type) { $tax_group_name = $generic_name . ' - ' . $product_type; if (!Validate::isGenericName($tax_group_name)) { // continue; $res_craete_params['error'] = 'Error In Procedure Product Type'; return $res_craete_params; } if ($id_tax_rules_group = TaxRulesGroup::getIdByName($tax_group_name)) { $trg = new TaxRulesGroup($id_tax_rules_group); if ($trg->active != 1) { $trg->active = 1; if (!$trg->update()) { $res_craete_params['error'] = 'An error occurred while updating the tax rule group: ' . (string) $tax_values['name']; return $res_craete_params; } } $is_new_record = false; } else { $trg = new TaxRulesGroup(); $trg->name = $tax_group_name; $trg->active = 1; if (!$trg->add()) { $res_craete_params['error'] = 'An error occurred while importing the tax rule group: ' . (string) $tax_values['name']; return $res_craete_params; } $is_new_record = true; } foreach ($params_tax as $tax_values) { if ($tax_values['product_type'] == $product_type) { if ($is_new_record) { // Creation $tr = new TaxRule(); $tr->id_tax_rules_group = $trg->id; $tr->id_country = $tax_values['id_country']; $tr->id_state = 0; $tr->id_county = 0; $tr->zipcode_from = 0; $tr->zipcode_to = 0; $tr->behavior = 0; $tr->description = ''; $tr->id_tax = $tax_values['id_tax']; $tr->save(); } } } } $res_craete_params['success'] = true; return $res_craete_params; }
protected function _displayEnableLink($token, $id, $value, $active, $id_category = NULL, $id_product = NULL) { global $currentIndex; $confirm = $value && TaxRule::isTaxInUse($id) ? 'onclick="return confirm(\'' . $this->l('This tax is currently in use in a tax rule. If you continue this tax will be removed from the tax rule, are you sure you want to continue?') . '\')"' : ''; echo '<a href="' . $currentIndex . '&' . $this->identifier . '=' . $id . '&' . $active . ((int) $id_category && (int) $id_product ? '&id_category=' . (int) $id_category : '') . '&token=' . ($token != null ? $token : $this->token) . '" ' . $confirm . '> <img src="../img/admin/' . ($value ? 'enabled.gif' : 'disabled.gif') . '" alt="' . ($value ? $this->l('Enabled') : $this->l('Disabled')) . '" title="' . ($value ? $this->l('Enabled') : $this->l('Disabled')) . '" /></a>'; }
/** * 自分自身と Target を比較し, ソートのための数値を返す. * * 以下の順で比較し、 * * 同一であれば 0 * 自分の方が大きければ正の整数 * 小さければ負の整数を返す. * * 1. apply_date * 2. rank * * このメソッドは usort() 関数などで使用する. * * @param TaxRule $Target 比較対象の TaxRule * @return integer */ public function compareTo(TaxRule $Target) { if ($this->getApplyDate() == $Target->getApplyDate()) { if ($this->getRank() == $Target->getRank()) { return 0; } if ($this->getRank() > $Target->getRank()) { return -1; } else { return 1; } } else { if ($this->getApplyDate() > $Target->getApplyDate()) { return -1; } else { return 1; } } }
/** * @param $module * @return mixed */ protected static function getTaxRules($module) { $taxRules = array(); $taxRuleGroups = TaxRulesGroup::getTaxRulesGroups(true); foreach ($taxRuleGroups as $taxRuleGroup) { /** @var TaxCore $taxItem */ $taxItem = ShopgateSettings::getTaxItemByTaxRuleGroupId($taxRuleGroup['id_tax_rules_group']); $rule = array('id' => $taxRuleGroup['id_tax_rules_group'], 'name' => $taxRuleGroup['name'], 'priority' => 0); $rule['product_tax_classes'] = array(array('id' => $taxItem->id, 'key' => is_array($taxItem->name) ? reset($taxItem->name) : '')); $rule['customer_tax_classes'] = array(array('key' => 'default', 'is_default' => true)); $rule['tax_rates'] = array(); if (version_compare(_PS_VERSION_, '1.5.0.1', '<')) { $taxRulesPrestashop = TaxRule::getTaxRulesByGroupId($taxRuleGroup['id_tax_rules_group']); } else { $taxRulesPrestashop = TaxRule::getTaxRulesByGroupId($module->context->language->id, $taxRuleGroup['id_tax_rules_group']); } foreach ($taxRulesPrestashop as $idCountry => $taxRuleItem) { if (version_compare(_PS_VERSION_, '1.5.0.1', '>=')) { $taxRuleItem = new TaxRule($taxRuleItem['id_tax_rule']); $idTax = $taxRuleItem->id_tax; $idCountry = $taxRuleItem->id_country; $idState = $taxRuleItem->id_state; } else { $idTax = self::getTaxIdFromTaxRule($taxRuleItem); $idState = key($taxRuleItem); } /** @var TaxCore $taxItem */ $taxItem = new Tax($idTax, $module->context->language->id); $country = Country::getIsoById($idCountry); $stateModel = new State($idState); $state = $stateModel->iso_code; $resultTaxRate = array(); $resultTaxRate['key'] = self::getTaxRateKey($taxItem, $country, $state); //Fix for 1.4.x.x the taxes were exported multiple if (self::arrayValueExists('key', $resultTaxRate['key'], $rule['tax_rates'])) { continue; } $rule['tax_rates'][] = $resultTaxRate; } if ($taxItem->active && Configuration::get('PS_TAX') == 1) { $taxRules[] = $rule; } } return $taxRules; }
protected function processCreateRule() { $zip_code = Tools::getValue('zipcode'); $id_rule = (int) Tools::getValue('id_tax_rule'); $id_tax = (int) Tools::getValue('id_tax'); $id_tax_rules_group = (int) Tools::getValue('id_tax_rules_group'); $id_customer_group = (int) Tools::getValue('customer_group'); $behavior = (int) Tools::getValue('behavior'); $description = pSQL(Tools::getValue('description')); if ((int) ($id_country = Tools::getValue('country')) == 0) { $countries = Country::getCountries($this->context->language->id); $this->selected_countries = array(); foreach ($countries as $country) { $this->selected_countries[] = (int) $country['id_country']; } } else { $this->selected_countries = array($id_country); } $this->selected_states = Tools::getValue('states'); if (empty($this->selected_states) || count($this->selected_states) == 0) { $this->selected_states = array(0); } $tax_rules_group = new TaxRulesGroup((int) $id_tax_rules_group); foreach ($this->selected_countries as $id_country) { $first = true; foreach ($this->selected_states as $id_state) { if ($tax_rules_group->hasUniqueTaxRuleForCountry($id_country, $id_state, $id_rule)) { $this->errors[] = Tools::displayError('A tax rule already exists for this country/state with tax only behavior.'); continue; } $tr = new TaxRule(); // update or creation? if (isset($id_rule) && $first) { $tr->id = $id_rule; $first = false; } $tr->id_tax = $id_tax; $tax_rules_group = new TaxRulesGroup((int) $id_tax_rules_group); $tr->id_tax_rules_group = (int) $tax_rules_group->id; $tr->id_country = (int) $id_country; $tr->id_state = (int) $id_state; $tr->id_group = (int) $id_customer_group; list($tr->zipcode_from, $tr->zipcode_to) = $tr->breakDownZipCode($zip_code); // Construct Object Country $country = new Country((int) $id_country, (int) $this->context->language->id); if ($zip_code && $country->need_zip_code) { if ($country->zip_code_format) { foreach (array($tr->zipcode_from, $tr->zipcode_to) as $zip_code) { if ($zip_code) { if (!$country->checkZipCode($zip_code)) { $this->errors[] = sprintf(Tools::displayError('The Zip/postal code is invalid. It must be typed as follows: %s for %s.'), str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format))), $country->name); } } } } } $tr->behavior = (int) $behavior; $tr->description = $description; $this->tax_rule = $tr; $_POST['id_state'] = $tr->id_state; $this->errors = array_merge($this->errors, $this->validateTaxRule($tr)); if (count($this->errors) == 0) { $tax_rules_group = $this->updateTaxRulesGroup($tax_rules_group); $tr->id = (int) $tax_rules_group->getIdTaxRuleGroupFromHistorizedId((int) $tr->id); $tr->id_tax_rules_group = (int) $tax_rules_group->id; if (!$tr->save()) { $this->errors[] = Tools::displayError('An error has occurred: Cannot save the current tax rule.'); } } } } if (count($this->errors) == 0) { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . (int) $tax_rules_group->id . '&conf=4&update' . $this->table . '&token=' . $this->token); } else { $this->display = 'edit'; } }
public function hasUniqueTaxRuleForCountry($id_country, $id_state, $id_tax_rule = false) { $rules = TaxRule::getTaxRulesByGroupId((int) Context::getContext()->language->id, (int) $this->id); foreach ($rules as $rule) { if ($rule['id_country'] == $id_country && $id_state == $rule['id_state'] && !$rule['behavior'] && (int) $id_tax_rule != $rule['id_tax_rule']) { return true; } } return false; }
protected function afterUpdate($object) { global $cookie; TaxRule::deleteByGroupId($object->id); foreach (Country::getCountries($cookie->id_lang, true) as $country) { $id_tax = (int) Tools::getValue('tax_' . $country['id_country'] . '_0'); // default country rule if (!empty($id_tax)) { $tr = new TaxRule(); $tr->id_tax_rules_group = $object->id; $tr->id_country = (int) $country['id_country']; $tr->id_state = 0; $tr->id_county = 0; $tr->id_tax = $id_tax; $tr->state_behavior = 0; $tr->county_behavior = 0; $tr->save(); } // state specific rule if (!empty($country['contains_states'])) { foreach ($country['states'] as $state) { $state_behavior = (int) Tools::getValue('behavior_state_' . $state['id_state']); if ($state_behavior != PS_PRODUCT_TAX) { $tr = new TaxRule(); $tr->id_tax_rules_group = $object->id; $tr->id_country = (int) $country['id_country']; $tr->id_state = (int) $state['id_state']; $tr->id_county = 0; $tr->id_tax = (int) Tools::getValue('tax_' . $country['id_country'] . '_' . $state['id_state']); $tr->state_behavior = $state_behavior; $tr->county_behavior = 0; $tr->save(); } // county specific rule if (State::hasCounties($state['id_state'])) { $counties = County::getCounties($state['id_state']); foreach ($counties as $county) { $county_behavior = (int) Tools::getValue('behavior_county_' . $county['id_county']); if ($county_behavior != County::USE_STATE_TAX) { $tr = new TaxRule(); $tr->id_tax_rules_group = $object->id; $tr->id_country = (int) $country['id_country']; $tr->id_state = (int) $state['id_state']; $tr->id_county = (int) $county['id_county']; $tr->id_tax = (int) Tools::getValue('tax_' . $country['id_country'] . '_' . $state['id_state'] . '_' . $county['id_county']); $tr->state_behavior = 0; $tr->county_behavior = $county_behavior; $tr->save(); } } } } } } }
/** * @param SimpleXMLElement $xml * @return bool * @throws PrestaShopException */ protected function _installTaxes($xml) { if (isset($xml->taxes->tax)) { $assoc_taxes = array(); foreach ($xml->taxes->tax as $taxData) { /** @var SimpleXMLElement $taxData */ $attributes = $taxData->attributes(); if ($id_tax = Tax::getTaxIdByName($attributes['name'])) { $assoc_taxes[(int) $attributes['id']] = $id_tax; continue; } $tax = new Tax(); $tax->name[(int) Configuration::get('PS_LANG_DEFAULT')] = (string) $attributes['name']; $tax->rate = (double) $attributes['rate']; $tax->active = 1; if (($error = $tax->validateFields(false, true)) !== true || ($error = $tax->validateFieldsLang(false, true)) !== true) { $this->_errors[] = Tools::displayError('Invalid tax properties.') . ' ' . $error; return false; } if (!$tax->add()) { $this->_errors[] = Tools::displayError('An error occurred while importing the tax: ') . (string) $attributes['name']; return false; } $assoc_taxes[(int) $attributes['id']] = $tax->id; } foreach ($xml->taxes->taxRulesGroup as $group) { /** @var SimpleXMLElement $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) { /** @var SimpleXMLElement $rule */ $rule_attributes = $rule->attributes(); // Validation if (!isset($rule_attributes['iso_code_country'])) { continue; } $id_country = (int) 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; $zipcode_from = 0; $zipcode_to = 0; $behavior = $rule_attributes['behavior']; if (isset($rule_attributes['zipcode_from'])) { $zipcode_from = $rule_attributes['zipcode_from']; if (isset($rule_attributes['zipcode_to'])) { $zipcode_to = $rule_attributes['zipcode_to']; } } // 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->zipcode_from = $zipcode_from; $tr->zipcode_to = $zipcode_to; $tr->behavior = $behavior; $tr->description = ''; $tr->id_tax = $assoc_taxes[strval($rule_attributes['id_tax'])]; $tr->save(); } } } return true; }
protected function importTaxRules() { $this->truncateTables(array('tax_rule')); $handle = $this->openCsvFile('tax_rules.csv'); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) { $res = false; $fields = $this->filterFields('TaxRule', $this->tax_rule_fields, $line); if (!isset($fields['id'])) { $tax_rule = new TaxRule($line[0]); $tax_rule->id = $line[0]; } else { $tax_rule = new TaxRule($fields['id']); } foreach ($fields as $key => $field) { $tax_rule->{$key} = $field; } $tax_rule->force_id = true; if (!$res) { $res = $tax_rule->add(); } } $this->closeCsvFile($handle); return true; }