dolibarr  7.0.0-beta
deplacement.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
28 
33 {
34  public $element='deplacement';
35  public $table_element='deplacement';
36  public $table_element_line = '';
37  public $fk_element = '';
38  public $ismultientitymanaged = 0; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
39 
40  var $datec; // Creation date
41  var $dated;
42  var $fk_user_author;
43  var $fk_user;
44  var $km;
45  var $socid;
46  var $statut; // 0=draft, 1=validated
47  var $extraparams=array();
48 
49  var $statuts=array();
50  var $statuts_short=array();
51 
57  function __construct($db)
58  {
59  $this->db = $db;
60 
61  $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
62  $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
63  }
64 
72  function create($user)
73  {
74  global $conf;
75 
76  // Check parameters
77  if (empty($this->type) || $this->type < 0)
78  {
79  $this->error='ErrorBadParameter';
80  return -1;
81  }
82  if (empty($this->fk_user) || $this->fk_user < 0)
83  {
84  $this->error='ErrorBadParameter';
85  return -1;
86  }
87 
88  $now=dol_now();
89 
90  $this->db->begin();
91 
92  $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
93  $sql.= "datec";
94  //$sql.= ", dated";
95  $sql.= ", entity";
96  $sql.= ", fk_user_author";
97  $sql.= ", fk_user";
98  $sql.= ", type";
99  $sql.= ", note_private";
100  $sql.= ", note_public";
101  $sql.= ", fk_projet";
102  $sql.= ", fk_soc";
103  $sql.= ") VALUES (";
104  $sql.= " '".$this->db->idate($now)."'";
105  $sql.= ", ".$conf->entity;
106  $sql.= ", ".$user->id;
107  $sql.= ", ".$this->fk_user;
108  $sql.= ", '".$this->db->escape($this->type)."'";
109  $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
110  $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
111  $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
112  $sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
113  $sql.= ")";
114 
115  dol_syslog(get_class($this)."::create", LOG_DEBUG);
116  $result = $this->db->query($sql);
117  if ($result)
118  {
119  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
120 
121  // Call trigger
122  $result=$this->call_trigger('DEPLACEMENT_CREATE',$user);
123  if ($result < 0)
124  {
125  $this->db->rollback();
126  return -2;
127  }
128  // End call triggers
129 
130  $result=$this->update($user);
131  if ($result > 0)
132  {
133  $this->db->commit();
134  return $this->id;
135  }
136  else
137  {
138  $this->error=$this->db->error();
139  $this->db->rollback();
140  return $result;
141  }
142  }
143  else
144  {
145  $this->error=$this->db->error()." sql=".$sql;
146  $this->db->rollback();
147  return -1;
148  }
149 
150  }
151 
158  function update($user)
159  {
160  global $langs;
161 
162  // Clean parameters
163  $this->km=price2num($this->km);
164 
165  // Check parameters
166  if (! is_numeric($this->km)) $this->km = 0;
167  if (empty($this->date))
168  {
169  $this->error='ErrorBadParameter';
170  return -1;
171  }
172  if (empty($this->type) || $this->type < 0)
173  {
174  $this->error='ErrorBadParameter';
175  return -1;
176  }
177  if (empty($this->fk_user) || $this->fk_user < 0)
178  {
179  $this->error='ErrorBadParameter';
180  return -1;
181  }
182 
183  $this->db->begin();
184 
185  $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
186  $sql .= " SET km = ".$this->km; // This is a distance or amount
187  $sql .= " , dated = '".$this->db->idate($this->date)."'";
188  $sql .= " , type = '".$this->db->escape($this->type)."'";
189  $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
190  $sql .= " , fk_user = ".$this->fk_user;
191  $sql .= " , fk_user_modif = ".$user->id;
192  $sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
193  $sql .= " , note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
194  $sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
195  $sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
196  $sql .= " WHERE rowid = ".$this->id;
197 
198  dol_syslog(get_class($this)."::update", LOG_DEBUG);
199  $result = $this->db->query($sql);
200  if ($result)
201  {
202  $this->db->commit();
203  return 1;
204  }
205  else
206  {
207  $this->error=$this->db->lasterror();
208  $this->db->rollback();
209  return -1;
210  }
211  }
212 
220  function fetch($id, $ref='')
221  {
222  $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet, extraparams";
223  $sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
224  $sql.= " WHERE entity IN (".getEntity('deplacement').")";
225  if ($ref) $sql.= " AND ref ='".$this->db->escape($ref)."'";
226  else $sql.= " AND rowid = ".$id;
227 
228  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
229  $result = $this->db->query($sql);
230  if ( $result )
231  {
232  $obj = $this->db->fetch_object($result);
233 
234  $this->id = $obj->rowid;
235  $this->ref = $obj->rowid;
236  $this->date = $this->db->jdate($obj->dated);
237  $this->fk_user = $obj->fk_user;
238  $this->socid = $obj->fk_soc;
239  $this->km = $obj->km;
240  $this->type = $obj->type;
241  $this->statut = $obj->fk_statut;
242  $this->note_private = $obj->note_private;
243  $this->note_public = $obj->note_public;
244  $this->fk_project = $obj->fk_projet;
245 
246  $this->extraparams = (array) json_decode($obj->extraparams, true);
247 
248  return 1;
249  }
250  else
251  {
252  $this->error=$this->db->error();
253  return -1;
254  }
255  }
256 
263  function delete($id)
264  {
265  $this->db->begin();
266 
267  $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
268 
269  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
270  $result = $this->db->query($sql);
271  if ($result)
272  {
273  $this->db->commit();
274  return 1;
275  }
276  else
277  {
278  $this->error=$this->db->error();
279  $this->db->rollback();
280  return -1;
281  }
282  }
283 
284 
291  function getLibStatut($mode=0)
292  {
293  return $this->LibStatut($this->statut,$mode);
294  }
295 
303  function LibStatut($statut,$mode=0)
304  {
305  global $langs;
306 
307  if ($mode == 0)
308  {
309  return $langs->trans($this->statuts[$statut]);
310  }
311  if ($mode == 1)
312  {
313  return $langs->trans($this->statuts_short[$statut]);
314  }
315  if ($mode == 2)
316  {
317  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
318  if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
319  if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
320  }
321  if ($mode == 3)
322  {
323  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
324  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
325  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
326  }
327  if ($mode == 4)
328  {
329  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
330  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
331  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
332  }
333  if ($mode == 5)
334  {
335  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
336  if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
337  if ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
338  }
339  }
340 
347  function getNomUrl($withpicto=0)
348  {
349  global $langs;
350 
351  $result='';
352  $label=$langs->trans("Show").': '.$this->ref;
353 
354  $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
355  $linkend='</a>';
356 
357  $picto='trip';
358 
359 
360  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
361  if ($withpicto && $withpicto != 2) $result.=' ';
362  if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
363  return $result;
364  }
365 
366 
373  function listOfTypes($active=1)
374  {
375  global $langs;
376 
377  $ret=array();
378 
379  $sql = "SELECT id, code, label";
380  $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
381  $sql.= " WHERE active = ".$active;
382 
383  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
384  $result = $this->db->query($sql);
385  if ( $result )
386  {
387  $num = $this->db->num_rows($result);
388  $i=0;
389  while ($i < $num)
390  {
391  $obj = $this->db->fetch_object($result);
392  $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
393  $i++;
394  }
395  }
396  else
397  {
398  dol_print_error($this->db);
399  }
400 
401  return $ret;
402  }
403 
410  function info($id)
411  {
412  $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
413  $sql.= ' c.tms';
414  $sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
415  $sql.= ' WHERE c.rowid = '.$id;
416 
417  dol_syslog(get_class($this).'::info', LOG_DEBUG);
418  $result = $this->db->query($sql);
419 
420  if ($result)
421  {
422  if ($this->db->num_rows($result))
423  {
424  $obj = $this->db->fetch_object($result);
425  $this->id = $obj->rowid;
426  if ($obj->fk_user_author)
427  {
428  $cuser = new User($this->db);
429  $cuser->fetch($obj->fk_user_author);
430  $this->user_creation = $cuser;
431  }
432  if ($obj->fk_user_modif)
433  {
434  $muser = new User($this->db);
435  $muser->fetch($obj->fk_user_modif);
436  $this->user_modification = $muser;
437  }
438  $this->date_creation = $this->db->jdate($obj->datec);
439  $this->date_modification = $this->db->jdate($obj->tms);
440  }
441  $this->db->free($result);
442  }
443  else
444  {
445  dol_print_error($this->db);
446  }
447  }
448 
449 }
450 
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
Class to manage trips and working credit notes.
create($user)
Create object in database TODO Add ref number.
listOfTypes($active=1)
List of types.
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_now($mode='gmt')
Return date for now.
fetch($id, $ref='')
Load an object from database.
LibStatut($statut, $mode=0)
Renvoi le libelle d'un statut donne.
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
call_trigger($trigger_name, $user)
Call trigger based on this instance.
getLibStatut($mode=0)
Retourne le libelle du statut.
type
Definition: viewcat.php:283
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
info($id)
Information on record.
update($user)
Update record.