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