FIX #3661 Margin is not selected correctly when adding a product

This commit is contained in:
Laurent Destailleur 2015-11-05 01:05:17 +01:00
parent 826cc6db1b
commit 77371af35a
3 changed files with 15 additions and 11 deletions

View File

@ -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 $("#search_'.$htmlname.'").trigger("change"); // To tell that input text field was modified
} }
,delay: 500 ,delay: 500

View File

@ -489,6 +489,7 @@ jQuery(document).ready(function() {
}); });
$("#prod_entry_mode_predef").on( "click", function() { $("#prod_entry_mode_predef").on( "click", function() {
console.log("click prod_entry_mode_predef");
setforpredef(); setforpredef();
jQuery('#trlinefordates').show(); jQuery('#trlinefordates').show();
}); });
@ -496,6 +497,7 @@ jQuery(document).ready(function() {
/* When changing predefined product, we reload list of supplier prices */ /* When changing predefined product, we reload list of supplier prices */
$("#idprod, #idprodfournprice").change(function() $("#idprod, #idprodfournprice").change(function()
{ {
console.log("change #idprod, #idprodfournprice, conf->global->MARGIN_TYPE=<?php echo $conf->global->MARGIN_TYPE ?>");
setforpredef(); setforpredef();
jQuery('#trlinefordates').show(); jQuery('#trlinefordates').show();
@ -516,6 +518,7 @@ jQuery(document).ready(function() {
var options = ''; var options = '';
var defaultkey = ''; var defaultkey = '';
var defaultprice = ''; var defaultprice = '';
var bestpricefound = 0;
var i = 0; var i = 0;
$(data).each(function() { $(data).each(function() {
if (this.id != 'pmpprice') if (this.id != 'pmpprice')
@ -523,24 +526,23 @@ jQuery(document).ready(function() {
i++; i++;
// If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0) // If margin is calculated on best supplier price, we set it by defaut (but only if value is not 0)
var defaultbuyprice = '<?php echo ((isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == '1')?'bestsupplierprice':''); ?>'; var defaultbuyprice = '<?php echo ((isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == '1')?'bestsupplierprice':''); ?>'; // We set here default value to use
if (i == 1 && this.price > 0 && 'bestsupplierprice' == defaultbuyprice) { defaultkey = this.id; defaultprice = this.price; } 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
options += '<option value="'+this.id+'" price="'+this.price+'"';
if (this.price > 0 && i == 1) { defaultkey = this.id; defaultprice = this.price; }
options += '>'+this.label+'</option>';
} }
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) // If margin is calculated on PMP, we set it by defaut (but only if value is not 0)
var defaultbuyprice = '<?php echo ((isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == 'pmp')?'pmp':''); ?>'; var defaultbuyprice = '<?php echo ((isset($conf->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; } if (this.price > 0 && 'pmp' == defaultbuyprice) { defaultkey = this.id; defaultprice = this.price; }
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("defaultkey="+defaultkey);
$("#fournprice_predef").html(options).show(); $("#fournprice_predef").html(options).show();
if (defaultkey != '') if (defaultkey != '')
{ {
@ -555,6 +557,7 @@ jQuery(document).ready(function() {
$("#buying_price").val(Math.round(defaultprice,<?php print ($conf->global->MAIN_MAX_DECIMALS_UNIT ? $conf->global->MAIN_MAX_DECIMALS_UNIT : 5); ?>)); $("#buying_price").val(Math.round(defaultprice,<?php print ($conf->global->MAIN_MAX_DECIMALS_UNIT ? $conf->global->MAIN_MAX_DECIMALS_UNIT : 5); ?>));
$("#fournprice_predef").change(function() { $("#fournprice_predef").change(function() {
console.log("change on fournprice_predef");
/* Hide field buying_price according to choice into list (if 'inputprice' or not) */ /* Hide field buying_price according to choice into list (if 'inputprice' or not) */
var linevalue=$(this).find('option:selected').val(); var linevalue=$(this).find('option:selected').val();
var pricevalue = $(this).find('option:selected').attr("price"); var pricevalue = $(this).find('option:selected').attr("price");

View File

@ -38,7 +38,7 @@ $langs->load('stocks');
/* /*
* View * View
*/ */
top_httphead(); top_httphead();
@ -59,7 +59,7 @@ if ($idprod > 0)
$sql.= " WHERE pfp.fk_product = ".$idprod; $sql.= " WHERE pfp.fk_product = ".$idprod;
$sql.= " AND p.tobuy = 1"; $sql.= " AND p.tobuy = 1";
$sql.= " AND s.fournisseur = 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); dol_syslog("Ajax::getSupplierPrices", LOG_DEBUG);
$result=$db->query($sql); $result=$db->query($sql);
@ -117,7 +117,7 @@ if ($idprod > 0)
$label = price($price,0,$langs,0,0,-1,$conf->currency)."/".$langs->trans("Unit"); $label = price($price,0,$langs,0,0,-1,$conf->currency)."/".$langs->trans("Unit");
if ($objp->ref_fourn) $label.=' ('.$objp->ref_fourn.')'; 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++; $i++;
} }