dolibarr  17.0.3
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  public $statuts_logo = array();
104 
105 
109  const STATUS_DRAFT = 0;
110 
114  const STATUS_VALIDATED = 1;
115 
119  const STATUS_REFUNDED = 2;
120 
126  public function __construct(DoliDB $db)
127  {
128  $this->db = $db;
129 
130  $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
131  $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
132  $this->statuts_logo = array(0 => 'status0', 1=>'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
133  }
134 
142  public function create($user)
143  {
144  global $conf;
145 
146  // Check parameters
147  if (empty($this->type) || $this->type < 0) {
148  $this->error = 'ErrorBadParameter';
149  return -1;
150  }
151  if (empty($this->fk_user) || $this->fk_user < 0) {
152  $this->error = 'ErrorBadParameter';
153  return -1;
154  }
155 
156  $now = dol_now();
157 
158  $this->db->begin();
159 
160  $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
161  $sql .= "datec";
162  //$sql.= ", dated";
163  $sql .= ", entity";
164  $sql .= ", fk_user_author";
165  $sql .= ", fk_user";
166  $sql .= ", type";
167  $sql .= ", note_private";
168  $sql .= ", note_public";
169  $sql .= ", fk_projet";
170  $sql .= ", fk_soc";
171  $sql .= ") VALUES (";
172  $sql .= " '".$this->db->idate($now)."'";
173  $sql .= ", ".((int) $conf->entity);
174  $sql .= ", ".((int) $user->id);
175  $sql .= ", ".((int) $this->fk_user);
176  $sql .= ", '".$this->db->escape($this->type)."'";
177  $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
178  $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
179  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
180  $sql .= ", ".($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null");
181  $sql .= ")";
182 
183  dol_syslog(get_class($this)."::create", LOG_DEBUG);
184  $result = $this->db->query($sql);
185  if ($result) {
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  $this->db->rollback();
192  return -2;
193  }
194  // End call triggers
195 
196  $result = $this->update($user);
197  if ($result > 0) {
198  $this->db->commit();
199  return $this->id;
200  } else {
201  $this->error = $this->db->error();
202  $this->db->rollback();
203  return $result;
204  }
205  } else {
206  $this->error = $this->db->error()." sql=".$sql;
207  $this->db->rollback();
208  return -1;
209  }
210  }
211 
218  public function update($user)
219  {
220  global $langs;
221 
222  // Clean parameters
223  $this->km = price2num($this->km);
224 
225  // Check parameters
226  if (!is_numeric($this->km)) {
227  $this->km = 0;
228  }
229  if (empty($this->date)) {
230  $this->error = 'ErrorBadParameter';
231  return -1;
232  }
233  if (empty($this->type) || $this->type < 0) {
234  $this->error = 'ErrorBadParameter';
235  return -1;
236  }
237  if (empty($this->fk_user) || $this->fk_user < 0) {
238  $this->error = 'ErrorBadParameter';
239  return -1;
240  }
241 
242  $this->db->begin();
243 
244  $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
245  $sql .= " SET km = ".((float) $this->km); // This is a distance or amount
246  $sql .= " , dated = '".$this->db->idate($this->date)."'";
247  $sql .= " , type = '".$this->db->escape($this->type)."'";
248  $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
249  $sql .= " , fk_user = ".((int) $this->fk_user);
250  $sql .= " , fk_user_modif = ".((int) $user->id);
251  $sql .= " , fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
252  $sql .= " , note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
253  $sql .= " , note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
254  $sql .= " , fk_projet = ".($this->fk_project > 0 ? $this->fk_project : 0);
255  $sql .= " WHERE rowid = ".((int) $this->id);
256 
257  dol_syslog(get_class($this)."::update", LOG_DEBUG);
258  $result = $this->db->query($sql);
259  if ($result) {
260  $this->db->commit();
261  return 1;
262  } else {
263  $this->error = $this->db->lasterror();
264  $this->db->rollback();
265  return -1;
266  }
267  }
268 
276  public function fetch($id, $ref = '')
277  {
278  $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
279  $sql .= " FROM ".MAIN_DB_PREFIX."deplacement";
280  $sql .= " WHERE entity IN (".getEntity('deplacement').")";
281  if ($ref) {
282  $sql .= " AND ref ='".$this->db->escape($ref)."'";
283  } else {
284  $sql .= " AND rowid = ".((int) $id);
285  }
286 
287  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
288  $result = $this->db->query($sql);
289  if ($result) {
290  $obj = $this->db->fetch_object($result);
291 
292  $this->id = $obj->rowid;
293  $this->ref = $obj->rowid;
294  $this->date = $this->db->jdate($obj->dated);
295  $this->fk_user = $obj->fk_user;
296  $this->socid = $obj->fk_soc;
297  $this->km = $obj->km;
298  $this->type = $obj->type;
299  $this->statut = $obj->fk_statut;
300  $this->note_private = $obj->note_private;
301  $this->note_public = $obj->note_public;
302  $this->fk_project = $obj->fk_project;
303 
304  $this->extraparams = (array) json_decode($obj->extraparams, true);
305 
306  return 1;
307  } else {
308  $this->error = $this->db->error();
309  return -1;
310  }
311  }
312 
319  public function delete($user)
320  {
321  $this->db->begin();
322 
323  $id = $this->id;
324 
325  $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".((int) $id);
326 
327  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
328  $result = $this->db->query($sql);
329  if ($result) {
330  $this->db->commit();
331  return 1;
332  } else {
333  $this->error = $this->db->error();
334  $this->db->rollback();
335  return -1;
336  }
337  }
338 
339 
346  public function getLibStatut($mode = 0)
347  {
348  return $this->LibStatut($this->statut, $mode);
349  }
350 
351  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
359  public function LibStatut($status, $mode = 0)
360  {
361  // phpcs:enable
362  global $langs;
363 
364  $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
365  $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
366 
367  $statusType = $this->statuts_logo[$status];
368 
369  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
370  }
371 
378  public function getNomUrl($withpicto = 0)
379  {
380  global $langs;
381 
382  $result = '';
383  $label = $langs->trans("Show").': '.$this->ref;
384 
385  $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
386  $linkend = '</a>';
387 
388  $picto = 'trip';
389 
390 
391  if ($withpicto) {
392  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
393  }
394  if ($withpicto && $withpicto != 2) {
395  $result .= ' ';
396  }
397  if ($withpicto != 2) {
398  $result .= $link.$this->ref.$linkend;
399  }
400  return $result;
401  }
402 
403 
410  public function listOfTypes($active = 1)
411  {
412  global $langs;
413 
414  $ret = array();
415 
416  $sql = "SELECT id, code, label";
417  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
418  $sql .= " WHERE active = ".((int) $active);
419 
420  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
421  $result = $this->db->query($sql);
422  if ($result) {
423  $num = $this->db->num_rows($result);
424  $i = 0;
425  while ($i < $num) {
426  $obj = $this->db->fetch_object($result);
427  $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
428  $i++;
429  }
430  } else {
431  dol_print_error($this->db);
432  }
433 
434  return $ret;
435  }
436 
443  public function info($id)
444  {
445  $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
446  $sql .= ' c.tms as datem';
447  $sql .= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
448  $sql .= ' WHERE c.rowid = '.((int) $id);
449 
450  dol_syslog(get_class($this).'::info', LOG_DEBUG);
451  $result = $this->db->query($sql);
452 
453  if ($result) {
454  if ($this->db->num_rows($result)) {
455  $obj = $this->db->fetch_object($result);
456  $this->id = $obj->rowid;
457 
458  $this->user_creation_id = $obj->fk_user_author;
459  $this->user_modification_id = $obj->fk_user_modif;
460  $this->date_creation = $this->db->jdate($obj->datec);
461  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
462  }
463  $this->db->free($result);
464  } else {
465  dol_print_error($this->db);
466  }
467  }
468 }
Deplacement\getLibStatut
getLibStatut($mode=0)
Retourne le libelle du statut.
Definition: deplacement.class.php:346
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1493
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Deplacement\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: deplacement.class.php:109
Deplacement\create
create($user)
Create object in database TODO Add ref number.
Definition: deplacement.class.php:142
Deplacement\listOfTypes
listOfTypes($active=1)
List of types.
Definition: deplacement.class.php:410
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
Deplacement\STATUS_REFUNDED
const STATUS_REFUNDED
Refunded status.
Definition: deplacement.class.php:119
ref
$object ref
Definition: info.php:78
Deplacement\__construct
__construct(DoliDB $db)
Constructor.
Definition: deplacement.class.php:126
Deplacement\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: deplacement.class.php:114
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
Deplacement\info
info($id)
Information on record.
Definition: deplacement.class.php:443
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
Deplacement\fetch
fetch($id, $ref='')
Load an object from database.
Definition: deplacement.class.php:276
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10740
Deplacement
Class to manage trips and working credit notes.
Definition: deplacement.class.php:33
Deplacement\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: deplacement.class.php:359
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
Deplacement\getNomUrl
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
Definition: deplacement.class.php:378
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5806
Deplacement\update
update($user)
Update record.
Definition: deplacement.class.php:218
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119