bank.lib.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  5. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/lib/bank.lib.php
  23. * \ingroup bank
  24. * \brief Ensemble de fonctions de base pour le module banque
  25. */
  26. /**
  27. * Prepare array with list of tabs
  28. *
  29. * @param Account $object Object related to tabs
  30. * @return array Array of tabs to show
  31. */
  32. function bank_prepare_head(Account $object)
  33. {
  34. global $db, $langs, $conf, $user;
  35. $h = 0;
  36. $head = array();
  37. $head[$h][0] = DOL_URL_ROOT . '/compta/bank/card.php?id=' . $object->id;
  38. $head[$h][1] = $langs->trans("AccountCard");
  39. $head[$h][2] = 'bankname';
  40. $h++;
  41. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/account.php?id=" . $object->id;
  42. $head[$h][1] = $langs->trans("Transactions");
  43. $head[$h][2] = 'journal';
  44. $h++;
  45. // if ($conf->global->MAIN_FEATURES_LEVEL >= 1)
  46. // {
  47. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/treso.php?account=" . $object->id;
  48. $head[$h][1] = $langs->trans("PlannedTransactions");
  49. $head[$h][2] = 'cash';
  50. $h++;
  51. // }
  52. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/annuel.php?account=" . $object->id;
  53. $head[$h][1] = $langs->trans("IOMonthlyReporting");
  54. $head[$h][2] = 'annual';
  55. $h++;
  56. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/graph.php?account=" . $object->id;
  57. $head[$h][1] = $langs->trans("Graph");
  58. $head[$h][2] = 'graph';
  59. $h++;
  60. if ($object->courant != Account::TYPE_CASH)
  61. {
  62. $head[$h][0] = DOL_URL_ROOT."/compta/bank/releve.php?account=".$object->id;
  63. $head[$h][1] = $langs->trans("AccountStatements");
  64. $head[$h][2] = 'statement';
  65. $h++;
  66. }
  67. // Attached files
  68. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  69. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  70. $upload_dir = $conf->bank->dir_output . "/" . dol_sanitizeFileName($object->ref);
  71. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview\.png)$'));
  72. $nbLinks=Link::count($db, $object->element, $object->id);
  73. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/document.php?account=" . $object->id;
  74. $head[$h][1] = $langs->trans("Documents");
  75. if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
  76. $head[$h][2] = 'document';
  77. $h++;
  78. // Show more tabs from modules
  79. // Entries must be declared in modules descriptor with line
  80. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  81. // $this->tabs = array('entity:-tabname); to remove a tab
  82. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
  83. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
  84. return $head;
  85. }
  86. /**
  87. * Prepare array with list of tabs
  88. *
  89. * @param Object $object Object related to tabs
  90. * @return array Array of tabs to shoc
  91. */
  92. function bank_admin_prepare_head($object)
  93. {
  94. global $langs, $conf, $user;
  95. $h = 0;
  96. $head = array();
  97. $head[$h][0] = DOL_URL_ROOT . '/admin/bank.php';
  98. $head[$h][1] = $langs->trans("Miscellaneous");
  99. $head[$h][2] = 'general';
  100. $h++;
  101. $head[$h][0] = DOL_URL_ROOT . '/admin/chequereceipts.php';
  102. $head[$h][1] = $langs->trans("CheckReceiptShort");
  103. $head[$h][2] = 'checkreceipts';
  104. $h++;
  105. // Show more tabs from modules
  106. // Entries must be declared in modules descriptor with line
  107. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  108. // $this->tabs = array('entity:-tabname); to remove a tab
  109. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
  110. $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
  111. $head[$h][1] = $langs->trans("ExtraFields");
  112. $head[$h][2] = 'attributes';
  113. $h++;
  114. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
  115. return $head;
  116. }
  117. /**
  118. * Check SWIFT informations for a bank account
  119. *
  120. * @param Account $account A bank account
  121. * @return boolean True if informations are valid, false otherwise
  122. */
  123. function checkSwiftForAccount($account)
  124. {
  125. $swift = $account->bic;
  126. if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Check IBAN number informations for a bank account.
  134. *
  135. * @param Account $account A bank account
  136. * @return boolean True if informations are valid, false otherwise
  137. */
  138. function checkIbanForAccount($account)
  139. {
  140. require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
  141. $iban = new IBAN($account->iban);
  142. $check = $iban->Verify();
  143. if ($check) return true;
  144. else return false;
  145. }
  146. /**
  147. * Check account number informations for a bank account
  148. *
  149. * @param Account $account A bank account
  150. * @return boolean True if informations are valid, false otherwise
  151. */
  152. function checkBanForAccount($account)
  153. {
  154. $country_code = $account->getCountryCode();
  155. // For compatibility between
  156. // account of type CompanyBankAccount class (we use number, cle_rib)
  157. // account of type Account class (we use num_compte, cle)
  158. if (empty($account->number))
  159. $account->number = $account->num_compte;
  160. if (empty($account->cle))
  161. $account->cle = $account->cle_rib;
  162. 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);
  163. if ($country_code == 'FR') { // France rules
  164. $coef = array(62, 34, 3);
  165. // Concatenation des differents codes.
  166. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet) . trim($account->number) . trim($account->cle));
  167. // On remplace les eventuelles lettres par des chiffres.
  168. //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
  169. $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
  170. // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
  171. // Multiplication de chaque groupe par les coef du tableau
  172. for ($i = 0, $s = 0; $i < 3; $i++) {
  173. $code = substr($rib, 7 * $i, 7);
  174. $s += (0 + $code) * $coef[$i];
  175. }
  176. // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
  177. $cle_rib = 97 - ($s % 97);
  178. if ($cle_rib == $account->cle) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. if ($country_code == 'BE') { // Belgium rules
  184. }
  185. if ($country_code == 'ES') { // Spanish rules
  186. $CCC = strtolower(trim($account->number));
  187. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet));
  188. $cle_rib = strtolower(checkES($rib, $CCC));
  189. if ($cle_rib == strtolower($account->cle)) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. if ($country_code == 'AU') { // Australian
  195. if (strlen($account->code_banque) > 7)
  196. return false; // Sould be 6 but can be 123-456
  197. else if (strlen($account->code_banque) < 6)
  198. return false; // Sould be 6
  199. else
  200. return true;
  201. }
  202. // No particular rule
  203. // If account is CompanyBankAccount class, we use number
  204. // If account is Account class, we use num_compte
  205. if (empty($account->number)) {
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * Returns the key for Spanish Banks Accounts
  212. *
  213. * @param string $IentOfi IentOfi
  214. * @param string $InumCta InumCta
  215. * @return string Key
  216. */
  217. function checkES($IentOfi, $InumCta)
  218. {
  219. if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
  220. $keycontrol = "";
  221. return $keycontrol;
  222. }
  223. $ccc = $IentOfi . $InumCta;
  224. $numbers = "1234567890";
  225. $i = 0;
  226. while ($i <= strlen($ccc) - 1) {
  227. if (strpos($numbers, substr($ccc, $i, 1)) === false) {
  228. $keycontrol = "";
  229. return $keycontrol;
  230. }
  231. $i++;
  232. }
  233. $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  234. $sum = 0;
  235. for ($i = 2; $i < 10; $i++) {
  236. $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
  237. }
  238. $key = 11 - $sum % 11;
  239. if ($key == 10)
  240. $key = 1;
  241. if ($key == 11)
  242. $key = 0;
  243. $keycontrol = $key;
  244. $sum = 0;
  245. for ($i = 0; $i < 11; $i++) {
  246. $sum += $values[$i] * substr($InumCta, $i, 1);
  247. }
  248. $key = 11 - $sum % 11;
  249. if ($key == 10)
  250. $key = 1;
  251. if ($key == 11)
  252. $key = 0;
  253. $keycontrol .= $key;
  254. return $keycontrol;
  255. }