paymentterm.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. public $code;
  47. public $sortorder;
  48. public $active;
  49. public $libelle;
  50. public $libelle_facture;
  51. public $type_cdr;
  52. public $nbjour;
  53. public $decalage;
  54. /**
  55. * Constructor
  56. *
  57. * @param DoliDB $db Database handler
  58. */
  59. public function __construct(DoliDB $db)
  60. {
  61. $this->db = $db;
  62. }
  63. /**
  64. * Create in database
  65. *
  66. * @param User $user User that create
  67. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  68. * @return int <0 if KO, Id of created object if OK
  69. */
  70. public function create($user, $notrigger = 0)
  71. {
  72. global $conf, $langs;
  73. $error = 0;
  74. // Clean parameters
  75. if (isset($this->code)) {
  76. $this->code = trim($this->code);
  77. }
  78. if (isset($this->sortorder)) {
  79. $this->sortorder = trim($this->sortorder);
  80. }
  81. if (isset($this->active)) {
  82. $this->active = trim($this->active);
  83. }
  84. if (isset($this->libelle)) {
  85. $this->libelle = trim($this->libelle);
  86. }
  87. if (isset($this->libelle_facture)) {
  88. $this->libelle_facture = trim($this->libelle_facture);
  89. }
  90. if (isset($this->type_cdr)) {
  91. $this->type_cdr = trim($this->type_cdr);
  92. }
  93. if (isset($this->nbjour)) {
  94. $this->nbjour = trim($this->nbjour);
  95. }
  96. if (isset($this->decalage)) {
  97. $this->decalage = trim($this->decalage);
  98. }
  99. // Check parameters
  100. // Put here code to add control on parameters values
  101. // Insert request
  102. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
  103. $sql .= "entity,";
  104. $sql .= "code,";
  105. $sql .= "sortorder,";
  106. $sql .= "active,";
  107. $sql .= "libelle,";
  108. $sql .= "libelle_facture,";
  109. $sql .= "type_cdr,";
  110. $sql .= "nbjour,";
  111. $sql .= "decalage";
  112. $sql .= ") VALUES (";
  113. $sql .= " ".(!isset($this->entity) ?getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
  114. $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
  115. $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
  116. $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
  117. $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
  118. $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
  119. $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
  120. $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
  121. $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'")."";
  122. $sql .= ")";
  123. $this->db->begin();
  124. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  125. $resql = $this->db->query($sql);
  126. if (!$resql) {
  127. $error++;
  128. $this->errors[] = "Error ".$this->db->lasterror();
  129. }
  130. if (!$error) {
  131. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
  132. }
  133. // Commit or rollback
  134. if ($error) {
  135. foreach ($this->errors as $errmsg) {
  136. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  137. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  138. }
  139. $this->db->rollback();
  140. return -1 * $error;
  141. } else {
  142. $this->db->commit();
  143. return $this->id;
  144. }
  145. }
  146. /**
  147. * Load object in memory from database
  148. *
  149. * @param int $id Id object
  150. * @param string $code Code object
  151. * @return int <0 if KO, >0 if OK
  152. */
  153. public function fetch($id, $code = '')
  154. {
  155. global $langs;
  156. $sql = "SELECT";
  157. $sql .= " t.rowid,";
  158. $sql .= " t.entity,";
  159. $sql .= " t.code,";
  160. $sql .= " t.sortorder,";
  161. $sql .= " t.active,";
  162. $sql .= " t.libelle,";
  163. $sql .= " t.libelle_facture,";
  164. $sql .= " t.type_cdr,";
  165. $sql .= " t.nbjour,";
  166. $sql .= " t.decalage";
  167. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  168. if ($id) {
  169. $sql .= " WHERE t.rowid = ".((int) $id);
  170. }
  171. if ($code) {
  172. $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
  173. }
  174. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  175. $resql = $this->db->query($sql);
  176. if ($resql) {
  177. if ($this->db->num_rows($resql)) {
  178. $obj = $this->db->fetch_object($resql);
  179. $this->id = $obj->rowid;
  180. $this->code = $obj->code;
  181. $this->sortorder = $obj->sortorder;
  182. $this->active = $obj->active;
  183. $this->libelle = $obj->libelle;
  184. $this->libelle_facture = $obj->libelle_facture;
  185. $this->type_cdr = $obj->type_cdr;
  186. $this->nbjour = $obj->nbjour;
  187. $this->decalage = $obj->decalage;
  188. }
  189. $this->db->free($resql);
  190. return 1;
  191. } else {
  192. $this->error = "Error ".$this->db->lasterror();
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Return id of default payment term
  198. *
  199. * @return int <0 if KO, >0 if OK
  200. */
  201. public function getDefaultId()
  202. {
  203. global $langs;
  204. $ret = 0;
  205. $sql = "SELECT";
  206. $sql .= " t.rowid";
  207. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  208. $sql .= " WHERE t.code = 'RECEP'";
  209. $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
  210. dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
  211. $resql = $this->db->query($sql);
  212. if ($resql) {
  213. if ($this->db->num_rows($resql)) {
  214. $obj = $this->db->fetch_object($resql);
  215. if ($obj) {
  216. $ret = $obj->rowid;
  217. }
  218. }
  219. $this->db->free($resql);
  220. return $ret;
  221. } else {
  222. $this->error = "Error ".$this->db->lasterror();
  223. return -1;
  224. }
  225. }
  226. /**
  227. * Update database
  228. *
  229. * @param User $user User that modify
  230. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  231. * @return int <0 if KO, >0 if OK
  232. */
  233. public function update($user = null, $notrigger = 0)
  234. {
  235. global $conf, $langs;
  236. $error = 0;
  237. // Clean parameters
  238. if (isset($this->code)) {
  239. $this->code = trim($this->code);
  240. }
  241. if (isset($this->sortorder)) {
  242. $this->sortorder = trim($this->sortorder);
  243. }
  244. if (isset($this->active)) {
  245. $this->active = trim($this->active);
  246. }
  247. if (isset($this->libelle)) {
  248. $this->libelle = trim($this->libelle);
  249. }
  250. if (isset($this->libelle_facture)) {
  251. $this->libelle_facture = trim($this->libelle_facture);
  252. }
  253. if (isset($this->type_cdr)) {
  254. $this->type_cdr = trim($this->type_cdr);
  255. }
  256. if (isset($this->nbjour)) {
  257. $this->nbjour = trim($this->nbjour);
  258. }
  259. if (isset($this->decalage)) {
  260. $this->decalage = trim($this->decalage);
  261. }
  262. // Check parameters
  263. // Put here code to add control on parameters values
  264. // Update request
  265. $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
  266. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  267. $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
  268. $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
  269. $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
  270. $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
  271. $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
  272. $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
  273. $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null")."";
  274. $sql .= " WHERE rowid = ".((int) $this->id);
  275. $this->db->begin();
  276. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  277. $resql = $this->db->query($sql);
  278. if (!$resql) {
  279. $error++;
  280. $this->errors[] = "Error ".$this->db->lasterror();
  281. }
  282. // Commit or rollback
  283. if ($error) {
  284. foreach ($this->errors as $errmsg) {
  285. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  286. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  287. }
  288. $this->db->rollback();
  289. return -1 * $error;
  290. } else {
  291. $this->db->commit();
  292. return 1;
  293. }
  294. }
  295. /**
  296. * Delete object in database
  297. *
  298. * @param User $user User that delete
  299. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  300. * @return int <0 if KO, >0 if OK
  301. */
  302. public function delete($user, $notrigger = 0)
  303. {
  304. global $conf, $langs;
  305. $error = 0;
  306. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
  307. $sql .= " WHERE rowid = ".((int) $this->id);
  308. $this->db->begin();
  309. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  310. $resql = $this->db->query($sql);
  311. if (!$resql) {
  312. $error++;
  313. $this->errors[] = "Error ".$this->db->lasterror();
  314. }
  315. // Commit or rollback
  316. if ($error) {
  317. foreach ($this->errors as $errmsg) {
  318. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  319. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  320. }
  321. $this->db->rollback();
  322. return -1 * $error;
  323. } else {
  324. $this->db->commit();
  325. return 1;
  326. }
  327. }
  328. /**
  329. * Load an object from its id and create a new one in database
  330. *
  331. * @param User $user User making the clone
  332. * @param int $fromid Id of object to clone
  333. * @return int New id of clone
  334. */
  335. public function createFromClone(User $user, $fromid)
  336. {
  337. $error = 0;
  338. $object = new PaymentTerm($this->db);
  339. $this->db->begin();
  340. // Load source object
  341. $object->fetch($fromid);
  342. $object->id = 0;
  343. $object->statut = 0;
  344. // Clear fields
  345. // ...
  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. }