dolibarr 22.0.5
fichinterligne.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2011-2020 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
7 * Copyright (C) 2015-2020 Charlene Benke <charlie@patas-monkey.com>
8 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
9 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
11 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
33
38{
42 public $db;
43
47 public $error = '';
48
52 public $fk_fichinter;
53
57 public $desc;
58
62 public $date;
67 public $datei;
68
72 public $duration;
73
77 public $rang = 0;
78
82 public $tva_tx;
83
88 public $subprice;
89
93 public $element = 'fichinterdet';
94
98 public $table_element = 'fichinterdet';
99
103 public $fk_element = 'fk_fichinter';
104
105
106
112 public function __construct($db)
113 {
114 $this->db = $db;
115 }
116
123 public function fetch($rowid)
124 {
125 dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
126
127 $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date, ft.extraparams';
128 $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
129 $sql .= ' WHERE ft.rowid = '.((int) $rowid);
130
131 $resql = $this->db->query($sql);
132 if ($resql) {
133 $objp = $this->db->fetch_object($resql);
134 $this->rowid = $objp->rowid;
135 $this->id = $objp->rowid;
136 $this->fk_fichinter = $objp->fk_fichinter;
137 $this->date = $this->db->jdate($objp->date);
138 $this->datei = $this->db->jdate($objp->date); // For backward compatibility
139 $this->desc = $objp->description;
140 $this->duration = $objp->duree;
141 $this->rang = $objp->rang;
142
143 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
144
145 $this->db->free($resql);
146
147 $this->fetch_optionals();
148
149 return 1;
150 } else {
151 $this->error = $this->db->error().' sql='.$sql;
152 return -1;
153 }
154 }
155
163 public function insert($user, $notrigger = 0)
164 {
165 $error = 0;
166
167 dol_syslog("FichinterLigne::insert rang=".$this->rang);
168
169 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
170 $this->date = $this->datei;
171 }
172
173 $this->db->begin();
174
175 $rangToUse = $this->rang;
176 if ($rangToUse == -1) {
177 // Recupere rang max de la ligne d'intervention dans $rangmax
178 $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
179 $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
180 $resql = $this->db->query($sql);
181 if ($resql) {
182 $obj = $this->db->fetch_object($resql);
183 $rangToUse = $obj->max + 1;
184 } else {
185 dol_print_error($this->db);
186 $this->db->rollback();
187 return -1;
188 }
189 }
190
191 // Insertion dans base de la ligne
192 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
193 $sql .= ' (fk_fichinter, description, date, duree, rang)';
194 $sql .= " VALUES (".((int) $this->fk_fichinter).",";
195 $sql .= " '".$this->db->escape($this->desc)."',";
196 $sql .= " '".$this->db->idate($this->date)."',";
197 $sql .= " ".((int) $this->duration).",";
198 $sql .= ' '.((int) $rangToUse);
199 $sql .= ')';
200
201 dol_syslog("FichinterLigne::insert", LOG_DEBUG);
202 $resql = $this->db->query($sql);
203 if ($resql) {
204 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
205 $this->rowid = $this->id;
206
207 if (!$error) {
208 $result = $this->insertExtraFields();
209 if ($result < 0) {
210 $error++;
211 }
212 }
213
214
215 $result = $this->update_total();
216
217 if ($result > 0) {
218 $this->rang = $rangToUse;
219
220 if (!$notrigger) {
221 // Call trigger
222 $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
223 if ($result < 0) {
224 $error++;
225 }
226 // End call triggers
227 }
228 }
229
230 if (!$error) {
231 $this->db->commit();
232 return $result;
233 } else {
234 $this->db->rollback();
235 return -1;
236 }
237 } else {
238 $this->error = $this->db->error()." sql=".$sql;
239 $this->db->rollback();
240 return -1;
241 }
242 }
243
244
252 public function update($user, $notrigger = 0)
253 {
254 $error = 0;
255
256 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
257 $this->date = $this->datei;
258 }
259
260 $this->db->begin();
261
262 // Mise a jour ligne en base
263 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
264 $sql .= " description = '".$this->db->escape($this->desc)."',";
265 $sql .= " date = '".$this->db->idate($this->date)."',";
266 $sql .= " duree = ".((int) $this->duration).",";
267 $sql .= " rang = ".((int) $this->rang);
268 $sql .= " WHERE rowid = ".((int) $this->id);
269
270 dol_syslog("FichinterLigne::update", LOG_DEBUG);
271 $resql = $this->db->query($sql);
272 if ($resql) {
273 if (!$error) {
274 $result = $this->insertExtraFields();
275 if ($result < 0) {
276 $error++;
277 }
278 }
279
280 $result = $this->update_total();
281 if ($result > 0) {
282 if (!$notrigger) {
283 // Call trigger
284 $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
285 if ($result < 0) {
286 $error++;
287 }
288 // End call triggers
289 }
290 }
291
292 if (!$error) {
293 $this->db->commit();
294 return $result;
295 } else {
296 $this->error = $this->db->lasterror();
297 $this->db->rollback();
298 return -1;
299 }
300 } else {
301 $this->error = $this->db->lasterror();
302 $this->db->rollback();
303 return -1;
304 }
305 }
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
313 public function update_total()
314 {
315 // phpcs:enable
316 global $conf;
317
318 $this->db->begin();
319
320 $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
321 $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
322 $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
323
324 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
325 $resql = $this->db->query($sql);
326 if ($resql) {
327 $obj = $this->db->fetch_object($resql);
328 $total_duration = 0;
329 if (!empty($obj->total_duration)) {
330 $total_duration = $obj->total_duration;
331 }
332
333 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
334 $sql .= " SET duree = ".((int) $total_duration);
335 $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
336 $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
337 $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
338
339 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
340 $resql = $this->db->query($sql);
341 if ($resql) {
342 $this->db->commit();
343 return 1;
344 } else {
345 $this->error = $this->db->error();
346 $this->db->rollback();
347 return -2;
348 }
349 } else {
350 $this->error = $this->db->error();
351 $this->db->rollback();
352 return -1;
353 }
354 }
355
363 public function deleteLine($user, $notrigger = 0)
364 {
365 $error = 0;
366
367 dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
368
369 $this->db->begin();
370
371 $result = $this->deleteExtraFields();
372 if ($result < 0) {
373 $error++;
374 $this->db->rollback();
375 return -1;
376 }
377
378 $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
379 $resql = $this->db->query($sql);
380
381 if ($resql) {
382 $result = $this->update_total();
383 if ($result > 0) {
384 if (!$notrigger) {
385 // Call trigger
386 $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
387 if ($result < 0) {
388 $error++;
389 $this->db->rollback();
390 return -1;
391 }
392 // End call triggers
393 }
394
395 $this->db->commit();
396 return $result;
397 } else {
398 $this->db->rollback();
399 return -1;
400 }
401 } else {
402 $this->error = $this->db->error()." sql=".$sql;
403 $this->db->rollback();
404 return -1;
405 }
406 }
407}
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage intervention lines.
deleteLine($user, $notrigger=0)
Delete a intervention line.
fetch($rowid)
Retrieve the line of intervention.
update_total()
Update total duration into llx_fichinter.
update($user, $notrigger=0)
Update intervention into database.
__construct($db)
Constructor.
insert($user, $notrigger=0)
Insert the line into database.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79