dolibarr 18.0.6
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 public $code;
55 public $sortorder;
56 public $active;
57 public $libelle;
58 public $libelle_facture;
59 public $type_cdr;
60 public $nbjour;
61 public $decalage;
62
63
64
65
71 public function __construct(DoliDB $db)
72 {
73 $this->db = $db;
74 }
75
76
84 public function create($user, $notrigger = 0)
85 {
86 global $conf, $langs;
87 $error = 0;
88
89 // Clean parameters
90
91 if (isset($this->code)) {
92 $this->code = trim($this->code);
93 }
94 if (isset($this->sortorder)) {
95 $this->sortorder = trim($this->sortorder);
96 }
97 if (isset($this->active)) {
98 $this->active = trim($this->active);
99 }
100 if (isset($this->libelle)) {
101 $this->libelle = trim($this->libelle);
102 }
103 if (isset($this->libelle_facture)) {
104 $this->libelle_facture = trim($this->libelle_facture);
105 }
106 if (isset($this->type_cdr)) {
107 $this->type_cdr = trim($this->type_cdr);
108 }
109 if (isset($this->nbjour)) {
110 $this->nbjour = trim($this->nbjour);
111 }
112 if (isset($this->decalage)) {
113 $this->decalage = trim($this->decalage);
114 }
115
116
117 // Check parameters
118 // Put here code to add control on parameters values
119
120 // Insert request
121 $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
122 $sql .= "entity,";
123 $sql .= "code,";
124 $sql .= "sortorder,";
125 $sql .= "active,";
126 $sql .= "libelle,";
127 $sql .= "libelle_facture,";
128 $sql .= "type_cdr,";
129 $sql .= "nbjour,";
130 $sql .= "decalage";
131 $sql .= ") VALUES (";
132 $sql .= " ".(!isset($this->entity) ?getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
133 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
134 $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
135 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
136 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
137 $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
138 $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
139 $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
140 $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
141 $sql .= ")";
142
143 $this->db->begin();
144
145 dol_syslog(get_class($this)."::create", LOG_DEBUG);
146 $resql = $this->db->query($sql);
147 if (!$resql) {
148 $error++;
149 $this->errors[] = "Error ".$this->db->lasterror();
150 }
151
152 if (!$error) {
153 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
154 }
155
156 // Commit or rollback
157 if ($error) {
158 foreach ($this->errors as $errmsg) {
159 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
160 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
161 }
162 $this->db->rollback();
163 return -1 * $error;
164 } else {
165 $this->db->commit();
166 return $this->id;
167 }
168 }
169
170
178 public function fetch($id, $code = '')
179 {
180 global $langs;
181 $sql = "SELECT";
182 $sql .= " t.rowid,";
183 $sql .= " t.entity,";
184
185 $sql .= " t.code,";
186 $sql .= " t.sortorder,";
187 $sql .= " t.active,";
188 $sql .= " t.libelle,";
189 $sql .= " t.libelle_facture,";
190 $sql .= " t.type_cdr,";
191 $sql .= " t.nbjour,";
192 $sql .= " t.decalage";
193
194
195 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
196 if ($id) {
197 $sql .= " WHERE t.rowid = ".((int) $id);
198 }
199 if ($code) {
200 $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
201 }
202
203 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
204 $resql = $this->db->query($sql);
205 if ($resql) {
206 if ($this->db->num_rows($resql)) {
207 $obj = $this->db->fetch_object($resql);
208
209 $this->id = $obj->rowid;
210
211 $this->code = $obj->code;
212 $this->sortorder = $obj->sortorder;
213 $this->active = $obj->active;
214 $this->libelle = $obj->libelle;
215 $this->libelle_facture = $obj->libelle_facture;
216 $this->type_cdr = $obj->type_cdr;
217 $this->nbjour = $obj->nbjour;
218 $this->decalage = $obj->decalage;
219 }
220 $this->db->free($resql);
221
222 return 1;
223 } else {
224 $this->error = "Error ".$this->db->lasterror();
225 return -1;
226 }
227 }
228
229
235 public function getDefaultId()
236 {
237 global $langs;
238
239 $ret = 0;
240
241 $sql = "SELECT";
242 $sql .= " t.rowid";
243 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
244 $sql .= " WHERE t.code = 'RECEP'";
245 $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
246
247 dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
248 $resql = $this->db->query($sql);
249 if ($resql) {
250 if ($this->db->num_rows($resql)) {
251 $obj = $this->db->fetch_object($resql);
252 if ($obj) {
253 $ret = $obj->rowid;
254 }
255 }
256 $this->db->free($resql);
257 return $ret;
258 } else {
259 $this->error = "Error ".$this->db->lasterror();
260 return -1;
261 }
262 }
263
264
272 public function update($user = null, $notrigger = 0)
273 {
274 global $conf, $langs;
275
276 $error = 0;
277
278 // Clean parameters
279
280 if (isset($this->code)) {
281 $this->code = trim($this->code);
282 }
283 if (isset($this->sortorder)) {
284 $this->sortorder = trim($this->sortorder);
285 }
286 if (isset($this->active)) {
287 $this->active = trim($this->active);
288 }
289 if (isset($this->libelle)) {
290 $this->libelle = trim($this->libelle);
291 }
292 if (isset($this->libelle_facture)) {
293 $this->libelle_facture = trim($this->libelle_facture);
294 }
295 if (isset($this->type_cdr)) {
296 $this->type_cdr = trim($this->type_cdr);
297 }
298 if (isset($this->nbjour)) {
299 $this->nbjour = trim($this->nbjour);
300 }
301 if (isset($this->decalage)) {
302 $this->decalage = trim($this->decalage);
303 }
304
305
306
307 // Check parameters
308 // Put here code to add control on parameters values
309
310 // Update request
311 $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
312 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
313 $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
314 $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
315 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
316 $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
317 $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
318 $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
319 $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null");
320 $sql .= " WHERE rowid = ".((int) $this->id);
321
322 $this->db->begin();
323
324 dol_syslog(get_class($this)."::update", LOG_DEBUG);
325 $resql = $this->db->query($sql);
326 if (!$resql) {
327 $error++;
328 $this->errors[] = "Error ".$this->db->lasterror();
329 }
330
331 // Commit or rollback
332 if ($error) {
333 foreach ($this->errors as $errmsg) {
334 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
335 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
336 }
337 $this->db->rollback();
338 return -1 * $error;
339 } else {
340 $this->db->commit();
341 return 1;
342 }
343 }
344
345
353 public function delete($user, $notrigger = 0)
354 {
355 global $conf, $langs;
356 $error = 0;
357
358 $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
359 $sql .= " WHERE rowid = ".((int) $this->id);
360
361 $this->db->begin();
362
363 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
364 $resql = $this->db->query($sql);
365 if (!$resql) {
366 $error++;
367 $this->errors[] = "Error ".$this->db->lasterror();
368 }
369
370 // Commit or rollback
371 if ($error) {
372 foreach ($this->errors as $errmsg) {
373 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
374 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
375 }
376 $this->db->rollback();
377 return -1 * $error;
378 } else {
379 $this->db->commit();
380 return 1;
381 }
382 }
383
384
385
393 public function createFromClone(User $user, $fromid)
394 {
395 $error = 0;
396
397 $object = new PaymentTerm($this->db);
398
399 $this->db->begin();
400
401 // Load source object
402 $object->fetch($fromid);
403 $object->id = 0;
404 $object->statut = 0;
405
406 // Clear fields
407 // ...
408
409 // Create clone
410 $object->context['createfromclone'] = 'createfromclone';
411 $result = $object->create($user);
412
413 // Other options
414 if ($result < 0) {
415 $this->error = $object->error;
416 $error++;
417 }
418
419 unset($object->context['createfromclone']);
420
421 // End
422 if (!$error) {
423 $this->db->commit();
424 return $object->id;
425 } else {
426 $this->db->rollback();
427 return -1;
428 }
429 }
430
431
439 public function initAsSpecimen()
440 {
441 $this->id = 0;
442
443 $this->code = '';
444 $this->sortorder = '';
445 $this->active = '';
446 $this->libelle = '';
447 $this->libelle_facture = '';
448 $this->type_cdr = '';
449 $this->nbjour = '';
450 $this->decalage = '';
451 }
452}
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.