From 26b00dc5caf37bab641540b5f54cefe356b647d9 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Tue, 30 Oct 2018 17:40:05 +0100 Subject: [PATCH 1/3] NEW possibility to defined rounding rules by currency --- htdocs/admin/limits.php | 100 ++++++++++++++++---------- htdocs/core/lib/multicurrency.lib.php | 29 +++++++- 2 files changed, 89 insertions(+), 40 deletions(-) diff --git a/htdocs/admin/limits.php b/htdocs/admin/limits.php index ca4b8fca512..b492664fd97 100644 --- a/htdocs/admin/limits.php +++ b/htdocs/admin/limits.php @@ -1,6 +1,6 @@ - * Copyright (C) 2009-2012 Regis Houssin + * Copyright (C) 2009-2018 Regis Houssin * Copyright (C) 2010 Juanjo Menent * * This program is free software; you can redistribute it and/or modify @@ -32,31 +32,37 @@ $langs->loadLangs(array('companies', 'products', 'admin')); if (! $user->admin) accessforbidden(); $action = GETPOST('action','alpha'); +$currencycode = GETPOST('currencycode', 'alpha'); + +$mainmaxdecimalsunit = 'MAIN_MAX_DECIMALS_UNIT'.(! empty($currencycode)?'_'.$currencycode:''); +$mainmaxdecimalstot = 'MAIN_MAX_DECIMALS_TOT'.(! empty($currencycode)?'_'.$currencycode:''); +$mainmaxdecimalsshown = 'MAIN_MAX_DECIMALS_SHOWN'.(! empty($currencycode)?'_'.$currencycode:''); +$mainroundingruletot = 'MAIN_ROUNDING_RULE_TOT'.(! empty($currencycode)?'_'.$currencycode:''); if ($action == 'update') { - $error=0; - $MAXDEC=8; - if ($_POST["MAIN_MAX_DECIMALS_UNIT"] > $MAXDEC - || $_POST["MAIN_MAX_DECIMALS_TOT"] > $MAXDEC - || $_POST["MAIN_MAX_DECIMALS_SHOWN"] > $MAXDEC) + $error=0; + $MAXDEC=8; + if ($_POST[$mainmaxdecimalsunit] > $MAXDEC + || $_POST[$mainmaxdecimalstot] > $MAXDEC + || $_POST[$mainmaxdecimalsshown] > $MAXDEC) { $error++; setEventMessages($langs->trans("ErrorDecimalLargerThanAreForbidden",$MAXDEC), null, 'errors'); } - if ($_POST["MAIN_MAX_DECIMALS_UNIT"] < 0 - || $_POST["MAIN_MAX_DECIMALS_TOT"] < 0 - || $_POST["MAIN_MAX_DECIMALS_SHOWN"] < 0) + if ($_POST[$mainmaxdecimalsunit].(! empty($currencycode)?'_'.$currencycode:'') < 0 + || $_POST[$mainmaxdecimalstot] < 0 + || $_POST[$mainmaxdecimalsshown] < 0) { $langs->load("errors"); $error++; setEventMessages($langs->trans("ErrorNegativeValueNotAllowed"), null, 'errors'); } - if ($_POST["MAIN_ROUNDING_RULE_TOT"]) + if ($_POST[$mainroundingruletot]) { - if ($_POST["MAIN_ROUNDING_RULE_TOT"] * pow(10,$_POST["MAIN_MAX_DECIMALS_TOT"]) < 1) + if ($_POST[$mainroundingruletot] * pow(10,$_POST[$mainmaxdecimalstot]) < 1) { $langs->load("errors"); $error++; @@ -66,22 +72,21 @@ if ($action == 'update') if (! $error) { - dolibarr_set_const($db, "MAIN_MAX_DECIMALS_UNIT", $_POST["MAIN_MAX_DECIMALS_UNIT"],'chaine',0,'',$conf->entity); - dolibarr_set_const($db, "MAIN_MAX_DECIMALS_TOT", $_POST["MAIN_MAX_DECIMALS_TOT"],'chaine',0,'',$conf->entity); - dolibarr_set_const($db, "MAIN_MAX_DECIMALS_SHOWN", $_POST["MAIN_MAX_DECIMALS_SHOWN"],'chaine',0,'',$conf->entity); + dolibarr_set_const($db, $mainmaxdecimalsunit, $_POST[$mainmaxdecimalsunit],'chaine',0,'',$conf->entity); + dolibarr_set_const($db, $mainmaxdecimalstot, $_POST[$mainmaxdecimalstot],'chaine',0,'',$conf->entity); + dolibarr_set_const($db, $mainmaxdecimalsshown, $_POST[$mainmaxdecimalsshown],'chaine',0,'',$conf->entity); - dolibarr_set_const($db, "MAIN_ROUNDING_RULE_TOT", $_POST["MAIN_ROUNDING_RULE_TOT"],'chaine',0,'',$conf->entity); + dolibarr_set_const($db, $mainroundingruletot, $_POST[$mainroundingruletot],'chaine',0,'',$conf->entity); - header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup"); + header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup".(! empty($currencycode)?'¤cycode='.$currencycode:'')); exit; } } - /* * View -*/ + */ $form=new Form($db); @@ -89,6 +94,31 @@ llxHeader(); print load_fiche_titre($langs->trans("LimitsSetup"),'','title_setup'); +if ($conf->multicurrency->enabled && $conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY) +{ + require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php'; + + $currencycode = (! empty($currencycode)?$currencycode:$conf->currency); + $aCurrencies = array($conf->currency); // Default currency always first position + + $sql = 'SELECT rowid, code FROM '.MAIN_DB_PREFIX.'multicurrency'; + $sql.= ' WHERE entity = '.$conf->entity; + $sql.= ' AND code != "'.$conf->currency.'"'; // Default currency always first position + $resql = $db->query($sql); + if ($resql) + { + while ($obj = $db->fetch_object($resql)) + { + $aCurrencies[] = $obj->code; + } + + if (! empty($aCurrencies) && count($aCurrencies) > 1) + { + $head = multicurrencyLimitPrepareHead($aCurrencies); + dol_fiche_head($head, $currencycode, '', -1, "multicurrency"); + } + } +} print $langs->trans("LimitsDesc")."
\n"; print "
\n"; @@ -98,29 +128,29 @@ if ($action == 'edit') print '
'; print ''; print ''; + if ($conf->multicurrency->enabled && $conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY) { + print ''; + } clearstatcache(); print ''; print ''; - print ''; - + print ''; print ''; - - - print ''; + print ''; + print ''; + print ''; print ''; + print ''; print '
'.$langs->trans("Parameter").''.$langs->trans("Value").'
'; print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"),$langs->trans("ParameterActiveForNextInputOnly")); - print '
'; print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"),$langs->trans("ParameterActiveForNextInputOnly")); - print '
'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'
'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'
'; print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"),$langs->trans("ParameterActiveForNextInputOnly")); - print '
'; @@ -136,32 +166,28 @@ else print ''; print ''; - print ''; - + print ''; print ''; - - - print ''; + print ''; + print ''; + print ''; print ''; + print ''; print '
'.$langs->trans("Parameter").''.$langs->trans("Value").'
'; print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"),$langs->trans("ParameterActiveForNextInputOnly")); - print ''.$conf->global->MAIN_MAX_DECIMALS_UNIT.'
'.(! empty($conf->global->$mainmaxdecimalsunit)?$conf->global->$mainmaxdecimalsunit:$conf->global->MAIN_MAX_DECIMALS_UNIT).'
'; print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"),$langs->trans("ParameterActiveForNextInputOnly")); - print ''.$conf->global->MAIN_MAX_DECIMALS_TOT.'
'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").''.$conf->global->MAIN_MAX_DECIMALS_SHOWN.'
'.(! empty($conf->global->$mainmaxdecimalstot)?$conf->global->$mainmaxdecimalstot:$conf->global->MAIN_MAX_DECIMALS_TOT).'
'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").''.(! empty($conf->global->$mainmaxdecimalsshown)?$conf->global->$mainmaxdecimalsshown:$conf->global->MAIN_MAX_DECIMALS_SHOWN).'
'; print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"),$langs->trans("ParameterActiveForNextInputOnly")); - print ''.$conf->global->MAIN_ROUNDING_RULE_TOT.'
'.(! empty($conf->global->$mainroundingruletot)?$conf->global->$mainroundingruletot:$conf->global->MAIN_ROUNDING_RULE_TOT).'
'; print ''; } - if (empty($mysoc->country_code)) { $langs->load("errors"); @@ -170,7 +196,6 @@ if (empty($mysoc->country_code)) } else { - // Show examples print ''.$langs->trans("ExamplesWithCurrentSetup").":
\n"; @@ -196,7 +221,6 @@ else print " - ".$langs->trans("VAT").": ".$vat.'%'; print "   ->   ".$langs->trans("TotalPriceAfterRounding").": ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."
\n"; - // Add vat rates examples specific to country $vat_rates=array(); diff --git a/htdocs/core/lib/multicurrency.lib.php b/htdocs/core/lib/multicurrency.lib.php index 339ca2d01bc..968fe3d956d 100644 --- a/htdocs/core/lib/multicurrency.lib.php +++ b/htdocs/core/lib/multicurrency.lib.php @@ -1,6 +1,6 @@ - * Copyright (C) 2015 ATM Consulting +/* Copyright (C) 2015 ATM Consulting + * Copyright (C) 2018 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 @@ -44,3 +44,28 @@ function multicurrencyAdminPrepareHead() return $head; } + +/** + * Prepare array with list of currency tabs + * + * @param array $aCurrencies Currencies array + * @return array Array of tabs + */ +function multicurrencyLimitPrepareHead($aCurrencies) +{ + global $langs; + + $i=0; + $head = array(); + + foreach($aCurrencies as $currency) + { + $head[$i][0] = $_SERVER['PHP_SELF'].'?currencycode='.$currency; + $head[$i][1] = $langs->trans("Currency".$currency).' ('.$langs->getCurrencySymbol($currency).')'; + $head[$i][2] = $currency; + + $i++; + } + + return $head; +} \ No newline at end of file From 6e770676388649eff4427f51cd1060cd52c448d1 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Tue, 30 Oct 2018 17:59:14 +0100 Subject: [PATCH 2/3] FIX better check --- htdocs/admin/limits.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/admin/limits.php b/htdocs/admin/limits.php index b492664fd97..104fb2cd388 100644 --- a/htdocs/admin/limits.php +++ b/htdocs/admin/limits.php @@ -94,7 +94,7 @@ llxHeader(); print load_fiche_titre($langs->trans("LimitsSetup"),'','title_setup'); -if ($conf->multicurrency->enabled && $conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY) +if (! empty($conf->multicurrency->enabled) && ! empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) { require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php'; @@ -128,7 +128,7 @@ if ($action == 'edit') print ''; print ''; print ''; - if ($conf->multicurrency->enabled && $conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY) { + if (! empty($conf->multicurrency->enabled) && ! empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) { print ''; } From 863fa7360d5acf903272796e75badc0f83690982 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 19 Dec 2019 09:57:37 +0000 Subject: [PATCH 3/3] Fixing style errors. --- htdocs/core/lib/multicurrency.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/multicurrency.lib.php b/htdocs/core/lib/multicurrency.lib.php index d9d0140fba1..f17d79c6207 100644 --- a/htdocs/core/lib/multicurrency.lib.php +++ b/htdocs/core/lib/multicurrency.lib.php @@ -68,4 +68,4 @@ function multicurrencyLimitPrepareHead($aCurrencies) } return $head; -} \ No newline at end of file +}