From 70fc1fdf4ee1ec0be357d816f085fdffcc06cf08 Mon Sep 17 00:00:00 2001 From: rGaillard Date: Sat, 19 May 2012 14:55:28 +0000 Subject: [PATCH] [-] Core: Fix PSCFV-2109 specific prices when updating product --- .../template/controllers/products/prices.tpl | 15 +++++++-- .../specific_price_rule/helpers/form/form.tpl | 6 ++++ classes/Product.php | 3 +- classes/SpecificPrice.php | 9 +++--- classes/SpecificPriceRule.php | 2 +- controllers/admin/AdminProductsController.php | 15 ++++----- .../AdminSpecificPriceRuleController.php | 31 +++++++++++++++++-- 7 files changed, 62 insertions(+), 19 deletions(-) diff --git a/admin-dev/themes/default/template/controllers/products/prices.tpl b/admin-dev/themes/default/template/controllers/products/prices.tpl index fe36e298c..278a59b59 100644 --- a/admin-dev/themes/default/template/controllers/products/prices.tpl +++ b/admin-dev/themes/default/template/controllers/products/prices.tpl @@ -324,6 +324,12 @@ jQuery(document).ready(Customer.init); $('#id_product_attribute').change(function() { $('#sp_current_ht_price').html(product_prices[$('#id_product_attribute option:selected').val()]); }); + $('#leave_bprice').click(function() { + if (this.checked) + $('#sp_price').attr('disabled', 'disabled'); + else + $('#sp_price').removeAttr('disabled'); + }); $('.datepicker').datetimepicker({ prevText: '', @@ -355,12 +361,17 @@ jQuery(document).ready(Customer.init); {$currency->prefix} - + {$currency->suffix} - + +
+ +
diff --git a/admin-dev/themes/default/template/controllers/specific_price_rule/helpers/form/form.tpl b/admin-dev/themes/default/template/controllers/specific_price_rule/helpers/form/form.tpl index c72014959..38b6deff2 100755 --- a/admin-dev/themes/default/template/controllers/specific_price_rule/helpers/form/form.tpl +++ b/admin-dev/themes/default/template/controllers/specific_price_rule/helpers/form/form.tpl @@ -162,6 +162,12 @@ function new_condition_group() toggle_condition_group(last_condition_group); } $(document).ready(function() { + $('#leave_bprice_on').click(function() { + if (this.checked) + $('#price').attr('disabled', 'disabled'); + else + $('#price').removeAttr('disabled'); + }); $('#specific_price_rule_form').live('submit', function(e) { var html = ''; $.each(conditions, function() { diff --git a/classes/Product.php b/classes/Product.php index 74eff4f12..ede2b511d 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2432,10 +2432,11 @@ class ProductCore extends ObjectModel } $result = self::$_pricesLevel2[$cache_id_2]; - if (!$specific_price || $specific_price['price'] <= 0) + if (!$specific_price || $specific_price['price'] < 0) $price = (float)$result['price']; else $price = (float)$specific_price['price']; + // convert only if the specific price is in the default currency (id_currency = 0) if (!$specific_price || !($specific_price['price'] >= 0 && $specific_price['id_currency'])) $price = Tools::convertPrice($price, $id_currency); diff --git a/classes/SpecificPrice.php b/classes/SpecificPrice.php index 20d65b322..977e1f936 100644 --- a/classes/SpecificPrice.php +++ b/classes/SpecificPrice.php @@ -61,7 +61,7 @@ class SpecificPriceCore extends ObjectModel 'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), - 'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), + 'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isNegativePrice', 'required' => true), 'from_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), 'reduction' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), 'reduction_type' => array('type' => self::TYPE_STRING, 'validate' => 'isReductionType', 'required' => true), @@ -230,7 +230,7 @@ class SpecificPriceCore extends ObjectModel (`to` = \'0000-00-00 00:00:00\' OR \''.$now.'\' <= `to`) ) AND id_cart IN (0, '.(int)$id_cart.') - ORDER BY `id_product_attribute` DESC, `from_quantity` DESC, `score` DESC'); + ORDER BY `id_specific_price_rule` ASC, `id_product_attribute` DESC, `from_quantity` DESC, `score` DESC'); } return self::$_specificPriceCache[$key]; } @@ -398,8 +398,9 @@ class SpecificPriceCore extends ObjectModel return Configuration::get('PS_SPECIFIC_PRICE_FEATURE_ACTIVE'); } - public static function exists($id_product, $id_product_attribute, $id_shop, $id_group, $id_country, $id_currency, $id_customer, $from_quantity, $from, $to) + public static function exists($id_product, $id_product_attribute, $id_shop, $id_group, $id_country, $id_currency, $id_customer, $from_quantity, $from, $to, $rule = false) { + $rule = ' AND `id_specific_price_rule`'.(!$rule ? '=0' : '!=0'); return (int)Db::getInstance()->getValue('SELECT `id_specific_price` FROM '._DB_PREFIX_.'specific_price WHERE `id_product`='.(int)$id_product.' AND @@ -411,7 +412,7 @@ class SpecificPriceCore extends ObjectModel `id_customer`='.(int)$id_customer.' AND `from_quantity`='.(int)$from_quantity.' AND `from` >= \''.pSQL($from).'\' AND - `to` <= \''.pSQL($to).'\''); + `to` <= \''.pSQL($to).'\''.$rule); } } diff --git a/classes/SpecificPriceRule.php b/classes/SpecificPriceRule.php index 9d0fb7cfe..b0d6241db 100755 --- a/classes/SpecificPriceRule.php +++ b/classes/SpecificPriceRule.php @@ -52,7 +52,7 @@ class SpecificPriceRuleCore extends ObjectModel 'id_currency' => array('type' => self::TYPE_INT, 'required' => true), 'id_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'from_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), - 'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), + 'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isNegativePrice', 'required' => true), 'reduction' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), 'reduction_type' => array('type' => self::TYPE_STRING, 'validate' => 'isReductionType', 'required' => true), 'from' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'required' => false), diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index b8bb3ceb2..b1fe7193d 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -766,7 +766,7 @@ class AdminProductsControllerCore extends AdminController $id_country = Tools::getValue('sp_id_country'); $id_group = Tools::getValue('sp_id_group'); $id_customer = Tools::getValue('sp_id_customer'); - $price = Tools::getValue('sp_price'); + $price = Tools::getValue('leave_bprice') ? '-1' : Tools::getValue('sp_price'); $from_quantity = Tools::getValue('sp_from_quantity'); $reduction = (float)(Tools::getValue('sp_reduction')); $reduction_type = !$reduction ? 'amount' : Tools::getValue('sp_reduction_type'); @@ -776,7 +776,6 @@ class AdminProductsControllerCore extends AdminController $to = Tools::getValue('sp_to'); if (!$to) $to = '0000-00-00 00:00:00'; - if ($this->_validateSpecificPrice($id_shop, $id_currency, $id_country, $id_group, $id_customer, $price, $from_quantity, $reduction, $reduction_type, $from, $to, $id_product_attribute)) { $specificPrice = new SpecificPrice(); @@ -1331,7 +1330,7 @@ class AdminProductsControllerCore extends AdminController { if (!Validate::isUnsignedId($id_shop) || !Validate::isUnsignedId($id_currency) || !Validate::isUnsignedId($id_country) || !Validate::isUnsignedId($id_group) || !Validate::isUnsignedId($id_customer)) $this->errors[] = Tools::displayError('Wrong IDs'); - elseif ((empty($price) && empty($reduction)) || (!empty($price) && !Validate::isPrice($price)) || (!empty($reduction) && !Validate::isPrice($reduction))) + elseif ((!isset($price) && !isset($reduction)) || (isset($price) && !Validate::isNegativePrice($price)) || (isset($reduction) && !Validate::isPrice($reduction))) $this->errors[] = Tools::displayError('Invalid price/discount amount'); elseif (!Validate::isUnsignedInt($from_quantity)) $this->errors[] = Tools::displayError('Invalid quantity'); @@ -1339,7 +1338,7 @@ class AdminProductsControllerCore extends AdminController $this->errors[] = Tools::displayError('Please select a discount type (amount or percentage)'); elseif ($from && $to && (!Validate::isDateFormat($from) || !Validate::isDateFormat($to))) $this->errors[] = Tools::displayError('Wrong from/to date'); - elseif (SpecificPrice::exists((int)$this->object->id, $id_combination, $id_shop, $id_group, $id_country, $id_currency, $id_customer, $from_quantity, $from, $to)) + elseif (SpecificPrice::exists((int)$this->object->id, $id_combination, $id_shop, $id_group, $id_country, $id_currency, $id_customer, $from_quantity, $from, $to, false)) $this->errors[] = Tools::displayError('A specific price already exists for these parameters'); else return true; @@ -2806,10 +2805,7 @@ class AdminProductsControllerCore extends AdminController protected function _getFinalPrice($specific_price, $product_price, $tax_rate) { - $price = Tools::ps_round((float)$specific_price['price'] ? $specific_price['price'] : $product_price, 2); - if (!(float)($specific_price['reduction'])) - return (float)($specific_price['price']); - return ($specific_price['reduction_type'] == 'amount') ? ($price - $specific_price['reduction'] / (1 + $tax_rate / 100)) : ($price - $price * $specific_price['reduction']); + return $this->object->getPrice(false, $specific_price['id_product_attribute'], 2); } protected function _displaySpecificPriceModificationForm($defaultCurrency, $shops, $currencies, $countries, $groups) @@ -2899,12 +2895,13 @@ class AdminProductsControllerCore extends AdminController $content .= ' '.($id_shop_sp ? $shops[$id_shop_sp]['name'] : $this->l('All shops')).''; } + $new_price = $specific_price['price'] == -1 ? $obj->price : $specific_price['price']; $content .= ' '.($specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->l('All currencies')).' '.($specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->l('All countries')).' '.($specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->l('All groups')).' '.(isset($customer_full_name) ? $customer_full_name : $this->l('All customers')).' - '.Tools::displayPrice((float)$specific_price['price'], $current_specific_currency).' + '.Tools::displayPrice((float)$new_price, $current_specific_currency).' '.$reduction.' '.$period.' '.$specific_price['from_quantity'].' diff --git a/controllers/admin/AdminSpecificPriceRuleController.php b/controllers/admin/AdminSpecificPriceRuleController.php index b7986fc20..12450bf4a 100755 --- a/controllers/admin/AdminSpecificPriceRuleController.php +++ b/controllers/admin/AdminSpecificPriceRuleController.php @@ -127,6 +127,9 @@ class AdminSpecificPriceRuleControllerCore extends AdminController public function renderForm() { + if (!$this->object->id) + $this->object->price = -1; + $this->fields_form = array( 'legend' => array( 'title' => $this->l('Specific price rules'), @@ -195,9 +198,26 @@ class AdminSpecificPriceRuleControllerCore extends AdminController 'label' => $this->l('Price (tax excl.):'), 'name' => 'price', 'size' => 6, + 'disabled' => ($this->object->price == -1 ? 1 : 0), 'maxlength' => 10, 'suffix' => $this->context->currency->getSign('right'), - 'desc' => $this->l('Put 0 to not change the price') + + ), + array( + 'type' => 'checkbox', + 'name' => 'leave_bprice', + 'values' => array( + 'query' => array( + array( + 'id' => 'on', + 'name' => $this->l('Leave base price'), + 'val' => '1', + 'checked' => '1' + ), + ), + 'id' => 'id', + 'name' => 'name' + ) ), array( 'type' => 'date', @@ -233,11 +253,17 @@ class AdminSpecificPriceRuleControllerCore extends AdminController 'class' => 'button' ), ); + if ($value = $this->getFieldValue($this->object, 'price') != -1) + $price = number_format($value, 2); + else + $price = ''; $this->fields_value = array( - 'price' => number_format((($value = $this->getFieldValue($this->object, 'price')) ? $value : 0), 2), + 'price' => $price, 'from_quantity' => (($value = $this->getFieldValue($this->object, 'from_quantity')) ? $value : 1), 'reduction' => number_format((($value = $this->getFieldValue($this->object, 'reduction')) ? $value : 0), 2), + 'leave_bprice_on' => ($value = $this->getFieldValue($this->object, 'price')) ? $value : 1 ); + $attribute_groups = array(); $attributes = Attribute::getAttributes((int)$this->context->language->id); foreach ($attributes as $attribute) @@ -270,6 +296,7 @@ class AdminSpecificPriceRuleControllerCore extends AdminController public function processSave() { + $_POST['price'] = Tools::getValue('leave_bprice_on') ? '-1' : Tools::getValue('price'); if (Validate::isLoadedObject(($object = parent::processSave()))) { $object->deleteConditions();