dolibarr 24.0.0-beta
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-2025 Charlene Benke <charlene@patas-monkey.com>
8 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
9 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
11 * Copyright (C) 2024-2026 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 $special_code;
78
82 public $rang = 0;
83
87 public $tva_tx;
88
93 public $subprice;
94
98 public $element = 'fichinterdet';
99
103 public $table_element = 'fichinterdet';
104
108 public $fk_element = 'fk_fichinter';
109
110
111
117 public function __construct($db)
118 {
119 $this->db = $db;
120 }
121
128 public function fetch($rowid)
129 {
130 dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
131
132 $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date, ft.product_type, ft.special_code, ft.extraparams';
133 $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
134 $sql .= ' WHERE ft.rowid = '.((int) $rowid);
135
136 $resql = $this->db->query($sql);
137 if ($resql) {
138 $objp = $this->db->fetch_object($resql);
139 $this->rowid = $objp->rowid;
140 $this->id = $objp->rowid;
141 $this->fk_fichinter = $objp->fk_fichinter;
142 $this->date = $this->db->jdate($objp->date);
143 $this->datei = $this->db->jdate($objp->date); // For backward compatibility
144 $this->desc = $objp->description;
145 $this->product_type = $objp->product_type;
146 $this->duration = $objp->duree;
147 $this->rang = $objp->rang;
148 $this->special_code = $objp->special_code;
149
150 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
151
152 $this->db->free($resql);
153
154 $this->fetch_optionals();
155
156 return 1;
157 } else {
158 $this->error = $this->db->error().' sql='.$sql;
159 return -1;
160 }
161 }
162
170 public function insert($user, $notrigger = 0)
171 {
172 $error = 0;
173
174 dol_syslog("FichinterLigne::insert rang=".$this->rang);
175
176 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
177 $this->date = $this->datei;
178 }
179
180 // Check parameters
181 if ($this->product_type < 0) {
182 return -1;
183 }
184
185 $this->db->begin();
186
187 $rangToUse = $this->rang;
188 if ($rangToUse == -1) {
189 // Retrieve max rank of intervention line into $rangmax
190 $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
191 $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
192 $resql = $this->db->query($sql);
193 if ($resql) {
194 $obj = $this->db->fetch_object($resql);
195 $rangToUse = (int) $obj->max + 1;
196 } else {
197 dol_print_error($this->db);
198 $this->db->rollback();
199 return -1;
200 }
201 }
202
203 // Insert line into database
204 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
205 $sql .= ' (fk_fichinter, description, date, duree, rang, product_type, special_code)';
206 $sql .= " VALUES (".((int) $this->fk_fichinter).",";
207 $sql .= " '".$this->db->escape($this->desc)."',";
208 $sql .= " '".$this->db->idate($this->date)."',";
209 $sql .= " ".((int) $this->duration).",";
210 $sql .= ' '.((int) $rangToUse).",";
211 $sql .= " ".((int) $this->product_type).",";
212 $sql .= " ".((int) $this->special_code);
213 $sql .= ')';
214
215 dol_syslog("FichinterLigne::insert", LOG_DEBUG);
216 $resql = $this->db->query($sql);
217 if ($resql) {
218 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
219 $this->rowid = $this->id;
220
221 $result = $this->insertExtraFields();
222 if ($result < 0) {
223 $error++;
224 }
225
226
227 $result = $this->update_total();
228
229 if ($result > 0) {
230 $this->rang = $rangToUse;
231
232 if (!$notrigger) {
233 // Call trigger
234 $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
235 if ($result < 0) {
236 $error++;
237 }
238 // End call triggers
239 }
240 }
241
242 if (!$error) {
243 $this->db->commit();
244 return $result;
245 } else {
246 $this->db->rollback();
247 return -1;
248 }
249 } else {
250 $this->error = $this->db->error()." sql=".$sql;
251 $this->db->rollback();
252 return -1;
253 }
254 }
255
256
264 public function update($user, $notrigger = 0)
265 {
266 $error = 0;
267
268 if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
269 $this->date = $this->datei;
270 }
271
272 $this->db->begin();
273
274 // Update line in database
275 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
276 $sql .= " description = '".$this->db->escape($this->desc)."',";
277 $sql .= " date = '".$this->db->idate($this->date)."',";
278 $sql .= " duree = ".((int) $this->duration).",";
279 $sql .= " rang = ".((int) $this->rang).",";
280 $sql .= " product_type = ".((int) $this->product_type);
281 $sql .= " WHERE rowid = ".((int) $this->id);
282
283 dol_syslog("FichinterLigne::update", LOG_DEBUG);
284 $resql = $this->db->query($sql);
285 if ($resql) {
286 $result = $this->insertExtraFields();
287 if ($result < 0) {
288 $error++;
289 }
290
291 $result = $this->update_total();
292 if ($result > 0) {
293 if (!$notrigger) {
294 // Call trigger
295 $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
296 if ($result < 0) {
297 $error++;
298 }
299 // End call triggers
300 }
301 }
302
303 if (!$error) {
304 $this->db->commit();
305 return $result;
306 } else {
307 $this->error = $this->db->lasterror();
308 $this->db->rollback();
309 return -1;
310 }
311 } else {
312 $this->error = $this->db->lasterror();
313 $this->db->rollback();
314 return -1;
315 }
316 }
317
318 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
324 public function update_total()
325 {
326 // phpcs:enable
327 global $conf;
328
329 $this->db->begin();
330
331 $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
332 $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
333 $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
334 $sql .= " AND product_type <> 9";
335
336 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
337 $resql = $this->db->query($sql);
338 if ($resql) {
339 $obj = $this->db->fetch_object($resql);
340 $total_duration = 0;
341 if (!empty($obj->total_duration)) {
342 $total_duration = $obj->total_duration;
343 }
344
345 $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
346 $sql .= " SET duree = ".((int) $total_duration);
347 $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
348 $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
349 $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
350
351 dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
352 $resql = $this->db->query($sql);
353 if ($resql) {
354 $this->db->commit();
355 return 1;
356 } else {
357 $this->error = $this->db->error();
358 $this->db->rollback();
359 return -2;
360 }
361 } else {
362 $this->error = $this->db->error();
363 $this->db->rollback();
364 return -1;
365 }
366 }
367
375 public function deleteLine($user, $notrigger = 0)
376 {
377 $error = 0;
378
379 dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
380
381 $this->db->begin();
382
383 $result = $this->deleteExtraFields();
384 if ($result < 0) {
385 $error++;
386 $this->db->rollback();
387 return -1;
388 }
389
390 $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
391 $resql = $this->db->query($sql);
392
393 if ($resql) {
394 $result = $this->update_total();
395 if ($result > 0) {
396 if (!$notrigger) {
397 // Call trigger
398 $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
399 if ($result < 0) {
400 $error++;
401 $this->db->rollback();
402 return -1;
403 }
404 // End call triggers
405 }
406
407 $this->db->commit();
408 return $result;
409 } else {
410 $this->db->rollback();
411 return -1;
412 }
413 } else {
414 $this->error = $this->db->error()." sql=".$sql;
415 $this->db->rollback();
416 return -1;
417 }
418 }
419}
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.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:487