diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php
index a1d2ac3cf02..a4a94fe66c9 100644
--- a/htdocs/core/lib/ajax.lib.php
+++ b/htdocs/core/lib/ajax.lib.php
@@ -171,6 +171,7 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt
}
});
}
+ console.log("ajax_autocompleter new value selected, we trigger change");
$("#search_'.$htmlname.'").trigger("change"); // To tell that input text field was modified
}
,delay: 500
diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php
index 938a615ea77..b779dae443b 100644
--- a/htdocs/core/tpl/objectline_create.tpl.php
+++ b/htdocs/core/tpl/objectline_create.tpl.php
@@ -489,6 +489,7 @@ jQuery(document).ready(function() {
});
$("#prod_entry_mode_predef").on( "click", function() {
+ console.log("click prod_entry_mode_predef");
setforpredef();
jQuery('#trlinefordates').show();
});
@@ -496,6 +497,7 @@ jQuery(document).ready(function() {
/* When changing predefined product, we reload list of supplier prices */
$("#idprod, #idprodfournprice").change(function()
{
+ console.log("change #idprod, #idprodfournprice, conf->global->MARGIN_TYPE=global->MARGIN_TYPE ?>");
setforpredef();
jQuery('#trlinefordates').show();
@@ -516,6 +518,7 @@ jQuery(document).ready(function() {
var options = '';
var defaultkey = '';
var defaultprice = '';
+ var bestpricefound = 0;
var i = 0;
$(data).each(function() {
if (this.id != 'pmpprice')
@@ -523,24 +526,23 @@ jQuery(document).ready(function() {
i++;
// If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0)
- var defaultbuyprice = 'global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == '1')?'bestsupplierprice':''); ?>';
- if (i == 1 && this.price > 0 && 'bestsupplierprice' == defaultbuyprice) { defaultkey = this.id; defaultprice = this.price; }
-
- options += '';
+ var defaultbuyprice = 'global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == '1')?'bestsupplierprice':''); ?>'; // We set here default value to use
+ console.log(this.id+" "+this.price+" "+defaultbuyprice+" "+(this.price > 0));
+ if (bestpricefound == 0 && this.price > 0 && 'bestsupplierprice' == defaultbuyprice) { defaultkey = this.id; defaultprice = 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)
var defaultbuyprice = 'global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == 'pmp')?'pmp':''); ?>';
+ console.log(this.id+" "+this.price+" "+defaultbuyprice);
if (this.price > 0 && 'pmp' == defaultbuyprice) { defaultkey = this.id; defaultprice = this.price; }
-
- options += '';
}
+ options += '';
});
options += '';
+ console.log("defaultkey="+defaultkey);
+
$("#fournprice_predef").html(options).show();
if (defaultkey != '')
{
@@ -555,6 +557,7 @@ jQuery(document).ready(function() {
$("#buying_price").val(Math.round(defaultprice,global->MAIN_MAX_DECIMALS_UNIT ? $conf->global->MAIN_MAX_DECIMALS_UNIT : 5); ?>));
$("#fournprice_predef").change(function() {
+ console.log("change on fournprice_predef");
/* Hide field buying_price according to choice into list (if 'inputprice' or not) */
var linevalue=$(this).find('option:selected').val();
var pricevalue = $(this).find('option:selected').attr("price");
diff --git a/htdocs/fourn/ajax/getSupplierPrices.php b/htdocs/fourn/ajax/getSupplierPrices.php
index c39acd9dd29..664c8a2da4e 100644
--- a/htdocs/fourn/ajax/getSupplierPrices.php
+++ b/htdocs/fourn/ajax/getSupplierPrices.php
@@ -38,7 +38,7 @@ $langs->load('stocks');
/*
* View
-*/
+ */
top_httphead();
@@ -59,7 +59,7 @@ if ($idprod > 0)
$sql.= " WHERE pfp.fk_product = ".$idprod;
$sql.= " AND p.tobuy = 1";
$sql.= " AND s.fournisseur = 1";
- $sql.= " ORDER BY s.nom, pfp.ref_fourn DESC";
+ $sql.= " ORDER BY pfp.unitprice, s.nom, pfp.ref_fourn DESC"; // Best price first
dol_syslog("Ajax::getSupplierPrices", LOG_DEBUG);
$result=$db->query($sql);
@@ -117,7 +117,7 @@ if ($idprod > 0)
$label = price($price,0,$langs,0,0,-1,$conf->currency)."/".$langs->trans("Unit");
if ($objp->ref_fourn) $label.=' ('.$objp->ref_fourn.')';
- $prices[] = array("id" => $objp->idprodfournprice, "price" => price($price,0,'',0), "label" => $label, "title" => $title);
+ $prices[] = array("id" => $objp->idprodfournprice, "price" => price2num($price,0,'',0), "label" => $label, "title" => $title); // For price field, we must use price2num(), for label or title, price()
$i++;
}