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 importTaxRulesGroups() { $this->truncateTables(array('tax_rules_group', 'tax_rules_group_shop')); $handle = $this->openCsvFile('tax_rule_groups.csv'); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) { $res = false; $fields = $this->filterFields('TaxRulesGroup', $this->tax_rule_group_fields, $line); if (!isset($fields['id'])) { $tax_rule_group = new TaxRulesGroup($line[0]); $tax_rule_group->id = $line[0]; } else { $tax_rule_group = new TaxRulesGroup($fields['id']); } foreach ($fields as $key => $field) { $tax_rule_group->{$key} = $field; } $tax_rule_group->force_id = true; if (!$res) { $res = $tax_rule_group->add(); } } $this->closeCsvFile($handle); return true; }