Esempio n. 1
0
 /**
  * Return package shipping cost
  *
  * @param integer $carrier_id Carrier ID (default : current carrier)
  * @param boolean $use_tax
  * @param JeproshopCountryModelCountry $default_country
  * @param Array $product_list
  * @param array $product_list List of product concerned by the shipping. If null, all the product of the cart are used to calculate the shipping cost
  *
  * @return float Shipping total
  */
 public function getPackageShippingCost($carrier_id = null, $use_tax = true, JeproshopCountryModelCountry $default_country = null, $product_list = null, $zone_id = null)
 {
     if ($this->isVirtualCart()) {
         return 0;
     }
     if (!$default_country) {
         $default_country = JeproshopContext::getContext()->country;
     }
     $complete_product_list = $this->getProducts();
     if (is_null($product_list)) {
         $products = $complete_product_list;
     } else {
         $products = $product_list;
     }
     if (JeproshopSettingModelSetting::getValue('tax_address_type') == 'address_invoice_id') {
         $address_id = (int) $this->address_invoice_id;
     } elseif (count($product_list)) {
         $prod = current($product_list);
         $address_id = (int) $prod->address_delivery_id;
     } else {
         $address_id = null;
     }
     if (!JeproshopAddressModelAddress::addressExists($address_id)) {
         $address_id = null;
     }
     $cache_id = 'get_package_shipping_cost_' . (int) $this->cart_id . '_' . (int) $address_id . '_' . (int) $carrier_id . '_' . (int) $use_tax . '_' . (int) $default_country->country_id;
     if ($products) {
         foreach ($products as $product) {
             $cache_id .= '_' . (int) $product->product_id . '_' . (int) $product->product_attribute_id;
         }
     }
     if (JeproshopCache::isStored($cache_id)) {
         return JeproshopCache::retrieve($cache_id);
     }
     // Order total in default currency without fees
     $order_total = $this->getOrderTotal(true, JeproshopCartModelCart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING, $product_list);
     // Start with shipping cost at 0
     $shipping_cost = 0;
     // If no product added, return 0
     if (!count($products)) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     if (!isset($zone_id)) {
         // Get id zone
         if (!$this->isMultiAddressDelivery() && isset($this->address_delivery_id) && $this->address_delivery_id && JeproshopCustomerModelCustomer::customerHasAddress($this->customer_id, $this->address_delivery_id)) {
             $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId((int) $this->address_delivery_id);
         } else {
             if (!JeproshopValidator::isLoadedObject($default_country, 'country_id')) {
                 $default_country = new Country(JeproshopSettingModelSeting::getValue('PS_COUNTRY_DEFAULT'), JeproshopSettingModelSeting::getValue('PS_LANG_DEFAULT'));
             }
             $zone_id = (int) $default_country->zone_id;
         }
     }
     if ($carrier_id && !$this->isCarrierInRange((int) $carrier_id, (int) $zone_id)) {
         $carrier_id = '';
     }
     if (empty($carrier_id) && $this->isCarrierInRange((int) JeproshopSettingModelSeting::getValue('default_carrier'), (int) $zone_id)) {
         $carrier_id = (int) JeproshopSettingModelSeting::getValue('default_carrier');
     }
     $total_package_without_shipping_tax_inc = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
     if (empty($carrier_id)) {
         if ((int) $this->customer_id) {
             $customer = new JeproshopCustomerModelCustomer((int) $this->customer_id);
             $result = JeproshopCarrierModelCarrier::getCarriers((int) JeproshopSettingModelSeting::getValue('default_lang'), true, false, (int) $zone_id, $customer->getGroups());
             unset($customer);
         } else {
             $result = JeproshopCarrierModelCarrier::getCarriers((int) JeproshopSettingModelSeting::getValue('default_lang'), true, false, (int) $zone_id);
         }
         foreach ($result as $k => $row) {
             if ($row->carrier_id == JeproshopSettingModelSeting::getValue('default_carrier')) {
                 continue;
             }
             if (!isset(self::$_carriers[$row->carrier_id])) {
                 self::$_carriers[$row->carrier_id] = new JeproshopCarrierModelCarrier((int) $row->carrier_id);
             }
             $carrier = self::$_carriers[$row->carrier_id];
             // Get only carriers that are compliant with shipping method
             if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && $carrier->getMaxDeliveryPriceByWeight((int) $zone_id) === false || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && $carrier->getMaxDeliveryPriceByPrice((int) $zone_id) === false) {
                 unset($result[$k]);
                 continue;
             }
             // If out-of-range behavior carrier is set on "Deactivated carrier"
             if ($row->range_behavior) {
                 $check_delivery_price_by_weight = JeproshopCarrierModelCarrier::checkDeliveryPriceByWeight($row->carrier_id, $this->getTotalWeight(), (int) $zone_id);
                 $total_order = $total_package_without_shipping_tax_inc;
                 $check_delivery_price_by_price = JeproshopCarrierModelCarrier::checkDeliveryPriceByPrice($row->carrier_id, $total_order, (int) $zone_id, (int) $this->currency_id);
                 // Get only carriers that have a range compatible with cart
                 if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && !$check_delivery_price_by_weight || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && !$check_delivery_price_by_price) {
                     unset($result[$k]);
                     continue;
                 }
             }
             if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD) {
                 $shipping = $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), (int) $zone_id);
             } else {
                 $shipping = $carrier->getDeliveryPriceByPrice($order_total, (int) $zone_id, (int) $this->currency_id);
             }
             if (!isset($min_shipping_price)) {
                 $min_shipping_price = $shipping;
             }
             if ($shipping <= $min_shipping_price) {
                 $carrier_id = (int) $row->carrier_id;
                 $min_shipping_price = $shipping;
             }
         }
     }
     if (empty($carrier_id)) {
         $carrier_id = JeproshopSettingModelSeting::getValue('default_carrier');
     }
     if (!isset(self::$_carriers[$carrier_id])) {
         self::$_carriers[$carrier_id] = new JeproshopCarrierModelCarrier((int) $carrier_id, JeproshopSettingModelSeting::getValue('default_lang'));
     }
     $carrier = self::$_carriers[$carrier_id];
     // No valid Carrier or $carrier_id <= 0 ?
     if (!JeproshopValidator::isLoadedObject($carrier, 'carrier_id')) {
         JeproshopCache::store($cache_id, 0);
         return 0;
     }
     if (!$carrier->published) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Free fees if free carrier
     if ($carrier->is_free == 1) {
         JeproshopCache::store($cache_id, 0);
         return 0;
     }
     // Select carrier tax
     if ($use_tax && !JeproshopTaxModelTax::excludedTaxOption()) {
         $address = JeproshopAddressModelAddress::initialize((int) $address_id);
         $carrier_tax = $carrier->getTaxesRate($address);
     }
     $configuration = JeproshopSettingModelSeting::getValueMultiple(array('PS_SHIPPING_FREE_PRICE', 'PS_SHIPPING_HANDLING', 'PS_SHIPPING_METHOD', 'PS_SHIPPING_FREE_WEIGHT'));
     // Free fees
     $free_fees_price = 0;
     if (isset($configuration['PS_SHIPPING_FREE_PRICE'])) {
         $free_fees_price = JeproshopValidator::convertPrice((double) $configuration['PS_SHIPPING_FREE_PRICE'], JeproshopCurrencyModelCurrency::getCurrencyInstance((int) $this->currency_id));
     }
     $orderTotalWithDiscounts = $this->getOrderTotal(true, JeproshopCartModelCart::BOTH_WITHOUT_SHIPPING, null, null, false);
     if ($orderTotalWithDiscounts >= (double) $free_fees_price && (double) $free_fees_price > 0) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     if (isset($configuration['PS_SHIPPING_FREE_WEIGHT']) && $this->getTotalWeight() >= (double) $configuration['PS_SHIPPING_FREE_WEIGHT'] && (double) $configuration['PS_SHIPPING_FREE_WEIGHT'] > 0) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Get shipping cost using correct method
     if ($carrier->range_behavior) {
         if (!isset($zone_id)) {
             // Get id zone
             if (isset($this->address_delivery_id) && $this->address_delivery_id && JeproshopCustomerModelCustomer::customerHasAddress($this->customer_id, $this->address_delivery_id)) {
                 $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId((int) $this->address_delivery_id);
             } else {
                 $zone_id = (int) $default_country->zone_id;
             }
         }
         if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && !JeproshopCarrierModelCarrier::checkDeliveryPriceByWeight($carrier->id, $this->getTotalWeight(), (int) $zone_id) || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && !JeproshopCarrierModelCarrier::checkDeliveryPriceByPrice($carrier->id, $total_package_without_shipping_tax_inc, $zone_id, (int) $this->currency_id)) {
             $shipping_cost += 0;
         } else {
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $zone_id);
             } else {
                 // by price
                 $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $zone_id, (int) $this->currency_id);
             }
         }
     } else {
         if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD) {
             $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $zone_id);
         } else {
             $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $zone_id, (int) $this->currency_id);
         }
     }
     // Adding handling charges
     if (isset($configuration['PS_SHIPPING_HANDLING']) && $carrier->shipping_handling) {
         $shipping_cost += (double) $configuration['PS_SHIPPING_HANDLING'];
     }
     // Additional Shipping Cost per product
     foreach ($products as $product) {
         if (!$product->is_virtual) {
             $shipping_cost += $product->additional_shipping_cost * $product->cart_quantity;
         }
     }
     $shipping_cost = JeproshopValidator::convertPrice($shipping_cost, JeproshopCurrencyModelCurrency::getCurrencyInstance((int) $this->currency_id));
     /*/get external shipping cost from module
             if ($carrier->shipping_external)
             {
                 $module_name = $carrier->external_module_name;
                 $module = Module::getInstanceByName($module_name);
     
                 if (JeproshopValidator::isLoadedObject($module))
                 {
                     if (array_key_exists('carrier_id', $module))
                         $module->carrier_id = $carrier->id;
                     if ($carrier->need_range)
                         if (method_exists($module, 'getPackageShippingCost'))
                             $shipping_cost = $module->getPackageShippingCost($this, $shipping_cost, $products);
                         else
                             $shipping_cost = $module->getOrderShippingCost($this, $shipping_cost);
                     else
                         $shipping_cost = $module->getOrderShippingCostExternal($this);
     
                     // Check if carrier is available
                     if ($shipping_cost === false)
                     {
                         JeproshopCache::store($cache_id, false);
                         return false;
                     }
                 }
                 else
                 {
                     JeproshopCache::store($cache_id, false);
                     return false;
                 }
             } */
     // Apply tax
     if ($use_tax && isset($carrier_tax)) {
         $shipping_cost *= 1 + $carrier_tax / 100;
     }
     $shipping_cost = (double) JeproshopValidator::roundPrice((double) $shipping_cost, 2);
     JeproshopCache::store($cache_id, $shipping_cost);
     return $shipping_cost;
 }
Esempio n. 2
0
 /**
  * For a given {product, warehouse}, gets the carrier available
  *
  * @param JeproshopProductModelProduct $product The id of the product, or an array with at least the package size and weight
  * @param int $warehouse_id
  * @param int $address_delivery_id
  * @param int $shop_id
  * @param $cart
  * @return array
  */
 public static function getAvailableCarrierList(JeproshopProductModelProduct $product, $warehouse_id, $address_delivery_id = null, $shop_id = null, $cart = null)
 {
     if (is_null($shop_id)) {
         $shop_id = JeproshopContext::getContext()->shop->shop_id;
     }
     if (is_null($cart)) {
         $cart = JeproshopContext::getContext()->cart;
     }
     $address_id = (int) (!is_null($address_delivery_id) && $address_delivery_id != 0 ? $address_delivery_id : $cart->address_delivery_id);
     if ($address_id) {
         $address = new JeproshopAddressModelAddress($address_id);
         $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId($address->address_id);
         // Check the country of the address is activated
         if (!JeproshopAddressModelAddress::isCountryActiveById($address->address_id)) {
             return array();
         }
     } else {
         $country = new JeproshopCountryModelCountry(JeproshopSettingModelSetting::getValue('default_country'));
         $izone_id = $country->zone_id;
     }
     // Does the product is linked with carriers?
     $query = new DbQuery();
     $query->select('id_carrier');
     $query->from('product_carrier', 'pc');
     $query->innerJoin('carrier', 'c', 'c.id_reference = pc.id_carrier_reference AND c.deleted = 0');
     $query->where('pc.id_product = ' . (int) $product->product_id);
     $query->where('pc.id_shop = ' . (int) $shop_id);
     $cache_id = 'Carrier::getAvailableCarrierList_' . (int) $product->id . '-' . (int) $id_shop;
     if (!Cache::isStored($cache_id)) {
         $carriers_for_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
         Cache::store($cache_id, $carriers_for_product);
     }
     $carriers_for_product = Cache::retrieve($cache_id);
     $carrier_list = array();
     if (!empty($carriers_for_product)) {
         //the product is linked with carriers
         foreach ($carriers_for_product as $carrier) {
             //check if the linked carriers are available in current zone
             if (Carrier::checkCarrierZone($carrier['id_carrier'], $id_zone)) {
                 $carrier_list[] = $carrier['id_carrier'];
             }
         }
         if (empty($carrier_list)) {
             return array();
         }
         //no linked carrier are available for this zone
     }
     // The product is not directly linked with a carrier
     // Get all the carriers linked to a warehouse
     if ($warehouse_id) {
         $warehouse = new JeproshopWarehouseModelWarehouse($warehouse_id);
         $warehouse_carrier_list = $warehouse->getCarriers();
     }
     $available_carrier_list = array();
     $customer = new JeproshopCustomerModelCustomer($cart->customer_id);
     $carriers = JeproshopCarrierModelCarrier::getCarriersForOrder($zone_id, $customer->getGroups(), $cart);
     foreach ($carriers as $carrier) {
         $available_carrier_list[] = $carrier->carrier_id;
     }
     if ($carrier_list) {
         $carrier_list = array_intersect($available_carrier_list, $carrier_list);
     } else {
         $carrier_list = $available_carrier_list;
     }
     if (isset($warehouse_carrier_list)) {
         $carrier_list = array_intersect($carrier_list, $warehouse_carrier_list);
     }
     if ($product->width > 0 || $product->height > 0 || $product->depth > 0 || $product->weight > 0) {
         foreach ($carrier_list as $key => $carrier_id) {
             $carrier = new JeproshopCarrierModelCarrier($carrier_id);
             if ($carrier->max_width > 0 && $carrier->max_width < $product->width || $carrier->max_height > 0 && $carrier->max_height < $product->height || $carrier->max_depth > 0 && $carrier->max_depth < $product->depth || $carrier->max_weight > 0 && $carrier->max_weight < $product->weight) {
                 unset($carrier_list[$key]);
             }
         }
     }
     return $carrier_list;
 }