Ejemplo n.º 1
0
 /**
  * translates string with key $msg_id
  *
  * @param string $msg_id
  * @param int $count NOT USED YET, only for ITranslator needs
  * @return string translated string
  */
 public function translate($msg_id, $count = NULL)
 {
     if ($msg_id == "") {
         return NULL;
     }
     ///**************
     if (!isset($this->dictionary[$msg_id])) {
         dibi::insert(self::TABLE, array('msg_id' => $msg_id))->execute();
         $this->dictionary[$msg_id] = $msg_id;
         CacheTools::invalidate('dictionary');
     }
     /**	applying parameters **/
     $args = func_get_args();
     $argsCount = count($args);
     $requiredArgsCount = preg_match_all($this->paramsRegexp, $this->dictionary[$msg_id], $matches);
     if ($requiredArgsCount > $argsCount - 1) {
         throw new InvalidArgumentException("Insufficient number of arguments in translate function. Provided string '{$msg_id}'");
     }
     //        if ($argsCount > 1) {
     //	volane z Rules.php, aby sa mi to nesexovalo s '%label' a tym percentom a nemusel ho zdvojovat
     if ($argsCount > 1 && !is_null($args[1])) {
         //	vola sa to aj s parametrami z Rules.php napr. pre MIN_LENGTH a pod., treba zdvojit percento
         $msg_id = str_replace(array('%label', '%name', '%value'), array('%%label', '%%name', '%%value'), $this->dictionary[$msg_id]);
         //		dump($args);
         array_shift($args);
         //		dump($args);
         return vsprintf($msg_id, $args);
     } else {
         return $this->dictionary[$msg_id];
     }
     /**	applying parameters END **/
 }
Ejemplo n.º 2
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;
    }
Ejemplo n.º 3
0
 public function getPackageShippingCost($id_carrier = null, $use_tax = true, Country $default_country = null, $product_list = null, $id_zone = 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::getPackageShippingCost((int) $id_carrier, $use_tax, $default_country, $product_list);
     }
     /* Retrieve the original carrier fee tax excluded */
     $tax_excluded_cost = parent::getPackageShippingCost((int) $id_carrier, false, $default_country, $product_list);
     /* 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)) {
         return parent::getPackageShippingCost((int) $id_carrier, $use_tax, $default_country, $product_list);
     }
     /* If we do already know it, then return it */
     return $tax_excluded_cost + (double) CacheTools::getCarrierTaxAmount($this);
 }
Ejemplo n.º 4
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)) . '"}');