From 16ea13ecb81136cd247c305f76285a838cd1fc32 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 30 Oct 2023 01:38:20 +0100 Subject: [PATCH] NEW Allow sync of currency rates with currency layer by default. --- htdocs/admin/multicurrency.php | 94 ++++++++++--------- .../core/modules/modMultiCurrency.class.php | 2 +- .../class/multicurrency.class.php | 17 +++- 3 files changed, 62 insertions(+), 51 deletions(-) diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index 2b3263efbde..5afd1ce8c85 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -215,7 +215,7 @@ if ($conf->use_javascript_ajax) { print ''; // Online payment with currency on document. This option should be on by default. -if ($conf->global->MAIN_FEATURES_LEVEL >= 2) { +if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) { print ''; print ''.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").''; print ''; @@ -260,51 +260,6 @@ print ''; print ''; print ''; -print '
'; - -if (getDolGlobalString('MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION')) { - print '
'; - print ''; - print ''; - - print '
'; - print ''; - - $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net - - print ''; - print ''."\n"; - print ''; - - print ''; - print ''; - print ''; - - print ''; - print ''; - print ''; - - /*print ''; - print ''; - print '';*/ - - print '
'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).''; - print ''; - print ''; - print ''; - print '
'.$langs->transnoentitiesnoconv("multicurrency_appId").''; - print ' '; - print '
'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").''; - print ' '; // Default: USD - print ''; - print '
'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").''; - print ' '; // Example: EUR - print '
'; - print '
'; - print '
'; - - print ''; -} print '
'; print ''; @@ -377,6 +332,53 @@ print ' '; + +print '
'; + +if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) { + print ''; + print ''; + print ''; + + print '
'; + print '
'; + + $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net + + print ''; + print ''."\n"; + print ''; + + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + /*print ''; + print ''; + print '';*/ + + print '
'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).''; + print ''; + print ''; + print ''; + print '
'.$langs->transnoentitiesnoconv("multicurrency_appId").''; + print ' '; + print '
'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").''; + print ' '; // Default: USD + print '
'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").''; + print ' '; // Example: EUR + print '
'; + print '
'; + print '
'; + + print ''; +} + + // End of page llxFooter(); $db->close(); diff --git a/htdocs/core/modules/modMultiCurrency.class.php b/htdocs/core/modules/modMultiCurrency.class.php index c1e1a3a795a..2abcf240310 100644 --- a/htdocs/core/modules/modMultiCurrency.class.php +++ b/htdocs/core/modules/modMultiCurrency.class.php @@ -150,7 +150,7 @@ class modMultiCurrency extends DolibarrModules $datestart=dol_mktime(21, 15, 0, $arraydate['mon'], $arraydate['mday'], $arraydate['year']); $this->cronjobs = array( - 0 =>array('priority'=>61, 'label'=>'MutltiCurrencyAutoUpdateCurrencies', 'jobtype'=>'method', 'class'=>'multicurrency/class/multicurrency.class.php', 'objectname'=>'MultiCurrency', 'method'=>'syncRates', 'parameters'=>'0,0,cron', 'comment'=>'Update all the currencies using the currencylayer API. An APi key needs to be given in the multi-currency module config on constant MULTICURRENCY_APP_ID', 'frequency'=>1, 'unitfrequency'=>2678400, 'status'=>$statusatinstall, 'test'=>'isModEnabled("cron")', 'datestart'=>$datestart), + 0 =>array('priority'=>61, 'label'=>'MutltiCurrencyAutoUpdateCurrencies', 'jobtype'=>'method', 'class'=>'multicurrency/class/multicurrency.class.php', 'objectname'=>'MultiCurrency', 'method'=>'syncRates', 'parameters'=>'0,0,cron', 'comment'=>'Update all the currencies using the currencylayer API. An APi key needs to be given in the multi-currency module config page', 'frequency'=>1, 'unitfrequency'=>2678400, 'status'=>$statusatinstall, 'test'=>'isModEnabled("cron")', 'datestart'=>$datestart), ); // Permissions diff --git a/htdocs/multicurrency/class/multicurrency.class.php b/htdocs/multicurrency/class/multicurrency.class.php index bee9aa4eced..a895c830d7e 100644 --- a/htdocs/multicurrency/class/multicurrency.class.php +++ b/htdocs/multicurrency/class/multicurrency.class.php @@ -638,15 +638,24 @@ class MultiCurrency extends CommonObject /** * Sync rates from API * - * @param string $key Key to use. Come from $conf->global->MULTICURRENCY_APP_ID. + * @param string $key Key to use. Come from getDolGlobalString("MULTICURRENCY_APP_ID") * @param int $addifnotfound Add if not found * @param string $mode "" for standard use, "cron" to use it in a cronjob - * @return int <0 if KO, >0 if OK, if mode = "cron" OK is + * @return int <0 if KO, >0 if OK, if mode = "cron" OK is 0 */ public function syncRates($key, $addifnotfound = 0, $mode = "") { global $conf, $db, $langs; + if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) { + if ($mode == "cron") { + $this->output = $langs->trans('Use of API for currency update is disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER'); + } else { + setEventMessages($langs->trans('Use of API for currency update is disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER'), null, 'errors'); + } + return -1; + } + if (empty($key)) { $key = getDolGlobalString("MULTICURRENCY_APP_ID"); } @@ -688,9 +697,9 @@ class MultiCurrency extends CommonObject dol_syslog("Failed to call endpoint ".$response->error->info, LOG_WARNING); if ($mode == "cron") { $this->output = $langs->trans('multicurrency_syncronize_error', $response->error->info); + } else { + setEventMessages($langs->trans('multicurrency_syncronize_error', $response->error->info), null, 'errors'); } - setEventMessages($langs->trans('multicurrency_syncronize_error', $response->error->info), null, 'errors'); - return -1; } } else {