Exemplo n.º 1
0
    public static function calculateProducts($cart)
    {
        $a = new Address($cart->id_address_delivery);
        $c = new Country($a->id_country);
        $res = array('chrono10' => false, 'chronoclassic' => false, 'chrono18' => false);
        $cache = Db::getInstance()->executeS('SELECT chrono10, chrono18, chronoclassic, last_updated FROM `' . _DB_PREFIX_ . 'chrono_calculateproducts_cache2` WHERE postcode = "' . pSQL($a->postcode) . '" && country = "' . pSQL($c->iso_code) . '"');
        if (empty($cache) || $cache[0]['last_updated'] + 24 * 3600 < time()) {
            // QUICKCOST & CALCULATE PRODUCTS
            include_once _MYDIR_ . '/libraries/QuickcostServiceWSService.php';
            $ws = new QuickcostServiceWSService();
            $cp = new calculateProducts();
            $cp->accountNumber = Configuration::get('CHRONOPOST_GENERAL_ACCOUNT');
            $cp->password = Configuration::get('CHRONOPOST_GENERAL_PASSWORD');
            $cp->depZipCode = Configuration::get('CHRONOPOST_SHIPPER_ZIPCODE');
            $cp->depCountryCode = 'FR';
            $cp->weight = $cart->getTotalWeight() * Configuration::get('CHRONOPOST_GENERAL_WEIGHTCOEF') + 0.1;
            $cp->arrCountryCode = $c->iso_code;
            $cp->arrZipCode = $a->postcode;
            $cp->type = 'M';
            try {
                $cpres = $ws->calculateProducts($cp);
            } catch (Exception $e) {
                return $res;
            }
            if (empty($cpres->return->productList)) {
                return $res;
            }
            foreach ($cpres->return->productList as $product) {
                if ($product->productCode == 2) {
                    $res['chrono10'] = true;
                }
                if ($product->productCode == 16) {
                    $res['chrono18'] = true;
                }
                if ($product->productCode == 44) {
                    $res['chronoclassic'] = true;
                }
            }
            // INSERT cache
            if (empty($cache)) {
                $sql = 'INSERT INTO `' . _DB_PREFIX_ . 'chrono_calculateproducts_cache2`
					(`postcode`,`country`, `chrono10`,`chrono18`, `chronoclassic`,`last_updated`) VALUES
					("' . pSQL($a->postcode) . '", "' . pSQL($c->iso_code) . '", ' . ($res['chrono10'] == true ? 1 : 0) . ', ' . ($res['chrono18'] == true ? 1 : 0) . ',
					' . ($res['chronoclassic'] == true ? 1 : 0) . ', ' . time() . ')';
                Db::getInstance()->Execute($sql);
            } else {
                // UPDATE cache
                Db::getInstance()->Execute('UPDATE `' . _DB_PREFIX_ . 'chrono_calculateproducts_cache2`
					SET `chrono10` = ' . ($res['chrono10'] == true ? 1 : 0) . ', `chrono18` = ' . ($res['chrono18'] == true ? 1 : 0) . ',
					 `chronoclassic` = ' . ($res['chronoclassic'] == true ? 1 : 0) . ',
					 `last_updated` = ' . time() . ' WHERE postcode = "' . pSQL($a->postcode) . '" && country = "' . pSQL($c->iso_code) . '"');
            }
        } else {
            $res['chrono10'] = $cache[0]['chrono10'];
            $res['chrono18'] = $cache[0]['chrono18'];
            $res['chronoclassic'] = $cache[0]['chronoclassic'];
        }
        return $res;
    }