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

This commit is contained in:
Laurent Destailleur 2014-02-16 19:42:43 +01:00
parent 3aba53a9ae
commit 85e93872b3
4 changed files with 132 additions and 87 deletions

View File

@ -262,43 +262,57 @@ if (! empty($usemargins) && $user->rights->margins->creer)
return false;
}
var np_price = 0;
if (remise.val().replace(',','.') != 100)
var price = 0;
remisejs=price2numjs(remise.val());
if (remisejs != 100)
{
if (npRate == "np_marginRate")
np_price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
else {
if (npRate == "np_markRate")
np_price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
}
bpjs=price2numjs(buying_price.val());
ratejs=price2numjs(rate.val());
if (npRate == "marginRate")
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(np_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 = <?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('.');
/* Function similar to price2num in PHP */
function price2numjs(num)
{
<?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 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
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);
}

View File

@ -608,47 +608,63 @@ function checkFreeLine(e, npRate)
return false;
}
var np_price = 0;
if (remise.val().replace(',','.') != 100)
var price = 0;
remisejs=price2numjs(remise.val());
if (remisejs != 100)
{
bpjs=price2numjs(buying_price.val());
ratejs=price2numjs(rate.val());
if (npRate == "np_marginRate")
np_price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
else {
if (npRate == "np_markRate")
np_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 == "np_markRate")
price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100));
}
$("input[name='price_ht']:first").val(formatFloat(np_price));
$("input[name='price_ht']:first").val(price); // TODO Must use a function like php price to have here a formated value
update_price('price_ht', 'price_ttc');
return true;
}
function roundFloat(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('.');
/* Function similar to price2num in PHP */
function price2numjs(num)
{
<?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 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
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);
}
function formatFloat(num) {
return roundFloat(num).replace('.', ',');
}
<?php } ?>
});
</script>

View File

@ -471,11 +471,12 @@ if (! empty($conf->margin->enabled))
}
var price = 0;
if (remise.val().replace(',','.') != 100)
remisejs=price2numjs(remise.val());
if (remisejs != 100)
{
bpjs=price2numjs(buying_price.val());
ratejs=price2numjs(rate.val());
remisejs=price2numjs(remise.val());
if (npRate == "marginRate")
price = ((bpjs * (1 + ratejs / 100)) / (1 - remisejs / 100));

View File

@ -270,42 +270,56 @@ if (! empty($usemargins) && $user->rights->margins->creer)
return false;
}
var np_price = 0;
if (remise.val().replace(',','.') != 100)
var price = 0;
remisejs=price2numjs(remise.val());
if (remisejs != 100)
{
bpjs=price2numjs(buying_price.val());
ratejs=price2numjs(rate.val());
if (npRate == "np_marginRate_predef")
np_price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
else {
if (npRate == "np_markRate_predef")
np_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 == "np_markRate_predef")
price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100));
}
$("input[name='price_ht_predef']:last").val(roundFloat(np_price));
$("input[name='price_ht_predef']:last").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 = <?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); ?>;
/* Function similar to price2num in PHP */
function price2numjs(num)
{
<?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 nbdec = 0;
var rounding = main_rounding;
var pos = amount.indexOf('.');
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
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);
}