dolibarr  13.0.2
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) {
38 }
39 
40 // Parameters
41 $action = GETPOST('action', 'aZ09');
42 
43 
44 /*
45  * Actions
46  */
47 
48 
49 if (preg_match('/set_([a-z0-9_\-]+)/i', $action, $reg))
50 {
51  $code = $reg[1];
52  $value = GETPOST($code, 'alpha');
53  if (dolibarr_set_const($db, $code, $value, 'chaine', 0, '', $conf->entity) > 0)
54  {
55  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
56  } else {
57  setEventMessages($langs->trans("Error"), null, 'errors');
58  }
59 }
60 
61 if (preg_match('/del_([a-z0-9_\-]+)/i', $action, $reg))
62 {
63  $code = $reg[1];
64  if (dolibarr_del_const($db, $code, 0) > 0)
65  {
66  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
67  } else {
68  setEventMessages($langs->trans("Error"), null, 'errors');
69  }
70 }
71 
72 if ($action == 'add_currency')
73 {
74  $error = 0;
75 
76  $langs->loadCacheCurrencies('');
77 
78  $code = GETPOST('code', 'alpha');
79  $rate = price2num(GETPOST('rate', 'alpha'));
80  $currency = new MultiCurrency($db);
81  $currency->code = $code;
82  $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
83 
84  if (empty($rate))
85  {
86  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
87  $error++;
88  }
89  if (!$error)
90  {
91  if ($currency->create($user) > 0)
92  {
93  if ($currency->addRate($rate)) setEventMessages($langs->trans('RecordSaved'), array());
94  else setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
95  } else setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
96  }
97 } elseif ($action == 'update_currency')
98 {
99  $error = 0;
100 
101  if (GETPOST('updatecurrency', 'alpha'))
102  {
103  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
104  $rate = price2num(GETPOST('rate', 'alpha'));
105  $currency = new MultiCurrency($db);
106 
107  if (empty($rate))
108  {
109  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
110  $error++;
111  }
112  if (!$error)
113  {
114  if ($currency->fetch($fk_multicurrency) > 0)
115  {
116  $result = $currency->updateRate($rate);
117  if ($result < 0) {
118  setEventMessages(null, $currency->errors, 'errors');
119  }
120  }
121  }
122  } elseif (GETPOST('deletecurrency', 'alpha'))
123  {
124  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
125  $currency = new MultiCurrency($db);
126 
127  if ($currency->fetch($fk_multicurrency) > 0)
128  {
129  if ($currency->delete() > 0) setEventMessages($langs->trans('RecordDeleted'), array());
130  else setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
131  }
132  }
133 } elseif ($action == 'setapilayer')
134 {
135  if (GETPOSTISSET('modify_apilayer'))
136  {
137  dolibarr_set_const($db, 'MULTICURRENCY_APP_ID', GETPOST('MULTICURRENCY_APP_ID', 'alpha'));
138  dolibarr_set_const($db, 'MULTICURRENCY_APP_SOURCE', GETPOST('MULTICURRENCY_APP_SOURCE', 'alpha'));
139  //dolibarr_set_const($db, 'MULTICURRENCY_ALTERNATE_SOURCE', GETPOST('MULTICURRENCY_ALTERNATE_SOURCE', 'alpha'));
140  } else {
141  $result = MultiCurrency::syncRates($conf->global->MULTICURRENCY_APP_ID);
142  if ($result > 0) {
143  setEventMessages($langs->trans("CurrencyRateSyncSucceed"), null, "mesgs");
144  }
145  }
146 }
147 
148 
149 $TCurrency = array();
150 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
151 $resql = $db->query($sql);
152 if ($resql)
153 {
154  while ($obj = $db->fetch_object($resql))
155  {
156  $currency = new MultiCurrency($db);
157  $currency->fetch($obj->rowid);
158  $TCurrency[] = $currency;
159  }
160 }
161 
162 
163 /*
164  * View
165  */
166 
167 $form = new Form($db);
168 
169 $page_name = "MultiCurrencySetup";
170 
171 llxHeader('', $langs->trans($page_name));
172 
173 // Subheader
174 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
175 print load_fiche_titre($langs->trans($page_name), $linkback);
176 
177 // Configuration header
179 print dol_get_fiche_head($head, 'settings', $langs->trans("ModuleSetup"), -1, "multicurrency");
180 
181 
182 print '<table class="noborder centpercent">';
183 print '<tr class="liste_titre">';
184 print '<td>'.$langs->trans("Parameters").'</td>'."\n";
185 print '<td class="center">'.$langs->trans("Status").'</td>'."\n";
186 print '</tr>';
187 
188 print '<tr class="oddeven">';
189 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
190 print '<td class="center">';
191 if ($conf->use_javascript_ajax) {
192  print ajax_constantonoff('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE');
193 } else {
194  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
195  print $form->selectarray("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE", $arrval, $conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE);
196 }
197 print '</td></tr>';
198 
199 
200 print '<tr class="oddeven">';
201 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
202 print '<td class="center">';
203 if ($conf->use_javascript_ajax) {
204  print ajax_constantonoff('MULTICURRENCY_USE_ORIGIN_TX');
205 } else {
206  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
207  print $form->selectarray("MULTICURRENCY_USE_ORIGIN_TX", $arrval, $conf->global->MULTICURRENCY_USE_ORIGIN_TX);
208 }
209 print '</td></tr>';
210 
211 // Online payment with currency on document. This option should be on by default.
212 if ($conf->global->MAIN_FEATURES_LEVEL >= 2)
213 {
214  print '<tr class="oddeven">';
215  print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
216  print '<td class="center">';
217  if ($conf->use_javascript_ajax) {
218  print ajax_constantonoff('MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT');
219  } else {
220  $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
221  print $form->selectarray("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT", $arrval, $conf->global->MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT);
222  }
223  print '</td></tr>';
224 }
225 
226 /* TODO uncomment when the functionality will integrated
227 
228 print '<tr class="oddeven">';
229 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
230 print '<td class="right">';
231 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
232 print '<input type="hidden" name="token" value="'.newToken().'">';
233 print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
234 print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
235 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
236 print '</form>';
237 print '</td></tr>';
238 */
239 
240 /* TODO uncomment when the functionality will integrated
241 
242 print '<tr class="oddeven">';
243 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
244 print '<td class="right">';
245 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
246 print '<input type="hidden" name="token" value="'.newToken().'">';
247 print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
248 print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
249 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
250 print '</form>';
251 print '</td></tr>';
252 
253 */
254 
255 print '</table>';
256 
257 print '<br>';
258 
259 if (!empty($conf->global->MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION))
260 {
261  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
262  print '<input type="hidden" name="token" value="'.newToken().'">';
263  print '<input type="hidden" name="action" value="setapilayer">';
264 
265  print '<div class="div-table-responsive-no-min">';
266  print '<table class="noborder centpercent">';
267 
268  $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
269 
270  print '<tr class="liste_titre">';
271  print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
272  print '<td class="right">';
273  print '<textarea id="response" class="hideobject" name="response"></textarea>';
274  print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
275  print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
276  print '</td></tr>';
277 
278  print '<tr class="oddeven">';
279  print '<td class="fieldrequired"><a target="_blank" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
280  print '<td class="right">';
281  print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
282  print '</td></tr>';
283 
284  print '<tr class="oddeven">';
285  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
286  print '<td class="right">';
287  print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
288  print '</form>';
289  print '</td></tr>';
290 
291  /*print '<tr class="oddeven">';
292  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
293  print '<td class="right">';
294  print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
295  print '</td></tr>';*/
296 
297  print '</table>';
298  print '</div>';
299  print '<br>';
300 
301  print '</form>';
302 }
303 
304 print '<div class="div-table-responsive-no-min">';
305 print '<table class="noborder centpercent">';
306 
307 print '<tr class="liste_titre">';
308 print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
309 print '<td class="center">'.$langs->trans("Rate").'</td>'."\n";
310 print '</tr>';
311 
312 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
313 print '<input type="hidden" name="token" value="'.newToken().'">';
314 print '<input type="hidden" name="action" value="add_currency">';
315 
316 print '<tr class="oddeven">';
317 print '<td>'.$form->selectCurrency('', 'code', 1).'</td>';
318 print '<td class="right">';
319 print '<input type="text" name="rate" value="" size="13" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
320 print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
321 print '</td>';
322 print '</tr>';
323 
324 print '</form>';
325 
326 print '<tr class="oddeven">';
327 print '<td>'.$conf->currency.$form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>';
328 print '<td class="right">1</td>';
329 print '</tr>';
330 
331 foreach ($TCurrency as &$currency)
332 {
333  if ($currency->code == $conf->currency) continue;
334 
335  print '<tr class="oddeven">';
336  print '<td>'.$currency->code.' - '.$currency->name.'</td>';
337  print '<td class="right">';
338  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
339  print '<input type="hidden" name="token" value="'.newToken().'">';
340  print '<input type="hidden" name="action" value="update_currency">';
341  print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
342  print '1 '.$conf->currency.' = ';
343  print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" />&nbsp;'.$currency->code.'&nbsp;';
344  print '<input type="submit" name="updatecurrency" class="button" value="'.$langs->trans("Modify").'">&nbsp;';
345  print '<input type="submit" name="deletecurrency" class="button" value="'.$langs->trans("Delete").'">';
346  print '</form>';
347  print '</td></tr>';
348 }
349 
350 print '</table>';
351 print '</div>';
352 
353 print '
354  <script type="text/javascript">
355  function getRates()
356  {
357  $("#bt_sync").attr("disabled", true);
358  return true;
359  }
360  </script>
361 ';
362 
363 // End of page
364 llxFooter();
365 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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:575
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
static syncRates($key, $addifnotfound=0)
Sync rates from API.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dolibarr_del_const($db, $name, $entity=1)
Effacement d&#39;une constante dans la base de donnees.
Definition: admin.lib.php:499
ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0, $forcereload=0, $marginleftonlyshort=2, $forcenoajax=0)
On/off button for constant.
Definition: ajax.lib.php:503
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 ...
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
multicurrencyAdminPrepareHead()
Prepare array with list of tabs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class Currency.
llxFooter()
Empty footer.
Definition: wrapper.php:59