tax_calculator = new TaxCalculatorCore(array(), 0); } else { if (!is_array($infos[$id_tax_rules_group]) || !array_key_exists('taxes', $infos[$id_tax_rules_group]) || !array_key_exists('computation_method', $infos[$id_tax_rules_group])) { throw new PrestaShopException('Error while parsing JSON to construct a JprestaUtilsTaxManager'); } $infos_rule = $infos[$id_tax_rules_group]; $taxes = []; foreach ($infos_rule['taxes'] as $id_tax) { $tax = new Tax((int)$id_tax); if ($tax) { $taxes[] = $tax; } } $this->tax_calculator = new TaxCalculatorCore($taxes, $infos_rule['computation_method']); } } public static function toPrettyString($json) { $infos = json_decode($json, true); if (!is_array($infos)) { return 'Cannot parse JSON to construct a JprestaUtilsTaxManager'; } $prettyString = ''; foreach ($infos as $id_tax_rules_group => $infos_rule) { $taxRulesGroup = new TaxRulesGroup($id_tax_rules_group); $prettyString .= '- ' . $taxRulesGroup->getFieldByLang('name') . " (#$id_tax_rules_group)\n"; if (count($infos_rule['taxes']) > 0) { $idx = 1; if ($infos_rule['computation_method'] === TaxCalculatorCore::COMBINE_METHOD) { $prettyString .= " Computation method: COMBINE METHOD\n"; } elseif ($infos_rule['computation_method'] === TaxCalculatorCore::ONE_AFTER_ANOTHER_METHOD) { $prettyString .= " Computation method: ONE AFTER ANOTHER METHOD\n"; } elseif ($infos_rule['computation_method'] === 0) { $prettyString .= " Computation method: FIRST TAX ONLY\n"; } else { $prettyString .= " Computation method: #" . $infos_rule['computation_method'] . "\n"; } foreach ($infos_rule['taxes'] as $id_tax) { $tax = new Tax((int)$id_tax); $prettyString .= " $idx) " . $tax->getFieldByLang('name') . " (#$id_tax)\n"; $idx++; } } else { $prettyString .= " (none)\n"; } } return $prettyString; } /** * @param int $id_shop * @param Address $address * @return string */ public static function toJson($id_shop, $address) { $infos = []; $sql = 'SELECT trgs.`id_tax_rules_group` FROM `' . _DB_PREFIX_ . 'tax_rules_group_shop` as trgs LEFT JOIN `' . _DB_PREFIX_ . 'tax_rules_group` as trg ON trgs.id_tax_rules_group=trg.id_tax_rules_group WHERE active=1 AND id_shop=' . (int)$id_shop; if (Tools::version_compare(_PS_VERSION_,'1.6.0.9','>')) { $sql .= ' AND deleted=0'; } $rows = JprestaUtils::dbSelectRows($sql, true, false); $cartWasEmpty = false; $context = Context::getContext(); if (empty($context->cart) && $context->cookie->id_cart) { // We need the cart to be initialized because usually, TaxManager uses it to check addresses $cart = new Cart($context->cookie->id_cart); $context->cart = $cart; $cartWasEmpty = true; } foreach ($rows as $row) { $id_tax_rules_group = $row['id_tax_rules_group']; $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules_group); if ($tax_manager) { $tax_calculator = $tax_manager->getTaxCalculator(); $infos[$id_tax_rules_group] = []; $infos[$id_tax_rules_group]['taxes'] = []; $infos[$id_tax_rules_group]['computation_method'] = $tax_calculator->computation_method; foreach ($tax_calculator->taxes as $tax) { $infos[$id_tax_rules_group]['taxes']['id_tax'] = $tax->id; } } } if ($cartWasEmpty) { // Restore empty cart to avoid different behavior $context->cart = null; } return json_encode($infos); } public static function isAvailableForThisAddress(Address $address) { return true; } public function getTaxCalculator() { return $this->tax_calculator; } } }