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