dolibarr 21.0.0-beta
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
34require '../../main.inc.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
37
46// Load translation files required by the page
47$langs->loadLangs(array('banks', 'categories', 'multicurrency'));
48
49$action = GETPOST('action', 'aZ09');
50
51$hookmanager->initHooks(array('banktransfer'));
52
53$socid = 0;
54if ($user->socid > 0) {
55 $socid = $user->socid;
56}
57if (!$user->hasRight('banque', 'transfer')) {
59}
60
61$MAXLINESFORTRANSFERT = 20;
62
63$error = 0;
64
65
66/*
67 * Actions
68 */
69
70$parameters = array('socid' => $socid);
71$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
72if ($reshook < 0) {
73 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
74}
75if ($action == 'add' && $user->hasRight('banque', 'transfer')) {
76 $langs->load('errors');
77 $i = 1;
78
79 $dateo = array();
80 $label = array();
81 $amount = array();
82 $amountto = array();
83 $accountfrom = array();
84 $accountto = array();
85 $type = array();
86 $tabnum = array();
87 $maxtab = 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
98 $tabnum[$i] = 0;
99 if (!empty($label[$i]) || !($amount[$i] <= 0) || !($accountfrom[$i] < 0) || !($accountto[$i] < 0)) {
100 $tabnum[$i] = 1;
101 $maxtab = $i;
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 if ($tmpaccountto->currency_code == $tmpaccountfrom->currency_code) {
144 $amountto[$n] = $amount[$n];
145 } else {
146 if (!$amountto[$n]) {
147 $error++;
148 setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n.' '.$langs->transnoentities("AmountToOthercurrency")), null, 'errors');
149 }
150 }
151 if ($amountto[$n] < 0) {
152 $error++;
153 setEventMessages($langs->trans("AmountMustBePositive").' #'.$n, null, 'errors');
154 }
155
156 if ($tmpaccountto->id == $tmpaccountfrom->id) {
157 $error++;
158 setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers").' #'.$n, null, 'errors');
159 }
160
161 if (!$error) {
162 $bank_line_id_from = 0;
163 $bank_line_id_to = 0;
164 $result = 0;
165
166 // By default, electronic transfer from bank to bank
167 $typefrom = $type[$n];
168 $typeto = $type[$n];
169 if ($tmpaccountto->type == Account::TYPE_CASH || $tmpaccountfrom->type == Account::TYPE_CASH) {
170 // This is transfer of change
171 $typefrom = 'LIQ';
172 $typeto = 'LIQ';
173 }
174
175 if (!$error) {
176 $bank_line_id_from = $tmpaccountfrom->addline($dateo[$n], $typefrom, $label[$n], price2num(-1 * (float) $amount[$n]), '', 0, $user);
177 }
178 if (!($bank_line_id_from > 0)) {
179 $error++;
180 }
181 if (!$error) {
182 $bank_line_id_to = $tmpaccountto->addline($dateo[$n], $typeto, $label[$n], $amountto[$n], '', 0, $user);
183 }
184 if (!($bank_line_id_to > 0)) {
185 $error++;
186 }
187
188 if (!$error) {
189 $result = $tmpaccountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
190 }
191 if (!($result > 0)) {
192 $error++;
193 }
194 if (!$error) {
195 $result = $tmpaccountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
196 }
197 if (!($result > 0)) {
198 $error++;
199 }
200
201 if (!$error) {
202 $mesg = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount[$n], $langs->transnoentitiesnoconv("Currency".$conf->currency));
203 $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);
204 $mesg = str_replace('{s2}', '<a href="bankentries_list.php?id='.$tmpaccountto->id.'">'.$tmpaccountto->label.'</a>', $mesg);
205 setEventMessages($mesg, null, 'mesgs');
206 } else {
207 $error++;
208 setEventMessages($tmpaccountfrom->error.' '.$tmpaccountto->error, null, 'errors');
209 }
210 }
211 }
212 $n++;
213 }
214
215 if (!$error) {
216 $db->commit();
217
218 header("Location: ".DOL_URL_ROOT.'/compta/bank/transfer.php');
219 exit;
220 } else {
221 $db->rollback();
222 }
223}
224
225
226/*
227 * View
228 */
229
230$form = new Form($db);
231
232$help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
233$title = $langs->trans('MenuBankInternalTransfer');
234
235llxHeader('', $title, $help_url);
236
237
238print '<script type="text/javascript">
239 $(document).ready(function () {
240 $(".selectbankaccount").change(function() {
241 console.log("We change bank account. We check if currency differs. If yes, we show multicurrency field");
242 i = $(this).attr("name").replace("_account_to", "").replace("_account_from", "");
243 console.log(i);
244 init_page(i);
245 });
246
247 function init_page(i) {
248 var atleast2differentcurrency = false;
249
250 $(".selectbankaccount").each(function( index ) {
251 // Scan all line i and set atleast2differentcurrency if there is 2 different values among all lines
252 var account1 = $("#select"+index+"_account_from").val();
253 var account2 = $("#select"+index+"_account_to").val();
254 var currencycode1 = $("#select"+index+"_account_from option:selected").attr("data-currency-code");
255 var currencycode2 = $("#select"+index+"_account_to option:selected").attr("data-currency-code");
256 console.log("Set atleast2differentcurrency according to currencycode found for index="+index+" currencycode1="+currencycode1+" currencycode2="+currencycode2);
257
258 atleast2differentcurrency = (currencycode2!==currencycode1 && currencycode1 !== undefined && currencycode2 !== undefined && currencycode2!=="" && currencycode1!=="");
259 if (atleast2differentcurrency) {
260 return false;
261 }
262 });
263
264
265 if (atleast2differentcurrency) {
266 console.log("We show multicurrency field");
267 $(".multicurrency").show();
268 } else {
269 console.log("We hide multicurrency field");
270 $(".multicurrency").hide();
271 }
272
273 // Show all linew with view=view
274 $("select").each(function() {
275 if( $(this).attr("view")){
276 $(this).closest("tr").removeClass("hidejs").removeClass("hideobject");
277 }
278 });
279
280 }
281
282 init_page(1);
283 });
284 </script>';
285
286
287print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
288
289print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
290print '<br><br>';
291
292print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
293print '<input type="hidden" name="token" value="'.newToken().'">';
294print '<input type="hidden" name="action" value="add">';
295
296print '<div>';
297
298print '<div class="div-table-responsive-no-min">';
299print '<table id="tablemouvbank" class="noborder centpercent">';
300
301print '<tr class="liste_titre">';
302print '<th>'.$langs->trans("TransferFrom").'</th>';
303print '<th>'.$langs->trans("TransferTo").'</th>';
304print '<th>'.$langs->trans("Type").'</th>';
305print '<th>'.$langs->trans("Date").'</th>';
306print '<th>'.$langs->trans("Description").'</th>';
307print '<th class="right">'.$langs->trans("Amount").'</th>';
308print '<td class="hideobject multicurrency right">'.$langs->trans("AmountToOthercurrency").'</td>';
309print '</tr>';
310
311for ($i = 1 ; $i < $MAXLINESFORTRANSFERT; $i++) {
312 $label = '';
313 $amount = '';
314 $amountto = '';
315
316 if ($error) {
317 $label = GETPOST($i.'_label', 'alpha');
318 $amount = GETPOST($i.'_amount', 'alpha');
319 $amountto = GETPOST($i.'_amountto', 'alpha');
320 }
321
322 if ($i == 1) {
323 $classi = 'numvir number'.$i;
324 $classi .= ' active';
325 } else {
326 $classi = 'numvir number'.$i;
327 $classi .= ' hidejs hideobject';
328 }
329
330 print '<tr class="oddeven nowraponall '.$classi.'"><td>';
331 print img_picto('', 'bank_account', 'class="paddingright"');
332 $form->select_comptes(($error ? GETPOSTINT($i.'_account_from') : ''), $i.'_account_from', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
333 print '</td>';
334
335 print '<td class="nowraponall">';
336 print img_picto('', 'bank_account', 'class="paddingright"');
337 $form->select_comptes(($error ? GETPOSTINT($i.'_account_to') : ''), $i.'_account_to', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
338 print "</td>\n";
339
340 // Payment mode
341 print '<td class="nowraponall">';
342 $idpaymentmodetransfer = dol_getIdFromCode($db, 'VIR', 'c_paiement');
343 $form->select_types_paiements(($error ? GETPOST($i.'_type', 'aZ09') : $idpaymentmodetransfer), $i.'_type', '', 0, 1, 0, 0, 1, 'minwidth100');
344 print "</td>\n";
345
346 // Date
347 print '<td class="nowraponall">';
348 print $form->selectDate((!empty($dateo[$i]) ? $dateo[$i] : ''), $i.'_', 0, 0, 0, 'add');
349 print "</td>\n";
350
351 // Description
352 print '<td><input name="'.$i.'_label" class="flat quatrevingtpercent selectjs" type="text" value="'.dol_escape_htmltag($label).'"></td>';
353
354 // Amount
355 print '<td class="right"><input name="'.$i.'_amount" class="flat right selectjs" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
356
357 // AmountToOthercurrency
358 print '<td class="hideobject multicurrency right"><input name="'.$i.'_amountto" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
359
360 print '</tr>';
361}
362
363print '</table>';
364print '</div>';
365print '</div>';
366print '<div id="btncont" style="display: flex; align-items: center">';
367print '<a id="btnincrement" style="margin-left:35%" class="btnTitle btnTitlePlus" onclick="increment()" title="'.dol_escape_htmltag($langs->trans("Add")).'">
368 <span class="fa fa-plus-circle valignmiddle btnTitle-icon">
369 </span>
370 </a>';
371print '<br><div class=""><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
372print '</div>';
373
374print '</form>';
375
376print '<script type="text/javascript">
377 function increment() {
378 console.log("We click to show next line");
379 $(".numvir").nextAll(".hidejs:first").removeClass("hidejs").removeClass("hideobject").addClass("active").show();
380 }
381 </script>
382 ';
383
384// End of page
385llxFooter();
386
387$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:71
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
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($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
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.
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.