This commit is contained in:
Laurent Destailleur 2020-01-26 23:22:47 +01:00
parent 08841d4475
commit d13e5bc90b
2 changed files with 40 additions and 38 deletions

View File

@ -670,46 +670,47 @@ if (!empty($usemargins) && $user->rights->margins->creer)
var i = 0; var i = 0;
$(data).each(function() { $(data).each(function() {
if (this.id != 'pmpprice' && this.id != 'costprice') /* 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 i++;
// If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0) this.price = parseFloat(this.price); // to fix when this.price >0
//console.log("id="+this.id+"-price="+this.price+"-"+(this.price > 0)); // If margin is calculated on best supplier price, we set it by defaut (but only if value is not 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 //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 (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 margin is calculated on PMP, we set it by defaut (but only if value is not 0)
if ('pmp' == defaultbuyprice || 'costprice' == defaultbuyprice) 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; if (this.price > 0) {
//console.log("pmppricevalue="+pmppricevalue); defaultkey = this.id; defaultprice = this.price; pmppriceid = this.id; pmppricevalue = this.price;
} //console.log("pmppricevalue="+pmppricevalue);
} }
} }
if (this.id == 'costprice') }
{ 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 margin is calculated on Cost price, we set it by defaut (but only if value is not 0)
if ('costprice' == defaultbuyprice) 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; } 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 += '<option value="'+this.id+'" price="'+this.price+'">'+this.label+'</option>'; }
}); options += '<option value="'+this.id+'" price="'+this.price+'">'+this.label+'</option>';
options += '<option value="inputprice" price="'+defaultprice+'"><?php echo $langs->trans("InputPrice"); ?></option>'; });
options += '<option value="inputprice" price="'+defaultprice+'"><?php echo $langs->trans("InputPrice"); ?></option>';
console.log("finally selected defaultkey="+defaultkey+" defaultprice="+defaultprice); console.log("finally selected defaultkey="+defaultkey+" defaultprice="+defaultprice);
$("#fournprice_predef").html(options).show(); $("#fournprice_predef").html(options).show();
if (defaultkey != '') if (defaultkey != '')
{ {
$("#fournprice_predef").val(defaultkey); $("#fournprice_predef").val(defaultkey);
} }
/* At loading, no product are yet selected, so we hide field of buying_price */ /* At loading, no product are yet selected, so we hide field of buying_price */

View File

@ -84,16 +84,17 @@ if ($idprod > 0)
} }
} }
// Add price for costprice // After best supplier prices and before 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()
if(!empty($conf->stock->enabled)) if(!empty($conf->stock->enabled))
{ {
// Add price for pmp // Add price for pmp
$price=$producttmp->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() $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); echo json_encode($prices);