dolibarr 21.0.0-beta
bank.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
5 * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
7 * Copyright (C) 2021 Ferran Marcet <fmarcet@2byte.es>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 * or see https://www.gnu.org/
24 */
25
40{
41 global $db, $langs, $conf, $user;
42
43 $h = 0;
44 $head = array();
45
46 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/card.php?id='.$object->id;
47 $head[$h][1] = $langs->trans("BankAccount");
48 $head[$h][2] = 'bankname';
49 $h++;
50
51 $head[$h][0] = DOL_URL_ROOT."/compta/bank/bankentries_list.php?id=".$object->id;
52 $head[$h][1] = $langs->trans("BankTransactions");
53 $head[$h][2] = 'journal';
54 $h++;
55
56 if ($object->canBeConciliated() > 0) {
57 $allowautomaticconciliation = getDolGlobalBool('MAIN_ALLOW_AUTOMATIC_CONCILIATION'); // TODO
58 $titletoconciliatemanual = $langs->trans("Conciliate");
59 $titletoconciliateauto = $langs->trans("Conciliate");
60 if ($allowautomaticconciliation) {
61 $titletoconciliatemanual .= ' ('.$langs->trans("Manual").')';
62 $titletoconciliateauto .= ' ('.$langs->trans("Auto").')';
63 }
64
65 $param = '';
66
67 // If not cash account and can be reconciliate
68 if ($user->hasRight('banque', 'consolidate')) {
69 $head[$h][0] = DOL_URL_ROOT."/compta/bank/bankentries_list.php?id=".$object->id.'&action=reconcile&sortfield=b.datev,b.dateo,b.rowid&sortorder=asc,asc,asc&search_conciliated=0&search_account='.$object->id.$param;
70 $head[$h][1] = $titletoconciliatemanual;
71 $head[$h][2] = 'reconcile';
72 $h++;
73 }/* else {
74 $buttonreconcile = '<a class="butActionRefused classfortooltip" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$titletoconciliatemanual.'</a>';
75 }*/
76
77 if ($allowautomaticconciliation) {
78 // If not cash account and can be reconciliate
79 if ($user->hasRight('banque', 'consolidate')) {
80 $newparam = $param;
81 $newparam = preg_replace('/search_conciliated=\d+/i', '', $newparam);
82
83 $head[$h][0] = DOL_URL_ROOT."/compta/bank/bankentries_list.php?id=".$object->id.'&action=reconcile&sortfield=b.datev,b.dateo,b.rowid&sortorder=asc,asc,asc&search_conciliated=0&search_account='.$object->id.$newparam;
84 $head[$h][1] = $titletoconciliateauto;
85 $head[$h][2] = 'reconcileauto';
86 $h++;
87
88 //$buttonreconcile .= ' <a class="butAction" style="margin-bottom: 5px !important; margin-top: 5px !important" href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?action=reconcile&sortfield=b.datev,b.dateo,b.rowid&sortorder=asc,asc,asc&search_conciliated=0'.$newparam.'">'.$titletoconciliateauto.'</a>';
89 }/* else {
90 $buttonreconcile .= ' <a class="butActionRefused" style="margin-bottom: 5px !important; margin-top: 5px !important" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$titletoconciliateauto.'</a>';
91 }*/
92 }
93 }
94
95 if ($object->type != Account::TYPE_CASH || getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
96 $nbReceipts = 0;
97
98 // List of all standing receipts
99 $sql = "SELECT COUNT(DISTINCT(b.num_releve)) as nb";
100 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
101 $sql .= " WHERE b.fk_account = ".((int) $object->id);
102
103 $resql = $db->query($sql);
104 if ($resql) {
105 $obj = $db->fetch_object($resql);
106 if ($obj) {
107 $nbReceipts = $obj->nb;
108 }
109 $db->free($resql);
110 }
111
112 $head[$h][0] = DOL_URL_ROOT."/compta/bank/releve.php?account=".((int) $object->id);
113 $head[$h][1] = $langs->trans("AccountStatements");
114 if (($nbReceipts) > 0) {
115 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbReceipts).'</span>';
116 }
117 $head[$h][2] = 'statement';
118 $h++;
119 }
120
121 // Attached files
122 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
123 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
124 $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
125 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
126 $nbLinks = Link::count($db, $object->element, $object->id);
127 $head[$h][0] = DOL_URL_ROOT."/compta/bank/document.php?account=".$object->id;
128 $head[$h][1] = $langs->trans("Documents");
129 if (($nbFiles + $nbLinks) > 0) {
130 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
131 }
132 $head[$h][2] = 'document';
133 $h++;
134
135 $head[$h][0] = DOL_URL_ROOT."/compta/bank/annuel.php?account=".$object->id;
136 $head[$h][1] = $langs->trans("IOMonthlyReporting");
137 $head[$h][2] = 'annual';
138 $h++;
139
140 $head[$h][0] = DOL_URL_ROOT."/compta/bank/graph.php?account=".$object->id;
141 $head[$h][1] = $langs->trans("Graph");
142 $head[$h][2] = 'graph';
143 $h++;
144
145 $head[$h][0] = DOL_URL_ROOT."/compta/bank/treso.php?account=".$object->id;
146 $head[$h][1] = $langs->trans("PlannedTransactions");
147 $head[$h][2] = 'cash';
148 $h++;
149
150 // Show more tabs from modules
151 // Entries must be declared in modules descriptor with line
152 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
153 // $this->tabs = array('entity:-tabname); to remove a tab
154 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
155
156 /*$head[$h][0] = DOL_URL_ROOT . "/compta/bank/info.php?id=" . $object->id;
157 $head[$h][1] = $langs->trans("Info");
158 $head[$h][2] = 'info';
159 $h++;*/
160
161 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
162
163 return $head;
164}
172{
173 global $langs, $conf, $db;
174
175 $langs->loadLangs(array("compta"));
176
177 $extrafields = new ExtraFields($db);
178 $extrafields->fetch_name_optionals_label('bank_account');
179 $extrafields->fetch_name_optionals_label('bank');
180
181 $h = 0;
182 $head = array();
183
184 $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
185 $head[$h][1] = $langs->trans("Miscellaneous");
186 $head[$h][2] = 'general';
187 $h++;
188
189 $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
190 $head[$h][1] = $langs->trans("CheckReceiptShort");
191 $head[$h][2] = 'checkreceipts';
192 $h++;
193
194
195 // Show more tabs from modules
196 // Entries must be declared in modules descriptor with line
197 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
198 // $this->tabs = array('entity:-tabname); to remove a tab
199 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
200
201 $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
202 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankAccounts").')';
203 $nbExtrafields = $extrafields->attributes['bank_account']['count'];
204 if ($nbExtrafields > 0) {
205 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
206 }
207 $head[$h][2] = 'attributes';
208 $h++;
209
210 $head[$h][0] = DOL_URL_ROOT.'/admin/bankline_extrafields.php';
211 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankTransactions").')';
212 $nbExtrafields = $extrafields->attributes['bank']['count'];
213 if ($nbExtrafields > 0) {
214 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
215 }
216 $head[$h][2] = 'bankline_extrafields';
217 $h++;
218
219 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
220
221
222 return $head;
223}
224
225
234{
235 global $langs, $conf, $db;
236 $h = 0;
237 $head = array();
238
239 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
240 $head[$h][1] = $langs->trans("AccountStatement");
241 $head[$h][2] = 'statement';
242 $h++;
243
244 // Attached files
245 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
246 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
247 $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
248 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
249 $nbLinks = Link::count($db, $object->element, $object->id);
250
251 $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
252 $head[$h][1] = $langs->trans("Documents");
253 if (($nbFiles + $nbLinks) > 0) {
254 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
255 }
256 $head[$h][2] = 'document';
257 $h++;
258
259 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
260
261 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
262
263 return $head;
264}
265
266
274{
275 global $db, $langs, $conf;
276
277 $h = 0;
278 $head = array();
279
280 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
281 $head[$h][1] = $langs->trans("VariousPayment");
282 $head[$h][2] = 'card';
283 $h++;
284
285 // Show more tabs from modules
286 // Entries must be declared in modules descriptor with line
287 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
288 // $this->tabs = array('entity:-tabname); to remove a tab
289 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
290
291 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
292 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
293 $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
294 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
295 $nbLinks = Link::count($db, $object->element, $object->id);
296 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
297 $head[$h][1] = $langs->trans('Documents');
298 if (($nbFiles + $nbLinks) > 0) {
299 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
300 }
301 $head[$h][2] = 'documents';
302 $h++;
303
304 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
305 $head[$h][1] = $langs->trans("Info");
306 $head[$h][2] = 'info';
307 $h++;
308
309 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
310
311 return $head;
312}
313
321function checkSwiftForAccount($account = null, $swift = null)
322{
323 if ($account == null && $swift == null) {
324 return false;
325 } elseif ($swift == null) {
326 $swift = $account->bic;
327 }
328 if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
329 return true;
330 } else {
331 return false;
332 }
333}
334
342function checkIbanForAccount($account = null, $ibantocheck = null)
343{
344 if ($account == null && $ibantocheck == null) {
345 return false;
346 } elseif ($ibantocheck == null) {
347 $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
348 }
349 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
350
351 $iban = new PHP_IBAN\IBAN($ibantocheck);
352 $check = $iban->Verify();
353
354 if ($check) {
355 return true;
356 } else {
357 return false;
358 }
359}
360
368{
369 if ($account->getCountryCode() == 'FR') {
370 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
371 $ibantoprint = preg_replace('/[^a-zA-Z0-9]/', '', empty($account->iban) ? '' : $account->iban);
372 $iban = new PHP_IBAN\IBAN($ibantoprint);
373 return $iban->HumanFormat();
374 }
375
376 return $account->iban;
377}
378
385function checkBanForAccount($account)
386{
387 $country_code = $account->getCountryCode();
388
389 // For compatibility between
390 // account of type CompanyBankAccount class (we use number, cle_rib)
391 // account of type Account class (we use num_compte, cle)
392 if (empty($account->number)) {
393 $account->number = $account->num_compte; // @phan-suppress-current-line PhanUndeclaredProperty
394 }
395 if (empty($account->cle)) {
396 $account->cle = $account->cle_rib;
397 }
398
399 dol_syslog("bank.lib::checkBanForAccount account->code_banque=".$account->code_banque." account->code_guichet=".$account->code_guichet." account->number=".$account->number." account->cle=".$account->cle." account->iban=".$account->iban." country_code=".$country_code, LOG_DEBUG);
400
401 if ($country_code == 'FR') { // France rules
402 $coef = array(62, 34, 3);
403 // Concatenate the code parts
404 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
405 // On replace les eventuelles lettres par des chiffres.
406 //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
407 $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
408 // Separation du rib en 3 groups de 7 + 1 group de 2.
409 // Multiplication de chaque group par les coef du tableau
410
411 for ($i = 0, $s = 0; $i < 3; $i++) {
412 $code = substr($rib, 7 * $i, 7);
413 $s += ((int) $code) * $coef[$i];
414 }
415 // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
416 $cle_rib = 97 - ($s % 97);
417 if ($cle_rib == $account->cle) {
418 return true;
419 }
420 return false;
421 }
422
423 /*
424 if ($country_code == 'BE') { // Belgian rules
425 }
426 */
427
428 if ($country_code == 'ES') { // Spanish rules
429 $CCC = strtolower(trim($account->number));
430 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
431 $cle_rib = strtolower(checkES($rib, $CCC));
432 if ($cle_rib == strtolower($account->cle)) {
433 return true;
434 }
435 return false;
436 }
437 if ($country_code == 'AU') { // Australian
438 if (strlen($account->code_banque) > 7) {
439 return false; // Should be 6 but can be 123-456
440 } elseif (strlen($account->code_banque) < 6) {
441 return false; // Should be 6
442 } else {
443 return true;
444 }
445 }
446
447 // No particular rule
448 // If account is CompanyBankAccount class, we use number
449 // If account is Account class, we use num_compte
450 if (empty($account->number)) {
451 return false;
452 }
453
454 return true;
455}
456
457
458
466function checkES($IentOfi, $InumCta)
467{
468 if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
469 $keycontrol = "";
470 return $keycontrol;
471 }
472
473 $ccc = $IentOfi.$InumCta;
474 $numbers = "1234567890";
475
476 $i = 0;
477
478 while ($i <= strlen($ccc) - 1) {
479 if (strpos($numbers, substr($ccc, $i, 1)) === false) {
480 $keycontrol = "";
481 return $keycontrol;
482 }
483 $i++;
484 }
485
486 $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
487 $sum = 0;
488
489 for ($i = 2; $i < 10; $i++) {
490 $sum += $values[$i] * (int) substr($IentOfi, $i - 2, 1);
491 }
492
493 $key = 11 - $sum % 11;
494
495 if ($key == 10) {
496 $key = 1;
497 }
498 if ($key == 11) {
499 $key = 0;
500 }
501
502 $keycontrol = $key;
503
504 $sum = 0;
505
506 for ($i = 0; $i < 11; $i++) {
507 $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
508 }
509
510 $key = 11 - $sum % 11;
511
512 if ($key == 10) {
513 $key = 1;
514 }
515 if ($key == 11) {
516 $key = 0;
517 }
518
519 $keycontrol .= $key;
520 return $keycontrol;
521}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
checkIbanForAccount($account=null, $ibantocheck=null)
Check IBAN number information for a bank account.
Definition bank.lib.php:342
checkES($IentOfi, $InumCta)
Returns the key for Spanish Banks Accounts.
Definition bank.lib.php:466
account_statement_prepare_head($object, $num)
Prepare array with list of tabs.
Definition bank.lib.php:233
checkBanForAccount($account)
Check account number information for a bank account.
Definition bank.lib.php:385
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:367
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition bank.lib.php:39
various_payment_prepare_head($object)
Prepare array with list of tabs.
Definition bank.lib.php:273
checkSwiftForAccount($account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:321
bank_admin_prepare_head($object)
Prepare array with list of tabs.
Definition bank.lib.php:171
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage standard extra fields.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
getDolGlobalBool($key, $default=false)
Return a Dolibarr global constant boolean value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79