transfer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  7. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  8. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/compta/bank/transfer.php
  25. * \ingroup bank
  26. * \brief Page for entering a bank transfer
  27. */
  28. // Load Dolibarr environment
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('banks', 'categories', 'multicurrency'));
  34. $socid = 0;
  35. if ($user->socid > 0) {
  36. $socid = $user->socid;
  37. }
  38. if (!$user->rights->banque->transfer) {
  39. accessforbidden();
  40. }
  41. $action = GETPOST('action', 'aZ09');
  42. $error = 0;
  43. $hookmanager->initHooks(array('banktransfer'));
  44. /*
  45. * Actions
  46. */
  47. $parameters = array('socid' => $socid);
  48. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  49. if ($reshook < 0) {
  50. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  51. }
  52. if ($action == 'add') {
  53. $langs->load("errors");
  54. $dateo = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  55. $label = GETPOST('label', 'alpha');
  56. $amount = price2num(GETPOST('amount', 'alpha'), 'MT', 2);
  57. $amountto = price2num(GETPOST('amountto', 'alpha'), 'MT', 2);
  58. if (!$label) {
  59. $error++;
  60. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")), null, 'errors');
  61. }
  62. if (!$amount) {
  63. $error++;
  64. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
  65. }
  66. if (!GETPOST('account_from', 'int')) {
  67. $error++;
  68. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferFrom")), null, 'errors');
  69. }
  70. if (!GETPOST('account_to', 'int')) {
  71. $error++;
  72. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferTo")), null, 'errors');
  73. }
  74. if (!$error) {
  75. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  76. $accountfrom = new Account($db);
  77. $accountfrom->fetch(GETPOST('account_from', 'int'));
  78. $accountto = new Account($db);
  79. $accountto->fetch(GETPOST('account_to', 'int'));
  80. if ($accountto->currency_code == $accountfrom->currency_code) {
  81. $amountto = $amount;
  82. } else {
  83. if (!$amountto) {
  84. $error++;
  85. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountTo")), null, 'errors');
  86. }
  87. }
  88. if ($amountto < 0) {
  89. $error++;
  90. setEventMessages($langs->trans("AmountMustBePositive"), null, 'errors');
  91. }
  92. if ($accountto->id == $accountfrom->id) {
  93. $error++;
  94. setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers"), null, 'errors');
  95. }
  96. if (empty($error)) {
  97. $db->begin();
  98. $bank_line_id_from = 0;
  99. $bank_line_id_to = 0;
  100. $result = 0;
  101. // By default, electronic transfert from bank to bank
  102. $typefrom = 'PRE';
  103. $typeto = 'VIR';
  104. if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH) {
  105. // This is transfer of change
  106. $typefrom = 'LIQ';
  107. $typeto = 'LIQ';
  108. }
  109. if (!$error) {
  110. $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, price2num(-1 * $amount), '', '', $user);
  111. }
  112. if (!($bank_line_id_from > 0)) {
  113. $error++;
  114. }
  115. if (!$error) {
  116. $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, $amountto, '', '', $user);
  117. }
  118. if (!($bank_line_id_to > 0)) {
  119. $error++;
  120. }
  121. if (!$error) {
  122. $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
  123. }
  124. if (!($result > 0)) {
  125. $error++;
  126. }
  127. if (!$error) {
  128. $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
  129. }
  130. if (!($result > 0)) {
  131. $error++;
  132. }
  133. if (!$error) {
  134. $mesgs = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount, $langs->transnoentitiesnoconv("Currency".$conf->currency));
  135. $mesgs = str_replace('{s1}', '<a href="bankentries_list.php?id='.$accountfrom->id.'&sortfield=b.datev,b.dateo,b.rowid&sortorder=desc">'.$accountfrom->label.'</a>', $mesgs);
  136. $mesgs = str_replace('{s2}', '<a href="bankentries_list.php?id='.$accountto->id.'">'.$accountto->label.'</a>', $mesgs);
  137. setEventMessages($mesgs, null, 'mesgs');
  138. $db->commit();
  139. } else {
  140. setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors');
  141. $db->rollback();
  142. }
  143. }
  144. }
  145. }
  146. /*
  147. * View
  148. */
  149. $form = new Form($db);
  150. $help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
  151. $title = $langs->trans('MenuBankInternalTransfer');
  152. llxHeader('', $title, $help_url);
  153. print ' <script type="text/javascript">
  154. $(document).ready(function () {
  155. $(".selectbankaccount").change(function() {
  156. console.log("We change bank account");
  157. init_page();
  158. });
  159. function init_page() {
  160. console.log("Set fields according to currency");
  161. var account1 = $("#selectaccount_from").val();
  162. var account2 = $("#selectaccount_to").val();
  163. var currencycode1="";
  164. var currencycode2="";
  165. $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account1})
  166. .done(function( data ) {
  167. if (data != null)
  168. {
  169. var item= $.parseJSON(data);
  170. if (item.num==-1) {
  171. console.error("Error: "+item.error);
  172. } else if (item.num!==0) {
  173. currencycode1 = item.value;
  174. }
  175. $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account2})
  176. .done(function( data ) {
  177. if (data != null)
  178. {
  179. var item=$.parseJSON(data);
  180. if (item.num==-1) {
  181. console.error("Error: "+item.error);
  182. } else if (item.num!==0) {
  183. currencycode2 = item.value;
  184. }
  185. if (currencycode2!==currencycode1 && currencycode2!=="" && currencycode1!=="") {
  186. $(".multicurrency").show();
  187. } else {
  188. $(".multicurrency").hide();
  189. }
  190. }
  191. else {
  192. console.error("Error: Ajax url has returned an empty page. Should be an empty json array.");
  193. }
  194. }).fail(function( data ) {
  195. console.error("Error: has returned an empty page. Should be an empty json array.");
  196. });
  197. }
  198. else {
  199. console.error("Error: has returned an empty page. Should be an empty json array.");
  200. }
  201. }).fail(function( data ) {
  202. console.error("Error: has returned an empty page. Should be an empty json array.");
  203. });
  204. }
  205. init_page();
  206. });
  207. </script>';
  208. $account_from = '';
  209. $account_to = '';
  210. $label = '';
  211. $amount = '';
  212. $amountto = '';
  213. if ($error) {
  214. $account_from = GETPOST('account_from', 'int');
  215. $account_to = GETPOST('account_to', 'int');
  216. $label = GETPOST('label', 'alpha');
  217. $amount = GETPOST('amount', 'alpha');
  218. }
  219. print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
  220. print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
  221. print "<br><br>";
  222. print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
  223. print '<input type="hidden" name="token" value="'.newToken().'">';
  224. print '<input type="hidden" name="action" value="add">';
  225. print '<div class="div-table-responsive-no-min">';
  226. print '<table class="noborder centpercent">';
  227. print '<tr class="liste_titre">';
  228. print '<th>'.$langs->trans("TransferFrom").'</th>';
  229. print '<th>'.$langs->trans("TransferTo").'</th>';
  230. print '<th>'.$langs->trans("Date").'</th>';
  231. print '<th>'.$langs->trans("Description").'</th>';
  232. print '<th class="right">'.$langs->trans("Amount").'</th>';
  233. //print '<td class="hideobject" class="multicurrency">'.$langs->trans("AmountToOthercurrency").'</td>';
  234. print '</tr>';
  235. print '<tr class="oddeven">';
  236. print '<td class="nowraponall">';
  237. print img_picto('', 'bank_account', 'class="paddingright"');
  238. $form->select_comptes($account_from, 'account_from', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1, 'minwidth100');
  239. print "</td>";
  240. print '<td class="nowraponall">';
  241. print img_picto('', 'bank_account', 'class="paddingright"');
  242. $form->select_comptes($account_to, 'account_to', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1, 'minwidth100');
  243. print "</td>\n";
  244. print '<td class="nowraponall">';
  245. print $form->selectDate((!empty($dateo) ? $dateo : ''), '', '', '', '', 'add');
  246. print "</td>";
  247. print '<td><input name="label" class="flat quatrevingtpercent" type="text" value="'.dol_escape_htmltag($label).'"></td>';
  248. print '<td class="right"><input name="amount" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
  249. //print '<td class="hideobject" class="multicurrency"><input name="amountto" class="flat" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
  250. print "</table>";
  251. print '</div>';
  252. print '<br><div class="center"><input type="submit" class="button" value="'.$langs->trans("Create").'"></div>';
  253. print "</form>";
  254. // End of page
  255. llxFooter();
  256. $db->close();