/** * creates of a dejala carrier corresponding to $dejalaProduct */ public static function createDejalaCarrier($dejalaConfig, $dejalaProduct) { // MFR091130 - get id zone from the country used in the module (if the store zones were customized) - default is 1 (Europe) $id_zone = 1; $moduleCountryIsoCode = strtoupper($dejalaConfig->country); $countryID = Country::getByIso($moduleCountryIsoCode); if (intval($countryID)) { $id_zone = Country::getIdZone($countryID); } $vatRate = floatval($dejalaProduct['vat']); // MFR091130 - get or create the tax & attach it to our zone if needed $id_tax = Tax::getTaxIdByRate($vatRate); if (!$id_tax) { $tax = new Tax(); $tax->rate = $vatRate; $defaultLanguage = Configuration::get('PS_LANG_DEFAULT'); $tax->name[$defaultLanguage] = $tax->rate . '%'; $tax->add(); $id_tax = Tax::getTaxIdByRate($vatRate); } if (!Tax::zoneHasTax($id_tax, $id_zone)) { // MFR : direct call because $tax->addZone($id_zone) causes errors when called Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'tax_zone` (`id_tax` , `id_zone`) VALUES (' . intval($id_tax) . ', ' . intval($id_zone) . ')'); } $carrier = new Carrier(); $carrier->name = 'dejala'; $carrier->id_tax = $id_tax; $carrier->url = 'http://tracking.dejala.' . $dejalaConfig->country . '/tracker/@'; $carrier->active = true; $carrier->deleted = 0; $carrier->shipping_handling = false; $carrier->range_behavior = 0; $carrier->is_module = 1; $languages = Language::getLanguages(true); foreach ($languages as $language) { if ($language['iso_code'] == 'fr') { $carrier->delay[$language['id_lang']] = utf8_encode('Quand vous voulez... Par coursier, ' . $dejalaProduct['timelimit'] . 'H'); } if ($language['iso_code'] == 'en') { $carrier->delay[$language['id_lang']] = utf8_encode('When you want... Dispatch rider, ' . $dejalaProduct['timelimit'] . 'H range'); } if ($language['iso_code'] == 'es') { $carrier->delay[$language['id_lang']] = utf8_encode('Cuando quiera... Por mensajero, ' . $dejalaProduct['timelimit'] . 'H'); } } $carrier->add(); $sql = 'INSERT INTO `' . _DB_PREFIX_ . 'carrier_zone` (`id_carrier` , `id_zone`) VALUES (' . intval($carrier->id) . ', ' . intval($id_zone) . ')'; Db::getInstance()->Execute($sql); $rangeW = new RangeWeight(); $rangeW->id_carrier = $carrier->id; $rangeW->delimiter1 = 0; $rangeW->delimiter2 = $dejalaProduct['max_weight']; $rangeW->add(); $vat_factor = 1 + $dejalaProduct['vat'] / 100; $priceTTC = round($dejalaProduct['price'] * $vat_factor + $dejalaProduct['margin'], 2); $priceHT = round($priceTTC / $vat_factor, 2); $priceList = '(NULL' . ',' . $rangeW->id . ',' . $carrier->id . ',' . $id_zone . ',' . $priceHT . ')'; $carrier->addDeliveryPrice($priceList); return new Carrier($carrier->id); }
/** * Return the total of the item, with or without tax * * @param boolean $includeTax Whether or not to include tax * * @return float The total, as a float */ public function total($includeTax = true) { $price = $this->price; if ($includeTax) { $price = $this->tax->add($price); } return (double) ($price * $this->quantity); }
/** * @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; }
public function productImport() { global $cookie; $this->receiveTab(); $handle = $this->openCsvFile(); $defaultLanguageId = intval(Configuration::get('PS_LANG_DEFAULT')); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++) { if (Tools::getValue('convert')) { $this->utf8_encode_array($line); } $info = self::getMaskedRow($line); if (array_key_exists('id', $info) and intval($info['id']) and Product::existsInDatabase(intval($info['id']))) { $product = new Product(intval($info['id'])); if ($product->reduction_from == '0000-00-00') { $product->reduction_from = date('Y-m-d'); } if ($product->reduction_to == '0000-00-00') { $product->reduction_to = date('Y-m-d'); } $categoryData = Product::getIndexedCategories(intval($product->id)); foreach ($categoryData as $tmp) { $product->category[] = $tmp['id_category']; } } else { $product = new Product(); } self::setEntityDefaultValues($product); self::array_walk($info, array('AdminImport', 'fillInfo'), $product); // Find id_tax corresponding to given values for product taxe if (isset($product->tax_rate)) { $product->id_tax = intval(Tax::getTaxIdByRate(floatval($product->tax_rate))); } if (isset($product->tax_rate) and !$product->id_tax) { $tax = new Tax(); $tax->rate = floatval($product->tax_rate); $tax->name = self::createMultiLangField(strval($product->tax_rate)); if (($fieldError = $tax->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $tax->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $tax->add()) { $product->id_tax = intval($tax->id); } else { $this->_errors[] = 'TAX ' . $tax->name[$defaultLanguageId] . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } if (isset($product->manufacturer) and is_numeric($product->manufacturer) and Manufacturer::manufacturerExists(intval($product->manufacturer))) { $product->id_manufacturer = intval($product->manufacturer); } elseif (isset($product->manufacturer) and is_string($product->manufacturer) and !empty($product->manufacturer)) { if ($manufacturer = Manufacturer::getIdByName($product->manufacturer)) { $product->id_manufacturer = intval($manufacturer); } else { $manufacturer = new Manufacturer(); $manufacturer->name = $product->manufacturer; if (($fieldError = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $manufacturer->add()) { $product->id_manufacturer = intval($manufacturer->id); } else { $this->_errors[] = $manufacturer->name . (isset($manufacturer->id) ? ' (' . $manufacturer->id . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } if (isset($product->supplier) and is_numeric($product->supplier) and Supplier::supplierExists(intval($product->supplier))) { $product->id_supplier = intval($product->supplier); } elseif (isset($product->supplier) and is_string($product->supplier) and !empty($product->supplier)) { if ($supplier = Supplier::getIdByName($product->supplier)) { $product->id_supplier = intval($supplier); } else { $supplier = new Supplier(); $supplier->name = $product->supplier; if (($fieldError = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $supplier->add()) { $product->id_supplier = intval($supplier->id); } else { $this->_errors[] = $supplier->name . (isset($supplier->id) ? ' (' . $supplier->id . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } if (isset($product->price_tex) and !isset($product->price_tin)) { $product->price = $product->price_tex; } elseif (isset($product->price_tin) and !isset($product->price_tex)) { $product->price = $product->price_tin; // If a tax is already included in price, withdraw it from price if ($product->tax_rate) { $product->price = floatval(number_format($product->price / (1 + $product->tax_rate / 100), 6)); } } elseif (isset($product->price_tin) and isset($product->price_tex)) { $product->price = $product->price_tex; } if (isset($product->category) and is_array($product->category) and sizeof($product->category)) { $product->id_category = array(); // Reset default values array foreach ($product->category as $value) { if (is_numeric($value)) { if (Category::categoryExists(intval($value))) { $product->id_category[] = intval($value); } else { $categoryToCreate = new Category(); $categoryToCreate->id = intval($value); $categoryToCreate->name = self::createMultiLangField($value); $categoryToCreate->active = 1; $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $categoryToCreate->add()) { $product->id_category[] = intval($categoryToCreate->id); } else { $this->_errors[] = $categoryToCreate->name[$defaultLanguageId] . (isset($categoryToCreate->id) ? ' (' . $categoryToCreate->id . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } elseif (is_string($value) and !empty($value)) { $category = Category::searchByName($defaultLanguageId, $value, true); if ($category['id_category']) { $product->id_category[] = intval($category['id_category']); } else { $categoryToCreate = new Category(); $categoryToCreate->name = self::createMultiLangField($value); $categoryToCreate->active = 1; $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $categoryToCreate->add()) { $product->id_category[] = intval($categoryToCreate->id); } else { $this->_errors[] = $categoryToCreate->name[$defaultLanguageId] . (isset($categoryToCreate->id) ? ' (' . $categoryToCreate->id . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } } } $product->id_category_default = isset($product->id_category[0]) ? intval($product->id_category[0]) : ''; $link_rewrite = is_array($product->link_rewrite) ? $product->link_rewrite[$defaultLanguageId] : ''; $valid_link = Validate::isLinkRewrite($link_rewrite); $bak = $product->link_rewrite; if (isset($product->link_rewrite[$defaultLanguageId]) and empty($product->link_rewrite[$defaultLanguageId]) or !$valid_link) { $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]); } if (!$valid_link) { $this->_warnings[] = Tools::displayError('Rewrited link for') . ' ' . $bak . (isset($info['id']) ? ' (ID ' . $info['id'] . ') ' : '') . ' ' . Tools::displayError('was re-written as') . ' ' . $link_rewrite; } $product->link_rewrite = self::createMultiLangField($link_rewrite); $res = false; $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true); $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true); if ($fieldError === true and $langFieldError === true) { // check quantity if ($product->quantity == NULL) { $product->quantity = 0; } // If id product AND id product already in base, trying to update if ($product->id and Product::existsInDatabase(intval($product->id))) { $datas = Db::getInstance()->getRow('SELECT `date_add` FROM `' . _DB_PREFIX_ . 'product` WHERE `id_product` = ' . intval($product->id)); $product->date_add = pSQL($datas['date_add']); $res = $product->update(); } // If no id_product or update failed if (!$res) { $res = $product->add(); } } // If both failed, mysql error if (!$res) { $this->_errors[] = $info['name'] . (isset($info['id']) ? ' (ID ' . $info['id'] . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } else { if (isset($product->tags) and !empty($product->tags)) { $tag = new Tag(); $array = self::createMultiLangField($product->tags); foreach ($array as $key => $tags) { $a = $tag->addTags($key, $product->id, $tags); } } if (isset($product->image) and is_array($product->image) and sizeof($product->image)) { $productHasImages = (bool) Image::getImages(intval($cookie->id_lang), intval($product->id)); foreach ($product->image as $key => $url) { if (!empty($url)) { $image = new Image(); $image->id_product = intval($product->id); $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = (!$key and !$productHasImages) ? true : false; $image->legend = self::createMultiLangField($product->name[$defaultLanguageId]); if (($fieldError = $image->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $image->add()) { self::copyImg($product->id, $image->id, $url); } else { $this->_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } } if (isset($product->id_category)) { $product->updateCategories(array_map('intval', $product->id_category)); } $features = get_object_vars($product); foreach ($features as $feature => $value) { if (!strncmp($feature, '#F_', 3) and Tools::strlen($product->{$feature})) { $feature_name = str_replace('#F_', '', $feature); $id_feature = Feature::addFeatureImport($feature_name); $id_feature_value = FeatureValue::addFeatureValueImport($id_feature, $product->{$feature}); Product::addFeatureProductImport($product->id, $id_feature, $id_feature_value); } } } } $this->closeCsvFile($handle); }
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; }
protected function importTaxes() { $this->truncateTables(array('tax', 'tax_lang')); $handle = $this->openCsvFile('taxes.csv'); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) { $res = false; $fields = $this->filterFields('Tax', $this->tax_fields, $line); if (!isset($fields['id'])) { $tax = new Tax($line[0]); $tax->id = $line[0]; } else { $tax = new Tax($fields['id']); } foreach ($fields as $key => $field) { if ($key == 'name') { $tax->{$key} = $this->multilFild($field); } else { $tax->{$key} = $field; } } $tax->force_id = true; if (!$res) { $res = $tax->add(); } } $this->closeCsvFile($handle); return true; }