diff --git a/htdocs/core/datepicker.php b/htdocs/core/datepicker.php index eae723946f678d1e30cb02be993eed74587acec1..63b765fbadb29b08b80d2b750f24c00fe231c532 100644 --- a/htdocs/core/datepicker.php +++ b/htdocs/core/datepicker.php @@ -56,7 +56,7 @@ print '<html>'."\n"; print '<head>'."\n"; if (GETPOST('mode') && GETPOST('mode') == 'test') { - print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js"></script>'."\n"; + print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php"></script>'."\n"; } else { @@ -288,4 +288,4 @@ function displayBox($selectedDate,$month,$year) </tr> </table> <?php -}//end function \ No newline at end of file +}//end function diff --git a/htdocs/core/js/lib_head.js b/htdocs/core/js/lib_head.js.php similarity index 84% rename from htdocs/core/js/lib_head.js rename to htdocs/core/js/lib_head.js.php index ad799e72bd08a51cd417e9c83588e875ba2deb1b..15a25bf10ed734e2a0d8dc23a7c160e0df25daf1 100644 --- a/htdocs/core/js/lib_head.js +++ b/htdocs/core/js/lib_head.js.php @@ -1,25 +1,49 @@ -// Copyright (C) 2005-2014 Laurent Destailleur <eldy@users.sourceforge.net> -// Copyright (C) 2005-2014 Regis Houssin <regis.houssin@capnetworks.com> -// -// 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 <http://www.gnu.org/licenses/>. -// 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) -// +<?php +/* Copyright (C) 2005-2014 Laurent Destailleur <eldy@users.sourceforge.net> + * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@capnetworks.com> + * Copyright (C) 2015 Raphaƫl Doursenaud <rdoursenaud@gpcsolutions.fr> + * + * 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 <http://www.gnu.org/licenses/>. + * or see http://www.gnu.org/ + */ + +/** + * \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 ''; + + <?php + $dec = ','; + $thousand = ' '; + if ($langs->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 = <?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 = 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 d7949fa1d5214a267e780483b27b4b5804f33a1b..485336335e62742b25484576f0f8fc6f043d63a8 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 ''; - - <?php - $dec=','; $thousand=' '; - if ($langs->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 = <?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); - } - <?php } ?> @@ -594,7 +557,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,<?php print ($conf->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"); diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index 883c1f5eea9246d4cc1bbe45995e8d4e34786c2c..4316ab0b318a297d9ed73eea5368b38a1dda872e 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 ''; - - <?php - $dec=','; $thousand=' '; - if ($langs->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 = <?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',''); // 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); - } - <?php } ?> diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index a3c647a5e47cfcbde71a373f13699ffaf29fcc43..890027c9d302db42d1d76e85551dc11fccfbd522 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 '<!-- Includes JS of Dolibarr -->'."\n"; - print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js?version='.urlencode(DOL_VERSION).($ext?'&'.$ext:'').'"></script>'."\n"; + print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php?version='.urlencode(DOL_VERSION).($ext?'&'.$ext:'').'"></script>'."\n"; // Add datepicker default options print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/datepicker.js.php?lang='.$langs->defaultlang.($ext?'&'.$ext:'').'"></script>'."\n";