Fix: javascript calculation of unit price from margin rate was broken.

This commit is contained in:
Laurent Destailleur 2014-02-16 19:26:41 +01:00
parent b081083a1a
commit 3aba53a9ae
2 changed files with 43 additions and 30 deletions

View File

@ -1145,7 +1145,7 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights-
$price_ht = ''; $price_ht = '';
$tva_tx = ''; $tva_tx = '';
} }
if (GETPOST('usenewaddlineform')) { if (GETPOST('usenewaddlineform')) { // TODO Remove this
$idprod=GETPOST('idprod', 'int'); $idprod=GETPOST('idprod', 'int');
$product_desc = (GETPOST('product_desc')?GETPOST('product_desc'):(GETPOST('np_desc')?GETPOST('np_desc'):(GETPOST('dp_desc')?GETPOST('dp_desc'):''))); $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'); $price_ht = GETPOST('price_ht');
@ -1324,8 +1324,8 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights-
} }
// Margin // Margin
$fournprice=(GETPOST('fournprice'.$predef)?GETPOST('fournprice'.$predef):''); $fournprice=price2num(GETPOST('fournprice'.$predef)?GETPOST('fournprice'.$predef):'');
$buyingprice=(GETPOST('buying_price'.$predef)?GETPOST('buying_price'.$predef):''); $buyingprice=price2num(GETPOST('buying_price'.$predef)?GETPOST('buying_price'.$predef):'');
// Local Taxes // Local Taxes
$localtax1_tx= get_localtax($tva_tx, 1, $object->client); $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); $localtax2_rate=get_localtax($vat_rate,2,$object->client);
// Add buying price // Add buying price
$fournprice=(GETPOST('fournprice')?GETPOST('fournprice'):''); $fournprice=price2num(GETPOST('fournprice')?GETPOST('fournprice'):'');
$buyingprice=(GETPOST('buying_price')?GETPOST('buying_price'):''); $buyingprice=price2num(GETPOST('buying_price')?GETPOST('buying_price'):'');
//Extrafields //Extrafields
$extrafieldsline = new ExtraFields($db); $extrafieldsline = new ExtraFields($db);

View File

@ -473,39 +473,52 @@ if (! empty($conf->margin->enabled))
var price = 0; var price = 0;
if (remise.val().replace(',','.') != 100) if (remise.val().replace(',','.') != 100)
{ {
bpjs=price2numjs(buying_price.val());
ratejs=price2numjs(rate.val());
remisejs=price2numjs(remise.val());
if (npRate == "marginRate") if (npRate == "marginRate")
price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100)); price = ((bpjs * (1 + ratejs / 100)) / (1 - remisejs / 100));
else { else if (npRate == "markRate")
if (npRate == "markRate") price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100));
price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
} }
} $("input[name='price_ht']:first").val(price); // TODO Must use a function like php price to have here a formated value
$("input[name='price_ht']:first").val(roundFloat(price));
return true; return true;
} }
// TODO This works for french numbers only /* Function similar to price2num in PHP */
function roundFloat(num) { function price2numjs(num)
var main_max_dec_shown = <?php echo $conf->global->MAIN_MAX_DECIMALS_SHOWN; ?>;
var main_rounding = <?php echo min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>;
var amount = num.toString().replace(',','.'); // should be useless
var nbdec = 0;
var rounding = main_rounding;
var pos = amount.indexOf('.');
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; <?php
} $dec=','; $thousand=' ';
//amount = parseFloat(amount) + (1 / Math.pow(100, rounding)); // to avoid floating-point errors if ($langs->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 main_max_dec_shown = <?php echo $conf->global->MAIN_MAX_DECIMALS_SHOWN; ?>;
var main_rounding_unit = <?php echo $conf->global->MAIN_MAX_DECIMALS_UNIT; ?>;
var main_rounding_tot = <?php echo $conf->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
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); return parseFloat(amount).toFixed(rounding);
} }