bank.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /**
  24. * \file htdocs/core/lib/bank.lib.php
  25. * \ingroup bank
  26. * \brief Ensemble de fonctions de base pour le module banque
  27. */
  28. /**
  29. * Prepare array with list of tabs
  30. *
  31. * @param Account $object Object related to tabs
  32. * @return array Array of tabs to show
  33. */
  34. function bank_prepare_head(Account $object)
  35. {
  36. global $db, $langs, $conf, $user;
  37. $h = 0;
  38. $head = array();
  39. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/card.php?id='.$object->id;
  40. $head[$h][1] = $langs->trans("BankAccount");
  41. $head[$h][2] = 'bankname';
  42. $h++;
  43. $head[$h][0] = DOL_URL_ROOT."/compta/bank/bankentries_list.php?id=".$object->id;
  44. $head[$h][1] = $langs->trans("BankTransactions");
  45. $head[$h][2] = 'journal';
  46. $h++;
  47. // if ($conf->global->MAIN_FEATURES_LEVEL >= 1)
  48. // {
  49. $head[$h][0] = DOL_URL_ROOT."/compta/bank/treso.php?account=".$object->id;
  50. $head[$h][1] = $langs->trans("PlannedTransactions");
  51. $head[$h][2] = 'cash';
  52. $h++;
  53. // }
  54. $head[$h][0] = DOL_URL_ROOT."/compta/bank/annuel.php?account=".$object->id;
  55. $head[$h][1] = $langs->trans("IOMonthlyReporting");
  56. $head[$h][2] = 'annual';
  57. $h++;
  58. $head[$h][0] = DOL_URL_ROOT."/compta/bank/graph.php?account=".$object->id;
  59. $head[$h][1] = $langs->trans("Graph");
  60. $head[$h][2] = 'graph';
  61. $h++;
  62. if ($object->courant != Account::TYPE_CASH || !empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
  63. $nbReceipts = 0;
  64. // List of all standing receipts
  65. $sql = "SELECT COUNT(DISTINCT(b.num_releve)) as nb";
  66. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  67. $sql .= " WHERE b.fk_account = ".((int) $object->id);
  68. $resql = $db->query($sql);
  69. if ($resql) {
  70. $obj = $db->fetch_object($resql);
  71. if ($obj) {
  72. $nbReceipts = $obj->nb;
  73. }
  74. $db->free($resql);
  75. }
  76. $head[$h][0] = DOL_URL_ROOT."/compta/bank/releve.php?account=".((int) $object->id);
  77. $head[$h][1] = $langs->trans("AccountStatements");
  78. if (($nbReceipts) > 0) {
  79. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbReceipts).'</span>';
  80. }
  81. $head[$h][2] = 'statement';
  82. $h++;
  83. }
  84. // Attached files
  85. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  86. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  87. $upload_dir = $conf->bank->dir_output."/".dol_sanitizeFileName($object->ref);
  88. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  89. $nbLinks = Link::count($db, $object->element, $object->id);
  90. $head[$h][0] = DOL_URL_ROOT."/compta/bank/document.php?account=".$object->id;
  91. $head[$h][1] = $langs->trans("Documents");
  92. if (($nbFiles + $nbLinks) > 0) {
  93. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  94. }
  95. $head[$h][2] = 'document';
  96. $h++;
  97. // Show more tabs from modules
  98. // Entries must be declared in modules descriptor with line
  99. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  100. // $this->tabs = array('entity:-tabname); to remove a tab
  101. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
  102. /*$head[$h][0] = DOL_URL_ROOT . "/compta/bank/info.php?id=" . $object->id;
  103. $head[$h][1] = $langs->trans("Info");
  104. $head[$h][2] = 'info';
  105. $h++;*/
  106. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
  107. return $head;
  108. }
  109. /**
  110. * Prepare array with list of tabs
  111. *
  112. * @param Object $object Object related to tabs
  113. * @return array Array of tabs to shoc
  114. */
  115. function bank_admin_prepare_head($object)
  116. {
  117. global $langs, $conf, $user, $db;
  118. $extrafields = new ExtraFields($db);
  119. $extrafields->fetch_name_optionals_label('bank_account');
  120. $extrafields->fetch_name_optionals_label('bank');
  121. $h = 0;
  122. $head = array();
  123. $head[$h][0] = DOL_URL_ROOT.'/admin/bank.php';
  124. $head[$h][1] = $langs->trans("Miscellaneous");
  125. $head[$h][2] = 'general';
  126. $h++;
  127. $head[$h][0] = DOL_URL_ROOT.'/admin/chequereceipts.php';
  128. $head[$h][1] = $langs->trans("CheckReceiptShort");
  129. $head[$h][2] = 'checkreceipts';
  130. $h++;
  131. // Show more tabs from modules
  132. // Entries must be declared in modules descriptor with line
  133. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  134. // $this->tabs = array('entity:-tabname); to remove a tab
  135. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
  136. $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
  137. $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankAccounts").')';
  138. $nbExtrafields = $extrafields->attributes['bank_account']['count'];
  139. if ($nbExtrafields > 0) {
  140. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  141. }
  142. $head[$h][2] = 'attributes';
  143. $h++;
  144. $head[$h][0] = DOL_URL_ROOT.'/admin/bankline_extrafields.php';
  145. $head[$h][1] = $langs->trans("ExtraFields").' ('.$langs->trans("BankTransactions").')';
  146. $nbExtrafields = $extrafields->attributes['bank']['count'];
  147. if ($nbExtrafields > 0) {
  148. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  149. }
  150. $head[$h][2] = 'bankline_extrafields';
  151. $h++;
  152. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
  153. return $head;
  154. }
  155. /**
  156. * Prepare array with list of tabs
  157. *
  158. * @param Object $object Object related to tabs
  159. * @param Object $num val to account statement
  160. * @return array Array of tabs to shoc
  161. */
  162. function account_statement_prepare_head($object, $num)
  163. {
  164. global $langs, $conf, $user, $db;
  165. $h = 0;
  166. $head = array();
  167. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$object->id.'&num='.$num;
  168. $head[$h][1] = $langs->trans("AccountStatement");
  169. $head[$h][2] = 'statement';
  170. $h++;
  171. // Attached files
  172. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  173. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  174. $upload_dir = $conf->bank->dir_output."/".$object->id.'/statement/'.dol_sanitizeFileName($num);
  175. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  176. $nbLinks = Link::count($db, $object->element, $object->id);
  177. $head[$h][0] = DOL_URL_ROOT."/compta/bank/account_statement_document.php?account=".$object->id."&num=".$num;
  178. $head[$h][1] = $langs->trans("Documents");
  179. if (($nbFiles + $nbLinks) > 0) {
  180. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  181. }
  182. $head[$h][2] = 'document';
  183. $h++;
  184. complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement');
  185. complete_head_from_modules($conf, $langs, $object, $head, $h, 'account_statement', 'remove');
  186. return $head;
  187. }
  188. /**
  189. * Prepare array with list of tabs
  190. *
  191. * @param Object $object Object related to tabs
  192. * @return array Array of tabs to shoc
  193. */
  194. function various_payment_prepare_head($object)
  195. {
  196. global $db, $langs, $conf;
  197. $h = 0;
  198. $head = array();
  199. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
  200. $head[$h][1] = $langs->trans("VariousPayment");
  201. $head[$h][2] = 'card';
  202. $h++;
  203. // Show more tabs from modules
  204. // Entries must be declared in modules descriptor with line
  205. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  206. // $this->tabs = array('entity:-tabname); to remove a tab
  207. complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment');
  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."/".dol_sanitizeFileName($object->ref);
  211. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  212. $nbLinks = Link::count($db, $object->element, $object->id);
  213. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
  214. $head[$h][1] = $langs->trans('Documents');
  215. if (($nbFiles + $nbLinks) > 0) {
  216. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  217. }
  218. $head[$h][2] = 'documents';
  219. $h++;
  220. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
  221. $head[$h][1] = $langs->trans("Info");
  222. $head[$h][2] = 'info';
  223. $h++;
  224. complete_head_from_modules($conf, $langs, $object, $head, $h, 'various_payment', 'remove');
  225. return $head;
  226. }
  227. /**
  228. * Check SWIFT informations for a bank account
  229. *
  230. * @param Account $account A bank account
  231. * @return boolean True if informations are valid, false otherwise
  232. */
  233. function checkSwiftForAccount($account)
  234. {
  235. $swift = $account->bic;
  236. if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * Check IBAN number informations for a bank account.
  244. *
  245. * @param Account $account A bank account
  246. * @return boolean True if informations are valid, false otherwise
  247. */
  248. function checkIbanForAccount(Account $account)
  249. {
  250. require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
  251. $ibantocheck = ($account->iban ? $account->iban : $account->iban_prefix); // iban or iban_prefix for backward compatibility
  252. $iban = new PHP_IBAN\IBAN($ibantocheck);
  253. $check = $iban->Verify();
  254. if ($check) {
  255. return true;
  256. } else {
  257. return false;
  258. }
  259. }
  260. /**
  261. * Returns the iban human readable
  262. *
  263. * @param Account $account Account object
  264. * @return string
  265. */
  266. function getIbanHumanReadable(Account $account)
  267. {
  268. if ($account->getCountryCode() == 'FR') {
  269. require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
  270. $ibantoprint = preg_replace('/[^a-zA-Z0-9]/', '', $account->iban);
  271. $iban = new PHP_IBAN\IBAN($ibantoprint);
  272. return $iban->HumanFormat();
  273. }
  274. return $account->iban;
  275. }
  276. /**
  277. * Check account number informations for a bank account
  278. *
  279. * @param Account $account A bank account
  280. * @return boolean True if informations are valid, false otherwise
  281. */
  282. function checkBanForAccount($account)
  283. {
  284. $country_code = $account->getCountryCode();
  285. // For compatibility between
  286. // account of type CompanyBankAccount class (we use number, cle_rib)
  287. // account of type Account class (we use num_compte, cle)
  288. if (empty($account->number)) {
  289. $account->number = $account->num_compte;
  290. }
  291. if (empty($account->cle)) {
  292. $account->cle = $account->cle_rib;
  293. }
  294. 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);
  295. if ($country_code == 'FR') { // France rules
  296. $coef = array(62, 34, 3);
  297. // Concatenation des differents codes.
  298. $rib = strtolower(trim($account->code_banque).trim($account->code_guichet).trim($account->number).trim($account->cle));
  299. // On remplace les eventuelles lettres par des chiffres.
  300. //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
  301. $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
  302. // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
  303. // Multiplication de chaque groupe par les coef du tableau
  304. for ($i = 0, $s = 0; $i < 3; $i++) {
  305. $code = substr($rib, 7 * $i, 7);
  306. $s += (0 + (int) $code) * $coef[$i];
  307. }
  308. // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
  309. $cle_rib = 97 - ($s % 97);
  310. if ($cle_rib == $account->cle) {
  311. return true;
  312. }
  313. return false;
  314. }
  315. if ($country_code == 'BE') { // Belgium rules
  316. }
  317. if ($country_code == 'ES') { // Spanish rules
  318. $CCC = strtolower(trim($account->number));
  319. $rib = strtolower(trim($account->code_banque).trim($account->code_guichet));
  320. $cle_rib = strtolower(checkES($rib, $CCC));
  321. if ($cle_rib == strtolower($account->cle)) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. if ($country_code == 'AU') { // Australian
  327. if (strlen($account->code_banque) > 7) {
  328. return false; // Sould be 6 but can be 123-456
  329. } elseif (strlen($account->code_banque) < 6) {
  330. return false; // Sould be 6
  331. } else {
  332. return true;
  333. }
  334. }
  335. // No particular rule
  336. // If account is CompanyBankAccount class, we use number
  337. // If account is Account class, we use num_compte
  338. if (empty($account->number)) {
  339. return false;
  340. }
  341. return true;
  342. }
  343. /**
  344. * Returns the key for Spanish Banks Accounts
  345. *
  346. * @param string $IentOfi IentOfi
  347. * @param string $InumCta InumCta
  348. * @return string Key
  349. */
  350. function checkES($IentOfi, $InumCta)
  351. {
  352. if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
  353. $keycontrol = "";
  354. return $keycontrol;
  355. }
  356. $ccc = $IentOfi.$InumCta;
  357. $numbers = "1234567890";
  358. $i = 0;
  359. while ($i <= strlen($ccc) - 1) {
  360. if (strpos($numbers, substr($ccc, $i, 1)) === false) {
  361. $keycontrol = "";
  362. return $keycontrol;
  363. }
  364. $i++;
  365. }
  366. $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  367. $sum = 0;
  368. for ($i = 2; $i < 10; $i++) {
  369. $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
  370. }
  371. $key = 11 - $sum % 11;
  372. if ($key == 10) {
  373. $key = 1;
  374. }
  375. if ($key == 11) {
  376. $key = 0;
  377. }
  378. $keycontrol = $key;
  379. $sum = 0;
  380. for ($i = 0; $i < 11; $i++) {
  381. $sum += $values[$i] * (int) substr($InumCta, $i, 1); //int to cast result of substr to a number
  382. }
  383. $key = 11 - $sum % 11;
  384. if ($key == 10) {
  385. $key = 1;
  386. }
  387. if ($key == 11) {
  388. $key = 0;
  389. }
  390. $keycontrol .= $key;
  391. return $keycontrol;
  392. }