dolibarr 21.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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30
35{
39 public $element = 'deplacement';
40
44 public $table_element = 'deplacement';
45
49 public $table_element_line = '';
50
54 public $fk_element = '';
55
59 public $fk_soc;
63 public $date;
67 public $type;
68
74 public $datec;
75
81 public $dated;
82
86 public $fk_user_author;
87
91 public $fk_user;
92
96 public $km;
97
101 public $socid;
102
106 public $statut;
110 public $extraparams = array();
111
112
116 const STATUS_DRAFT = 0;
117
122
127
133 public function __construct(DoliDB $db)
134 {
135 $this->db = $db;
136
137 $this->ismultientitymanaged = 0;
138 }
139
147 public function create($user)
148 {
149 global $conf;
150
151 // Check parameters
152 if (empty($this->type) || $this->type < 0) {
153 $this->error = 'ErrorBadParameter';
154 return -1;
155 }
156 if (empty($this->fk_user) || $this->fk_user < 0) {
157 $this->error = 'ErrorBadParameter';
158 return -1;
159 }
160
161 $now = dol_now();
162
163 $this->db->begin();
164
165 $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
166 $sql .= "datec";
167 //$sql.= ", dated";
168 $sql .= ", entity";
169 $sql .= ", fk_user_author";
170 $sql .= ", fk_user";
171 $sql .= ", type";
172 $sql .= ", note_private";
173 $sql .= ", note_public";
174 $sql .= ", fk_projet";
175 $sql .= ", fk_soc";
176 $sql .= ") VALUES (";
177 $sql .= " '".$this->db->idate($now)."'";
178 $sql .= ", ".((int) $conf->entity);
179 $sql .= ", ".((int) $user->id);
180 $sql .= ", ".((int) $this->fk_user);
181 $sql .= ", '".$this->db->escape($this->type)."'";
182 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
183 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
184 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
185 $sql .= ", ".($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null");
186 $sql .= ")";
187
188 dol_syslog(get_class($this)."::create", LOG_DEBUG);
189 $result = $this->db->query($sql);
190 if ($result) {
191 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
192
193 // Call trigger
194 $result = $this->call_trigger('DEPLACEMENT_CREATE', $user);
195 if ($result < 0) {
196 $this->db->rollback();
197 return -2;
198 }
199 // End call triggers
200
201 $result = $this->update($user);
202 if ($result > 0) {
203 $this->db->commit();
204 return $this->id;
205 } else {
206 $this->error = $this->db->error();
207 $this->db->rollback();
208 return $result;
209 }
210 } else {
211 $this->error = $this->db->error()." sql=".$sql;
212 $this->db->rollback();
213 return -1;
214 }
215 }
216
223 public function update($user)
224 {
225 // Clean parameters
226 $this->km = (float) price2num($this->km);
227
228 // Check parameters
229 if (!is_numeric($this->km)) {
230 $this->km = 0;
231 }
232 if (empty($this->date)) {
233 $this->error = 'ErrorBadParameter';
234 return -1;
235 }
236 if (empty($this->type) || $this->type < 0) {
237 $this->error = 'ErrorBadParameter';
238 return -1;
239 }
240 if (empty($this->fk_user) || $this->fk_user < 0) {
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 = ".((float) $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 = ".((int) $this->fk_user);
253 $sql .= " , fk_user_modif = ".((int) $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 = ".((int) $this->id);
259
260 dol_syslog(get_class($this)."::update", LOG_DEBUG);
261 $result = $this->db->query($sql);
262 if ($result) {
263 $this->db->commit();
264 return 1;
265 } else {
266 $this->error = $this->db->lasterror();
267 $this->db->rollback();
268 return -1;
269 }
270 }
271
279 public function fetch($id, $ref = '')
280 {
281 $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
282 $sql .= " FROM ".MAIN_DB_PREFIX."deplacement";
283 $sql .= " WHERE entity IN (".getEntity('deplacement').")";
284 if ($ref) {
285 $sql .= " AND ref ='".$this->db->escape($ref)."'";
286 } else {
287 $sql .= " AND rowid = ".((int) $id);
288 }
289
290 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
291 $result = $this->db->query($sql);
292 if ($result) {
293 $obj = $this->db->fetch_object($result);
294
295 $this->id = $obj->rowid;
296 $this->ref = $obj->rowid;
297 $this->date = $this->db->jdate($obj->dated);
298 $this->fk_user = $obj->fk_user;
299 $this->socid = $obj->fk_soc;
300 $this->km = $obj->km;
301 $this->type = $obj->type;
302 $this->statut = $obj->fk_statut;
303 $this->note_private = $obj->note_private;
304 $this->note_public = $obj->note_public;
305 $this->fk_project = $obj->fk_project;
306
307 $this->extraparams = (array) json_decode($obj->extraparams, true);
308
309 return 1;
310 } else {
311 $this->error = $this->db->error();
312 return -1;
313 }
314 }
315
322 public function delete($user)
323 {
324 $this->db->begin();
325
326 $id = $this->id;
327
328 $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".((int) $id);
329
330 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
331 $result = $this->db->query($sql);
332 if ($result) {
333 $this->db->commit();
334 return 1;
335 } else {
336 $this->error = $this->db->error();
337 $this->db->rollback();
338 return -1;
339 }
340 }
341
342
349 public function getLibStatut($mode = 0)
350 {
351 return $this->LibStatut($this->statut, $mode);
352 }
353
354 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
362 public function LibStatut($status, $mode = 0)
363 {
364 // phpcs:enable
365 global $langs;
366
367 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
368 global $langs;
369 //$langs->load("mymodule@mymodule");
370 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
371 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
372 $this->labelStatus[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
373 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
374 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
375 $this->labelStatusShort[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
376 }
377
378 $status_logo = array(0 => 'status0', 1 => 'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
379 $statusType = $status_logo[$status];
380
381 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
382 }
383
390 public function getNomUrl($withpicto = 0)
391 {
392 global $langs;
393
394 $result = '';
395 $label = $langs->trans("Show").': '.$this->ref;
396
397 $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
398 $linkend = '</a>';
399
400 $picto = 'trip';
401
402
403 if ($withpicto) {
404 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
405 }
406 if ($withpicto && $withpicto != 2) {
407 $result .= ' ';
408 }
409 if ($withpicto != 2) {
410 $result .= $link.$this->ref.$linkend;
411 }
412 return $result;
413 }
414
415
422 public function listOfTypes($active = 1)
423 {
424 global $langs;
425
426 $ret = array();
427
428 $sql = "SELECT id, code, label";
429 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
430 $sql .= " WHERE active = ".((int) $active);
431
432 dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
433 $result = $this->db->query($sql);
434 if ($result) {
435 $num = $this->db->num_rows($result);
436 $i = 0;
437 while ($i < $num) {
438 $obj = $this->db->fetch_object($result);
439 $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
440 $i++;
441 }
442 } else {
443 dol_print_error($this->db);
444 }
445
446 return $ret;
447 }
448
455 public function info($id)
456 {
457 $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
458 $sql .= ' c.tms as datem';
459 $sql .= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
460 $sql .= ' WHERE c.rowid = '.((int) $id);
461
462 dol_syslog(get_class($this).'::info', LOG_DEBUG);
463 $result = $this->db->query($sql);
464
465 if ($result) {
466 if ($this->db->num_rows($result)) {
467 $obj = $this->db->fetch_object($result);
468
469 $this->id = $obj->rowid;
470
471 $this->user_creation_id = $obj->fk_user_author;
472 $this->user_modification_id = $obj->fk_user_modif;
473 $this->date_creation = $this->db->jdate($obj->datec);
474 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
475 }
476 $this->db->free($result);
477 } else {
478 dol_print_error($this->db);
479 }
480 }
481}
$object ref
Definition info.php:89
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 clickable 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.
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149