Fix: javascript calculation of unit price from margin rate was broken.
This commit is contained in:
parent
3aba53a9ae
commit
85e93872b3
@ -262,43 +262,57 @@ if (! empty($usemargins) && $user->rights->margins->creer)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var np_price = 0;
|
var price = 0;
|
||||||
if (remise.val().replace(',','.') != 100)
|
remisejs=price2numjs(remise.val());
|
||||||
|
|
||||||
|
if (remisejs != 100)
|
||||||
{
|
{
|
||||||
if (npRate == "np_marginRate")
|
bpjs=price2numjs(buying_price.val());
|
||||||
np_price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
|
ratejs=price2numjs(rate.val());
|
||||||
else {
|
|
||||||
if (npRate == "np_markRate")
|
if (npRate == "marginRate")
|
||||||
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 == "markRate")
|
||||||
|
price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 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(np_price));
|
|
||||||
|
|
||||||
return true;
|
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
|
/* Function similar to price2num in PHP */
|
||||||
var nbdec = 0;
|
function price2numjs(num)
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -608,47 +608,63 @@ function checkFreeLine(e, npRate)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var np_price = 0;
|
var price = 0;
|
||||||
if (remise.val().replace(',','.') != 100)
|
remisejs=price2numjs(remise.val());
|
||||||
|
|
||||||
|
if (remisejs != 100)
|
||||||
{
|
{
|
||||||
|
bpjs=price2numjs(buying_price.val());
|
||||||
|
ratejs=price2numjs(rate.val());
|
||||||
|
|
||||||
if (npRate == "np_marginRate")
|
if (npRate == "np_marginRate")
|
||||||
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 {
|
else if (npRate == "np_markRate")
|
||||||
if (npRate == "np_markRate")
|
price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100));
|
||||||
np_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(formatFloat(np_price));
|
|
||||||
update_price('price_ht', 'price_ttc');
|
update_price('price_ht', 'price_ttc');
|
||||||
|
|
||||||
return true;
|
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;
|
/* Function similar to price2num in PHP */
|
||||||
var pos = amount.indexOf('.');
|
function price2numjs(num)
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatFloat(num) {
|
|
||||||
return roundFloat(num).replace('.', ',');
|
|
||||||
}
|
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -471,11 +471,12 @@ if (! empty($conf->margin->enabled))
|
|||||||
}
|
}
|
||||||
|
|
||||||
var price = 0;
|
var price = 0;
|
||||||
if (remise.val().replace(',','.') != 100)
|
remisejs=price2numjs(remise.val());
|
||||||
|
|
||||||
|
if (remisejs != 100)
|
||||||
{
|
{
|
||||||
bpjs=price2numjs(buying_price.val());
|
bpjs=price2numjs(buying_price.val());
|
||||||
ratejs=price2numjs(rate.val());
|
ratejs=price2numjs(rate.val());
|
||||||
remisejs=price2numjs(remise.val());
|
|
||||||
|
|
||||||
if (npRate == "marginRate")
|
if (npRate == "marginRate")
|
||||||
price = ((bpjs * (1 + ratejs / 100)) / (1 - remisejs / 100));
|
price = ((bpjs * (1 + ratejs / 100)) / (1 - remisejs / 100));
|
||||||
|
|||||||
@ -270,42 +270,56 @@ if (! empty($usemargins) && $user->rights->margins->creer)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var np_price = 0;
|
var price = 0;
|
||||||
if (remise.val().replace(',','.') != 100)
|
remisejs=price2numjs(remise.val());
|
||||||
|
|
||||||
|
if (remisejs != 100)
|
||||||
{
|
{
|
||||||
|
bpjs=price2numjs(buying_price.val());
|
||||||
|
ratejs=price2numjs(rate.val());
|
||||||
|
|
||||||
if (npRate == "np_marginRate_predef")
|
if (npRate == "np_marginRate_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 {
|
else if (npRate == "np_markRate_predef")
|
||||||
if (npRate == "np_markRate_predef")
|
price = ((bpjs / (1 - ratejs / 100)) / (1 - remisejs / 100));
|
||||||
np_price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
|
|
||||||
}
|
}
|
||||||
}
|
$("input[name='price_ht_predef']:last").val(price); // TODO Must use a function like php price to have here a formated value
|
||||||
$("input[name='price_ht_predef']:last").val(roundFloat(np_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user