companybankaccount.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2013 Peter Fontaine <contact@peterfontaine.fr>
  6. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/societe/class/companybankaccount.class.php
  23. * \ingroup societe
  24. * \brief File of class to manage bank accounts description of third parties
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  27. /**
  28. * Class to manage bank accounts description of third parties
  29. */
  30. class CompanyBankAccount extends Account
  31. {
  32. public $socid;
  33. public $default_rib;
  34. /**
  35. * Value 'FRST' or 'RCUR' (For SEPA mandate). Warning, in database, we store 'RECUR'.
  36. *
  37. * @var string
  38. */
  39. public $frstrecur;
  40. public $rum;
  41. public $date_rum;
  42. public $stripe_card_ref; // ID of BAN into an external payment system
  43. public $stripe_account; // Account of the external payment system
  44. /**
  45. * Date creation record (datec)
  46. *
  47. * @var integer
  48. */
  49. public $datec;
  50. /**
  51. * Date modification record (tms)
  52. *
  53. * @var integer
  54. */
  55. public $datem;
  56. /**
  57. * Constructor
  58. *
  59. * @param DoliDB $db Database handler
  60. */
  61. public function __construct(DoliDB $db)
  62. {
  63. $this->db = $db;
  64. $this->socid = 0;
  65. $this->solde = 0;
  66. $this->error_number = 0;
  67. $this->default_rib = 0;
  68. }
  69. /**
  70. * Create bank information record.
  71. *
  72. * @param User $user User
  73. * @param int $notrigger 1=Disable triggers
  74. * @return int <0 if KO, >= 0 if OK
  75. */
  76. public function create(User $user = null, $notrigger = 0)
  77. {
  78. $now = dol_now();
  79. $error = 0;
  80. // Check paramaters
  81. if (empty($this->socid)) {
  82. $this->error = 'BadValueForParameter';
  83. return -1;
  84. }
  85. // Correct ->default_rib to not set the new account as default, if there is already 1. We want to be sure to have always 1 default for type = 'ban'.
  86. // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
  87. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".((int) $this->socid)." AND default_rib = 1 AND type = 'ban'";
  88. $result = $this->db->query($sql);
  89. if ($result) {
  90. $numrows = $this->db->num_rows($result);
  91. if ($this->default_rib && $numrows > 0) {
  92. $this->default_rib = 0;
  93. }
  94. if (empty($this->default_rib) && $numrows == 0) {
  95. $this->default_rib = 1;
  96. }
  97. }
  98. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
  99. $sql .= " VALUES (".((int) $this->socid).", 'ban', '".$this->db->idate($now)."')";
  100. $resql = $this->db->query($sql);
  101. if ($resql) {
  102. if ($this->db->affected_rows($resql)) {
  103. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
  104. if (!$notrigger) {
  105. // Call trigger
  106. $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
  107. if ($result < 0) {
  108. $error++;
  109. }
  110. // End call triggers
  111. if (!$error) {
  112. return $this->id;
  113. } else {
  114. return 0;
  115. }
  116. } else {
  117. return 1;
  118. }
  119. }
  120. } else {
  121. $this->error = $this->db->lasterror();
  122. return 0;
  123. }
  124. }
  125. /**
  126. * Update bank account
  127. *
  128. * @param User $user Object user
  129. * @param int $notrigger 1=Disable triggers
  130. * @return int <=0 if KO, >0 if OK
  131. */
  132. public function update(User $user = null, $notrigger = 0)
  133. {
  134. global $conf, $langs;
  135. $error = 0;
  136. if (!$this->id) {
  137. return -1;
  138. }
  139. if (dol_strlen($this->domiciliation) > 255) {
  140. $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
  141. }
  142. if (dol_strlen($this->owner_address) > 255) {
  143. $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
  144. }
  145. $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
  146. $sql .= " bank = '".$this->db->escape($this->bank)."'";
  147. $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
  148. $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
  149. $sql .= ",number='".$this->db->escape($this->number)."'";
  150. $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
  151. $sql .= ",bic='".$this->db->escape($this->bic)."'";
  152. $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
  153. $sql .= ",domiciliation = '".$this->db->escape($this->domiciliation)."'";
  154. $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
  155. $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  156. $sql .= ",default_rib = ".((int) $this->default_rib);
  157. if (!empty($conf->prelevement->enabled)) {
  158. $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
  159. $sql .= ",rum = '".$this->db->escape($this->rum)."'";
  160. $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
  161. }
  162. if (trim($this->label) != '') {
  163. $sql .= ",label = '".$this->db->escape($this->label)."'";
  164. } else {
  165. $sql .= ",label = NULL";
  166. }
  167. $sql .= ",stripe_card_ref = '".$this->db->escape($this->stripe_card_ref)."'";
  168. $sql .= ",stripe_account = '".$this->db->escape($this->stripe_account)."'";
  169. $sql .= " WHERE rowid = ".((int) $this->id);
  170. $result = $this->db->query($sql);
  171. if ($result) {
  172. if (!$notrigger) {
  173. // Call trigger
  174. $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
  175. if ($result < 0) {
  176. $error++;
  177. }
  178. // End call triggers
  179. if (!$error) {
  180. return 1;
  181. } else {
  182. return -1;
  183. }
  184. } else {
  185. return 1;
  186. }
  187. } else {
  188. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  189. $this->error = $langs->trans('ErrorDuplicateField');
  190. } else {
  191. $this->error = $this->db->lasterror();
  192. }
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Load record from database
  198. *
  199. * @param int $id Id of record
  200. * @param int $socid Id of company. If this is filled, function will return the first entry found (matching $default and $type)
  201. * @param int $default If id of company filled, we say if we want first record among all (-1), default record (1) or non default record (0)
  202. * @param int $type If id of company filled, we say if we want record of this type only
  203. * @return int <0 if KO, >0 if OK
  204. */
  205. public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
  206. {
  207. if (empty($id) && empty($socid)) {
  208. return -1;
  209. }
  210. $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
  211. $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum,";
  212. $sql .= " stripe_card_ref, stripe_account";
  213. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
  214. if ($id) {
  215. $sql .= " WHERE rowid = ".((int) $id);
  216. } elseif ($socid > 0) {
  217. $sql .= " WHERE fk_soc = ".((int) $socid);
  218. if ($default > -1) {
  219. $sql .= " AND default_rib = ".((int) $default);
  220. }
  221. if ($type) {
  222. $sql .= " AND type = '".$this->db->escape($type)."'";
  223. }
  224. }
  225. $resql = $this->db->query($sql);
  226. if ($resql) {
  227. if ($this->db->num_rows($resql)) {
  228. $obj = $this->db->fetch_object($resql);
  229. $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
  230. $this->id = $obj->rowid;
  231. $this->type = $obj->type;
  232. $this->socid = $obj->fk_soc;
  233. $this->bank = $obj->bank;
  234. $this->code_banque = $obj->code_banque;
  235. $this->code_guichet = $obj->code_guichet;
  236. $this->number = $obj->number;
  237. $this->cle_rib = $obj->cle_rib;
  238. $this->bic = $obj->bic;
  239. $this->iban = $obj->iban;
  240. $this->domiciliation = $obj->domiciliation;
  241. $this->proprio = $obj->proprio;
  242. $this->owner_address = $obj->owner_address;
  243. $this->label = $obj->label;
  244. $this->default_rib = $obj->default_rib;
  245. $this->datec = $this->db->jdate($obj->datec);
  246. $this->datem = $this->db->jdate($obj->datem);
  247. $this->rum = $obj->rum;
  248. $this->frstrecur = $obj->frstrecur;
  249. $this->date_rum = $this->db->jdate($obj->date_rum);
  250. $this->stripe_card_ref = $obj->stripe_card_ref;
  251. $this->stripe_account = $obj->stripe_account;
  252. }
  253. $this->db->free($resql);
  254. return 1;
  255. } else {
  256. dol_print_error($this->db);
  257. return -1;
  258. }
  259. }
  260. /**
  261. * Delete a rib from database
  262. *
  263. * @param User $user User deleting
  264. * @param int $notrigger 1=Disable triggers
  265. * @return int <0 if KO, >0 if OK
  266. */
  267. public function delete(User $user = null, $notrigger = 0)
  268. {
  269. $error = 0;
  270. dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
  271. $this->db->begin();
  272. if (!$error && !$notrigger) {
  273. // Call trigger
  274. $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
  275. if ($result < 0) {
  276. $error++;
  277. }
  278. // End call triggers
  279. }
  280. if (!$error) {
  281. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
  282. $sql .= " WHERE rowid = ".((int) $this->id);
  283. if (!$this->db->query($sql)) {
  284. $error++;
  285. $this->errors[] = $this->db->lasterror();
  286. }
  287. }
  288. if (!$error) {
  289. $this->db->commit();
  290. return 1;
  291. } else {
  292. $this->db->rollback();
  293. return -1 * $error;
  294. }
  295. }
  296. /**
  297. * Return RIB
  298. *
  299. * @param boolean $displayriblabel Prepend or Hide Label
  300. * @return string RIB
  301. */
  302. public function getRibLabel($displayriblabel = true)
  303. {
  304. $rib = '';
  305. if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
  306. if ($this->label && $displayriblabel) {
  307. $rib = $this->label." : ";
  308. }
  309. $rib .= (string) $this->iban;
  310. }
  311. return $rib;
  312. }
  313. /**
  314. * Set a BAN as Default
  315. *
  316. * @param int $rib RIB id
  317. * @param string $resetolddefaultfor Reset if we have already a default value for type = 'ban'
  318. * @return int 0 if KO, 1 if OK
  319. */
  320. public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
  321. {
  322. $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
  323. $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
  324. dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
  325. $result1 = $this->db->query($sql1);
  326. if ($result1) {
  327. if ($this->db->num_rows($result1) == 0) {
  328. return 0;
  329. } else {
  330. $obj = $this->db->fetch_object($result1);
  331. $this->db->begin();
  332. $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
  333. $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
  334. if ($resetolddefaultfor) {
  335. $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
  336. }
  337. $result2 = $this->db->query($sql2);
  338. $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
  339. $sql3 .= " WHERE rowid = ".((int) $obj->id);
  340. $result3 = $this->db->query($sql3);
  341. if (!$result2 || !$result3) {
  342. dol_print_error($this->db);
  343. $this->db->rollback();
  344. return -1;
  345. } else {
  346. $this->db->commit();
  347. return 1;
  348. }
  349. }
  350. } else {
  351. dol_print_error($this->db);
  352. return -1;
  353. }
  354. }
  355. /**
  356. * Initialise an instance with random values.
  357. * Used to build previews or test instances.
  358. * id must be 0 if object instance is a specimen.
  359. *
  360. * @return void
  361. */
  362. public function initAsSpecimen()
  363. {
  364. $this->specimen = 1;
  365. $this->ref = 'CBA';
  366. $this->label = 'CustomerCorp Bank account';
  367. $this->bank = 'CustomerCorp Bank';
  368. $this->courant = Account::TYPE_CURRENT;
  369. $this->clos = Account::STATUS_OPEN;
  370. $this->code_banque = '123';
  371. $this->code_guichet = '456';
  372. $this->number = 'CUST12345';
  373. $this->cle_rib = 50;
  374. $this->bic = 'CC12';
  375. $this->iban = 'FR999999999';
  376. $this->domiciliation = 'Bank address of customer corp';
  377. $this->proprio = 'Owner';
  378. $this->owner_address = 'Owner address';
  379. $this->country_id = 1;
  380. $this->rum = 'UMR-CU1212-0007-5-1475405262';
  381. $this->date_rum = dol_now() - 10000;
  382. $this->frstrecur = 'FRST';
  383. $this->socid = 1;
  384. }
  385. }