diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php
index 9056f3bde9d..b60c48da09c 100644
--- a/htdocs/core/tpl/objectline_create.tpl.php
+++ b/htdocs/core/tpl/objectline_create.tpl.php
@@ -670,46 +670,47 @@ if (!empty($usemargins) && $user->rights->margins->creer)
var i = 0;
$(data).each(function() {
- if (this.id != 'pmpprice' && this.id != 'costprice')
- {
- i++;
- this.price = parseFloat(this.price); // to fix when this.price >0
- // If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0)
- //console.log("id="+this.id+"-price="+this.price+"-"+(this.price > 0));
- if (bestpricefound == 0 && this.price > 0) { defaultkey = this.id; defaultprice = this.price; bestpriceid = this.id; bestpricevalue = this.price; bestpricefound=1; } // bestpricefound is used to take the first price > 0
- }
- if (this.id == 'pmpprice')
- {
- // If margin is calculated on PMP, we set it by defaut (but only if value is not 0)
- //console.log("id="+this.id+"-price="+this.price);
- if ('pmp' == defaultbuyprice || 'costprice' == defaultbuyprice)
- {
- if (this.price > 0) {
- defaultkey = this.id; defaultprice = this.price; pmppriceid = this.id; pmppricevalue = this.price;
- //console.log("pmppricevalue="+pmppricevalue);
- }
- }
- }
- if (this.id == 'costprice')
- {
- // If margin is calculated on Cost price, we set it by defaut (but only if value is not 0)
- //console.log("id="+this.id+"-price="+this.price+"-pmppricevalue="+pmppricevalue);
- if ('costprice' == defaultbuyprice)
- {
- if (this.price > 0) { defaultkey = this.id; defaultprice = this.price; costpriceid = this.id; costpricevalue = this.price; }
- else if (pmppricevalue > 0) { defaultkey = pmppriceid; defaultprice = pmppricevalue; }
- }
- }
- options += '';
- });
- options += '';
+ /* Warning: Lines must be processed in order: best supplier price, then pmpprice line then costprice */
+ if (this.id != 'pmpprice' && this.id != 'costprice')
+ {
+ i++;
+ this.price = parseFloat(this.price); // to fix when this.price >0
+ // If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0)
+ //console.log("id="+this.id+"-price="+this.price+"-"+(this.price > 0));
+ if (bestpricefound == 0 && this.price > 0) { defaultkey = this.id; defaultprice = this.price; bestpriceid = this.id; bestpricevalue = this.price; bestpricefound=1; } // bestpricefound is used to take the first price > 0
+ }
+ if (this.id == 'pmpprice')
+ {
+ // If margin is calculated on PMP, we set it by defaut (but only if value is not 0)
+ console.log("id="+this.id+"-price="+this.price);
+ if ('pmp' == defaultbuyprice || 'costprice' == defaultbuyprice)
+ {
+ if (this.price > 0) {
+ defaultkey = this.id; defaultprice = this.price; pmppriceid = this.id; pmppricevalue = this.price;
+ //console.log("pmppricevalue="+pmppricevalue);
+ }
+ }
+ }
+ if (this.id == 'costprice')
+ {
+ // If margin is calculated on Cost price, we set it by defaut (but only if value is not 0)
+ console.log("id="+this.id+"-price="+this.price+"-pmppricevalue="+pmppricevalue);
+ if ('costprice' == defaultbuyprice)
+ {
+ if (this.price > 0) { defaultkey = this.id; defaultprice = this.price; costpriceid = this.id; costpricevalue = this.price; }
+ else if (pmppricevalue > 0) { defaultkey = 'pmpprice'; defaultprice = pmppricevalue; }
+ }
+ }
+ options += '';
+ });
+ options += '';
console.log("finally selected defaultkey="+defaultkey+" defaultprice="+defaultprice);
$("#fournprice_predef").html(options).show();
if (defaultkey != '')
{
- $("#fournprice_predef").val(defaultkey);
+ $("#fournprice_predef").val(defaultkey);
}
/* At loading, no product are yet selected, so we hide field of buying_price */
diff --git a/htdocs/fourn/ajax/getSupplierPrices.php b/htdocs/fourn/ajax/getSupplierPrices.php
index 24c68a6aced..2d18c6ea86e 100644
--- a/htdocs/fourn/ajax/getSupplierPrices.php
+++ b/htdocs/fourn/ajax/getSupplierPrices.php
@@ -84,16 +84,17 @@ if ($idprod > 0)
}
}
- // Add price for costprice
- $price=$producttmp->cost_price;
- $prices[] = array("id" => 'costprice', "price" => price2num($price), "label" => $langs->trans("CostPrice").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
-
+ // After best supplier prices and before costprice
if(!empty($conf->stock->enabled))
{
// Add price for pmp
$price=$producttmp->pmp;
$prices[] = array("id" => 'pmpprice', "price" => price2num($price), "label" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
}
+
+ // Add price for costprice (at end)
+ $price=$producttmp->cost_price;
+ $prices[] = array("id" => 'costprice', "price" => price2num($price), "label" => $langs->trans("CostPrice").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
}
echo json_encode($prices);