dolibarr 21.0.0-beta
multicurrency.php
Go to the documentation of this file.
1<?php
2/* <one line to give the program's name and a brief idea of what it does.>
3 * Copyright (C) 2015 ATM Consulting <support@atm-consulting.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
32
41// Load translation files required by the page
42$langs->loadLangs(array('admin', 'multicurrency'));
43
44// Access control
45if (!$user->admin || !isModEnabled('multicurrency')) {
47}
48
49// Parameters
50$action = GETPOST('action', 'aZ09');
51
52
53/*
54 * Actions
55 */
56
57$reg = array();
58if (preg_match('/set_([a-z0-9_\-]+)/i', $action, $reg)) {
59 $code = $reg[1];
60 $value = GETPOST($code, 'alpha');
61 if (dolibarr_set_const($db, $code, $value, 'chaine', 0, '', $conf->entity) > 0) {
62 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
63 } else {
64 setEventMessages($langs->trans("Error"), null, 'errors');
65 }
66}
67
68if (preg_match('/del_([a-z0-9_\-]+)/i', $action, $reg)) {
69 $code = $reg[1];
70 if (dolibarr_del_const($db, $code, 0) > 0) {
71 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
72 } else {
73 setEventMessages($langs->trans("Error"), null, 'errors');
74 }
75}
76
77if ($action == 'add_currency') {
78 $error = 0;
79
80 $langs->loadCacheCurrencies('');
81
82 $code = GETPOST('code', 'alpha');
83 $rate = price2num(GETPOST('rate', 'alpha'));
84 $currency = new MultiCurrency($db);
85 $currency->code = $code;
86 $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
87
88 if (empty($currency->code) || $currency->code == '-1') {
89 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Currency")), null, 'errors');
90 $error++;
91 }
92 if (empty($rate)) {
93 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
94 $error++;
95 }
96
97 if (!$error) {
98 if ($currency->create($user) > 0) {
99 if ($currency->addRate($rate)) {
100 setEventMessages($langs->trans('RecordSaved'), array());
101 } else {
102 setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
103 }
104 } else {
105 setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
106 }
107 }
108} elseif ($action == 'update_currency') {
109 $error = 0;
110
111 if (GETPOST('updatecurrency', 'alpha')) {
112 $fk_multicurrency = GETPOSTINT('fk_multicurrency');
113 $rate = price2num(GETPOST('rate', 'alpha'));
114 $currency = new MultiCurrency($db);
115
116 if (empty($rate)) {
117 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
118 $error++;
119 }
120 if (!$error) {
121 if ($currency->fetch($fk_multicurrency) > 0) {
122 $result = $currency->updateRate($rate);
123 if ($result < 0) {
124 setEventMessages(null, $currency->errors, 'errors');
125 }
126 }
127 }
128 } elseif (GETPOST('deletecurrency', 'alpha')) {
129 $fk_multicurrency = GETPOSTINT('fk_multicurrency');
130 $currency = new MultiCurrency($db);
131
132 if ($currency->fetch($fk_multicurrency) > 0) {
133 if ($currency->delete($user) > 0) {
134 setEventMessages($langs->trans('RecordDeleted'), array());
135 } else {
136 setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
137 }
138 }
139 }
140} elseif ($action == 'setapilayer') {
141 if (GETPOSTISSET('modify_apilayer')) {
142 dolibarr_set_const($db, 'MULTICURRENCY_APP_ID', GETPOST('MULTICURRENCY_APP_ID', 'alpha'));
143 dolibarr_set_const($db, 'MULTICURRENCY_APP_SOURCE', GETPOST('MULTICURRENCY_APP_SOURCE', 'alpha'));
144 //dolibarr_set_const($db, 'MULTICURRENCY_ALTERNATE_SOURCE', GETPOST('MULTICURRENCY_ALTERNATE_SOURCE', 'alpha'));
145 } else {
146 $multiurrency = new MultiCurrency($db);
147 $result = $multiurrency->syncRates(getDolGlobalString('MULTICURRENCY_APP_ID'));
148 if ($result > 0) {
149 setEventMessages($langs->trans("CurrencyRateSyncSucceed"), null, "mesgs");
150 }
151 }
152}
153
154
155$TAvailableCurrency = array();
156$sql = "SELECT code_iso, label, unicode, active FROM ".MAIN_DB_PREFIX."c_currencies";
157$resql = $db->query($sql);
158if ($resql) {
159 while ($obj = $db->fetch_object($resql)) {
160 $TAvailableCurrency[$obj->code_iso] = array('code' => $obj->code_iso, 'active' => $obj->active);
161 }
162}
163
164$TCurrency = array();
165$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE entity = ".((int) $conf->entity);
166$resql = $db->query($sql);
167if ($resql) {
168 while ($obj = $db->fetch_object($resql)) {
169 $currency = new MultiCurrency($db);
170 $currency->fetch($obj->rowid);
171 $TCurrency[] = $currency;
172 }
173}
174
175
176/*
177 * View
178 */
179
180$form = new Form($db);
181
182$page_name = "MultiCurrencySetup";
183$help_url = '';
184
185llxHeader('', $langs->trans($page_name), $help_url, '', 0, 0, '', '', '', 'mod-admin page-multicurrency');
186
187// Subheader
188$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
189print load_fiche_titre($langs->trans($page_name), $linkback);
190
191// Configuration header
193print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "multicurrency");
194
195
196print '<div class="div-table-responsive-no-min">';
197print '<table class="noborder centpercent">';
198print '<tr class="liste_titre">';
199print '<td>'.$langs->trans("Parameters").'</td>'."\n";
200print '<td class="center">'.$langs->trans("Status").'</td>'."\n";
201print '</tr>';
202
203print '<tr class="oddeven">';
204print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
205print '<td class="center">';
206if ($conf->use_javascript_ajax) {
207 print ajax_constantonoff('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE');
208} else {
209 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
210 print $form->selectarray("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE", $arrval, $conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE);
211}
212print '</td></tr>';
213
214
215print '<tr class="oddeven">';
216print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
217print '<td class="center">';
218if ($conf->use_javascript_ajax) {
219 print ajax_constantonoff('MULTICURRENCY_USE_ORIGIN_TX', array(), null, 0, 0, 0, 2, 0, 1);
220} else {
221 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
222 print $form->selectarray("MULTICURRENCY_USE_ORIGIN_TX", $arrval, $conf->global->MULTICURRENCY_USE_ORIGIN_TX);
223}
224print '</td></tr>';
225
226// Online payment with currency on document. This option should be on by default.
227if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) {
228 print '<tr class="oddeven">';
229 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
230 print '<td class="center">';
231 if ($conf->use_javascript_ajax) {
232 print ajax_constantonoff('MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT');
233 } else {
234 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
235 print $form->selectarray("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT", $arrval, $conf->global->MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT);
236 }
237 print '</td></tr>';
238}
239
240/* TODO uncomment when the functionality will integrated
241
242print '<tr class="oddeven">';
243print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
244print '<td class="right">';
245print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
246print '<input type="hidden" name="token" value="'.newToken().'">';
247print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
248print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
249print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
250print '</form>';
251print '</td></tr>';
252*/
253
254/* TODO uncomment when the functionality will integrated
255
256print '<tr class="oddeven">';
257print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
258print '<td class="right">';
259print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
260print '<input type="hidden" name="token" value="'.newToken().'">';
261print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
262print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
263print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
264print '</form>';
265print '</td></tr>';
266
267*/
268
269print '</table>';
270print '</div>';
271
272
273print '<div class="div-table-responsive-no-min">';
274print '<table class="noborder centpercent nomarginbottom">';
275
276print '<tr class="liste_titre">';
277print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
278print '<td class="right">'.$langs->trans("Rate").' / '.$langs->getCurrencySymbol($conf->currency).'</td>'."\n";
279print '</tr>';
280
281print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
282print '<input type="hidden" name="token" value="'.newToken().'">';
283print '<input type="hidden" name="action" value="add_currency">';
284
285print '<tr class="oddeven">';
286print '<td>'.$form->selectCurrency('', 'code', 1, '1').'</td>';
287print '<td class="right">';
288print '<input type="text" name="rate" value="" class="width75 right" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
289print '<input type="submit" class="button button-add smallpaddingimp" value="'.$langs->trans("Add").'">';
290print '</td>';
291print '</tr>';
292
293print '</form>';
294
295// Main currency
296print '<tr class="oddeven">';
297print '<td>'.$conf->currency;
298print ' ('.$langs->getCurrencySymbol($conf->currency).')';
299print $form->textwithpicto(' ', $langs->trans("BaseCurrency"));
300if (!empty($TAvailableCurrency[$conf->currency]) && empty($TAvailableCurrency[$conf->currency]['active'])) {
301 print img_warning('Warning: This code has been disabled into Home - Setup - Dictionaries - Currencies');
302}
303print '</td>';
304print '<td class="right">1</td>';
305print '</tr>';
306
307foreach ($TCurrency as &$currency) {
308 if ($currency->code == $conf->currency) {
309 continue;
310 }
311
312 print '<tr class="oddeven">';
313 print '<td>'.$currency->code.' - '.$currency->name;
314 if (!empty($TAvailableCurrency[$currency->code]) && empty($TAvailableCurrency[$currency->code]['active'])) {
315 print img_warning('Warning: The code '.$currency->code.' has been disabled into Home - Setup - Dictionaries - Currencies');
316 }
317 print '</td>';
318 print '<td class="right">';
319 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
320 print '<input type="hidden" name="token" value="'.newToken().'">';
321 print '<input type="hidden" name="action" value="update_currency">';
322 print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
323 print '1 '.$conf->currency.' = ';
324 print '<input type="text" name="rate" class="width75 right" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13">&nbsp;'.$currency->code.'&nbsp;';
325 print '<input type="submit" name="updatecurrency" class="button button-edit smallpaddingimp" value="'.$langs->trans("Modify").'">&nbsp;';
326 print '<input type="submit" name="deletecurrency" class="button smallpaddingimp" value="'.$langs->trans("Delete").'">';
327 print '</form>';
328 print '</td></tr>';
329}
330
331print '</table>';
332print '</div>';
333
334print '
335 <script type="text/javascript">
336 function getRates()
337 {
338 $("#bt_sync").attr("disabled", true);
339 return true;
340 }
341 </script>
342';
343
344
345print '<br>';
346
347if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) {
348 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
349 print '<input type="hidden" name="token" value="'.newToken().'">';
350 print '<input type="hidden" name="action" value="setapilayer">';
351
352 print '<div class="div-table-responsive-no-min">';
353 print '<table class="noborder centpercent">';
354
355 $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
356
357 print '<tr class="liste_titre">';
358 print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
359 print '<td class="right">';
360 print '<textarea id="response" class="hideobject" name="response"></textarea>';
361 print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
362 print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
363 print '</td></tr>';
364
365 print '<tr class="oddeven">';
366 print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
367 print '<td class="right">';
368 print '<input type="text" name="MULTICURRENCY_APP_ID" value="' . getDolGlobalString('MULTICURRENCY_APP_ID').'" size="28" />&nbsp;';
369 print '</td></tr>';
370
371 print '<tr class="oddeven">';
372 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
373 print '<td class="right">';
374 print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="' . getDolGlobalString('MULTICURRENCY_APP_SOURCE').'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
375 print '</td></tr>';
376
377 /*print '<tr class="oddeven">';
378 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
379 print '<td class="right">';
380 print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
381 print '</td></tr>';*/
382
383 print '</table>';
384 print '</div>';
385 print '<br>';
386
387 print '</form>';
388}
389
390
391// End of page
392llxFooter();
393$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage generation of HTML components Only common components must be here.
Class Currency.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
multicurrencyAdminPrepareHead()
Prepare array with list of tabs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.