From 3aba53a9ae0a31c16396516a4b3f837d0c75a4bd Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 16 Feb 2014 19:26:41 +0100 Subject: [PATCH] Fix: javascript calculation of unit price from margin rate was broken. --- htdocs/compta/facture.php | 10 ++-- htdocs/core/tpl/objectline_edit.tpl.php | 63 +++++++++++++++---------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 69c24341f9c..a03dddfc515 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1145,7 +1145,7 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights- $price_ht = ''; $tva_tx = ''; } - if (GETPOST('usenewaddlineform')) { + if (GETPOST('usenewaddlineform')) { // TODO Remove this $idprod=GETPOST('idprod', 'int'); $product_desc = (GETPOST('product_desc')?GETPOST('product_desc'):(GETPOST('np_desc')?GETPOST('np_desc'):(GETPOST('dp_desc')?GETPOST('dp_desc'):''))); $price_ht = GETPOST('price_ht'); @@ -1324,8 +1324,8 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights- } // Margin - $fournprice=(GETPOST('fournprice'.$predef)?GETPOST('fournprice'.$predef):''); - $buyingprice=(GETPOST('buying_price'.$predef)?GETPOST('buying_price'.$predef):''); + $fournprice=price2num(GETPOST('fournprice'.$predef)?GETPOST('fournprice'.$predef):''); + $buyingprice=price2num(GETPOST('buying_price'.$predef)?GETPOST('buying_price'.$predef):''); // Local Taxes $localtax1_tx= get_localtax($tva_tx, 1, $object->client); @@ -1446,8 +1446,8 @@ elseif ($action == 'updateligne' && $user->rights->facture->creer && ! GETPOST(' $localtax2_rate=get_localtax($vat_rate,2,$object->client); // Add buying price - $fournprice=(GETPOST('fournprice')?GETPOST('fournprice'):''); - $buyingprice=(GETPOST('buying_price')?GETPOST('buying_price'):''); + $fournprice=price2num(GETPOST('fournprice')?GETPOST('fournprice'):''); + $buyingprice=price2num(GETPOST('buying_price')?GETPOST('buying_price'):''); //Extrafields $extrafieldsline = new ExtraFields($db); diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index ee961f5ad70..6707eb269a7 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -473,39 +473,52 @@ if (! empty($conf->margin->enabled)) var price = 0; if (remise.val().replace(',','.') != 100) { + bpjs=price2numjs(buying_price.val()); + ratejs=price2numjs(rate.val()); + remisejs=price2numjs(remise.val()); + if (npRate == "marginRate") - price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100)); - else { - if (npRate == "markRate") - price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100)); - } + price = ((bpjs * (1 + ratejs / 100)) / (1 - remisejs / 100)); + else if (npRate == "markRate") + price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100)); } - $("input[name='price_ht']:first").val(roundFloat(price)); + $("input[name='price_ht']:first").val(price); // TODO Must use a function like php price to have here a formated value return true; } - // TODO This works for french numbers only - function roundFloat(num) { - var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; - var main_rounding = global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>; + /* Function similar to price2num in PHP */ + function price2numjs(num) + { + transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") $dec=$langs->transnoentitiesnoconv("SeparatorDecimal"); + if ($langs->transnoentitiesnoconv("SeparatorThousand")!= "SeparatorThousand") $thousand=$langs->transnoentitiesnoconv("SeparatorThousand"); + if ($thousand == 'None') $thousand=''; + print "var dec='".$dec."'; var thousand='".$thousand."';\n"; + ?> - var amount = num.toString().replace(',','.'); // should be useless - var nbdec = 0; - var rounding = main_rounding; - var pos = amount.indexOf('.'); + var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; + var main_rounding_unit = global->MAIN_MAX_DECIMALS_UNIT; ?>; + var main_rounding_tot = global->MAIN_MAX_DECIMALS_TOT; ?>; + + var amount = num.toString(); + + // rounding for unit price + var rounding = main_rounding_unit; + var pos = amount.indexOf(dec); var decpart = ''; - if (pos >= 0) - decpart = amount.substr(pos+1).replace('/0+$/i',''); // Supprime les 0 de fin de partie decimale - nbdec = decpart.length; - if (nbdec > rounding) - rounding = nbdec; - // Si on depasse max - if (rounding > main_max_dec_shown) - { - rounding = main_max_dec_shown; - } - //amount = parseFloat(amount) + (1 / Math.pow(100, rounding)); // to avoid floating-point errors + if (pos >= 0) decpart = amount.substr(pos+1).replace('/0+$/i',''); // Supprime les 0 de fin de partie decimale + var nbdec = decpart.length; + if (nbdec > rounding) rounding = nbdec; + // If rounding higher than max shown + if (rounding > main_max_dec_shown) rounding = main_max_dec_shown; + + if (thousand != ',' && thousand != '.') amount=amount.replace(',','.'); + amount=amount.replace(' ',''); // To avoid spaces + amount=amount.replace(thousand,''); // Replace of thousand before replace of dec to avoid pb if thousand is . + amount=amount.replace(dec,'.'); + return parseFloat(amount).toFixed(rounding); }