diff --git a/htdocs/core/tpl/freeproductline_create.tpl.php b/htdocs/core/tpl/freeproductline_create.tpl.php index 51479f24399ba34755c12d44f1b6d904742425b7..f88d7741ed8c9d7a3d46a47edc83ac46181f54ce 100644 --- a/htdocs/core/tpl/freeproductline_create.tpl.php +++ b/htdocs/core/tpl/freeproductline_create.tpl.php @@ -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) + + /* 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 = <?php echo min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>; + 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(); - var amount = num.toString().replace(',','.'); // should be useless - var nbdec = 0; - var rounding = main_rounding; - var pos = amount.indexOf('.'); + // 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); } diff --git a/htdocs/core/tpl/objectline_add.tpl.php b/htdocs/core/tpl/objectline_add.tpl.php index 92f965ad96854e80d64fbed7c1a9a55f77fa0d0c..de72eced350709533c6a08ef35cb632eca608b8e 100644 --- a/htdocs/core/tpl/objectline_add.tpl.php +++ b/htdocs/core/tpl/objectline_add.tpl.php @@ -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) { + + + +/* 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 = <?php echo min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>; + 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().replace(',','.'); // should be useless - var nbdec = 0; - var rounding = main_rounding; - var pos = amount.indexOf('.'); + 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> diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index 6707eb269a7f0bd4cbbe04eb13ef9258f6e9c172..58ce0e6481f028787d441e415a8f82be0c53094a 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -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)); diff --git a/htdocs/core/tpl/predefinedproductline_create.tpl.php b/htdocs/core/tpl/predefinedproductline_create.tpl.php index 4816c94e26d0385c3a38bfd259795b6580aeff52..ec430015135a267ed12c72d6d4e8ac8227f8a0f3 100644 --- a/htdocs/core/tpl/predefinedproductline_create.tpl.php +++ b/htdocs/core/tpl/predefinedproductline_create.tpl.php @@ -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) { + /* 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 = <?php echo min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>; + 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(); - var amount = num.toString().replace(',','.'); // should be useless - var nbdec = 0; - var rounding = main_rounding; - var pos = amount.indexOf('.'); + // 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); }