dolibarr 19.0.4
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
28require_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
61
62 public $fk_soc;
63 public $date;
64 public $type;
65
71 public $datec;
72
78 public $dated;
79
83 public $fk_user_author;
84
88 public $fk_user;
89
93 public $km;
94
98 public $socid;
99
103 public $statut;
104 public $extraparams = array();
105
106
110 const STATUS_DRAFT = 0;
111
116
121
127 public function __construct(DoliDB $db)
128 {
129 $this->db = $db;
130 }
131
139 public function create($user)
140 {
141 global $conf;
142
143 // Check parameters
144 if (empty($this->type) || $this->type < 0) {
145 $this->error = 'ErrorBadParameter';
146 return -1;
147 }
148 if (empty($this->fk_user) || $this->fk_user < 0) {
149 $this->error = 'ErrorBadParameter';
150 return -1;
151 }
152
153 $now = dol_now();
154
155 $this->db->begin();
156
157 $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
158 $sql .= "datec";
159 //$sql.= ", dated";
160 $sql .= ", entity";
161 $sql .= ", fk_user_author";
162 $sql .= ", fk_user";
163 $sql .= ", type";
164 $sql .= ", note_private";
165 $sql .= ", note_public";
166 $sql .= ", fk_projet";
167 $sql .= ", fk_soc";
168 $sql .= ") VALUES (";
169 $sql .= " '".$this->db->idate($now)."'";
170 $sql .= ", ".((int) $conf->entity);
171 $sql .= ", ".((int) $user->id);
172 $sql .= ", ".((int) $this->fk_user);
173 $sql .= ", '".$this->db->escape($this->type)."'";
174 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
175 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
176 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
177 $sql .= ", ".($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null");
178 $sql .= ")";
179
180 dol_syslog(get_class($this)."::create", LOG_DEBUG);
181 $result = $this->db->query($sql);
182 if ($result) {
183 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
184
185 // Call trigger
186 $result = $this->call_trigger('DEPLACEMENT_CREATE', $user);
187 if ($result < 0) {
188 $this->db->rollback();
189 return -2;
190 }
191 // End call triggers
192
193 $result = $this->update($user);
194 if ($result > 0) {
195 $this->db->commit();
196 return $this->id;
197 } else {
198 $this->error = $this->db->error();
199 $this->db->rollback();
200 return $result;
201 }
202 } else {
203 $this->error = $this->db->error()." sql=".$sql;
204 $this->db->rollback();
205 return -1;
206 }
207 }
208
215 public function update($user)
216 {
217 // Clean parameters
218 $this->km = price2num($this->km);
219
220 // Check parameters
221 if (!is_numeric($this->km)) {
222 $this->km = 0;
223 }
224 if (empty($this->date)) {
225 $this->error = 'ErrorBadParameter';
226 return -1;
227 }
228 if (empty($this->type) || $this->type < 0) {
229 $this->error = 'ErrorBadParameter';
230 return -1;
231 }
232 if (empty($this->fk_user) || $this->fk_user < 0) {
233 $this->error = 'ErrorBadParameter';
234 return -1;
235 }
236
237 $this->db->begin();
238
239 $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
240 $sql .= " SET km = ".((float) $this->km); // This is a distance or amount
241 $sql .= " , dated = '".$this->db->idate($this->date)."'";
242 $sql .= " , type = '".$this->db->escape($this->type)."'";
243 $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
244 $sql .= " , fk_user = ".((int) $this->fk_user);
245 $sql .= " , fk_user_modif = ".((int) $user->id);
246 $sql .= " , fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
247 $sql .= " , note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
248 $sql .= " , note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
249 $sql .= " , fk_projet = ".($this->fk_project > 0 ? $this->fk_project : 0);
250 $sql .= " WHERE rowid = ".((int) $this->id);
251
252 dol_syslog(get_class($this)."::update", LOG_DEBUG);
253 $result = $this->db->query($sql);
254 if ($result) {
255 $this->db->commit();
256 return 1;
257 } else {
258 $this->error = $this->db->lasterror();
259 $this->db->rollback();
260 return -1;
261 }
262 }
263
271 public function fetch($id, $ref = '')
272 {
273 $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
274 $sql .= " FROM ".MAIN_DB_PREFIX."deplacement";
275 $sql .= " WHERE entity IN (".getEntity('deplacement').")";
276 if ($ref) {
277 $sql .= " AND ref ='".$this->db->escape($ref)."'";
278 } else {
279 $sql .= " AND rowid = ".((int) $id);
280 }
281
282 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
283 $result = $this->db->query($sql);
284 if ($result) {
285 $obj = $this->db->fetch_object($result);
286
287 $this->id = $obj->rowid;
288 $this->ref = $obj->rowid;
289 $this->date = $this->db->jdate($obj->dated);
290 $this->fk_user = $obj->fk_user;
291 $this->socid = $obj->fk_soc;
292 $this->km = $obj->km;
293 $this->type = $obj->type;
294 $this->statut = $obj->fk_statut;
295 $this->note_private = $obj->note_private;
296 $this->note_public = $obj->note_public;
297 $this->fk_project = $obj->fk_project;
298
299 $this->extraparams = (array) json_decode($obj->extraparams, true);
300
301 return 1;
302 } else {
303 $this->error = $this->db->error();
304 return -1;
305 }
306 }
307
314 public function delete($user)
315 {
316 $this->db->begin();
317
318 $id = $this->id;
319
320 $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".((int) $id);
321
322 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
323 $result = $this->db->query($sql);
324 if ($result) {
325 $this->db->commit();
326 return 1;
327 } else {
328 $this->error = $this->db->error();
329 $this->db->rollback();
330 return -1;
331 }
332 }
333
334
341 public function getLibStatut($mode = 0)
342 {
343 return $this->LibStatut($this->statut, $mode);
344 }
345
346 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
354 public function LibStatut($status, $mode = 0)
355 {
356 // phpcs:enable
357 global $langs;
358
359 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
360 global $langs;
361 //$langs->load("mymodule@mymodule");
362 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
363 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
364 $this->labelStatus[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
365 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
366 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
367 $this->labelStatusShort[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
368 }
369
370 $status_logo = array(0 => 'status0', 1=>'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
371 $statusType = $status_logo[$status];
372
373 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
374 }
375
382 public function getNomUrl($withpicto = 0)
383 {
384 global $langs;
385
386 $result = '';
387 $label = $langs->trans("Show").': '.$this->ref;
388
389 $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
390 $linkend = '</a>';
391
392 $picto = 'trip';
393
394
395 if ($withpicto) {
396 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
397 }
398 if ($withpicto && $withpicto != 2) {
399 $result .= ' ';
400 }
401 if ($withpicto != 2) {
402 $result .= $link.$this->ref.$linkend;
403 }
404 return $result;
405 }
406
407
414 public function listOfTypes($active = 1)
415 {
416 global $langs;
417
418 $ret = array();
419
420 $sql = "SELECT id, code, label";
421 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
422 $sql .= " WHERE active = ".((int) $active);
423
424 dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
425 $result = $this->db->query($sql);
426 if ($result) {
427 $num = $this->db->num_rows($result);
428 $i = 0;
429 while ($i < $num) {
430 $obj = $this->db->fetch_object($result);
431 $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
432 $i++;
433 }
434 } else {
435 dol_print_error($this->db);
436 }
437
438 return $ret;
439 }
440
447 public function info($id)
448 {
449 $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
450 $sql .= ' c.tms as datem';
451 $sql .= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
452 $sql .= ' WHERE c.rowid = '.((int) $id);
453
454 dol_syslog(get_class($this).'::info', LOG_DEBUG);
455 $result = $this->db->query($sql);
456
457 if ($result) {
458 if ($this->db->num_rows($result)) {
459 $obj = $this->db->fetch_object($result);
460
461 $this->id = $obj->rowid;
462
463 $this->user_creation_id = $obj->fk_user_author;
464 $this->user_modification_id = $obj->fk_user_modif;
465 $this->date_creation = $this->db->jdate($obj->datec);
466 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
467 }
468 $this->db->free($result);
469 } else {
470 dol_print_error($this->db);
471 }
472 }
473}
$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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:121