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