multicurrency.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /**
  19. * \file admin/multicurrency.php
  20. * \ingroup multicurrency
  21. * \brief This file is an example module setup page
  22. * Put some comments here
  23. */
  24. // Dolibarr environment
  25. require '../main.inc.php';
  26. // Libraries
  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. // Translations
  31. $langs->load("admin");
  32. $langs->load("multicurrency");
  33. // Access control
  34. if (! $user->admin) {
  35. accessforbidden();
  36. }
  37. // Parameters
  38. $action = GETPOST('action', 'alpha');
  39. /*
  40. * Actions
  41. */
  42. if (preg_match('/set_(.*)/',$action,$reg))
  43. {
  44. $code=$reg[1];
  45. if (dolibarr_set_const($db, $code, GETPOST($code), 'chaine', 0, '', $conf->entity) > 0)
  46. {
  47. header("Location: ".$_SERVER["PHP_SELF"]);
  48. exit;
  49. }
  50. else
  51. {
  52. dol_print_error($db);
  53. }
  54. }
  55. if (preg_match('/del_(.*)/',$action,$reg))
  56. {
  57. $code=$reg[1];
  58. if (dolibarr_del_const($db, $code, 0) > 0)
  59. {
  60. header("Location: ".$_SERVER["PHP_SELF"]);
  61. exit;
  62. }
  63. else
  64. {
  65. dol_print_error($db);
  66. }
  67. }
  68. if ($action == 'add_currency')
  69. {
  70. $langs->loadCacheCurrencies('');
  71. $code = GETPOST('code', 'alpha');
  72. $rate = GETPOST('rate', 'alpha');
  73. $currency = new MultiCurrency($db);
  74. $currency->code = $code;
  75. $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
  76. if ($currency->create($user) > 0)
  77. {
  78. if ($currency->addRate($rate)) setEventMessages($langs->trans('RecordSaved'), array());
  79. else setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
  80. }
  81. else setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
  82. }
  83. elseif ($action == 'update_currency')
  84. {
  85. $submit = GETPOST('submit', 'alpha');
  86. if ($submit == $langs->trans('Modify'))
  87. {
  88. $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
  89. $rate = GETPOST('rate', 'float');
  90. $currency = new MultiCurrency($db);
  91. if ($currency->fetch($fk_multicurrency) > 0)
  92. {
  93. $currency->updateRate($rate);
  94. }
  95. }
  96. elseif ($submit == $langs->trans('Delete'))
  97. {
  98. $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
  99. $currency = new MultiCurrency($db);
  100. if ($currency->fetch($fk_multicurrency) > 0)
  101. {
  102. if ($currency->delete() > 0) setEventMessages($langs->trans('RecordDeleted'), array());
  103. else setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
  104. }
  105. }
  106. }
  107. elseif ($action == 'synchronize')
  108. {
  109. $response = GETPOST('response');
  110. $response = json_decode($response);
  111. if ($response->success)
  112. {
  113. MultiCurrency::syncRates($response);
  114. }
  115. else
  116. {
  117. setEventMessages($langs->trans('multicurrency_syncronize_error', $response->error->info), null, 'errors');
  118. }
  119. }
  120. $TCurrency = array();
  121. $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
  122. $resql = $db->query($sql);
  123. if ($resql)
  124. {
  125. while ($obj = $db->fetch_object($resql))
  126. {
  127. $currency = new MultiCurrency($db);
  128. $currency->fetch($obj->rowid);
  129. $TCurrency[] = $currency;
  130. }
  131. }
  132. /*
  133. * View
  134. */
  135. $page_name = "MultiCurrencySetup";
  136. llxHeader('', $langs->trans($page_name));
  137. // Subheader
  138. $linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php">'
  139. . $langs->trans("BackToModuleList") . '</a>';
  140. print_fiche_titre($langs->trans($page_name), $linkback);
  141. // Configuration header
  142. $head = multicurrencyAdminPrepareHead();
  143. dol_fiche_head(
  144. $head,
  145. 'settings',
  146. $langs->trans("ModuleSetup"),
  147. 0,
  148. "multicurrency"
  149. );
  150. // Setup page goes here
  151. $form=new Form($db);
  152. $var=false;
  153. print '<table class="noborder" width="100%">';
  154. print '<tr class="liste_titre">';
  155. print '<td>'.$langs->trans("Parameters").'</td>'."\n";
  156. print '<td align="center" width="20">&nbsp;</td>';
  157. print '<td align="center" width="100">'.$langs->trans("Value").'</td>'."\n";
  158. print '<tr class="oddeven">';
  159. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useRateOnDocumentDate").'</td>';
  160. print '<td align="center" width="20">&nbsp;</td>';
  161. print '<td align="right" width="400">';
  162. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  163. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  164. print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE">';
  165. print $form->selectyesno("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE",$conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE,1);
  166. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  167. print '</form>';
  168. print '</td></tr>';
  169. print '<tr class="oddeven">';
  170. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
  171. print '<td align="center" width="20">&nbsp;</td>';
  172. print '<td align="right" width="400">';
  173. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  174. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  175. print '<input type="hidden" name="action" value="set_MULTICURRENCY_USE_ORIGIN_TX">';
  176. print $form->selectyesno("MULTICURRENCY_USE_ORIGIN_TX",$conf->global->MULTICURRENCY_USE_ORIGIN_TX,1);
  177. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  178. print '</form>';
  179. print '</td></tr>';
  180. /* TODO uncomment when the functionality will integrated
  181. print '<tr class="oddeven">';
  182. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
  183. print '<td align="center" width="20">&nbsp;</td>';
  184. print '<td align="right" width="400">';
  185. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  186. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  187. print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
  188. print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
  189. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  190. print '</form>';
  191. print '</td></tr>';
  192. */
  193. /* TODO uncomment when the functionality will integrated
  194. print '<tr class="oddeven">';
  195. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
  196. print '<td align="center" width="20">&nbsp;</td>';
  197. print '<td align="right" width="400">';
  198. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  199. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  200. print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
  201. print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
  202. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  203. print '</form>';
  204. print '</td></tr>';
  205. */
  206. print '</table>';
  207. print '<br />';
  208. if (!empty($conf->global->MAIN_MULTICURRENCY_ALLOW_SYNCHRONIZATION))
  209. {
  210. $var=false;
  211. print '<table class="noborder" width="100%">';
  212. print '<tr class="liste_titre">';
  213. print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize")).'</td>'."\n";
  214. print '<td align="center" width="20">&nbsp;</td>';
  215. print '<td align="right" width="100">';
  216. print '<form id="form_sync" action="" method="POST">';
  217. print '<input type="hidden" name="action" value="synchronize" />';
  218. print '<textarea id="response" class="hideobject" name="response"></textarea>';
  219. print $langs->trans("Value").'&nbsp;<input type="button" id="bt_sync" class="button" onclick="javascript:getRates();" value="'.$langs->trans('Synchronize').'" />';
  220. print '</form>';
  221. print '</td></tr>';
  222. print '<tr class="oddeven">';
  223. print '<td><a target="_blank" href="https://currencylayer.com">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
  224. print '<td align="center" width="20">&nbsp;</td>';
  225. print '<td align="right" width="400">';
  226. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  227. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  228. print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_ID">';
  229. print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
  230. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  231. print '</form>';
  232. print '</td></tr>';
  233. print '<tr class="oddeven">';
  234. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
  235. print '<td align="center" width="20">&nbsp;</td>';
  236. print '<td align="right" width="400">';
  237. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  238. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  239. print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_SOURCE">';
  240. print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
  241. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  242. print '</form>';
  243. print '</td></tr>';
  244. print '<tr class="oddeven">';
  245. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
  246. print '<td align="center" width="20">&nbsp;</td>';
  247. print '<td align="right" width="400">';
  248. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  249. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  250. print '<input type="hidden" name="action" value="set_MULTICURRENCY_ALTERNATE_SOURCE">';
  251. print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
  252. print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
  253. print '</form>';
  254. print '</td></tr>';
  255. print '</table>';
  256. print '<br />';
  257. }
  258. print '<table class="noborder" width="100%">';
  259. print '<tr class="liste_titre">';
  260. print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
  261. print '<td align="center" width="20">&nbsp;</td>';
  262. print '<td align="center" width="100">'.$langs->trans("Rate").'</td>'."\n";
  263. print '<tr class="oddeven">';
  264. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  265. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  266. print '<input type="hidden" name="action" value="add_currency">';
  267. print '<td>'.$form->selectCurrency('', 'code').'</td>';
  268. print '<td align="center" width="20">&nbsp;</td>';
  269. print '<td align="right" width="300">';
  270. print '<input type="text" name="rate" value="" size="13" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
  271. print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
  272. print '</td></form></tr>';
  273. print '<tr class="oddeven">';
  274. print '<td>'.$conf->currency.$form->textwithpicto(' ', $langs->trans("BaseCurrency")).'</td>';
  275. print '<td align="center" width="20">&nbsp;</td>';
  276. print '<td align="right" width="300">1';
  277. print '</td></form></tr>';
  278. foreach ($TCurrency as &$currency)
  279. {
  280. if($currency->code == $conf->currency) continue;
  281. print '<tr class="oddeven">';
  282. print '<td>'.$currency->code.' - '.$currency->name.'</td>';
  283. print '<td align="center" width="20">&nbsp;</td>';
  284. print '<td align="right" width="400">';
  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="update_currency">';
  288. print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
  289. print '1 '.$conf->currency.' = ';
  290. print '<input type="text" name="rate" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13" />&nbsp;'.$currency->code.'&nbsp;';
  291. print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Modify").'">&nbsp;';
  292. print '<input type="submit" name="submit" class="button" value="'.$langs->trans("Delete").'">';
  293. print '</form>';
  294. print '</td></tr>';
  295. }
  296. print '</table>';
  297. print '
  298. <script type="text/javascript">
  299. function getRates()
  300. {
  301. $("#bt_sync").attr("disabled", true);
  302. 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 : '').'";
  303. $.ajax({
  304. url: url_sync,
  305. dataType: "jsonp"
  306. }).done(function(response) {
  307. $("#response").val(JSON.stringify(response));
  308. $("#form_sync").submit();
  309. });
  310. }
  311. </script>
  312. ';
  313. llxFooter();
  314. $db->close();