dolibarr  17.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 
37 function bank_prepare_head(Account $object)
38 {
39  global $db, $langs, $conf, $user;
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 || !empty($conf->global->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 }
132 function bank_admin_prepare_head($object)
133 {
134  global $langs, $conf, $user, $db;
135 
136  $extrafields = new ExtraFields($db);
137  $extrafields->fetch_name_optionals_label('bank_account');
138  $extrafields->fetch_name_optionals_label('bank');
139 
140  $h = 0;
141  $head = array();
142 
143  $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
144  $head[$h][1] = $langs->trans("Miscellaneous");
145  $head[$h][2] = 'general';
146  $h++;
147 
148  $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
149  $head[$h][1] = $langs->trans("CheckReceiptShort");
150  $head[$h][2] = 'checkreceipts';
151  $h++;
152 
153 
154  // Show more tabs from modules
155  // Entries must be declared in modules descriptor with line
156  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
157  // $this->tabs = array('entity:-tabname); to remove a tab
158  complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
159 
160  $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
161  $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankAccounts").')';
162  $nbExtrafields = $extrafields->attributes['bank_account']['count'];
163  if ($nbExtrafields > 0) {
164  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
165  }
166  $head[$h][2] = 'attributes';
167  $h++;
168 
169  $head[$h][0] = DOL_URL_ROOT.'/admin/bankline_extrafields.php';
170  $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankTransactions").')';
171  $nbExtrafields = $extrafields->attributes['bank']['count'];
172  if ($nbExtrafields > 0) {
173  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
174  }
175  $head[$h][2] = 'bankline_extrafields';
176  $h++;
177 
178  complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
179 
180 
181  return $head;
182 }
183 
184 
192 function account_statement_prepare_head($object, $num)
193 {
194  global $langs, $conf, $user, $db;
195  $h = 0;
196  $head = array();
197 
198  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
199  $head[$h][1] = $langs->trans("AccountStatement");
200  $head[$h][2] = 'statement';
201  $h++;
202 
203  // Attached files
204  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
205  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
206  $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
207  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
208  $nbLinks = Link::count($db, $object->element, $object->id);
209 
210  $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
211  $head[$h][1] = $langs->trans("Documents");
212  if (($nbFiles + $nbLinks) > 0) {
213  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
214  }
215  $head[$h][2] = 'document';
216  $h++;
217 
218  complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
219 
220  complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
221 
222  return $head;
223 }
224 
225 
233 {
234  global $db, $langs, $conf;
235 
236  $h = 0;
237  $head = array();
238 
239  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
240  $head[$h][1] = $langs->trans("VariousPayment");
241  $head[$h][2] = 'card';
242  $h++;
243 
244  // Show more tabs from modules
245  // Entries must be declared in modules descriptor with line
246  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
247  // $this->tabs = array('entity:-tabname); to remove a tab
248  complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
249 
250  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
251  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
252  $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
253  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
254  $nbLinks = Link::count($db, $object->element, $object->id);
255  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
256  $head[$h][1] = $langs->trans('Documents');
257  if (($nbFiles + $nbLinks) > 0) {
258  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
259  }
260  $head[$h][2] = 'documents';
261  $h++;
262 
263  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
264  $head[$h][1] = $langs->trans("Info");
265  $head[$h][2] = 'info';
266  $h++;
267 
268  complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
269 
270  return $head;
271 }
272 
279 function checkSwiftForAccount($account)
280 {
281  $swift = $account->bic;
282  if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
283  return true;
284  } else {
285  return false;
286  }
287 }
288 
295 function checkIbanForAccount(Account $account)
296 {
297  require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
298 
299  $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
300 
301  $iban = new PHP_IBAN\IBAN($ibantocheck);
302  $check = $iban->Verify();
303 
304  if ($check) {
305  return true;
306  } else {
307  return false;
308  }
309 }
310 
317 function getIbanHumanReadable(Account $account)
318 {
319  if ($account->getCountryCode() == 'FR') {
320  require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
321  $ibantoprint = preg_replace('/[^a-zA-Z0-9]/', '', empty($account->iban)?'':$account->iban);
322  $iban = new PHP_IBAN\IBAN($ibantoprint);
323  return $iban->HumanFormat();
324  }
325 
326  return $account->iban;
327 }
328 
335 function checkBanForAccount($account)
336 {
337  $country_code = $account->getCountryCode();
338 
339  // For compatibility between
340  // account of type CompanyBankAccount class (we use number, cle_rib)
341  // account of type Account class (we use num_compte, cle)
342  if (empty($account->number)) {
343  $account->number = $account->num_compte;
344  }
345  if (empty($account->cle)) {
346  $account->cle = $account->cle_rib;
347  }
348 
349  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);
350 
351  if ($country_code == 'FR') { // France rules
352  $coef = array(62, 34, 3);
353  // Concatenation des differents codes.
354  $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
355  // On remplace les eventuelles lettres par des chiffres.
356  //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
357  $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
358  // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
359  // Multiplication de chaque groupe par les coef du tableau
360 
361  for ($i = 0, $s = 0; $i < 3; $i++) {
362  $code = substr($rib, 7 * $i, 7);
363  $s += (0 + (int) $code) * $coef[$i];
364  }
365  // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
366  $cle_rib = 97 - ($s % 97);
367  if ($cle_rib == $account->cle) {
368  return true;
369  }
370  return false;
371  }
372 
373  if ($country_code == 'BE') { // Belgium rules
374  }
375 
376  if ($country_code == 'ES') { // Spanish rules
377  $CCC = strtolower(trim($account->number));
378  $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
379  $cle_rib = strtolower(checkES($rib, $CCC));
380  if ($cle_rib == strtolower($account->cle)) {
381  return true;
382  }
383  return false;
384  }
385  if ($country_code == 'AU') { // Australian
386  if (strlen($account->code_banque) > 7) {
387  return false; // Sould be 6 but can be 123-456
388  } elseif (strlen($account->code_banque) < 6) {
389  return false; // Sould be 6
390  } else {
391  return true;
392  }
393  }
394 
395  // No particular rule
396  // If account is CompanyBankAccount class, we use number
397  // If account is Account class, we use num_compte
398  if (empty($account->number)) {
399  return false;
400  }
401 
402  return true;
403 }
404 
405 
406 
414 function checkES($IentOfi, $InumCta)
415 {
416  if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
417  $keycontrol = "";
418  return $keycontrol;
419  }
420 
421  $ccc = $IentOfi.$InumCta;
422  $numbers = "1234567890";
423 
424  $i = 0;
425 
426  while ($i <= strlen($ccc) - 1) {
427  if (strpos($numbers, substr($ccc, $i, 1)) === false) {
428  $keycontrol = "";
429  return $keycontrol;
430  }
431  $i++;
432  }
433 
434  $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
435  $sum = 0;
436 
437  for ($i = 2; $i < 10; $i++) {
438  $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
439  }
440 
441  $key = 11 - $sum % 11;
442 
443  if ($key == 10) {
444  $key = 1;
445  }
446  if ($key == 11) {
447  $key = 0;
448  }
449 
450  $keycontrol = $key;
451 
452  $sum = 0;
453 
454  for ($i = 0; $i < 11; $i++) {
455  $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
456  }
457 
458  $key = 11 - $sum % 11;
459 
460  if ($key == 10) {
461  $key = 1;
462  }
463  if ($key == 11) {
464  $key = 0;
465  }
466 
467  $keycontrol .= $key;
468  return $keycontrol;
469 }
checkES($IentOfi, $InumCta)
Returns the key for Spanish Banks Accounts.
Definition: bank.lib.php:414
account_statement_prepare_head($object, $num)
Prepare array with list of tabs.
Definition: bank.lib.php:192
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:295
checkBanForAccount($account)
Check account number informations for a bank account.
Definition: bank.lib.php:335
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition: bank.lib.php:317
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:279
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:232
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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:61
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).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.