_DPDPOLAND_PRICE_RULE_DB_, 'primary' => 'id_csv', 'multilang' => false, 'multishop' => true, 'fields' => array( 'id_csv' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'iso_country' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything'), 'price_from' => array('type' => self::TYPE_STRING, 'validate' => 'isFloat'), 'price_to' => array('type' => self::TYPE_STRING, 'validate' => 'isFloat'), 'weight_from' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything'), 'weight_to' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything'), 'parcel_price' => array('type' => self::TYPE_STRING, 'validate' => 'isFloat'), 'cod_price' => array('type' => self::TYPE_STRING, 'validate' => 'isFloat'), 'id_carrier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate') ) ); /** * Collects data from database about price rules * * @param string|int $start From which element data should be taken * @param string|int $limit How many elements should be taken * @return array|false|mysqli_result|null|PDOStatement|resource Price rules data */ public static function getAllData($start = '', $limit = '') { return DB::getInstance()->executeS(' SELECT `id_csv`, `iso_country`, `price_from`, `price_to`, `weight_from`, `weight_to`, `parcel_price`, `cod_price`, `id_carrier` FROM `'._DB_PREFIX_._DPDPOLAND_PRICE_RULE_DB_.'` WHERE `id_shop` = "'.(int)Context::getContext()->shop->id.'" '.($start && $limit ? 'LIMIT '.(int)$start.', '.(int)$limit : '') ); } /** * Deletes all price rules for current shop * * @return bool Price rules deleted successfully */ public static function deleteAllData() { return DB::getInstance()->execute(' DELETE FROM `'._DB_PREFIX_._DPDPOLAND_PRICE_RULE_DB_.'` WHERE `id_shop` = "'.(int)Context::getContext()->shop->id.'" '); } /** * Collects price rules data of current shop * * @return array|false|mysqli_result|null|PDOStatement|resource Price rules data */ public static function getCSVData() { return DB::getInstance()->executeS(' SELECT `iso_country`, `price_from`, `price_to`, `weight_from`, `weight_to`, `parcel_price`, `id_carrier`, `cod_price` FROM `'._DB_PREFIX_._DPDPOLAND_PRICE_RULE_DB_.'` WHERE `id_shop` = "'.(int)Context::getContext()->shop->id.'" '); } /** * Returns carrier price according to cart products / parameters * * @param float $total_weight Sum of products weight * @param int $id_carrier Carrier ID * @param Cart $cart Cart object * @return bool|int|float Carrier price */ public static function getPrice($total_weight, $id_carrier, Cart $cart) { if ($id_country = (int)Tools::getValue('id_country')) $iso_country = Country::getIsoById($id_country); else { $address = new Address((int)$cart->id_address_delivery); $iso_country = Country::getIsoById((int)$address->id_country); } if(!$iso_country) $iso_country = 'PL'; $cart_total_price = $cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING); $id_currency_pl = Currency::getIdByIsoCode(_DPDPOLAND_CURRENCY_ISO_, (int)Context::getContext()->shop->id); $currency_from = new Currency((int)$cart->id_currency); $currency_to = new Currency((int)$id_currency_pl); $cart_total_price = Tools::convertPriceFull($cart_total_price, $currency_from, $currency_to); $price_rules = DB::getInstance()->executeS(' SELECT * FROM `'._DB_PREFIX_._DPDPOLAND_PRICE_RULE_DB_.'` WHERE (`iso_country` = "'.pSQL($iso_country).'" OR `iso_country` = "*") AND (`weight_from` <= "'.pSQL($total_weight).'" AND `weight_to` >= "'.pSQL($total_weight).'" OR `price_from` <= "'.pSQL($cart_total_price).'" AND `price_to` >= "'.pSQL($cart_total_price).'") AND `id_carrier` = "'.(int)$id_carrier.'" AND `id_shop` = "'.(int)Context::getContext()->shop->id.'" '); if (!$price_rules) return false; $available_prices_count = count($price_rules); for ($i = 0; $i < $available_prices_count; $i++) if ($price_rules[$i]['iso_country'] != '*' && !Country::getByIso($price_rules[$i]['iso_country'])) //if country is not deleted unset($price_rules[$i]); if (!$price_rules) return false; $matching_price_rule = null; if (is_array($price_rules)) { foreach ($price_rules as $price_rule) { if ($price_rule['price_from'] <= $cart_total_price && $price_rule['price_to'] > $cart_total_price && $price_rule['weight_from'] <= $total_weight && $price_rule['weight_to'] > $total_weight ) { $matching_price_rule = $price_rule; break; } } } if (null == $matching_price_rule) { $matching_price_rule = $price_rules[0]; //accept first matching rule } if (!$matching_price_rule['cod_price']) $matching_price_rule['cod_price'] = 0; //CSV validation allows empty value of COD price $price = $matching_price_rule['parcel_price']; if ($id_carrier == _DPDPOLAND_STANDARD_COD_ID_) $price += $matching_price_rule['cod_price']; return $price; } }