From dbc87aa0b08f8ed95cd97a6d4dc9059e2fcb62ff Mon Sep 17 00:00:00 2001 From: rGaillard Date: Tue, 2 Oct 2012 10:21:04 +0000 Subject: [PATCH] // Fix some bug within sharing orders between shops --- classes/Cart.php | 71 ++++++++++++++++----- classes/Product.php | 17 +++-- classes/tax/Tax.php | 7 +- controllers/front/ParentOrderController.php | 22 ++++++- 4 files changed, 93 insertions(+), 24 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 87962da33..27ac63854 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -205,12 +205,12 @@ class CartCore extends ObjectModel public function updateAddressId($id_address, $id_address_new) { $to_update = false; - if ($this->id_address_invoice == $id_address) + if (!isset($this->id_address_invoice) || $this->id_address_invoice == $id_address) { $to_update = true; $this->context->cart->id_address_invoice = $id_address_new; } - if ($this->id_address_delivery == $id_address) + if (!isset($this->id_address_delivery) || $this->id_address_delivery == $id_address) { $to_update = true; $this->id_address_delivery = $id_address_new; @@ -223,6 +223,12 @@ class CartCore extends ObjectModel WHERE `id_cart` = '.(int)$this->id.' AND `id_address_delivery` = '.(int)$id_address; Db::getInstance()->execute($sql); + + $sql = 'UPDATE `'._DB_PREFIX_.'customization` + SET `id_address_delivery` = '.(int)$id_address_new.' + WHERE `id_cart` = '.(int)$this->id.' + AND `id_address_delivery` = '.(int)$id_address; + Db::getInstance()->execute($sql); } public function delete() @@ -411,6 +417,12 @@ class CartCore extends ObjectModel return $this->_products; } + $shop_group = Shop::getGroupFromShop(Shop::getContextShopID(), false); + if ($shop_group['share_order']) + $id_shop = 'cp.id_shop'; + else + $id_shop = (int)Shop::getContextShopID(); + if (!$id_country) $id_country = Context::getContext()->country->id; @@ -431,10 +443,10 @@ class CartCore extends ObjectModel // Build JOIN $sql->leftJoin('product', 'p', 'p.`id_product` = cp.`id_product`'); - $sql->join(Shop::addSqlAssociation('product', 'p')); + $sql->innerJoin('product_shop', 'product_shop', 'product_shop.id_shop='.$id_shop); $sql->leftJoin('product_lang', 'pl', ' p.`id_product` = pl.`id_product` - AND pl.`id_lang` = '.(int)$this->id_lang.Shop::addSqlRestrictionOnLang('pl') + AND pl.`id_lang` = '.(int)$this->id_lang.Shop::addSqlRestrictionOnLang('pl', $id_shop) ); $sql->leftJoin('tax_rule', 'tr', ' @@ -451,7 +463,7 @@ class CartCore extends ObjectModel $sql->leftJoin('category_lang', 'cl', ' product_shop.`id_category_default` = cl.`id_category` - AND cl.`id_lang` = '.(int)$this->id_lang.Shop::addSqlRestrictionOnLang('cl') + AND cl.`id_lang` = '.(int)$this->id_lang.Shop::addSqlRestrictionOnLang('cl', $id_shop) ); // @todo test if everything is ok, then refactorise call of this method @@ -492,7 +504,7 @@ class CartCore extends ObjectModel '); $sql->leftJoin('product_attribute', 'pa', 'pa.`id_product_attribute` = cp.`id_product_attribute`'); - $sql->join(Shop::addSqlAssociation('product_attribute', 'pa', false)); + $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', 'product_attribute_shop.id_shop='.$id_shop); $sql->leftJoin('product_attribute_image', 'pai', 'pai.`id_product_attribute` = pa.`id_product_attribute`'); $sql->leftJoin('image_lang', 'il', 'il.id_image = pai.id_image AND il.id_lang = '.(int)$this->id_lang); } @@ -519,6 +531,7 @@ class CartCore extends ObjectModel if (empty($result)) return array(); + $cart_shop_context = Context::getContext()->cloneContext(); foreach ($result as $row) { if (isset($row['ecotax_attr']) && $row['ecotax_attr'] > 0) @@ -538,6 +551,9 @@ class CartCore extends ObjectModel if (!Address::addressExists($address_id)) $address_id = null; + if ($cart_shop_context->shop->id != $row['id_shop']) + $cart_shop_context->shop = new Shop((int)$row['id_shop']); + if ($this->_taxCalculationMethod == PS_TAX_EXC) { $row['price'] = Product::getPriceStatic( @@ -553,7 +569,10 @@ class CartCore extends ObjectModel ((int)$this->id_customer ? (int)$this->id_customer : null), (int)$this->id, ((int)$address_id ? (int)$address_id : null), - $specific_price_output + $specific_price_output, + true, + true, + $cart_shop_context ); // Here taxes are computed only once the quantity has been applied to the product price $row['price_wt'] = Product::getPriceStatic( @@ -568,7 +587,11 @@ class CartCore extends ObjectModel false, ((int)$this->id_customer ? (int)$this->id_customer : null), (int)$this->id, - ((int)$address_id ? (int)$address_id : null) + ((int)$address_id ? (int)$address_id : null), + null, + true, + true, + $cart_shop_context ); $tax_rate = Tax::getProductTaxRate((int)$row['id_product'], (int)$address_id); @@ -591,7 +614,10 @@ class CartCore extends ObjectModel ((int)$this->id_customer ? (int)$this->id_customer : null), (int)$this->id, ((int)$address_id ? (int)$address_id : null), - $specific_price_output + $specific_price_output, + true, + true, + $cart_shop_context ); $row['price_wt'] = Product::getPriceStatic( @@ -606,9 +632,13 @@ class CartCore extends ObjectModel false, ((int)$this->id_customer ? (int)$this->id_customer : null), (int)$this->id, - ((int)$address_id ? (int)$address_id : null) + ((int)$address_id ? (int)$address_id : null), + $null, + true, + true, + $cart_shop_context ); - + // In case when you use QuantityDiscount, getPriceStatic() can be return more of 2 decimals $row['price_wt'] = Tools::ps_round($row['price_wt'], 2); $row['total_wt'] = $row['price_wt'] * (int)$row['cart_quantity']; @@ -1338,6 +1368,9 @@ class CartCore extends ObjectModel foreach ($products as $product) // products refer to the cart details { + if ($virtual_context->shop->id != $product['id_shop']) + $virtual_context->shop = new Shop((int)$product['id_shop']); + if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') $address_id = (int)$this->id_address_invoice; else @@ -1360,7 +1393,11 @@ class CartCore extends ObjectModel false, (int)$this->id_customer ? (int)$this->id_customer : null, (int)$this->id, - $address_id + $address_id, + $null, + true, + true, + $virtual_context ); $total_ecotax = $product['ecotax'] * (int)$product['cart_quantity']; @@ -1368,7 +1405,7 @@ class CartCore extends ObjectModel if ($with_taxes) { - $product_tax_rate = (float)Tax::getProductTaxRate((int)$product['id_product'], (int)$address_id); + $product_tax_rate = (float)Tax::getProductTaxRate((int)$product['id_product'], (int)$address_id, $virtual_context); $product_eco_tax_rate = Tax::getProductEcotaxRate((int)$address_id); $total_price = ($total_price - $total_ecotax) * (1 + $product_tax_rate / 100); @@ -1390,14 +1427,18 @@ class CartCore extends ObjectModel false, ((int)$this->id_customer ? (int)$this->id_customer : null), (int)$this->id, - ((int)$address_id ? (int)$address_id : null) + ((int)$address_id ? (int)$address_id : null), + $null, + true, + true, + $virtual_context ); $total_price = Tools::ps_round($price, 2) * (int)$product['cart_quantity']; if (!$with_taxes) { - $product_tax_rate = (float)Tax::getProductTaxRate((int)$product['id_product'], (int)$address_id); + $product_tax_rate = (float)Tax::getProductTaxRate((int)$product['id_product'], (int)$address_id, $virtual_context); $total_price = Tools::ps_round($total_price / (1 + ($product_tax_rate / 100)), 2); } } diff --git a/classes/Product.php b/classes/Product.php index aa50097a4..05c802f4c 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2487,10 +2487,17 @@ class ProductCore extends ObjectModel $id_customer = 0, $use_customer_price = true, $id_cart = 0, $real_quantity = 0) { static $address = null; + static $context = null; if ($address === null) $address = new Address(); + if ($context == null) + $context = Context::getContext()->cloneContext(); + + if ($context->shop->id != (int)$id_shop) + $context->shop = new Shop((int)$id_shop); + if (!$use_customer_price) $id_customer = 0; @@ -2522,17 +2529,16 @@ class ProductCore extends ObjectModel $sql = new DbQuery(); $sql->select('product_shop.`price`, product_shop.`ecotax`'); $sql->from('product', 'p'); - $sql->join(Shop::addSqlAssociation('product', 'p')); + $sql->innerJoin('product_shop', 'product_shop', '(product_shop.id_product=p.id_product AND product_shop.id_shop='.(int)$id_shop.')'); $sql->where('p.`id_product` = '.(int)$id_product); if (Combination::isFeatureActive()) { $sql->select('product_attribute_shop.id_product_attribute, product_attribute_shop.`price` AS attribute_price, product_attribute_shop.default_on'); $sql->leftJoin('product_attribute', 'pa', 'pa.`id_product` = p.`id_product`'); - $sql->join(Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.id_shop ='.(int)$id_shop)); + $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.id_product_attribute=pa.id_product AND product_attribute_shop.id_shop='.(int)$id_shop.')'); } else $sql->select('0 as id_product_attribute'); - $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); foreach ($res as $row) { @@ -2573,7 +2579,7 @@ class ProductCore extends ObjectModel $address->id_state = $id_state; $address->postcode = $zipcode; - $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int)$id_product)); + $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int)$id_product, $context)); $product_tax_calculator = $tax_manager->getTaxCalculator(); // Add Tax @@ -4197,14 +4203,13 @@ class ProductCore extends ObjectModel { if (!$context) $context = Context::getContext(); - $key = 'product_id_tax_rules_group_'.(int)$id_product.'_'.(int)$context->shop->id; if (!Cache::isStored($key)) Cache::store($key, Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_tax_rules_group` FROM `'._DB_PREFIX_.'product_shop` - WHERE `id_product` = '.(int)$id_product.' AND id_shop='.(int)Context::getContext()->shop->id)); + WHERE `id_product` = '.(int)$id_product.' AND id_shop='.(int)$context->shop->id)); return Cache::retrieve($key); } diff --git a/classes/tax/Tax.php b/classes/tax/Tax.php index ba9f1fcac..ef2294717 100644 --- a/classes/tax/Tax.php +++ b/classes/tax/Tax.php @@ -250,10 +250,13 @@ class TaxCore extends ObjectModel * @param integer $id_country * @return Tax */ - public static function getProductTaxRate($id_product, $id_address = null) + public static function getProductTaxRate($id_product, $id_address = null, Context $context = null) { + if ($context == null) + $context = Context::getContext(); + $address = Address::initialize($id_address); - $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product); + $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context); $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules); $tax_calculator = $tax_manager->getTaxCalculator(); diff --git a/controllers/front/ParentOrderController.php b/controllers/front/ParentOrderController.php index 53b752a95..26a9d18a4 100644 --- a/controllers/front/ParentOrderController.php +++ b/controllers/front/ParentOrderController.php @@ -288,10 +288,30 @@ class ParentOrderControllerCore extends FrontController Product::addCustomizationPrice($summary['products'], $customizedDatas); } + $cart_product_context = Context::getContext()->cloneContext(); foreach ($summary['products'] as $key => &$product) { $product['quantity'] = $product['cart_quantity'];// for compatibility with 1.2 themes - $product['price_without_specific_price'] = Product::getPriceStatic($product['id_product'], !Product::getTaxCalculationMethod(), $product['id_product_attribute'], 2, null, false, false); + + if ($cart_product_context->shop->id != $product['id_shop']) + $cart_product_context->shop = new Shop((int)$product['id_shop']); + $product['price_without_specific_price'] = Product::getPriceStatic($product['id_product'], + !Product::getTaxCalculationMethod(), + $product['id_product_attribute'], + 2, + null, + false, + false, + 1, + false, + null, + null, + null, + $null, + true, + true, + $cart_product_context); + if (Product::getTaxCalculationMethod()) $product['is_discounted'] = $product['price_without_specific_price'] != $product['price']; else