dolibarr  16.0.1
fichinter.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-2020 Frédéric France <frederic.france@netlogic.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
30 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
32 
33 
37 class Fichinter extends CommonObject
38 {
39 
40  public $fields = array(
41  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
42  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'$conf->societe->enabled', 'visible'=>-1, 'notnull'=>1, 'position'=>15),
43  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>'$conf->project->enabled', 'visible'=>-1, 'position'=>20),
44  'fk_contrat' =>array('type'=>'integer', 'label'=>'Fk contrat', 'enabled'=>'$conf->contrat->enabled', 'visible'=>-1, 'position'=>25),
45  'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>30),
46  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),
47  'ref_client' =>array('type'=>'varchar(255)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>36),
48  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>40, 'index'=>1),
49  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>45),
50  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
51  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
52  'datei' =>array('type'=>'date', 'label'=>'Datei', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
53  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
54  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>70),
55  'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
56  'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
57  'dateo' =>array('type'=>'date', 'label'=>'Dateo', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
58  'datee' =>array('type'=>'date', 'label'=>'Datee', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
59  'datet' =>array('type'=>'date', 'label'=>'Datet', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
60  'duree' =>array('type'=>'double', 'label'=>'Duree', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
61  'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
62  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
63  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>115),
64  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>120),
65  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'Last main doc', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
66  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>130),
67  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
68  );
72  public $element = 'fichinter';
73 
77  public $table_element = 'fichinter';
78 
82  public $fk_element = 'fk_fichinter';
83 
87  public $table_element_line = 'fichinterdet';
88 
92  public $picto = 'intervention';
93 
97  protected $table_ref_field = 'ref';
98 
102  public $socid;
103 
104  public $author;
105 
111  public $datec;
112 
113  public $datev;
114  public $dateo;
115  public $datee;
116  public $datet;
117 
123  public $datem;
124 
128  public $duration;
129 
133  public $statut = 0; // 0=draft, 1=validated, 2=invoiced, 3=Terminate
134 
138  public $description;
139 
143  public $fk_contrat = 0;
144 
148  public $fk_project = 0;
149 
154  public $ref_client;
155 
159  public $extraparams = array();
160 
164  public $lines = array();
165 
169  const STATUS_DRAFT = 0;
170 
174  const STATUS_VALIDATED = 1;
175 
179  const STATUS_BILLED = 2;
180 
184  const STATUS_CLOSED = 3;
185 
186 
192  public function __construct($db)
193  {
194  $this->db = $db;
195  }
196 
197  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
203  public function load_state_board()
204  {
205  // phpcs:enable
206  global $user;
207 
208  $this->nb = array();
209  $clause = "WHERE";
210 
211  $sql = "SELECT count(fi.rowid) as nb";
212  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as fi";
213  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON fi.fk_soc = s.rowid";
214  if (empty($user->rights->societe->client->voir) && !$user->socid) {
215  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
216  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
217  $clause = "AND";
218  }
219  $sql .= " ".$clause." fi.entity IN (".getEntity('intervention').")";
220 
221  $resql = $this->db->query($sql);
222  if ($resql) {
223  while ($obj = $this->db->fetch_object($resql)) {
224  $this->nb["interventions"] = $obj->nb;
225  }
226  $this->db->free($resql);
227  return 1;
228  } else {
229  dol_print_error($this->db);
230  $this->error = $this->db->error();
231  return -1;
232  }
233  }
234 
242  public function create($user, $notrigger = 0)
243  {
244  global $conf, $langs;
245 
246  $error = 0;
247 
248  dol_syslog(get_class($this)."::create ref=".$this->ref);
249 
250  // Check parameters
251  if (!empty($this->ref)) { // We check that ref is not already used
252  $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
253  if ($result > 0) {
254  $this->error = 'ErrorRefAlreadyExists';
255  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
256  $this->db->rollback();
257  return -1;
258  }
259  }
260  if (!is_numeric($this->duration)) {
261  $this->duration = 0;
262  }
263  if (isset($this->ref_client)) {
264  $this->ref_client = trim($this->ref_client);
265  }
266 
267  if ($this->socid <= 0) {
268  $this->error = 'ErrorFicheinterCompanyDoesNotExist';
269  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
270  return -1;
271  }
272 
273  $soc = new Societe($this->db);
274  $result = $soc->fetch($this->socid);
275 
276  $now = dol_now();
277 
278  $this->db->begin();
279 
280  $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter (";
281  $sql .= "fk_soc";
282  $sql .= ", datec";
283  $sql .= ", ref";
284  $sql .= ", ref_client";
285  $sql .= ", entity";
286  $sql .= ", fk_user_author";
287  $sql .= ", fk_user_modif";
288  $sql .= ", description";
289  $sql .= ", model_pdf";
290  $sql .= ", fk_projet";
291  $sql .= ", fk_contrat";
292  $sql .= ", fk_statut";
293  $sql .= ", note_private";
294  $sql .= ", note_public";
295  $sql .= ") ";
296  $sql .= " VALUES (";
297  $sql .= $this->socid;
298  $sql .= ", '".$this->db->idate($now)."'";
299  $sql .= ", '".$this->db->escape($this->ref)."'";
300  $sql .= ", ".($this->ref_client ? "'".$this->db->escape($this->ref_client)."'" : "null");
301  $sql .= ", ".((int) $conf->entity);
302  $sql .= ", ".((int) $user->id);
303  $sql .= ", ".((int) $user->id);
304  $sql .= ", ".($this->description ? "'".$this->db->escape($this->description)."'" : "null");
305  $sql .= ", '".$this->db->escape($this->model_pdf)."'";
306  $sql .= ", ".($this->fk_project ? ((int) $this->fk_project) : 0);
307  $sql .= ", ".($this->fk_contrat ? ((int) $this->fk_contrat) : 0);
308  $sql .= ", ".((int) $this->statut);
309  $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
310  $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
311  $sql .= ")";
312 
313  dol_syslog(get_class($this)."::create", LOG_DEBUG);
314  $result = $this->db->query($sql);
315  if ($result) {
316  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."fichinter");
317 
318  if ($this->id) {
319  $this->ref = '(PROV'.$this->id.')';
320  $sql = 'UPDATE '.MAIN_DB_PREFIX."fichinter SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
321 
322  dol_syslog(get_class($this)."::create", LOG_DEBUG);
323  $resql = $this->db->query($sql);
324  if (!$resql) {
325  $error++;
326  }
327  }
328 
329  if (!$error) {
330  $result = $this->insertExtraFields();
331  if ($result < 0) {
332  $error++;
333  }
334  }
335 
336  // Add linked object
337  if (!$error && $this->origin && $this->origin_id) {
338  $ret = $this->add_object_linked();
339  if (!$ret) {
340  dol_print_error($this->db);
341  }
342  }
343 
344 
345  if (!$error && !$notrigger) {
346  // Call trigger
347  $result = $this->call_trigger('FICHINTER_CREATE', $user);
348  if ($result < 0) {
349  $error++;
350  }
351  // End call triggers
352  }
353 
354  if (!$error) {
355  $this->db->commit();
356  return $this->id;
357  } else {
358  $this->db->rollback();
359  $this->error = join(',', $this->errors);
360  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
361  return -1;
362  }
363  } else {
364  $this->error = $this->db->error();
365  $this->db->rollback();
366  return -1;
367  }
368  }
369 
377  public function update($user, $notrigger = 0)
378  {
379  global $conf;
380 
381  if (!is_numeric($this->duration)) {
382  $this->duration = 0;
383  }
384  if (!dol_strlen($this->fk_project)) {
385  $this->fk_project = 0;
386  }
387  if (isset($this->ref_client)) {
388  $this->ref_client = trim($this->ref_client);
389  }
390 
391  $error = 0;
392 
393  $this->db->begin();
394 
395  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter SET ";
396  $sql .= "description = '".$this->db->escape($this->description)."'";
397  $sql .= ", duree = ".((int) $this->duration);
398  $sql .= ", ref_client = ".($this->ref_client ? "'".$this->db->escape($this->ref_client)."'" : "null");
399  $sql .= ", fk_projet = ".((int) $this->fk_project);
400  $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
401  $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
402  $sql .= ", fk_user_modif = ".((int) $user->id);
403  $sql .= " WHERE rowid = ".((int) $this->id);
404 
405  dol_syslog(get_class($this)."::update", LOG_DEBUG);
406  if ($this->db->query($sql)) {
407  if (!$error) {
408  $result = $this->insertExtraFields();
409  if ($result < 0) {
410  $error++;
411  }
412  }
413 
414  if (!$error && !$notrigger) {
415  // Call trigger
416  $result = $this->call_trigger('FICHINTER_MODIFY', $user);
417  if ($result < 0) {
418  $error++; $this->db->rollback(); return -1;
419  }
420  // End call triggers
421  }
422 
423  $this->db->commit();
424  return 1;
425  } else {
426  $this->error = $this->db->error();
427  $this->db->rollback();
428  return -1;
429  }
430  }
431 
439  public function fetch($rowid, $ref = '')
440  {
441  $sql = "SELECT f.rowid, f.ref, f.ref_client, f.description, f.fk_soc, f.fk_statut,";
442  $sql .= " f.datec, f.dateo, f.datee, f.datet, f.fk_user_author,";
443  $sql .= " f.date_valid as datev,";
444  $sql .= " f.tms as datem,";
445  $sql .= " f.duree, f.fk_projet as fk_project, f.note_public, f.note_private, f.model_pdf, f.extraparams, fk_contrat, f.entity as entity";
446  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
447  if ($ref) {
448  $sql .= " WHERE f.entity IN (".getEntity('intervention').")";
449  $sql .= " AND f.ref = '".$this->db->escape($ref)."'";
450  } else {
451  $sql .= " WHERE f.rowid = ".((int) $rowid);
452  }
453 
454  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
455  $resql = $this->db->query($sql);
456  if ($resql) {
457  if ($this->db->num_rows($resql)) {
458  $obj = $this->db->fetch_object($resql);
459 
460  $this->id = $obj->rowid;
461  $this->ref = $obj->ref;
462  $this->ref_client = $obj->ref_client;
463  $this->description = $obj->description;
464  $this->socid = $obj->fk_soc;
465  $this->statut = $obj->fk_statut;
466  $this->duration = $obj->duree;
467  $this->datec = $this->db->jdate($obj->datec);
468  $this->dateo = $this->db->jdate($obj->dateo);
469  $this->datee = $this->db->jdate($obj->datee);
470  $this->datet = $this->db->jdate($obj->datet);
471  $this->datev = $this->db->jdate($obj->datev);
472  $this->datem = $this->db->jdate($obj->datem);
473  $this->fk_project = $obj->fk_project;
474  $this->note_public = $obj->note_public;
475  $this->note_private = $obj->note_private;
476  $this->model_pdf = $obj->model_pdf;
477  $this->modelpdf = $obj->model_pdf; // deprecated
478  $this->fk_contrat = $obj->fk_contrat;
479  $this->entity = $obj->entity;
480 
481  $this->user_creation = $obj->fk_user_author;
482 
483  $this->extraparams = (array) json_decode($obj->extraparams, true);
484 
485  if ($this->statut == 0) {
486  $this->brouillon = 1;
487  }
488 
489  // Retrieve extrafields
490  $this->fetch_optionals();
491 
492  /*
493  * Lines
494  */
495  $result = $this->fetch_lines();
496  if ($result < 0) {
497  return -3;
498  }
499  $this->db->free($resql);
500  return 1;
501  }
502  } else {
503  $this->error = $this->db->lasterror();
504  return -1;
505  }
506  }
507 
514  public function setDraft($user)
515  {
516  global $langs, $conf;
517 
518  $error = 0;
519 
520  // Protection
521  if ($this->statut <= self::STATUS_DRAFT) {
522  return 0;
523  }
524 
525  dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
526 
527  $this->db->begin();
528 
529  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
530  $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
531  $sql .= " WHERE rowid = ".((int) $this->id);
532 
533  $resql = $this->db->query($sql);
534  if ($resql) {
535  if (!$error) {
536  $this->oldcopy = clone $this;
537  }
538 
539  if (!$error) {
540  // Call trigger
541  $result = $this->call_trigger('FICHINTER_UNVALIDATE', $user);
542  if ($result < 0) {
543  $error++;
544  }
545  }
546 
547  if (!$error) {
548  $this->statut = self::STATUS_DRAFT;
549  $this->db->commit();
550  return 1;
551  } else {
552  $this->db->rollback();
553  return -1;
554  }
555  } else {
556  $this->db->rollback();
557  $this->error = $this->db->lasterror();
558  return -1;
559  }
560  }
561 
569  public function setValid($user, $notrigger = 0)
570  {
571  global $conf;
572  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
573 
574  $error = 0;
575 
576  if ($this->statut != 1) {
577  $this->db->begin();
578 
579  $now = dol_now();
580 
581  // Define new ref
582  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
583  $num = $this->getNextNumRef($this->thirdparty);
584  } else {
585  $num = $this->ref;
586  }
587  $this->newref = dol_sanitizeFileName($num);
588 
589  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
590  $sql .= " SET fk_statut = 1";
591  $sql .= ", ref = '".$this->db->escape($num)."'";
592  $sql .= ", date_valid = '".$this->db->idate($now)."'";
593  $sql .= ", fk_user_valid = ".($user->id > 0 ? (int) $user->id : "null");
594  $sql .= " WHERE rowid = ".((int) $this->id);
595  $sql .= " AND entity = ".((int) $conf->entity);
596  $sql .= " AND fk_statut = 0";
597 
598  dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
599  $resql = $this->db->query($sql);
600  if (!$resql) {
601  dol_print_error($this->db);
602  $error++;
603  }
604 
605  if (!$error && !$notrigger) {
606  // Call trigger
607  $result = $this->call_trigger('FICHINTER_VALIDATE', $user);
608  if ($result < 0) {
609  $error++;
610  }
611  // End call triggers
612  }
613 
614  if (!$error) {
615  $this->oldref = $this->ref;
616 
617  // Rename directory if dir was a temporary ref
618  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
619  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
620 
621  // Now we rename also files into index
622  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'ficheinter/".$this->db->escape($this->newref)."'";
623  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
624  $resql = $this->db->query($sql);
625  if (!$resql) {
626  $error++; $this->error = $this->db->lasterror();
627  }
628 
629  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
630  $oldref = dol_sanitizeFileName($this->ref);
631  $newref = dol_sanitizeFileName($num);
632  $dirsource = $conf->ficheinter->dir_output.'/'.$oldref;
633  $dirdest = $conf->ficheinter->dir_output.'/'.$newref;
634  if (!$error && file_exists($dirsource)) {
635  dol_syslog(get_class($this)."::setValid rename dir ".$dirsource." into ".$dirdest);
636 
637  if (@rename($dirsource, $dirdest)) {
638  dol_syslog("Rename ok");
639  // Rename docs starting with $oldref with $newref
640  $listoffiles = dol_dir_list($conf->ficheinter->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
641  foreach ($listoffiles as $fileentry) {
642  $dirsource = $fileentry['name'];
643  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
644  $dirsource = $fileentry['path'].'/'.$dirsource;
645  $dirdest = $fileentry['path'].'/'.$dirdest;
646  @rename($dirsource, $dirdest);
647  }
648  }
649  }
650  }
651  }
652 
653  // Set new ref and define current statut
654  if (!$error) {
655  $this->ref = $num;
656  $this->statut = 1;
657  $this->brouillon = 0;
658  $this->date_validation = $now;
659  $this->db->commit();
660  return 1;
661  } else {
662  $this->db->rollback();
663  dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
664  return -1;
665  }
666  }
667  }
668 
674  public function getAmount()
675  {
676  global $db;
677 
678  $amount = 0;
679 
680  $this->author = new User($db);
681  $this->author->fetch($this->user_creation);
682 
683  $thm = $this->author->thm;
684 
685  foreach ($this->lines as $line) {
686  $amount += ($line->duration / 60 / 60 * $thm);
687  }
688 
689  return price2num($amount, 'MT');
690  }
691 
692 
704  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
705  {
706  global $conf;
707 
708  $outputlangs->load("interventions");
709 
710  if (!dol_strlen($modele)) {
711  $modele = 'soleil';
712 
713  if (!empty($this->model_pdf)) {
714  $modele = $this->model_pdf;
715  } elseif (!empty($this->modelpdf)) { // deprecated
716  $modele = $this->modelpdf;
717  } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
718  $modele = $conf->global->FICHEINTER_ADDON_PDF;
719  }
720  }
721 
722  $modelpath = "core/modules/fichinter/doc/";
723 
724  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
725  }
726 
733  public function getLibStatut($mode = 0)
734  {
735  return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
736  }
737 
738  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
746  public function LibStatut($status, $mode = 0)
747  {
748  // phpcs:enable
749  // Init/load array of translation of status
750  if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) {
751  global $langs;
752  $langs->load("fichinter");
753 
754  $this->statuts[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
755  $this->statuts[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
756  $this->statuts[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
757  $this->statuts[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
758  $this->statuts_short[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
759  $this->statuts_short[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
760  $this->statuts_short[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
761  $this->statuts_short[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
762  $this->statuts_logo[self::STATUS_DRAFT] = 'status0';
763  $this->statuts_logo[self::STATUS_VALIDATED] = 'status1';
764  $this->statuts_logo[self::STATUS_BILLED] = 'status6';
765  $this->statuts_logo[self::STATUS_CLOSED] = 'status6';
766  }
767 
768  return dolGetStatus($this->statuts[$status], $this->statuts_short[$status], '', $this->statuts_logo[$status], $mode);
769  }
770 
780  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1)
781  {
782  global $conf, $langs, $hookmanager;
783 
784  $result = '';
785 
786  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
787  if (isset($this->status)) {
788  $label .= ' '.$this->getLibStatut(5);
789  }
790  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
791 
792  $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
793 
794  if ($option !== 'nolink') {
795  // Add param to save lastsearch_values or not
796  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
797  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
798  $add_save_lastsearch_values = 1;
799  }
800  if ($add_save_lastsearch_values) {
801  $url .= '&save_lastsearch_values=1';
802  }
803  }
804 
805  $linkclose = '';
806  if (empty($notooltip)) {
807  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
808  $label = $langs->trans("ShowIntervention");
809  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
810  }
811  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
812  $linkclose .= ' class="classfortooltip"';
813 
814  /*
815  $hookmanager->initHooks(array('fichinterdao'));
816  $parameters=array('id'=>$this->id);
817  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
818  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
819  */
820  }
821 
822  $linkstart = '<a href="'.$url.'"';
823  $linkstart .= $linkclose.'>';
824  $linkend = '</a>';
825 
826  $result .= $linkstart;
827  if ($withpicto) {
828  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
829  }
830  if ($withpicto != 2) {
831  $result .= $this->ref;
832  }
833  $result .= $linkend;
834 
835  global $action;
836  $hookmanager->initHooks(array('interventiondao'));
837  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
838  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
839  if ($reshook > 0) {
840  $result = $hookmanager->resPrint;
841  } else {
842  $result .= $hookmanager->resPrint;
843  }
844 
845  return $result;
846  }
847 
848 
856  public function getNextNumRef($soc)
857  {
858  global $conf, $db, $langs;
859  $langs->load("interventions");
860 
861  if (!empty($conf->global->FICHEINTER_ADDON)) {
862  $mybool = false;
863 
864  $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
865  $classname = "mod_".$conf->global->FICHEINTER_ADDON;
866 
867  // Include file with class
868  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
869 
870  foreach ($dirmodels as $reldir) {
871  $dir = dol_buildpath($reldir."core/modules/fichinter/");
872 
873  // Load file with numbering class (if found)
874  $mybool |= @include_once $dir.$file;
875  }
876 
877  if ($mybool === false) {
878  dol_print_error('', "Failed to include file ".$file);
879  return '';
880  }
881 
882  $obj = new $classname();
883  $numref = "";
884  $numref = $obj->getNextValue($soc, $this);
885 
886  if ($numref != "") {
887  return $numref;
888  } else {
889  dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
890  return "";
891  }
892  } else {
893  $langs->load("errors");
894  print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
895  return "";
896  }
897  }
898 
905  public function info($id)
906  {
907  global $conf;
908 
909  $sql = "SELECT f.rowid,";
910  $sql .= " f.datec,";
911  $sql .= " f.tms as date_modification,";
912  $sql .= " f.date_valid as datev,";
913  $sql .= " f.fk_user_author,";
914  $sql .= " f.fk_user_modif as fk_user_modification,";
915  $sql .= " f.fk_user_valid";
916  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
917  $sql .= " WHERE f.rowid = ".((int) $id);
918 
919  $resql = $this->db->query($sql);
920  if ($resql) {
921  if ($this->db->num_rows($resql)) {
922  $obj = $this->db->fetch_object($resql);
923 
924  $this->id = $obj->rowid;
925 
926  $this->date_creation = $this->db->jdate($obj->datec);
927  $this->date_modification = $this->db->jdate($obj->date_modification);
928  $this->date_validation = $this->db->jdate($obj->datev);
929 
930  $cuser = new User($this->db);
931  $cuser->fetch($obj->fk_user_author);
932  $this->user_creation = $cuser;
933 
934  if ($obj->fk_user_valid) {
935  $vuser = new User($this->db);
936  $vuser->fetch($obj->fk_user_valid);
937  $this->user_validation = $vuser;
938  }
939  if ($obj->fk_user_modification) {
940  $muser = new User($this->db);
941  $muser->fetch($obj->fk_user_modification);
942  $this->user_modification = $muser;
943  }
944  }
945  $this->db->free($resql);
946  } else {
947  dol_print_error($this->db);
948  }
949  }
950 
958  public function delete($user, $notrigger = 0)
959  {
960  global $conf, $langs;
961  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
962 
963  $error = 0;
964 
965  $this->db->begin();
966 
967  if (!$error && !$notrigger) {
968  // Call trigger
969  $result = $this->call_trigger('FICHINTER_DELETE', $user);
970  if ($result < 0) {
971  $error++; $this->db->rollback(); return -1;
972  }
973  // End call triggers
974  }
975 
976  // Delete linked object
977  if (!$error) {
978  $res = $this->deleteObjectLinked();
979  if ($res < 0) {
980  $error++;
981  }
982  }
983 
984  // Delete linked contacts
985  if (!$error) {
986  $res = $this->delete_linked_contact();
987  if ($res < 0) {
988  $this->error = 'ErrorFailToDeleteLinkedContact';
989  $error++;
990  }
991  }
992 
993  if (!$error) {
994  $main = MAIN_DB_PREFIX.'fichinterdet';
995  $ef = $main."_extrafields";
996  $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
997 
998  $resql = $this->db->query($sql);
999  if (!$resql) {
1000  $error++;
1001  }
1002  }
1003 
1004  if (!$error) {
1005  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
1006  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1007 
1008  $resql = $this->db->query($sql);
1009  if (!$resql) {
1010  $error++;
1011  }
1012  }
1013 
1014  if (!$error) {
1015  // Remove extrafields
1016  $res = $this->deleteExtraFields();
1017  if ($res < 0) {
1018  $error++;
1019  }
1020  }
1021 
1022  if (!$error) {
1023  // Delete object
1024  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
1025  $sql .= " WHERE rowid = ".((int) $this->id);
1026 
1027  dol_syslog("Fichinter::delete", LOG_DEBUG);
1028  $resql = $this->db->query($sql);
1029  if (!$resql) {
1030  $error++;
1031  }
1032  }
1033 
1034  if (!$error) {
1035  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1036  $this->deleteEcmFiles();
1037 
1038  // Remove directory with files
1039  $fichinterref = dol_sanitizeFileName($this->ref);
1040  if ($conf->ficheinter->dir_output) {
1041  $dir = $conf->ficheinter->dir_output."/".$fichinterref;
1042  $file = $conf->ficheinter->dir_output."/".$fichinterref."/".$fichinterref.".pdf";
1043  if (file_exists($file)) {
1044  dol_delete_preview($this);
1045 
1046  if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
1047  $langs->load("errors");
1048  $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
1049  return 0;
1050  }
1051  }
1052  if (file_exists($dir)) {
1053  if (!dol_delete_dir_recursive($dir)) {
1054  $langs->load("errors");
1055  $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
1056  return 0;
1057  }
1058  }
1059  }
1060  }
1061 
1062  if (!$error) {
1063  $this->db->commit();
1064  return 1;
1065  } else {
1066  $this->db->rollback();
1067  return -1;
1068  }
1069  }
1070 
1071  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1079  public function set_date_delivery($user, $date_delivery)
1080  {
1081  // phpcs:enable
1082  global $conf;
1083 
1084  if ($user->rights->ficheinter->creer) {
1085  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1086  $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
1087  $sql .= " WHERE rowid = ".((int) $this->id);
1088  $sql .= " AND fk_statut = 0";
1089 
1090  if ($this->db->query($sql)) {
1091  $this->date_delivery = $date_delivery;
1092  return 1;
1093  } else {
1094  $this->error = $this->db->error();
1095  dol_syslog("Fichinter::set_date_delivery Erreur SQL");
1096  return -1;
1097  }
1098  }
1099  }
1100 
1101  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1109  public function set_description($user, $description)
1110  {
1111  // phpcs:enable
1112  global $conf;
1113 
1114  if ($user->rights->ficheinter->creer) {
1115  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1116  $sql .= " SET description = '".$this->db->escape($description)."',";
1117  $sql .= " fk_user_modif = ".$user->id;
1118  $sql .= " WHERE rowid = ".((int) $this->id);
1119 
1120  if ($this->db->query($sql)) {
1121  $this->description = $description;
1122  return 1;
1123  } else {
1124  $this->error = $this->db->error();
1125  dol_syslog("Fichinter::set_description Erreur SQL");
1126  return -1;
1127  }
1128  }
1129  }
1130 
1131 
1132  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1140  public function set_contrat($user, $contractid)
1141  {
1142  // phpcs:enable
1143  global $conf;
1144 
1145  if ($user->rights->ficheinter->creer) {
1146  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1147  $sql .= " SET fk_contrat = ".((int) $contractid);
1148  $sql .= " WHERE rowid = ".((int) $this->id);
1149 
1150  if ($this->db->query($sql)) {
1151  $this->fk_contrat = $contractid;
1152  return 1;
1153  } else {
1154  $this->error = $this->db->error();
1155  return -1;
1156  }
1157  }
1158  return -2;
1159  }
1160 
1161 
1162 
1170  public function createFromClone(User $user, $socid = 0)
1171  {
1172  global $hookmanager;
1173 
1174  $error = 0;
1175 
1176  $this->db->begin();
1177 
1178  // get extrafields so they will be clone
1179  foreach ($this->lines as $line) {
1180  $line->fetch_optionals();
1181  }
1182 
1183  // Load source object
1184  $objFrom = clone $this;
1185 
1186  // Change socid if needed
1187  if (!empty($socid) && $socid != $this->socid) {
1188  $objsoc = new Societe($this->db);
1189 
1190  if ($objsoc->fetch($socid) > 0) {
1191  $this->socid = $objsoc->id;
1192  //$this->cond_reglement_id = (! empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1193  //$this->mode_reglement_id = (! empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1194  $this->fk_project = '';
1195  $this->fk_delivery_address = '';
1196  }
1197 
1198  // TODO Change product price if multi-prices
1199  }
1200 
1201  $this->id = 0;
1202  $this->ref = '';
1203  $this->statut = 0;
1204 
1205  // Clear fields
1206  $this->user_author_id = $user->id;
1207  $this->user_valid = 0;
1208  $this->date_creation = '';
1209  $this->date_validation = '';
1210  $this->ref_client = '';
1211 
1212  // Create clone
1213  $this->context['createfromclone'] = 'createfromclone';
1214  $result = $this->create($user);
1215  if ($result < 0) {
1216  $error++;
1217  }
1218 
1219  if (!$error) {
1220  // Add lines because it is not included into create function
1221  foreach ($this->lines as $line) {
1222  $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration, $line->array_options);
1223  }
1224 
1225  // Hook of thirdparty module
1226  if (is_object($hookmanager)) {
1227  $parameters = array('objFrom'=>$objFrom);
1228  $action = '';
1229  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1230  if ($reshook < 0) {
1231  $error++;
1232  }
1233  }
1234  }
1235 
1236  unset($this->context['createfromclone']);
1237 
1238  // End
1239  if (!$error) {
1240  $this->db->commit();
1241  return $this->id;
1242  } else {
1243  $this->db->rollback();
1244  return -1;
1245  }
1246  }
1247 
1248 
1260  public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
1261  {
1262  dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
1263 
1264  if ($this->statut == 0) {
1265  $this->db->begin();
1266 
1267  // Insertion ligne
1268  $line = new FichinterLigne($this->db);
1269 
1270  $line->fk_fichinter = $fichinterid;
1271  $line->desc = $desc;
1272  $line->datei = $date_intervention;
1273  $line->duration = $duration;
1274 
1275  if (is_array($array_options) && count($array_options) > 0) {
1276  $line->array_options = $array_options;
1277  }
1278 
1279  $result = $line->insert($user);
1280 
1281  if ($result >= 0) {
1282  $this->db->commit();
1283  return 1;
1284  } else {
1285  $this->error = $this->db->error();
1286  $this->db->rollback();
1287  return -1;
1288  }
1289  }
1290  }
1291 
1292 
1300  public function initAsSpecimen()
1301  {
1302  global $user, $langs, $conf;
1303 
1304  $now = dol_now();
1305 
1306  // Initialise parametres
1307  $this->id = 0;
1308  $this->ref = 'SPECIMEN';
1309  $this->ref_client = 'SPECIMEN CLIENT';
1310  $this->specimen = 1;
1311  $this->socid = 1;
1312  $this->datec = $now;
1313  $this->note_private = 'Private note';
1314  $this->note_public = 'SPECIMEN';
1315  $this->duration = 0;
1316  $nbp = 25;
1317  $xnbp = 0;
1318  while ($xnbp < $nbp) {
1319  $line = new FichinterLigne($this->db);
1320  $line->desc = $langs->trans("Description")." ".$xnbp;
1321  $line->datei = ($now - 3600 * (1 + $xnbp));
1322  $line->duration = 600;
1323  $line->fk_fichinter = 0;
1324  $this->lines[$xnbp] = $line;
1325  $xnbp++;
1326 
1327  $this->duration += $line->duration;
1328  }
1329  }
1330 
1331  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1337  public function fetch_lines()
1338  {
1339  // phpcs:enable
1340  $this->lines = array();
1341 
1342  $sql = "SELECT rowid, fk_fichinter, description, duree, date, rang";
1343  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1344  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1345  $sql .= " ORDER BY rang ASC, date ASC";
1346 
1347  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1348 
1349  $resql = $this->db->query($sql);
1350  if ($resql) {
1351  $num = $this->db->num_rows($resql);
1352  $i = 0;
1353  while ($i < $num) {
1354  $objp = $this->db->fetch_object($resql);
1355 
1356  $line = new FichinterLigne($this->db);
1357  $line->id = $objp->rowid;
1358  $line->fk_fichinter = $objp->fk_fichinter;
1359  $line->desc = $objp->description;
1360  $line->duration = $objp->duree;
1361  //For invoicing we calculing hours
1362  $line->qty = round($objp->duree / 3600, 2);
1363  $line->date = $this->db->jdate($objp->date);
1364  $line->datei = $this->db->jdate($objp->date);
1365  $line->rang = $objp->rang;
1366  $line->product_type = 1;
1367  $line->fetch_optionals();
1368 
1369  $this->lines[$i] = $line;
1370  $i++;
1371  }
1372  $this->db->free($resql);
1373 
1374  return 1;
1375  } else {
1376  $this->error = $this->db->error();
1377  return -1;
1378  }
1379  }
1380 
1389  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1390  {
1391  $tables = array(
1392  'fichinter'
1393  );
1394 
1395  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1396  }
1397 
1406  public function setRefClient($user, $ref_client, $notrigger = 0)
1407  {
1408  // phpcs:enable
1409  if (!empty($user->rights->ficheinter->creer)) {
1410  $error = 0;
1411 
1412  $this->db->begin();
1413 
1414  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
1415  $sql .= " WHERE rowid = ".((int) $this->id);
1416 
1417  dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
1418  $resql = $this->db->query($sql);
1419  if (!$resql) {
1420  $this->errors[] = $this->db->error();
1421  $error++;
1422  }
1423 
1424  if (!$error) {
1425  $this->oldcopy = clone $this;
1426  $this->ref_client = $ref_client;
1427  }
1428 
1429  if (!$notrigger && empty($error)) {
1430  // Call trigger
1431  $result = $this->call_trigger('FICHINTER_MODIFY', $user);
1432  if ($result < 0) {
1433  $error++;
1434  }
1435  // End call triggers
1436  }
1437 
1438  if (!$error) {
1439  $this->db->commit();
1440  return 1;
1441  } else {
1442  foreach ($this->errors as $errmsg) {
1443  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
1444  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1445  }
1446  $this->db->rollback();
1447  return -1 * $error;
1448  }
1449  } else {
1450  return -1;
1451  }
1452  }
1453 }
1454 
1459 {
1463  public $db;
1464 
1468  public $error = '';
1469 
1470  // From llx_fichinterdet
1474  public $fk_fichinter;
1475 
1476  public $desc; // Description ligne
1477 
1481  public $date; // Date intervention
1486  public $datei; // Date intervention
1487 
1488  public $duration; // Duration of intervention
1489  public $rang = 0;
1490 
1494  public $element = 'fichinterdet';
1495 
1499  public $table_element = 'fichinterdet';
1500 
1504  public $fk_element = 'fk_fichinter';
1505 
1506 
1507 
1513  public function __construct($db)
1514  {
1515  $this->db = $db;
1516  }
1517 
1524  public function fetch($rowid)
1525  {
1526  $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,';
1527  $sql .= ' ft.date as datei';
1528  $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
1529  $sql .= ' WHERE ft.rowid = '.((int) $rowid);
1530 
1531  dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
1532  $result = $this->db->query($sql);
1533  if ($result) {
1534  $objp = $this->db->fetch_object($result);
1535  $this->rowid = $objp->rowid;
1536  $this->id = $objp->rowid;
1537  $this->fk_fichinter = $objp->fk_fichinter;
1538  $this->datei = $this->db->jdate($objp->datei);
1539  $this->desc = $objp->description;
1540  $this->duration = $objp->duree;
1541  $this->rang = $objp->rang;
1542 
1543  $this->db->free($result);
1544  return 1;
1545  } else {
1546  $this->error = $this->db->error().' sql='.$sql;
1547  return -1;
1548  }
1549  }
1550 
1558  public function insert($user, $notrigger = 0)
1559  {
1560  global $langs, $conf;
1561 
1562  $error = 0;
1563 
1564  dol_syslog("FichinterLigne::insert rang=".$this->rang);
1565 
1566  $this->db->begin();
1567 
1568  $rangToUse = $this->rang;
1569  if ($rangToUse == -1) {
1570  // Recupere rang max de la ligne d'intervention dans $rangmax
1571  $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
1572  $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
1573  $resql = $this->db->query($sql);
1574  if ($resql) {
1575  $obj = $this->db->fetch_object($resql);
1576  $rangToUse = $obj->max + 1;
1577  } else {
1578  dol_print_error($this->db);
1579  $this->db->rollback();
1580  return -1;
1581  }
1582  }
1583 
1584  // Insertion dans base de la ligne
1585  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
1586  $sql .= ' (fk_fichinter, description, date, duree, rang)';
1587  $sql .= " VALUES (".((int) $this->fk_fichinter).",";
1588  $sql .= " '".$this->db->escape($this->desc)."',";
1589  $sql .= " '".$this->db->idate($this->datei)."',";
1590  $sql .= " ".((int) $this->duration).",";
1591  $sql .= ' '.((int) $rangToUse);
1592  $sql .= ')';
1593 
1594  dol_syslog("FichinterLigne::insert", LOG_DEBUG);
1595  $resql = $this->db->query($sql);
1596  if ($resql) {
1597  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
1598  $this->rowid = $this->id;
1599 
1600  if (!$error) {
1601  $result = $this->insertExtraFields();
1602  if ($result < 0) {
1603  $error++;
1604  }
1605  }
1606 
1607 
1608  $result = $this->update_total();
1609 
1610  if ($result > 0) {
1611  $this->rang = $rangToUse;
1612 
1613  if (!$notrigger) {
1614  // Call trigger
1615  $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
1616  if ($result < 0) {
1617  $error++;
1618  }
1619  // End call triggers
1620  }
1621  }
1622 
1623  if (!$error) {
1624  $this->db->commit();
1625  return $result;
1626  } else {
1627  $this->db->rollback();
1628  return -1;
1629  }
1630  } else {
1631  $this->error = $this->db->error()." sql=".$sql;
1632  $this->db->rollback();
1633  return -1;
1634  }
1635  }
1636 
1637 
1645  public function update($user, $notrigger = 0)
1646  {
1647  global $langs, $conf;
1648 
1649  $error = 0;
1650 
1651  $this->db->begin();
1652 
1653  // Mise a jour ligne en base
1654  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
1655  $sql .= " description='".$this->db->escape($this->desc)."'";
1656  $sql .= ",date='".$this->db->idate($this->datei)."'";
1657  $sql .= ",duree=".$this->duration;
1658  $sql .= ",rang='".$this->db->escape($this->rang)."'";
1659  $sql .= " WHERE rowid = ".((int) $this->id);
1660 
1661  dol_syslog("FichinterLigne::update", LOG_DEBUG);
1662  $resql = $this->db->query($sql);
1663  if ($resql) {
1664  if (!$error) {
1665  $result = $this->insertExtraFields();
1666  if ($result < 0) {
1667  $error++;
1668  }
1669  }
1670 
1671  $result = $this->update_total();
1672  if ($result > 0) {
1673  if (!$notrigger) {
1674  // Call trigger
1675  $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
1676  if ($result < 0) {
1677  $error++;
1678  }
1679  // End call triggers
1680  }
1681  }
1682 
1683  if (!$error) {
1684  $this->db->commit();
1685  return $result;
1686  } else {
1687  $this->error = $this->db->lasterror();
1688  $this->db->rollback();
1689  return -1;
1690  }
1691  } else {
1692  $this->error = $this->db->lasterror();
1693  $this->db->rollback();
1694  return -1;
1695  }
1696  }
1697 
1698  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1704  public function update_total()
1705  {
1706  // phpcs:enable
1707  global $conf;
1708 
1709  $this->db->begin();
1710 
1711  $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
1712  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1713  $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
1714 
1715  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1716  $resql = $this->db->query($sql);
1717  if ($resql) {
1718  $obj = $this->db->fetch_object($resql);
1719  $total_duration = 0;
1720  if (!empty($obj->total_duration)) {
1721  $total_duration = $obj->total_duration;
1722  }
1723 
1724  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
1725  $sql .= " SET duree = ".((int) $total_duration);
1726  $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->idate($obj->dateo)."'" : "null");
1727  $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->idate($obj->datee)."'" : "null");
1728  $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
1729 
1730  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1731  $resql = $this->db->query($sql);
1732  if ($resql) {
1733  $this->db->commit();
1734  return 1;
1735  } else {
1736  $this->error = $this->db->error();
1737  $this->db->rollback();
1738  return -2;
1739  }
1740  } else {
1741  $this->error = $this->db->error();
1742  $this->db->rollback();
1743  return -1;
1744  }
1745  }
1746 
1754  public function deleteline($user, $notrigger = 0)
1755  {
1756  global $langs, $conf;
1757 
1758  $error = 0;
1759 
1760  if ($this->statut == 0) {
1761  dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
1762  $this->db->begin();
1763 
1764  $result = $this->deleteExtraFields();
1765  if ($result < 0) {
1766  $error++;
1767  $this->db->rollback();
1768  return -1;
1769  }
1770 
1771  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
1772  $resql = $this->db->query($sql);
1773 
1774  if ($resql) {
1775  $result = $this->update_total();
1776  if ($result > 0) {
1777  if (!$notrigger) {
1778  // Call trigger
1779  $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
1780  if ($result < 0) {
1781  $error++; $this->db->rollback(); return -1;
1782  }
1783  // End call triggers
1784  }
1785 
1786  $this->db->commit();
1787  return $result;
1788  } else {
1789  $this->db->rollback();
1790  return -1;
1791  }
1792  } else {
1793  $this->error = $this->db->error()." sql=".$sql;
1794  $this->db->rollback();
1795  return -1;
1796  }
1797  } else {
1798  return -2;
1799  }
1800  }
1801 }
getLibStatut($mode=0)
Returns the label status.
getAmount()
Returns amount based on user thm.
info($id)
Load information on object.
setDraft($user)
Set status to draft.
deleteline($user, $notrigger=0)
Delete a intervention line.
addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options= '')
Adding a line of intervention into data base.
create($user, $notrigger=0)
Create an intervention into data base.
$conf db
API class for accounts.
Definition: inc.php:41
Class to manage interventions.
dol_now($mode= 'auto')
Return date for now.
deleteObjectLinked($sourceid=null, $sourcetype= '', $targetid=null, $targettype= '', $rowid= '', $f_user=null, $notrigger=0)
Delete all links between an object $this.
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1434
delete_linked_contact($source= '', $code= '')
Delete all links between an object $this and all its contacts.
Class to manage Dolibarr users.
Definition: user.class.php:44
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Class to manage Dolibarr database access.
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
update($user, $notrigger=0)
Update an intervention.
const STATUS_DRAFT
Draft status.
set_date_delivery($user, $date_delivery)
Defines a delivery date of intervention.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
const STATUS_VALIDATED
Validated status.
getNextNumRef($soc)
Returns the next non used reference of intervention depending on the module numbering assets within F...
setRefClient($user, $ref_client, $notrigger=0)
Set customer reference number.
initAsSpecimen()
Initialise an instance with random values.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
__construct($db)
Constructor.
__construct($db)
Constructor.
Class to manage third parties objects (customers, suppliers, prospects...)
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
set_contrat($user, $contractid)
Link intervention to a contract.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1382
update($user, $notrigger=0)
Update intervention into database.
deleteEcmFiles($mode=0)
Delete related files of object in database.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
set_description($user, $description)
Define the label of the intervention.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
const STATUS_CLOSED
Closed.
fetch_lines()
Load array lines -&gt;lines.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
LibStatut($status, $mode=0)
Returns the label of a status.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setValid($user, $notrigger=0)
Validate a intervention.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage intervention lines.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add objects linked in llx_element_element.
insert($user, $notrigger=0)
Insert the line into database.
const STATUS_BILLED
Billed.
update_total()
Update total duration into llx_fichinter.
$object ref
Definition: info.php:77
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
fetch($rowid)
Retrieve the line of intervention.
load_state_board()
Load indicators into this-&gt;nb for board.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1230
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
getNomUrl($withpicto=0, $option= '', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
fetch($rowid, $ref= '')
Fetch a intervention.
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid