dolibarr 21.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
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
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;
46if ($user->socid > 0) {
47 $socid = $user->socid;
48}
49if (!$user->hasRight('banque', 'transfer')) {
51}
52
53$MAXLINESFORTRANSFERT = 20;
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
64if ($reshook < 0) {
65 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
66}
67if ($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 < $MAXLINESFORTRANSFERT) {
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 < $MAXLINESFORTRANSFERT) {
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
193 if (!$error) {
194 $mesg = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount[$n], $langs->transnoentitiesnoconv("Currency".$conf->currency));
195 $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);
196 $mesg = str_replace('{s2}', '<a href="bankentries_list.php?id='.$tmpaccountto->id.'">'.$tmpaccountto->label.'</a>', $mesg);
197 setEventMessages($mesg, null, 'mesgs');
198 } else {
199 $error++;
200 setEventMessages($tmpaccountfrom->error.' '.$tmpaccountto->error, null, 'errors');
201 }
202 }
203 }
204 $n++;
205 }
206
207 if (!$error) {
208 $db->commit();
209
210 header("Location: ".DOL_URL_ROOT.'/compta/bank/transfer.php');
211 exit;
212 } else {
213 $db->rollback();
214 }
215}
216
217
218/*
219 * View
220 */
221
222$form = new Form($db);
223
224$help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
225$title = $langs->trans('MenuBankInternalTransfer');
226
227llxHeader('', $title, $help_url);
228
229
230print '<script type="text/javascript">
231 $(document).ready(function () {
232 $(".selectbankaccount").change(function() {
233 console.log("We change bank account. We check if currency differs. If yes, we show multicurrency field");
234 i = $(this).attr("name").replace("_account_to", "").replace("_account_from", "");
235 console.log(i);
236 init_page(i);
237 });
238
239 function init_page(i) {
240 var atleast2differentcurrency = false;
241
242 $(".selectbankaccount").each(function( index ) {
243 // Scan all line i and set atleast2differentcurrency if there is 2 different values among all lines
244 var account1 = $("#select"+index+"_account_from").val();
245 var account2 = $("#select"+index+"_account_to").val();
246 var currencycode1 = $("#select"+index+"_account_from option:selected").attr("data-currency-code");
247 var currencycode2 = $("#select"+index+"_account_to option:selected").attr("data-currency-code");
248 console.log("Set atleast2differentcurrency according to currencycode found for index="+index+" currencycode1="+currencycode1+" currencycode2="+currencycode2);
249
250 atleast2differentcurrency = (currencycode2!==currencycode1 && currencycode1 !== undefined && currencycode2 !== undefined && currencycode2!=="" && currencycode1!=="");
251 if (atleast2differentcurrency) {
252 return false;
253 }
254 });
255
256
257 if (atleast2differentcurrency) {
258 console.log("We show multicurrency field");
259 $(".multicurrency").show();
260 } else {
261 console.log("We hide multicurrency field");
262 $(".multicurrency").hide();
263 }
264
265 // Show all linew with view=view
266 $("select").each(function() {
267 if( $(this).attr("view")){
268 $(this).closest("tr").removeClass("hidejs").removeClass("hideobject");
269 }
270 });
271
272 }
273
274 init_page(1);
275 });
276 </script>';
277
278
279print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
280
281print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
282print '<br><br>';
283
284print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
285print '<input type="hidden" name="token" value="'.newToken().'">';
286print '<input type="hidden" name="action" value="add">';
287
288print '<div>';
289
290print '<div class="div-table-responsive-no-min">';
291print '<table id="tablemouvbank" class="noborder centpercent">';
292
293print '<tr class="liste_titre">';
294print '<th>'.$langs->trans("TransferFrom").'</th>';
295print '<th>'.$langs->trans("TransferTo").'</th>';
296print '<th>'.$langs->trans("Type").'</th>';
297print '<th>'.$langs->trans("Date").'</th>';
298print '<th>'.$langs->trans("Description").'</th>';
299print '<th class="right">'.$langs->trans("Amount").'</th>';
300print '<td class="hideobject multicurrency right">'.$langs->trans("AmountToOthercurrency").'</td>';
301print '</tr>';
302
303for ($i = 1 ; $i < $MAXLINESFORTRANSFERT; $i++) {
304 $label = '';
305 $amount = '';
306 $amountto = '';
307
308 if ($error) {
309 $label = GETPOST($i.'_label', 'alpha');
310 $amount = GETPOST($i.'_amount', 'alpha');
311 $amountto = GETPOST($i.'_amountto', 'alpha');
312 }
313
314 if ($i == 1) {
315 $classi = 'numvir number'.$i;
316 $classi .= ' active';
317 } else {
318 $classi = 'numvir number'.$i;
319 $classi .= ' hidejs hideobject';
320 }
321
322 print '<tr class="oddeven nowraponall '.$classi.'"><td>';
323 print img_picto('', 'bank_account', 'class="paddingright"');
324 $form->select_comptes(($error ? GETPOSTINT($i.'_account_from') : ''), $i.'_account_from', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
325 print '</td>';
326
327 print '<td class="nowraponall">';
328 print img_picto('', 'bank_account', 'class="paddingright"');
329 $form->select_comptes(($error ? GETPOSTINT($i.'_account_to') : ''), $i.'_account_to', 0, '', 1, '', isModEnabled('multicurrency') ? 1 : 0, 'minwidth100');
330 print "</td>\n";
331
332 // Payment mode
333 print '<td class="nowraponall">';
334 $idpaymentmodetransfer = dol_getIdFromCode($db, 'VIR', 'c_paiement');
335 $form->select_types_paiements(($error ? GETPOST($i.'_type', 'aZ09') : $idpaymentmodetransfer), $i.'_type', '', 0, 1, 0, 0, 1, 'minwidth100');
336 print "</td>\n";
337
338 // Date
339 print '<td class="nowraponall">';
340 print $form->selectDate((!empty($dateo[$i]) ? $dateo[$i] : ''), $i.'_', 0, 0, 0, 'add');
341 print "</td>\n";
342
343 // Description
344 print '<td><input name="'.$i.'_label" class="flat quatrevingtpercent selectjs" type="text" value="'.dol_escape_htmltag($label).'"></td>';
345
346 // Amount
347 print '<td class="right"><input name="'.$i.'_amount" class="flat right selectjs" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
348
349 // AmountToOthercurrency
350 print '<td class="hideobject multicurrency right"><input name="'.$i.'_amountto" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
351
352 print '</tr>';
353}
354
355print '</table>';
356print '</div>';
357print '</div>';
358print '<div id="btncont" style="display: flex; align-items: center">';
359print '<a id="btnincrement" style="margin-left:35%" class="btnTitle btnTitlePlus" onclick="increment()" title="'.dol_escape_htmltag($langs->trans("Add")).'">
360 <span class="fa fa-plus-circle valignmiddle btnTitle-icon">
361 </span>
362 </a>';
363print '<br><div class=""><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
364print '</div>';
365
366print '</form>';
367
368print '<script type="text/javascript">
369 function increment() {
370 console.log("We click to show next line");
371 $(".numvir").nextAll(".hidejs:first").removeClass("hidejs").removeClass("hideobject").addClass("active").show();
372 }
373 </script>
374 ';
375
376// End of page
377llxFooter();
378
379$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:70
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.