dolibarr  20.0.0-alpha
transfer.php
Go to the documentation of this file.
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-2024 Frédéric France <frederic.france@free.fr>
9  * Copyright (C) 2023 Maxime Nicolas <maxime@oarces.com>
10  * Copyright (C) 2023 Benjamin GREMBI <benjamin@oarces.com>
11  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
33 // Load Dolibarr environment
34 require '../../main.inc.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
37 
38 // Load translation files required by the page
39 $langs->loadLangs(array('banks', 'categories', 'multicurrency'));
40 
41 $action = GETPOST('action', 'aZ09');
42 
43 $hookmanager->initHooks(array('banktransfer'));
44 
45 $socid = 0;
46 if ($user->socid > 0) {
47  $socid = $user->socid;
48 }
49 if (!$user->hasRight('banque', 'transfer')) {
51 }
52 
53 $MAXLINES = 10;
54 
55 $error = 0;
56 
57 
58 /*
59  * Actions
60  */
61 
62 $parameters = array('socid' => $socid);
63 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
64 if ($reshook < 0) {
65  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
66 }
67 if ($action == 'add' && $user->hasRight('banque', 'transfer')) {
68  $langs->load('errors');
69  $i = 1;
70 
71  $dateo = array();
72  $label = array();
73  $amount = array();
74  $amountto = array();
75  $accountfrom = array();
76  $accountto = array();
77  $type = array();
78  $tabnum = array();
79  $maxtab = 1;
80 
81  while ($i < $MAXLINES) {
82  $dateo[$i] = dol_mktime(12, 0, 0, GETPOSTINT($i.'_month'), GETPOSTINT($i.'_day'), GETPOSTINT($i.'_year'));
83  $label[$i] = GETPOST($i.'_label', 'alpha');
84  $amount[$i] = price2num(GETPOST($i.'_amount', 'alpha'), 'MT', 2);
85  $amountto[$i] = price2num(GETPOST($i.'_amountto', 'alpha'), 'MT', 2);
86  $accountfrom[$i] = GETPOSTINT($i.'_account_from');
87  $accountto[$i] = GETPOSTINT($i.'_account_to');
88  $type[$i] = GETPOSTINT($i.'_type');
89 
90  $tabnum[$i] = 0;
91  if (!empty($label[$i]) || !($amount[$i] <= 0) || !($accountfrom[$i] < 0) || !($accountto[$i] < 0)) {
92  $tabnum[$i] = 1;
93  $maxtab = $i;
94  }
95  $i++;
96  }
97 
98  $db->begin();
99 
100  $n = 1;
101  while ($n < $MAXLINES) {
102  if ($tabnum[$n] === 1) {
103  if ($accountfrom[$n] < 0) {
104  $error++;
105  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("TransferFrom")), null, 'errors');
106  }
107  if ($accountto[$n] < 0) {
108  $error++;
109  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("TransferTo")), null, 'errors');
110  }
111  if (!$type[$n]) {
112  $error++;
113  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Type")), null, 'errors');
114  }
115  if (!$dateo[$n]) {
116  $error++;
117  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Date")), null, 'errors');
118  }
119 
120  if (!($label[$n])) {
121  $error++;
122  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' . $langs->transnoentities("Description")), null, 'errors');
123  }
124  if (!($amount[$n])) {
125  $error++;
126  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Amount")), null, 'errors');
127  }
128 
129  $tmpaccountfrom = new Account($db);
130  $tmpaccountfrom->fetch(GETPOSTINT($n.'_account_from'));
131 
132  $tmpaccountto = new Account($db);
133  $tmpaccountto->fetch(GETPOSTINT($n.'_account_to'));
134 
135  if ($tmpaccountto->currency_code == $tmpaccountfrom->currency_code) {
136  $amountto[$n] = $amount[$n];
137  } else {
138  if (!$amountto[$n]) {
139  $error++;
140  setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n.' '.$langs->transnoentities("AmountToOthercurrency")), null, 'errors');
141  }
142  }
143  if ($amountto[$n] < 0) {
144  $error++;
145  setEventMessages($langs->trans("AmountMustBePositive").' #'.$n, null, 'errors');
146  }
147 
148  if ($tmpaccountto->id == $tmpaccountfrom->id) {
149  $error++;
150  setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers").' #'.$n, null, 'errors');
151  }
152 
153  if (!$error) {
154  $bank_line_id_from = 0;
155  $bank_line_id_to = 0;
156  $result = 0;
157 
158  // By default, electronic transfer from bank to bank
159  $typefrom = $type[$n];
160  $typeto = $type[$n];
161  if ($tmpaccountto->type == Account::TYPE_CASH || $tmpaccountfrom->type == Account::TYPE_CASH) {
162  // This is transfer of change
163  $typefrom = 'LIQ';
164  $typeto = 'LIQ';
165  }
166 
167  if (!$error) {
168  $bank_line_id_from = $tmpaccountfrom->addline($dateo[$n], $typefrom, $label[$n], price2num(-1 * (float) $amount[$n]), '', '', $user);
169  }
170  if (!($bank_line_id_from > 0)) {
171  $error++;
172  }
173  if (!$error) {
174  $bank_line_id_to = $tmpaccountto->addline($dateo[$n], $typeto, $label[$n], $amountto[$n], '', '', $user);
175  }
176  if (!($bank_line_id_to > 0)) {
177  $error++;
178  }
179 
180  if (!$error) {
181  $result = $tmpaccountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
182  }
183  if (!($result > 0)) {
184  $error++;
185  }
186  if (!$error) {
187  $result = $tmpaccountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
188  }
189  if (!($result > 0)) {
190  $error++;
191  }
192  if (!$error) {
193  $mesg = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount[$n], $langs->transnoentitiesnoconv("Currency".$conf->currency));
194  $mesg = str_replace('{s1}', '<a href="bankentries_list.php?id='.$tmpaccountfrom->id.'&sortfield=b.datev,b.dateo,b.rowid&sortorder=desc">'.$tmpaccountfrom->label.'</a>', $mesgs);
195  $mesg = str_replace('{s2}', '<a href="bankentries_list.php?id='.$tmpaccountto->id.'">'.$tmpaccountto->label.'</a>', $mesgs);
196  setEventMessages($mesg, null, 'mesgs');
197  } else {
198  $error++;
199  setEventMessages($tmpaccountfrom->error.' '.$tmpaccountto->error, null, 'errors');
200  }
201  }
202  }
203  $n++;
204  }
205 
206  if (!$error) {
207  $db->commit();
208 
209  header("Location: ".DOL_URL_ROOT.'/compta/bank/transfer.php');
210  exit;
211  } else {
212  $db->rollback();
213  }
214 }
215 
216 
217 /*
218  * View
219  */
220 
221 $form = new Form($db);
222 
223 $help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
224 $title = $langs->trans('MenuBankInternalTransfer');
225 
226 llxHeader('', $title, $help_url);
227 
228 
229 print '<script type="text/javascript">
230  $(document).ready(function () {
231  $(".selectbankaccount").change(function() {
232  console.log("We change bank account. We check if currency differs. If yes, we show multicurrency field");
233  i = $(this).attr("name").replace("_account_to", "").replace("_account_from", "");
234  console.log(i);
235  init_page(i);
236  });
237 
238  function init_page(i) {
239  var atleast2differentcurrency = false;
240 
241  $(".selectbankaccount").each(function( index ) {
242  // Scan all line i and set atleast2differentcurrency if there is 2 different values among all lines
243  var account1 = $("#select"+index+"_account_from").val();
244  var account2 = $("#select"+index+"_account_to").val();
245  var currencycode1 = $("#select"+index+"_account_from option:selected").attr("data-currency-code");
246  var currencycode2 = $("#select"+index+"_account_to option:selected").attr("data-currency-code");
247  console.log("Set atleast2differentcurrency according to currencycode found for index="+index+" currencycode1="+currencycode1+" currencycode2="+currencycode2);
248 
249  atleast2differentcurrency = (currencycode2!==currencycode1 && currencycode1 !== undefined && currencycode2 !== undefined && currencycode2!=="" && currencycode1!=="");
250  if (atleast2differentcurrency) {
251  return false;
252  }
253  });
254 
255 
256  if (atleast2differentcurrency) {
257  console.log("We show multicurrency field");
258  $(".multicurrency").show();
259  } else {
260  console.log("We hide multicurrency field");
261  $(".multicurrency").hide();
262  }
263 
264  // Show all linew with view=view
265  $("select").each(function() {
266  if( $(this).attr("view")){
267  $(this).closest("tr").removeClass("hidejs").removeClass("hideobject");
268  }
269  });
270 
271  }
272 
273  init_page(1);
274  });
275  </script>';
276 
277 
278 print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
279 
280 print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
281 print '<br><br>';
282 
283 print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
284 print '<input type="hidden" name="token" value="'.newToken().'">';
285 print '<input type="hidden" name="action" value="add">';
286 
287 print '<div>';
288 
289 print '<div class="div-table-responsive-no-min">';
290 print '<table id="tablemouvbank" class="noborder centpercent">';
291 
292 print '<tr class="liste_titre">';
293 print '<th>'.$langs->trans("TransferFrom").'</th>';
294 print '<th>'.$langs->trans("TransferTo").'</th>';
295 print '<th>'.$langs->trans("Type").'</th>';
296 print '<th>'.$langs->trans("Date").'</th>';
297 print '<th>'.$langs->trans("Description").'</th>';
298 print '<th class="right">'.$langs->trans("Amount").'</th>';
299 print '<td class="hideobject multicurrency right">'.$langs->trans("AmountToOthercurrency").'</td>';
300 print '</tr>';
301 
302 for ($i = 1 ; $i < $MAXLINES; $i++) {
303  $label = '';
304  $amount = '';
305  $amountto = '';
306 
307  if ($error) {
308  $label = GETPOST($i.'_label', 'alpha');
309  $amount = GETPOST($i.'_amount', 'alpha');
310  $amountto = GETPOST($i.'_amountto', 'alpha');
311  }
312 
313  if ($i == 1) {
314  $classi = 'numvir number'.$i;
315  $classi .= ' active';
316  } else {
317  $classi = 'numvir number'.$i;
318  $classi .= ' hidejs hideobject';
319  }
320 
321  print '<tr class="oddeven nowraponall '.$classi.'"><td>';
322  print img_picto('', 'bank_account', 'class="paddingright"');
323  $form->select_comptes(($error ? GETPOSTINT($i.'_account_from') : ''), $i.'_account_from', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
324  print '</td>';
325 
326  print '<td class="nowraponall">';
327  print img_picto('', 'bank_account', 'class="paddingright"');
328  $form->select_comptes(($error ? GETPOSTINT($i.'_account_to') : ''), $i.'_account_to', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
329  print "</td>\n";
330 
331  // Payment mode
332  print '<td class="nowraponall">';
333  $idpaymentmodetransfer = dol_getIdFromCode($db, 'VIR', 'c_paiement');
334  $form->select_types_paiements(($error ? GETPOST($i.'_type', 'aZ09') : $idpaymentmodetransfer), $i.'_type', '', 0, 1, 0, 0, 1, 'minwidth100');
335  print "</td>\n";
336 
337  // Date
338  print '<td class="nowraponall">';
339  print $form->selectDate((!empty($dateo[$i]) ? $dateo[$i] : ''), $i.'_', 0, 0, 0, 'add');
340  print "</td>\n";
341 
342  // Description
343  print '<td><input name="'.$i.'_label" class="flat quatrevingtpercent selectjs" type="text" value="'.dol_escape_htmltag($label).'"></td>';
344 
345  // Amount
346  print '<td class="right"><input name="'.$i.'_amount" class="flat right selectjs" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
347 
348  // AmountToOthercurrency
349  print '<td class="hideobject multicurrency right"><input name="'.$i.'_amountto" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
350 
351  print '</tr>';
352 }
353 
354 print '</table>';
355 print '</div>';
356 print '</div>';
357 print '<div id="btncont" style="display: flex; align-items: center">';
358 print '<a id="btnincrement" style="margin-left:35%" class="btnTitle btnTitlePlus" onclick="increment()" title="'.dol_escape_htmltag($langs->trans("Add")).'">
359  <span class="fa fa-plus-circle valignmiddle btnTitle-icon">
360  </span>
361  </a>';
362 print '<br><div class=""><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
363 print '</div>';
364 
365 print '</form>';
366 
367 print '<script type="text/javascript">
368  function increment() {
369  console.log("We click to show next line");
370  $(".numvir").nextAll(".hidejs:first").removeClass("hidejs").removeClass("hideobject").addClass("active").show();
371  }
372  </script>
373  ';
374 
375 // End of page
376 llxFooter();
377 
378 $db->close();
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:118
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage generation of HTML components Only common components must be here.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
isModEnabled($module)
Is Dolibarr module enabled.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.