Fix: javascript calculation of unit price from margin rate was broken.
This commit is contained in:
parent
b081083a1a
commit
3aba53a9ae
@ -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);
|
||||||
|
|||||||
@ -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(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;
|
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); ?>;
|
<?php
|
||||||
|
$dec=','; $thousand=' ';
|
||||||
|
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 amount = num.toString().replace(',','.'); // should be useless
|
var main_max_dec_shown = <?php echo $conf->global->MAIN_MAX_DECIMALS_SHOWN; ?>;
|
||||||
var nbdec = 0;
|
var main_rounding_unit = <?php echo $conf->global->MAIN_MAX_DECIMALS_UNIT; ?>;
|
||||||
var rounding = main_rounding;
|
var main_rounding_tot = <?php echo $conf->global->MAIN_MAX_DECIMALS_TOT; ?>;
|
||||||
var pos = amount.indexOf('.');
|
|
||||||
|
var amount = num.toString();
|
||||||
|
|
||||||
|
// rounding for unit price
|
||||||
|
var rounding = main_rounding_unit;
|
||||||
|
var pos = amount.indexOf(dec);
|
||||||
var decpart = '';
|
var decpart = '';
|
||||||
if (pos >= 0)
|
if (pos >= 0) decpart = amount.substr(pos+1).replace('/0+$/i',''); // Supprime les 0 de fin de partie decimale
|
||||||
decpart = amount.substr(pos+1).replace('/0+$/i',''); // Supprime les 0 de fin de partie decimale
|
var nbdec = decpart.length;
|
||||||
nbdec = decpart.length;
|
if (nbdec > rounding) rounding = nbdec;
|
||||||
if (nbdec > rounding)
|
// If rounding higher than max shown
|
||||||
rounding = nbdec;
|
if (rounding > main_max_dec_shown) rounding = main_max_dec_shown;
|
||||||
// Si on depasse max
|
|
||||||
if (rounding > main_max_dec_shown)
|
if (thousand != ',' && thousand != '.') amount=amount.replace(',','.');
|
||||||
{
|
amount=amount.replace(' ',''); // To avoid spaces
|
||||||
rounding = main_max_dec_shown;
|
amount=amount.replace(thousand,''); // Replace of thousand before replace of dec to avoid pb if thousand is .
|
||||||
}
|
amount=amount.replace(dec,'.');
|
||||||
//amount = parseFloat(amount) + (1 / Math.pow(100, rounding)); // to avoid floating-point errors
|
|
||||||
return parseFloat(amount).toFixed(rounding);
|
return parseFloat(amount).toFixed(rounding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user