dolibarr 21.0.0-alpha
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';
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->db->free($resql);
144
145 $this->fetch_optionals();
146
147 return 1;
148 } else {
149 $this->error = $this->db->error().' sql='.$sql;
150 return -1;
151 }
152 }
153
161 public function insert($user, $notrigger = 0)
162 {
163 $error = 0;
164
165 dol_syslog("FichinterLigne::insert rang=".$this->rang);
166
167 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
168 $this->date = $this->datei;
169 }
170
171 $this->db->begin();
172
173 $rangToUse = $this->rang;
174 if ($rangToUse == -1) {
175 // Recupere rang max de la ligne d'intervention dans $rangmax
176 $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
177 $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
178 $resql = $this->db->query($sql);
179 if ($resql) {
180 $obj = $this->db->fetch_object($resql);
181 $rangToUse = $obj->max + 1;
182 } else {
183 dol_print_error($this->db);
184 $this->db->rollback();
185 return -1;
186 }
187 }
188
189 // Insertion dans base de la ligne
190 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
191 $sql .= ' (fk_fichinter, description, date, duree, rang)';
192 $sql .= " VALUES (".((int) $this->fk_fichinter).",";
193 $sql .= " '".$this->db->escape($this->desc)."',";
194 $sql .= " '".$this->db->idate($this->date)."',";
195 $sql .= " ".((int) $this->duration).",";
196 $sql .= ' '.((int) $rangToUse);
197 $sql .= ')';
198
199 dol_syslog("FichinterLigne::insert", LOG_DEBUG);
200 $resql = $this->db->query($sql);
201 if ($resql) {
202 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
203 $this->rowid = $this->id;
204
205 if (!$error) {
206 $result = $this->insertExtraFields();
207 if ($result < 0) {
208 $error++;
209 }
210 }
211
212
213 $result = $this->update_total();
214
215 if ($result > 0) {
216 $this->rang = $rangToUse;
217
218 if (!$notrigger) {
219 // Call trigger
220 $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
221 if ($result < 0) {
222 $error++;
223 }
224 // End call triggers
225 }
226 }
227
228 if (!$error) {
229 $this->db->commit();
230 return $result;
231 } else {
232 $this->db->rollback();
233 return -1;
234 }
235 } else {
236 $this->error = $this->db->error()." sql=".$sql;
237 $this->db->rollback();
238 return -1;
239 }
240 }
241
242
250 public function update($user, $notrigger = 0)
251 {
252 $error = 0;
253
254 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
255 $this->date = $this->datei;
256 }
257
258 $this->db->begin();
259
260 // Mise a jour ligne en base
261 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
262 $sql .= " description = '".$this->db->escape($this->desc)."',";
263 $sql .= " date = '".$this->db->idate($this->date)."',";
264 $sql .= " duree = ".((int) $this->duration).",";
265 $sql .= " rang = ".((int) $this->rang);
266 $sql .= " WHERE rowid = ".((int) $this->id);
267
268 dol_syslog("FichinterLigne::update", LOG_DEBUG);
269 $resql = $this->db->query($sql);
270 if ($resql) {
271 if (!$error) {
272 $result = $this->insertExtraFields();
273 if ($result < 0) {
274 $error++;
275 }
276 }
277
278 $result = $this->update_total();
279 if ($result > 0) {
280 if (!$notrigger) {
281 // Call trigger
282 $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
283 if ($result < 0) {
284 $error++;
285 }
286 // End call triggers
287 }
288 }
289
290 if (!$error) {
291 $this->db->commit();
292 return $result;
293 } else {
294 $this->error = $this->db->lasterror();
295 $this->db->rollback();
296 return -1;
297 }
298 } else {
299 $this->error = $this->db->lasterror();
300 $this->db->rollback();
301 return -1;
302 }
303 }
304
305 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
311 public function update_total()
312 {
313 // phpcs:enable
314 global $conf;
315
316 $this->db->begin();
317
318 $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
319 $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
320 $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
321
322 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
323 $resql = $this->db->query($sql);
324 if ($resql) {
325 $obj = $this->db->fetch_object($resql);
326 $total_duration = 0;
327 if (!empty($obj->total_duration)) {
328 $total_duration = $obj->total_duration;
329 }
330
331 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
332 $sql .= " SET duree = ".((int) $total_duration);
333 $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
334 $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
335 $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
336
337 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
338 $resql = $this->db->query($sql);
339 if ($resql) {
340 $this->db->commit();
341 return 1;
342 } else {
343 $this->error = $this->db->error();
344 $this->db->rollback();
345 return -2;
346 }
347 } else {
348 $this->error = $this->db->error();
349 $this->db->rollback();
350 return -1;
351 }
352 }
353
361 public function deleteLine($user, $notrigger = 0)
362 {
363 $error = 0;
364
365 dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
366
367 $this->db->begin();
368
369 $result = $this->deleteExtraFields();
370 if ($result < 0) {
371 $error++;
372 $this->db->rollback();
373 return -1;
374 }
375
376 $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
377 $resql = $this->db->query($sql);
378
379 if ($resql) {
380 $result = $this->update_total();
381 if ($result > 0) {
382 if (!$notrigger) {
383 // Call trigger
384 $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
385 if ($result < 0) {
386 $error++;
387 $this->db->rollback();
388 return -1;
389 }
390 // End call triggers
391 }
392
393 $this->db->commit();
394 return $result;
395 } else {
396 $this->db->rollback();
397 return -1;
398 }
399 } else {
400 $this->error = $this->db->error()." sql=".$sql;
401 $this->db->rollback();
402 return -1;
403 }
404 }
405}
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.