dolibarr  18.0.0
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  public $fields = array(
40  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
41  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'isModEnabled("societe")', 'visible'=>-1, 'notnull'=>1, 'position'=>15),
42  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label'=>'Fk projet', 'enabled'=>'isModEnabled("project")', 'visible'=>-1, 'position'=>20),
43  'fk_contrat' =>array('type'=>'integer', 'label'=>'Fk contrat', 'enabled'=>'$conf->contrat->enabled', 'visible'=>-1, 'position'=>25),
44  'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>30),
45  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),
46  'ref_client' =>array('type'=>'varchar(255)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>36),
47  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>40, 'index'=>1),
48  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>45),
49  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
50  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
51  'datei' =>array('type'=>'date', 'label'=>'Datei', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
52  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
53  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>70),
54  'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
55  'dateo' =>array('type'=>'date', 'label'=>'Dateo', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
56  'datee' =>array('type'=>'date', 'label'=>'Datee', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
57  'datet' =>array('type'=>'date', 'label'=>'Datet', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
58  'duree' =>array('type'=>'double', 'label'=>'Duree', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
59  'description' =>array('type'=>'html', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
60  'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
61  'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>115),
62  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>120),
63  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'Last main doc', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
64  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>130),
65  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
66  'fk_statut' =>array('type'=>'integer', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
67  );
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.last_main_doc, 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  $this->last_main_doc = $obj->last_main_doc;
486 
487  if ($this->statut == 0) {
488  $this->brouillon = 1;
489  }
490 
491  // Retrieve extrafields
492  $this->fetch_optionals();
493 
494  /*
495  * Lines
496  */
497  $result = $this->fetch_lines();
498  if ($result < 0) {
499  return -3;
500  }
501  $this->db->free($resql);
502  return 1;
503  }
504 
505  return 0;
506  } else {
507  $this->error = $this->db->lasterror();
508  return -1;
509  }
510  }
511 
518  public function setDraft($user)
519  {
520  $error = 0;
521 
522  // Protection
523  if ($this->statut <= self::STATUS_DRAFT) {
524  return 0;
525  }
526 
527  dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
528 
529  $this->oldcopy = dol_clone($this);
530 
531  $this->db->begin();
532 
533  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
534  $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
535  $sql .= " WHERE rowid = ".((int) $this->id);
536 
537  $resql = $this->db->query($sql);
538  if ($resql) {
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  return 0;
669  }
670 
676  public function getAmount()
677  {
678  global $db;
679 
680  $amount = 0;
681 
682  $this->author = new User($db);
683  $this->author->fetch($this->user_creation);
684 
685  $thm = $this->author->thm;
686 
687  foreach ($this->lines as $line) {
688  $amount += ($line->duration / 60 / 60 * $thm);
689  }
690 
691  return price2num($amount, 'MT');
692  }
693 
694 
706  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
707  {
708  global $conf;
709 
710  $outputlangs->load("interventions");
711 
712  if (!dol_strlen($modele)) {
713  $modele = 'soleil';
714 
715  if (!empty($this->model_pdf)) {
716  $modele = $this->model_pdf;
717  } elseif (!empty($this->modelpdf)) { // deprecated
718  $modele = $this->modelpdf;
719  } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
720  $modele = $conf->global->FICHEINTER_ADDON_PDF;
721  }
722  }
723 
724  $modelpath = "core/modules/fichinter/doc/";
725 
726  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
727  }
728 
735  public function getLibStatut($mode = 0)
736  {
737  return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
738  }
739 
740  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
748  public function LibStatut($status, $mode = 0)
749  {
750  // phpcs:enable
751  // Init/load array of translation of status
752  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
753  global $langs;
754  $langs->load("fichinter");
755 
756  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
757  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
758  $this->labelStatus[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
759  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
760  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
761  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
762  $this->labelStatusShort[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
763  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
764  }
765 
766  $statuscode = 'status'.$status;
767  if ($status == self::STATUS_BILLED || $status == self::STATUS_CLOSED) {
768  $statuscode = 'status6';
769  }
770  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statuscode, $mode);
771  }
772 
780  public function getTooltipContentArray($params)
781  {
782  global $conf, $langs;
783 
784  $langs->load('fichinter');
785 
786  $datas = [];
787  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
788  if (isset($this->status)) {
789  $datas['picto'] .= ' '.$this->getLibStatut(5);
790  }
791  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
792 
793  return $datas;
794  }
795 
806  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '')
807  {
808  global $conf, $langs, $hookmanager;
809 
810  if (!empty($conf->dol_no_mouse_hover)) {
811  $notooltip = 1; // Force disable tooltips
812  }
813 
814  $result = '';
815  $params = [
816  'id' => $this->id,
817  'objecttype' => $this->element,
818  'option' => $option,
819  ];
820  $classfortooltip = 'classfortooltip';
821  $dataparams = '';
822  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
823  $classfortooltip = 'classforajaxtooltip';
824  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
825  $label = '';
826  } else {
827  $label = implode($this->getTooltipContentArray($params));
828  }
829 
830  $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
831 
832  if ($option !== 'nolink') {
833  // Add param to save lastsearch_values or not
834  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
835  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
836  $add_save_lastsearch_values = 1;
837  }
838  if ($add_save_lastsearch_values) {
839  $url .= '&save_lastsearch_values=1';
840  }
841  }
842 
843  $linkclose = '';
844  if (empty($notooltip)) {
845  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
846  $label = $langs->trans("ShowIntervention");
847  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
848  }
849  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
850  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
851  } else {
852  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
853  }
854 
855  if ($option == 'nolink' || empty($url)) {
856  $linkstart = '<span';
857  } else {
858  $linkstart = '<a href="'.$url.'"';
859  }
860  $linkstart .= $linkclose.'>';
861  if ($option == 'nolink' || empty($url)) {
862  $linkend = '</span>';
863  } else {
864  $linkend = '</a>';
865  }
866 
867  $result .= $linkstart;
868  if ($withpicto) {
869  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
870  }
871 
872  if ($withpicto != 2) {
873  $result .= $this->ref;
874  }
875 
876  $result .= $linkend;
877 
878  global $action;
879  $hookmanager->initHooks(array('interventiondao'));
880  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
881  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
882  if ($reshook > 0) {
883  $result = $hookmanager->resPrint;
884  } else {
885  $result .= $hookmanager->resPrint;
886  }
887 
888  return $result;
889  }
890 
891 
899  public function getNextNumRef($soc)
900  {
901  global $conf, $db, $langs;
902  $langs->load("interventions");
903 
904  if (!empty($conf->global->FICHEINTER_ADDON)) {
905  $mybool = false;
906 
907  $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
908  $classname = "mod_".$conf->global->FICHEINTER_ADDON;
909 
910  // Include file with class
911  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
912 
913  foreach ($dirmodels as $reldir) {
914  $dir = dol_buildpath($reldir."core/modules/fichinter/");
915 
916  // Load file with numbering class (if found)
917  $mybool |= @include_once $dir.$file;
918  }
919 
920  if ($mybool === false) {
921  dol_print_error('', "Failed to include file ".$file);
922  return '';
923  }
924 
925  $obj = new $classname();
926  $numref = "";
927  $numref = $obj->getNextValue($soc, $this);
928 
929  if ($numref != "") {
930  return $numref;
931  } else {
932  dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
933  return "";
934  }
935  } else {
936  $langs->load("errors");
937  print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
938  return "";
939  }
940  }
941 
948  public function info($id)
949  {
950  $sql = "SELECT f.rowid,";
951  $sql .= " f.datec,";
952  $sql .= " f.tms as date_modification,";
953  $sql .= " f.date_valid as datev,";
954  $sql .= " f.fk_user_author,";
955  $sql .= " f.fk_user_modif as fk_user_modification,";
956  $sql .= " f.fk_user_valid";
957  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
958  $sql .= " WHERE f.rowid = ".((int) $id);
959 
960  $resql = $this->db->query($sql);
961  if ($resql) {
962  if ($this->db->num_rows($resql)) {
963  $obj = $this->db->fetch_object($resql);
964 
965  $this->id = $obj->rowid;
966 
967  $this->date_creation = $this->db->jdate($obj->datec);
968  $this->date_modification = $this->db->jdate($obj->date_modification);
969  $this->date_validation = $this->db->jdate($obj->datev);
970 
971  $cuser = new User($this->db);
972  $cuser->fetch($obj->fk_user_author);
973  $this->user_creation = $cuser;
974 
975  if ($obj->fk_user_valid) {
976  $vuser = new User($this->db);
977  $vuser->fetch($obj->fk_user_valid);
978  $this->user_validation = $vuser;
979  }
980  if ($obj->fk_user_modification) {
981  $muser = new User($this->db);
982  $muser->fetch($obj->fk_user_modification);
983  $this->user_modification = $muser;
984  }
985  }
986  $this->db->free($resql);
987  } else {
988  dol_print_error($this->db);
989  }
990  }
991 
999  public function delete(User $user, $notrigger = 0)
1000  {
1001  global $conf, $langs;
1002  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1003 
1004  $error = 0;
1005 
1006  $this->db->begin();
1007 
1008  if (!$error && !$notrigger) {
1009  // Call trigger
1010  $result = $this->call_trigger('FICHINTER_DELETE', $user);
1011  if ($result < 0) {
1012  $error++; $this->db->rollback(); return -1;
1013  }
1014  // End call triggers
1015  }
1016 
1017  // Delete linked object
1018  if (!$error) {
1019  $res = $this->deleteObjectLinked();
1020  if ($res < 0) {
1021  $error++;
1022  }
1023  }
1024 
1025  // Delete linked contacts
1026  if (!$error) {
1027  $res = $this->delete_linked_contact();
1028  if ($res < 0) {
1029  $this->error = 'ErrorFailToDeleteLinkedContact';
1030  $error++;
1031  }
1032  }
1033 
1034  if (!$error) {
1035  $main = MAIN_DB_PREFIX.'fichinterdet';
1036  $ef = $main."_extrafields";
1037  $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
1038 
1039  $resql = $this->db->query($sql);
1040  if (!$resql) {
1041  $error++;
1042  }
1043  }
1044 
1045  if (!$error) {
1046  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
1047  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1048 
1049  $resql = $this->db->query($sql);
1050  if (!$resql) {
1051  $error++;
1052  }
1053  }
1054 
1055  if (!$error) {
1056  // Remove extrafields
1057  $res = $this->deleteExtraFields();
1058  if ($res < 0) {
1059  $error++;
1060  }
1061  }
1062 
1063  if (!$error) {
1064  // Delete object
1065  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
1066  $sql .= " WHERE rowid = ".((int) $this->id);
1067 
1068  dol_syslog("Fichinter::delete", LOG_DEBUG);
1069  $resql = $this->db->query($sql);
1070  if (!$resql) {
1071  $error++;
1072  }
1073  }
1074 
1075  if (!$error) {
1076  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1077  $this->deleteEcmFiles();
1078 
1079  // Remove directory with files
1080  $fichinterref = dol_sanitizeFileName($this->ref);
1081  if ($conf->ficheinter->dir_output) {
1082  $dir = $conf->ficheinter->dir_output."/".$fichinterref;
1083  $file = $conf->ficheinter->dir_output."/".$fichinterref."/".$fichinterref.".pdf";
1084  if (file_exists($file)) {
1085  dol_delete_preview($this);
1086 
1087  if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
1088  $langs->load("errors");
1089  $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
1090  return 0;
1091  }
1092  }
1093  if (file_exists($dir)) {
1094  if (!dol_delete_dir_recursive($dir)) {
1095  $langs->load("errors");
1096  $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
1097  return 0;
1098  }
1099  }
1100  }
1101  }
1102 
1103  if (!$error) {
1104  $this->db->commit();
1105  return 1;
1106  } else {
1107  $this->db->rollback();
1108  return -1;
1109  }
1110  }
1111 
1112  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1120  public function set_date_delivery($user, $date_delivery)
1121  {
1122  // phpcs:enable
1123  if ($user->rights->ficheinter->creer) {
1124  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1125  $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
1126  $sql .= " WHERE rowid = ".((int) $this->id);
1127  $sql .= " AND fk_statut = 0";
1128 
1129  if ($this->db->query($sql)) {
1130  $this->date_delivery = $date_delivery;
1131  return 1;
1132  } else {
1133  $this->error = $this->db->error();
1134  dol_syslog("Fichinter::set_date_delivery Erreur SQL");
1135  return -1;
1136  }
1137  }
1138 
1139  return 0;
1140  }
1141 
1142  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1150  public function set_description($user, $description)
1151  {
1152  // phpcs:enable
1153  if ($user->rights->ficheinter->creer) {
1154  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1155  $sql .= " SET description = '".$this->db->escape($description)."',";
1156  $sql .= " fk_user_modif = ".$user->id;
1157  $sql .= " WHERE rowid = ".((int) $this->id);
1158 
1159  if ($this->db->query($sql)) {
1160  $this->description = $description;
1161  return 1;
1162  } else {
1163  $this->error = $this->db->error();
1164  dol_syslog("Fichinter::set_description Erreur SQL");
1165  return -1;
1166  }
1167  }
1168 
1169  return 0;
1170  }
1171 
1172 
1173  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1181  public function set_contrat($user, $contractid)
1182  {
1183  // phpcs:enable
1184  if ($user->rights->ficheinter->creer) {
1185  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1186  $sql .= " SET fk_contrat = ".((int) $contractid);
1187  $sql .= " WHERE rowid = ".((int) $this->id);
1188 
1189  if ($this->db->query($sql)) {
1190  $this->fk_contrat = $contractid;
1191  return 1;
1192  } else {
1193  $this->error = $this->db->error();
1194  return -1;
1195  }
1196  }
1197 
1198  return -2;
1199  }
1200 
1201 
1202 
1210  public function createFromClone(User $user, $socid = 0)
1211  {
1212  global $hookmanager;
1213 
1214  $error = 0;
1215 
1216  $this->db->begin();
1217 
1218  // get extrafields so they will be clone
1219  foreach ($this->lines as $line) {
1220  $line->fetch_optionals();
1221  }
1222 
1223  // Load source object
1224  $objFrom = clone $this;
1225 
1226  // Change socid if needed
1227  if (!empty($socid) && $socid != $this->socid) {
1228  $objsoc = new Societe($this->db);
1229 
1230  if ($objsoc->fetch($socid) > 0) {
1231  $this->socid = $objsoc->id;
1232  //$this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1233  //$this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1234  $this->fk_project = '';
1235  $this->fk_delivery_address = '';
1236  }
1237 
1238  // TODO Change product price if multi-prices
1239  }
1240 
1241  $this->id = 0;
1242  $this->ref = '';
1243  $this->statut = 0;
1244 
1245  // Clear fields
1246  $this->user_author_id = $user->id;
1247  $this->user_valid = 0;
1248  $this->date_creation = '';
1249  $this->date_validation = '';
1250  $this->ref_client = '';
1251 
1252  // Create clone
1253  $this->context['createfromclone'] = 'createfromclone';
1254  $result = $this->create($user);
1255  if ($result < 0) {
1256  $error++;
1257  }
1258 
1259  if (!$error) {
1260  // Add lines because it is not included into create function
1261  foreach ($this->lines as $line) {
1262  $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration, $line->array_options);
1263  }
1264 
1265  // Hook of thirdparty module
1266  if (is_object($hookmanager)) {
1267  $parameters = array('objFrom'=>$objFrom);
1268  $action = '';
1269  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1270  if ($reshook < 0) {
1271  $this->setErrorsFromObject($hookmanager);
1272  $error++;
1273  }
1274  }
1275  }
1276 
1277  unset($this->context['createfromclone']);
1278 
1279  // End
1280  if (!$error) {
1281  $this->db->commit();
1282  return $this->id;
1283  } else {
1284  $this->db->rollback();
1285  return -1;
1286  }
1287  }
1288 
1289 
1301  public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
1302  {
1303  dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
1304 
1305  if ($this->statut == self::STATUS_DRAFT) {
1306  $this->db->begin();
1307 
1308  // Insertion ligne
1309  $line = new FichinterLigne($this->db);
1310 
1311  $line->fk_fichinter = $fichinterid;
1312  $line->desc = $desc;
1313  $line->date = $date_intervention;
1314  $line->datei = $date_intervention; // For backward compatibility
1315  $line->duration = $duration;
1316 
1317  if (is_array($array_options) && count($array_options) > 0) {
1318  $line->array_options = $array_options;
1319  }
1320 
1321  $result = $line->insert($user);
1322 
1323  if ($result >= 0) {
1324  $this->db->commit();
1325  return 1;
1326  } else {
1327  $this->error = $this->db->error();
1328  $this->db->rollback();
1329  return -1;
1330  }
1331  }
1332 
1333  return 0;
1334  }
1335 
1336 
1344  public function initAsSpecimen()
1345  {
1346  global $langs;
1347 
1348  $now = dol_now();
1349 
1350  // Initialise parametres
1351  $this->id = 0;
1352  $this->ref = 'SPECIMEN';
1353  $this->ref_client = 'SPECIMEN CLIENT';
1354  $this->specimen = 1;
1355  $this->socid = 1;
1356  $this->datec = $now;
1357  $this->note_private = 'Private note';
1358  $this->note_public = 'SPECIMEN';
1359  $this->duration = 0;
1360  $nbp = 25;
1361  $xnbp = 0;
1362  while ($xnbp < $nbp) {
1363  $line = new FichinterLigne($this->db);
1364  $line->desc = $langs->trans("Description")." ".$xnbp;
1365  $line->date = ($now - 3600 * (1 + $xnbp));
1366  $line->datei = ($now - 3600 * (1 + $xnbp)); // For backward compatibility
1367  $line->duration = 600;
1368  $line->fk_fichinter = 0;
1369  $this->lines[$xnbp] = $line;
1370  $xnbp++;
1371 
1372  $this->duration += $line->duration;
1373  }
1374  }
1375 
1376  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1382  public function fetch_lines()
1383  {
1384  // phpcs:enable
1385  $this->lines = array();
1386 
1387  $sql = "SELECT rowid, fk_fichinter, description, duree, date, rang";
1388  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1389  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1390  $sql .= " ORDER BY rang ASC, date ASC";
1391 
1392  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1393 
1394  $resql = $this->db->query($sql);
1395  if ($resql) {
1396  $num = $this->db->num_rows($resql);
1397  $i = 0;
1398  while ($i < $num) {
1399  $objp = $this->db->fetch_object($resql);
1400 
1401  $line = new FichinterLigne($this->db);
1402  $line->id = $objp->rowid;
1403  $line->fk_fichinter = $objp->fk_fichinter;
1404  $line->desc = $objp->description;
1405  $line->duration = $objp->duree;
1406  //For invoicing we calculing hours
1407  $line->qty = round($objp->duree / 3600, 2);
1408  $line->date = $this->db->jdate($objp->date);
1409  $line->datei = $this->db->jdate($objp->date); // For backward compatibility
1410  $line->rang = $objp->rang;
1411  $line->product_type = 1;
1412  $line->fetch_optionals();
1413 
1414  $this->lines[$i] = $line;
1415  $i++;
1416  }
1417  $this->db->free($resql);
1418 
1419  return 1;
1420  } else {
1421  $this->error = $this->db->error();
1422  return -1;
1423  }
1424  }
1425 
1434  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1435  {
1436  $tables = array(
1437  'fichinter'
1438  );
1439 
1440  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1441  }
1442 
1451  public function setRefClient($user, $ref_client, $notrigger = 0)
1452  {
1453  // phpcs:enable
1454  if (!empty($user->rights->ficheinter->creer)) {
1455  $error = 0;
1456 
1457  $this->db->begin();
1458 
1459  $this->oldcopy = dol_clone($this);
1460 
1461  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
1462  $sql .= " WHERE rowid = ".((int) $this->id);
1463 
1464  dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
1465  $resql = $this->db->query($sql);
1466  if (!$resql) {
1467  $this->errors[] = $this->db->error();
1468  $error++;
1469  }
1470 
1471  if (!$error) {
1472  $this->ref_client = $ref_client;
1473  }
1474 
1475  if (!$notrigger && empty($error)) {
1476  // Call trigger
1477  $result = $this->call_trigger('FICHINTER_MODIFY', $user);
1478  if ($result < 0) {
1479  $error++;
1480  }
1481  // End call triggers
1482  }
1483 
1484  if (!$error) {
1485  $this->db->commit();
1486  return 1;
1487  } else {
1488  foreach ($this->errors as $errmsg) {
1489  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
1490  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1491  }
1492  $this->db->rollback();
1493  return -1 * $error;
1494  }
1495  } else {
1496  return -1;
1497  }
1498  }
1499 
1507  public function getKanbanView($option = '', $arraydata = null)
1508  {
1509  global $langs;
1510 
1511  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1512 
1513  $return = '<div class="box-flex-item box-flex-grow-zero">';
1514  $return .= '<div class="info-box info-box-sm">';
1515  $return .= '<span class="info-box-icon bg-infobox-action">';
1516  $return .= img_picto('', $this->picto);
1517  $return .= '</span>';
1518  $return .= '<div class="info-box-content">';
1519  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1520  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1521  if (!empty($arraydata['thirdparty'])) {
1522  $tmpthirdparty = $arraydata['thirdparty'];
1523  $return .= '<br><span class="info-box-label">'.$tmpthirdparty->getNomUrl(1).'</span>';
1524  }
1525  if (property_exists($this, 'duration')) {
1526  $return .= '<br><span class="info-box-label ">'.$langs->trans("Duration").' : '.convertSecondToTime($this->duration, 'allhourmin').'</span>';
1527  }
1528  if (method_exists($this, 'getLibStatut')) {
1529  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1530  }
1531  $return .= '</div>';
1532  $return .= '</div>';
1533  $return .= '</div>';
1534  return $return;
1535  }
1536 }
1537 
1542 {
1546  public $db;
1547 
1551  public $error = '';
1552 
1553  // From llx_fichinterdet
1557  public $fk_fichinter;
1558 
1559  public $desc; // Description ligne
1560 
1564  public $date; // Date intervention
1569  public $datei; // Date intervention
1570 
1571  public $duration; // Duration of intervention
1572  public $rang = 0;
1573  public $tva_tx;
1574  public $subprice;
1575 
1579  public $element = 'fichinterdet';
1580 
1584  public $table_element = 'fichinterdet';
1585 
1589  public $fk_element = 'fk_fichinter';
1590 
1591 
1592 
1598  public function __construct($db)
1599  {
1600  $this->db = $db;
1601  }
1602 
1609  public function fetch($rowid)
1610  {
1611  dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
1612 
1613  $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date';
1614  $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
1615  $sql .= ' WHERE ft.rowid = '.((int) $rowid);
1616 
1617  $resql = $this->db->query($sql);
1618  if ($resql) {
1619  $objp = $this->db->fetch_object($resql);
1620  $this->rowid = $objp->rowid;
1621  $this->id = $objp->rowid;
1622  $this->fk_fichinter = $objp->fk_fichinter;
1623  $this->date = $this->db->jdate($objp->date);
1624  $this->datei = $this->db->jdate($objp->date); // For backward compatibility
1625  $this->desc = $objp->description;
1626  $this->duration = $objp->duree;
1627  $this->rang = $objp->rang;
1628 
1629  $this->db->free($resql);
1630 
1631  $this->fetch_optionals();
1632 
1633  return 1;
1634  } else {
1635  $this->error = $this->db->error().' sql='.$sql;
1636  return -1;
1637  }
1638  }
1639 
1647  public function insert($user, $notrigger = 0)
1648  {
1649  $error = 0;
1650 
1651  dol_syslog("FichinterLigne::insert rang=".$this->rang);
1652 
1653  if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
1654  $this->date = $this->datei;
1655  }
1656 
1657  $this->db->begin();
1658 
1659  $rangToUse = $this->rang;
1660  if ($rangToUse == -1) {
1661  // Recupere rang max de la ligne d'intervention dans $rangmax
1662  $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
1663  $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
1664  $resql = $this->db->query($sql);
1665  if ($resql) {
1666  $obj = $this->db->fetch_object($resql);
1667  $rangToUse = $obj->max + 1;
1668  } else {
1669  dol_print_error($this->db);
1670  $this->db->rollback();
1671  return -1;
1672  }
1673  }
1674 
1675  // Insertion dans base de la ligne
1676  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
1677  $sql .= ' (fk_fichinter, description, date, duree, rang)';
1678  $sql .= " VALUES (".((int) $this->fk_fichinter).",";
1679  $sql .= " '".$this->db->escape($this->desc)."',";
1680  $sql .= " '".$this->db->idate($this->date)."',";
1681  $sql .= " ".((int) $this->duration).",";
1682  $sql .= ' '.((int) $rangToUse);
1683  $sql .= ')';
1684 
1685  dol_syslog("FichinterLigne::insert", LOG_DEBUG);
1686  $resql = $this->db->query($sql);
1687  if ($resql) {
1688  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
1689  $this->rowid = $this->id;
1690 
1691  if (!$error) {
1692  $result = $this->insertExtraFields();
1693  if ($result < 0) {
1694  $error++;
1695  }
1696  }
1697 
1698 
1699  $result = $this->update_total();
1700 
1701  if ($result > 0) {
1702  $this->rang = $rangToUse;
1703 
1704  if (!$notrigger) {
1705  // Call trigger
1706  $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
1707  if ($result < 0) {
1708  $error++;
1709  }
1710  // End call triggers
1711  }
1712  }
1713 
1714  if (!$error) {
1715  $this->db->commit();
1716  return $result;
1717  } else {
1718  $this->db->rollback();
1719  return -1;
1720  }
1721  } else {
1722  $this->error = $this->db->error()." sql=".$sql;
1723  $this->db->rollback();
1724  return -1;
1725  }
1726  }
1727 
1728 
1736  public function update($user, $notrigger = 0)
1737  {
1738  $error = 0;
1739 
1740  if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
1741  $this->date = $this->datei;
1742  }
1743 
1744  $this->db->begin();
1745 
1746  // Mise a jour ligne en base
1747  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
1748  $sql .= " description = '".$this->db->escape($this->desc)."',";
1749  $sql .= " date = '".$this->db->idate($this->date)."',";
1750  $sql .= " duree = ".((int) $this->duration).",";
1751  $sql .= " rang = ".((int) $this->rang);
1752  $sql .= " WHERE rowid = ".((int) $this->id);
1753 
1754  dol_syslog("FichinterLigne::update", LOG_DEBUG);
1755  $resql = $this->db->query($sql);
1756  if ($resql) {
1757  if (!$error) {
1758  $result = $this->insertExtraFields();
1759  if ($result < 0) {
1760  $error++;
1761  }
1762  }
1763 
1764  $result = $this->update_total();
1765  if ($result > 0) {
1766  if (!$notrigger) {
1767  // Call trigger
1768  $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
1769  if ($result < 0) {
1770  $error++;
1771  }
1772  // End call triggers
1773  }
1774  }
1775 
1776  if (!$error) {
1777  $this->db->commit();
1778  return $result;
1779  } else {
1780  $this->error = $this->db->lasterror();
1781  $this->db->rollback();
1782  return -1;
1783  }
1784  } else {
1785  $this->error = $this->db->lasterror();
1786  $this->db->rollback();
1787  return -1;
1788  }
1789  }
1790 
1791  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1797  public function update_total()
1798  {
1799  // phpcs:enable
1800  global $conf;
1801 
1802  $this->db->begin();
1803 
1804  $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
1805  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1806  $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
1807 
1808  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1809  $resql = $this->db->query($sql);
1810  if ($resql) {
1811  $obj = $this->db->fetch_object($resql);
1812  $total_duration = 0;
1813  if (!empty($obj->total_duration)) {
1814  $total_duration = $obj->total_duration;
1815  }
1816 
1817  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
1818  $sql .= " SET duree = ".((int) $total_duration);
1819  $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
1820  $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
1821  $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
1822 
1823  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1824  $resql = $this->db->query($sql);
1825  if ($resql) {
1826  $this->db->commit();
1827  return 1;
1828  } else {
1829  $this->error = $this->db->error();
1830  $this->db->rollback();
1831  return -2;
1832  }
1833  } else {
1834  $this->error = $this->db->error();
1835  $this->db->rollback();
1836  return -1;
1837  }
1838  }
1839 
1847  public function deleteline($user, $notrigger = 0)
1848  {
1849  global $langs, $conf;
1850 
1851  $error = 0;
1852 
1853  if ($this->statut == 0) {
1854  dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
1855  $this->db->begin();
1856 
1857  $result = $this->deleteExtraFields();
1858  if ($result < 0) {
1859  $error++;
1860  $this->db->rollback();
1861  return -1;
1862  }
1863 
1864  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
1865  $resql = $this->db->query($sql);
1866 
1867  if ($resql) {
1868  $result = $this->update_total();
1869  if ($result > 0) {
1870  if (!$notrigger) {
1871  // Call trigger
1872  $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
1873  if ($result < 0) {
1874  $error++; $this->db->rollback(); return -1;
1875  }
1876  // End call triggers
1877  }
1878 
1879  $this->db->commit();
1880  return $result;
1881  } else {
1882  $this->db->rollback();
1883  return -1;
1884  }
1885  } else {
1886  $this->error = $this->db->error()." sql=".$sql;
1887  $this->db->rollback();
1888  return -1;
1889  }
1890  } else {
1891  return -2;
1892  }
1893  }
1894 }
Fichinter\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1, $morecss='')
Return clicable name (with picto eventually)
Definition: fichinter.class.php:806
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:51
Fichinter\set_description
set_description($user, $description)
Define the label of the intervention.
Definition: fichinter.class.php:1150
FichinterLigne\update_total
update_total()
Update total duration into llx_fichinter.
Definition: fichinter.class.php:1797
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1323
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
dol_delete_dir_recursive
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:1485
Fichinter\STATUS_CLOSED
const STATUS_CLOSED
Closed.
Definition: fichinter.class.php:184
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Fichinter\fetch_lines
fetch_lines()
Load array lines ->lines.
Definition: fichinter.class.php:1382
Fichinter\LibStatut
LibStatut($status, $mode=0)
Returns the label of a status.
Definition: fichinter.class.php:748
FichinterLigne\fetch
fetch($rowid)
Retrieve the line of intervention.
Definition: fichinter.class.php:1609
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5107
CommonObject\setErrorsFromObject
setErrorsFromObject($object)
setErrorsFromObject
Definition: commonobject.class.php:731
FichinterLigne\update
update($user, $notrigger=0)
Update intervention into database.
Definition: fichinter.class.php:1736
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1158
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:62
Fichinter\STATUS_BILLED
const STATUS_BILLED
Billed.
Definition: fichinter.class.php:179
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4170
Fichinter\setValid
setValid($user, $notrigger=0)
Validate a intervention.
Definition: fichinter.class.php:569
dol_clone
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
Definition: functions.lib.php:1255
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:45
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5955
Fichinter\load_state_board
load_state_board()
Load indicators into this->nb for board.
Definition: fichinter.class.php:203
convertSecondToTime
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition: date.lib.php:239
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4135
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5361
FichinterLigne\insert
insert($user, $notrigger=0)
Insert the line into database.
Definition: fichinter.class.php:1647
dol_delete_file
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:1334
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:266
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6137
CommonObject\isExistingObject
static isExistingObject($element, $id, $ref='', $ref_ext='')
Check an object id/ref exists If you don't need/want to instantiate object and just need to know if o...
Definition: commonobject.class.php:689
CommonObject\delete_linked_contact
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
Definition: commonobject.class.php:1210
Fichinter\getLibStatut
getLibStatut($mode=0)
Returns the label status.
Definition: fichinter.class.php:735
FichinterLigne
Class to manage intervention lines.
Definition: fichinter.class.php:1541
CommonObject\deleteEcmFiles
deleteEcmFiles($mode=0)
Delete related files of object in database.
Definition: commonobject.class.php:10135
Fichinter\getAmount
getAmount()
Returns amount based on user thm.
Definition: fichinter.class.php:676
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
Fichinter\create
create($user, $notrigger=0)
Create an intervention into data base.
Definition: fichinter.class.php:242
Fichinter\getTooltipContentArray
getTooltipContentArray($params)
getTooltipContentArray
Definition: fichinter.class.php:780
Fichinter\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: fichinter.class.php:1434
Fichinter\info
info($id)
Load information on object.
Definition: fichinter.class.php:948
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:5986
Fichinter\setDraft
setDraft($user)
Set status to draft.
Definition: fichinter.class.php:518
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
Fichinter\addline
addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options='')
Adding a line of intervention into data base.
Definition: fichinter.class.php:1301
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3997
Fichinter
Class to manage interventions.
Definition: fichinter.class.php:37
Fichinter\createFromClone
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
Definition: fichinter.class.php:1210
ref
$object ref
Definition: info.php:78
User
Class to manage Dolibarr users.
Definition: user.class.php:47
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6097
Fichinter\fetch
fetch($rowid, $ref='')
Fetch a intervention.
Definition: fichinter.class.php:439
Fichinter\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: fichinter.class.php:169
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1537
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3760
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10967
Fichinter\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: fichinter.class.php:706
Fichinter\$table_ref_field
$table_ref_field
{}
Definition: fichinter.class.php:97
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4473
Fichinter\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: fichinter.class.php:174
Fichinter\update
update($user, $notrigger=0)
Update an intervention.
Definition: fichinter.class.php:377
Fichinter\getNextNumRef
getNextNumRef($soc)
Returns the next non used reference of intervention depending on the module numbering assets within F...
Definition: fichinter.class.php:899
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3056
Fichinter\setRefClient
setRefClient($user, $ref_client, $notrigger=0)
Set customer reference number.
Definition: fichinter.class.php:1451
Fichinter\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: fichinter.class.php:1344
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5770
Fichinter\getKanbanView
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: fichinter.class.php:1507
Fichinter\set_date_delivery
set_date_delivery($user, $date_delivery)
Defines a delivery date of intervention.
Definition: fichinter.class.php:1120
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:156
Fichinter\__construct
__construct($db)
Constructor.
Definition: fichinter.class.php:192
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8504
Fichinter\set_contrat
set_contrat($user, $contractid)
Link intervention to a contract.
Definition: fichinter.class.php:1181
FichinterLigne\deleteline
deleteline($user, $notrigger=0)
Delete a intervention line.
Definition: fichinter.class.php:1847
FichinterLigne\__construct
__construct($db)
Constructor.
Definition: fichinter.class.php:1598