dolibarr 22.0.5
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("Reports");
137 $head[$h][2] = 'annual';
138 $h++;
139
140 $head[$h][0] = DOL_URL_ROOT."/compta/bank/treso.php?account=".$object->id;
141 $head[$h][1] = $langs->trans("PlannedTransactions");
142 $head[$h][2] = 'cash';
143 $h++;
144
145 // Show more tabs from modules
146 // Entries must be declared in modules descriptor with line
147 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
148 // $this->tabs = array('entity:-tabname); to remove a tab
149 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
150
151 /*$head[$h][0] = DOL_URL_ROOT . "/compta/bank/info.php?id=" . $object->id;
152 $head[$h][1] = $langs->trans("Info");
153 $head[$h][2] = 'info';
154 $h++;*/
155
156 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
157
158 return $head;
159}
160
168{
169 global $db, $langs, $conf, $user;
170
171 $h = 0;
172 $head = array();
173
174 $head[$h][0] = DOL_URL_ROOT."/compta/bank/annuel.php?account=".$object->id;
175 $head[$h][1] = $langs->trans("IOMonthlyReporting");
176 $head[$h][2] = 'annual';
177 $h++;
178
179 $head[$h][0] = DOL_URL_ROOT."/compta/bank/graph.php?account=".$object->id;
180 $head[$h][1] = $langs->trans("Graph");
181 $head[$h][2] = 'graph';
182 $h++;
183
184 return $head;
185}
193{
194 global $langs, $conf, $db;
195
196 $langs->loadLangs(array("compta"));
197
198 $extrafields = new ExtraFields($db);
199 $extrafields->fetch_name_optionals_label('bank_account');
200 $extrafields->fetch_name_optionals_label('bank');
201
202 $h = 0;
203 $head = array();
204
205 $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
206 $head[$h][1] = $langs->trans("Miscellaneous");
207 $head[$h][2] = 'general';
208 $h++;
209
210 $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
211 $head[$h][1] = $langs->trans("CheckReceiptShort");
212 $head[$h][2] = 'checkreceipts';
213 $h++;
214
215
216 // Show more tabs from modules
217 // Entries must be declared in modules descriptor with line
218 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
219 // $this->tabs = array('entity:-tabname); to remove a tab
220 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
221
222 $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
223 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankAccounts").')';
224 $nbExtrafields = $extrafields->attributes['bank_account']['count'];
225 if ($nbExtrafields > 0) {
226 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
227 }
228 $head[$h][2] = 'attributes';
229 $h++;
230
231 $head[$h][0] = DOL_URL_ROOT.'/admin/bankline_extrafields.php';
232 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankTransactions").')';
233 $nbExtrafields = $extrafields->attributes['bank']['count'];
234 if ($nbExtrafields > 0) {
235 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
236 }
237 $head[$h][2] = 'bankline_extrafields';
238 $h++;
239
240 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
241
242
243 return $head;
244}
245
246
255{
256 global $langs, $conf, $db;
257 $h = 0;
258 $head = array();
259
260 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
261 $head[$h][1] = $langs->trans("AccountStatement");
262 $head[$h][2] = 'statement';
263 $h++;
264
265 // Attached files
266 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
267 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
268 $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
269 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
270 $nbLinks = Link::count($db, $object->element, $object->id);
271
272 $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
273 $head[$h][1] = $langs->trans("Documents");
274 if (($nbFiles + $nbLinks) > 0) {
275 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
276 }
277 $head[$h][2] = 'document';
278 $h++;
279
280 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
281
282 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
283
284 return $head;
285}
286
287
295{
296 global $db, $langs, $conf;
297
298 $h = 0;
299 $head = array();
300
301 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
302 $head[$h][1] = $langs->trans("VariousPayment");
303 $head[$h][2] = 'card';
304 $h++;
305
306 // Show more tabs from modules
307 // Entries must be declared in modules descriptor with line
308 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
309 // $this->tabs = array('entity:-tabname); to remove a tab
310 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
311
312 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
313 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
314 $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
315 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
316 $nbLinks = Link::count($db, $object->element, $object->id);
317 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
318 $head[$h][1] = $langs->trans('Documents');
319 if (($nbFiles + $nbLinks) > 0) {
320 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
321 }
322 $head[$h][2] = 'documents';
323 $h++;
324
325 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
326 $head[$h][1] = $langs->trans("Info");
327 $head[$h][2] = 'info';
328 $h++;
329
330 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
331
332 return $head;
333}
334
342function checkSwiftForAccount($account = null, $swift = null)
343{
344 if ($account == null && $swift == null) {
345 return false;
346 } elseif ($swift == null) {
347 $swift = $account->bic;
348 }
349 if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
350 return true;
351 } else {
352 return false;
353 }
354}
355
363function checkIbanForAccount($account = null, $ibantocheck = null)
364{
365 if ($account == null && $ibantocheck == null) {
366 return false;
367 } elseif ($ibantocheck == null) {
368 $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
369 }
370 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
371
372 $iban = new PHP_IBAN\IBAN($ibantocheck);
373 $check = $iban->Verify();
374
375 if ($check) {
376 return true;
377 } else {
378 return false;
379 }
380}
381
389{
390 if ($account->getCountryCode() == 'FR') {
391 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
392 $ibantoprint = preg_replace('/[^a-zA-Z0-9]/', '', empty($account->iban) ? '' : $account->iban);
393 $iban = new PHP_IBAN\IBAN($ibantoprint);
394 return $iban->HumanFormat();
395 }
396
397 return $account->iban;
398}
399
406function checkBanForAccount($account)
407{
408 $country_code = $account->getCountryCode();
409
410 // For compatibility between
411 // account of type CompanyBankAccount class (we use number, cle_rib)
412 // account of type Account class (we use num_compte, cle)
413 if (empty($account->number)) {
414 $account->number = $account->num_compte; // @phan-suppress-current-line PhanUndeclaredProperty
415 }
416 if (empty($account->cle)) {
417 $account->cle = $account->cle_rib;
418 }
419
420 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);
421
422 if ($country_code == 'FR') { // France rules
423 $coef = array(62, 34, 3);
424 // Concatenate the code parts
425 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
426 // On replace les eventuelles lettres par des chiffres.
427 //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
428 $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
429 // Separation du rib en 3 groups de 7 + 1 group de 2.
430 // Multiplication de chaque group par les coef du tableau
431
432 for ($i = 0, $s = 0; $i < 3; $i++) {
433 $code = substr($rib, 7 * $i, 7);
434 $s += ((int) $code) * $coef[$i];
435 }
436 // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
437 $cle_rib = 97 - ($s % 97);
438 if ($cle_rib == $account->cle) {
439 return true;
440 }
441 return false;
442 }
443
444 /*
445 if ($country_code == 'BE') { // Belgian rules
446 }
447 */
448
449 if ($country_code == 'ES') { // Spanish rules
450 $CCC = strtolower(trim($account->number));
451 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
452 $cle_rib = strtolower(checkES($rib, $CCC));
453 if ($cle_rib == strtolower($account->cle)) {
454 return true;
455 }
456 return false;
457 }
458 if ($country_code == 'AU') { // Australian
459 if (strlen($account->code_banque) > 7) {
460 return false; // Should be 6 but can be 123-456
461 } elseif (strlen($account->code_banque) < 6) {
462 return false; // Should be 6
463 } else {
464 return true;
465 }
466 }
467
468 // No particular rule
469 // If account is CompanyBankAccount class, we use number
470 // If account is Account class, we use num_compte
471 if (empty($account->number)) {
472 return false;
473 }
474
475 return true;
476}
477
478
479
487function checkES($IentOfi, $InumCta)
488{
489 if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
490 $keycontrol = "";
491 return $keycontrol;
492 }
493
494 $ccc = $IentOfi.$InumCta;
495 $numbers = "1234567890";
496
497 $i = 0;
498
499 while ($i <= strlen($ccc) - 1) {
500 if (strpos($numbers, substr($ccc, $i, 1)) === false) {
501 $keycontrol = "";
502 return $keycontrol;
503 }
504 $i++;
505 }
506
507 $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
508 $sum = 0;
509
510 for ($i = 2; $i < 10; $i++) {
511 $sum += $values[$i] * (int) substr($IentOfi, $i - 2, 1);
512 }
513
514 $key = 11 - $sum % 11;
515
516 if ($key == 10) {
517 $key = 1;
518 }
519 if ($key == 11) {
520 $key = 0;
521 }
522
523 $keycontrol = $key;
524
525 $sum = 0;
526
527 for ($i = 0; $i < 11; $i++) {
528 $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
529 }
530
531 $key = 11 - $sum % 11;
532
533 if ($key == 10) {
534 $key = 1;
535 }
536 if ($key == 11) {
537 $key = 0;
538 }
539
540 $keycontrol .= $key;
541 return $keycontrol;
542}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
checkIbanForAccount($account=null, $ibantocheck=null)
Check IBAN number information for a bank account.
Definition bank.lib.php:363
checkES($IentOfi, $InumCta)
Returns the key for Spanish Banks Accounts.
Definition bank.lib.php:487
bank_report_prepare_head(Account $object)
Prepare array with list of tabs for bank report.
Definition bank.lib.php:167
account_statement_prepare_head($object, $num)
Prepare array with list of tabs.
Definition bank.lib.php:254
checkBanForAccount($account)
Check account number information for a bank account.
Definition bank.lib.php:406
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:388
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:294
checkSwiftForAccount($account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:342
bank_admin_prepare_head($object)
Prepare array with list of tabs.
Definition bank.lib.php:192
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.
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).
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
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