userbankaccount.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  7. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/user/class/userbankaccount.class.php
  24. * \ingroup user
  25. * \brief File of class to manage bank accounts description of users
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. /**
  29. * Class to manage bank accounts description of users
  30. */
  31. class UserBankAccount extends Account
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'user_bank_account';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'user_rib';
  41. /**
  42. * Date creation record (datec)
  43. *
  44. * @var integer
  45. */
  46. public $datec;
  47. /**
  48. * Date modification record (tms)
  49. *
  50. * @var integer
  51. */
  52. public $datem;
  53. /**
  54. * User id of bank account
  55. *
  56. * @var integer
  57. */
  58. public $userid;
  59. /**
  60. * Constructor
  61. *
  62. * @param DoliDB $db Database handler
  63. */
  64. public function __construct(DoliDB $db)
  65. {
  66. $this->db = $db;
  67. $this->userid = 0;
  68. $this->solde = 0;
  69. $this->error_number = 0;
  70. }
  71. /**
  72. * Create bank information record
  73. *
  74. * @param User $user User
  75. * @param int $notrigger 1=Disable triggers
  76. * @return int <0 if KO, >= 0 if OK
  77. */
  78. public function create(User $user = null, $notrigger = 0)
  79. {
  80. $now = dol_now();
  81. $sql = "INSERT INTO ".$this->db->prefix()."user_rib (fk_user, datec)";
  82. $sql .= " VALUES (".$this->userid.", '".$this->db->idate($now)."')";
  83. $resql = $this->db->query($sql);
  84. if ($resql) {
  85. if ($this->db->affected_rows($resql)) {
  86. $this->id = $this->db->last_insert_id($this->db->prefix()."user_rib");
  87. return $this->update($user);
  88. }
  89. } else {
  90. print $this->db->error();
  91. return 0;
  92. }
  93. }
  94. /**
  95. * Update bank account
  96. *
  97. * @param User $user Object user
  98. * @param int $notrigger 1=Disable triggers
  99. * @return int <=0 if KO, >0 if OK
  100. */
  101. public function update(User $user = null, $notrigger = 0)
  102. {
  103. if (!$this->id) {
  104. $this->create();
  105. }
  106. $sql = "UPDATE ".$this->db->prefix()."user_rib SET";
  107. $sql .= " bank = '".$this->db->escape($this->bank)."'";
  108. $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
  109. $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
  110. $sql .= ",number='".$this->db->escape($this->number)."'";
  111. $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
  112. $sql .= ",bic='".$this->db->escape($this->bic)."'";
  113. $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
  114. $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
  115. $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
  116. $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  117. $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
  118. $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
  119. $sql .= ",fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
  120. if (trim($this->label) != '') {
  121. $sql .= ",label = '".$this->db->escape($this->label)."'";
  122. } else {
  123. $sql .= ",label = NULL";
  124. }
  125. $sql .= " WHERE rowid = ".((int) $this->id);
  126. $result = $this->db->query($sql);
  127. if ($result) {
  128. return 1;
  129. } else {
  130. dol_print_error($this->db);
  131. return 0;
  132. }
  133. }
  134. /**
  135. * Load record from database
  136. *
  137. * @param int $id Id of record
  138. * @param string $ref Ref of record
  139. * @param int $userid User id
  140. * @return int <0 if KO, >0 if OK
  141. */
  142. public function fetch($id, $ref = '', $userid = 0)
  143. {
  144. if (empty($id) && empty($ref) && empty($userid)) {
  145. return -1;
  146. }
  147. $sql = "SELECT ur.rowid, ur.fk_user, ur.entity, ur.bank, ur.number, ur.code_banque, ur.code_guichet, ur.cle_rib, ur.bic, ur.iban_prefix as iban, ur.domiciliation, ur.proprio";
  148. $sql .= ", ur.owner_address, ur.label, ur.datec, ur.tms as datem";
  149. $sql .= ', ur.currency_code, ur.state_id, ur.fk_country as country_id';
  150. $sql .= ', c.code as country_code, c.label as country';
  151. $sql .= ', d.code_departement as state_code, d.nom as state';
  152. $sql .= " FROM ".$this->db->prefix()."user_rib as ur";
  153. $sql .= ' LEFT JOIN '.$this->db->prefix().'c_country as c ON ur.fk_country=c.rowid';
  154. $sql .= ' LEFT JOIN '.$this->db->prefix().'c_departements as d ON ur.state_id=d.rowid';
  155. if ($id) {
  156. $sql .= " WHERE ur.rowid = ".((int) $id);
  157. }
  158. if ($ref) {
  159. $sql .= " WHERE ur.label = '".$this->db->escape($ref)."'";
  160. }
  161. if ($userid) {
  162. $sql .= " WHERE ur.fk_user = ".((int) $userid);
  163. }
  164. $resql = $this->db->query($sql);
  165. if ($resql) {
  166. if ($this->db->num_rows($resql)) {
  167. $obj = $this->db->fetch_object($resql);
  168. $this->id = $obj->rowid;
  169. $this->userid = $obj->fk_user;
  170. $this->bank = $obj->bank;
  171. $this->code_banque = $obj->code_banque;
  172. $this->code_guichet = $obj->code_guichet;
  173. $this->number = $obj->number;
  174. $this->cle_rib = $obj->cle_rib;
  175. $this->bic = $obj->bic;
  176. $this->iban = $obj->iban;
  177. $this->domiciliation = $obj->domiciliation;
  178. $this->proprio = $obj->proprio;
  179. $this->owner_address = $obj->owner_address;
  180. $this->label = $obj->label;
  181. $this->datec = $this->db->jdate($obj->datec);
  182. $this->datem = $this->db->jdate($obj->datem);
  183. $this->currency_code = $obj->currency_code;
  184. $this->state_id = $obj->state_id;
  185. $this->state_code = $obj->state_code;
  186. $this->state = $obj->state;
  187. $this->country_id = $obj->country_id;
  188. $this->country_code = $obj->country_code;
  189. $this->country = $obj->country;
  190. }
  191. $this->db->free($resql);
  192. return 1;
  193. } else {
  194. dol_print_error($this->db);
  195. return -1;
  196. }
  197. }
  198. /**
  199. * Delete user bank account from database
  200. *
  201. * @param User $user User deleting
  202. * @return int <0 if KO, >0 if OK
  203. */
  204. public function delete(User $user = null)
  205. {
  206. $error = 0;
  207. $this->db->begin();
  208. // Delete link between tag and bank account
  209. /*
  210. if (!$error) {
  211. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
  212. $sql .= " WHERE fk_account = ".((int) $this->id);
  213. $resql = $this->db->query($sql);
  214. if (!$resql) {
  215. $error++;
  216. $this->error = "Error ".$this->db->lasterror();
  217. }
  218. }
  219. */
  220. if (!$error) {
  221. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  222. $sql .= " WHERE rowid = ".((int) $this->id);
  223. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  224. $result = $this->db->query($sql);
  225. if ($result) {
  226. // Remove extrafields
  227. /*
  228. if (!$error) {
  229. $result = $this->deleteExtraFields();
  230. if ($result < 0) {
  231. $error++;
  232. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  233. }
  234. }*/
  235. } else {
  236. $error++;
  237. $this->error = "Error ".$this->db->lasterror();
  238. }
  239. }
  240. if (!$error) {
  241. $this->db->commit();
  242. return 1;
  243. } else {
  244. $this->db->rollback();
  245. return -1;
  246. }
  247. }
  248. /**
  249. * Return RIB
  250. *
  251. * @param boolean $displayriblabel Prepend or Hide Label
  252. * @return string RIB
  253. */
  254. public function getRibLabel($displayriblabel = true)
  255. {
  256. $rib = '';
  257. if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib) {
  258. if ($this->label && $displayriblabel) {
  259. $rib = $this->label." : ";
  260. }
  261. $rib .= $this->iban;
  262. }
  263. return $rib;
  264. }
  265. }