bank.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /* Copyright (C) 2006-2016 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("Card");
  39. $head[$h][2] = 'bankname';
  40. $h++;
  41. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/bankentries.php?id=" . $object->id;
  42. $head[$h][1] = $langs->trans("BankTransactions");
  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. /*$head[$h][0] = DOL_URL_ROOT . "/compta/bank/info.php?id=" . $object->id;
  84. $head[$h][1] = $langs->trans("Info");
  85. $head[$h][2] = 'info';
  86. $h++;*/
  87. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
  88. return $head;
  89. }
  90. /**
  91. * Prepare array with list of tabs
  92. *
  93. * @param Object $object Object related to tabs
  94. * @return array Array of tabs to shoc
  95. */
  96. function bank_admin_prepare_head($object)
  97. {
  98. global $langs, $conf, $user;
  99. $h = 0;
  100. $head = array();
  101. $head[$h][0] = DOL_URL_ROOT . '/admin/bank.php';
  102. $head[$h][1] = $langs->trans("Miscellaneous");
  103. $head[$h][2] = 'general';
  104. $h++;
  105. $head[$h][0] = DOL_URL_ROOT . '/admin/chequereceipts.php';
  106. $head[$h][1] = $langs->trans("CheckReceiptShort");
  107. $head[$h][2] = 'checkreceipts';
  108. $h++;
  109. // Show more tabs from modules
  110. // Entries must be declared in modules descriptor with line
  111. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  112. // $this->tabs = array('entity:-tabname); to remove a tab
  113. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin');
  114. $head[$h][0] = DOL_URL_ROOT.'/admin/bank_extrafields.php';
  115. $head[$h][1] = $langs->trans("ExtraFields");
  116. $head[$h][2] = 'attributes';
  117. $h++;
  118. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank_admin', 'remove');
  119. return $head;
  120. }
  121. /**
  122. * Prepare array with list of tabs
  123. *
  124. * @param Object $object Object related to tabs
  125. * @return array Array of tabs to shoc
  126. */
  127. function various_payment_prepare_head($object) {
  128. global $db, $langs, $conf;
  129. $h = 0;
  130. $head = array();
  131. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$object->id;
  132. $head[$h][1] = $langs->trans("Card");
  133. $head[$h][2] = 'card';
  134. $h++;
  135. // Show more tabs from modules
  136. // Entries must be declared in modules descriptor with line
  137. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  138. // $this->tabs = array('entity:-tabname); to remove a tab
  139. complete_head_from_modules($conf,$langs,$object,$head,$h,'various_payment');
  140. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  141. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  142. $upload_dir = $conf->banque->dir_output . "/" . dol_sanitizeFileName($object->ref);
  143. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
  144. $nbLinks=Link::count($db, $object->element, $object->id);
  145. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/document.php?id='.$object->id;
  146. $head[$h][1] = $langs->trans('Documents');
  147. if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
  148. $head[$h][2] = 'documents';
  149. $h++;
  150. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/various_payment/info.php?id='.$object->id;
  151. $head[$h][1] = $langs->trans("Info");
  152. $head[$h][2] = 'info';
  153. $h++;
  154. complete_head_from_modules($conf,$langs,$object,$head,$h,'various_payment', 'remove');
  155. return $head;
  156. }
  157. /**
  158. * Check SWIFT informations for a bank account
  159. *
  160. * @param Account $account A bank account
  161. * @return boolean True if informations are valid, false otherwise
  162. */
  163. function checkSwiftForAccount($account)
  164. {
  165. $swift = $account->bic;
  166. if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * Check IBAN number informations for a bank account.
  174. *
  175. * @param Account $account A bank account
  176. * @return boolean True if informations are valid, false otherwise
  177. */
  178. function checkIbanForAccount($account)
  179. {
  180. require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
  181. $iban = new IBAN($account->iban);
  182. $check = $iban->Verify();
  183. if ($check) return true;
  184. else return false;
  185. }
  186. /**
  187. * Check account number informations for a bank account
  188. *
  189. * @param Account $account A bank account
  190. * @return boolean True if informations are valid, false otherwise
  191. */
  192. function checkBanForAccount($account)
  193. {
  194. $country_code = $account->getCountryCode();
  195. // For compatibility between
  196. // account of type CompanyBankAccount class (we use number, cle_rib)
  197. // account of type Account class (we use num_compte, cle)
  198. if (empty($account->number))
  199. $account->number = $account->num_compte;
  200. if (empty($account->cle))
  201. $account->cle = $account->cle_rib;
  202. 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);
  203. if ($country_code == 'FR') { // France rules
  204. $coef = array(62, 34, 3);
  205. // Concatenation des differents codes.
  206. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet) . trim($account->number) . trim($account->cle));
  207. // On remplace les eventuelles lettres par des chiffres.
  208. //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
  209. $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
  210. // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
  211. // Multiplication de chaque groupe par les coef du tableau
  212. for ($i = 0, $s = 0; $i < 3; $i++) {
  213. $code = substr($rib, 7 * $i, 7);
  214. $s += (0 + (int) $code) * $coef[$i];
  215. }
  216. // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
  217. $cle_rib = 97 - ($s % 97);
  218. if ($cle_rib == $account->cle) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. if ($country_code == 'BE') { // Belgium rules
  224. }
  225. if ($country_code == 'ES') { // Spanish rules
  226. $CCC = strtolower(trim($account->number));
  227. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet));
  228. $cle_rib = strtolower(checkES($rib, $CCC));
  229. if ($cle_rib == strtolower($account->cle)) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. if ($country_code == 'AU') { // Australian
  235. if (strlen($account->code_banque) > 7)
  236. return false; // Sould be 6 but can be 123-456
  237. else if (strlen($account->code_banque) < 6)
  238. return false; // Sould be 6
  239. else
  240. return true;
  241. }
  242. // No particular rule
  243. // If account is CompanyBankAccount class, we use number
  244. // If account is Account class, we use num_compte
  245. if (empty($account->number)) {
  246. return false;
  247. }
  248. return true;
  249. }
  250. /**
  251. * Returns the key for Spanish Banks Accounts
  252. *
  253. * @param string $IentOfi IentOfi
  254. * @param string $InumCta InumCta
  255. * @return string Key
  256. */
  257. function checkES($IentOfi, $InumCta)
  258. {
  259. if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
  260. $keycontrol = "";
  261. return $keycontrol;
  262. }
  263. $ccc = $IentOfi . $InumCta;
  264. $numbers = "1234567890";
  265. $i = 0;
  266. while ($i <= strlen($ccc) - 1) {
  267. if (strpos($numbers, substr($ccc, $i, 1)) === false) {
  268. $keycontrol = "";
  269. return $keycontrol;
  270. }
  271. $i++;
  272. }
  273. $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  274. $sum = 0;
  275. for ($i = 2; $i < 10; $i++) {
  276. $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
  277. }
  278. $key = 11 - $sum % 11;
  279. if ($key == 10)
  280. $key = 1;
  281. if ($key == 11)
  282. $key = 0;
  283. $keycontrol = $key;
  284. $sum = 0;
  285. for ($i = 0; $i < 11; $i++) {
  286. $sum += $values[$i] * substr($InumCta, $i, 1);
  287. }
  288. $key = 11 - $sum % 11;
  289. if ($key == 10)
  290. $key = 1;
  291. if ($key == 11)
  292. $key = 0;
  293. $keycontrol .= $key;
  294. return $keycontrol;
  295. }