Exemple #1
0
    /**
     * 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;
    }
Exemple #2
0
$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)) . '"}');