dolibarr  7.0.0-beta
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 {
31  var $db;
32  var $error;
33  var $errors=array();
34  //public $element='c_payment_term'; //!< Id that identify managed objects
35  //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
36  var $context =array();
37 
38  var $id;
39 
40  var $code;
41  var $sortorder;
42  var $active;
43  var $libelle;
44  var $libelle_facture;
45  var $type_cdr;
46  var $nbjour;
47  var $decalage;
48 
49 
50 
51 
57  function __construct($db)
58  {
59  $this->db = $db;
60  return 1;
61  }
62 
63 
71  function create($user, $notrigger=0)
72  {
73  global $conf, $langs;
74  $error=0;
75 
76  // Clean parameters
77 
78  if (isset($this->code)) $this->code=trim($this->code);
79  if (isset($this->sortorder)) $this->sortorder=trim($this->sortorder);
80  if (isset($this->active)) $this->active=trim($this->active);
81  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
82  if (isset($this->libelle_facture)) $this->libelle_facture=trim($this->libelle_facture);
83  if (isset($this->type_cdr)) $this->type_cdr=trim($this->type_cdr);
84  if (isset($this->nbjour)) $this->nbjour=trim($this->nbjour);
85  if (isset($this->decalage)) $this->decalage=trim($this->decalage);
86 
87 
88  // Check parameters
89  // Put here code to add control on parameters values
90 
91  // Insert request
92  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(";
93  $sql.= "rowid,";
94  $sql.= "entity,";
95  $sql.= "code,";
96  $sql.= "sortorder,";
97  $sql.= "active,";
98  $sql.= "libelle,";
99  $sql.= "libelle_facture,";
100  $sql.= "type_cdr,";
101  $sql.= "nbjour,";
102  $sql.= "decalage";
103  $sql.= ") VALUES (";
104  $sql.= " ".(! isset($this->rowid)?'NULL':"'".$this->db->escape($this->rowid)."'").",";
105  $sql.= " ".(! isset($this->entity)?getEntity('c_payment_term'):"'".$this->db->escape($this->entity)."'").",";
106  $sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
107  $sql.= " ".(! isset($this->sortorder)?'NULL':"'".$this->db->escape($this->sortorder)."'").",";
108  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->escape($this->active)."'").",";
109  $sql.= " ".(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").",";
110  $sql.= " ".(! isset($this->libelle_facture)?'NULL':"'".$this->db->escape($this->libelle_facture)."'").",";
111  $sql.= " ".(! isset($this->type_cdr)?'NULL':"'".$this->db->escape($this->type_cdr)."'").",";
112  $sql.= " ".(! isset($this->nbjour)?'NULL':"'".$this->db->escape($this->nbjour)."'").",";
113  $sql.= " ".(! isset($this->decalage)?'NULL':"'".$this->db->escape($this->decalage)."'")."";
114  $sql.= ")";
115 
116  $this->db->begin();
117 
118  dol_syslog(get_class($this)."::create", LOG_DEBUG);
119  $resql=$this->db->query($sql);
120  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
121 
122  if (! $error)
123  {
124  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_payment_term");
125 
126  if (! $notrigger)
127  {
128  // Uncomment this and change MYOBJECT to your own tag if you
129  // want this action call a trigger.
130 
132  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
133  //$interface=new Interfaces($this->db);
134  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
135  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
137  }
138  }
139 
140  // Commit or rollback
141  if ($error)
142  {
143  foreach($this->errors as $errmsg)
144  {
145  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
146  $this->error.=($this->error?', '.$errmsg:$errmsg);
147  }
148  $this->db->rollback();
149  return -1*$error;
150  }
151  else
152  {
153  $this->db->commit();
154  return $this->id;
155  }
156  }
157 
158 
165  function fetch($id)
166  {
167  global $langs;
168  $sql = "SELECT";
169  $sql.= " t.rowid,";
170  $sql.= " t.entity";
171 
172  $sql.= " t.code,";
173  $sql.= " t.sortorder,";
174  $sql.= " t.active,";
175  $sql.= " t.libelle,";
176  $sql.= " t.libelle_facture,";
177  $sql.= " t.type_cdr,";
178  $sql.= " t.nbjour,";
179  $sql.= " t.decalage";
180 
181 
182  $sql.= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
183  $sql.= " WHERE t.rowid = ".$id;
184  $sql.= " AND t.entity = " . getEntity('c_payment_term');
185 
186  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
187  $resql=$this->db->query($sql);
188  if ($resql)
189  {
190  if ($this->db->num_rows($resql))
191  {
192  $obj = $this->db->fetch_object($resql);
193 
194  $this->id = $obj->rowid;
195 
196  $this->code = $obj->code;
197  $this->sortorder = $obj->sortorder;
198  $this->active = $obj->active;
199  $this->libelle = $obj->libelle;
200  $this->libelle_facture = $obj->libelle_facture;
201  $this->type_cdr = $obj->type_cdr;
202  $this->nbjour = $obj->nbjour;
203  $this->decalage = $obj->decalage;
204 
205 
206  }
207  $this->db->free($resql);
208 
209  return 1;
210  }
211  else
212  {
213  $this->error="Error ".$this->db->lasterror();
214  return -1;
215  }
216  }
217 
218 
224  function getDefaultId()
225  {
226  global $langs;
227 
228  $ret=0;
229 
230  $sql = "SELECT";
231  $sql.= " t.rowid";
232  $sql.= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
233  $sql.= " WHERE t.code = 'RECEP'";
234  $sql.= " AND t.entity = " . getEntity('c_payment_term');
235 
236  dol_syslog(get_class($this)."::getDefaultId", LOG_DEBUG);
237  $resql=$this->db->query($sql);
238  if ($resql)
239  {
240  if ($this->db->num_rows($resql))
241  {
242  $obj = $this->db->fetch_object($resql);
243  if ($obj) $ret=$obj->rowid;
244  }
245  $this->db->free($resql);
246  return $ret;
247  }
248  else
249  {
250  $this->error="Error ".$this->db->lasterror();
251  return -1;
252  }
253  }
254 
255 
263  function update($user=null, $notrigger=0)
264  {
265  global $conf, $langs;
266 
267  $error=0;
268 
269  // Clean parameters
270 
271  if (isset($this->code)) $this->code=trim($this->code);
272  if (isset($this->sortorder)) $this->sortorder=trim($this->sortorder);
273  if (isset($this->active)) $this->active=trim($this->active);
274  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
275  if (isset($this->libelle_facture)) $this->libelle_facture=trim($this->libelle_facture);
276  if (isset($this->type_cdr)) $this->type_cdr=trim($this->type_cdr);
277  if (isset($this->nbjour)) $this->nbjour=trim($this->nbjour);
278  if (isset($this->decalage)) $this->decalage=trim($this->decalage);
279 
280 
281 
282  // Check parameters
283  // Put here code to add control on parameters values
284 
285  // Update request
286  $sql = "UPDATE ".MAIN_DB_PREFIX."c_payment_term SET";
287  $sql.= " code=".(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").",";
288  $sql.= " sortorder=".(isset($this->sortorder)?$this->sortorder:"null").",";
289  $sql.= " active=".(isset($this->active)?$this->active:"null").",";
290  $sql.= " libelle=".(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").",";
291  $sql.= " libelle_facture=".(isset($this->libelle_facture)?"'".$this->db->escape($this->libelle_facture)."'":"null").",";
292  $sql.= " type_cdr=".(isset($this->type_cdr)?$this->type_cdr:"null").",";
293  $sql.= " nbjour=".(isset($this->nbjour)?$this->nbjour:"null").",";
294  $sql.= " decalage=".(isset($this->decalage)?$this->decalage:"null")."";
295  $sql.= " WHERE rowid = " . $this->id;
296  $sql.= " AND entity = " . getEntity('c_payment_term');
297 
298  $this->db->begin();
299 
300  dol_syslog(get_class($this)."::update", LOG_DEBUG);
301  $resql = $this->db->query($sql);
302  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
303 
304  if (! $error)
305  {
306  if (! $notrigger)
307  {
308  // Uncomment this and change MYOBJECT to your own tag if you
309  // want this action call a trigger.
310 
311  // Call triggers
312  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
313  //$interface=new Interfaces($this->db);
314  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
315  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
316  // End call triggers
317  }
318  }
319 
320  // Commit or rollback
321  if ($error)
322  {
323  foreach($this->errors as $errmsg)
324  {
325  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
326  $this->error.=($this->error?', '.$errmsg:$errmsg);
327  }
328  $this->db->rollback();
329  return -1*$error;
330  }
331  else
332  {
333  $this->db->commit();
334  return 1;
335  }
336  }
337 
338 
346  function delete($user, $notrigger=0)
347  {
348  global $conf, $langs;
349  $error=0;
350 
351  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_payment_term";
352  $sql.= " WHERE rowid = " . $this->id;
353  $sql.= " AND t.entity = " . getEntity('c_payment_term');
354 
355  $this->db->begin();
356 
357  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
358  $resql = $this->db->query($sql);
359  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
360 
361  if (! $error)
362  {
363  if (! $notrigger)
364  {
365  // Uncomment this and change MYOBJECT to your own tag if you
366  // want this action call a trigger.
367 
369  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
370  //$interface=new Interfaces($this->db);
371  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
372  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
374  }
375  }
376 
377  // Commit or rollback
378  if ($error)
379  {
380  foreach($this->errors as $errmsg)
381  {
382  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
383  $this->error.=($this->error?', '.$errmsg:$errmsg);
384  }
385  $this->db->rollback();
386  return -1*$error;
387  }
388  else
389  {
390  $this->db->commit();
391  return 1;
392  }
393  }
394 
395 
396 
403  function createFromClone($fromid)
404  {
405  global $user,$langs;
406 
407  $error=0;
408 
409  $object=new PaymentTerm($this->db);
410 
411  $object->context['createfromclone'] = 'createfromclone';
412 
413  $this->db->begin();
414 
415  // Load source object
416  $object->fetch($fromid);
417  $object->id=0;
418  $object->statut=0;
419 
420  // Clear fields
421  // ...
422 
423  // Create clone
424  $result=$object->create($user);
425 
426  // Other options
427  if ($result < 0)
428  {
429  $this->error=$object->error;
430  $error++;
431  }
432 
433  if (! $error)
434  {
435 
436 
437 
438  }
439 
440  unset($this->context['createfromclone']);
441 
442  // End
443  if (! $error)
444  {
445  $this->db->commit();
446  return $object->id;
447  }
448  else
449  {
450  $this->db->rollback();
451  return -1;
452  }
453  }
454 
455 
463  function initAsSpecimen()
464  {
465  $this->id=0;
466 
467  $this->code='';
468  $this->sortorder='';
469  $this->active='';
470  $this->libelle='';
471  $this->libelle_facture='';
472  $this->type_cdr='';
473  $this->nbjour='';
474  $this->decalage='';
475  }
476 
477 }
update($user=null, $notrigger=0)
Update database.
fetch($id)
Load object in memory from database.
create($user, $notrigger=0)
Create in database.
$error
To return error code (or message)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
$db
To store db handler.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
getDefaultId()
Return id of default payment term.
$errors
To return several error codes (or messages)
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:1013
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.