dolibarr  16.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  *
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;
135  $h = 0;
136  $head = array();
137 
138  $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
139  $head[$h][1] = $langs->trans("Miscellaneous");
140  $head[$h][2] = 'general';
141  $h++;
142 
143  $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
144  $head[$h][1] = $langs->trans("CheckReceiptShort");
145  $head[$h][2] = 'checkreceipts';
146  $h++;
147 
148 
149  // Show more tabs from modules
150  // Entries must be declared in modules descriptor with line
151  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
152  // $this->tabs = array('entity:-tabname); to remove a tab
153  complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
154 
155  $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
156  $head[$h][1] = $langs->trans("ExtraFields");
157  $head[$h][2] = 'attributes';
158  $h++;
159 
160  complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
161 
162 
163  return $head;
164 }
165 
166 
174 function account_statement_prepare_head($object, $num)
175 {
176  global $langs, $conf, $user, $db;
177  $h = 0;
178  $head = array();
179 
180  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
181  $head[$h][1] = $langs->trans("AccountStatement");
182  $head[$h][2] = 'statement';
183  $h++;
184 
185  // Attached files
186  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
187  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
188  $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
189  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
190  $nbLinks = Link::count($db, $object->element, $object->id);
191 
192  $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
193  $head[$h][1] = $langs->trans("Documents");
194  if (($nbFiles + $nbLinks) > 0) {
195  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
196  }
197  $head[$h][2] = 'document';
198  $h++;
199 
200  complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
201 
202  complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
203 
204  return $head;
205 }
206 
207 
215 {
216  global $db, $langs, $conf;
217 
218  $h = 0;
219  $head = array();
220 
221  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
222  $head[$h][1] = $langs->trans("VariousPayment");
223  $head[$h][2] = 'card';
224  $h++;
225 
226  // Show more tabs from modules
227  // Entries must be declared in modules descriptor with line
228  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
229  // $this->tabs = array('entity:-tabname); to remove a tab
230  complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
231 
232  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
233  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
234  $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
235  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
236  $nbLinks = Link::count($db, $object->element, $object->id);
237  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
238  $head[$h][1] = $langs->trans('Documents');
239  if (($nbFiles + $nbLinks) > 0) {
240  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
241  }
242  $head[$h][2] = 'documents';
243  $h++;
244 
245  $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
246  $head[$h][1] = $langs->trans("Info");
247  $head[$h][2] = 'info';
248  $h++;
249 
250  complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
251 
252  return $head;
253 }
254 
261 function checkSwiftForAccount($account)
262 {
263  $swift = $account->bic;
264  if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
265  return true;
266  } else {
267  return false;
268  }
269 }
270 
277 function checkIbanForAccount(Account $account)
278 {
279  require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
280 
281  $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
282 
283  $iban = new PHP_IBAN\IBAN($ibantocheck);
284  $check = $iban->Verify();
285 
286  if ($check) {
287  return true;
288  } else {
289  return false;
290  }
291 }
292 
299 function checkBanForAccount($account)
300 {
301  $country_code = $account->getCountryCode();
302 
303  // For compatibility between
304  // account of type CompanyBankAccount class (we use number, cle_rib)
305  // account of type Account class (we use num_compte, cle)
306  if (empty($account->number)) {
307  $account->number = $account->num_compte;
308  }
309  if (empty($account->cle)) {
310  $account->cle = $account->cle_rib;
311  }
312 
313  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);
314 
315  if ($country_code == 'FR') { // France rules
316  $coef = array(62, 34, 3);
317  // Concatenation des differents codes.
318  $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
319  // On remplace les eventuelles lettres par des chiffres.
320  //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
321  $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
322  // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
323  // Multiplication de chaque groupe par les coef du tableau
324 
325  for ($i = 0, $s = 0; $i < 3; $i++) {
326  $code = substr($rib, 7 * $i, 7);
327  $s += (0 + (int) $code) * $coef[$i];
328  }
329  // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
330  $cle_rib = 97 - ($s % 97);
331  if ($cle_rib == $account->cle) {
332  return true;
333  }
334  return false;
335  }
336 
337  if ($country_code == 'BE') { // Belgium rules
338  }
339 
340  if ($country_code == 'ES') { // Spanish rules
341  $CCC = strtolower(trim($account->number));
342  $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
343  $cle_rib = strtolower(checkES($rib, $CCC));
344  if ($cle_rib == strtolower($account->cle)) {
345  return true;
346  }
347  return false;
348  }
349  if ($country_code == 'AU') { // Australian
350  if (strlen($account->code_banque) > 7) {
351  return false; // Sould be 6 but can be 123-456
352  } elseif (strlen($account->code_banque) < 6) {
353  return false; // Sould be 6
354  } else {
355  return true;
356  }
357  }
358 
359  // No particular rule
360  // If account is CompanyBankAccount class, we use number
361  // If account is Account class, we use num_compte
362  if (empty($account->number)) {
363  return false;
364  }
365 
366  return true;
367 }
368 
369 
370 
378 function checkES($IentOfi, $InumCta)
379 {
380  if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
381  $keycontrol = "";
382  return $keycontrol;
383  }
384 
385  $ccc = $IentOfi.$InumCta;
386  $numbers = "1234567890";
387 
388  $i = 0;
389 
390  while ($i <= strlen($ccc) - 1) {
391  if (strpos($numbers, substr($ccc, $i, 1)) === false) {
392  $keycontrol = "";
393  return $keycontrol;
394  }
395  $i++;
396  }
397 
398  $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
399  $sum = 0;
400 
401  for ($i = 2; $i < 10; $i++) {
402  $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
403  }
404 
405  $key = 11 - $sum % 11;
406 
407  if ($key == 10) {
408  $key = 1;
409  }
410  if ($key == 11) {
411  $key = 0;
412  }
413 
414  $keycontrol = $key;
415 
416  $sum = 0;
417 
418  for ($i = 0; $i < 11; $i++) {
419  $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
420  }
421 
422  $key = 11 - $sum % 11;
423 
424  if ($key == 10) {
425  $key = 1;
426  }
427  if ($key == 11) {
428  $key = 0;
429  }
430 
431  $keycontrol .= $key;
432  return $keycontrol;
433 }
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
bank_admin_prepare_head
bank_admin_prepare_head($object)
Prepare array with list of tabs.
Definition: bank.lib.php:132
checkIbanForAccount
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:277
checkSwiftForAccount
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:261
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
complete_head_from_modules
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add')
Complete or removed entries into a head array (used to build tabs).
Definition: functions.lib.php:9038
account_statement_prepare_head
account_statement_prepare_head($object, $num)
Prepare array with list of tabs.
Definition: bank.lib.php:174
checkBanForAccount
checkBanForAccount($account)
Check account number informations for a bank account.
Definition: bank.lib.php:299
Account\TYPE_CASH
const TYPE_CASH
Cash account.
Definition: account.class.php:346
checkES
checkES($IentOfi, $InumCta)
Returns the key for Spanish Banks Accounts.
Definition: bank.lib.php:378
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
bank_prepare_head
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition: bank.lib.php:37
various_payment_prepare_head
various_payment_prepare_head($object)
Prepare array with list of tabs.
Definition: bank.lib.php:214
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Account
Class to manage bank accounts.
Definition: account.class.php:38