bankcateg.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* Copyright (C) 2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file compta/bank/class/bankcateg.class.php
  21. * \ingroup bank
  22. * \brief This file is CRUD class file (Create/Read/Update/Delete) for bank categories
  23. */
  24. /**
  25. * Class to manage bank categories
  26. */
  27. class BankCateg // extends CommonObject
  28. {
  29. //public $element='bank_categ'; //!< Id that identify managed objects
  30. //public $table_element='bank_categ'; //!< Name of table without prefix where object is stored
  31. /**
  32. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  33. */
  34. public $picto = 'generic';
  35. /**
  36. * @var int ID
  37. */
  38. public $id;
  39. /**
  40. * @var string bank categories label
  41. */
  42. public $label;
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. */
  48. public function __construct(DoliDB $db)
  49. {
  50. $this->db = $db;
  51. }
  52. /**
  53. * Create in database
  54. *
  55. * @param User $user User that create
  56. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  57. * @return int <0 if KO, Id of created object if OK
  58. */
  59. public function create(User $user, $notrigger = 0)
  60. {
  61. global $conf;
  62. $error = 0;
  63. // Clean parameters
  64. if (isset($this->label)) {
  65. $this->label = trim($this->label);
  66. }
  67. // Insert request
  68. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_categ (";
  69. $sql .= "label";
  70. $sql .= ", entity";
  71. $sql .= ") VALUES (";
  72. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'")."";
  73. $sql .= ", ".((int) $conf->entity);
  74. $sql .= ")";
  75. $this->db->begin();
  76. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  77. $resql = $this->db->query($sql);
  78. if (!$resql) {
  79. $error++;
  80. $this->errors[] = "Error ".$this->db->lasterror();
  81. }
  82. if (!$error) {
  83. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_categ");
  84. }
  85. // Commit or rollback
  86. if ($error) {
  87. foreach ($this->errors as $errmsg) {
  88. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  89. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  90. }
  91. $this->db->rollback();
  92. return -1 * $error;
  93. } else {
  94. $this->db->commit();
  95. return $this->id;
  96. }
  97. }
  98. /**
  99. * Load object in memory from database
  100. *
  101. * @param int $id Id object
  102. * @return int <0 if KO, >0 if OK
  103. */
  104. public function fetch($id)
  105. {
  106. global $conf;
  107. $sql = "SELECT";
  108. $sql .= " t.rowid,";
  109. $sql .= " t.label";
  110. $sql .= " FROM ".MAIN_DB_PREFIX."bank_categ as t";
  111. $sql .= " WHERE t.rowid = ".((int) $id);
  112. $sql .= " AND t.entity = ".$conf->entity;
  113. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  114. $resql = $this->db->query($sql);
  115. if ($resql) {
  116. if ($this->db->num_rows($resql)) {
  117. $obj = $this->db->fetch_object($resql);
  118. $this->id = $obj->rowid;
  119. $this->label = $obj->label;
  120. }
  121. $this->db->free($resql);
  122. return 1;
  123. } else {
  124. $this->error = "Error ".$this->db->lasterror();
  125. return -1;
  126. }
  127. }
  128. /**
  129. * Update database
  130. *
  131. * @param User $user User that modify
  132. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  133. * @return int <0 if KO, >0 if OK
  134. */
  135. public function update(User $user = null, $notrigger = 0)
  136. {
  137. global $conf;
  138. $error = 0;
  139. // Clean parameters
  140. if (isset($this->label)) {
  141. $this->label = trim($this->label);
  142. }
  143. // Check parameters
  144. // Put here code to add control on parameters values
  145. // Update request
  146. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_categ SET";
  147. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null")."";
  148. $sql .= " WHERE rowid=".((int) $this->id);
  149. $sql .= " AND entity = ".$conf->entity;
  150. $this->db->begin();
  151. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  152. $resql = $this->db->query($sql);
  153. if (!$resql) {
  154. $error++;
  155. $this->errors[] = "Error ".$this->db->lasterror();
  156. }
  157. // Commit or rollback
  158. if ($error) {
  159. foreach ($this->errors as $errmsg) {
  160. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  161. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  162. }
  163. $this->db->rollback();
  164. return -1 * $error;
  165. } else {
  166. $this->db->commit();
  167. return 1;
  168. }
  169. }
  170. /**
  171. * Delete object in database
  172. *
  173. * @param User $user User that delete
  174. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  175. * @return int <0 if KO, >0 if OK
  176. */
  177. public function delete(User $user, $notrigger = 0)
  178. {
  179. global $conf;
  180. $error = 0;
  181. $this->db->begin();
  182. // Delete link between tag and bank account
  183. if (!$error) {
  184. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
  185. $sql .= " WHERE fk_categorie = ".((int) $this->id);
  186. $resql = $this->db->query($sql);
  187. if (!$resql) {
  188. $error++;
  189. $this->errors[] = "Error ".$this->db->lasterror();
  190. }
  191. }
  192. // Delete link between tag and bank lines
  193. if (!$error) {
  194. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class";
  195. $sql .= " WHERE fk_categ = ".((int) $this->id);
  196. $resql = $this->db->query($sql);
  197. if (!$resql) {
  198. $error++;
  199. $this->errors[] = "Error ".$this->db->lasterror();
  200. }
  201. }
  202. // Delete bank categ
  203. if (!$error) {
  204. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_categ";
  205. $sql .= " WHERE rowid=".((int) $this->id);
  206. $resql = $this->db->query($sql);
  207. if (!$resql) {
  208. $error++;
  209. $this->errors[] = "Error ".$this->db->lasterror();
  210. }
  211. }
  212. // Commit or rollback
  213. if ($error) {
  214. foreach ($this->errors as $errmsg) {
  215. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  216. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  217. }
  218. $this->db->rollback();
  219. return -1 * $error;
  220. } else {
  221. $this->db->commit();
  222. return 1;
  223. }
  224. }
  225. /**
  226. * Load an object from its id and create a new one in database
  227. *
  228. * @param User $user User making the clone
  229. * @param int $fromid Id of object to clone
  230. * @return int New id of clone
  231. */
  232. public function createFromClone(User $user, $fromid)
  233. {
  234. $error = 0;
  235. $object = new BankCateg($this->db);
  236. $this->db->begin();
  237. // Load source object
  238. $object->fetch($fromid);
  239. $object->id = 0;
  240. $object->statut = 0;
  241. // Create clone
  242. $object->context['createfromclone'] = 'createfromclone';
  243. $result = $object->create($user);
  244. // Other options
  245. if ($result < 0) {
  246. $this->error = $object->error;
  247. $error++;
  248. }
  249. unset($object->context['createfromclone']);
  250. // End
  251. if (!$error) {
  252. $this->db->commit();
  253. return $object->id;
  254. } else {
  255. $this->db->rollback();
  256. return -1;
  257. }
  258. }
  259. /**
  260. * Returns all bank categories
  261. *
  262. * @return BankCateg[]
  263. */
  264. public function fetchAll()
  265. {
  266. global $conf;
  267. $return = array();
  268. $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."bank_categ WHERE entity = ".$conf->entity." ORDER BY label";
  269. $resql = $this->db->query($sql);
  270. if ($resql) {
  271. while ($obj = $this->db->fetch_object($resql)) {
  272. $tmp = new BankCateg($this->db);
  273. $tmp->id = $obj->rowid;
  274. $tmp->label = $obj->label;
  275. $return[] = $tmp;
  276. }
  277. }
  278. return $return;
  279. }
  280. /**
  281. * Initialise an instance with random values.
  282. * Used to build previews or test instances.
  283. * id must be 0 if object instance is a specimen.
  284. *
  285. * @return void
  286. */
  287. public function initAsSpecimen()
  288. {
  289. $this->id = 0;
  290. $this->label = '';
  291. }
  292. }