multicurrency.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. /**
  19. * \file admin/multicurrency.php
  20. * \ingroup multicurrency
  21. * \brief Page to setup multicurrency module
  22. */
  23. // Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('admin', 'multicurrency'));
  30. // Access control
  31. if (!$user->admin || !isModEnabled('multicurrency')) {
  32. accessforbidden();
  33. }
  34. // Parameters
  35. $action = GETPOST('action', 'aZ09');
  36. /*
  37. * Actions
  38. */
  39. $reg = array();
  40. if (preg_match('/set_([a-z0-9_\-]+)/i', $action, $reg)) {
  41. $code = $reg[1];
  42. $value = GETPOST($code, 'alpha');
  43. if (dolibarr_set_const($db, $code, $value, 'chaine', 0, '', $conf->entity) > 0) {
  44. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  45. } else {
  46. setEventMessages($langs->trans("Error"), null, 'errors');
  47. }
  48. }
  49. if (preg_match('/del_([a-z0-9_\-]+)/i', $action, $reg)) {
  50. $code = $reg[1];
  51. if (dolibarr_del_const($db, $code, 0) > 0) {
  52. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  53. } else {
  54. setEventMessages($langs->trans("Error"), null, 'errors');
  55. }
  56. }
  57. if ($action == 'add_currency') {
  58. $error = 0;
  59. $langs->loadCacheCurrencies('');
  60. $code = GETPOST('code', 'alpha');
  61. $rate = price2num(GETPOST('rate', 'alpha'));
  62. $currency = new MultiCurrency($db);
  63. $currency->code = $code;
  64. $currency->name = !empty($langs->cache_currencies[$code]['label']) ? $langs->cache_currencies[$code]['label'].' ('.$langs->getCurrencySymbol($code).')' : $code;
  65. if (empty($currency->code) || $currency->code == '-1') {
  66. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Currency")), null, 'errors');
  67. $error++;
  68. }
  69. if (empty($rate)) {
  70. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
  71. $error++;
  72. }
  73. if (!$error) {
  74. if ($currency->create($user) > 0) {
  75. if ($currency->addRate($rate)) {
  76. setEventMessages($langs->trans('RecordSaved'), array());
  77. } else {
  78. setEventMessages($langs->trans('ErrorAddRateFail'), array(), 'errors');
  79. }
  80. } else {
  81. setEventMessages($langs->trans('ErrorAddCurrencyFail'), $currency->errors, 'errors');
  82. }
  83. }
  84. } elseif ($action == 'update_currency') {
  85. $error = 0;
  86. if (GETPOST('updatecurrency', 'alpha')) {
  87. $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
  88. $rate = price2num(GETPOST('rate', 'alpha'));
  89. $currency = new MultiCurrency($db);
  90. if (empty($rate)) {
  91. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Rate")), null, 'errors');
  92. $error++;
  93. }
  94. if (!$error) {
  95. if ($currency->fetch($fk_multicurrency) > 0) {
  96. $result = $currency->updateRate($rate);
  97. if ($result < 0) {
  98. setEventMessages(null, $currency->errors, 'errors');
  99. }
  100. }
  101. }
  102. } elseif (GETPOST('deletecurrency', 'alpha')) {
  103. $fk_multicurrency = GETPOST('fk_multicurrency', 'int');
  104. $currency = new MultiCurrency($db);
  105. if ($currency->fetch($fk_multicurrency) > 0) {
  106. if ($currency->delete() > 0) {
  107. setEventMessages($langs->trans('RecordDeleted'), array());
  108. } else {
  109. setEventMessages($langs->trans('ErrorDeleteCurrencyFail'), array(), 'errors');
  110. }
  111. }
  112. }
  113. } elseif ($action == 'setapilayer') {
  114. if (GETPOSTISSET('modify_apilayer')) {
  115. dolibarr_set_const($db, 'MULTICURRENCY_APP_ID', GETPOST('MULTICURRENCY_APP_ID', 'alpha'));
  116. dolibarr_set_const($db, 'MULTICURRENCY_APP_SOURCE', GETPOST('MULTICURRENCY_APP_SOURCE', 'alpha'));
  117. //dolibarr_set_const($db, 'MULTICURRENCY_ALTERNATE_SOURCE', GETPOST('MULTICURRENCY_ALTERNATE_SOURCE', 'alpha'));
  118. } else {
  119. $multiurrency = new MultiCurrency($db);
  120. $result = $multiurrency->syncRates(getDolGlobalString('MULTICURRENCY_APP_ID'));
  121. if ($result > 0) {
  122. setEventMessages($langs->trans("CurrencyRateSyncSucceed"), null, "mesgs");
  123. }
  124. }
  125. }
  126. $TAvailableCurrency = array();
  127. $sql = "SELECT code_iso, label, unicode, active FROM ".MAIN_DB_PREFIX."c_currencies";
  128. $resql = $db->query($sql);
  129. if ($resql) {
  130. while ($obj = $db->fetch_object($resql)) {
  131. $TAvailableCurrency[$obj->code_iso] = array('code'=>$obj->code_iso, 'active'=>$obj->active);
  132. }
  133. }
  134. $TCurrency = array();
  135. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE entity = ".((int) $conf->entity);
  136. $resql = $db->query($sql);
  137. if ($resql) {
  138. while ($obj = $db->fetch_object($resql)) {
  139. $currency = new MultiCurrency($db);
  140. $currency->fetch($obj->rowid);
  141. $TCurrency[] = $currency;
  142. }
  143. }
  144. /*
  145. * View
  146. */
  147. $form = new Form($db);
  148. $page_name = "MultiCurrencySetup";
  149. $help_url = '';
  150. llxHeader('', $langs->trans($page_name), $help_url);
  151. // Subheader
  152. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  153. print load_fiche_titre($langs->trans($page_name), $linkback);
  154. // Configuration header
  155. $head = multicurrencyAdminPrepareHead();
  156. print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "multicurrency");
  157. print '<div class="div-table-responsive-no-min">';
  158. print '<table class="noborder centpercent">';
  159. print '<tr class="liste_titre">';
  160. print '<td>'.$langs->trans("Parameters").'</td>'."\n";
  161. print '<td class="center">'.$langs->trans("Status").'</td>'."\n";
  162. print '</tr>';
  163. print '<tr class="oddeven">';
  164. print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE").'</td>';
  165. print '<td class="center">';
  166. if ($conf->use_javascript_ajax) {
  167. print ajax_constantonoff('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE');
  168. } else {
  169. $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
  170. print $form->selectarray("MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE", $arrval, $conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE);
  171. }
  172. print '</td></tr>';
  173. print '<tr class="oddeven">';
  174. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_useOriginTx").'</td>';
  175. print '<td class="center">';
  176. if ($conf->use_javascript_ajax) {
  177. print ajax_constantonoff('MULTICURRENCY_USE_ORIGIN_TX', null, null, 0, 0, 0, 2, 0, 1);
  178. } else {
  179. $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
  180. print $form->selectarray("MULTICURRENCY_USE_ORIGIN_TX", $arrval, $conf->global->MULTICURRENCY_USE_ORIGIN_TX);
  181. }
  182. print '</td></tr>';
  183. // Online payment with currency on document. This option should be on by default.
  184. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) {
  185. print '<tr class="oddeven">';
  186. print '<td>'.$langs->transnoentitiesnoconv("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT").'</td>';
  187. print '<td class="center">';
  188. if ($conf->use_javascript_ajax) {
  189. print ajax_constantonoff('MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT');
  190. } else {
  191. $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
  192. print $form->selectarray("MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT", $arrval, $conf->global->MULTICURRENCY_USE_CURRENCY_ON_DOCUMENT);
  193. }
  194. print '</td></tr>';
  195. }
  196. /* TODO uncomment when the functionality will integrated
  197. print '<tr class="oddeven">';
  198. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_buyPriceInCurrency").'</td>';
  199. print '<td class="right">';
  200. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  201. print '<input type="hidden" name="token" value="'.newToken().'">';
  202. print '<input type="hidden" name="action" value="set_MULTICURRENCY_BUY_PRICE_IN_CURRENCY">';
  203. print $form->selectyesno("MULTICURRENCY_BUY_PRICE_IN_CURRENCY",$conf->global->MULTICURRENCY_BUY_PRICE_IN_CURRENCY,1);
  204. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  205. print '</form>';
  206. print '</td></tr>';
  207. */
  208. /* TODO uncomment when the functionality will integrated
  209. print '<tr class="oddeven">';
  210. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_modifyRateApplication").'</td>';
  211. print '<td class="right">';
  212. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  213. print '<input type="hidden" name="token" value="'.newToken().'">';
  214. print '<input type="hidden" name="action" value="set_MULTICURRENCY_MODIFY_RATE_APPLICATION">';
  215. print $form->selectarray('MULTICURRENCY_MODIFY_RATE_APPLICATION', array('PU_DOLIBARR' => 'PU_DOLIBARR', 'PU_CURRENCY' => 'PU_CURRENCY'), $conf->global->MULTICURRENCY_MODIFY_RATE_APPLICATION);
  216. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  217. print '</form>';
  218. print '</td></tr>';
  219. */
  220. print '</table>';
  221. print '</div>';
  222. print '<div class="div-table-responsive-no-min">';
  223. print '<table class="noborder centpercent nomarginbottom">';
  224. print '<tr class="liste_titre">';
  225. print '<td>'.$form->textwithpicto($langs->trans("CurrenciesUsed"), $langs->transnoentitiesnoconv("CurrenciesUsed_help_to_add")).'</td>'."\n";
  226. print '<td class="right">'.$langs->trans("Rate").' / '.$langs->getCurrencySymbol($conf->currency).'</td>'."\n";
  227. print '</tr>';
  228. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  229. print '<input type="hidden" name="token" value="'.newToken().'">';
  230. print '<input type="hidden" name="action" value="add_currency">';
  231. print '<tr class="oddeven">';
  232. print '<td>'.$form->selectCurrency('', 'code', 1, '1').'</td>';
  233. print '<td class="right">';
  234. print '<input type="text" name="rate" value="" class="width75 right" placeholder="'.$langs->trans('Rate').'" />&nbsp;';
  235. print '<input type="submit" class="button button-add smallpaddingimp" value="'.$langs->trans("Add").'">';
  236. print '</td>';
  237. print '</tr>';
  238. print '</form>';
  239. // Main currency
  240. print '<tr class="oddeven">';
  241. print '<td>'.$conf->currency;
  242. print ' ('.$langs->getCurrencySymbol($conf->currency).')';
  243. print $form->textwithpicto(' ', $langs->trans("BaseCurrency"));
  244. if (!empty($TAvailableCurrency[$conf->currency]) && empty($TAvailableCurrency[$conf->currency]['active'])) {
  245. print img_warning('Warning: This code has been disabled into Home - Setup - Dictionaries - Currencies');
  246. }
  247. print '</td>';
  248. print '<td class="right">1</td>';
  249. print '</tr>';
  250. foreach ($TCurrency as &$currency) {
  251. if ($currency->code == $conf->currency) {
  252. continue;
  253. }
  254. print '<tr class="oddeven">';
  255. print '<td>'.$currency->code.' - '.$currency->name;
  256. if (!empty($TAvailableCurrency[$currency->code]) && empty($TAvailableCurrency[$currency->code]['active'])) {
  257. print img_warning('Warning: The code '.$currency->code.' has been disabled into Home - Setup - Dictionaries - Currencies');
  258. }
  259. print '</td>';
  260. print '<td class="right">';
  261. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  262. print '<input type="hidden" name="token" value="'.newToken().'">';
  263. print '<input type="hidden" name="action" value="update_currency">';
  264. print '<input type="hidden" name="fk_multicurrency" value="'.$currency->id.'">';
  265. print '1 '.$conf->currency.' = ';
  266. print '<input type="text" name="rate" class="width75 right" value="'.($currency->rate->rate ? $currency->rate->rate : '').'" size="13">&nbsp;'.$currency->code.'&nbsp;';
  267. print '<input type="submit" name="updatecurrency" class="button button-edit smallpaddingimp" value="'.$langs->trans("Modify").'">&nbsp;';
  268. print '<input type="submit" name="deletecurrency" class="button smallpaddingimp" value="'.$langs->trans("Delete").'">';
  269. print '</form>';
  270. print '</td></tr>';
  271. }
  272. print '</table>';
  273. print '</div>';
  274. print '
  275. <script type="text/javascript">
  276. function getRates()
  277. {
  278. $("#bt_sync").attr("disabled", true);
  279. return true;
  280. }
  281. </script>
  282. ';
  283. print '<br>';
  284. if (!getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) {
  285. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="form_sync">';
  286. print '<input type="hidden" name="token" value="'.newToken().'">';
  287. print '<input type="hidden" name="action" value="setapilayer">';
  288. print '<div class="div-table-responsive-no-min">';
  289. print '<table class="noborder centpercent">';
  290. $urlforapilayer = 'https://currencylayer.com'; //https://apilayer.net
  291. print '<tr class="liste_titre">';
  292. print '<td>'.$form->textwithpicto($langs->trans("CurrencyLayerAccount"), $langs->trans("CurrencyLayerAccount_help_to_synchronize", $urlforapilayer)).'</td>'."\n";
  293. print '<td class="right">';
  294. print '<textarea id="response" class="hideobject" name="response"></textarea>';
  295. print '<input type="submit" name="modify_apilayer" class="button buttongen" value="'.$langs->trans("Modify").'">';
  296. print '<input type="submit" id="bt_sync" name="bt_sync_apilayer" class="button buttongen" value="'.$langs->trans('Synchronize').'" />';
  297. print '</td></tr>';
  298. print '<tr class="oddeven">';
  299. print '<td class="fieldrequired"><a target="_blank" rel="noopener noreferrer external" href="'.$urlforapilayer.'">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
  300. print '<td class="right">';
  301. print '<input type="text" name="MULTICURRENCY_APP_ID" value="' . getDolGlobalString('MULTICURRENCY_APP_ID').'" size="28" />&nbsp;';
  302. print '</td></tr>';
  303. print '<tr class="oddeven">';
  304. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
  305. print '<td class="right">';
  306. print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="' . getDolGlobalString('MULTICURRENCY_APP_SOURCE').'" size="10" placeholder="USD" />&nbsp;'; // Default: USD
  307. print '</td></tr>';
  308. /*print '<tr class="oddeven">';
  309. print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
  310. print '<td class="right">';
  311. print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" />&nbsp;'; // Example: EUR
  312. print '</td></tr>';*/
  313. print '</table>';
  314. print '</div>';
  315. print '<br>';
  316. print '</form>';
  317. }
  318. // End of page
  319. llxFooter();
  320. $db->close();