dolibarr 20.0.2
paymentterm.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
29class PaymentTerm // extends CommonObject
30{
34 public $db;
35
39 public $error = '';
40
44 public $errors = array();
45
46 //public $element='c_payment_term'; //!< Id that identify managed objects
47 //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
48 public $context = array();
49
53 public $id;
54
55
59 public $entity;
60
61 public $code;
62 public $sortorder;
63 public $active;
64 public $libelle;
65 public $libelle_facture;
66 public $type_cdr;
67 public $nbjour;
68 public $decalage;
69
70
71
72
78 public function __construct(DoliDB $db)
79 {
80 $this->db = $db;
81 }
82
83
91 public function create($user, $notrigger = 0)
92 {
93 global $conf, $langs;
94 $error = 0;
95
96 // Clean parameters
97
98 if (isset($this->code)) {
99 $this->code = trim($this->code);
100 }
101 if (isset($this->sortorder)) {
102 $this->sortorder = trim($this->sortorder);
103 }
104 if (isset($this->active)) {
105 $this->active = trim($this->active);
106 }
107 if (isset($this->libelle)) {
108 $this->libelle = trim($this->libelle);
109 }
110 if (isset($this->libelle_facture)) {
111 $this->libelle_facture = trim($this->libelle_facture);
112 }
113 if (isset($this->type_cdr)) {
114 $this->type_cdr = trim($this->type_cdr);
115 }
116 if (isset($this->nbjour)) {
117 $this->nbjour = trim($this->nbjour);
118 }
119 if (isset($this->decalage)) {
120 $this->decalage = trim($this->decalage);
121 }
122
123
124 // Check parameters
125 // Put here code to add control on parameters values
126
127 // Insert request
128 $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
129 $sql .= "entity,";
130 $sql .= "code,";
131 $sql .= "sortorder,";
132 $sql .= "active,";
133 $sql .= "libelle,";
134 $sql .= "libelle_facture,";
135 $sql .= "type_cdr,";
136 $sql .= "nbjour,";
137 $sql .= "decalage";
138 $sql .= ") VALUES (";
139 $sql .= " ".(!isset($this->entity) ? getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
140 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
141 $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
142 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
143 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
144 $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
145 $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
146 $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
147 $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
148 $sql .= ")";
149
150 $this->db->begin();
151
152 dol_syslog(get_class($this)."::create", LOG_DEBUG);
153 $resql = $this->db->query($sql);
154 if (!$resql) {
155 $error++;
156 $this->errors[] = "Error ".$this->db->lasterror();
157 }
158
159 if (!$error) {
160 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
161 }
162
163 // Commit or rollback
164 if ($error) {
165 foreach ($this->errors as $errmsg) {
166 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
167 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
168 }
169 $this->db->rollback();
170 return -1 * $error;
171 } else {
172 $this->db->commit();
173 return $this->id;
174 }
175 }
176
177
185 public function fetch($id, $code = '')
186 {
187 $sql = "SELECT";
188 $sql .= " t.rowid,";
189 $sql .= " t.entity,";
190 $sql .= " t.code,";
191 $sql .= " t.sortorder,";
192 $sql .= " t.active,";
193 $sql .= " t.libelle,";
194 $sql .= " t.libelle_facture,";
195 $sql .= " t.type_cdr,";
196 $sql .= " t.nbjour,";
197 $sql .= " t.decalage";
198 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
199 if ($id) {
200 $sql .= " WHERE t.rowid = ".((int) $id);
201 }
202 if ($code) {
203 $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
204 }
205
206 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
207 $resql = $this->db->query($sql);
208 if ($resql) {
209 if ($this->db->num_rows($resql)) {
210 $obj = $this->db->fetch_object($resql);
211
212 $this->id = $obj->rowid;
213
214 $this->code = $obj->code;
215 $this->sortorder = $obj->sortorder;
216 $this->active = $obj->active;
217 $this->libelle = $obj->libelle;
218 $this->libelle_facture = $obj->libelle_facture;
219 $this->type_cdr = $obj->type_cdr;
220 $this->nbjour = $obj->nbjour;
221 $this->decalage = $obj->decalage;
222 }
223 $this->db->free($resql);
224
225 return 1;
226 } else {
227 $this->error = "Error ".$this->db->lasterror();
228 return -1;
229 }
230 }
231
232
238 public function getDefaultId()
239 {
240 global $langs;
241
242 $ret = 0;
243
244 $sql = "SELECT";
245 $sql .= " t.rowid";
246 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
247 $sql .= " WHERE t.code = 'RECEP'";
248 $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
249
250 dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
251 $resql = $this->db->query($sql);
252 if ($resql) {
253 if ($this->db->num_rows($resql)) {
254 $obj = $this->db->fetch_object($resql);
255 if ($obj) {
256 $ret = $obj->rowid;
257 }
258 }
259 $this->db->free($resql);
260 return $ret;
261 } else {
262 $this->error = "Error ".$this->db->lasterror();
263 return -1;
264 }
265 }
266
267
275 public function update($user = null, $notrigger = 0)
276 {
277 global $conf, $langs;
278
279 $error = 0;
280
281 // Clean parameters
282
283 if (isset($this->code)) {
284 $this->code = trim($this->code);
285 }
286 if (isset($this->sortorder)) {
287 $this->sortorder = trim($this->sortorder);
288 }
289 if (isset($this->active)) {
290 $this->active = trim($this->active);
291 }
292 if (isset($this->libelle)) {
293 $this->libelle = trim($this->libelle);
294 }
295 if (isset($this->libelle_facture)) {
296 $this->libelle_facture = trim($this->libelle_facture);
297 }
298 if (isset($this->type_cdr)) {
299 $this->type_cdr = trim($this->type_cdr);
300 }
301 if (isset($this->nbjour)) {
302 $this->nbjour = trim($this->nbjour);
303 }
304 if (isset($this->decalage)) {
305 $this->decalage = trim($this->decalage);
306 }
307
308
309
310 // Check parameters
311 // Put here code to add control on parameters values
312
313 // Update request
314 $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
315 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
316 $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
317 $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
318 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
319 $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
320 $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
321 $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
322 $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null");
323 $sql .= " WHERE rowid = ".((int) $this->id);
324
325 $this->db->begin();
326
327 dol_syslog(get_class($this)."::update", LOG_DEBUG);
328 $resql = $this->db->query($sql);
329 if (!$resql) {
330 $error++;
331 $this->errors[] = "Error ".$this->db->lasterror();
332 }
333
334 // Commit or rollback
335 if ($error) {
336 foreach ($this->errors as $errmsg) {
337 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
338 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
339 }
340 $this->db->rollback();
341 return -1 * $error;
342 } else {
343 $this->db->commit();
344 return 1;
345 }
346 }
347
348
356 public function delete($user, $notrigger = 0)
357 {
358 global $conf, $langs;
359 $error = 0;
360
361 $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
362 $sql .= " WHERE rowid = ".((int) $this->id);
363
364 $this->db->begin();
365
366 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
367 $resql = $this->db->query($sql);
368 if (!$resql) {
369 $error++;
370 $this->errors[] = "Error ".$this->db->lasterror();
371 }
372
373 // Commit or rollback
374 if ($error) {
375 foreach ($this->errors as $errmsg) {
376 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
377 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
378 }
379 $this->db->rollback();
380 return -1 * $error;
381 } else {
382 $this->db->commit();
383 return 1;
384 }
385 }
386
387
388
396 public function createFromClone(User $user, $fromid)
397 {
398 $error = 0;
399
400 $object = new PaymentTerm($this->db);
401
402 $this->db->begin();
403
404 // Load source object
405 $object->fetch($fromid);
406 $object->id = 0;
407
408 // Create clone
409 $object->context['createfromclone'] = 'createfromclone';
410 $result = $object->create($user);
411
412 // Other options
413 if ($result < 0) {
414 $this->error = $object->error;
415 $error++;
416 }
417
418 unset($object->context['createfromclone']);
419
420 // End
421 if (!$error) {
422 $this->db->commit();
423 return $object->id;
424 } else {
425 $this->db->rollback();
426 return -1;
427 }
428 }
429
430
438 public function initAsSpecimen()
439 {
440 $this->id = 0;
441
442 $this->code = '';
443 $this->sortorder = '';
444 $this->active = '';
445 $this->libelle = '';
446 $this->libelle_facture = '';
447 $this->type_cdr = '';
448 $this->nbjour = '';
449 $this->decalage = '';
450
451 return 1;
452 }
453}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.