From d43d0bfec1c5657caea6e160a2ce1c395c05c899 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?=
 <marcosgdf@gmail.com>
Date: Tue, 8 Mar 2016 12:15:49 +0100
Subject: [PATCH] Fixes a little problem with multiprice rules, now undefined
 ones evaluate to 0% variation

---
 htdocs/product/admin/price_rules.php   | 16 ++++++++--------
 htdocs/product/class/product.class.php | 11 +++++------
 htdocs/product/price.php               |  2 +-
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/htdocs/product/admin/price_rules.php b/htdocs/product/admin/price_rules.php
index 64fa982fff1..e284572e7e7 100644
--- a/htdocs/product/admin/price_rules.php
+++ b/htdocs/product/admin/price_rules.php
@@ -52,10 +52,10 @@ if ($_POST) {
 			continue;
 		}
 
+		$i_var_percent = 0;
+
 		if ($i != 1) {
 			$i_var_percent = (float) price2num($var_percent[$i]);
-		} else {
-			$i_var_percent = 0;
 		}
 
 		$i_var_min_percent = (float) price2num($var_min_percent[$i]);
@@ -72,15 +72,15 @@ if ($_POST) {
 		if (!$check1 || !$check2) {
 
 			//If the level is between range but percent fields are empty, then we ensure it does not exist in DB
-			if ($check1 && !$check2) {
+			if ($check1) {
 				$db->query("DELETE FROM ".MAIN_DB_PREFIX."product_pricerules WHERE level = ".(int) $i);
 			}
 
 			continue;
 		}
 
-		$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules
-		SET level = ".(int) $i.", fk_level = ".$db->escape($i_fk_level).", var_percent = ".$i_var_percent.", var_min_percent = ".$i_var_min_percent;
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules (level, fk_level, var_percent, var_min_percent) VALUES (
+		".(int) $i.", ".$db->escape($i_fk_level).", ".$i_var_percent.", ".$i_var_min_percent.")";
 
 		if (!$db->query($sql)) {
 
@@ -166,7 +166,7 @@ $genPriceOptions = function($level) use ($price_options) {
 		<tr>
 			<td class="fieldrequired" style="text-align: center"><?php echo $langs->trans('SellingPrice') ?> 1</td>
 			<td></td>
-			<td style="text-align: center"><input type="text"  style="text-align: right" name="var_min_percent[1]" size="5" value="<?php echo isset($rules[1]) ? price($rules[1]->var_min_percent, 2) : '' ?>"> <?php echo $langs->trans('PercentDiscountOver', $langs->trans('SellingPrice').' 1') ?></td>
+			<td style="text-align: center"><input type="text"  style="text-align: right" name="var_min_percent[1]" size="5" value="<?php echo price(isset($rules[1]) ? $rules[1]->var_min_percent : 0, 2) ?>"> <?php echo $langs->trans('PercentDiscountOver', $langs->trans('SellingPrice').' 1') ?></td>
 		</tr>
 		<?php for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++): ?>
 			<tr>
@@ -179,11 +179,11 @@ $genPriceOptions = function($level) use ($price_options) {
 					?>
 					</td>
 				<td style="text-align: center">
-					<input type="text" style="text-align: right" name="var_percent[<?php echo $i ?>]" size="5" value="<?php echo isset($rules[$i]) ? price($rules[$i]->var_percent, 2) : '' ?>">
+					<input type="text" style="text-align: right" name="var_percent[<?php echo $i ?>]" size="5" value="<?php echo price(isset($rules[$i]) ? $rules[$i]->var_percent : 0, 2) ?>">
 					<?php echo $langs->trans('PercentVariationOver', Form::selectarray("fk_level[$i]", $genPriceOptions($i), (isset($rules[$i]) ? $rules[$i]->fk_level : null))) ?>
 				</td>
 				<td style="text-align: center">
-					<input type="text" style="text-align: right" name="var_min_percent[<?php echo $i ?>]" size="5" value="<?php echo isset($rules[$i]) ? price($rules[$i]->var_min_percent, 2) : '' ?>">
+					<input type="text" style="text-align: right" name="var_min_percent[<?php echo $i ?>]" size="5" value="<?php echo price(isset($rules[$i]) ? $rules[$i]->var_min_percent : 0, 2) ?>">
 					<?php echo $langs->trans('PercentDiscountOver', $langs->trans('SellingPrice').' '.$i) ?>
 				</td>
 			</tr>
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index 90b7f3cd994..66fe6edb19a 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -4108,13 +4108,12 @@ class Product extends CommonObject
 		for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
 
 			$price = $baseprice;
-			$price_min = 0;
+			$price_min = $baseprice;
 
-			if ($i > 1) {
-				//We have to make sure it does exist and it is > 0
-				if (isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
-					$price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent/100));
-				}
+			//We have to make sure it does exist and it is > 0
+			//First price level only allows changing min_price
+			if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
+				$price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent/100));
 			}
 
 			$prices[$i] = $price;
diff --git a/htdocs/product/price.php b/htdocs/product/price.php
index b6cf03fc837..cfdcfd33db7 100644
--- a/htdocs/product/price.php
+++ b/htdocs/product/price.php
@@ -194,7 +194,7 @@ if (empty($reshook))
 			$newlocaltax2_type = GETPOST('localtax2_type', 'array');
 
 			//Shall we generate prices using price rules?
-			$object->price_autogen = GETPOST('usePriceRules') == 'on' ? true : false;
+			$object->price_autogen = GETPOST('usePriceRules') == 'on';
 
 			for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++) 
 			{
-- 
GitLab