bank.lib.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /* Copyright (C) 2006-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/bank.lib.php
  21. * \brief Ensemble de fonctions de base pour le module banque
  22. * \ingroup banque
  23. */
  24. /**
  25. * Prepare array with list of tabs
  26. *
  27. * @param Object $object Object related to tabs
  28. * @return array Array of tabs to shoc
  29. */
  30. function bank_prepare_head($object)
  31. {
  32. global $langs, $conf, $user;
  33. $h = 0;
  34. $head = array();
  35. $head[$h][0] = DOL_URL_ROOT . '/compta/bank/fiche.php?id=' . $object->id;
  36. $head[$h][1] = $langs->trans("AccountCard");
  37. $head[$h][2] = 'bankname';
  38. $h++;
  39. if ($object->type == 0 || $object->type == 1) {
  40. $head[$h][0] = DOL_URL_ROOT . '/compta/bank/bankid_fr.php?id=' . $object->id;
  41. $head[$h][1] = $langs->trans("RIB");
  42. $head[$h][2] = 'bankid';
  43. $h++;
  44. }
  45. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/account.php?id=" . $object->id;
  46. $head[$h][1] = $langs->trans("Transactions");
  47. $head[$h][2] = 'journal';
  48. $h++;
  49. // if ($conf->global->MAIN_FEATURES_LEVEL >= 1)
  50. // {
  51. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/treso.php?account=" . $object->id;
  52. $head[$h][1] = $langs->trans("PlannedTransactions");
  53. $head[$h][2] = 'cash';
  54. $h++;
  55. // }
  56. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/annuel.php?account=" . $object->id;
  57. $head[$h][1] = $langs->trans("IOMonthlyReporting");
  58. $head[$h][2] = 'annual';
  59. $h++;
  60. $head[$h][0] = DOL_URL_ROOT . "/compta/bank/graph.php?account=" . $object->id;
  61. $head[$h][1] = $langs->trans("Graph");
  62. $head[$h][2] = 'graph';
  63. $h++;
  64. if ($object->courant != 2)
  65. {
  66. $head[$h][0] = DOL_URL_ROOT."/compta/bank/releve.php?account=".$object->id;
  67. $head[$h][1] = $langs->trans("AccountStatements");
  68. $head[$h][2] = 'statement';
  69. $h++;
  70. }
  71. // Show more tabs from modules
  72. // Entries must be declared in modules descriptor with line
  73. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  74. // $this->tabs = array('entity:-tabname); to remove a tab
  75. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank');
  76. complete_head_from_modules($conf, $langs, $object, $head, $h, 'bank', 'remove');
  77. return $head;
  78. }
  79. /**
  80. * Check account number informations for a bank account
  81. *
  82. * @param Account $account A bank account
  83. * @return int True if informations are valid, false otherwise
  84. */
  85. function checkBanForAccount($account)
  86. {
  87. $country_code = $account->getCountryCode();
  88. // For compatibility between
  89. // account of type CompanyBankAccount class (we use number, cle_rib)
  90. // account of type Account class (we use num_compte, cle)
  91. if (empty($account->number))
  92. $account->number = $account->num_compte;
  93. if (empty($account->cle))
  94. $account->cle = $account->cle_rib;
  95. 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);
  96. if ($country_code == 'FR') { // France rules
  97. $coef = array(62, 34, 3);
  98. // Concatenation des differents codes.
  99. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet) . trim($account->number) . trim($account->cle));
  100. // On remplace les eventuelles lettres par des chiffres.
  101. //$rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz","12345678912345678912345678"); //Ne marche pas
  102. $rib = strtr($rib, "abcdefghijklmnopqrstuvwxyz", "12345678912345678923456789");
  103. // Separation du rib en 3 groupes de 7 + 1 groupe de 2.
  104. // Multiplication de chaque groupe par les coef du tableau
  105. for ($i = 0, $s = 0; $i < 3; $i++) {
  106. $code = substr($rib, 7 * $i, 7);
  107. $s += (0 + $code) * $coef[$i];
  108. }
  109. // Soustraction du modulo 97 de $s a 97 pour obtenir la cle
  110. $cle_rib = 97 - ($s % 97);
  111. if ($cle_rib == $account->cle) {
  112. return true;
  113. }
  114. return false;
  115. }
  116. if ($country_code == 'BE') { // Belgium rules
  117. }
  118. if ($country_code == 'ES') { // Spanish rules
  119. $CCC = strtolower(trim($account->number));
  120. $rib = strtolower(trim($account->code_banque) . trim($account->code_guichet));
  121. $cle_rib = strtolower(checkES($rib, $CCC));
  122. if ($cle_rib == strtolower($account->cle)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. if ($country_code == 'AU') { // Australian
  128. if (strlen($account->code_banque) > 7)
  129. return false; // Sould be 6 but can be 123-456
  130. else if (strlen($account->code_banque) < 6)
  131. return false; // Sould be 6
  132. else
  133. return true;
  134. }
  135. // No particular rule
  136. // If account is CompanyBankAccount class, we use number
  137. // If account is Account class, we use num_compte
  138. if (empty($account->number)) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * Returns the key for Spanish Banks Accounts
  145. *
  146. * @param string $IentOfi IentOfi
  147. * @param string $InumCta InumCta
  148. * @return string Key
  149. */
  150. function checkES($IentOfi, $InumCta) {
  151. if (empty($IentOfi) || empty($InumCta) || strlen($IentOfi) != 8 || strlen($InumCta) != 10) {
  152. $keycontrol = "";
  153. return $keycontrol;
  154. }
  155. $ccc = $IentOfi . $InumCta;
  156. $numbers = "1234567890";
  157. $i = 0;
  158. while ($i <= strlen($ccc) - 1) {
  159. if (strpos($numbers, substr($ccc, $i, 1)) === false) {
  160. $keycontrol = "";
  161. return $keycontrol;
  162. }
  163. $i++;
  164. }
  165. $values = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  166. $sum = 0;
  167. for ($i = 2; $i < 10; $i++) {
  168. $sum += $values[$i] * substr($IentOfi, $i - 2, 1);
  169. }
  170. $key = 11 - $sum % 11;
  171. if ($key == 10)
  172. $key = 1;
  173. if ($key == 11)
  174. $key = 0;
  175. $keycontrol = $key;
  176. $sum = 0;
  177. for ($i = 0; $i < 11; $i++) {
  178. $sum += $values[$i] * substr($InumCta, $i, 1);
  179. }
  180. $key = 11 - $sum % 11;
  181. if ($key == 10)
  182. $key = 1;
  183. if ($key == 11)
  184. $key = 0;
  185. $keycontrol .= $key;
  186. return $keycontrol;
  187. }