/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param ConnectionInterface $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(ConnectionInterface $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their corresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aCategory !== null) { if ($this->aCategory->isModified() || $this->aCategory->isNew()) { $affectedRows += $this->aCategory->save($con); } $this->setCategory($this->aCategory); } if ($this->aTaxRule !== null) { if ($this->aTaxRule->isModified() || $this->aTaxRule->isNew()) { $affectedRows += $this->aTaxRule->save($con); } $this->setTaxRule($this->aTaxRule); } if ($this->isNew() || $this->isModified()) { // persist changes if ($this->isNew()) { $this->doInsert($con); } else { $this->doUpdate($con); } $affectedRows += 1; $this->resetModified(); } $this->alreadyInSave = false; } return $affectedRows; }
/** * @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 _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'); } } }
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(); } } } } } } }
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; }
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; }