From 2cf36d157fb6fee1fa4829f5afbbd70beea996dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Mon, 7 Dec 2015 14:15:00 +0100 Subject: [PATCH 1/2] FIX #3953 Don't round supplier price The price is already rounded when inserted on the product page. It's also rounded at display. There's no need to round it here. Rounding it leads to serious calculation errors. --- htdocs/core/tpl/objectline_create.tpl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index d7949fa1d52..25668b2876d 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -594,7 +594,7 @@ jQuery(document).ready(function() { /* Define default price at loading */ var defaultprice = $("#fournprice_predef").find('option:selected').attr("price"); - $("#buying_price").val(Math.round(defaultprice,global->MAIN_MAX_DECIMALS_UNIT ? $conf->global->MAIN_MAX_DECIMALS_UNIT : 5); ?>)); + $("#buying_price").val(defaultprice); $("#fournprice_predef").change(function() { console.log("change on fournprice_predef"); From 3e2309d0e6da1de1d29570447978f3c6a3634a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Mon, 7 Dec 2015 15:10:23 +0100 Subject: [PATCH 2/2] NEW Factorized price2numjs into lib_head.js.php Used public domain code from the MDN to provide a robust decimal rounding implementation. Warning for developers: the path of the javascript file has changed. Please update your scripts. --- htdocs/core/datepicker.php | 4 +- .../core/js/{lib_head.js => lib_head.js.php} | 161 +++++++++++++++--- htdocs/core/tpl/objectline_create.tpl.php | 37 ---- htdocs/core/tpl/objectline_edit.tpl.php | 36 ---- htdocs/main.inc.php | 2 +- 5 files changed, 143 insertions(+), 97 deletions(-) rename htdocs/core/js/{lib_head.js => lib_head.js.php} (84%) diff --git a/htdocs/core/datepicker.php b/htdocs/core/datepicker.php index eae723946f6..63b765fbadb 100644 --- a/htdocs/core/datepicker.php +++ b/htdocs/core/datepicker.php @@ -56,7 +56,7 @@ print ''."\n"; print ''."\n"; if (GETPOST('mode') && GETPOST('mode') == 'test') { - print ''."\n"; + print ''."\n"; } else { @@ -288,4 +288,4 @@ function displayBox($selectedDate,$month,$year) -// Copyright (C) 2005-2014 Regis Houssin -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// or see http://www.gnu.org/ + + * Copyright (C) 2005-2014 Regis Houssin + * Copyright (C) 2015 Raphaƫl Doursenaud + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see http://www.gnu.org/ + */ -// -// \file htdocs/core/js/lib_head.js -// \brief File that include javascript functions (included if option use_javascript activated) -// +/** + * \file htdocs/core/js/lib_head.js.php + * \brief File that include javascript functions (included if option use_javascript activated) + */ +//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language +//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations +if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK',1); +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); +if (! defined('NOLOGIN')) define('NOLOGIN',1); +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU',1); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML',1); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); + +session_cache_limiter(FALSE); + +require_once '../../main.inc.php'; + +// Define javascript type +header('Content-type: text/javascript; charset=UTF-8'); +// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access. +if (empty($dolibarr_nocache)) header('Cache-Control: max-age=3600, public, must-revalidate'); +else header('Cache-Control: no-cache'); +?> /* * ================================================================= @@ -923,7 +947,6 @@ function copyToClipboard(text,text2) return false; } - /* * Provide a function to get an URL GET parameter in javascript * @@ -938,3 +961,99 @@ function getParameterByName(name, valueifnotfound) results = regex.exec(location.search); return results === null ? valueifnotfound : decodeURIComponent(results[1].replace(/\+/g, " ")); } + +// Code in the public domain from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round +(function() { + /** + * Decimal adjustment of a number. + * + * @param {String} type The type of adjustment. + * @param {Number} value The number. + * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base). + * @returns {Number} The adjusted value. + */ + function decimalAdjust(type, value, exp) { + // If the exp is undefined or zero... + if (typeof exp === 'undefined' || +exp === 0) { + return Math[type](value); + } + value = +value; + exp = +exp; + // If the value is not a number or the exp is not an integer... + if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { + return NaN; + } + // Shift + value = value.toString().split('e'); + value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); + // Shift back + value = value.toString().split('e'); + return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); + } + + // Decimal round + if (!Math.round10) { + Math.round10 = function(value, exp) { + return decimalAdjust('round', value, exp); + }; + } + // Decimal floor + if (!Math.floor10) { + Math.floor10 = function(value, exp) { + return decimalAdjust('floor', value, exp); + }; + } + // Decimal ceil + if (!Math.ceil10) { + Math.ceil10 = function(value, exp) { + return decimalAdjust('ceil', value, exp); + }; + } +})(); + + +/** + * Function similar to PHP price2num() + * + * @param {number|string} amount The amount to convert/clean + * @returns {string} The amount in universal numeric format (Example: '99.99999') + * @todo Implement rounding parameter + */ +function price2numjs(amount) { + if (amount == '') return ''; + + transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") { + $dec = $langs->transnoentitiesnoconv("SeparatorDecimal"); + } + if ($langs->transnoentitiesnoconv("SeparatorThousand") != "SeparatorThousand") { + $thousand = $langs->transnoentitiesnoconv("SeparatorThousand"); + } + print "var dec='" . $dec . "'; var thousand='" . $thousand . "';\n"; // Set var in javascript + ?> + + var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; + var main_rounding_unit = global->MAIN_MAX_DECIMALS_UNIT; ?>; + var main_rounding_tot = global->MAIN_MAX_DECIMALS_TOT; ?>; + + var amount = amount.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', ''); // Remove 0 for decimal part + 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 Math.round10(amount, rounding); +} diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index 25668b2876d..485336335e6 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -464,43 +464,6 @@ if (! empty($usemargins) && $user->rights->margins->creer) return true; } - - /* Function similar to price2num in PHP */ - function price2numjs(num) - { - if (num == '') return ''; - - transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") $dec=$langs->transnoentitiesnoconv("SeparatorDecimal"); - if ($langs->transnoentitiesnoconv("SeparatorThousand")!= "SeparatorThousand") $thousand=$langs->transnoentitiesnoconv("SeparatorThousand"); - print "var dec='".$dec."'; var thousand='".$thousand."';\n"; // Set var in javascript - ?> - - var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; - var main_rounding_unit = global->MAIN_MAX_DECIMALS_UNIT; ?>; - var main_rounding_tot = 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); - } - diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index 883c1f5eea9..4316ab0b318 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -375,42 +375,6 @@ if (! empty($conf->margin->enabled)) return true; } - /* Function similar to price2num in PHP */ - function price2numjs(num) - { - if (num == '') return ''; - - transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") $dec=$langs->transnoentitiesnoconv("SeparatorDecimal"); - if ($langs->transnoentitiesnoconv("SeparatorThousand")!= "SeparatorThousand") $thousand=$langs->transnoentitiesnoconv("SeparatorThousand"); - print "var dec='".$dec."'; var thousand='".$thousand."';\n"; // Set var in javascript - ?> - - var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; - var main_rounding_unit = global->MAIN_MAX_DECIMALS_UNIT; ?>; - var main_rounding_tot = 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',''); // Remove 0 for decimal part - 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/main.inc.php b/htdocs/main.inc.php index a3c647a5e47..890027c9d30 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -1269,7 +1269,7 @@ function top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs // Global js function print ''."\n"; - print ''."\n"; + print ''."\n"; // Add datepicker default options print ''."\n";