dolibarr 25.0.0-alpha
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 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
30class PaymentTerm // extends CommonObject
31{
35 public $db;
36
40 public $error = '';
41
45 public $errors = array();
46
47 //public $element='c_payment_term'; //!< Id that identify managed objects
48 //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
52 public $context = array();
53
57 public $id;
58
59
63 public $entity;
64
68 public $code;
72 public $sortorder;
76 public $active;
80 public $libelle;
84 public $libelle_facture;
88 public $type_cdr;
92 public $nbjour;
96 public $decalage;
97
98
99
100
106 public function __construct(DoliDB $db)
107 {
108 $this->db = $db;
109 }
110
111
119 public function create($user, $notrigger = 0)
120 {
121 $error = 0;
122
123 // Clean parameters
124
125 if (isset($this->code)) {
126 $this->code = trim($this->code);
127 }
128 if (isset($this->sortorder)) {
129 $this->sortorder = trim($this->sortorder);
130 }
131 if (isset($this->active)) {
132 $this->active = trim($this->active);
133 }
134 if (isset($this->libelle)) {
135 $this->libelle = trim($this->libelle);
136 }
137 if (isset($this->libelle_facture)) {
138 $this->libelle_facture = trim($this->libelle_facture);
139 }
140 if (isset($this->type_cdr)) {
141 $this->type_cdr = trim($this->type_cdr);
142 }
143 if (isset($this->nbjour)) {
144 $this->nbjour = trim($this->nbjour);
145 }
146 if (isset($this->decalage)) {
147 $this->decalage = trim($this->decalage);
148 }
149
150
151 // Check parameters
152 // Put here code to add control on parameters values
153
154 // Insert request
155 $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
156 $sql .= "entity,";
157 $sql .= "code,";
158 $sql .= "sortorder,";
159 $sql .= "active,";
160 $sql .= "libelle,";
161 $sql .= "libelle_facture,";
162 $sql .= "type_cdr,";
163 $sql .= "nbjour,";
164 $sql .= "decalage";
165 $sql .= ") VALUES (";
166 $sql .= " ".(!isset($this->entity) ? getEntity('c_payment_term') : "'".$this->db->escape((string) $this->entity)."'").",";
167 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
168 $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
169 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
170 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
171 $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
172 $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
173 $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
174 $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
175 $sql .= ")";
176
177 $this->db->begin();
178
179 dol_syslog(get_class($this)."::create", LOG_DEBUG);
180 $resql = $this->db->query($sql);
181 if (!$resql) {
182 $error++;
183 $this->errors[] = "Error ".$this->db->lasterror();
184 }
185
186 if (!$error) {
187 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
188 }
189
190 // Commit or rollback
191 if ($error) {
192 foreach ($this->errors as $errmsg) {
193 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
194 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
195 }
196 $this->db->rollback();
197 return -1 * $error;
198 } else {
199 $this->db->commit();
200 return $this->id;
201 }
202 }
203
204
212 public function fetch($id, $code = '')
213 {
214 $sql = "SELECT";
215 $sql .= " t.rowid,";
216 $sql .= " t.entity,";
217 $sql .= " t.code,";
218 $sql .= " t.sortorder,";
219 $sql .= " t.active,";
220 $sql .= " t.libelle,";
221 $sql .= " t.libelle_facture,";
222 $sql .= " t.type_cdr,";
223 $sql .= " t.nbjour,";
224 $sql .= " t.decalage";
225 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
226 if ($id) {
227 $sql .= " WHERE t.rowid = ".((int) $id);
228 }
229 if ($code) {
230 $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
231 }
232
233 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
234 $resql = $this->db->query($sql);
235 if ($resql) {
236 if ($this->db->num_rows($resql)) {
237 $obj = $this->db->fetch_object($resql);
238
239 $this->id = $obj->rowid;
240
241 $this->code = $obj->code;
242 $this->sortorder = $obj->sortorder;
243 $this->active = $obj->active;
244 $this->libelle = $obj->libelle;
245 $this->libelle_facture = $obj->libelle_facture;
246 $this->type_cdr = $obj->type_cdr;
247 $this->nbjour = $obj->nbjour;
248 $this->decalage = $obj->decalage;
249 }
250 $this->db->free($resql);
251
252 return 1;
253 } else {
254 $this->error = "Error ".$this->db->lasterror();
255 return -1;
256 }
257 }
258
259
265 public function getDefaultId()
266 {
267 global $langs;
268
269 $ret = 0;
270
271 $sql = "SELECT";
272 $sql .= " t.rowid";
273 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
274 $sql .= " WHERE t.code = 'RECEP'";
275 $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
276
277 dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
278 $resql = $this->db->query($sql);
279 if ($resql) {
280 if ($this->db->num_rows($resql)) {
281 $obj = $this->db->fetch_object($resql);
282 if ($obj) {
283 $ret = $obj->rowid;
284 }
285 }
286 $this->db->free($resql);
287 return $ret;
288 } else {
289 $this->error = "Error ".$this->db->lasterror();
290 return -1;
291 }
292 }
293
294
302 public function update($user = null, $notrigger = 0)
303 {
304 global $conf, $langs;
305
306 $error = 0;
307
308 // Clean parameters
309
310 if (isset($this->code)) {
311 $this->code = trim($this->code);
312 }
313 if (isset($this->sortorder)) {
314 $this->sortorder = trim($this->sortorder);
315 }
316 if (isset($this->active)) {
317 $this->active = trim($this->active);
318 }
319 if (isset($this->libelle)) {
320 $this->libelle = trim($this->libelle);
321 }
322 if (isset($this->libelle_facture)) {
323 $this->libelle_facture = trim($this->libelle_facture);
324 }
325 if (isset($this->type_cdr)) {
326 $this->type_cdr = trim($this->type_cdr);
327 }
328 if (isset($this->nbjour)) {
329 $this->nbjour = trim($this->nbjour);
330 }
331 if (isset($this->decalage)) {
332 $this->decalage = trim($this->decalage);
333 }
334
335
336
337 // Check parameters
338 // Put here code to add control on parameters values
339
340 // Update request
341 $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
342 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
343 $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
344 $sql .= " active=".(isset($this->active) ? "'".$this->db->escape($this->active)."'" : "null").",";
345 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
346 $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
347 $sql .= " type_cdr=".(isset($this->type_cdr) ? "'".$this->db->escape($this->type_cdr)."'" : "null").",";
348 $sql .= " nbjour=".(isset($this->nbjour) ? "'".$this->db->escape($this->nbjour)."'" : "null").",";
349 $sql .= " decalage=".(isset($this->decalage) ? "'".$this->db->escape($this->decalage)."'" : "null");
350 $sql .= " WHERE rowid = ".((int) $this->id);
351
352 $this->db->begin();
353
354 dol_syslog(get_class($this)."::update", LOG_DEBUG);
355 $resql = $this->db->query($sql);
356 if (!$resql) {
357 $error++;
358 $this->errors[] = "Error ".$this->db->lasterror();
359 }
360
361 // Commit or rollback
362 if ($error) {
363 foreach ($this->errors as $errmsg) {
364 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
365 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
366 }
367 $this->db->rollback();
368 return -1 * $error;
369 } else {
370 $this->db->commit();
371 return 1;
372 }
373 }
374
375
383 public function delete($user, $notrigger = 0)
384 {
385 global $conf, $langs;
386 $error = 0;
387
388 $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
389 $sql .= " WHERE rowid = ".((int) $this->id);
390
391 $this->db->begin();
392
393 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
394 $resql = $this->db->query($sql);
395 if (!$resql) {
396 $error++;
397 $this->errors[] = "Error ".$this->db->lasterror();
398 }
399
400 // Commit or rollback
401 if ($error) {
402 foreach ($this->errors as $errmsg) {
403 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
404 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
405 }
406 $this->db->rollback();
407 return -1 * $error;
408 } else {
409 $this->db->commit();
410 return 1;
411 }
412 }
413
414
415
423 public function createFromClone(User $user, $fromid)
424 {
425 $error = 0;
426
427 $object = new PaymentTerm($this->db);
428
429 $this->db->begin();
430
431 // Load source object
432 $object->fetch($fromid);
433 $object->id = 0;
434
435 // Create clone
436 $object->context['createfromclone'] = 'createfromclone';
437 $result = $object->create($user);
438
439 // Other options
440 if ($result < 0) {
441 $this->error = $object->error;
442 $error++;
443 }
444
445 unset($object->context['createfromclone']);
446
447 // End
448 if (!$error) {
449 $this->db->commit();
450 return $object->id;
451 } else {
452 $this->db->rollback();
453 return -1;
454 }
455 }
456
457
465 public function initAsSpecimen()
466 {
467 $this->id = 0;
468
469 $this->code = '';
470 $this->sortorder = '';
471 $this->active = '';
472 $this->libelle = '';
473 $this->libelle_facture = '';
474 $this->type_cdr = '';
475 $this->nbjour = '';
476 $this->decalage = '';
477
478 return 1;
479 }
480}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
editval_textarea active