dolibarr  9.0.0
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@inodbox.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 {
37  public $element='deplacement';
38 
42  public $table_element='deplacement';
43 
47  public $table_element_line = '';
48 
52  public $fk_element = '';
53 
58  public $ismultientitymanaged = 0;
59 
60  public $datec; // Creation date
61  public $dated;
62 
66  public $fk_user_author;
67 
71  public $fk_user;
72 
73  public $km;
74  public $socid;
75  public $statut; // 0=draft, 1=validated
76  public $extraparams=array();
77 
78  public $statuts=array();
79  public $statuts_short=array();
80 
86  function __construct($db)
87  {
88  $this->db = $db;
89 
90  $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
91  $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
92  }
93 
101  function create($user)
102  {
103  global $conf;
104 
105  // Check parameters
106  if (empty($this->type) || $this->type < 0)
107  {
108  $this->error='ErrorBadParameter';
109  return -1;
110  }
111  if (empty($this->fk_user) || $this->fk_user < 0)
112  {
113  $this->error='ErrorBadParameter';
114  return -1;
115  }
116 
117  $now=dol_now();
118 
119  $this->db->begin();
120 
121  $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
122  $sql.= "datec";
123  //$sql.= ", dated";
124  $sql.= ", entity";
125  $sql.= ", fk_user_author";
126  $sql.= ", fk_user";
127  $sql.= ", type";
128  $sql.= ", note_private";
129  $sql.= ", note_public";
130  $sql.= ", fk_projet";
131  $sql.= ", fk_soc";
132  $sql.= ") VALUES (";
133  $sql.= " '".$this->db->idate($now)."'";
134  $sql.= ", ".$conf->entity;
135  $sql.= ", ".$user->id;
136  $sql.= ", ".$this->fk_user;
137  $sql.= ", '".$this->db->escape($this->type)."'";
138  $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
139  $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
140  $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
141  $sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
142  $sql.= ")";
143 
144  dol_syslog(get_class($this)."::create", LOG_DEBUG);
145  $result = $this->db->query($sql);
146  if ($result)
147  {
148  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
149 
150  // Call trigger
151  $result=$this->call_trigger('DEPLACEMENT_CREATE',$user);
152  if ($result < 0)
153  {
154  $this->db->rollback();
155  return -2;
156  }
157  // End call triggers
158 
159  $result=$this->update($user);
160  if ($result > 0)
161  {
162  $this->db->commit();
163  return $this->id;
164  }
165  else
166  {
167  $this->error=$this->db->error();
168  $this->db->rollback();
169  return $result;
170  }
171  }
172  else
173  {
174  $this->error=$this->db->error()." sql=".$sql;
175  $this->db->rollback();
176  return -1;
177  }
178  }
179 
186  function update($user)
187  {
188  global $langs;
189 
190  // Clean parameters
191  $this->km=price2num($this->km);
192 
193  // Check parameters
194  if (! is_numeric($this->km)) $this->km = 0;
195  if (empty($this->date))
196  {
197  $this->error='ErrorBadParameter';
198  return -1;
199  }
200  if (empty($this->type) || $this->type < 0)
201  {
202  $this->error='ErrorBadParameter';
203  return -1;
204  }
205  if (empty($this->fk_user) || $this->fk_user < 0)
206  {
207  $this->error='ErrorBadParameter';
208  return -1;
209  }
210 
211  $this->db->begin();
212 
213  $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
214  $sql .= " SET km = ".$this->km; // This is a distance or amount
215  $sql .= " , dated = '".$this->db->idate($this->date)."'";
216  $sql .= " , type = '".$this->db->escape($this->type)."'";
217  $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
218  $sql .= " , fk_user = ".$this->fk_user;
219  $sql .= " , fk_user_modif = ".$user->id;
220  $sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
221  $sql .= " , note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
222  $sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
223  $sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
224  $sql .= " WHERE rowid = ".$this->id;
225 
226  dol_syslog(get_class($this)."::update", LOG_DEBUG);
227  $result = $this->db->query($sql);
228  if ($result)
229  {
230  $this->db->commit();
231  return 1;
232  }
233  else
234  {
235  $this->error=$this->db->lasterror();
236  $this->db->rollback();
237  return -1;
238  }
239  }
240 
248  function fetch($id, $ref='')
249  {
250  $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet, extraparams";
251  $sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
252  $sql.= " WHERE entity IN (".getEntity('deplacement').")";
253  if ($ref) $sql.= " AND ref ='".$this->db->escape($ref)."'";
254  else $sql.= " AND rowid = ".$id;
255 
256  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
257  $result = $this->db->query($sql);
258  if ( $result )
259  {
260  $obj = $this->db->fetch_object($result);
261 
262  $this->id = $obj->rowid;
263  $this->ref = $obj->rowid;
264  $this->date = $this->db->jdate($obj->dated);
265  $this->fk_user = $obj->fk_user;
266  $this->socid = $obj->fk_soc;
267  $this->km = $obj->km;
268  $this->type = $obj->type;
269  $this->statut = $obj->fk_statut;
270  $this->note_private = $obj->note_private;
271  $this->note_public = $obj->note_public;
272  $this->fk_project = $obj->fk_projet;
273 
274  $this->extraparams = (array) json_decode($obj->extraparams, true);
275 
276  return 1;
277  }
278  else
279  {
280  $this->error=$this->db->error();
281  return -1;
282  }
283  }
284 
291  function delete($id)
292  {
293  $this->db->begin();
294 
295  $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
296 
297  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
298  $result = $this->db->query($sql);
299  if ($result)
300  {
301  $this->db->commit();
302  return 1;
303  }
304  else
305  {
306  $this->error=$this->db->error();
307  $this->db->rollback();
308  return -1;
309  }
310  }
311 
312 
319  function getLibStatut($mode=0)
320  {
321  return $this->LibStatut($this->statut,$mode);
322  }
323 
324  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
332  function LibStatut($statut,$mode=0)
333  {
334  // phpcs:enable
335  global $langs;
336 
337  if ($mode == 0)
338  {
339  return $langs->trans($this->statuts[$statut]);
340  }
341  elseif ($mode == 1)
342  {
343  return $langs->trans($this->statuts_short[$statut]);
344  }
345  elseif ($mode == 2)
346  {
347  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
348  if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
349  if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
350  }
351  elseif ($mode == 3)
352  {
353  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
354  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
355  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
356  }
357  elseif ($mode == 4)
358  {
359  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
360  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
361  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
362  }
363  elseif ($mode == 5)
364  {
365  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
366  if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
367  if ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
368  }
369  }
370 
377  function getNomUrl($withpicto=0)
378  {
379  global $langs;
380 
381  $result='';
382  $label=$langs->trans("Show").': '.$this->ref;
383 
384  $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
385  $linkend='</a>';
386 
387  $picto='trip';
388 
389 
390  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
391  if ($withpicto && $withpicto != 2) $result.=' ';
392  if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
393  return $result;
394  }
395 
396 
403  function listOfTypes($active=1)
404  {
405  global $langs;
406 
407  $ret=array();
408 
409  $sql = "SELECT id, code, label";
410  $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
411  $sql.= " WHERE active = ".$active;
412 
413  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
414  $result = $this->db->query($sql);
415  if ( $result )
416  {
417  $num = $this->db->num_rows($result);
418  $i=0;
419  while ($i < $num)
420  {
421  $obj = $this->db->fetch_object($result);
422  $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
423  $i++;
424  }
425  }
426  else
427  {
428  dol_print_error($this->db);
429  }
430 
431  return $ret;
432  }
433 
440  function info($id)
441  {
442  $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
443  $sql.= ' c.tms';
444  $sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
445  $sql.= ' WHERE c.rowid = '.$id;
446 
447  dol_syslog(get_class($this).'::info', LOG_DEBUG);
448  $result = $this->db->query($sql);
449 
450  if ($result)
451  {
452  if ($this->db->num_rows($result))
453  {
454  $obj = $this->db->fetch_object($result);
455  $this->id = $obj->rowid;
456  if ($obj->fk_user_author)
457  {
458  $cuser = new User($this->db);
459  $cuser->fetch($obj->fk_user_author);
460  $this->user_creation = $cuser;
461  }
462  if ($obj->fk_user_modif)
463  {
464  $muser = new User($this->db);
465  $muser->fetch($obj->fk_user_modif);
466  $this->user_modification = $muser;
467  }
468  $this->date_creation = $this->db->jdate($obj->datec);
469  $this->date_modification = $this->db->jdate($obj->tms);
470  }
471  $this->db->free($result);
472  }
473  else
474  {
475  dol_print_error($this->db);
476  }
477  }
478 }
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:41
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.
type
Definition: viewcat.php:284
dol_now($mode='gmt')
Return date for now.
fetch($id, $ref='')
Load an object from database.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
info($id)
Information on record.
update($user)
Update record.