cpaiement.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/paiement/class/cpaiement.class.php
  22. * \ingroup facture
  23. * \brief This file is to manage CRUD function of type of payments
  24. */
  25. /**
  26. * Class Cpaiement
  27. */
  28. class Cpaiement
  29. {
  30. /**
  31. * @var string Id to identify managed objects
  32. */
  33. public $element = 'cpaiement';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element = 'c_paiement';
  38. public $code;
  39. /**
  40. * @deprecated
  41. * @see $label
  42. */
  43. public $libelle;
  44. public $label;
  45. public $type;
  46. public $active;
  47. public $accountancy_code;
  48. public $module;
  49. /**
  50. * Constructor
  51. *
  52. * @param DoliDb $db Database handler
  53. */
  54. public function __construct(DoliDB $db)
  55. {
  56. $this->db = $db;
  57. }
  58. /**
  59. * Create object into database
  60. *
  61. * @param User $user User that creates
  62. * @param bool $notrigger false=launch triggers after, true=disable triggers
  63. *
  64. * @return int <0 if KO, Id of created object if OK
  65. */
  66. public function create(User $user, $notrigger = false)
  67. {
  68. dol_syslog(__METHOD__, LOG_DEBUG);
  69. $error = 0;
  70. // Clean parameters
  71. if (isset($this->code)) {
  72. $this->code = trim($this->code);
  73. }
  74. if (isset($this->libelle)) {
  75. $this->libelle = trim($this->libelle);
  76. }
  77. if (isset($this->label)) {
  78. $this->label = trim($this->label);
  79. }
  80. if (isset($this->type)) {
  81. $this->type = trim($this->type);
  82. }
  83. if (isset($this->active)) {
  84. $this->active = trim($this->active);
  85. }
  86. if (isset($this->accountancy_code)) {
  87. $this->accountancy_code = trim($this->accountancy_code);
  88. }
  89. if (isset($this->module)) {
  90. $this->module = trim($this->module);
  91. }
  92. // Check parameters
  93. // Put here code to add control on parameters values
  94. // Insert request
  95. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  96. $sql .= 'entity,';
  97. $sql .= 'code,';
  98. $sql .= 'libelle,';
  99. $sql .= 'type,';
  100. $sql .= 'active,';
  101. $sql .= 'accountancy_code,';
  102. $sql .= 'module';
  103. $sql .= ') VALUES (';
  104. $sql .= ' '.(!isset($this->entity) ?getEntity('c_paiement') : $this->entity).',';
  105. $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
  106. $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
  107. $sql .= ' '.(!isset($this->type) ? 'NULL' : $this->type).',';
  108. $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
  109. $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'").',';
  110. $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'");
  111. $sql .= ')';
  112. $this->db->begin();
  113. $resql = $this->db->query($sql);
  114. if (!$resql) {
  115. $error++;
  116. $this->errors[] = 'Error '.$this->db->lasterror();
  117. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  118. }
  119. if (!$error) {
  120. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  121. // Uncomment this and change MYOBJECT to your own tag if you
  122. // want this action to call a trigger.
  123. //if (!$notrigger) {
  124. // // Call triggers
  125. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  126. // if ($result < 0) $error++;
  127. // // End call triggers
  128. //}
  129. }
  130. // Commit or rollback
  131. if ($error) {
  132. $this->db->rollback();
  133. return -1 * $error;
  134. } else {
  135. $this->db->commit();
  136. return $this->id;
  137. }
  138. }
  139. /**
  140. * Load object in memory from the database
  141. *
  142. * @param int $id Id object
  143. * @param string $ref Ref
  144. *
  145. * @return int <0 if KO, 0 if not found, >0 if OK
  146. */
  147. public function fetch($id, $ref = null)
  148. {
  149. dol_syslog(__METHOD__, LOG_DEBUG);
  150. $sql = 'SELECT';
  151. $sql .= ' t.id,';
  152. $sql .= " t.code,";
  153. $sql .= " t.libelle as label,";
  154. $sql .= " t.type,";
  155. $sql .= " t.active,";
  156. $sql .= " t.accountancy_code,";
  157. $sql .= " t.module";
  158. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  159. if (null !== $ref) {
  160. $sql .= ' WHERE t.entity IN ('.getEntity('c_paiement').')';
  161. $sql .= " AND t.code = '".$this->db->escape($ref)."'";
  162. } else {
  163. $sql .= ' WHERE t.id = '.((int) $id);
  164. }
  165. $resql = $this->db->query($sql);
  166. if ($resql) {
  167. $numrows = $this->db->num_rows($resql);
  168. if ($numrows) {
  169. $obj = $this->db->fetch_object($resql);
  170. $this->id = $obj->id;
  171. $this->code = $obj->code;
  172. $this->libelle = $obj->label;
  173. $this->label = $obj->label;
  174. $this->type = $obj->type;
  175. $this->active = $obj->active;
  176. $this->accountancy_code = $obj->accountancy_code;
  177. $this->module = $obj->module;
  178. }
  179. $this->db->free($resql);
  180. if ($numrows) {
  181. return 1;
  182. } else {
  183. return 0;
  184. }
  185. } else {
  186. $this->errors[] = 'Error '.$this->db->lasterror();
  187. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  188. return -1;
  189. }
  190. }
  191. /**
  192. * Update object into database
  193. *
  194. * @param User $user User that modifies
  195. * @param bool $notrigger false=launch triggers after, true=disable triggers
  196. *
  197. * @return int <0 if KO, >0 if OK
  198. */
  199. public function update(User $user, $notrigger = false)
  200. {
  201. $error = 0;
  202. dol_syslog(__METHOD__, LOG_DEBUG);
  203. // Clean parameters
  204. if (isset($this->code)) {
  205. $this->code = trim($this->code);
  206. }
  207. if (isset($this->libelle)) {
  208. $this->libelle = trim($this->libelle);
  209. }
  210. if (isset($this->label)) {
  211. $this->label = trim($this->label);
  212. }
  213. if (isset($this->type)) {
  214. $this->type = trim($this->type);
  215. }
  216. if (isset($this->active)) {
  217. $this->active = trim($this->active);
  218. }
  219. if (isset($this->accountancy_code)) {
  220. $this->accountancy_code = trim($this->accountancy_code);
  221. }
  222. if (isset($this->module)) {
  223. $this->module = trim($this->module);
  224. }
  225. // Check parameters
  226. // Put here code to add a control on parameters values
  227. // Update request
  228. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  229. $sql .= ' id = '.(isset($this->id) ? $this->id : "null").',';
  230. $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
  231. $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
  232. $sql .= ' type = '.(isset($this->type) ? $this->type : "null").',';
  233. $sql .= ' active = '.(isset($this->active) ? $this->active : "null").',';
  234. $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null").',';
  235. $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null");
  236. $sql .= ' WHERE id = '.((int) $this->id);
  237. $this->db->begin();
  238. $resql = $this->db->query($sql);
  239. if (!$resql) {
  240. $error++;
  241. $this->errors[] = 'Error '.$this->db->lasterror();
  242. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  243. }
  244. // Uncomment this and change MYOBJECT to your own tag if you
  245. // want this action calls a trigger.
  246. //if (!$error && !$notrigger) {
  247. // // Call triggers
  248. // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  249. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  250. // // End call triggers
  251. //}
  252. // Commit or rollback
  253. if ($error) {
  254. $this->db->rollback();
  255. return -1 * $error;
  256. } else {
  257. $this->db->commit();
  258. return 1;
  259. }
  260. }
  261. /**
  262. * Delete object in database
  263. *
  264. * @param User $user User that deletes
  265. * @param bool $notrigger false=launch triggers after, true=disable triggers
  266. *
  267. * @return int <0 if KO, >0 if OK
  268. */
  269. public function delete(User $user, $notrigger = false)
  270. {
  271. dol_syslog(__METHOD__, LOG_DEBUG);
  272. $error = 0;
  273. $this->db->begin();
  274. // Uncomment this and change MYOBJECT to your own tag if you
  275. // want this action calls a trigger.
  276. //if (!$error && !$notrigger) {
  277. // // Call triggers
  278. // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
  279. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  280. // // End call triggers
  281. //}
  282. if (!$error) {
  283. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  284. $sql .= ' WHERE id = '.((int) $this->id);
  285. $resql = $this->db->query($sql);
  286. if (!$resql) {
  287. $error++;
  288. $this->errors[] = 'Error '.$this->db->lasterror();
  289. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  290. }
  291. }
  292. // Commit or rollback
  293. if ($error) {
  294. $this->db->rollback();
  295. return -1 * $error;
  296. } else {
  297. $this->db->commit();
  298. return 1;
  299. }
  300. }
  301. /**
  302. * Initialise object with example values
  303. * Id must be 0 if object instance is a specimen
  304. *
  305. * @return void
  306. */
  307. public function initAsSpecimen()
  308. {
  309. $this->id = 0;
  310. $this->code = '';
  311. $this->libelle = '';
  312. $this->label = '';
  313. $this->type = '';
  314. $this->active = '';
  315. $this->accountancy_code = '';
  316. $this->module = '';
  317. }
  318. }