dolibarr 21.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 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 global $conf, $langs;
122 $error = 0;
123
124 // Clean parameters
125
126 if (isset($this->code)) {
127 $this->code = trim($this->code);
128 }
129 if (isset($this->sortorder)) {
130 $this->sortorder = trim($this->sortorder);
131 }
132 if (isset($this->active)) {
133 $this->active = trim($this->active);
134 }
135 if (isset($this->libelle)) {
136 $this->libelle = trim($this->libelle);
137 }
138 if (isset($this->libelle_facture)) {
139 $this->libelle_facture = trim($this->libelle_facture);
140 }
141 if (isset($this->type_cdr)) {
142 $this->type_cdr = trim($this->type_cdr);
143 }
144 if (isset($this->nbjour)) {
145 $this->nbjour = trim($this->nbjour);
146 }
147 if (isset($this->decalage)) {
148 $this->decalage = trim($this->decalage);
149 }
150
151
152 // Check parameters
153 // Put here code to add control on parameters values
154
155 // Insert request
156 $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
157 $sql .= "entity,";
158 $sql .= "code,";
159 $sql .= "sortorder,";
160 $sql .= "active,";
161 $sql .= "libelle,";
162 $sql .= "libelle_facture,";
163 $sql .= "type_cdr,";
164 $sql .= "nbjour,";
165 $sql .= "decalage";
166 $sql .= ") VALUES (";
167 $sql .= " ".(!isset($this->entity) ? getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
168 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
169 $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
170 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
171 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
172 $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
173 $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
174 $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
175 $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'");
176 $sql .= ")";
177
178 $this->db->begin();
179
180 dol_syslog(get_class($this)."::create", LOG_DEBUG);
181 $resql = $this->db->query($sql);
182 if (!$resql) {
183 $error++;
184 $this->errors[] = "Error ".$this->db->lasterror();
185 }
186
187 if (!$error) {
188 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
189 }
190
191 // Commit or rollback
192 if ($error) {
193 foreach ($this->errors as $errmsg) {
194 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
195 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
196 }
197 $this->db->rollback();
198 return -1 * $error;
199 } else {
200 $this->db->commit();
201 return $this->id;
202 }
203 }
204
205
213 public function fetch($id, $code = '')
214 {
215 $sql = "SELECT";
216 $sql .= " t.rowid,";
217 $sql .= " t.entity,";
218 $sql .= " t.code,";
219 $sql .= " t.sortorder,";
220 $sql .= " t.active,";
221 $sql .= " t.libelle,";
222 $sql .= " t.libelle_facture,";
223 $sql .= " t.type_cdr,";
224 $sql .= " t.nbjour,";
225 $sql .= " t.decalage";
226 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
227 if ($id) {
228 $sql .= " WHERE t.rowid = ".((int) $id);
229 }
230 if ($code) {
231 $sql .= " WHERE t.code='".$this->db->escape($code)."' AND t.entity IN (".getEntity('payment_term').")";
232 }
233
234 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
235 $resql = $this->db->query($sql);
236 if ($resql) {
237 if ($this->db->num_rows($resql)) {
238 $obj = $this->db->fetch_object($resql);
239
240 $this->id = $obj->rowid;
241
242 $this->code = $obj->code;
243 $this->sortorder = $obj->sortorder;
244 $this->active = $obj->active;
245 $this->libelle = $obj->libelle;
246 $this->libelle_facture = $obj->libelle_facture;
247 $this->type_cdr = $obj->type_cdr;
248 $this->nbjour = $obj->nbjour;
249 $this->decalage = $obj->decalage;
250 }
251 $this->db->free($resql);
252
253 return 1;
254 } else {
255 $this->error = "Error ".$this->db->lasterror();
256 return -1;
257 }
258 }
259
260
266 public function getDefaultId()
267 {
268 global $langs;
269
270 $ret = 0;
271
272 $sql = "SELECT";
273 $sql .= " t.rowid";
274 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
275 $sql .= " WHERE t.code = 'RECEP'";
276 $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
277
278 dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
279 $resql = $this->db->query($sql);
280 if ($resql) {
281 if ($this->db->num_rows($resql)) {
282 $obj = $this->db->fetch_object($resql);
283 if ($obj) {
284 $ret = $obj->rowid;
285 }
286 }
287 $this->db->free($resql);
288 return $ret;
289 } else {
290 $this->error = "Error ".$this->db->lasterror();
291 return -1;
292 }
293 }
294
295
303 public function update($user = null, $notrigger = 0)
304 {
305 global $conf, $langs;
306
307 $error = 0;
308
309 // Clean parameters
310
311 if (isset($this->code)) {
312 $this->code = trim($this->code);
313 }
314 if (isset($this->sortorder)) {
315 $this->sortorder = trim($this->sortorder);
316 }
317 if (isset($this->active)) {
318 $this->active = trim($this->active);
319 }
320 if (isset($this->libelle)) {
321 $this->libelle = trim($this->libelle);
322 }
323 if (isset($this->libelle_facture)) {
324 $this->libelle_facture = trim($this->libelle_facture);
325 }
326 if (isset($this->type_cdr)) {
327 $this->type_cdr = trim($this->type_cdr);
328 }
329 if (isset($this->nbjour)) {
330 $this->nbjour = trim($this->nbjour);
331 }
332 if (isset($this->decalage)) {
333 $this->decalage = trim($this->decalage);
334 }
335
336
337
338 // Check parameters
339 // Put here code to add control on parameters values
340
341 // Update request
342 $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
343 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
344 $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
345 $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
346 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
347 $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
348 $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
349 $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
350 $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null");
351 $sql .= " WHERE rowid = ".((int) $this->id);
352
353 $this->db->begin();
354
355 dol_syslog(get_class($this)."::update", LOG_DEBUG);
356 $resql = $this->db->query($sql);
357 if (!$resql) {
358 $error++;
359 $this->errors[] = "Error ".$this->db->lasterror();
360 }
361
362 // Commit or rollback
363 if ($error) {
364 foreach ($this->errors as $errmsg) {
365 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
366 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
367 }
368 $this->db->rollback();
369 return -1 * $error;
370 } else {
371 $this->db->commit();
372 return 1;
373 }
374 }
375
376
384 public function delete($user, $notrigger = 0)
385 {
386 global $conf, $langs;
387 $error = 0;
388
389 $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
390 $sql .= " WHERE rowid = ".((int) $this->id);
391
392 $this->db->begin();
393
394 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
395 $resql = $this->db->query($sql);
396 if (!$resql) {
397 $error++;
398 $this->errors[] = "Error ".$this->db->lasterror();
399 }
400
401 // Commit or rollback
402 if ($error) {
403 foreach ($this->errors as $errmsg) {
404 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
405 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
406 }
407 $this->db->rollback();
408 return -1 * $error;
409 } else {
410 $this->db->commit();
411 return 1;
412 }
413 }
414
415
416
424 public function createFromClone(User $user, $fromid)
425 {
426 $error = 0;
427
428 $object = new PaymentTerm($this->db);
429
430 $this->db->begin();
431
432 // Load source object
433 $object->fetch($fromid);
434 $object->id = 0;
435
436 // Create clone
437 $object->context['createfromclone'] = 'createfromclone';
438 $result = $object->create($user);
439
440 // Other options
441 if ($result < 0) {
442 $this->error = $object->error;
443 $error++;
444 }
445
446 unset($object->context['createfromclone']);
447
448 // End
449 if (!$error) {
450 $this->db->commit();
451 return $object->id;
452 } else {
453 $this->db->rollback();
454 return -1;
455 }
456 }
457
458
466 public function initAsSpecimen()
467 {
468 $this->id = 0;
469
470 $this->code = '';
471 $this->sortorder = '';
472 $this->active = '';
473 $this->libelle = '';
474 $this->libelle_facture = '';
475 $this->type_cdr = '';
476 $this->nbjour = '';
477 $this->decalage = '';
478
479 return 1;
480 }
481}
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.