dolibarr  13.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  *
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 
28 class 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($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)) $this->code = trim($this->code);
92  if (isset($this->sortorder)) $this->sortorder = trim($this->sortorder);
93  if (isset($this->active)) $this->active = trim($this->active);
94  if (isset($this->libelle)) $this->libelle = trim($this->libelle);
95  if (isset($this->libelle_facture)) $this->libelle_facture = trim($this->libelle_facture);
96  if (isset($this->type_cdr)) $this->type_cdr = trim($this->type_cdr);
97  if (isset($this->nbjour)) $this->nbjour = trim($this->nbjour);
98  if (isset($this->decalage)) $this->decalage = trim($this->decalage);
99 
100 
101  // Check parameters
102  // Put here code to add control on parameters values
103 
104  // Insert request
105  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
106  $sql .= "entity,";
107  $sql .= "code,";
108  $sql .= "sortorder,";
109  $sql .= "active,";
110  $sql .= "libelle,";
111  $sql .= "libelle_facture,";
112  $sql .= "type_cdr,";
113  $sql .= "nbjour,";
114  $sql .= "decalage";
115  $sql .= ") VALUES (";
116  $sql .= " ".(!isset($this->entity) ?getEntity('c_payment_term') : "'".$this->db->escape($this->entity)."'").",";
117  $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
118  $sql .= " ".(!isset($this->sortorder) ? 'NULL' : "'".$this->db->escape($this->sortorder)."'").",";
119  $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
120  $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
121  $sql .= " ".(!isset($this->libelle_facture) ? 'NULL' : "'".$this->db->escape($this->libelle_facture)."'").",";
122  $sql .= " ".(!isset($this->type_cdr) ? 'NULL' : "'".$this->db->escape($this->type_cdr)."'").",";
123  $sql .= " ".(!isset($this->nbjour) ? 'NULL' : "'".$this->db->escape($this->nbjour)."'").",";
124  $sql .= " ".(!isset($this->decalage) ? 'NULL' : "'".$this->db->escape($this->decalage)."'")."";
125  $sql .= ")";
126 
127  $this->db->begin();
128 
129  dol_syslog(get_class($this)."::create", LOG_DEBUG);
130  $resql = $this->db->query($sql);
131  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
132 
133  if (!$error)
134  {
135  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
136  }
137 
138  // Commit or rollback
139  if ($error)
140  {
141  foreach ($this->errors as $errmsg)
142  {
143  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
144  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
145  }
146  $this->db->rollback();
147  return -1 * $error;
148  } else {
149  $this->db->commit();
150  return $this->id;
151  }
152  }
153 
154 
161  public function fetch($id)
162  {
163  global $langs;
164  $sql = "SELECT";
165  $sql .= " t.rowid,";
166  $sql .= " t.entity,";
167 
168  $sql .= " t.code,";
169  $sql .= " t.sortorder,";
170  $sql .= " t.active,";
171  $sql .= " t.libelle,";
172  $sql .= " t.libelle_facture,";
173  $sql .= " t.type_cdr,";
174  $sql .= " t.nbjour,";
175  $sql .= " t.decalage";
176 
177 
178  $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
179  $sql .= " WHERE t.rowid = ".$id;
180 
181  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
182  $resql = $this->db->query($sql);
183  if ($resql)
184  {
185  if ($this->db->num_rows($resql))
186  {
187  $obj = $this->db->fetch_object($resql);
188 
189  $this->id = $obj->rowid;
190 
191  $this->code = $obj->code;
192  $this->sortorder = $obj->sortorder;
193  $this->active = $obj->active;
194  $this->libelle = $obj->libelle;
195  $this->libelle_facture = $obj->libelle_facture;
196  $this->type_cdr = $obj->type_cdr;
197  $this->nbjour = $obj->nbjour;
198  $this->decalage = $obj->decalage;
199  }
200  $this->db->free($resql);
201 
202  return 1;
203  } else {
204  $this->error = "Error ".$this->db->lasterror();
205  return -1;
206  }
207  }
208 
209 
215  public function getDefaultId()
216  {
217  global $langs;
218 
219  $ret = 0;
220 
221  $sql = "SELECT";
222  $sql .= " t.rowid";
223  $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
224  $sql .= " WHERE t.code = 'RECEP'";
225  $sql .= " AND t.entity IN (".getEntity('c_payment_term').")";
226 
227  dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
228  $resql = $this->db->query($sql);
229  if ($resql)
230  {
231  if ($this->db->num_rows($resql))
232  {
233  $obj = $this->db->fetch_object($resql);
234  if ($obj) $ret = $obj->rowid;
235  }
236  $this->db->free($resql);
237  return $ret;
238  } else {
239  $this->error = "Error ".$this->db->lasterror();
240  return -1;
241  }
242  }
243 
244 
252  public function update($user = null, $notrigger = 0)
253  {
254  global $conf, $langs;
255 
256  $error = 0;
257 
258  // Clean parameters
259 
260  if (isset($this->code)) $this->code = trim($this->code);
261  if (isset($this->sortorder)) $this->sortorder = trim($this->sortorder);
262  if (isset($this->active)) $this->active = trim($this->active);
263  if (isset($this->libelle)) $this->libelle = trim($this->libelle);
264  if (isset($this->libelle_facture)) $this->libelle_facture = trim($this->libelle_facture);
265  if (isset($this->type_cdr)) $this->type_cdr = trim($this->type_cdr);
266  if (isset($this->nbjour)) $this->nbjour = trim($this->nbjour);
267  if (isset($this->decalage)) $this->decalage = trim($this->decalage);
268 
269 
270 
271  // Check parameters
272  // Put here code to add control on parameters values
273 
274  // Update request
275  $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
276  $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
277  $sql .= " sortorder=".(isset($this->sortorder) ? $this->sortorder : "null").",";
278  $sql .= " active=".(isset($this->active) ? $this->active : "null").",";
279  $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
280  $sql .= " libelle_facture=".(isset($this->libelle_facture) ? "'".$this->db->escape($this->libelle_facture)."'" : "null").",";
281  $sql .= " type_cdr=".(isset($this->type_cdr) ? $this->type_cdr : "null").",";
282  $sql .= " nbjour=".(isset($this->nbjour) ? $this->nbjour : "null").",";
283  $sql .= " decalage=".(isset($this->decalage) ? $this->decalage : "null")."";
284  $sql .= " WHERE rowid = ".$this->id;
285 
286  $this->db->begin();
287 
288  dol_syslog(get_class($this)."::update", LOG_DEBUG);
289  $resql = $this->db->query($sql);
290  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
291 
292  // Commit or rollback
293  if ($error)
294  {
295  foreach ($this->errors as $errmsg)
296  {
297  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
298  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
299  }
300  $this->db->rollback();
301  return -1 * $error;
302  } else {
303  $this->db->commit();
304  return 1;
305  }
306  }
307 
308 
316  public function delete($user, $notrigger = 0)
317  {
318  global $conf, $langs;
319  $error = 0;
320 
321  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
322  $sql .= " WHERE rowid = ".$this->id;
323 
324  $this->db->begin();
325 
326  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
327  $resql = $this->db->query($sql);
328  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
329 
330  // Commit or rollback
331  if ($error)
332  {
333  foreach ($this->errors as $errmsg)
334  {
335  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
336  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
337  }
338  $this->db->rollback();
339  return -1 * $error;
340  } else {
341  $this->db->commit();
342  return 1;
343  }
344  }
345 
346 
347 
355  public function createFromClone(User $user, $fromid)
356  {
357  $error = 0;
358 
359  $object = new PaymentTerm($this->db);
360 
361  $this->db->begin();
362 
363  // Load source object
364  $object->fetch($fromid);
365  $object->id = 0;
366  $object->statut = 0;
367 
368  // Clear fields
369  // ...
370 
371  // Create clone
372  $object->context['createfromclone'] = 'createfromclone';
373  $result = $object->create($user);
374 
375  // Other options
376  if ($result < 0)
377  {
378  $this->error = $object->error;
379  $error++;
380  }
381 
382  unset($object->context['createfromclone']);
383 
384  // End
385  if (!$error)
386  {
387  $this->db->commit();
388  return $object->id;
389  } else {
390  $this->db->rollback();
391  return -1;
392  }
393  }
394 
395 
403  public function initAsSpecimen()
404  {
405  $this->id = 0;
406 
407  $this->code = '';
408  $this->sortorder = '';
409  $this->active = '';
410  $this->libelle = '';
411  $this->libelle_facture = '';
412  $this->type_cdr = '';
413  $this->nbjour = '';
414  $this->decalage = '';
415  }
416 }
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update database.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
fetch($id)
Load object in memory from database.
Class to manage Dolibarr users.
Definition: user.class.php:44
create($user, $notrigger=0)
Create in database.
$conf db
API class for accounts.
Definition: inc.php:54
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
initAsSpecimen()
Initialise an instance with random values.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getDefaultId()
Return id of default payment term.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class to manage payment terms records in dictionary.
__construct($db)
Constructor.