NEW Allow sync of currency rates with currency layer by default.

This commit is contained in:
Laurent Destailleur 2023-10-30 01:38:20 +01:00
parent c13d1abd0c
commit 16ea13ecb8
3 changed files with 62 additions and 51 deletions

View file

@ -215,7 +215,7 @@ if ($conf->use_javascript_ajax) {
print '</td></tr>';
// 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 '<tr class="oddeven">';
print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
print '<td class="center">';
@ -260,51 +260,6 @@ print '</td></tr>';
print '</table>';
print '</div>';
print '<br>';
if (getDolGlobalString('MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION')) {
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="setapilayer">';
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
$urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
print '<tr class="liste_titre">';
print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
print '<td class="right">';
print '<textarea id="response" class="hideobject" name="response"></textarea>';
print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
print '</td></tr>';
print '<tr class="oddeven">';
print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_APP_ID" value="' . getDolGlobalString('MULTICURRENCY_APP_ID').'" size="28" />&nbsp;';
print '</td></tr>';
print '<tr class="oddeven">';
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="' . getDolGlobalString('MULTICURRENCY_APP_SOURCE').'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
print '</form>';
print '</td></tr>';
/*print '<tr class="oddeven">';
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
print '</td></tr>';*/
print '</table>';
print '</div>';
print '<br>';
print '</form>';
}
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent nomarginbottom">';
@ -377,6 +332,53 @@ print '
</script>
';
print '<br>';
if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) {
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="setapilayer">';
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
$urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
print '<tr class="liste_titre">';
print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
print '<td class="right">';
print '<textarea id="response" class="hideobject" name="response"></textarea>';
print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
print '</td></tr>';
print '<tr class="oddeven">';
print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_APP_ID" value="' . getDolGlobalString('MULTICURRENCY_APP_ID').'" size="28" />&nbsp;';
print '</td></tr>';
print '<tr class="oddeven">';
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="' . getDolGlobalString('MULTICURRENCY_APP_SOURCE').'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
print '</td></tr>';
/*print '<tr class="oddeven">';
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
print '<td class="right">';
print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
print '</td></tr>';*/
print '</table>';
print '</div>';
print '<br>';
print '</form>';
}
// End of page
llxFooter();
$db->close();

View file

@ -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

View file

@ -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 {