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