/** * Return the product tax * * @param integer $id_product * @param integer $id_address * @param boolean $getCarrierRate * @return Tax Rate */ public static function getProductTaxRate($id_product, $id_address = null, Context $context = null, $getCarrierRate = false) { if ($context == null) { $context = Context::getContext(); } include_once _PS_ROOT_DIR_ . '/modules/avalaratax/avalaratax.php'; /* Instanciate the Avalara module and check if active */ $avalara = new AvalaraTax(); if (!$avalara->active) { return parent::getProductTaxRate((int) $id_product, (int) $id_address, $context); } /* With Avalara, we disable the tax for non logged users */ if (!(int) $id_address) { return 0.0; } $region = Db::getInstance()->getValue(' SELECT s.`iso_code` FROM ' . _DB_PREFIX_ . 'address a LEFT JOIN ' . _DB_PREFIX_ . 'state s ON (s.`id_state` = a.`id_state`) WHERE a.`id_address` = ' . (int) $id_address); /* If the Merchant does not want to calculate taxes outside his state and we are outside the state, we return 0 */ if (!empty($region) && $region != Configuration::get('AVALARATAX_STATE') && !Configuration::get('AVALARATAX_TAX_OUTSIDE')) { return 0.0; } /* Retrieve the tax rate from the cache table or populate the cache if necessary */ $res = self::getTaxFromCache((int) $id_product, (int) $id_address, $region, $getCarrierRate); if (!Db::getInstance()->NumRows()) { include_once _PS_ROOT_DIR_ . '/modules/avalaratax/CacheTools.php'; if ($getCarrierRate) { CacheTools::updateCarrierTax($avalara, $context->cart, (int) $id_address, $region, true); } else { CacheTools::updateProductsTax($avalara, $context->cart, (int) $id_address, $region, true); } $res = self::getTaxFromCache((int) $id_product, (int) $id_address, $region, $getCarrierRate); } return $res; }
/** * @brief Calculate the shipping cost. * * Override it in order to calculate and add the correct tax amount. * * @note This method is only called in PrestaShop > 1.5 * @see Cart::getOrderShippingCost() for PrestaShop < 1.5 * * @param Array $delivery_option Delivery options * @param boolean $use_tax Do we want the taxes? * @param Country $default_country Default country (1.5 only) * * @return float Price of the shipping. */ public function getTotalShippingCost($delivery_option = null, $use_tax = true, Country $default_country = null) { include_once _PS_ROOT_DIR_ . '/modules/avalaratax/avalaratax.php'; /* Instanciate the Avalara module and check if active */ $avalara = new AvalaraTax(); if (!$avalara->active) { return parent::getTotalShippingCost($delivery_option, $use_tax, $default_country); } /* Retrieve the original carrier fee tax excluded */ $tax_excluded_cost = parent::getTotalShippingCost($delivery_option, false, $default_country); /* If we want price without tax or if this carrier is tax free, return this price */ if (!(int) $this->{Configuration::get('PS_TAX_ADDRESS_TYPE')} || !$use_tax) { return $tax_excluded_cost; } /* If there is no cache or cache expired, we regenerate it */ if (CacheTools::checkCarrierCache($this)) { $region = Db::getInstance()->getValue(' SELECT s.`iso_code` FROM ' . _DB_PREFIX_ . 'address a LEFT JOIN ' . _DB_PREFIX_ . 'state s ON (s.`id_state` = a.`id_state`) WHERE a.`id_address` = ' . (int) $this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); CacheTools::updateCarrierTax($avalara, $this, $this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $region); } /* If we do already know it, then return it */ return $tax_excluded_cost + (double) CacheTools::getCarrierTaxAmount($this); }
$ids_product = implode(', ', $ids_product); $address = new Address((int) $_POST['id_address']); if (!Validate::isLoadedObject($address)) { die('{"hasError":true, "errors":["Error while loading Address"]}'); } $region = null; if ((int) $address->id_state) { $state = new State((int) $address->id_state); if (!Validate::isLoadedObject($state)) { die('{"hasError":true, "errors":["Error while loading State"]}'); } $region = $state->iso_code; } $taxable = true; //check if it is outside the state and if we are in united state and if conf AVALARATAX_TAX_OUTSIDE IS ENABLE if ($region && !Configuration::get('AVALARATAX_TAX_OUTSIDE') && $region != Configuration::get('AVALARATAX_STATE')) { $taxable = false; } // Check cache before asking Avalara webservice $pc = CacheTools::checkProductCache($ids_product, $region, $cart); $cc = CacheTools::checkCarrierCache($cart); if (!$pc && !$cc) { die('{"hasError":false, "cached_tax":true}'); } if ($pc) { CacheTools::updateProductsTax($avalaraModule, $cart, (int) $_POST['id_address'], $region, $taxable); } if ($cc) { CacheTools::updateCarrierTax($avalaraModule, $cart, (int) $_POST['id_address'], $taxable); } die('{"hasError":false, "cached_tax":false, "total_tax":"' . Tools::displayPrice($cart->getOrderTotal() - $cart->getOrderTotal(false)) . '"}');