dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
16  */
17 
29 class 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  public $code;
56  public $sortorder;
57  public $active;
58  public $libelle;
59  public $libelle_facture;
60  public $type_cdr;
61  public $nbjour;
62  public $decalage;
63 
64 
65 
66 
72  function __construct($db)
73  {
74  $this->db = $db;
75  }
76 
77 
85  function create($user, $notrigger=0)
86  {
87  global $conf, $langs;
88  $error=0;
89 
90  // Clean parameters
91 
92  if (isset($this->code)) $this->code=trim($this->code);
93  if (isset($this->sortorder)) $this->sortorder=trim($this->sortorder);
94  if (isset($this->active)) $this->active=trim($this->active);
95  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
96  if (isset($this->libelle_facture)) $this->libelle_facture=trim($this->libelle_facture);
97  if (isset($this->type_cdr)) $this->type_cdr=trim($this->type_cdr);
98  if (isset($this->nbjour)) $this->nbjour=trim($this->nbjour);
99  if (isset($this->decalage)) $this->decalage=trim($this->decalage);
100 
101 
102  // Check parameters
103  // Put here code to add control on parameters values
104 
105  // Insert request
106  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
107  $sql.= "entity,";
108  $sql.= "code,";
109  $sql.= "sortorder,";
110  $sql.= "active,";
111  $sql.= "libelle,";
112  $sql.= "libelle_facture,";
113  $sql.= "type_cdr,";
114  $sql.= "nbjour,";
115  $sql.= "decalage";
116  $sql.= ") VALUES (";
117  $sql.= " ".(! isset($this->entity)?getEntity('c_payment_term'):"'".$this->db->escape($this->entity)."'").",";
118  $sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
119  $sql.= " ".(! isset($this->sortorder)?'NULL':"'".$this->db->escape($this->sortorder)."'").",";
120  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->escape($this->active)."'").",";
121  $sql.= " ".(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").",";
122  $sql.= " ".(! isset($this->libelle_facture)?'NULL':"'".$this->db->escape($this->libelle_facture)."'").",";
123  $sql.= " ".(! isset($this->type_cdr)?'NULL':"'".$this->db->escape($this->type_cdr)."'").",";
124  $sql.= " ".(! isset($this->nbjour)?'NULL':"'".$this->db->escape($this->nbjour)."'").",";
125  $sql.= " ".(! isset($this->decalage)?'NULL':"'".$this->db->escape($this->decalage)."'")."";
126  $sql.= ")";
127 
128  $this->db->begin();
129 
130  dol_syslog(get_class($this)."::create", LOG_DEBUG);
131  $resql=$this->db->query($sql);
132  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
133 
134  if (! $error)
135  {
136  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
137 
138  // Uncomment this and change MYOBJECT to your own tag if you
139  // want this action call a trigger.
140  //if (! $notrigger) {
141 
142  // // Call triggers
143  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
144  // $interface=new Interfaces($this->db);
145  // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
146  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
147  // // End call triggers
148  //}
149  }
150 
151  // Commit or rollback
152  if ($error)
153  {
154  foreach($this->errors as $errmsg)
155  {
156  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
157  $this->error.=($this->error?', '.$errmsg:$errmsg);
158  }
159  $this->db->rollback();
160  return -1*$error;
161  }
162  else
163  {
164  $this->db->commit();
165  return $this->id;
166  }
167  }
168 
169 
176  function fetch($id)
177  {
178  global $langs;
179  $sql = "SELECT";
180  $sql.= " t.rowid,";
181  $sql.= " t.entity,";
182 
183  $sql.= " t.code,";
184  $sql.= " t.sortorder,";
185  $sql.= " t.active,";
186  $sql.= " t.libelle,";
187  $sql.= " t.libelle_facture,";
188  $sql.= " t.type_cdr,";
189  $sql.= " t.nbjour,";
190  $sql.= " t.decalage";
191 
192 
193  $sql.= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
194  $sql.= " WHERE t.rowid = ".$id;
195 
196  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
197  $resql=$this->db->query($sql);
198  if ($resql)
199  {
200  if ($this->db->num_rows($resql))
201  {
202  $obj = $this->db->fetch_object($resql);
203 
204  $this->id = $obj->rowid;
205 
206  $this->code = $obj->code;
207  $this->sortorder = $obj->sortorder;
208  $this->active = $obj->active;
209  $this->libelle = $obj->libelle;
210  $this->libelle_facture = $obj->libelle_facture;
211  $this->type_cdr = $obj->type_cdr;
212  $this->nbjour = $obj->nbjour;
213  $this->decalage = $obj->decalage;
214  }
215  $this->db->free($resql);
216 
217  return 1;
218  }
219  else
220  {
221  $this->error="Error ".$this->db->lasterror();
222  return -1;
223  }
224  }
225 
226 
232  function getDefaultId()
233  {
234  global $langs;
235 
236  $ret=0;
237 
238  $sql = "SELECT";
239  $sql.= " t.rowid";
240  $sql.= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
241  $sql.= " WHERE t.code = 'RECEP'";
242  $sql.= " AND t.entity IN (".getEntity('c_payment_term').")";
243 
244  dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
245  $resql=$this->db->query($sql);
246  if ($resql)
247  {
248  if ($this->db->num_rows($resql))
249  {
250  $obj = $this->db->fetch_object($resql);
251  if ($obj) $ret=$obj->rowid;
252  }
253  $this->db->free($resql);
254  return $ret;
255  }
256  else
257  {
258  $this->error="Error ".$this->db->lasterror();
259  return -1;
260  }
261  }
262 
263 
271  function update($user=null, $notrigger=0)
272  {
273  global $conf, $langs;
274 
275  $error=0;
276 
277  // Clean parameters
278 
279  if (isset($this->code)) $this->code=trim($this->code);
280  if (isset($this->sortorder)) $this->sortorder=trim($this->sortorder);
281  if (isset($this->active)) $this->active=trim($this->active);
282  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
283  if (isset($this->libelle_facture)) $this->libelle_facture=trim($this->libelle_facture);
284  if (isset($this->type_cdr)) $this->type_cdr=trim($this->type_cdr);
285  if (isset($this->nbjour)) $this->nbjour=trim($this->nbjour);
286  if (isset($this->decalage)) $this->decalage=trim($this->decalage);
287 
288 
289 
290  // Check parameters
291  // Put here code to add control on parameters values
292 
293  // Update request
294  $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
295  $sql.= " code=".(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").",";
296  $sql.= " sortorder=".(isset($this->sortorder)?$this->sortorder:"null").",";
297  $sql.= " active=".(isset($this->active)?$this->active:"null").",";
298  $sql.= " libelle=".(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").",";
299  $sql.= " libelle_facture=".(isset($this->libelle_facture)?"'".$this->db->escape($this->libelle_facture)."'":"null").",";
300  $sql.= " type_cdr=".(isset($this->type_cdr)?$this->type_cdr:"null").",";
301  $sql.= " nbjour=".(isset($this->nbjour)?$this->nbjour:"null").",";
302  $sql.= " decalage=".(isset($this->decalage)?$this->decalage:"null")."";
303  $sql.= " WHERE rowid = " . $this->id;
304 
305  $this->db->begin();
306 
307  dol_syslog(get_class($this)."::update", LOG_DEBUG);
308  $resql = $this->db->query($sql);
309  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
310 
311  // Uncomment this and change MYOBJECT to your own tag if you
312  // want this action call a trigger.
313  //if (! $error && ! $notrigger) {
314  // Call triggers
315  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
316  //$interface=new Interfaces($this->db);
317  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
318  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
319  // End call triggers
320  //}
321 
322  // Commit or rollback
323  if ($error)
324  {
325  foreach($this->errors as $errmsg)
326  {
327  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
328  $this->error.=($this->error?', '.$errmsg:$errmsg);
329  }
330  $this->db->rollback();
331  return -1*$error;
332  }
333  else
334  {
335  $this->db->commit();
336  return 1;
337  }
338  }
339 
340 
348  function delete($user, $notrigger=0)
349  {
350  global $conf, $langs;
351  $error=0;
352 
353  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
354  $sql.= " WHERE rowid = " . $this->id;
355 
356  $this->db->begin();
357 
358  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
359  $resql = $this->db->query($sql);
360  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
361 
362  // Uncomment this and change MYOBJECT to your own tag if you
363  // want this action call a trigger.
364  //if (! $error && ! $notrigger) {
366  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
367  //$interface=new Interfaces($this->db);
368  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
369  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
371  //}
372 
373  // Commit or rollback
374  if ($error)
375  {
376  foreach($this->errors as $errmsg)
377  {
378  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
379  $this->error.=($this->error?', '.$errmsg:$errmsg);
380  }
381  $this->db->rollback();
382  return -1*$error;
383  }
384  else
385  {
386  $this->db->commit();
387  return 1;
388  }
389  }
390 
391 
392 
399  function createFromClone($fromid)
400  {
401  global $user,$langs;
402 
403  $error=0;
404 
405  $object=new PaymentTerm($this->db);
406 
407  $this->db->begin();
408 
409  // Load source object
410  $object->fetch($fromid);
411  $object->id=0;
412  $object->statut=0;
413 
414  // Clear fields
415  // ...
416 
417  // Create clone
418  $object->context['createfromclone'] = 'createfromclone';
419  $result=$object->create($user);
420 
421  // Other options
422  if ($result < 0)
423  {
424  $this->error=$object->error;
425  $error++;
426  }
427 
428  //if (! $error)
429  //{
430  //}
431 
432  unset($object->context['createfromclone']);
433 
434  // End
435  if (! $error)
436  {
437  $this->db->commit();
438  return $object->id;
439  }
440  else
441  {
442  $this->db->rollback();
443  return -1;
444  }
445  }
446 
447 
455  function initAsSpecimen()
456  {
457  $this->id=0;
458 
459  $this->code='';
460  $this->sortorder='';
461  $this->active='';
462  $this->libelle='';
463  $this->libelle_facture='';
464  $this->type_cdr='';
465  $this->nbjour='';
466  $this->decalage='';
467  }
468 }
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
update($user=null, $notrigger=0)
Update database.
fetch($id)
Load object in memory from database.
create($user, $notrigger=0)
Create in database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
initAsSpecimen()
Initialise an instance with random values.
getDefaultId()
Return id of default payment term.
Class to manage payment terms records in dictionary.
createFromClone($fromid)
Load an object from its id and create a new one in database.
__construct($db)
Constructor.