dolibarr  20.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@inodbox.com>
5  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6  * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.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 
55  public $fk_soc;
56  public $date;
57  public $type;
58 
64  public $datec;
65 
71  public $dated;
72 
76  public $fk_user_author;
77 
81  public $fk_user;
82 
86  public $km;
87 
91  public $socid;
92 
96  public $statut;
97  public $extraparams = array();
98 
99 
103  const STATUS_DRAFT = 0;
104 
108  const STATUS_VALIDATED = 1;
109 
113  const STATUS_REFUNDED = 2;
114 
120  public function __construct(DoliDB $db)
121  {
122  $this->db = $db;
123 
124  $this->ismultientitymanaged = 0;
125  }
126 
134  public function create($user)
135  {
136  global $conf;
137 
138  // Check parameters
139  if (empty($this->type) || $this->type < 0) {
140  $this->error = 'ErrorBadParameter';
141  return -1;
142  }
143  if (empty($this->fk_user) || $this->fk_user < 0) {
144  $this->error = 'ErrorBadParameter';
145  return -1;
146  }
147 
148  $now = dol_now();
149 
150  $this->db->begin();
151 
152  $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
153  $sql .= "datec";
154  //$sql.= ", dated";
155  $sql .= ", entity";
156  $sql .= ", fk_user_author";
157  $sql .= ", fk_user";
158  $sql .= ", type";
159  $sql .= ", note_private";
160  $sql .= ", note_public";
161  $sql .= ", fk_projet";
162  $sql .= ", fk_soc";
163  $sql .= ") VALUES (";
164  $sql .= " '".$this->db->idate($now)."'";
165  $sql .= ", ".((int) $conf->entity);
166  $sql .= ", ".((int) $user->id);
167  $sql .= ", ".((int) $this->fk_user);
168  $sql .= ", '".$this->db->escape($this->type)."'";
169  $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
170  $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
171  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
172  $sql .= ", ".($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null");
173  $sql .= ")";
174 
175  dol_syslog(get_class($this)."::create", LOG_DEBUG);
176  $result = $this->db->query($sql);
177  if ($result) {
178  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
179 
180  // Call trigger
181  $result = $this->call_trigger('DEPLACEMENT_CREATE', $user);
182  if ($result < 0) {
183  $this->db->rollback();
184  return -2;
185  }
186  // End call triggers
187 
188  $result = $this->update($user);
189  if ($result > 0) {
190  $this->db->commit();
191  return $this->id;
192  } else {
193  $this->error = $this->db->error();
194  $this->db->rollback();
195  return $result;
196  }
197  } else {
198  $this->error = $this->db->error()." sql=".$sql;
199  $this->db->rollback();
200  return -1;
201  }
202  }
203 
210  public function update($user)
211  {
212  // Clean parameters
213  $this->km = (float) price2num($this->km);
214 
215  // Check parameters
216  if (!is_numeric($this->km)) {
217  $this->km = 0;
218  }
219  if (empty($this->date)) {
220  $this->error = 'ErrorBadParameter';
221  return -1;
222  }
223  if (empty($this->type) || $this->type < 0) {
224  $this->error = 'ErrorBadParameter';
225  return -1;
226  }
227  if (empty($this->fk_user) || $this->fk_user < 0) {
228  $this->error = 'ErrorBadParameter';
229  return -1;
230  }
231 
232  $this->db->begin();
233 
234  $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
235  $sql .= " SET km = ".((float) $this->km); // This is a distance or amount
236  $sql .= " , dated = '".$this->db->idate($this->date)."'";
237  $sql .= " , type = '".$this->db->escape($this->type)."'";
238  $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
239  $sql .= " , fk_user = ".((int) $this->fk_user);
240  $sql .= " , fk_user_modif = ".((int) $user->id);
241  $sql .= " , fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
242  $sql .= " , note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
243  $sql .= " , note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
244  $sql .= " , fk_projet = ".($this->fk_project > 0 ? $this->fk_project : 0);
245  $sql .= " WHERE rowid = ".((int) $this->id);
246 
247  dol_syslog(get_class($this)."::update", LOG_DEBUG);
248  $result = $this->db->query($sql);
249  if ($result) {
250  $this->db->commit();
251  return 1;
252  } else {
253  $this->error = $this->db->lasterror();
254  $this->db->rollback();
255  return -1;
256  }
257  }
258 
266  public function fetch($id, $ref = '')
267  {
268  $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
269  $sql .= " FROM ".MAIN_DB_PREFIX."deplacement";
270  $sql .= " WHERE entity IN (".getEntity('deplacement').")";
271  if ($ref) {
272  $sql .= " AND ref ='".$this->db->escape($ref)."'";
273  } else {
274  $sql .= " AND rowid = ".((int) $id);
275  }
276 
277  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
278  $result = $this->db->query($sql);
279  if ($result) {
280  $obj = $this->db->fetch_object($result);
281 
282  $this->id = $obj->rowid;
283  $this->ref = $obj->rowid;
284  $this->date = $this->db->jdate($obj->dated);
285  $this->fk_user = $obj->fk_user;
286  $this->socid = $obj->fk_soc;
287  $this->km = $obj->km;
288  $this->type = $obj->type;
289  $this->statut = $obj->fk_statut;
290  $this->note_private = $obj->note_private;
291  $this->note_public = $obj->note_public;
292  $this->fk_project = $obj->fk_project;
293 
294  $this->extraparams = (array) json_decode($obj->extraparams, true);
295 
296  return 1;
297  } else {
298  $this->error = $this->db->error();
299  return -1;
300  }
301  }
302 
309  public function delete($user)
310  {
311  $this->db->begin();
312 
313  $id = $this->id;
314 
315  $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".((int) $id);
316 
317  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
318  $result = $this->db->query($sql);
319  if ($result) {
320  $this->db->commit();
321  return 1;
322  } else {
323  $this->error = $this->db->error();
324  $this->db->rollback();
325  return -1;
326  }
327  }
328 
329 
336  public function getLibStatut($mode = 0)
337  {
338  return $this->LibStatut($this->statut, $mode);
339  }
340 
341  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
349  public function LibStatut($status, $mode = 0)
350  {
351  // phpcs:enable
352  global $langs;
353 
354  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
355  global $langs;
356  //$langs->load("mymodule@mymodule");
357  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
358  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
359  $this->labelStatus[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
360  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
361  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
362  $this->labelStatusShort[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
363  }
364 
365  $status_logo = array(0 => 'status0', 1=>'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
366  $statusType = $status_logo[$status];
367 
368  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
369  }
370 
377  public 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) {
391  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
392  }
393  if ($withpicto && $withpicto != 2) {
394  $result .= ' ';
395  }
396  if ($withpicto != 2) {
397  $result .= $link.$this->ref.$linkend;
398  }
399  return $result;
400  }
401 
402 
409  public function listOfTypes($active = 1)
410  {
411  global $langs;
412 
413  $ret = array();
414 
415  $sql = "SELECT id, code, label";
416  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
417  $sql .= " WHERE active = ".((int) $active);
418 
419  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
420  $result = $this->db->query($sql);
421  if ($result) {
422  $num = $this->db->num_rows($result);
423  $i = 0;
424  while ($i < $num) {
425  $obj = $this->db->fetch_object($result);
426  $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
427  $i++;
428  }
429  } else {
430  dol_print_error($this->db);
431  }
432 
433  return $ret;
434  }
435 
442  public function info($id)
443  {
444  $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
445  $sql .= ' c.tms as datem';
446  $sql .= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
447  $sql .= ' WHERE c.rowid = '.((int) $id);
448 
449  dol_syslog(get_class($this).'::info', LOG_DEBUG);
450  $result = $this->db->query($sql);
451 
452  if ($result) {
453  if ($this->db->num_rows($result)) {
454  $obj = $this->db->fetch_object($result);
455 
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 }
$object ref
Definition: info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage trips and working credit notes.
listOfTypes($active=1)
List of types.
const STATUS_DRAFT
Draft status.
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
info($id)
Information on record.
update($user)
Update record.
create($user)
Create object in database TODO Add ref number.
const STATUS_REFUNDED
Refunded status.
fetch($id, $ref='')
Load an object from database.
__construct(DoliDB $db)
Constructor.
LibStatut($status, $mode=0)
Return the label of a given status.
getLibStatut($mode=0)
Return the label of the status.
const STATUS_VALIDATED
Validated status.
Class to manage Dolibarr database access.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
div float
Buy price without taxes.
Definition: style.css.php:960
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123