dolibarr 19.0.4
paymentterm.class.php
Go to the documentation of this file.
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
28class PaymentTerm // extends CommonObject
29{
33 public $db;
34
38 public $error = '';
39
43 public $errors = array();
44
45 //public $element='c_payment_term'; //!< Id that identify managed objects
46 //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
47 public $context = array();
48
52 public $id;
53
54
58 public $entity;
59
60 public $code;
61 public $sortorder;
62 public $active;
63 public $libelle;
64 public $libelle_facture;
65 public $type_cdr;
66 public $nbjour;
67 public $decalage;
68
69
70
71
77 public function __construct(DoliDB $db)
78 {
79 $this->db = $db;
80 }
81
82
90 public function create($user, $notrigger = 0)
91 {
92 global $conf, $langs;
93 $error = 0;
94
95 // Clean parameters
96
97 if (isset($this->code)) {
98 $this->code = trim($this->code);
99 }
100 if (isset($this->sortorder)) {
101 $this->sortorder = trim($this->sortorder);
102 }
103 if (isset($this->active)) {
104 $this->active = trim($this->active);
105 }
106 if (isset($this->libelle)) {
107 $this->libelle = trim($this->libelle);
108 }
109 if (isset($this->libelle_facture)) {
110 $this->libelle_facture = trim($this->libelle_facture);
111 }
112 if (isset($this->type_cdr)) {
113 $this->type_cdr = trim($this->type_cdr);
114 }
115 if (isset($this->nbjour)) {
116 $this->nbjour = trim($this->nbjour);
117 }
118 if (isset($this->decalage)) {
119 $this->decalage = trim($this->decalage);
120 }
121
122
123 // Check parameters
124 // Put here code to add control on parameters values
125
126 // Insert request
127 $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
128 $sql .= "entity,";
129 $sql .= "code,";
130 $sql .= "sortorder,";
131 $sql .= "active,";
132 $sql .= "libelle,";
133 $sql .= "libelle_facture,";
134 $sql .= "type_cdr,";
135 $sql .= "nbjour,";
136 $sql .= "decalage";
137 $sql .= ") VALUES (";
138 $sql .= " ".(!isset($this->entity) ? getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
139 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
140 $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
141 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
142 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
143 $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
144 $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
145 $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
146 $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
147 $sql .= ")";
148
149 $this->db->begin();
150
151 dol_syslog(get_class($this)."::create", LOG_DEBUG);
152 $resql = $this->db->query($sql);
153 if (!$resql) {
154 $error++;
155 $this->errors[] = "Error ".$this->db->lasterror();
156 }
157
158 if (!$error) {
159 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
160 }
161
162 // Commit or rollback
163 if ($error) {
164 foreach ($this->errors as $errmsg) {
165 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
166 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
167 }
168 $this->db->rollback();
169 return -1 * $error;
170 } else {
171 $this->db->commit();
172 return $this->id;
173 }
174 }
175
176
184 public function fetch($id, $code = '')
185 {
186 $sql = "SELECT";
187 $sql .= " t.rowid,";
188 $sql .= " t.entity,";
189 $sql .= " t.code,";
190 $sql .= " t.sortorder,";
191 $sql .= " t.active,";
192 $sql .= " t.libelle,";
193 $sql .= " t.libelle_facture,";
194 $sql .= " t.type_cdr,";
195 $sql .= " t.nbjour,";
196 $sql .= " t.decalage";
197 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
198 if ($id) {
199 $sql .= " WHERE t.rowid = ".((int) $id);
200 }
201 if ($code) {
202 $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
203 }
204
205 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
206 $resql = $this->db->query($sql);
207 if ($resql) {
208 if ($this->db->num_rows($resql)) {
209 $obj = $this->db->fetch_object($resql);
210
211 $this->id = $obj->rowid;
212
213 $this->code = $obj->code;
214 $this->sortorder = $obj->sortorder;
215 $this->active = $obj->active;
216 $this->libelle = $obj->libelle;
217 $this->libelle_facture = $obj->libelle_facture;
218 $this->type_cdr = $obj->type_cdr;
219 $this->nbjour = $obj->nbjour;
220 $this->decalage = $obj->decalage;
221 }
222 $this->db->free($resql);
223
224 return 1;
225 } else {
226 $this->error = "Error ".$this->db->lasterror();
227 return -1;
228 }
229 }
230
231
237 public function getDefaultId()
238 {
239 global $langs;
240
241 $ret = 0;
242
243 $sql = "SELECT";
244 $sql .= " t.rowid";
245 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
246 $sql .= " WHERE t.code = 'RECEP'";
247 $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
248
249 dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if ($resql) {
252 if ($this->db->num_rows($resql)) {
253 $obj = $this->db->fetch_object($resql);
254 if ($obj) {
255 $ret = $obj->rowid;
256 }
257 }
258 $this->db->free($resql);
259 return $ret;
260 } else {
261 $this->error = "Error ".$this->db->lasterror();
262 return -1;
263 }
264 }
265
266
274 public function update($user = null, $notrigger = 0)
275 {
276 global $conf, $langs;
277
278 $error = 0;
279
280 // Clean parameters
281
282 if (isset($this->code)) {
283 $this->code = trim($this->code);
284 }
285 if (isset($this->sortorder)) {
286 $this->sortorder = trim($this->sortorder);
287 }
288 if (isset($this->active)) {
289 $this->active = trim($this->active);
290 }
291 if (isset($this->libelle)) {
292 $this->libelle = trim($this->libelle);
293 }
294 if (isset($this->libelle_facture)) {
295 $this->libelle_facture = trim($this->libelle_facture);
296 }
297 if (isset($this->type_cdr)) {
298 $this->type_cdr = trim($this->type_cdr);
299 }
300 if (isset($this->nbjour)) {
301 $this->nbjour = trim($this->nbjour);
302 }
303 if (isset($this->decalage)) {
304 $this->decalage = trim($this->decalage);
305 }
306
307
308
309 // Check parameters
310 // Put here code to add control on parameters values
311
312 // Update request
313 $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
314 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
315 $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
316 $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
317 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
318 $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
319 $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
320 $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
321 $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null");
322 $sql .= " WHERE rowid = ".((int) $this->id);
323
324 $this->db->begin();
325
326 dol_syslog(get_class($this)."::update", LOG_DEBUG);
327 $resql = $this->db->query($sql);
328 if (!$resql) {
329 $error++;
330 $this->errors[] = "Error ".$this->db->lasterror();
331 }
332
333 // Commit or rollback
334 if ($error) {
335 foreach ($this->errors as $errmsg) {
336 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
337 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
338 }
339 $this->db->rollback();
340 return -1 * $error;
341 } else {
342 $this->db->commit();
343 return 1;
344 }
345 }
346
347
355 public function delete($user, $notrigger = 0)
356 {
357 global $conf, $langs;
358 $error = 0;
359
360 $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
361 $sql .= " WHERE rowid = ".((int) $this->id);
362
363 $this->db->begin();
364
365 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
366 $resql = $this->db->query($sql);
367 if (!$resql) {
368 $error++;
369 $this->errors[] = "Error ".$this->db->lasterror();
370 }
371
372 // Commit or rollback
373 if ($error) {
374 foreach ($this->errors as $errmsg) {
375 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
376 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
377 }
378 $this->db->rollback();
379 return -1 * $error;
380 } else {
381 $this->db->commit();
382 return 1;
383 }
384 }
385
386
387
395 public function createFromClone(User $user, $fromid)
396 {
397 $error = 0;
398
399 $object = new PaymentTerm($this->db);
400
401 $this->db->begin();
402
403 // Load source object
404 $object->fetch($fromid);
405 $object->id = 0;
406
407 // Create clone
408 $object->context['createfromclone'] = 'createfromclone';
409 $result = $object->create($user);
410
411 // Other options
412 if ($result < 0) {
413 $this->error = $object->error;
414 $error++;
415 }
416
417 unset($object->context['createfromclone']);
418
419 // End
420 if (!$error) {
421 $this->db->commit();
422 return $object->id;
423 } else {
424 $this->db->rollback();
425 return -1;
426 }
427 }
428
429
437 public function initAsSpecimen()
438 {
439 $this->id = 0;
440
441 $this->code = '';
442 $this->sortorder = '';
443 $this->active = '';
444 $this->libelle = '';
445 $this->libelle_facture = '';
446 $this->type_cdr = '';
447 $this->nbjour = '';
448 $this->decalage = '';
449 }
450}
Class to manage Dolibarr database access.
Class to manage payment terms records in dictionary.
create($user, $notrigger=0)
Create in database.
__construct(DoliDB $db)
Constructor.
fetch($id, $code='')
Load object in memory from database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
getDefaultId()
Return id of default payment term.
initAsSpecimen()
Initialise an instance with random values.
update($user=null, $notrigger=0)
Update database.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.