dolibarr  7.0.0-beta
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 <http://www.gnu.org/licenses/>.
17  */
18 
25 // Dolibarr environment
26 
27 require '../main.inc.php';
28 
29 // Libraries
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
33 
34 
35 // Translations
36 $langs->load("admin");
37 $langs->load("multicurrency");
38 
39 // Access control
40 if (! $user->admin) {
42 }
43 
44 // Parameters
45 $action = GETPOST('action', 'alpha');
46 /*
47  * Actions
48  */
49 
50 
51 if (preg_match('/set_(.*)/',$action,$reg))
52 {
53  $code=$reg[1];
54  if (dolibarr_set_const($db, $code, GETPOST($code), 'chaine', 0, '', $conf->entity) > 0)
55  {
56  header("Location: ".$_SERVER["PHP_SELF"]);
57  exit;
58  }
59  else
60  {
61  dol_print_error($db);
62  }
63 }
64 
65 if (preg_match('/del_(.*)/',$action,$reg))
66 {
67  $code=$reg[1];
68  if (dolibarr_del_const($db, $code, 0) > 0)
69  {
70  header("Location: ".$_SERVER["PHP_SELF"]);
71  exit;
72  }
73  else
74  {
75  dol_print_error($db);
76  }
77 }
78 
79 if ($action == 'add_currency')
80 {
81  $error=0;
82 
83  $langs->loadCacheCurrencies('');
84 
85  $code = GETPOST('code', 'alpha');
86  $rate = price2num(GETPOST('rate', 'alpha'));
87  $currency = new MultiCurrency($db);
88  $currency->code = $code;
89  $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
90 
91  if (empty($rate))
92  {
93  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
94  $error++;
95  }
96  if (! $error)
97  {
98  if ($currency->create($user) > 0)
99  {
100  if ($currency->addRate($rate)) setEventMessages($langs->trans('RecordSaved'), array());
101  else setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
102  }
103  else setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
104  }
105 }
106 elseif ($action == 'update_currency')
107 {
108  $error = 0;
109 
110  $submit = GETPOST('submit', 'alpha');
111 
112  if ($submit == $langs->trans('Modify'))
113  {
114  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
115  $rate = price2num(GETPOST('rate', 'alpha'));
116  $currency = new MultiCurrency($db);
117 
118  if (empty($rate))
119  {
120  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
121  $error++;
122  }
123  if (! $error)
124  {
125  if ($currency->fetch($fk_multicurrency) > 0)
126  {
127  $currency->updateRate($rate);
128  }
129  }
130  }
131  elseif ($submit == $langs->trans('Delete'))
132  {
133  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
134  $currency = new MultiCurrency($db);
135 
136  if ($currency->fetch($fk_multicurrency) > 0)
137  {
138  if ($currency->delete() > 0) setEventMessages($langs->trans('RecordDeleted'), array());
139  else setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
140  }
141  }
142 }
143 elseif ($action == 'synchronize')
144 {
145  $response = GETPOST('response');
146  $response = json_decode($response);
147 
148  if ($response->success)
149  {
150  MultiCurrency::syncRates($response);
151  }
152  else
153  {
154  setEventMessages($langs->trans('multicurrency_syncronize_error', $response->error->info), null, 'errors');
155  }
156 }
157 
158 
159 $TCurrency = array();
160 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
161 $resql = $db->query($sql);
162 if ($resql)
163 {
164  while ($obj = $db->fetch_object($resql))
165  {
166  $currency = new MultiCurrency($db);
167  $currency->fetch($obj->rowid);
168  $TCurrency[] = $currency;
169  }
170 }
171 
172 
173 /*
174  * View
175  */
176 
177 $page_name = "MultiCurrencySetup";
178 
179 llxHeader('', $langs->trans($page_name));
180 
181 // Subheader
182 $linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php">' . $langs->trans("BackToModuleList") . '</a>';
183 print_fiche_titre($langs->trans($page_name), $linkback);
184 
185 // Configuration header
187 dol_fiche_head($head, 'settings', $langs->trans("ModuleSetup"), -1, "multicurrency");
188 
189 // Setup page goes here
190 $form=new Form($db);
191 
192 $var=false;
193 print '<table class="noborder" width="100%">';
194 print '<tr class="liste_titre">';
195 print '<td>'.$langs->trans("Parameters").'</td>'."\n";
196 print '<td align="center" width="20">&nbsp;</td>';
197 print '<td align="center" width="100">'.$langs->trans("Value").'</td>'."\n";
198 
199 
200 print '<tr class="oddeven">';
201 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
202 print '<td align="center" width="20">&nbsp;</td>';
203 print '<td align="right" width="400">';
204 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
205 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
206 print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE">';
207 print $form->selectyesno("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE",$conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE,1);
208 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
209 print '</form>';
210 print '</td></tr>';
211 
212 
213 
214 print '<tr class="oddeven">';
215 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
216 print '<td align="center" width="20">&nbsp;</td>';
217 print '<td align="right" width="400">';
218 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
219 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
220 print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_ORIGIN_TX">';
221 print $form->selectyesno("MULTICURRENCY_USE_ORIGIN_TX",$conf->global->MULTICURRENCY_USE_ORIGIN_TX,1);
222 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
223 print '</form>';
224 print '</td></tr>';
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 align="center" width="20">&nbsp;</td>';
231 print '<td align="right" width="400">';
232 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
233 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
234 print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
235 print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
236 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
237 print '</form>';
238 print '</td></tr>';
239 */
240 
241 /* TODO uncomment when the functionality will integrated
242 
243 print '<tr class="oddeven">';
244 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
245 print '<td align="center" width="20">&nbsp;</td>';
246 print '<td align="right" width="400">';
247 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
248 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
249 print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
250 print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
251 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
252 print '</form>';
253 print '</td></tr>';
254 
255 */
256 
257 print '</table>';
258 print '<br>';
259 
260 if (!empty($conf->global->MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION))
261 {
262  $var=false;
263  print '<table class="noborder" width="100%">';
264  print '<tr class="liste_titre">';
265  print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize")).'</td>'."\n";
266  print '<td align="center" width="20">&nbsp;</td>';
267  print '<td align="right" width="100">';
268  print '<form id="form_sync" action="" method="POST">';
269  print '<input type="hidden" name="action" value="synchronize" />';
270  print '<textarea id="response" class="hideobject" name="response"></textarea>';
271  print $langs->trans("Value").'&nbsp;<input type="button" id="bt_sync" class="button" onclick="javascript:getRates();" value="'.$langs->trans('Synchronize').'" />';
272  print '</form>';
273  print '</td></tr>';
274 
275 
276 
277  print '<tr class="oddeven">';
278  print '<td><a target="_blank" href="https://currencylayer.com">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
279  print '<td align="center" width="20">&nbsp;</td>';
280  print '<td align="right" width="400">';
281  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
282  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
283  print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_ID">';
284  print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
285  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
286  print '</form>';
287  print '</td></tr>';
288 
289 
290  print '<tr class="oddeven">';
291  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
292  print '<td align="center" width="20">&nbsp;</td>';
293  print '<td align="right" width="400">';
294  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
295  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
296  print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_SOURCE">';
297  print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
298  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
299  print '</form>';
300  print '</td></tr>';
301 
302  print '<tr class="oddeven">';
303  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
304  print '<td align="center" width="20">&nbsp;</td>';
305  print '<td align="right" width="400">';
306  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
307  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
308  print '<input type="hidden" name="action" value="set_MULTICURRENCY_ALTERNATE_SOURCE">';
309  print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
310  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
311  print '</form>';
312  print '</td></tr>';
313 
314  print '</table>';
315  print '<br>';
316 }
317 
318 
319 print '<table class="noborder" width="100%">';
320 
321 print '<tr class="liste_titre">';
322 print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
323 print '<td align="center" width="20">&nbsp;</td>';
324 print '<td align="center" width="100">'.$langs->trans("Rate").'</td>'."\n";
325 
326 
327 print '<tr class="oddeven">';
328 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
329 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
330 print '<input type="hidden" name="action" value="add_currency">';
331 print '<td>'.$form->selectCurrency('', 'code').'</td>';
332 print '<td align="center" width="20">&nbsp;</td>';
333 print '<td align="right" width="300">';
334 print '<input type="text" name="rate" value="" size="13" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
335 print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
336 print '</td></form></tr>';
337 
338 
339 print '<tr class="oddeven">';
340 print '<td>'.$conf->currency.$form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>';
341 print '<td align="center" width="20">&nbsp;</td>';
342 print '<td align="right" width="300">1';
343 print '</td></form></tr>';
344 
345 foreach ($TCurrency as &$currency)
346 {
347  if ($currency->code == $conf->currency) continue;
348 
349  print '<tr class="oddeven">';
350  print '<td>'.$currency->code.' - '.$currency->name.'</td>';
351  print '<td align="center" width="20">&nbsp;</td>';
352  print '<td align="right" width="400">';
353  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
354  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
355  print '<input type="hidden" name="action" value="update_currency">';
356  print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
357  print '1 '.$conf->currency.' = ';
358  print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" />&nbsp;'.$currency->code.'&nbsp;';
359  print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Modify").'">&nbsp;';
360  print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Delete").'">';
361  print '</form>';
362 
363  print '</td></tr>';
364 }
365 
366 print '</table>';
367 
368 
369 
370 print '
371  <script type="text/javascript">
372  function getRates()
373  {
374  $("#bt_sync").attr("disabled", true);
375  var url_sync = "http://apilayer.net/api/live?access_key='.$conf->global->MULTICURRENCY_APP_ID.'&format=1'.(!empty($conf->global->MULTICURRENCY_APP_SOURCE) ? '&source='.$conf->global->MULTICURRENCY_APP_SOURCE : '').'";
376 
377  $.ajax({
378  url: url_sync,
379  dataType: "jsonp"
380  }).done(function(response) {
381  $("#response").val(JSON.stringify(response));
382  $("#form_sync").submit();
383  });
384  }
385  </script>
386 ';
387 
388 llxFooter();
389 
390 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
static syncRates($response)
Sync rates from api.
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:485
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dolibarr_del_const($db, $name, $entity=1)
Effacement d'une constante dans la base de donnees.
Definition: admin.lib.php:410
llxHeader()
Empty header.
Definition: wrapper.php:46
print_fiche_titre($title, $mesg='', $picto='title_generic.png', $pictoisfullpath=0, $id='')
Show a title with picto.
print
Draft customers invoices.
Definition: index.php:91
multicurrencyAdminPrepareHead()
Prepare array with list of tabs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->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)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Class Currency.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...