From 0602a2dbdbd0ad51badb7ca98ba78a04a2097b88 Mon Sep 17 00:00:00 2001 From: mDeflotte Date: Fri, 2 Dec 2011 10:48:26 +0000 Subject: [PATCH] // Fix some problems with getPackageShippingCost --- classes/Cart.php | 70 ++++++++++++++--- classes/CartRule.php | 19 ++--- classes/order/Order.php | 33 +++++++- controllers/admin/AdminOrdersController.php | 83 +++++++++++---------- 4 files changed, 145 insertions(+), 60 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 7ea1ddf97..9c23d5d2a 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -275,8 +275,8 @@ class CartCore extends ObjectModel $total_products_ti = $this->getOrderTotal(true, Cart::ONLY_PRODUCTS); $total_products_te = $this->getOrderTotal(false, Cart::ONLY_PRODUCTS); - $shipping_ti = $this->getPackageShippingCost(); - $shipping_te = $this->getPackageShippingCost(NULL, false); + $shipping_ti = $this->getTotalShippingCost(); + $shipping_te = $this->getTotalShippingCost(null, false); $result = Db::getInstance()->executeS(' SELECT * @@ -358,7 +358,7 @@ class CartCore extends ObjectModel $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, p.`is_virtual`, pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, p.`id_category_default`, p.`id_supplier`, p.`id_manufacturer`, p.`on_sale`, p.`ecotax`, p.`additional_shipping_cost`, p.`available_for_order`, p.`price`, p.`weight`, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, - sa.`quantity` quantity_available, p.`active`, p.`date_add`, p.`date_upd`, t.`id_tax`, tl.`name` AS tax, t.`rate`, stock.quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category, + stock.`quantity` quantity_available, p.`active`, p.`date_add`, p.`date_upd`, t.`id_tax`, tl.`name` AS tax, t.`rate`, stock.quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category, CONCAT(cp.`id_product`, cp.`id_product_attribute`, cp.`id_address_delivery`) AS unique_id, cp.id_address_delivery'); // Build FROM @@ -375,10 +375,6 @@ class CartCore extends ObjectModel $sql->leftJoin('tax_lang tl ON t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$this->id_lang); $sql->leftJoin('category_lang cl ON p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->id_lang.Context::getContext()->shop->addSqlRestrictionOnLang('cl')); - $sql->leftJoin('stock_available sa ON cp.`id_product` = sa.`id_product` - AND cp.`id_product_attribute` = sa.`id_product_attribute`'. - Context::getContext()->shop->addSqlRestriction(false, 'sa')); - // @todo test if everything is ok, then refactorise call of this method Product::sqlStock('cp', 'cp', false, null, $sql); @@ -1008,24 +1004,41 @@ class CartCore extends ObjectModel $withTaxes = false; foreach ($products AS $product) { + if (isset($product['id_product'])) + $id_product = (int)$product['id_product']; + else if (isset($product['product_id'])) + $id_product = (int)$product['product_id']; + else + $id_product = (int)$product['id']; + + if (isset($product['id_product_attribute'])) + $id_product_attribute = (int)$product['id_product_attribute']; + else + $id_product_attribute = (int)$product['product_attribute_id']; + + if (isset($product['cart_quantity'])) + $cart_quantity = (int)$product['cart_quantity']; + else + $cart_quantity = (int)$product['product_quantity']; + if ($this->_taxCalculationMethod == PS_TAX_EXC) { // Here taxes are computed only once the quantity has been applied to the product price - $price = Product::getPriceStatic((int)$product['id_product'], false, (int)$product['id_product_attribute'], 2, NULL, false, true, $product['cart_quantity'], false, (int)$this->id_customer ? (int)$this->id_customer : NULL, (int)$this->id, ($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')})); + $price = Product::getPriceStatic($id_product, false, $id_product_attribute, 2, NULL, false, true, $product['cart_quantity'], false, (int)$this->id_customer ? (int)$this->id_customer : NULL, (int)$this->id, ($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')})); $total_ecotax = $product['ecotax'] * (int)$product['cart_quantity']; $total_price = $price * (int)$product['cart_quantity']; if ($withTaxes) { - $total_price = ($total_price - $total_ecotax) * (1 + (float)(Tax::getProductTaxRate((int)$product['id_product'], (int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')})) / 100); + $total_price = ($total_price - $total_ecotax) * (1 + (float)(Tax::getProductTaxRate($id_product, (int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')})) / 100); $total_ecotax = $total_ecotax * (1 + Tax::getProductEcotaxRate((int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) / 100); $total_price = Tools::ps_round($total_price + $total_ecotax, 2); } } else { - $price = Product::getPriceStatic((int)($product['id_product']), true, (int)($product['id_product_attribute']), 2, NULL, false, true, $product['cart_quantity'], false, ((int)($this->id_customer) ? (int)($this->id_customer) : NULL), (int)($this->id), ((int)($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) ? (int)($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) : NULL)); + $price = Product::getPriceStatic($id_product, true, $id_product_attribute, 2, NULL, false, true, $product['cart_quantity'], false, ((int)($this->id_customer) ? (int)($this->id_customer) : NULL), (int)($this->id), ((int)($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) ? (int)($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')}) : NULL)); $total_price = Tools::ps_round($price, 2) * (int)($product['cart_quantity']); if (!$withTaxes) $total_price = Tools::ps_round($total_price / (1 + ((float)(Tax::getProductTaxRate((int)$product['id_product'], (int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')})) / 100)), 2); @@ -1493,7 +1506,8 @@ class CartCore extends ObjectModel } /** - * Does the cart use multiple + * Does the cart use multiple address + * @return boolean */ public function isMultiAddressDelivery() { @@ -1611,6 +1625,40 @@ class CartCore extends ObjectModel return $total_shipping; } + /** + * Return shipping total of a specific carriers for the cart + * + * @param int $id_carrier + * @param array $delivery_option Array of the delivery option for each address + * @param booleal $useTax + * @param Country $default_country + * @return float Shipping total + */ + public function getCarrierCost($id_carrier, $useTax = true, Country $default_country = null, $delivery_option = null) + { + if (is_null($delivery_option)) + $delivery_option = $this->getDeliveryOption($default_country); + + $total_shipping = 0; + $delivery_option_list = $this->getDeliveryOptionList(); + + + foreach ($delivery_option as $id_address => $key) + { + if (!isset($delivery_option_list[$id_address]) || !isset($delivery_option_list[$id_address][$key])) + continue; + if (isset($delivery_option_list[$id_address][$key]['carrier_list'][$id_carrier])) + { + if ($useTax) + $total_shipping += $delivery_option_list[$id_address][$key]['carrier_list'][$id_carrier]['total_price_with_tax']; + else + $total_shipping += $delivery_option_list[$id_address][$key]['carrier_list'][$id_carrier]['total_price_without_tax']; + } + } + + return $total_shipping; + } + /** * Return shipping total diff --git a/classes/CartRule.php b/classes/CartRule.php index 5d7108a64..e537937ba 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -452,16 +452,17 @@ class CartRuleCore extends ObjectModel if ($this->free_shipping) { if (!$this->carrier_restriction) - $reductionValue += $context->cart->getPackageShippingCost($context->cart->id_carrier, $useTax = true, $context->country); - elseif ($context->cart->id_carrier) + $reductionValue += $context->cart->getTotalShippingCost(null, $useTax = true, $context->country); + else { - $id_cart_rule = (int)Db::getInstance()->getValue(' - SELECT crc.id_cart_rule - FROM '._DB_PREFIX_.'cart_rule_carrier crc - WHERE crc.id_cart_rule = '.(int)$this->id.' - AND crc.id_carrier = '.(int)$context->cart->id_carrier); - if ($id_cart_rule) - $reductionValue += $context->cart->getPackageShippingCost($context->cart->id_carrier, $useTax, $context->country); + foreach ((int)Db::getInstance()->executeS(' + SELECT crc.id_cart_rule, crc.id_carrier + FROM '._DB_PREFIX_.'cart_rule_carrier crc + WHERE crc.id_cart_rule = '.(int)$this->id.' + AND crc.id_carrier = '.(int)$context->cart->id_carrier) + as $cart_rule + ) + $reductionValue += $context->cart->getCarrierCost($cart_rule['id_carrier'], $useTax, $context->country); } } diff --git a/classes/order/Order.php b/classes/order/Order.php index b3fff4dc4..8596b46a7 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -318,7 +318,36 @@ class OrderCore extends ObjectModel } return $this->_deleteProduct($orderDetail, (int)($quantity)); } - + + /** + * This function return products of the orders + * It's similar to Order::getProducts but witrh similar outputs of Cart::getProducts + * + * @return array + */ + public function getCartProducts() + { + $product_id_list = array(); + foreach ($this->getProducts() as $product) + $product_id_list[] = $this->id_address_delivery.'_' + .$product['product_id'].'_' + .$product['product_attribute_id'].'_' + .(isset($product['id_customization']) ? $product['id_customization'] : '0'); + + $product_list = array(); + foreach ($cart->getProducts() as $product) + { + $key = $this->id_address_delivery.'_' + .$product['id_product'].'_' + .$product['id_product_attribute'].'_' + .(isset($product['id_customization']) ? $product['id_customization'] : '0'); + + if (in_array($key, $product_id_list)) + $product_list[] = $product; + } + return $product_list; + } + /* DOES delete the product */ protected function _deleteProduct($orderDetail, $quantity) { @@ -340,7 +369,7 @@ class OrderCore extends ObjectModel $cart->update(); /* Update order */ - $shippingDiff = $this->total_shipping - $cart->getPackageShippingCost(); + $shippingDiff = $this->total_shipping - $cart->getPackageShippingCost($this->id_carrier, true, null, $this->getCartProducts()); $this->total_products -= $productPriceWithoutTax; // After upgrading from old version diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 83b8e554a..7e01aba96 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -980,15 +980,16 @@ class AdminOrdersControllerCore extends AdminController $order_invoice->total_paid_tax_incl = Tools::ps_round((float)$cart->getOrderTotal($use_taxes, Cart::BOTH), 2); $order_invoice->total_products = (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS); $order_invoice->total_products_wt = (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS); - $order_invoice->total_shipping_tax_excl = (float)$cart->getPackageShippingCost(null, false); - $order_invoice->total_shipping_tax_incl = (float)$cart->getPackageShippingCost(); + $order_invoice->total_shipping_tax_excl = (float)$cart->getTotalShippingCost(null, false); + $order_invoice->total_shipping_tax_incl = (float)$cart->getTotalShippingCost(); + $order_invoice->total_wrapping_tax_excl = abs($cart->getOrderTotal(false, Cart::ONLY_WRAPPING)); $order_invoice->total_wrapping_tax_incl = abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING)); // Update current order field, only shipping because other field is updated later - $order->total_shipping += (float)$cart->getPackageShippingCost(); - $order->total_shipping_tax_excl += (float)$cart->getPackageShippingCost(null, false); - $order->total_shipping_tax_incl += (float)$cart->getPackageShippingCost(null, $use_taxes); + $order->total_shipping += $order_invoice->total_shipping_tax_incl; + $order->total_shipping_tax_excl += $order_invoice->total_shipping_tax_excl; + $order->total_shipping_tax_incl += ($use_taxes) ? $order_invoice->total_shipping_tax_incl : $order_invoice->total_shipping_tax_excl; $order->total_wrapping += abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING)); $order->total_wrapping_tax_excl += abs($cart->getOrderTotal(false, Cart::ONLY_WRAPPING)); @@ -997,7 +998,13 @@ class AdminOrdersControllerCore extends AdminController // Adding an entry in order_carrier table Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'order_carrier` (`id_order`, `id_carrier`, `id_order_invoice`, `weight`, `shipping_cost_tax_excl`, `shipping_cost_tax_incl`, `date_add`) VALUES - ('.(int)$order->id.', '.(int)$order->id_carrier.', '.(int)$order_invoice->id.', '.(float)$cart->getTotalWeight().', '.(float)$cart->getPackageShippingCost(null, false).', '.(float)$cart->getPackageShippingCost(null, $use_taxes).', NOW())'); + ('.(int)$order->id.', + '.(int)$order->id_carrier.', + '.(int)$order_invoice->id.', + '.(float)$cart->getTotalWeight().', + '.$order_invoice->total_shipping_tax_excl.', + '.(($use_taxes) ? $order_invoice->total_shipping_tax_incl : $order_invoice->total_shipping_tax_excl).', + NOW())'); } // Update current invoice else @@ -1006,8 +1013,8 @@ class AdminOrdersControllerCore extends AdminController $order_invoice->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, Cart::BOTH)), 2); $order_invoice->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS); $order_invoice->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS); - $order_invoice->total_shipping_tax_excl += (float)$cart->getPackageShippingCost(null, false); - $order_invoice->total_shipping_tax_incl += (float)$cart->getPackageShippingCost(null, $use_taxes); + $order_invoice->total_shipping_tax_excl += (float)$cart->getTotalShippingCost(null, false); + $order_invoice->total_shipping_tax_incl += (float)$cart->getTotalShippingCost(null, $use_taxes); $order_invoice->total_wrapping_tax_excl += abs($cart->getOrderTotal(false, Cart::ONLY_WRAPPING)); $order_invoice->total_wrapping_tax_incl += abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING)); $order_invoice->update(); @@ -1015,11 +1022,11 @@ class AdminOrdersControllerCore extends AdminController } // Create Order detail information - $order_detail = new OrderDetail(); - $order_detail->createList($order, $cart, OrderHistory::getLastOrderState($order->id), $cart->getProducts(), (isset($order_invoice) ? $order_invoice->id : 0), $use_taxes); + $order_detail = new OrderDetail(); + $order_detail->createList($order, $cart, OrderHistory::getLastOrderState($order->id), $cart->getProducts(), (isset($order_invoice) ? $order_invoice->id : 0), $use_taxes); - // update totals amount of order - $order->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS); + // update totals amount of order + $order->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS); $order->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS); $order->total_paid += Tools::ps_round((float)($cart->getOrderTotal(true, Cart::BOTH)), 2); @@ -1039,19 +1046,19 @@ class AdminOrdersControllerCore extends AdminController $product = $products[max(array_keys($products))]; // Assign to smarty informations in order to show the new product line - $this->context->smarty->assign(array( - 'product' => $product, - 'order' => $order, - 'currency' => new Currency($order->id_currency), - 'can_edit' => $this->tabAccess['edit'], - 'invoices_collection' => $order->getInvoicesCollection() - )); + $this->context->smarty->assign(array( + 'product' => $product, + 'order' => $order, + 'currency' => new Currency($order->id_currency), + 'can_edit' => $this->tabAccess['edit'], + 'invoices_collection' => $order->getInvoicesCollection() + )); - die(Tools::jsonEncode(array( + die(Tools::jsonEncode(array( 'result' => true, 'view' => $this->context->smarty->fetch('orders/_product_line.tpl'), - 'can_edit' => $this->tabAccess['add'], - 'order' => $order + 'can_edit' => $this->tabAccess['add'], + 'order' => $order ))); } @@ -1174,25 +1181,25 @@ class AdminOrdersControllerCore extends AdminController $product = $products[$order_detail->id]; // Assign to smarty informations in order to show the new product line - $this->context->smarty->assign(array( - 'product' => $product, - 'order' => $order, - 'currency' => new Currency($order->id_currency), - 'can_edit' => $this->tabAccess['edit'] - )); + $this->context->smarty->assign(array( + 'product' => $product, + 'order' => $order, + 'currency' => new Currency($order->id_currency), + 'can_edit' => $this->tabAccess['edit'] + )); - if (!$res) - die(Tools::jsonEncode(array( + if (!$res) + die(Tools::jsonEncode(array( 'result' => $res, - 'error' => Tools::displayError('Error occured on edition of this product line') + 'error' => Tools::displayError('Error occured on edition of this product line') ))); - die(Tools::jsonEncode(array( + die(Tools::jsonEncode(array( 'result' => $res, 'view' => $this->context->smarty->fetch('orders/_product_line.tpl'), - 'can_edit' => $this->tabAccess['add'], - 'invoices_collection' => $order->getInvoicesCollection(), - 'order' => $order + 'can_edit' => $this->tabAccess['add'], + 'invoices_collection' => $order->getInvoicesCollection(), + 'order' => $order ))); } @@ -1252,14 +1259,14 @@ class AdminOrdersControllerCore extends AdminController $res &= $order_detail->delete(); if (!$res) - die(Tools::jsonEncode(array( + die(Tools::jsonEncode(array( 'result' => $res, - 'error' => Tools::displayError('Error occured on deletion of this product line') + 'error' => Tools::displayError('Error occured on deletion of this product line') ))); die(Tools::jsonEncode(array( 'result' => $res, - 'order' => $order + 'order' => $order ))); }