companybankaccount.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'societe_rib';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'societe_rib';
  41. /** @var bool $default_rib 1 = this object is the third party's default bank information */
  42. public $default_rib;
  43. /**
  44. * Value 'FRST' or 'RCUR' (For SEPA mandate). Warning, in database, we store 'RECUR'.
  45. *
  46. * @var string
  47. */
  48. public $frstrecur;
  49. public $rum;
  50. public $date_rum;
  51. public $stripe_card_ref; // ID of BAN into an external payment system
  52. public $stripe_account; // Account of the external payment system
  53. /**
  54. * Date creation record (datec)
  55. *
  56. * @var integer
  57. */
  58. public $datec;
  59. /**
  60. * Date modification record (tms)
  61. *
  62. * @var integer
  63. */
  64. public $datem;
  65. /**
  66. * @var string TRIGGER_PREFIX Dolibarr 16.0 and above use the prefix to prevent the creation of inconsistently
  67. * named triggers
  68. * @see CommonObject::call_trigger()
  69. */
  70. const TRIGGER_PREFIX = 'COMPANY_RIB';
  71. /**
  72. * Constructor
  73. *
  74. * @param DoliDB $db Database handler
  75. */
  76. public function __construct(DoliDB $db)
  77. {
  78. $this->db = $db;
  79. $this->socid = 0;
  80. $this->solde = 0;
  81. $this->error_number = 0;
  82. $this->default_rib = 0;
  83. }
  84. /**
  85. * Create bank information record.
  86. *
  87. * @param User $user User
  88. * @param int $notrigger 1=Disable triggers
  89. * @return int <0 if KO, > 0 if OK (ID of newly created company bank account information)
  90. */
  91. public function create(User $user = null, $notrigger = 0)
  92. {
  93. $now = dol_now();
  94. $error = 0;
  95. // Check paramaters
  96. if (empty($this->socid)) {
  97. $this->error = 'BadValueForParameter';
  98. $this->errors[] = $this->error;
  99. return -1;
  100. }
  101. // 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'.
  102. // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
  103. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".((int) $this->socid)." AND default_rib = 1 AND type = 'ban'";
  104. $result = $this->db->query($sql);
  105. if ($result) {
  106. $numrows = $this->db->num_rows($result);
  107. if ($this->default_rib && $numrows > 0) {
  108. $this->default_rib = 0;
  109. }
  110. if (empty($this->default_rib) && $numrows == 0) {
  111. $this->default_rib = 1;
  112. }
  113. }
  114. $this->db->begin();
  115. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
  116. $sql .= " VALUES (".((int) $this->socid).", 'ban', '".$this->db->idate($now)."')";
  117. $resql = $this->db->query($sql);
  118. if ($resql) {
  119. if ($this->db->affected_rows($resql)) {
  120. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
  121. if (!$notrigger) {
  122. // Call trigger
  123. $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
  124. if ($result < 0) {
  125. $error++;
  126. }
  127. // End call triggers
  128. }
  129. }
  130. } else {
  131. $error++;
  132. $this->error = $this->db->lasterror();
  133. $this->errors[] = $this->error;
  134. }
  135. if (!$error) {
  136. $this->db->commit();
  137. return $this->id;
  138. } else {
  139. $this->db->rollback();
  140. return -1;
  141. }
  142. }
  143. /**
  144. * Update bank account
  145. *
  146. * @param User $user Object user
  147. * @param int $notrigger 1=Disable triggers
  148. * @return int <=0 if KO, >0 if OK
  149. */
  150. public function update(User $user = null, $notrigger = 0)
  151. {
  152. global $conf, $langs;
  153. $error = 0;
  154. if (!$this->id) {
  155. return -1;
  156. }
  157. if (dol_strlen($this->domiciliation) > 255) {
  158. $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
  159. }
  160. if (dol_strlen($this->owner_address) > 255) {
  161. $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
  162. }
  163. $this->db->begin();
  164. $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
  165. $sql .= " bank = '".$this->db->escape($this->bank)."'";
  166. $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
  167. $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
  168. $sql .= ",number='".$this->db->escape($this->number)."'";
  169. $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
  170. $sql .= ",bic='".$this->db->escape($this->bic)."'";
  171. $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
  172. $sql .= ",domiciliation = '".$this->db->escape($this->domiciliation)."'";
  173. $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
  174. $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  175. $sql .= ",default_rib = ".((int) $this->default_rib);
  176. if (!empty($conf->prelevement->enabled)) {
  177. $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
  178. $sql .= ",rum = '".$this->db->escape($this->rum)."'";
  179. $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
  180. }
  181. if (trim($this->label) != '') {
  182. $sql .= ",label = '".$this->db->escape($this->label)."'";
  183. } else {
  184. $sql .= ",label = NULL";
  185. }
  186. $sql .= ",stripe_card_ref = '".$this->db->escape($this->stripe_card_ref)."'";
  187. $sql .= ",stripe_account = '".$this->db->escape($this->stripe_account)."'";
  188. $sql .= " WHERE rowid = ".((int) $this->id);
  189. $result = $this->db->query($sql);
  190. if ($result) {
  191. if (!$notrigger) {
  192. // Call trigger
  193. $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
  194. if ($result < 0) {
  195. $error++;
  196. }
  197. // End call triggers
  198. }
  199. } else {
  200. $error++;
  201. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  202. $this->error = $langs->trans('ErrorDuplicateField');
  203. } else {
  204. $this->error = $this->db->lasterror();
  205. }
  206. $this->errors[] = $this->error;
  207. }
  208. if (!$error) {
  209. $this->db->commit();
  210. return 1;
  211. } else {
  212. $this->db->rollback();
  213. return -1;
  214. }
  215. }
  216. /**
  217. * Load record from database
  218. *
  219. * @param int $id Id of record
  220. * @param int $socid Id of company. If this is filled, function will return the first entry found (matching $default and $type)
  221. * @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)
  222. * @param int $type If id of company filled, we say if we want record of this type only
  223. * @return int <0 if KO, >0 if OK
  224. */
  225. public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
  226. {
  227. if (empty($id) && empty($socid)) {
  228. return -1;
  229. }
  230. $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
  231. $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum,";
  232. $sql .= " stripe_card_ref, stripe_account";
  233. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
  234. if ($id) {
  235. $sql .= " WHERE rowid = ".((int) $id);
  236. } elseif ($socid > 0) {
  237. $sql .= " WHERE fk_soc = ".((int) $socid);
  238. if ($default > -1) {
  239. $sql .= " AND default_rib = ".((int) $default);
  240. }
  241. if ($type) {
  242. $sql .= " AND type = '".$this->db->escape($type)."'";
  243. }
  244. }
  245. $resql = $this->db->query($sql);
  246. if ($resql) {
  247. if ($this->db->num_rows($resql)) {
  248. $obj = $this->db->fetch_object($resql);
  249. $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
  250. $this->id = $obj->rowid;
  251. $this->type = $obj->type;
  252. $this->socid = $obj->fk_soc;
  253. $this->bank = $obj->bank;
  254. $this->code_banque = $obj->code_banque;
  255. $this->code_guichet = $obj->code_guichet;
  256. $this->number = $obj->number;
  257. $this->cle_rib = $obj->cle_rib;
  258. $this->bic = $obj->bic;
  259. $this->iban = $obj->iban;
  260. $this->domiciliation = $obj->domiciliation;
  261. $this->proprio = $obj->proprio;
  262. $this->owner_address = $obj->owner_address;
  263. $this->label = $obj->label;
  264. $this->default_rib = $obj->default_rib;
  265. $this->datec = $this->db->jdate($obj->datec);
  266. $this->datem = $this->db->jdate($obj->datem);
  267. $this->rum = $obj->rum;
  268. $this->frstrecur = $obj->frstrecur;
  269. $this->date_rum = $this->db->jdate($obj->date_rum);
  270. $this->stripe_card_ref = $obj->stripe_card_ref;
  271. $this->stripe_account = $obj->stripe_account;
  272. }
  273. $this->db->free($resql);
  274. return 1;
  275. } else {
  276. dol_print_error($this->db);
  277. return -1;
  278. }
  279. }
  280. /**
  281. * Delete a rib from database
  282. *
  283. * @param User $user User deleting
  284. * @param int $notrigger 1=Disable triggers
  285. * @return int <0 if KO, >0 if OK
  286. */
  287. public function delete(User $user = null, $notrigger = 0)
  288. {
  289. $error = 0;
  290. dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
  291. $this->db->begin();
  292. if (!$error && !$notrigger) {
  293. // Call trigger
  294. $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
  295. if ($result < 0) {
  296. $error++;
  297. }
  298. // End call triggers
  299. }
  300. if (!$error) {
  301. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
  302. $sql .= " WHERE rowid = ".((int) $this->id);
  303. if (!$this->db->query($sql)) {
  304. $error++;
  305. $this->errors[] = $this->db->lasterror();
  306. }
  307. }
  308. if (!$error) {
  309. $this->db->commit();
  310. return 1;
  311. } else {
  312. $this->db->rollback();
  313. return -1 * $error;
  314. }
  315. }
  316. /**
  317. * Return RIB
  318. *
  319. * @param boolean $displayriblabel Prepend or Hide Label
  320. * @return string RIB
  321. */
  322. public function getRibLabel($displayriblabel = true)
  323. {
  324. $rib = '';
  325. if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
  326. if ($this->label && $displayriblabel) {
  327. $rib = $this->label." : ";
  328. }
  329. $rib .= $this->iban;
  330. }
  331. return $rib;
  332. }
  333. /**
  334. * Set a BAN as Default
  335. *
  336. * @param int $rib RIB id
  337. * @param string $resetolddefaultfor Reset if we have already a default value for type = 'ban'
  338. * @return int 0 if KO, 1 if OK
  339. */
  340. public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
  341. {
  342. $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
  343. $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
  344. dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
  345. $result1 = $this->db->query($sql1);
  346. if ($result1) {
  347. if ($this->db->num_rows($result1) == 0) {
  348. return 0;
  349. } else {
  350. $obj = $this->db->fetch_object($result1);
  351. $this->db->begin();
  352. $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
  353. $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
  354. if ($resetolddefaultfor) {
  355. $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
  356. }
  357. $result2 = $this->db->query($sql2);
  358. $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
  359. $sql3 .= " WHERE rowid = ".((int) $obj->id);
  360. $result3 = $this->db->query($sql3);
  361. if (!$result2 || !$result3) {
  362. dol_print_error($this->db);
  363. $this->db->rollback();
  364. return -1;
  365. } else {
  366. $this->db->commit();
  367. return 1;
  368. }
  369. }
  370. } else {
  371. dol_print_error($this->db);
  372. return -1;
  373. }
  374. }
  375. /**
  376. * Initialise an instance with random values.
  377. * Used to build previews or test instances.
  378. * id must be 0 if object instance is a specimen.
  379. *
  380. * @return void
  381. */
  382. public function initAsSpecimen()
  383. {
  384. $this->specimen = 1;
  385. $this->ref = 'CBA';
  386. $this->label = 'CustomerCorp Bank account';
  387. $this->bank = 'CustomerCorp Bank';
  388. $this->courant = Account::TYPE_CURRENT;
  389. $this->clos = Account::STATUS_OPEN;
  390. $this->code_banque = '123';
  391. $this->code_guichet = '456';
  392. $this->number = 'CUST12345';
  393. $this->cle_rib = 50;
  394. $this->bic = 'CC12';
  395. $this->iban = 'FR999999999';
  396. $this->domiciliation = 'Bank address of customer corp';
  397. $this->proprio = 'Owner';
  398. $this->owner_address = 'Owner address';
  399. $this->country_id = 1;
  400. $this->rum = 'UMR-CU1212-0007-5-1475405262';
  401. $this->date_rum = dol_now() - 10000;
  402. $this->frstrecur = 'FRST';
  403. $this->socid = 1;
  404. }
  405. }