dolibarr  9.0.0
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 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', 'alpha');
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') ? GETPOST($code, 'alpha') : 1);
53  if (dolibarr_set_const($db, $code, $value, 'chaine', 0, '', $conf->entity) > 0)
54  {
55  header("Location: ".$_SERVER["PHP_SELF"]);
56  exit;
57  }
58  else
59  {
60  dol_print_error($db);
61  }
62 }
63 
64 if (preg_match('/del_([a-z0-9_\-]+)/i',$action,$reg))
65 {
66  $code=$reg[1];
67  if (dolibarr_del_const($db, $code, 0) > 0)
68  {
69  header("Location: ".$_SERVER["PHP_SELF"]);
70  exit;
71  }
72  else
73  {
74  dol_print_error($db);
75  }
76 }
77 
78 if ($action == 'add_currency')
79 {
80  $error=0;
81 
82  $langs->loadCacheCurrencies('');
83 
84  $code = GETPOST('code', 'alpha');
85  $rate = price2num(GETPOST('rate', 'alpha'));
86  $currency = new MultiCurrency($db);
87  $currency->code = $code;
88  $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
89 
90  if (empty($rate))
91  {
92  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
93  $error++;
94  }
95  if (! $error)
96  {
97  if ($currency->create($user) > 0)
98  {
99  if ($currency->addRate($rate)) setEventMessages($langs->trans('RecordSaved'), array());
100  else setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
101  }
102  else setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
103  }
104 }
105 elseif ($action == 'update_currency')
106 {
107  $error = 0;
108 
109  $submit = GETPOST('submit', 'alpha');
110 
111  if ($submit == $langs->trans('Modify'))
112  {
113  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
114  $rate = price2num(GETPOST('rate', 'alpha'));
115  $currency = new MultiCurrency($db);
116 
117  if (empty($rate))
118  {
119  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
120  $error++;
121  }
122  if (! $error)
123  {
124  if ($currency->fetch($fk_multicurrency) > 0)
125  {
126  $currency->updateRate($rate);
127  }
128  }
129  }
130  elseif ($submit == $langs->trans('Delete'))
131  {
132  $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
133  $currency = new MultiCurrency($db);
134 
135  if ($currency->fetch($fk_multicurrency) > 0)
136  {
137  if ($currency->delete() > 0) setEventMessages($langs->trans('RecordDeleted'), array());
138  else setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
139  }
140  }
141 }
142 elseif ($action == 'synchronize')
143 {
144  MultiCurrency::syncRates($conf->global->MULTICURRENCY_APP_ID);
145 }
146 
147 
148 $TCurrency = array();
149 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
150 $resql = $db->query($sql);
151 if ($resql)
152 {
153  while ($obj = $db->fetch_object($resql))
154  {
155  $currency = new MultiCurrency($db);
156  $currency->fetch($obj->rowid);
157  $TCurrency[] = $currency;
158  }
159 }
160 
161 
162 /*
163  * View
164  */
165 
166 $form=new Form($db);
167 
168 $page_name = "MultiCurrencySetup";
169 
170 llxHeader('', $langs->trans($page_name));
171 
172 // Subheader
173 $linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1">' . $langs->trans("BackToModuleList") . '</a>';
174 print load_fiche_titre($langs->trans($page_name), $linkback);
175 
176 // Configuration header
178 dol_fiche_head($head, 'settings', $langs->trans("ModuleSetup"), -1, "multicurrency");
179 
180 
181 print '<table class="noborder" width="100%">';
182 print '<tr class="liste_titre">';
183 print '<td>'.$langs->trans("Parameters").'</td>'."\n";
184 print '<td align="center">'.$langs->trans("Value").'</td>'."\n";
185 print '</tr>';
186 
187 print '<tr class="oddeven">';
188 print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
189 print '<td align="right">';
190 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
191 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
192 print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE">';
193 print $form->selectyesno("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE",$conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE,1);
194 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
195 print '</form>';
196 print '</td></tr>';
197 
198 
199 print '<tr class="oddeven">';
200 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
201 print '<td align="right">';
202 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
203 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
204 print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_ORIGIN_TX">';
205 print $form->selectyesno("MULTICURRENCY_USE_ORIGIN_TX",$conf->global->MULTICURRENCY_USE_ORIGIN_TX,1);
206 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
207 print '</form>';
208 print '</td></tr>';
209 
210 /* TODO uncomment when the functionality will integrated
211 
212 print '<tr class="oddeven">';
213 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
214 print '<td align="right">';
215 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
216 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
217 print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
218 print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
219 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
220 print '</form>';
221 print '</td></tr>';
222 */
223 
224 /* TODO uncomment when the functionality will integrated
225 
226 print '<tr class="oddeven">';
227 print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
228 print '<td align="right">';
229 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
230 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
231 print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
232 print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
233 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
234 print '</form>';
235 print '</td></tr>';
236 
237 */
238 
239 print '</table>';
240 
241 print '<br>';
242 
243 if (!empty($conf->global->MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION))
244 {
245  print '<div class="div-table-responsive-no-min">';
246  print '<table class="noborder" width="100%">';
247 
248  print '<tr class="liste_titre">';
249  print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize")).'</td>'."\n";
250  print '<td align="right">';
251  print '<form id="form_sync" action="" method="POST">';
252  print '<input type="hidden" name="action" value="synchronize" />';
253  print '<textarea id="response" class="hideobject" name="response"></textarea>';
254  print $langs->trans("Value").'&nbsp;<input type="button" id="bt_sync" class="button" onclick="javascript:getRates();" value="'.$langs->trans('Synchronize').'" />';
255  print '</form>';
256  print '</td></tr>';
257 
258 
259  print '<tr class="oddeven">';
260  print '<td><a target="_blank" href="https://currencylayer.com">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
261  print '<td align="right">';
262  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
263  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
264  print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_ID">';
265  print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
266  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
267  print '</form>';
268  print '</td></tr>';
269 
270 
271  print '<tr class="oddeven">';
272  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
273  print '<td align="right">';
274  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
275  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
276  print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_SOURCE">';
277  print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
278  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
279  print '</form>';
280  print '</td></tr>';
281 
282  print '<tr class="oddeven">';
283  print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
284  print '<td align="right">';
285  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
286  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
287  print '<input type="hidden" name="action" value="set_MULTICURRENCY_ALTERNATE_SOURCE">';
288  print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
289  print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
290  print '</form>';
291  print '</td></tr>';
292 
293  print '</table>';
294  print '</div>';
295  print '<br>';
296 }
297 
298 
299 print '<div class="div-table-responsive-no-min">';
300 print '<table class="noborder" width="100%">';
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 align="center">'.$langs->trans("Rate").'</td>'."\n";
305 print '</tr>';
306 
307 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
308 print '<tr class="oddeven">';
309 print '<td>'.$form->selectCurrency('', 'code').'</td>';
310 print '<td align="right">';
311 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
312 print '<input type="hidden" name="action" value="add_currency">';
313 print '<input type="text" name="rate" value="" size="13" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
314 print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
315 print '</td>';
316 print '</tr>';
317 print '</form>';
318 
319 print '<tr class="oddeven">';
320 print '<td>'.$conf->currency.$form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>';
321 print '<td align="right">1</td>';
322 print '</tr>';
323 
324 foreach ($TCurrency as &$currency)
325 {
326  if ($currency->code == $conf->currency) continue;
327 
328  print '<tr class="oddeven">';
329  print '<td>'.$currency->code.' - '.$currency->name.'</td>';
330  print '<td align="right">';
331  print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
332  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
333  print '<input type="hidden" name="action" value="update_currency">';
334  print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
335  print '1 '.$conf->currency.' = ';
336  print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" />&nbsp;'.$currency->code.'&nbsp;';
337  print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Modify").'">&nbsp;';
338  print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Delete").'">';
339  print '</form>';
340  print '</td></tr>';
341 }
342 
343 print '</table>';
344 print '</div>';
345 
346 print '
347  <script type="text/javascript">
348  function getRates()
349  {
350  $("#bt_sync").attr("disabled", true);
351  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 : '').'";
352 
353  $.ajax({
354  url: url_sync,
355  dataType: "jsonp"
356  }).done(function(response) {
357  $("#response").val(JSON.stringify(response));
358  $("#form_sync").submit();
359  });
360  }
361  </script>
362 ';
363 
364 // End of page
365 llxFooter();
366 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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:1053
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:510
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
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&#39;une constante dans la base de donnees.
Definition: admin.lib.php:435
llxHeader()
Empty header.
Definition: wrapper.php:44
multicurrencyAdminPrepareHead()
Prepare array with list of tabs.
Class Currency.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.