dolibarr  17.0.4
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'=>'$conf->societe->enabled', '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  'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
56  'dateo' =>array('type'=>'date', 'label'=>'Dateo', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
57  'datee' =>array('type'=>'date', 'label'=>'Datee', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
58  'datet' =>array('type'=>'date', 'label'=>'Datet', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
59  'duree' =>array('type'=>'double', 'label'=>'Duree', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
60  'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
61  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
62  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>115),
63  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>120),
64  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'Last main doc', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
65  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>130),
66  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
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  } else {
505  $this->error = $this->db->lasterror();
506  return -1;
507  }
508  }
509 
516  public function setDraft($user)
517  {
518  global $langs, $conf;
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) $this->entity);
596 
597  $sql .= " AND fk_statut = 0";
598 
599  dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
600  $resql = $this->db->query($sql);
601  if (!$resql) {
602  dol_print_error($this->db);
603  $error++;
604  }
605 
606  if (!$error && !$notrigger) {
607  // Call trigger
608  $result = $this->call_trigger('FICHINTER_VALIDATE', $user);
609  if ($result < 0) {
610  $error++;
611  }
612  // End call triggers
613  }
614 
615  if (!$error) {
616  $this->oldref = $this->ref;
617 
618  // Rename directory if dir was a temporary ref
619  if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
620  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
621 
622  // Now we rename also files into index
623  $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)."'";
624  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".((int) $this->entity);
625  $resql = $this->db->query($sql);
626  if (!$resql) {
627  $error++; $this->error = $this->db->lasterror();
628  }
629 
630  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
631  $oldref = dol_sanitizeFileName($this->ref);
632  $newref = dol_sanitizeFileName($num);
633  $dirsource = $conf->ficheinter->dir_output.'/'.$oldref;
634  $dirdest = $conf->ficheinter->dir_output.'/'.$newref;
635  if (!$error && file_exists($dirsource)) {
636  dol_syslog(get_class($this)."::setValid rename dir ".$dirsource." into ".$dirdest);
637 
638  if (@rename($dirsource, $dirdest)) {
639  dol_syslog("Rename ok");
640  // Rename docs starting with $oldref with $newref
641  $listoffiles = dol_dir_list($conf->ficheinter->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
642  foreach ($listoffiles as $fileentry) {
643  $dirsource = $fileentry['name'];
644  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
645  $dirsource = $fileentry['path'].'/'.$dirsource;
646  $dirdest = $fileentry['path'].'/'.$dirdest;
647  @rename($dirsource, $dirdest);
648  }
649  }
650  }
651  }
652  }
653 
654  // Set new ref and define current statut
655  if (!$error) {
656  $this->ref = $num;
657  $this->statut = 1;
658  $this->brouillon = 0;
659  $this->date_validation = $now;
660  $this->db->commit();
661  return 1;
662  } else {
663  $this->db->rollback();
664  dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
665  return -1;
666  }
667  }
668  }
669 
675  public function getAmount()
676  {
677  global $db;
678 
679  $amount = 0;
680 
681  $this->author = new User($db);
682  $this->author->fetch($this->user_creation);
683 
684  $thm = $this->author->thm;
685 
686  foreach ($this->lines as $line) {
687  $amount += ($line->duration / 60 / 60 * $thm);
688  }
689 
690  return price2num($amount, 'MT');
691  }
692 
693 
705  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
706  {
707  global $conf;
708 
709  $outputlangs->load("interventions");
710 
711  if (!dol_strlen($modele)) {
712  $modele = 'soleil';
713 
714  if (!empty($this->model_pdf)) {
715  $modele = $this->model_pdf;
716  } elseif (!empty($this->modelpdf)) { // deprecated
717  $modele = $this->modelpdf;
718  } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
719  $modele = $conf->global->FICHEINTER_ADDON_PDF;
720  }
721  }
722 
723  $modelpath = "core/modules/fichinter/doc/";
724 
725  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
726  }
727 
734  public function getLibStatut($mode = 0)
735  {
736  return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
737  }
738 
739  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
747  public function LibStatut($status, $mode = 0)
748  {
749  // phpcs:enable
750  // Init/load array of translation of status
751  if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) {
752  global $langs;
753  $langs->load("fichinter");
754 
755  $this->statuts[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
756  $this->statuts[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
757  $this->statuts[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
758  $this->statuts[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
759  $this->statuts_short[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
760  $this->statuts_short[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
761  $this->statuts_short[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
762  $this->statuts_short[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
763  $this->statuts_logo[self::STATUS_DRAFT] = 'status0';
764  $this->statuts_logo[self::STATUS_VALIDATED] = 'status1';
765  $this->statuts_logo[self::STATUS_BILLED] = 'status6';
766  $this->statuts_logo[self::STATUS_CLOSED] = 'status6';
767  }
768 
769  return dolGetStatus($this->statuts[$status], $this->statuts_short[$status], '', $this->statuts_logo[$status], $mode);
770  }
771 
781  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1)
782  {
783  global $conf, $langs, $hookmanager;
784 
785  $result = '';
786 
787  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
788  if (isset($this->status)) {
789  $label .= ' '.$this->getLibStatut(5);
790  }
791  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
792 
793  $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
794 
795  if ($option !== 'nolink') {
796  // Add param to save lastsearch_values or not
797  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
798  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
799  $add_save_lastsearch_values = 1;
800  }
801  if ($add_save_lastsearch_values) {
802  $url .= '&save_lastsearch_values=1';
803  }
804  }
805 
806  $linkclose = '';
807  if (empty($notooltip)) {
808  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
809  $label = $langs->trans("ShowIntervention");
810  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
811  }
812  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
813  $linkclose .= ' class="classfortooltip"';
814 
815  /*
816  $hookmanager->initHooks(array('fichinterdao'));
817  $parameters=array('id'=>$this->id);
818  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
819  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
820  */
821  }
822 
823  $linkstart = '<a href="'.$url.'"';
824  $linkstart .= $linkclose.'>';
825  $linkend = '</a>';
826 
827  $result .= $linkstart;
828  if ($withpicto) {
829  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
830  }
831  if ($withpicto != 2) {
832  $result .= $this->ref;
833  }
834  $result .= $linkend;
835 
836  global $action;
837  $hookmanager->initHooks(array('interventiondao'));
838  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
839  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
840  if ($reshook > 0) {
841  $result = $hookmanager->resPrint;
842  } else {
843  $result .= $hookmanager->resPrint;
844  }
845 
846  return $result;
847  }
848 
849 
857  public function getNextNumRef($soc)
858  {
859  global $conf, $db, $langs;
860  $langs->load("interventions");
861 
862  if (!empty($conf->global->FICHEINTER_ADDON)) {
863  $mybool = false;
864 
865  $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
866  $classname = "mod_".$conf->global->FICHEINTER_ADDON;
867 
868  // Include file with class
869  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
870 
871  foreach ($dirmodels as $reldir) {
872  $dir = dol_buildpath($reldir."core/modules/fichinter/");
873 
874  // Load file with numbering class (if found)
875  $mybool |= @include_once $dir.$file;
876  }
877 
878  if ($mybool === false) {
879  dol_print_error('', "Failed to include file ".$file);
880  return '';
881  }
882 
883  $obj = new $classname();
884  $numref = "";
885  $numref = $obj->getNextValue($soc, $this);
886 
887  if ($numref != "") {
888  return $numref;
889  } else {
890  dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
891  return "";
892  }
893  } else {
894  $langs->load("errors");
895  print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
896  return "";
897  }
898  }
899 
906  public function info($id)
907  {
908  global $conf;
909 
910  $sql = "SELECT f.rowid,";
911  $sql .= " f.datec,";
912  $sql .= " f.tms as date_modification,";
913  $sql .= " f.date_valid as datev,";
914  $sql .= " f.fk_user_author,";
915  $sql .= " f.fk_user_modif as fk_user_modification,";
916  $sql .= " f.fk_user_valid";
917  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
918  $sql .= " WHERE f.rowid = ".((int) $id);
919 
920  $resql = $this->db->query($sql);
921  if ($resql) {
922  if ($this->db->num_rows($resql)) {
923  $obj = $this->db->fetch_object($resql);
924 
925  $this->id = $obj->rowid;
926 
927  $this->date_creation = $this->db->jdate($obj->datec);
928  $this->date_modification = $this->db->jdate($obj->date_modification);
929  $this->date_validation = $this->db->jdate($obj->datev);
930 
931  $cuser = new User($this->db);
932  $cuser->fetch($obj->fk_user_author);
933  $this->user_creation = $cuser;
934 
935  if ($obj->fk_user_valid) {
936  $vuser = new User($this->db);
937  $vuser->fetch($obj->fk_user_valid);
938  $this->user_validation = $vuser;
939  }
940  if ($obj->fk_user_modification) {
941  $muser = new User($this->db);
942  $muser->fetch($obj->fk_user_modification);
943  $this->user_modification = $muser;
944  }
945  }
946  $this->db->free($resql);
947  } else {
948  dol_print_error($this->db);
949  }
950  }
951 
959  public function delete($user, $notrigger = 0)
960  {
961  global $conf, $langs;
962  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
963 
964  $error = 0;
965 
966  $this->db->begin();
967 
968  if (!$error && !$notrigger) {
969  // Call trigger
970  $result = $this->call_trigger('FICHINTER_DELETE', $user);
971  if ($result < 0) {
972  $error++; $this->db->rollback(); return -1;
973  }
974  // End call triggers
975  }
976 
977  // Delete linked object
978  if (!$error) {
979  $res = $this->deleteObjectLinked();
980  if ($res < 0) {
981  $error++;
982  }
983  }
984 
985  // Delete linked contacts
986  if (!$error) {
987  $res = $this->delete_linked_contact();
988  if ($res < 0) {
989  $this->error = 'ErrorFailToDeleteLinkedContact';
990  $error++;
991  }
992  }
993 
994  if (!$error) {
995  $main = MAIN_DB_PREFIX.'fichinterdet';
996  $ef = $main."_extrafields";
997  $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
998 
999  $resql = $this->db->query($sql);
1000  if (!$resql) {
1001  $error++;
1002  }
1003  }
1004 
1005  if (!$error) {
1006  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
1007  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1008 
1009  $resql = $this->db->query($sql);
1010  if (!$resql) {
1011  $error++;
1012  }
1013  }
1014 
1015  if (!$error) {
1016  // Remove extrafields
1017  $res = $this->deleteExtraFields();
1018  if ($res < 0) {
1019  $error++;
1020  }
1021  }
1022 
1023  if (!$error) {
1024  // Delete object
1025  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
1026  $sql .= " WHERE rowid = ".((int) $this->id);
1027 
1028  dol_syslog("Fichinter::delete", LOG_DEBUG);
1029  $resql = $this->db->query($sql);
1030  if (!$resql) {
1031  $error++;
1032  }
1033  }
1034 
1035  if (!$error) {
1036  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1037  $this->deleteEcmFiles();
1038 
1039  // Remove directory with files
1040  $fichinterref = dol_sanitizeFileName($this->ref);
1041  if ($conf->ficheinter->dir_output) {
1042  $dir = $conf->ficheinter->dir_output."/".$fichinterref;
1043  $file = $conf->ficheinter->dir_output."/".$fichinterref."/".$fichinterref.".pdf";
1044  if (file_exists($file)) {
1045  dol_delete_preview($this);
1046 
1047  if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
1048  $langs->load("errors");
1049  $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
1050  return 0;
1051  }
1052  }
1053  if (file_exists($dir)) {
1054  if (!dol_delete_dir_recursive($dir)) {
1055  $langs->load("errors");
1056  $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
1057  return 0;
1058  }
1059  }
1060  }
1061  }
1062 
1063  if (!$error) {
1064  $this->db->commit();
1065  return 1;
1066  } else {
1067  $this->db->rollback();
1068  return -1;
1069  }
1070  }
1071 
1072  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1080  public function set_date_delivery($user, $date_delivery)
1081  {
1082  // phpcs:enable
1083  global $conf;
1084 
1085  if ($user->rights->ficheinter->creer) {
1086  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1087  $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
1088  $sql .= " WHERE rowid = ".((int) $this->id);
1089  $sql .= " AND fk_statut = 0";
1090 
1091  if ($this->db->query($sql)) {
1092  $this->date_delivery = $date_delivery;
1093  return 1;
1094  } else {
1095  $this->error = $this->db->error();
1096  dol_syslog("Fichinter::set_date_delivery Erreur SQL");
1097  return -1;
1098  }
1099  }
1100  }
1101 
1102  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1110  public function set_description($user, $description)
1111  {
1112  // phpcs:enable
1113  global $conf;
1114 
1115  if ($user->rights->ficheinter->creer) {
1116  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1117  $sql .= " SET description = '".$this->db->escape($description)."',";
1118  $sql .= " fk_user_modif = ".$user->id;
1119  $sql .= " WHERE rowid = ".((int) $this->id);
1120 
1121  if ($this->db->query($sql)) {
1122  $this->description = $description;
1123  return 1;
1124  } else {
1125  $this->error = $this->db->error();
1126  dol_syslog("Fichinter::set_description Erreur SQL");
1127  return -1;
1128  }
1129  }
1130  }
1131 
1132 
1133  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1141  public function set_contrat($user, $contractid)
1142  {
1143  // phpcs:enable
1144  global $conf;
1145 
1146  if ($user->rights->ficheinter->creer) {
1147  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
1148  $sql .= " SET fk_contrat = ".((int) $contractid);
1149  $sql .= " WHERE rowid = ".((int) $this->id);
1150 
1151  if ($this->db->query($sql)) {
1152  $this->fk_contrat = $contractid;
1153  return 1;
1154  } else {
1155  $this->error = $this->db->error();
1156  return -1;
1157  }
1158  }
1159  return -2;
1160  }
1161 
1162 
1163 
1171  public function createFromClone(User $user, $socid = 0)
1172  {
1173  global $hookmanager;
1174 
1175  $error = 0;
1176 
1177  $this->db->begin();
1178 
1179  // get extrafields so they will be clone
1180  foreach ($this->lines as $line) {
1181  $line->fetch_optionals();
1182  }
1183 
1184  // Load source object
1185  $objFrom = clone $this;
1186 
1187  // Change socid if needed
1188  if (!empty($socid) && $socid != $this->socid) {
1189  $objsoc = new Societe($this->db);
1190 
1191  if ($objsoc->fetch($socid) > 0) {
1192  $this->socid = $objsoc->id;
1193  //$this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1194  //$this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1195  $this->fk_project = '';
1196  $this->fk_delivery_address = '';
1197  }
1198 
1199  // TODO Change product price if multi-prices
1200  }
1201 
1202  $this->id = 0;
1203  $this->ref = '';
1204  $this->statut = 0;
1205 
1206  // Clear fields
1207  $this->user_author_id = $user->id;
1208  $this->user_valid = 0;
1209  $this->date_creation = '';
1210  $this->date_validation = '';
1211  $this->ref_client = '';
1212 
1213  // Create clone
1214  $this->context['createfromclone'] = 'createfromclone';
1215  $result = $this->create($user);
1216  if ($result < 0) {
1217  $error++;
1218  }
1219 
1220  if (!$error) {
1221  // Add lines because it is not included into create function
1222  foreach ($this->lines as $line) {
1223  $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration, $line->array_options);
1224  }
1225 
1226  // Hook of thirdparty module
1227  if (is_object($hookmanager)) {
1228  $parameters = array('objFrom'=>$objFrom);
1229  $action = '';
1230  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1231  if ($reshook < 0) {
1232  $this->errors += $hookmanager->errors;
1233  $this->error = $hookmanager->error;
1234  $error++;
1235  }
1236  }
1237  }
1238 
1239  unset($this->context['createfromclone']);
1240 
1241  // End
1242  if (!$error) {
1243  $this->db->commit();
1244  return $this->id;
1245  } else {
1246  $this->db->rollback();
1247  return -1;
1248  }
1249  }
1250 
1251 
1263  public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
1264  {
1265  dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
1266 
1267  if ($this->statut == 0) {
1268  $this->db->begin();
1269 
1270  // Insertion ligne
1271  $line = new FichinterLigne($this->db);
1272 
1273  $line->fk_fichinter = $fichinterid;
1274  $line->desc = $desc;
1275  $line->date = $date_intervention;
1276  $line->datei = $date_intervention; // For backward compatibility
1277  $line->duration = $duration;
1278 
1279  if (is_array($array_options) && count($array_options) > 0) {
1280  $line->array_options = $array_options;
1281  }
1282 
1283  $result = $line->insert($user);
1284 
1285  if ($result >= 0) {
1286  $this->db->commit();
1287  return 1;
1288  } else {
1289  $this->error = $this->db->error();
1290  $this->db->rollback();
1291  return -1;
1292  }
1293  }
1294  }
1295 
1296 
1304  public function initAsSpecimen()
1305  {
1306  global $user, $langs, $conf;
1307 
1308  $now = dol_now();
1309 
1310  // Initialise parametres
1311  $this->id = 0;
1312  $this->ref = 'SPECIMEN';
1313  $this->ref_client = 'SPECIMEN CLIENT';
1314  $this->specimen = 1;
1315  $this->socid = 1;
1316  $this->datec = $now;
1317  $this->note_private = 'Private note';
1318  $this->note_public = 'SPECIMEN';
1319  $this->duration = 0;
1320  $nbp = 25;
1321  $xnbp = 0;
1322  while ($xnbp < $nbp) {
1323  $line = new FichinterLigne($this->db);
1324  $line->desc = $langs->trans("Description")." ".$xnbp;
1325  $line->date = ($now - 3600 * (1 + $xnbp));
1326  $line->datei = ($now - 3600 * (1 + $xnbp)); // For backward compatibility
1327  $line->duration = 600;
1328  $line->fk_fichinter = 0;
1329  $this->lines[$xnbp] = $line;
1330  $xnbp++;
1331 
1332  $this->duration += $line->duration;
1333  }
1334  }
1335 
1336  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1342  public function fetch_lines()
1343  {
1344  // phpcs:enable
1345  $this->lines = array();
1346 
1347  $sql = "SELECT rowid, fk_fichinter, description, duree, date, rang";
1348  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1349  $sql .= " WHERE fk_fichinter = ".((int) $this->id);
1350  $sql .= " ORDER BY rang ASC, date ASC";
1351 
1352  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1353 
1354  $resql = $this->db->query($sql);
1355  if ($resql) {
1356  $num = $this->db->num_rows($resql);
1357  $i = 0;
1358  while ($i < $num) {
1359  $objp = $this->db->fetch_object($resql);
1360 
1361  $line = new FichinterLigne($this->db);
1362  $line->id = $objp->rowid;
1363  $line->fk_fichinter = $objp->fk_fichinter;
1364  $line->desc = $objp->description;
1365  $line->duration = $objp->duree;
1366  //For invoicing we calculing hours
1367  $line->qty = round($objp->duree / 3600, 2);
1368  $line->date = $this->db->jdate($objp->date);
1369  $line->datei = $this->db->jdate($objp->date); // For backward compatibility
1370  $line->rang = $objp->rang;
1371  $line->product_type = 1;
1372  $line->fetch_optionals();
1373 
1374  $this->lines[$i] = $line;
1375  $i++;
1376  }
1377  $this->db->free($resql);
1378 
1379  return 1;
1380  } else {
1381  $this->error = $this->db->error();
1382  return -1;
1383  }
1384  }
1385 
1394  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1395  {
1396  $tables = array(
1397  'fichinter'
1398  );
1399 
1400  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1401  }
1402 
1411  public function setRefClient($user, $ref_client, $notrigger = 0)
1412  {
1413  // phpcs:enable
1414  if (!empty($user->rights->ficheinter->creer)) {
1415  $error = 0;
1416 
1417  $this->db->begin();
1418 
1419  $this->oldcopy = dol_clone($this);
1420 
1421  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
1422  $sql .= " WHERE rowid = ".((int) $this->id);
1423 
1424  dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
1425  $resql = $this->db->query($sql);
1426  if (!$resql) {
1427  $this->errors[] = $this->db->error();
1428  $error++;
1429  }
1430 
1431  if (!$error) {
1432  $this->ref_client = $ref_client;
1433  }
1434 
1435  if (!$notrigger && empty($error)) {
1436  // Call trigger
1437  $result = $this->call_trigger('FICHINTER_MODIFY', $user);
1438  if ($result < 0) {
1439  $error++;
1440  }
1441  // End call triggers
1442  }
1443 
1444  if (!$error) {
1445  $this->db->commit();
1446  return 1;
1447  } else {
1448  foreach ($this->errors as $errmsg) {
1449  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
1450  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1451  }
1452  $this->db->rollback();
1453  return -1 * $error;
1454  }
1455  } else {
1456  return -1;
1457  }
1458  }
1459 }
1460 
1465 {
1469  public $db;
1470 
1474  public $error = '';
1475 
1476  // From llx_fichinterdet
1480  public $fk_fichinter;
1481 
1482  public $desc; // Description ligne
1483 
1487  public $date; // Date intervention
1492  public $datei; // Date intervention
1493 
1494  public $duration; // Duration of intervention
1495  public $rang = 0;
1496  public $tva_tx;
1497  public $subprice;
1498 
1502  public $element = 'fichinterdet';
1503 
1507  public $table_element = 'fichinterdet';
1508 
1512  public $fk_element = 'fk_fichinter';
1513 
1514 
1515 
1521  public function __construct($db)
1522  {
1523  $this->db = $db;
1524  }
1525 
1532  public function fetch($rowid)
1533  {
1534  $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date';
1535  $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
1536  $sql .= ' WHERE ft.rowid = '.((int) $rowid);
1537 
1538  dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
1539  $result = $this->db->query($sql);
1540  if ($result) {
1541  $objp = $this->db->fetch_object($result);
1542  $this->rowid = $objp->rowid;
1543  $this->id = $objp->rowid;
1544  $this->fk_fichinter = $objp->fk_fichinter;
1545  $this->date = $this->db->jdate($objp->date);
1546  $this->datei = $this->db->jdate($objp->date); // For backward compatibility
1547  $this->desc = $objp->description;
1548  $this->duration = $objp->duree;
1549  $this->rang = $objp->rang;
1550 
1551  $this->db->free($result);
1552  return 1;
1553  } else {
1554  $this->error = $this->db->error().' sql='.$sql;
1555  return -1;
1556  }
1557  }
1558 
1566  public function insert($user, $notrigger = 0)
1567  {
1568  global $langs, $conf;
1569 
1570  $error = 0;
1571 
1572  dol_syslog("FichinterLigne::insert rang=".$this->rang);
1573 
1574  if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
1575  $this->date = $this->datei;
1576  }
1577 
1578  $this->db->begin();
1579 
1580  $rangToUse = $this->rang;
1581  if ($rangToUse == -1) {
1582  // Recupere rang max de la ligne d'intervention dans $rangmax
1583  $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
1584  $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
1585  $resql = $this->db->query($sql);
1586  if ($resql) {
1587  $obj = $this->db->fetch_object($resql);
1588  $rangToUse = $obj->max + 1;
1589  } else {
1590  dol_print_error($this->db);
1591  $this->db->rollback();
1592  return -1;
1593  }
1594  }
1595 
1596  // Insertion dans base de la ligne
1597  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
1598  $sql .= ' (fk_fichinter, description, date, duree, rang)';
1599  $sql .= " VALUES (".((int) $this->fk_fichinter).",";
1600  $sql .= " '".$this->db->escape($this->desc)."',";
1601  $sql .= " '".$this->db->idate($this->date)."',";
1602  $sql .= " ".((int) $this->duration).",";
1603  $sql .= ' '.((int) $rangToUse);
1604  $sql .= ')';
1605 
1606  dol_syslog("FichinterLigne::insert", LOG_DEBUG);
1607  $resql = $this->db->query($sql);
1608  if ($resql) {
1609  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
1610  $this->rowid = $this->id;
1611 
1612  if (!$error) {
1613  $result = $this->insertExtraFields();
1614  if ($result < 0) {
1615  $error++;
1616  }
1617  }
1618 
1619 
1620  $result = $this->update_total();
1621 
1622  if ($result > 0) {
1623  $this->rang = $rangToUse;
1624 
1625  if (!$notrigger) {
1626  // Call trigger
1627  $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
1628  if ($result < 0) {
1629  $error++;
1630  }
1631  // End call triggers
1632  }
1633  }
1634 
1635  if (!$error) {
1636  $this->db->commit();
1637  return $result;
1638  } else {
1639  $this->db->rollback();
1640  return -1;
1641  }
1642  } else {
1643  $this->error = $this->db->error()." sql=".$sql;
1644  $this->db->rollback();
1645  return -1;
1646  }
1647  }
1648 
1649 
1657  public function update($user, $notrigger = 0)
1658  {
1659  global $langs, $conf;
1660 
1661  $error = 0;
1662 
1663  if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
1664  $this->date = $this->datei;
1665  }
1666 
1667  $this->db->begin();
1668 
1669  // Mise a jour ligne en base
1670  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
1671  $sql .= " description = '".$this->db->escape($this->desc)."',";
1672  $sql .= " date = '".$this->db->idate($this->date)."',";
1673  $sql .= " duree = ".((int) $this->duration).",";
1674  $sql .= " rang = ".((int) $this->rang);
1675  $sql .= " WHERE rowid = ".((int) $this->id);
1676 
1677  dol_syslog("FichinterLigne::update", LOG_DEBUG);
1678  $resql = $this->db->query($sql);
1679  if ($resql) {
1680  if (!$error) {
1681  $result = $this->insertExtraFields();
1682  if ($result < 0) {
1683  $error++;
1684  }
1685  }
1686 
1687  $result = $this->update_total();
1688  if ($result > 0) {
1689  if (!$notrigger) {
1690  // Call trigger
1691  $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
1692  if ($result < 0) {
1693  $error++;
1694  }
1695  // End call triggers
1696  }
1697  }
1698 
1699  if (!$error) {
1700  $this->db->commit();
1701  return $result;
1702  } else {
1703  $this->error = $this->db->lasterror();
1704  $this->db->rollback();
1705  return -1;
1706  }
1707  } else {
1708  $this->error = $this->db->lasterror();
1709  $this->db->rollback();
1710  return -1;
1711  }
1712  }
1713 
1714  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1720  public function update_total()
1721  {
1722  // phpcs:enable
1723  global $conf;
1724 
1725  $this->db->begin();
1726 
1727  $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
1728  $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
1729  $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
1730 
1731  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1732  $resql = $this->db->query($sql);
1733  if ($resql) {
1734  $obj = $this->db->fetch_object($resql);
1735  $total_duration = 0;
1736  if (!empty($obj->total_duration)) {
1737  $total_duration = $obj->total_duration;
1738  }
1739 
1740  $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
1741  $sql .= " SET duree = ".((int) $total_duration);
1742  $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
1743  $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
1744  $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
1745 
1746  dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
1747  $resql = $this->db->query($sql);
1748  if ($resql) {
1749  $this->db->commit();
1750  return 1;
1751  } else {
1752  $this->error = $this->db->error();
1753  $this->db->rollback();
1754  return -2;
1755  }
1756  } else {
1757  $this->error = $this->db->error();
1758  $this->db->rollback();
1759  return -1;
1760  }
1761  }
1762 
1770  public function deleteline($user, $notrigger = 0)
1771  {
1772  global $langs, $conf;
1773 
1774  $error = 0;
1775 
1776  if ($this->statut == 0) {
1777  dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
1778  $this->db->begin();
1779 
1780  $result = $this->deleteExtraFields();
1781  if ($result < 0) {
1782  $error++;
1783  $this->db->rollback();
1784  return -1;
1785  }
1786 
1787  $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
1788  $resql = $this->db->query($sql);
1789 
1790  if ($resql) {
1791  $result = $this->update_total();
1792  if ($result > 0) {
1793  if (!$notrigger) {
1794  // Call trigger
1795  $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
1796  if ($result < 0) {
1797  $error++; $this->db->rollback(); return -1;
1798  }
1799  // End call triggers
1800  }
1801 
1802  $this->db->commit();
1803  return $result;
1804  } else {
1805  $this->db->rollback();
1806  return -1;
1807  }
1808  } else {
1809  $this->error = $this->db->error()." sql=".$sql;
1810  $this->db->rollback();
1811  return -1;
1812  }
1813  } else {
1814  return -2;
1815  }
1816  }
1817 }
$object ref
Definition: info.php:78
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
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...
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage Dolibarr database access.
Class to manage interventions.
const STATUS_BILLED
Billed.
addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options='')
Adding a line of intervention into data base.
initAsSpecimen()
Initialise an instance with random values.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getLibStatut($mode=0)
Returns the label status.
update($user, $notrigger=0)
Update an intervention.
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
set_date_delivery($user, $date_delivery)
Defines a delivery date of intervention.
const STATUS_DRAFT
Draft status.
create($user, $notrigger=0)
Create an intervention into data base.
set_description($user, $description)
Define the label of the intervention.
const STATUS_VALIDATED
Validated status.
const STATUS_CLOSED
Closed.
setDraft($user)
Set status to draft.
getNextNumRef($soc)
Returns the next non used reference of intervention depending on the module numbering assets within F...
getAmount()
Returns amount based on user thm.
fetch($rowid, $ref='')
Fetch a intervention.
setRefClient($user, $ref_client, $notrigger=0)
Set customer reference number.
set_contrat($user, $contractid)
Link intervention to a contract.
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
LibStatut($status, $mode=0)
Returns the label of a status.
setValid($user, $notrigger=0)
Validate a intervention.
load_state_board()
Load indicators into this->nb for board.
__construct($db)
Constructor.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
info($id)
Load information on object.
fetch_lines()
Load array lines ->lines.
Class to manage intervention lines.
fetch($rowid)
Retrieve the line of intervention.
update_total()
Update total duration into llx_fichinter.
update($user, $notrigger=0)
Update intervention into database.
deleteline($user, $notrigger=0)
Delete a intervention line.
__construct($db)
Constructor.
insert($user, $notrigger=0)
Insert the line into database.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
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:1402
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:1251
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:61
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1454
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db
API class for accounts.
Definition: inc.php:41