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