dolibarr 19.0.4
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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
37function bank_prepare_head(Account $object)
38{
39 global $db, $langs, $conf;
40 $h = 0;
41 $head = array();
42
43 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/card.php?id='.$object->id;
44 $head[$h][1] = $langs->trans("BankAccount");
45 $head[$h][2] = 'bankname';
46 $h++;
47
48 $head[$h][0] = DOL_URL_ROOT."/compta/bank/bankentries_list.php?id=".$object->id;
49 $head[$h][1] = $langs->trans("BankTransactions");
50 $head[$h][2] = 'journal';
51 $h++;
52
53 // if ($conf->global->MAIN_FEATURES_LEVEL >= 1)
54 // {
55 $head[$h][0] = DOL_URL_ROOT."/compta/bank/treso.php?account=".$object->id;
56 $head[$h][1] = $langs->trans("PlannedTransactions");
57 $head[$h][2] = 'cash';
58 $h++;
59 // }
60
61 $head[$h][0] = DOL_URL_ROOT."/compta/bank/annuel.php?account=".$object->id;
62 $head[$h][1] = $langs->trans("IOMonthlyReporting");
63 $head[$h][2] = 'annual';
64 $h++;
65
66 $head[$h][0] = DOL_URL_ROOT."/compta/bank/graph.php?account=".$object->id;
67 $head[$h][1] = $langs->trans("Graph");
68 $head[$h][2] = 'graph';
69 $h++;
70
71 if ($object->courant != Account::TYPE_CASH || getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
72 $nbReceipts = 0;
73
74 // List of all standing receipts
75 $sql = "SELECT COUNT(DISTINCT(b.num_releve)) as nb";
76 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
77 $sql .= " WHERE b.fk_account = ".((int) $object->id);
78
79 $resql = $db->query($sql);
80 if ($resql) {
81 $obj = $db->fetch_object($resql);
82 if ($obj) {
83 $nbReceipts = $obj->nb;
84 }
85 $db->free($resql);
86 }
87
88 $head[$h][0] = DOL_URL_ROOT."/compta/bank/releve.php?account=".((int) $object->id);
89 $head[$h][1] = $langs->trans("AccountStatements");
90 if (($nbReceipts) > 0) {
91 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbReceipts).'</span>';
92 }
93 $head[$h][2] = 'statement';
94 $h++;
95 }
96
97 // Attached files
98 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
99 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
100 $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
101 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
102 $nbLinks = Link::count($db, $object->element, $object->id);
103 $head[$h][0] = DOL_URL_ROOT."/compta/bank/document.php?account=".$object->id;
104 $head[$h][1] = $langs->trans("Documents");
105 if (($nbFiles + $nbLinks) > 0) {
106 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
107 }
108 $head[$h][2] = 'document';
109 $h++;
110
111 // Show more tabs from modules
112 // Entries must be declared in modules descriptor with line
113 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
114 // $this->tabs = array('entity:-tabname); to remove a tab
115 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
116
117 /*$head[$h][0] = DOL_URL_ROOT . "/compta/bank/info.php?id=" . $object->id;
118 $head[$h][1] = $langs->trans("Info");
119 $head[$h][2] = 'info';
120 $h++;*/
121
122 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
123
124 return $head;
125}
132function bank_admin_prepare_head($object)
133{
134 global $langs, $conf, $db;
135
136 $langs->loadLangs(array("compta"));
137
138 $extrafields = new ExtraFields($db);
139 $extrafields->fetch_name_optionals_label('bank_account');
140 $extrafields->fetch_name_optionals_label('bank');
141
142 $h = 0;
143 $head = array();
144
145 $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
146 $head[$h][1] = $langs->trans("Miscellaneous");
147 $head[$h][2] = 'general';
148 $h++;
149
150 $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
151 $head[$h][1] = $langs->trans("CheckReceiptShort");
152 $head[$h][2] = 'checkreceipts';
153 $h++;
154
155
156 // Show more tabs from modules
157 // Entries must be declared in modules descriptor with line
158 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
159 // $this->tabs = array('entity:-tabname); to remove a tab
160 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
161
162 $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
163 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankAccounts").')';
164 $nbExtrafields = $extrafields->attributes['bank_account']['count'];
165 if ($nbExtrafields > 0) {
166 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
167 }
168 $head[$h][2] = 'attributes';
169 $h++;
170
171 $head[$h][0] = DOL_URL_ROOT.'/admin/bankline_extrafields.php';
172 $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankTransactions").')';
173 $nbExtrafields = $extrafields->attributes['bank']['count'];
174 if ($nbExtrafields > 0) {
175 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
176 }
177 $head[$h][2] = 'bankline_extrafields';
178 $h++;
179
180 complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
181
182
183 return $head;
184}
185
186
194function account_statement_prepare_head($object, $num)
195{
196 global $langs, $conf, $db;
197 $h = 0;
198 $head = array();
199
200 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
201 $head[$h][1] = $langs->trans("AccountStatement");
202 $head[$h][2] = 'statement';
203 $h++;
204
205 // Attached files
206 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
207 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
208 $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
209 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
210 $nbLinks = Link::count($db, $object->element, $object->id);
211
212 $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
213 $head[$h][1] = $langs->trans("Documents");
214 if (($nbFiles + $nbLinks) > 0) {
215 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
216 }
217 $head[$h][2] = 'document';
218 $h++;
219
220 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
221
222 complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
223
224 return $head;
225}
226
227
235{
236 global $db, $langs, $conf;
237
238 $h = 0;
239 $head = array();
240
241 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
242 $head[$h][1] = $langs->trans("VariousPayment");
243 $head[$h][2] = 'card';
244 $h++;
245
246 // Show more tabs from modules
247 // Entries must be declared in modules descriptor with line
248 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
249 // $this->tabs = array('entity:-tabname); to remove a tab
250 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
251
252 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
253 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
254 $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
255 $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
256 $nbLinks = Link::count($db, $object->element, $object->id);
257 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
258 $head[$h][1] = $langs->trans('Documents');
259 if (($nbFiles + $nbLinks) > 0) {
260 $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
261 }
262 $head[$h][2] = 'documents';
263 $h++;
264
265 $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
266 $head[$h][1] = $langs->trans("Info");
267 $head[$h][2] = 'info';
268 $h++;
269
270 complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
271
272 return $head;
273}
274
282function checkSwiftForAccount(Account $account = null, $swift = null)
283{
284 if ($account == null && $swift == null) {
285 return false;
286 } elseif ($swift == null) {
287 $swift = $account->bic;
288 }
289 if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
290 return true;
291 } else {
292 return false;
293 }
294}
295
303function checkIbanForAccount(Account $account = null, $ibantocheck = null)
304{
305 if ($account == null && $ibantocheck == null) {
306 return false;
307 } elseif ($ibantocheck == null) {
308 $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
309 }
310 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
311
312 $iban = new PHP_IBAN\IBAN($ibantocheck);
313 $check = $iban->Verify();
314
315 if ($check) {
316 return true;
317 } else {
318 return false;
319 }
320}
321
329{
330 if ($account->getCountryCode() == 'FR') {
331 require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
332 $ibantoprint = preg_replace('/[^a-zA-Z0-9]/', '', empty($account->iban) ? '' : $account->iban);
333 $iban = new PHP_IBAN\IBAN($ibantoprint);
334 return $iban->HumanFormat();
335 }
336
337 return $account->iban;
338}
339
346function checkBanForAccount($account)
347{
348 $country_code = $account->getCountryCode();
349
350 // For compatibility between
351 // account of type CompanyBankAccount class (we use number, cle_rib)
352 // account of type Account class (we use num_compte, cle)
353 if (empty($account->number)) {
354 $account->number = $account->num_compte;
355 }
356 if (empty($account->cle)) {
357 $account->cle = $account->cle_rib;
358 }
359
360 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);
361
362 if ($country_code == 'FR') { // France rules
363 $coef = array(62, 34, 3);
364 // Concatenation des differents codes.
365 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
366 // On remplace les eventuelles lettres par des chiffres.
367 //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
368 $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
369 // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
370 // Multiplication de chaque groupe par les coef du tableau
371
372 for ($i = 0, $s = 0; $i < 3; $i++) {
373 $code = substr($rib, 7 * $i, 7);
374 $s += ((int) $code) * $coef[$i];
375 }
376 // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
377 $cle_rib = 97 - ($s % 97);
378 if ($cle_rib == $account->cle) {
379 return true;
380 }
381 return false;
382 }
383
384 if ($country_code == 'BE') { // Belgium rules
385 }
386
387 if ($country_code == 'ES') { // Spanish rules
388 $CCC = strtolower(trim($account->number));
389 $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
390 $cle_rib = strtolower(checkES($rib, $CCC));
391 if ($cle_rib == strtolower($account->cle)) {
392 return true;
393 }
394 return false;
395 }
396 if ($country_code == 'AU') { // Australian
397 if (strlen($account->code_banque) > 7) {
398 return false; // Sould be 6 but can be 123-456
399 } elseif (strlen($account->code_banque) < 6) {
400 return false; // Sould be 6
401 } else {
402 return true;
403 }
404 }
405
406 // No particular rule
407 // If account is CompanyBankAccount class, we use number
408 // If account is Account class, we use num_compte
409 if (empty($account->number)) {
410 return false;
411 }
412
413 return true;
414}
415
416
417
425function checkES($IentOfi, $InumCta)
426{
427 if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
428 $keycontrol = "";
429 return $keycontrol;
430 }
431
432 $ccc = $IentOfi.$InumCta;
433 $numbers = "1234567890";
434
435 $i = 0;
436
437 while ($i <= strlen($ccc) - 1) {
438 if (strpos($numbers, substr($ccc, $i, 1)) === false) {
439 $keycontrol = "";
440 return $keycontrol;
441 }
442 $i++;
443 }
444
445 $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
446 $sum = 0;
447
448 for ($i = 2; $i < 10; $i++) {
449 $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
450 }
451
452 $key = 11 - $sum % 11;
453
454 if ($key == 10) {
455 $key = 1;
456 }
457 if ($key == 11) {
458 $key = 0;
459 }
460
461 $keycontrol = $key;
462
463 $sum = 0;
464
465 for ($i = 0; $i < 11; $i++) {
466 $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
467 }
468
469 $key = 11 - $sum % 11;
470
471 if ($key == 10) {
472 $key = 1;
473 }
474 if ($key == 11) {
475 $key = 0;
476 }
477
478 $keycontrol .= $key;
479 return $keycontrol;
480}
checkES($IentOfi, $InumCta)
Returns the key for Spanish Banks Accounts.
Definition bank.lib.php:425
checkSwiftForAccount(Account $account=null, $swift=null)
Check SWIFT informations for a bank account.
Definition bank.lib.php:282
checkIbanForAccount(Account $account=null, $ibantocheck=null)
Check IBAN number informations for a bank account.
Definition bank.lib.php:303
account_statement_prepare_head($object, $num)
Prepare array with list of tabs.
Definition bank.lib.php:194
checkBanForAccount($account)
Check account number informations for a bank account.
Definition bank.lib.php:346
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:328
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition bank.lib.php:37
various_payment_prepare_head($object)
Prepare array with list of tabs.
Definition bank.lib.php:234
bank_admin_prepare_head($object)
Prepare array with list of tabs.
Definition bank.lib.php:132
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage standard extra fields.
dol_dir_list($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:62
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.