cpaiement.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. * Copyright (C) 2023 Frédéric France <frederic.france@netlogic.fr>
  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/compta/paiement/class/cpaiement.class.php
  23. * \ingroup facture
  24. * \brief This file is to manage CRUD function of type of payments
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
  28. /**
  29. * Class Cpaiement
  30. */
  31. class Cpaiement extends CommonDict
  32. {
  33. /**
  34. * @var string Id to identify managed objects
  35. */
  36. public $element = 'cpaiement';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'c_paiement';
  41. /**
  42. * @var string
  43. * @deprecated
  44. * @see $label
  45. */
  46. public $libelle;
  47. public $type;
  48. public $active;
  49. public $accountancy_code;
  50. public $module;
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDb $db Database handler
  55. */
  56. public function __construct(DoliDB $db)
  57. {
  58. $this->db = $db;
  59. }
  60. /**
  61. * Create object into database
  62. *
  63. * @param User $user User that creates
  64. * @param bool $notrigger false=launch triggers after, true=disable triggers
  65. *
  66. * @return int Return integer <0 if KO, Id of created object if OK
  67. */
  68. public function create(User $user, $notrigger = false)
  69. {
  70. dol_syslog(__METHOD__, LOG_DEBUG);
  71. $error = 0;
  72. // Clean parameters
  73. if (isset($this->code)) {
  74. $this->code = trim($this->code);
  75. }
  76. if (isset($this->libelle)) {
  77. $this->libelle = trim($this->libelle);
  78. }
  79. if (isset($this->label)) {
  80. $this->label = trim($this->label);
  81. }
  82. if (isset($this->type)) {
  83. $this->type = trim($this->type);
  84. }
  85. if (isset($this->active)) {
  86. $this->active = trim($this->active);
  87. }
  88. if (isset($this->accountancy_code)) {
  89. $this->accountancy_code = trim($this->accountancy_code);
  90. }
  91. if (isset($this->module)) {
  92. $this->module = trim($this->module);
  93. }
  94. // Check parameters
  95. // Put here code to add control on parameters values
  96. // Insert request
  97. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  98. $sql .= 'entity,';
  99. $sql .= 'code,';
  100. $sql .= 'libelle,';
  101. $sql .= 'type,';
  102. $sql .= 'active,';
  103. $sql .= 'accountancy_code,';
  104. $sql .= 'module';
  105. $sql .= ') VALUES (';
  106. $sql .= ' '.(!isset($this->entity) ? getEntity('c_paiement') : $this->entity).',';
  107. $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
  108. $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
  109. $sql .= ' '.(!isset($this->type) ? 'NULL' : $this->type).',';
  110. $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
  111. $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'").',';
  112. $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'");
  113. $sql .= ')';
  114. $this->db->begin();
  115. $resql = $this->db->query($sql);
  116. if (!$resql) {
  117. $error++;
  118. $this->errors[] = 'Error '.$this->db->lasterror();
  119. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  120. }
  121. if (!$error) {
  122. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  123. // Uncomment this and change MYOBJECT to your own tag if you
  124. // want this action to call a trigger.
  125. //if (!$notrigger) {
  126. // // Call triggers
  127. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  128. // if ($result < 0) $error++;
  129. // // End call triggers
  130. //}
  131. }
  132. // Commit or rollback
  133. if ($error) {
  134. $this->db->rollback();
  135. return -1 * $error;
  136. } else {
  137. $this->db->commit();
  138. return $this->id;
  139. }
  140. }
  141. /**
  142. * Load object in memory from the database
  143. *
  144. * @param int $id Id object
  145. * @param string $ref Ref
  146. *
  147. * @return int Return integer <0 if KO, 0 if not found, >0 if OK
  148. */
  149. public function fetch($id, $ref = null)
  150. {
  151. dol_syslog(__METHOD__, LOG_DEBUG);
  152. $sql = 'SELECT';
  153. $sql .= ' t.id,';
  154. $sql .= " t.code,";
  155. $sql .= " t.libelle as label,";
  156. $sql .= " t.type,";
  157. $sql .= " t.active,";
  158. $sql .= " t.accountancy_code,";
  159. $sql .= " t.module";
  160. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  161. if (null !== $ref) {
  162. $sql .= ' WHERE t.entity IN ('.getEntity('c_paiement').')';
  163. $sql .= " AND t.code = '".$this->db->escape($ref)."'";
  164. } else {
  165. $sql .= ' WHERE t.id = '.((int) $id);
  166. }
  167. $resql = $this->db->query($sql);
  168. if ($resql) {
  169. $numrows = $this->db->num_rows($resql);
  170. if ($numrows) {
  171. $obj = $this->db->fetch_object($resql);
  172. $this->id = $obj->id;
  173. $this->code = $obj->code;
  174. $this->libelle = $obj->label;
  175. $this->label = $obj->label;
  176. $this->type = $obj->type;
  177. $this->active = $obj->active;
  178. $this->accountancy_code = $obj->accountancy_code;
  179. $this->module = $obj->module;
  180. }
  181. $this->db->free($resql);
  182. if ($numrows) {
  183. return 1;
  184. } else {
  185. return 0;
  186. }
  187. } else {
  188. $this->errors[] = 'Error '.$this->db->lasterror();
  189. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  190. return -1;
  191. }
  192. }
  193. /**
  194. * Update object into database
  195. *
  196. * @param User $user User that modifies
  197. * @param bool $notrigger false=launch triggers after, true=disable triggers
  198. *
  199. * @return int Return integer <0 if KO, >0 if OK
  200. */
  201. public function update(User $user, $notrigger = false)
  202. {
  203. $error = 0;
  204. dol_syslog(__METHOD__, LOG_DEBUG);
  205. // Clean parameters
  206. if (isset($this->code)) {
  207. $this->code = trim($this->code);
  208. }
  209. if (isset($this->libelle)) {
  210. $this->libelle = trim($this->libelle);
  211. }
  212. if (isset($this->label)) {
  213. $this->label = trim($this->label);
  214. }
  215. if (isset($this->type)) {
  216. $this->type = trim($this->type);
  217. }
  218. if (isset($this->active)) {
  219. $this->active = trim($this->active);
  220. }
  221. if (isset($this->accountancy_code)) {
  222. $this->accountancy_code = trim($this->accountancy_code);
  223. }
  224. if (isset($this->module)) {
  225. $this->module = trim($this->module);
  226. }
  227. // Check parameters
  228. // Put here code to add a control on parameters values
  229. // Update request
  230. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  231. $sql .= ' id = '.(isset($this->id) ? $this->id : "null").',';
  232. $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
  233. $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
  234. $sql .= ' type = '.(isset($this->type) ? $this->type : "null").',';
  235. $sql .= ' active = '.(isset($this->active) ? $this->active : "null").',';
  236. $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null").',';
  237. $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null");
  238. $sql .= ' WHERE id = '.((int) $this->id);
  239. $this->db->begin();
  240. $resql = $this->db->query($sql);
  241. if (!$resql) {
  242. $error++;
  243. $this->errors[] = 'Error '.$this->db->lasterror();
  244. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  245. }
  246. // Uncomment this and change MYOBJECT to your own tag if you
  247. // want this action calls a trigger.
  248. //if (!$error && !$notrigger) {
  249. // // Call triggers
  250. // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  251. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  252. // // End call triggers
  253. //}
  254. // Commit or rollback
  255. if ($error) {
  256. $this->db->rollback();
  257. return -1 * $error;
  258. } else {
  259. $this->db->commit();
  260. return 1;
  261. }
  262. }
  263. /**
  264. * Delete object in database
  265. *
  266. * @param User $user User that deletes
  267. * @param bool $notrigger false=launch triggers after, true=disable triggers
  268. *
  269. * @return int Return integer <0 if KO, >0 if OK
  270. */
  271. public function delete(User $user, $notrigger = false)
  272. {
  273. dol_syslog(__METHOD__, LOG_DEBUG);
  274. $error = 0;
  275. $this->db->begin();
  276. // Uncomment this and change MYOBJECT to your own tag if you
  277. // want this action calls a trigger.
  278. //if (!$error && !$notrigger) {
  279. // // Call triggers
  280. // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
  281. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  282. // // End call triggers
  283. //}
  284. if (!$error) {
  285. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  286. $sql .= ' WHERE id = '.((int) $this->id);
  287. $resql = $this->db->query($sql);
  288. if (!$resql) {
  289. $error++;
  290. $this->errors[] = 'Error '.$this->db->lasterror();
  291. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  292. }
  293. }
  294. // Commit or rollback
  295. if ($error) {
  296. $this->db->rollback();
  297. return -1 * $error;
  298. } else {
  299. $this->db->commit();
  300. return 1;
  301. }
  302. }
  303. /**
  304. * Initialise object with example values
  305. * Id must be 0 if object instance is a specimen
  306. *
  307. * @return void
  308. */
  309. public function initAsSpecimen()
  310. {
  311. $this->id = 0;
  312. $this->code = '';
  313. $this->libelle = '';
  314. $this->label = '';
  315. $this->type = '';
  316. $this->active = '';
  317. $this->accountancy_code = '';
  318. $this->module = '';
  319. }
  320. }