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