dolibarr 21.0.3
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'), 'chaine', 0, '', $conf->entity);
143 dolibarr_set_const($db, 'MULTICURRENCY_APP_SOURCE', GETPOST('MULTICURRENCY_APP_SOURCE', 'alpha'), 'chaine', 0, '', $conf->entity);
144 } else {
145 $multiurrency = new MultiCurrency($db);
146 $result = $multiurrency->syncRates(getDolGlobalString('MULTICURRENCY_APP_ID'));
147 if ($result > 0) {
148 setEventMessages($langs->trans("CurrencyRateSyncSucceed"), null, "mesgs");
149 }
150 }
151}
152
153
154$TAvailableCurrency = array();
155$sql = "SELECT code_iso, label, unicode, active FROM ".MAIN_DB_PREFIX."c_currencies";
156$resql = $db->query($sql);
157if ($resql) {
158 while ($obj = $db->fetch_object($resql)) {
159 $TAvailableCurrency[$obj->code_iso] = array('code' => $obj->code_iso, 'active' => $obj->active);
160 }
161}
162
163$TCurrency = array();
164$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE entity = ".((int) $conf->entity);
165$resql = $db->query($sql);
166if ($resql) {
167 while ($obj = $db->fetch_object($resql)) {
168 $currency = new MultiCurrency($db);
169 $currency->fetch($obj->rowid);
170 $TCurrency[] = $currency;
171 }
172}
173
174
175/*
176 * View
177 */
178
179$form = new Form($db);
180
181$page_name = "MultiCurrencySetup";
182$help_url = '';
183
184llxHeader('', $langs->trans($page_name), $help_url, '', 0, 0, '', '', '', 'mod-admin page-multicurrency');
185
186// Subheader
187$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
188print load_fiche_titre($langs->trans($page_name), $linkback);
189
190// Configuration header
192print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "multicurrency");
193
194
195print '<div class="div-table-responsive-no-min">';
196print '<table class="noborder centpercent">';
197print '<tr class="liste_titre">';
198print '<td>'.$langs->trans("Parameters").'</td>'."\n";
199print '<td class="center">'.$langs->trans("Status").'</td>'."\n";
200print '</tr>';
201
202print '<tr class="oddeven">';
203print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
204print '<td class="center">';
205if ($conf->use_javascript_ajax) {
206 print ajax_constantonoff('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE');
207} else {
208 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
209 print $form->selectarray("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE", $arrval, $conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE);
210}
211print '</td></tr>';
212
213
214print '<tr class="oddeven">';
215print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
216print '<td class="center">';
217if ($conf->use_javascript_ajax) {
218 print ajax_constantonoff('MULTICURRENCY_USE_ORIGIN_TX', array(), null, 0, 0, 0, 2, 0, 1);
219} else {
220 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
221 print $form->selectarray("MULTICURRENCY_USE_ORIGIN_TX", $arrval, $conf->global->MULTICURRENCY_USE_ORIGIN_TX);
222}
223print '</td></tr>';
224
225// Online payment with currency on document. This option should be on by default.
226if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) {
227 print '<tr class="oddeven">';
228 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
229 print '<td class="center">';
230 if ($conf->use_javascript_ajax) {
231 print ajax_constantonoff('MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT');
232 } else {
233 $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
234 print $form->selectarray("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT", $arrval, $conf->global->MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT);
235 }
236 print '</td></tr>';
237}
238
239/* TODO uncomment when the functionality will integrated
240
241print '<tr class="oddeven">';
242print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
243print '<td class="right">';
244print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
245print '<input type="hidden" name="token" value="'.newToken().'">';
246print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
247print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
248print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
249print '</form>';
250print '</td></tr>';
251*/
252
253/* TODO uncomment when the functionality will integrated
254
255print '<tr class="oddeven">';
256print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
257print '<td class="right">';
258print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
259print '<input type="hidden" name="token" value="'.newToken().'">';
260print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
261print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
262print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
263print '</form>';
264print '</td></tr>';
265
266*/
267
268print '</table>';
269print '</div>';
270
271
272print '<div class="div-table-responsive-no-min">';
273print '<table class="noborder centpercent nomarginbottom">';
274
275print '<tr class="liste_titre">';
276print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
277print '<td class="right">'.$langs->trans("Rate").' / '.$langs->getCurrencySymbol($conf->currency).'</td>'."\n";
278print '</tr>';
279
280print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
281print '<input type="hidden" name="token" value="'.newToken().'">';
282print '<input type="hidden" name="action" value="add_currency">';
283
284print '<tr class="oddeven">';
285print '<td>'.$form->selectCurrency('', 'code', 1, '1').'</td>';
286print '<td class="right">';
287print '<input type="text" name="rate" value="" class="width75 right" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
288print '<input type="submit" class="button button-add smallpaddingimp" value="'.$langs->trans("Add").'">';
289print '</td>';
290print '</tr>';
291
292print '</form>';
293
294// Main currency
295print '<tr class="oddeven">';
296print '<td>'.$conf->currency;
297print ' ('.$langs->getCurrencySymbol($conf->currency).')';
298print $form->textwithpicto(' ', $langs->trans("BaseCurrency"));
299if (!empty($TAvailableCurrency[$conf->currency]) && empty($TAvailableCurrency[$conf->currency]['active'])) {
300 print img_warning('Warning: This code has been disabled into Home - Setup - Dictionaries - Currencies');
301}
302print '</td>';
303print '<td class="right">1</td>';
304print '</tr>';
305
306foreach ($TCurrency as &$currency) {
307 if ($currency->code == $conf->currency) {
308 continue;
309 }
310
311 print '<tr class="oddeven">';
312 print '<td>'.$currency->code.' - '.$currency->name;
313 if (!empty($TAvailableCurrency[$currency->code]) && empty($TAvailableCurrency[$currency->code]['active'])) {
314 print img_warning('Warning: The code '.$currency->code.' has been disabled into Home - Setup - Dictionaries - Currencies');
315 }
316 print '</td>';
317 print '<td class="right">';
318 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
319 print '<input type="hidden" name="token" value="'.newToken().'">';
320 print '<input type="hidden" name="action" value="update_currency">';
321 print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
322 print '1 '.$conf->currency.' = ';
323 print '<input type="text" name="rate" class="width75 right" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13">&nbsp;'.$currency->code.'&nbsp;';
324 print '<input type="submit" name="updatecurrency" class="button button-edit smallpaddingimp" value="'.$langs->trans("Modify").'">&nbsp;';
325 print '<input type="submit" name="deletecurrency" class="button smallpaddingimp" value="'.$langs->trans("Delete").'">';
326 print '</form>';
327 print '</td></tr>';
328}
329
330print '</table>';
331print '</div>';
332
333print '
334 <script type="text/javascript">
335 function getRates()
336 {
337 $("#bt_sync").attr("disabled", true);
338 return true;
339 }
340 </script>
341';
342
343
344print '<br>';
345
346if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) {
347 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
348 print '<input type="hidden" name="token" value="'.newToken().'">';
349 print '<input type="hidden" name="action" value="setapilayer">';
350
351 print '<div class="div-table-responsive-no-min">';
352 print '<table class="noborder centpercent">';
353
354 $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
355
356 print '<tr class="liste_titre">';
357 print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
358 print '<td class="right">';
359 print '<textarea id="response" class="hideobject" name="response"></textarea>';
360 print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
361 print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
362 print '</td></tr>';
363
364 print '<tr class="oddeven">';
365 print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
366 print '<td class="right">';
367 print '<input type="text" name="MULTICURRENCY_APP_ID" value="' . getDolGlobalString('MULTICURRENCY_APP_ID').'" size="28" />&nbsp;';
368 print '</td></tr>';
369
370 print '<tr class="oddeven">';
371 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
372 print '<td class="right">';
373 print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="' . getDolGlobalString('MULTICURRENCY_APP_SOURCE').'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
374 print '</td></tr>';
375
376 /*print '<tr class="oddeven">';
377 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
378 print '<td class="right">';
379 print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
380 print '</td></tr>';*/
381
382 print '</table>';
383 print '</div>';
384 print '<br>';
385
386 print '</form>';
387}
388
389
390// End of page
391llxFooter();
392$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.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:87
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.
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.