From bb4576c4abcd53fb38ec1890be092205adc45938 Mon Sep 17 00:00:00 2001 From: aFolletete Date: Tue, 20 Dec 2011 22:52:41 +0000 Subject: [PATCH] // when you adding a product on order, you can set a specific price git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@11444 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/Product.php | 20 ++++++++-- classes/SpecificPrice.php | 17 ++++++++- controllers/admin/AdminOrdersController.php | 42 +++++++++++++++++---- js/admin_order.js | 3 +- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/classes/Product.php b/classes/Product.php index e02fb0852..10dda838f 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2191,12 +2191,13 @@ class ProductCore extends ObjectModel * @param variable_reference $specificPriceOutput. * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @param boolean $with_ecotax insert ecotax in price output. + * @param bool $flush_cache set to true in order to flush getPriceStatic cache for these parameters * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, - $use_customer_price = true) + $use_customer_price = true, $flush_cache = false) { if (!$context) $context = Context::getContext(); @@ -2234,6 +2235,10 @@ class ProductCore extends ObjectModel $condition = ' AND `id_product_attribute` = '.(int)$id_product_attribute; } + // Flush cache + if (isset(self::$_cart_quantity[$cache_name]) && $flush_cache) + unset(self::$_cart_quantity[$cache_name]); + if (!isset(self::$_cart_quantity[$cache_name]) || self::$_cart_quantity[$cache_name] != (int)$quantity) { self::$_cart_quantity[$cache_name] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' @@ -2305,7 +2310,8 @@ class ProductCore extends ObjectModel $specific_price_output, $use_group_reduction, $id_customer, - $use_customer_price + $use_customer_price, + $flush_cache ); } @@ -2327,12 +2333,12 @@ class ProductCore extends ObjectModel * @param boolean $with_ecotax insert ecotax in price output. * @param variable_reference $specific_price_output * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object - * + * @param bool $flush_cache set to true in order to flush getPriceStatic cache for these parameters * @return float Product price **/ public static function priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency, $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $with_ecotax, &$specific_price, $use_group_reduction, - $id_customer = 0, $use_customer_price = true) + $id_customer = 0, $use_customer_price = true, $flush_cache = false) { if (!$use_customer_price) $id_customer = 0; @@ -2358,11 +2364,17 @@ class ProductCore extends ObjectModel $id_customer ); + // Flush cache + if (isset(self::$_prices[$cache_id]) && $flush_cache) + unset(self::$_prices[$cache_id]); + if (isset(self::$_prices[$cache_id])) return self::$_prices[$cache_id]; // fetch price & attribute price $cache_id_2 = $id_product.'-'.$id_product_attribute; + if (isset(self::$_pricesLevel2[$cache_id_2]) && $flush_cache) + unset(self::$_pricesLevel2[$cache_id_2]); if (!isset(self::$_pricesLevel2[$cache_id_2])) { $sql = new DbQuery(); diff --git a/classes/SpecificPrice.php b/classes/SpecificPrice.php index 48aff43e4..39525e136 100644 --- a/classes/SpecificPrice.php +++ b/classes/SpecificPrice.php @@ -75,6 +75,8 @@ class SpecificPriceCore extends ObjectModel { if (parent::add($autodate, $nullValues)) { + // Flush cache when we adding a new specific price + self::$_specificPriceCache = array(); // Set cache of feature detachable to true Configuration::updateGlobalValue('PS_SPECIFIC_PRICE_FEATURE_ACTIVE', '1'); return true; @@ -82,10 +84,21 @@ class SpecificPriceCore extends ObjectModel return false; } + public function update($null_values = false) + { + if (parent::update($null_values)) + { + // Flush cache when we updating a new specific price + self::$_specificPriceCache = array(); + } + } + public function delete() { if (parent::delete()) { + // Flush cache when we deletind a new specific price + self::$_specificPriceCache = array(); // Refresh cache of feature detachable Configuration::updateGlobalValue('PS_SPECIFIC_PRICE_FEATURE_ACTIVE', self::isCurrentlyUsed($this->def['table'])); return true; @@ -223,7 +236,7 @@ class SpecificPriceCore extends ObjectModel SELECT *, '.self::_getScoreQuery($id_product, $id_shop, $id_currency, $id_country, $id_group, $id_customer).' FROM `'._DB_PREFIX_.'specific_price` - WHERE + WHERE `id_product` IN(0, '.(int)$id_product.') AND '.(!$all_combinations ? '`id_product_attribute` IN(0, '.(int)$id_product_attribute.') AND ' : '').' `id_shop` IN(0, '.(int)$id_shop.') AND @@ -268,7 +281,7 @@ class SpecificPriceCore extends ObjectModel SELECT *, '.self::_getScoreQuery($id_product, $id_shop, $id_currency, $id_country, $id_group, $id_customer).' FROM `'._DB_PREFIX_.'specific_price` - WHERE + WHERE `id_product` IN(0, '.(int)$id_product.') AND `id_product_attribute` IN(0, '.(int)$id_product_attribute.') AND `id_shop` IN(0, '.(int)$id_shop.') AND diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 0fce85607..756aaaa65 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -1219,21 +1219,45 @@ class AdminOrdersControllerCore extends AdminController $this->context->cart = $cart; $this->context->customer = new Customer($order->id_customer); - // Add product to cart - $cart->updateQty($product_informations['product_quantity'], $product->id, isset($combination) ? $combination->id : null, false, 'up', new Shop($cart->id_shop)); - $use_taxes = ($order->getTaxCalculationMethod() == PS_TAX_INC); $initial_prodcut_price_tax_incl = Product::getPriceStatic($product->id, $use_taxes, isset($combination) ? $combination->id : null, 2, null, false, true, 1, false, $order->id_customer, $cart->id, $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); - if ($product_informations['product_price_tax_incl'] < $initial_prodcut_price_tax_incl) + // Creating specific price if needed + if ($product_informations['product_price_tax_incl'] != $initial_prodcut_price_tax_incl) { - $reduction_tax_incl = $initial_prodcut_price_tax_incl - $product_informations['product_price_tax_incl']; - - // TODO Use Specific price !!! + $specific_price = new SpecificPrice(); + $specific_price->id_shop = 0; + $specific_price->id_group_shop = 0; + $specific_price->id_currency = 0; + $specific_price->id_country = 0; + $specific_price->id_group = 0; + $specific_price->id_customer = $order->id_customer; + $specific_price->id_product = $product->id; + if (isset($combination)) + $specific_price->id_product_attribute = $combination->id; + else + $specific_price->id_product_attribute = 0; + $specific_price->price = $product_informations['product_price_tax_excl']; + $specific_price->from_quantity = 1; + $specific_price->reduction = 0; + $specific_price->reduction_type = 'amount'; + $specific_price->from = '0000-00-00 00:00:00'; + $specific_price->to = '0000-00-00 00:00:00'; + $specific_price->add(); } + // Flush cache + // @TODO clean the following line used only in order to clean cache of getPriceStatic method + $specific_price_output = null; + Product::getPriceStatic($product->id, $use_taxes, isset($combination) ? $combination->id : null, 2, null, false, true, 1, + false, $order->id_customer, $cart->id, $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $specific_price_output, true, true, null, + true, true); + + // Add product to cart + $cart->updateQty($product_informations['product_quantity'], $product->id, isset($combination) ? $combination->id : null, false, 'up', new Shop($cart->id_shop)); + // If order is valid, we can create a new invoice or edit an existing invoice if ($order->hasInvoice()) { @@ -1340,6 +1364,10 @@ class AdminOrdersControllerCore extends AdminController // Save changes of order $order->update(); + // Delete specific price if exists + if (isset($specific_price)) + $specific_price->delete(); + $products = $this->getProducts($order); // Get the last product $product = $products[max(array_keys($products))]; diff --git a/js/admin_order.js b/js/admin_order.js index 58f3c3fa4..b70921de1 100644 --- a/js/admin_order.js +++ b/js/admin_order.js @@ -370,7 +370,8 @@ function init() $('#add_product_product_stock').css('font-weight', 'normal').css('color', 'black').css('font-size', '1em');; }); - $('#submitAddProduct').click(function() { + $('#submitAddProduct').click(function(e) { + e.preventDefault(); var go = true; if ($('input#add_product_product_id').val() == 0)