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