dolibarr  16.0.5
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 || empty($conf->multicurrency->enabled)) {
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  $result = MultiCurrency::syncRates($conf->global->MULTICURRENCY_APP_ID);
138  if ($result > 0) {
139  setEventMessages($langs->trans("CurrencyRateSyncSucceed"), null, "mesgs");
140  }
141  }
142 }
143 
144 
145 $TCurrency = array();
146 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE entity = ".((int) $conf->entity);
147 $resql = $db->query($sql);
148 if ($resql) {
149  while ($obj = $db->fetch_object($resql)) {
150  $currency = new MultiCurrency($db);
151  $currency->fetch($obj->rowid);
152  $TCurrency[] = $currency;
153  }
154 }
155 
156 
157 /*
158  * View
159  */
160 
161 $form = new Form($db);
162 
163 $page_name = "MultiCurrencySetup";
164 $help_url = '';
165 
166 llxHeader('', $langs->trans($page_name), $help_url);
167 
168 // Subheader
169 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
170 print load_fiche_titre($langs->trans($page_name), $linkback);
171 
172 // Configuration header
174 print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "multicurrency");
175 
176 
177 print '<div class="div-table-responsive-no-min">';
178 print '<table class="noborder centpercent">';
179 print '<tr class="liste_titre">';
180 print '<td>'.$langs->trans("Parameters").'</td>'."\n";
181 print '<td class="center">'.$langs->trans("Status").'</td>'."\n";
182 print '</tr>';
183 
184 print '<tr class="oddeven">';
185 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
186 print '<td class="center">';
187 if ($conf->use_javascript_ajax) {
188  print ajax_constantonoff('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE');
189 } else {
190  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
191  print $form->selectarray("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE", $arrval, $conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE);
192 }
193 print '</td></tr>';
194 
195 
196 print '<tr class="oddeven">';
197 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
198 print '<td class="center">';
199 if ($conf->use_javascript_ajax) {
200  print ajax_constantonoff('MULTICURRENCY_USE_ORIGIN_TX', null, null, 0, 0, 0, 2, 0, 1);
201 } else {
202  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
203  print $form->selectarray("MULTICURRENCY_USE_ORIGIN_TX", $arrval, $conf->global->MULTICURRENCY_USE_ORIGIN_TX);
204 }
205 print '</td></tr>';
206 
207 // Online payment with currency on document. This option should be on by default.
208 if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
209  print '<tr class="oddeven">';
210  print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
211  print '<td class="center">';
212  if ($conf->use_javascript_ajax) {
213  print ajax_constantonoff('MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT');
214  } else {
215  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
216  print $form->selectarray("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT", $arrval, $conf->global->MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT);
217  }
218  print '</td></tr>';
219 }
220 
221 /* TODO uncomment when the functionality will integrated
222 
223 print '<tr class="oddeven">';
224 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
225 print '<td class="right">';
226 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
227 print '<input type="hidden" name="token" value="'.newToken().'">';
228 print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
229 print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
230 print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
231 print '</form>';
232 print '</td></tr>';
233 */
234 
235 /* TODO uncomment when the functionality will integrated
236 
237 print '<tr class="oddeven">';
238 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
239 print '<td class="right">';
240 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
241 print '<input type="hidden" name="token" value="'.newToken().'">';
242 print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
243 print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
244 print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
245 print '</form>';
246 print '</td></tr>';
247 
248 */
249 
250 print '</table>';
251 print '</div>';
252 
253 print '<br>';
254 
255 if (!empty($conf->global->MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION)) {
256  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
257  print '<input type="hidden" name="token" value="'.newToken().'">';
258  print '<input type="hidden" name="action" value="setapilayer">';
259 
260  print '<div class="div-table-responsive-no-min">';
261  print '<table class="noborder centpercent">';
262 
263  $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
264 
265  print '<tr class="liste_titre">';
266  print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
267  print '<td class="right">';
268  print '<textarea id="response" class="hideobject" name="response"></textarea>';
269  print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
270  print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
271  print '</td></tr>';
272 
273  print '<tr class="oddeven">';
274  print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
275  print '<td class="right">';
276  print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
277  print '</td></tr>';
278 
279  print '<tr class="oddeven">';
280  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
281  print '<td class="right">';
282  print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
283  print '</form>';
284  print '</td></tr>';
285 
286  /*print '<tr class="oddeven">';
287  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
288  print '<td class="right">';
289  print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
290  print '</td></tr>';*/
291 
292  print '</table>';
293  print '</div>';
294  print '<br>';
295 
296  print '</form>';
297 }
298 
299 print '<div class="div-table-responsive-no-min">';
300 print '<table class="noborder centpercent nomarginbottom">';
301 
302 print '<tr class="liste_titre">';
303 print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
304 print '<td class="right">'.$langs->trans("Rate").' / '.$langs->getCurrencySymbol($conf->currency).'</td>'."\n";
305 print '</tr>';
306 
307 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
308 print '<input type="hidden" name="token" value="'.newToken().'">';
309 print '<input type="hidden" name="action" value="add_currency">';
310 
311 print '<tr class="oddeven">';
312 print '<td>'.$form->selectCurrency('', 'code', 1, '1').'</td>';
313 print '<td class="right">';
314 print '<input type="text" name="rate" value="" class="width75 right" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
315 print '<input type="submit" class="button button-add smallpaddingimp" value="'.$langs->trans("Add").'">';
316 print '</td>';
317 print '</tr>';
318 
319 print '</form>';
320 
321 print '<tr class="oddeven">';
322 print '<td>'.$conf->currency;
323 print ' ('.$langs->getCurrencySymbol($conf->currency).')';
324 print $form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>';
325 print '<td class="right">1</td>';
326 print '</tr>';
327 
328 foreach ($TCurrency as &$currency) {
329  if ($currency->code == $conf->currency) {
330  continue;
331  }
332 
333  print '<tr class="oddeven">';
334  print '<td>'.$currency->code.' - '.$currency->name.'</td>';
335  print '<td class="right">';
336  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
337  print '<input type="hidden" name="token" value="'.newToken().'">';
338  print '<input type="hidden" name="action" value="update_currency">';
339  print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
340  print '1 '.$conf->currency.' = ';
341  print '<input type="text" name="rate" class="width75 right" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13">&nbsp;'.$currency->code.'&nbsp;';
342  print '<input type="submit" name="updatecurrency" class="button button-edit smallpaddingimp" value="'.$langs->trans("Modify").'">&nbsp;';
343  print '<input type="submit" name="deletecurrency" class="button smallpaddingimp" value="'.$langs->trans("Delete").'">';
344  print '</form>';
345  print '</td></tr>';
346 }
347 
348 print '</table>';
349 print '</div>';
350 
351 print '
352  <script type="text/javascript">
353  function getRates()
354  {
355  $("#bt_sync").attr("disabled", true);
356  return true;
357  }
358  </script>
359 ';
360 
361 // End of page
362 llxFooter();
363 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
dolibarr_del_const
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:552
MultiCurrency\syncRates
static syncRates($key, $addifnotfound=0)
Sync rates from API.
Definition: multicurrency.class.php:640
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
$help_url
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:116
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
MultiCurrency
Class Currency.
Definition: multicurrency.class.php:39
ajax_constantonoff
ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0, $forcereload=0, $marginleftonlyshort=2, $forcenoajax=0, $setzeroinsteadofdel=0, $suffix='', $mode='')
On/off button for constant.
Definition: ajax.lib.php:573
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dolibarr_set_const
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:627
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
multicurrencyAdminPrepareHead
multicurrencyAdminPrepareHead()
Prepare array with list of tabs.
Definition: multicurrency.lib.php:31
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59