dolibarr  13.0.2
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  * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29 
34 {
38  public $element = 'deplacement';
39 
43  public $table_element = 'deplacement';
44 
48  public $table_element_line = '';
49 
53  public $fk_element = '';
54 
59  public $ismultientitymanaged = 0;
60 
66  public $datec;
67 
73  public $dated;
74 
78  public $fk_user_author;
79 
83  public $fk_user;
84 
88  public $km;
89 
93  public $socid;
94 
98  public $statut;
99  public $extraparams = array();
100 
101  public $statuts = array();
102  public $statuts_short = array();
103 
107  const STATUS_DRAFT = 0;
108 
112  const STATUS_VALIDATED = 1;
113 
117  const STATUS_REFUNDED = 2;
118 
124  public function __construct($db)
125  {
126  $this->db = $db;
127 
128  $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
129  $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
130  }
131 
139  public function create($user)
140  {
141  global $conf;
142 
143  // Check parameters
144  if (empty($this->type) || $this->type < 0)
145  {
146  $this->error = 'ErrorBadParameter';
147  return -1;
148  }
149  if (empty($this->fk_user) || $this->fk_user < 0)
150  {
151  $this->error = 'ErrorBadParameter';
152  return -1;
153  }
154 
155  $now = dol_now();
156 
157  $this->db->begin();
158 
159  $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
160  $sql .= "datec";
161  //$sql.= ", dated";
162  $sql .= ", entity";
163  $sql .= ", fk_user_author";
164  $sql .= ", fk_user";
165  $sql .= ", type";
166  $sql .= ", note_private";
167  $sql .= ", note_public";
168  $sql .= ", fk_projet";
169  $sql .= ", fk_soc";
170  $sql .= ") VALUES (";
171  $sql .= " '".$this->db->idate($now)."'";
172  $sql .= ", ".$conf->entity;
173  $sql .= ", ".$user->id;
174  $sql .= ", ".$this->fk_user;
175  $sql .= ", '".$this->db->escape($this->type)."'";
176  $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
177  $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
178  $sql .= ", ".($this->fk_project > 0 ? $this->fk_project : 0);
179  $sql .= ", ".($this->fk_soc > 0 ? $this->fk_soc : "null");
180  $sql .= ")";
181 
182  dol_syslog(get_class($this)."::create", LOG_DEBUG);
183  $result = $this->db->query($sql);
184  if ($result)
185  {
186  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
187 
188  // Call trigger
189  $result = $this->call_trigger('DEPLACEMENT_CREATE', $user);
190  if ($result < 0)
191  {
192  $this->db->rollback();
193  return -2;
194  }
195  // End call triggers
196 
197  $result = $this->update($user);
198  if ($result > 0)
199  {
200  $this->db->commit();
201  return $this->id;
202  } else {
203  $this->error = $this->db->error();
204  $this->db->rollback();
205  return $result;
206  }
207  } else {
208  $this->error = $this->db->error()." sql=".$sql;
209  $this->db->rollback();
210  return -1;
211  }
212  }
213 
220  public function update($user)
221  {
222  global $langs;
223 
224  // Clean parameters
225  $this->km = price2num($this->km);
226 
227  // Check parameters
228  if (!is_numeric($this->km)) $this->km = 0;
229  if (empty($this->date))
230  {
231  $this->error = 'ErrorBadParameter';
232  return -1;
233  }
234  if (empty($this->type) || $this->type < 0)
235  {
236  $this->error = 'ErrorBadParameter';
237  return -1;
238  }
239  if (empty($this->fk_user) || $this->fk_user < 0)
240  {
241  $this->error = 'ErrorBadParameter';
242  return -1;
243  }
244 
245  $this->db->begin();
246 
247  $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
248  $sql .= " SET km = ".$this->km; // This is a distance or amount
249  $sql .= " , dated = '".$this->db->idate($this->date)."'";
250  $sql .= " , type = '".$this->db->escape($this->type)."'";
251  $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
252  $sql .= " , fk_user = ".$this->fk_user;
253  $sql .= " , fk_user_modif = ".$user->id;
254  $sql .= " , fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
255  $sql .= " , note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
256  $sql .= " , note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
257  $sql .= " , fk_projet = ".($this->fk_project > 0 ? $this->fk_project : 0);
258  $sql .= " WHERE rowid = ".$this->id;
259 
260  dol_syslog(get_class($this)."::update", LOG_DEBUG);
261  $result = $this->db->query($sql);
262  if ($result)
263  {
264  $this->db->commit();
265  return 1;
266  } else {
267  $this->error = $this->db->lasterror();
268  $this->db->rollback();
269  return -1;
270  }
271  }
272 
280  public function fetch($id, $ref = '')
281  {
282  $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
283  $sql .= " FROM ".MAIN_DB_PREFIX."deplacement";
284  $sql .= " WHERE entity IN (".getEntity('deplacement').")";
285  if ($ref) $sql .= " AND ref ='".$this->db->escape($ref)."'";
286  else $sql .= " AND rowid = ".$id;
287 
288  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
289  $result = $this->db->query($sql);
290  if ($result)
291  {
292  $obj = $this->db->fetch_object($result);
293 
294  $this->id = $obj->rowid;
295  $this->ref = $obj->rowid;
296  $this->date = $this->db->jdate($obj->dated);
297  $this->fk_user = $obj->fk_user;
298  $this->socid = $obj->fk_soc;
299  $this->km = $obj->km;
300  $this->type = $obj->type;
301  $this->statut = $obj->fk_statut;
302  $this->note_private = $obj->note_private;
303  $this->note_public = $obj->note_public;
304  $this->fk_project = $obj->fk_project;
305 
306  $this->extraparams = (array) json_decode($obj->extraparams, true);
307 
308  return 1;
309  } else {
310  $this->error = $this->db->error();
311  return -1;
312  }
313  }
314 
321  public function delete($id)
322  {
323  $this->db->begin();
324 
325  $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
326 
327  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
328  $result = $this->db->query($sql);
329  if ($result)
330  {
331  $this->db->commit();
332  return 1;
333  } else {
334  $this->error = $this->db->error();
335  $this->db->rollback();
336  return -1;
337  }
338  }
339 
340 
347  public function getLibStatut($mode = 0)
348  {
349  return $this->LibStatut($this->statut, $mode);
350  }
351 
352  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
360  public function LibStatut($status, $mode = 0)
361  {
362  // phpcs:enable
363  global $langs;
364 
365  if ($mode == 0)
366  {
367  return $langs->trans($this->statuts[$status]);
368  } elseif ($mode == 1)
369  {
370  return $langs->trans($this->statuts_short[$status]);
371  } elseif ($mode == 2)
372  {
373  if ($status == 0) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts_short[$status]);
374  elseif ($status == 1) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts_short[$status]);
375  elseif ($status == 2) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts_short[$status]);
376  } elseif ($mode == 3)
377  {
378  if ($status == 0 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0');
379  elseif ($status == 1 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
380  elseif ($status == 2 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
381  } elseif ($mode == 4)
382  {
383  if ($status == 0 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
384  elseif ($status == 1 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
385  elseif ($status == 2 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts[$status]);
386  } elseif ($mode == 5)
387  {
388  if ($status == 0 && !empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut0');
389  elseif ($status == 1 && !empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
390  elseif ($status == 2 && !empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
391  }
392  }
393 
400  public function getNomUrl($withpicto = 0)
401  {
402  global $langs;
403 
404  $result = '';
405  $label = $langs->trans("Show").': '.$this->ref;
406 
407  $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
408  $linkend = '</a>';
409 
410  $picto = 'trip';
411 
412 
413  if ($withpicto) $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
414  if ($withpicto && $withpicto != 2) $result .= ' ';
415  if ($withpicto != 2) $result .= $link.$this->ref.$linkend;
416  return $result;
417  }
418 
419 
426  public function listOfTypes($active = 1)
427  {
428  global $langs;
429 
430  $ret = array();
431 
432  $sql = "SELECT id, code, label";
433  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
434  $sql .= " WHERE active = ".$active;
435 
436  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
437  $result = $this->db->query($sql);
438  if ($result)
439  {
440  $num = $this->db->num_rows($result);
441  $i = 0;
442  while ($i < $num)
443  {
444  $obj = $this->db->fetch_object($result);
445  $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
446  $i++;
447  }
448  } else {
449  dol_print_error($this->db);
450  }
451 
452  return $ret;
453  }
454 
461  public function info($id)
462  {
463  $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
464  $sql .= ' c.tms';
465  $sql .= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
466  $sql .= ' WHERE c.rowid = '.$id;
467 
468  dol_syslog(get_class($this).'::info', LOG_DEBUG);
469  $result = $this->db->query($sql);
470 
471  if ($result)
472  {
473  if ($this->db->num_rows($result))
474  {
475  $obj = $this->db->fetch_object($result);
476  $this->id = $obj->rowid;
477  if ($obj->fk_user_author)
478  {
479  $cuser = new User($this->db);
480  $cuser->fetch($obj->fk_user_author);
481  $this->user_creation = $cuser;
482  }
483  if ($obj->fk_user_modif)
484  {
485  $muser = new User($this->db);
486  $muser->fetch($obj->fk_user_modif);
487  $this->user_modification = $muser;
488  }
489  $this->date_creation = $this->db->jdate($obj->datec);
490  $this->date_modification = $this->db->jdate($obj->tms);
491  }
492  $this->db->free($result);
493  } else {
494  dol_print_error($this->db);
495  }
496  }
497 }
const STATUS_DRAFT
Draft status.
Class to manage trips and working credit notes.
create($user)
Create object in database TODO Add ref number.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
const STATUS_REFUNDED
Refunded status.
const STATUS_VALIDATED
Validated status.
dol_now($mode= 'auto')
Return date for now.
listOfTypes($active=1)
List of types.
Class to manage Dolibarr users.
Definition: user.class.php:44
$conf db
API class for accounts.
Definition: inc.php:54
__construct($db)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
call_trigger($triggerName, $user)
Call trigger based on this instance.
fetch($id, $ref= '')
Load an object from database.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
getLibStatut($mode=0)
Retourne le libelle du statut.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
info($id)
Information on record.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
update($user)
Update record.