paymentterm.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file compta/facture/class/paymentterm.class.php
  19. * \ingroup facture
  20. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  21. */
  22. /**
  23. * Class to manage payment terms records in dictionary
  24. */
  25. class PaymentTerm // extends CommonObject
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var string Error code (or message)
  33. */
  34. public $error = '';
  35. /**
  36. * @var string[] Error codes (or messages)
  37. */
  38. public $errors = array();
  39. //public $element='c_payment_term'; //!< Id that identify managed objects
  40. //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
  41. public $context = array();
  42. /**
  43. * @var int ID
  44. */
  45. public $id;
  46. /**
  47. * @var int Entity ID
  48. */
  49. public $entity;
  50. public $code;
  51. public $sortorder;
  52. public $active;
  53. public $libelle;
  54. public $libelle_facture;
  55. public $type_cdr;
  56. public $nbjour;
  57. public $decalage;
  58. /**
  59. * Constructor
  60. *
  61. * @param DoliDB $db Database handler
  62. */
  63. public function __construct(DoliDB $db)
  64. {
  65. $this->db = $db;
  66. }
  67. /**
  68. * Create in database
  69. *
  70. * @param User $user User that create
  71. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  72. * @return int Return integer <0 if KO, Id of created object if OK
  73. */
  74. public function create($user, $notrigger = 0)
  75. {
  76. global $conf, $langs;
  77. $error = 0;
  78. // Clean parameters
  79. if (isset($this->code)) {
  80. $this->code = trim($this->code);
  81. }
  82. if (isset($this->sortorder)) {
  83. $this->sortorder = trim($this->sortorder);
  84. }
  85. if (isset($this->active)) {
  86. $this->active = trim($this->active);
  87. }
  88. if (isset($this->libelle)) {
  89. $this->libelle = trim($this->libelle);
  90. }
  91. if (isset($this->libelle_facture)) {
  92. $this->libelle_facture = trim($this->libelle_facture);
  93. }
  94. if (isset($this->type_cdr)) {
  95. $this->type_cdr = trim($this->type_cdr);
  96. }
  97. if (isset($this->nbjour)) {
  98. $this->nbjour = trim($this->nbjour);
  99. }
  100. if (isset($this->decalage)) {
  101. $this->decalage = trim($this->decalage);
  102. }
  103. // Check parameters
  104. // Put here code to add control on parameters values
  105. // Insert request
  106. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
  107. $sql .= "entity,";
  108. $sql .= "code,";
  109. $sql .= "sortorder,";
  110. $sql .= "active,";
  111. $sql .= "libelle,";
  112. $sql .= "libelle_facture,";
  113. $sql .= "type_cdr,";
  114. $sql .= "nbjour,";
  115. $sql .= "decalage";
  116. $sql .= ") VALUES (";
  117. $sql .= " ".(!isset($this->entity) ? getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
  118. $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
  119. $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
  120. $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
  121. $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
  122. $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
  123. $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
  124. $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
  125. $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
  126. $sql .= ")";
  127. $this->db->begin();
  128. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  129. $resql = $this->db->query($sql);
  130. if (!$resql) {
  131. $error++;
  132. $this->errors[] = "Error ".$this->db->lasterror();
  133. }
  134. if (!$error) {
  135. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
  136. }
  137. // Commit or rollback
  138. if ($error) {
  139. foreach ($this->errors as $errmsg) {
  140. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  141. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  142. }
  143. $this->db->rollback();
  144. return -1 * $error;
  145. } else {
  146. $this->db->commit();
  147. return $this->id;
  148. }
  149. }
  150. /**
  151. * Load object in memory from database
  152. *
  153. * @param int $id Id object
  154. * @param string $code Code object
  155. * @return int Return integer <0 if KO, >0 if OK
  156. */
  157. public function fetch($id, $code = '')
  158. {
  159. $sql = "SELECT";
  160. $sql .= " t.rowid,";
  161. $sql .= " t.entity,";
  162. $sql .= " t.code,";
  163. $sql .= " t.sortorder,";
  164. $sql .= " t.active,";
  165. $sql .= " t.libelle,";
  166. $sql .= " t.libelle_facture,";
  167. $sql .= " t.type_cdr,";
  168. $sql .= " t.nbjour,";
  169. $sql .= " t.decalage";
  170. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  171. if ($id) {
  172. $sql .= " WHERE t.rowid = ".((int) $id);
  173. }
  174. if ($code) {
  175. $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
  176. }
  177. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  178. $resql = $this->db->query($sql);
  179. if ($resql) {
  180. if ($this->db->num_rows($resql)) {
  181. $obj = $this->db->fetch_object($resql);
  182. $this->id = $obj->rowid;
  183. $this->code = $obj->code;
  184. $this->sortorder = $obj->sortorder;
  185. $this->active = $obj->active;
  186. $this->libelle = $obj->libelle;
  187. $this->libelle_facture = $obj->libelle_facture;
  188. $this->type_cdr = $obj->type_cdr;
  189. $this->nbjour = $obj->nbjour;
  190. $this->decalage = $obj->decalage;
  191. }
  192. $this->db->free($resql);
  193. return 1;
  194. } else {
  195. $this->error = "Error ".$this->db->lasterror();
  196. return -1;
  197. }
  198. }
  199. /**
  200. * Return id of default payment term
  201. *
  202. * @return int Return integer <0 if KO, >0 if OK
  203. */
  204. public function getDefaultId()
  205. {
  206. global $langs;
  207. $ret = 0;
  208. $sql = "SELECT";
  209. $sql .= " t.rowid";
  210. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  211. $sql .= " WHERE t.code = 'RECEP'";
  212. $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
  213. dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
  214. $resql = $this->db->query($sql);
  215. if ($resql) {
  216. if ($this->db->num_rows($resql)) {
  217. $obj = $this->db->fetch_object($resql);
  218. if ($obj) {
  219. $ret = $obj->rowid;
  220. }
  221. }
  222. $this->db->free($resql);
  223. return $ret;
  224. } else {
  225. $this->error = "Error ".$this->db->lasterror();
  226. return -1;
  227. }
  228. }
  229. /**
  230. * Update database
  231. *
  232. * @param User $user User that modify
  233. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  234. * @return int Return integer <0 if KO, >0 if OK
  235. */
  236. public function update($user = null, $notrigger = 0)
  237. {
  238. global $conf, $langs;
  239. $error = 0;
  240. // Clean parameters
  241. if (isset($this->code)) {
  242. $this->code = trim($this->code);
  243. }
  244. if (isset($this->sortorder)) {
  245. $this->sortorder = trim($this->sortorder);
  246. }
  247. if (isset($this->active)) {
  248. $this->active = trim($this->active);
  249. }
  250. if (isset($this->libelle)) {
  251. $this->libelle = trim($this->libelle);
  252. }
  253. if (isset($this->libelle_facture)) {
  254. $this->libelle_facture = trim($this->libelle_facture);
  255. }
  256. if (isset($this->type_cdr)) {
  257. $this->type_cdr = trim($this->type_cdr);
  258. }
  259. if (isset($this->nbjour)) {
  260. $this->nbjour = trim($this->nbjour);
  261. }
  262. if (isset($this->decalage)) {
  263. $this->decalage = trim($this->decalage);
  264. }
  265. // Check parameters
  266. // Put here code to add control on parameters values
  267. // Update request
  268. $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
  269. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  270. $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
  271. $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
  272. $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
  273. $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
  274. $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
  275. $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
  276. $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null");
  277. $sql .= " WHERE rowid = ".((int) $this->id);
  278. $this->db->begin();
  279. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  280. $resql = $this->db->query($sql);
  281. if (!$resql) {
  282. $error++;
  283. $this->errors[] = "Error ".$this->db->lasterror();
  284. }
  285. // Commit or rollback
  286. if ($error) {
  287. foreach ($this->errors as $errmsg) {
  288. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  289. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  290. }
  291. $this->db->rollback();
  292. return -1 * $error;
  293. } else {
  294. $this->db->commit();
  295. return 1;
  296. }
  297. }
  298. /**
  299. * Delete object in database
  300. *
  301. * @param User $user User that delete
  302. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  303. * @return int Return integer <0 if KO, >0 if OK
  304. */
  305. public function delete($user, $notrigger = 0)
  306. {
  307. global $conf, $langs;
  308. $error = 0;
  309. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
  310. $sql .= " WHERE rowid = ".((int) $this->id);
  311. $this->db->begin();
  312. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  313. $resql = $this->db->query($sql);
  314. if (!$resql) {
  315. $error++;
  316. $this->errors[] = "Error ".$this->db->lasterror();
  317. }
  318. // Commit or rollback
  319. if ($error) {
  320. foreach ($this->errors as $errmsg) {
  321. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  322. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  323. }
  324. $this->db->rollback();
  325. return -1 * $error;
  326. } else {
  327. $this->db->commit();
  328. return 1;
  329. }
  330. }
  331. /**
  332. * Load an object from its id and create a new one in database
  333. *
  334. * @param User $user User making the clone
  335. * @param int $fromid Id of object to clone
  336. * @return int New id of clone
  337. */
  338. public function createFromClone(User $user, $fromid)
  339. {
  340. $error = 0;
  341. $object = new PaymentTerm($this->db);
  342. $this->db->begin();
  343. // Load source object
  344. $object->fetch($fromid);
  345. $object->id = 0;
  346. // Create clone
  347. $object->context['createfromclone'] = 'createfromclone';
  348. $result = $object->create($user);
  349. // Other options
  350. if ($result < 0) {
  351. $this->error = $object->error;
  352. $error++;
  353. }
  354. unset($object->context['createfromclone']);
  355. // End
  356. if (!$error) {
  357. $this->db->commit();
  358. return $object->id;
  359. } else {
  360. $this->db->rollback();
  361. return -1;
  362. }
  363. }
  364. /**
  365. * Initialise an instance with random values.
  366. * Used to build previews or test instances.
  367. * id must be 0 if object instance is a specimen.
  368. *
  369. * @return void
  370. */
  371. public function initAsSpecimen()
  372. {
  373. $this->id = 0;
  374. $this->code = '';
  375. $this->sortorder = '';
  376. $this->active = '';
  377. $this->libelle = '';
  378. $this->libelle_facture = '';
  379. $this->type_cdr = '';
  380. $this->nbjour = '';
  381. $this->decalage = '';
  382. }
  383. }