[-] Core: Fix PSCFV-2109 specific prices when updating product

This commit is contained in:
rGaillard
2012-05-19 14:55:28 +00:00
parent c7df242539
commit 70fc1fdf4e
7 changed files with 62 additions and 19 deletions
@@ -324,6 +324,12 @@ jQuery(document).ready(Customer.init);
$('#id_product_attribute').change(function() { $('#id_product_attribute').change(function() {
$('#sp_current_ht_price').html(product_prices[$('#id_product_attribute option:selected').val()]); $('#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({ $('.datepicker').datetimepicker({
prevText: '', prevText: '',
@@ -355,12 +361,17 @@ jQuery(document).ready(Customer.init);
<span id="spm_currency_sign_pre_0" style="font-weight:bold; color:#000000; font-size:12px"> <span id="spm_currency_sign_pre_0" style="font-weight:bold; color:#000000; font-size:12px">
{$currency->prefix} {$currency->prefix}
</span> </span>
<input type="text" name="sp_price" value="{$product->price|string_format:'%.2f'}" size="11" /> <input type="text" disabled="disabled" name="sp_price" id="sp_price" value="{$product->price|string_format:'%.2f'}" size="11" />
<span id="spm_currency_sign_post_0" style="font-weight:bold; color:#000000; font-size:12px"> <span id="spm_currency_sign_post_0" style="font-weight:bold; color:#000000; font-size:12px">
{$currency->suffix} {$currency->suffix}
</span> </span>
</div> </div>
<label>
{l s='Leave base price:'}
</label>
<div class="margin-form">
<input id="leave_bprice" type="checkbox" value="1" checked="checked" name="leave_bprice" />
</div>
<label>{l s='Apply a discount of:'}</label> <label>{l s='Apply a discount of:'}</label>
<div class="margin-form"> <div class="margin-form">
<input type="text" name="sp_reduction" value="0.00" size="11" /> <input type="text" name="sp_reduction" value="0.00" size="11" />
@@ -162,6 +162,12 @@ function new_condition_group()
toggle_condition_group(last_condition_group); toggle_condition_group(last_condition_group);
} }
$(document).ready(function() { $(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) { $('#specific_price_rule_form').live('submit', function(e) {
var html = ''; var html = '';
$.each(conditions, function() { $.each(conditions, function() {
+2 -1
View File
@@ -2432,10 +2432,11 @@ class ProductCore extends ObjectModel
} }
$result = self::$_pricesLevel2[$cache_id_2]; $result = self::$_pricesLevel2[$cache_id_2];
if (!$specific_price || $specific_price['price'] <= 0) if (!$specific_price || $specific_price['price'] < 0)
$price = (float)$result['price']; $price = (float)$result['price'];
else else
$price = (float)$specific_price['price']; $price = (float)$specific_price['price'];
// convert only if the specific price is in the default currency (id_currency = 0) // 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'])) if (!$specific_price || !($specific_price['price'] >= 0 && $specific_price['id_currency']))
$price = Tools::convertPrice($price, $id_currency); $price = Tools::convertPrice($price, $id_currency);
+5 -4
View File
@@ -61,7 +61,7 @@ class SpecificPriceCore extends ObjectModel
'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_group' => 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), '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), 'from_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'reduction' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), 'reduction' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'reduction_type' => array('type' => self::TYPE_STRING, 'validate' => 'isReductionType', '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`) (`to` = \'0000-00-00 00:00:00\' OR \''.$now.'\' <= `to`)
) )
AND id_cart IN (0, '.(int)$id_cart.') 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]; return self::$_specificPriceCache[$key];
} }
@@ -398,8 +398,9 @@ class SpecificPriceCore extends ObjectModel
return Configuration::get('PS_SPECIFIC_PRICE_FEATURE_ACTIVE'); 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` return (int)Db::getInstance()->getValue('SELECT `id_specific_price`
FROM '._DB_PREFIX_.'specific_price FROM '._DB_PREFIX_.'specific_price
WHERE `id_product`='.(int)$id_product.' AND WHERE `id_product`='.(int)$id_product.' AND
@@ -411,7 +412,7 @@ class SpecificPriceCore extends ObjectModel
`id_customer`='.(int)$id_customer.' AND `id_customer`='.(int)$id_customer.' AND
`from_quantity`='.(int)$from_quantity.' AND `from_quantity`='.(int)$from_quantity.' AND
`from` >= \''.pSQL($from).'\' AND `from` >= \''.pSQL($from).'\' AND
`to` <= \''.pSQL($to).'\''); `to` <= \''.pSQL($to).'\''.$rule);
} }
} }
+1 -1
View File
@@ -52,7 +52,7 @@ class SpecificPriceRuleCore extends ObjectModel
'id_currency' => array('type' => self::TYPE_INT, 'required' => true), 'id_currency' => array('type' => self::TYPE_INT, 'required' => true),
'id_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'from_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', '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' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'reduction_type' => array('type' => self::TYPE_STRING, 'validate' => 'isReductionType', 'required' => true), 'reduction_type' => array('type' => self::TYPE_STRING, 'validate' => 'isReductionType', 'required' => true),
'from' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'required' => false), 'from' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'required' => false),
@@ -766,7 +766,7 @@ class AdminProductsControllerCore extends AdminController
$id_country = Tools::getValue('sp_id_country'); $id_country = Tools::getValue('sp_id_country');
$id_group = Tools::getValue('sp_id_group'); $id_group = Tools::getValue('sp_id_group');
$id_customer = Tools::getValue('sp_id_customer'); $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'); $from_quantity = Tools::getValue('sp_from_quantity');
$reduction = (float)(Tools::getValue('sp_reduction')); $reduction = (float)(Tools::getValue('sp_reduction'));
$reduction_type = !$reduction ? 'amount' : Tools::getValue('sp_reduction_type'); $reduction_type = !$reduction ? 'amount' : Tools::getValue('sp_reduction_type');
@@ -776,7 +776,6 @@ class AdminProductsControllerCore extends AdminController
$to = Tools::getValue('sp_to'); $to = Tools::getValue('sp_to');
if (!$to) if (!$to)
$to = '0000-00-00 00:00:00'; $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)) 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(); $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)) 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'); $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'); $this->errors[] = Tools::displayError('Invalid price/discount amount');
elseif (!Validate::isUnsignedInt($from_quantity)) elseif (!Validate::isUnsignedInt($from_quantity))
$this->errors[] = Tools::displayError('Invalid 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)'); $this->errors[] = Tools::displayError('Please select a discount type (amount or percentage)');
elseif ($from && $to && (!Validate::isDateFormat($from) || !Validate::isDateFormat($to))) elseif ($from && $to && (!Validate::isDateFormat($from) || !Validate::isDateFormat($to)))
$this->errors[] = Tools::displayError('Wrong from/to date'); $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'); $this->errors[] = Tools::displayError('A specific price already exists for these parameters');
else else
return true; return true;
@@ -2806,10 +2805,7 @@ class AdminProductsControllerCore extends AdminController
protected function _getFinalPrice($specific_price, $product_price, $tax_rate) protected function _getFinalPrice($specific_price, $product_price, $tax_rate)
{ {
$price = Tools::ps_round((float)$specific_price['price'] ? $specific_price['price'] : $product_price, 2); return $this->object->getPrice(false, $specific_price['id_product_attribute'], 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']);
} }
protected function _displaySpecificPriceModificationForm($defaultCurrency, $shops, $currencies, $countries, $groups) protected function _displaySpecificPriceModificationForm($defaultCurrency, $shops, $currencies, $countries, $groups)
@@ -2899,12 +2895,13 @@ class AdminProductsControllerCore extends AdminController
$content .= ' $content .= '
<td class="cell border">'.($id_shop_sp ? $shops[$id_shop_sp]['name'] : $this->l('All shops')).'</td>'; <td class="cell border">'.($id_shop_sp ? $shops[$id_shop_sp]['name'] : $this->l('All shops')).'</td>';
} }
$new_price = $specific_price['price'] == -1 ? $obj->price : $specific_price['price'];
$content .= ' $content .= '
<td class="cell border">'.($specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->l('All currencies')).'</td> <td class="cell border">'.($specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->l('All currencies')).'</td>
<td class="cell border">'.($specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->l('All countries')).'</td> <td class="cell border">'.($specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->l('All countries')).'</td>
<td class="cell border">'.($specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->l('All groups')).'</td> <td class="cell border">'.($specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->l('All groups')).'</td>
<td class="cell border" title="'.$this->l('ID:').' '.$specific_price['id_customer'].'">'.(isset($customer_full_name) ? $customer_full_name : $this->l('All customers')).'</td> <td class="cell border" title="'.$this->l('ID:').' '.$specific_price['id_customer'].'">'.(isset($customer_full_name) ? $customer_full_name : $this->l('All customers')).'</td>
<td class="cell border">'.Tools::displayPrice((float)$specific_price['price'], $current_specific_currency).'</td> <td class="cell border">'.Tools::displayPrice((float)$new_price, $current_specific_currency).'</td>
<td class="cell border">'.$reduction.'</td> <td class="cell border">'.$reduction.'</td>
<td class="cell border">'.$period.'</td> <td class="cell border">'.$period.'</td>
<td class="cell border">'.$specific_price['from_quantity'].'</th> <td class="cell border">'.$specific_price['from_quantity'].'</th>
@@ -127,6 +127,9 @@ class AdminSpecificPriceRuleControllerCore extends AdminController
public function renderForm() public function renderForm()
{ {
if (!$this->object->id)
$this->object->price = -1;
$this->fields_form = array( $this->fields_form = array(
'legend' => array( 'legend' => array(
'title' => $this->l('Specific price rules'), 'title' => $this->l('Specific price rules'),
@@ -195,9 +198,26 @@ class AdminSpecificPriceRuleControllerCore extends AdminController
'label' => $this->l('Price (tax excl.):'), 'label' => $this->l('Price (tax excl.):'),
'name' => 'price', 'name' => 'price',
'size' => 6, 'size' => 6,
'disabled' => ($this->object->price == -1 ? 1 : 0),
'maxlength' => 10, 'maxlength' => 10,
'suffix' => $this->context->currency->getSign('right'), '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( array(
'type' => 'date', 'type' => 'date',
@@ -233,11 +253,17 @@ class AdminSpecificPriceRuleControllerCore extends AdminController
'class' => 'button' 'class' => 'button'
), ),
); );
if ($value = $this->getFieldValue($this->object, 'price') != -1)
$price = number_format($value, 2);
else
$price = '';
$this->fields_value = array( $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), 'from_quantity' => (($value = $this->getFieldValue($this->object, 'from_quantity')) ? $value : 1),
'reduction' => number_format((($value = $this->getFieldValue($this->object, 'reduction')) ? $value : 0), 2), '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(); $attribute_groups = array();
$attributes = Attribute::getAttributes((int)$this->context->language->id); $attributes = Attribute::getAttributes((int)$this->context->language->id);
foreach ($attributes as $attribute) foreach ($attributes as $attribute)
@@ -270,6 +296,7 @@ class AdminSpecificPriceRuleControllerCore extends AdminController
public function processSave() public function processSave()
{ {
$_POST['price'] = Tools::getValue('leave_bprice_on') ? '-1' : Tools::getValue('price');
if (Validate::isLoadedObject(($object = parent::processSave()))) if (Validate::isLoadedObject(($object = parent::processSave())))
{ {
$object->deleteConditions(); $object->deleteConditions();