dolibarr  21.0.0-alpha
contrat.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2012 Destailleur Laurent <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
6  * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
7  * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
8  * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
9  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
11  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
12  * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
13  * Copyright (C) 2015-2018 Ferran Marcet <fmarcet@2byte.es>
14  * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
15  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <https://www.gnu.org/licenses/>.
29  */
30 
37 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38 require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
39 require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
40 require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
41 
45 class Contrat extends CommonObject
46 {
50  public $element = 'contrat';
51 
55  public $table_element = 'contrat';
56 
60  public $table_element_line = 'contratdet';
61 
65  public $fk_element = 'fk_contrat';
66 
70  public $picto = 'contract';
71 
76  public $restrictiononfksoc = 1;
77 
81  protected $table_ref_field = 'ref';
82 
87  public $ref_customer;
88  public $from;
89 
94  public $ref_supplier;
95 
100  public $entity;
101 
106  public $socid;
107 
113  public $fk_soc;
114 
115 
116  public $societe; // Object societe
117 
123  public $statut = 0;
128  public $status = 0;
129 
130  public $product;
131 
135  public $fk_user_author;
136 
142  public $user_author_id;
143 
144 
149  public $user_creation;
150 
154  public $user_cloture;
155 
159  public $date_contrat;
160 
165  public $signed_status = 0;
166 
167  public $commercial_signature_id;
168  public $fk_commercial_signature;
169  public $commercial_suivi_id;
170  public $fk_commercial_suivi;
171 
176  public $fk_projet;
177 
178  public $extraparams = array();
179 
183  public $lines = array();
184 
185  public $nbofservices;
186  public $nbofserviceswait;
187  public $nbofservicesopened;
188  public $nbofservicesexpired;
189  public $nbofservicesclosed;
190  //public $lower_planned_end_date;
191  //public $higher_planner_end_date;
192 
197  protected $lines_id_index_mapper = array();
198 
199 
224  // BEGIN MODULEBUILDER PROPERTIES
228  public $fields = array(
229  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
230  'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
231  'ref_ext' => array('type' => 'varchar(255)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 0, 'position' => 20),
232  'ref_customer' => array('type' => 'varchar(50)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => -1, 'position' => 25, 'searchall' => 1),
233  'ref_supplier' => array('type' => 'varchar(50)', 'label' => 'RefSupplier', 'enabled' => 1, 'visible' => -1, 'position' => 26, 'searchall' => 1),
234  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
235  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
236  'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 40),
237  'date_contrat' => array('type' => 'datetime', 'label' => 'Date contrat', 'enabled' => 1, 'visible' => -1, 'position' => 45),
238  'signed_status' => array('type' => 'smallint(6)', 'label' => 'SignedStatus', 'enabled' => 1, 'visible' => -1, 'position' => 50, 'arrayofkeyval' => array(0 => 'NoSignature', 1 => 'SignedSender', 2 => 'SignedReceiver', 9 => 'SignedAll')),
239  'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 70),
240  'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 75),
241  'fk_commercial_signature' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative Signature', 'enabled' => 1, 'visible' => -1, 'position' => 80),
242  'fk_commercial_suivi' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative follower', 'enabled' => 1, 'visible' => -1, 'position' => 85),
243  'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 90),
244  'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105, 'searchall' => 1),
245  'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110, 'searchall' => 1),
246  'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 115),
247  'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 120),
248  'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 125),
249  'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
250  'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'Last main doc', 'enabled' => 1, 'visible' => -1, 'position' => 140),
251  'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'notnull' => 1, 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed'))
252  );
253  // END MODULEBUILDER PROPERTIES
254 
255  const STATUS_DRAFT = 0;
256  const STATUS_VALIDATED = 1;
257  const STATUS_CLOSED = 2;
258 
259  /*
260  * No signature
261  */
262  const STATUS_NO_SIGNATURE = 0;
263 
264  /*
265  * Signed by sender
266  */
267  const STATUS_SIGNED_SENDER = 1;
268 
269  /*
270  * Signed by receiver
271  */
272  const STATUS_SIGNED_RECEIVER = 2;
273 
274  /*
275  * Signed by all
276  */
277  const STATUS_SIGNED_ALL = 9; // To handle future kind of signature (ex: tripartite contract)
278 
279 
285  public function __construct($db)
286  {
287  $this->db = $db;
288 
289  $this->ismultientitymanaged = 1;
290  $this->isextrafieldmanaged = 1;
291  }
292 
299  public function getNextNumRef($soc)
300  {
301  global $db, $langs, $conf;
302  $langs->load("contracts");
303 
304  if (getDolGlobalString('CONTRACT_ADDON')) {
305  $mybool = false;
306 
307  $file = getDolGlobalString('CONTRACT_ADDON') . ".php";
308  $classname = getDolGlobalString('CONTRACT_ADDON');
309 
310  // Include file with class
311  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
312 
313  foreach ($dirmodels as $reldir) {
314  $dir = dol_buildpath($reldir."core/modules/contract/");
315 
316  // Load file with numbering class (if found)
317  $mybool = ((bool) @include_once $dir.$file) || $mybool;
318  }
319 
320  if (!$mybool) {
321  dol_print_error(null, "Failed to include file ".$file);
322  return '';
323  }
324 
325  $obj = new $classname();
326  '@phan-var-force CommonNumRefGenerator $obj';
327  $numref = $obj->getNextValue($soc, $this);
328 
329  if ($numref != "") {
330  return $numref;
331  } else {
332  $this->error = $obj->error;
333  dol_print_error($db, get_class($this)."::getNextValue ".$obj->error);
334  return "";
335  }
336  } else {
337  $langs->load("errors");
338  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Contract"));
339  return "";
340  }
341  }
342 
343  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
354  public function active_line($user, $line_id, $date_start, $date_end = '', $comment = '')
355  {
356  // phpcs:enable
357  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
358  if ($result < 0) {
359  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
360  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
361  }
362  return $result;
363  }
364 
365 
366  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
376  public function close_line($user, $line_id, $date_end, $comment = '')
377  {
378  // phpcs:enable
379  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
380  if ($result < 0) {
381  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
382  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
383  }
384  return $result;
385  }
386 
387 
399  public function activateAll($user, $date_start = '', $notrigger = 0, $comment = '', $date_end = '')
400  {
401  if (empty($date_start)) {
402  $date_start = dol_now();
403  }
404 
405  $this->db->begin();
406 
407  $error = 0;
408 
409  // Load lines
410  $this->fetch_lines();
411 
412  foreach ($this->lines as $contratline) {
413  // Open lines not already open
414  if ($contratline->statut != ContratLigne::STATUS_OPEN) {
415  $contratline->context = $this->context;
416 
417  $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment); // This call trigger LINECONTRACT_ACTIVATE
418  if ($result < 0) {
419  $error++;
420  $this->error = $contratline->error;
421  $this->errors = $contratline->errors;
422  break;
423  }
424  }
425  }
426 
427  if (!$error && $this->statut == 0) {
428  $result = $this->validate($user, '', $notrigger);
429  if ($result < 0) {
430  $error++;
431  }
432  }
433 
434  if (!$error) {
435  $this->db->commit();
436  return 1;
437  } else {
438  $this->db->rollback();
439  return -1;
440  }
441  }
442 
452  public function closeAll(User $user, $notrigger = 0, $comment = '')
453  {
454  $this->db->begin();
455 
456  // Load lines
457  $this->fetch_lines();
458 
459  $now = dol_now();
460 
461  $error = 0;
462 
463  foreach ($this->lines as $contratline) {
464  // Close lines not already closed
465  if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
466  $contratline->date_end_real = $now;
467  $contratline->date_cloture = $now; // For backward compatibility
468  $contratline->user_closing_id = $user->id;
469  $contratline->statut = ContratLigne::STATUS_CLOSED;
470  $result = $contratline->close_line($user, $now, $comment, $notrigger);
471  if ($result < 0) {
472  $error++;
473  $this->error = $contratline->error;
474  $this->errors = $contratline->errors;
475  break;
476  }
477  }
478  }
479 
480  if (!$error && $this->statut == 0) {
481  $result = $this->validate($user, '', $notrigger);
482  if ($result < 0) {
483  $error++;
484  }
485  }
486 
487  if (!$error) {
488  $this->db->commit();
489  return 1;
490  } else {
491  $this->db->rollback();
492  return -1;
493  }
494  }
495 
504  public function validate(User $user, $force_number = '', $notrigger = 0)
505  {
506  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
507  global $conf;
508 
509  $now = dol_now();
510 
511  $error = 0;
512  dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number);
513 
514 
515  $this->db->begin();
516 
517  $this->fetch_thirdparty();
518 
519  // A contract is validated so we can move thirdparty to status customer
520  if (!getDolGlobalString('CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION') && $this->thirdparty->fournisseur == 0) {
521  $result = $this->thirdparty->setAsCustomer();
522  }
523 
524  // Define new ref
525  if ($force_number) {
526  $num = $force_number;
527  } elseif (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
528  $num = $this->getNextNumRef($this->thirdparty);
529  } else {
530  $num = $this->ref;
531  }
532  $this->newref = dol_sanitizeFileName($num);
533 
534  if ($num) {
535  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$this->db->escape($num)."', statut = 1";
536  //$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
537  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 0";
538 
539  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
540  $resql = $this->db->query($sql);
541  if (!$resql) {
542  dol_print_error($this->db);
543  $error++;
544  $this->error = $this->db->lasterror();
545  }
546 
547  // Trigger calls
548  if (!$error && !$notrigger) {
549  // Call trigger
550  $result = $this->call_trigger('CONTRACT_VALIDATE', $user);
551  if ($result < 0) {
552  $error++;
553  }
554  // End call triggers
555  }
556 
557  if (!$error) {
558  $this->oldref = $this->ref;
559 
560  // Rename directory if dir was a temporary ref
561  if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
562  // Now we rename also files into index
563  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'contract/".$this->db->escape($this->newref)."'";
564  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
565  $resql = $this->db->query($sql);
566  if (!$resql) {
567  $error++;
568  $this->error = $this->db->lasterror();
569  }
570  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'contract/".$this->db->escape($this->newref)."'";
571  $sql .= " WHERE filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
572  $resql = $this->db->query($sql);
573  if (!$resql) {
574  $error++;
575  $this->error = $this->db->lasterror();
576  }
577 
578  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
579  $oldref = dol_sanitizeFileName($this->ref);
580  $newref = dol_sanitizeFileName($num);
581  $dirsource = $conf->contract->dir_output.'/'.$oldref;
582  $dirdest = $conf->contract->dir_output.'/'.$newref;
583  if (!$error && file_exists($dirsource)) {
584  dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
585 
586  if (@rename($dirsource, $dirdest)) {
587  dol_syslog("Rename ok");
588  // Rename docs starting with $oldref with $newref
589  $listoffiles = dol_dir_list($conf->contract->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
590  foreach ($listoffiles as $fileentry) {
591  $dirsource = $fileentry['name'];
592  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
593  $dirsource = $fileentry['path'].'/'.$dirsource;
594  $dirdest = $fileentry['path'].'/'.$dirdest;
595  @rename($dirsource, $dirdest);
596  }
597  }
598  }
599  }
600  }
601 
602  // Set new ref and define current statut
603  if (!$error) {
604  $this->ref = $num;
605  $this->status = self::STATUS_VALIDATED;
606  $this->statut = self::STATUS_VALIDATED; // deprecated
607  $this->date_validation = $now;
608  }
609  } else {
610  $error++;
611  }
612 
613  if (!$error) {
614  $this->db->commit();
615  return 1;
616  } else {
617  $this->db->rollback();
618  return -1;
619  }
620  }
621 
629  public function reopen($user, $notrigger = 0)
630  {
631  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
632 
633  $now = dol_now();
634 
635  $error = 0;
636  dol_syslog(get_class($this).'::reopen user='.$user->id);
637 
638  $this->db->begin();
639 
640  $this->fetch_thirdparty();
641 
642  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0";
643  //$sql.= ", fk_user_valid = null, date_valid = null";
644  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 1";
645 
646  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
647  $resql = $this->db->query($sql);
648  if (!$resql) {
649  dol_print_error($this->db);
650  $error++;
651  $this->error = $this->db->lasterror();
652  }
653 
654  // Trigger calls
655  if (!$error && !$notrigger) {
656  // Call trigger
657  $result = $this->call_trigger('CONTRACT_REOPEN', $user);
658  if ($result < 0) {
659  $error++;
660  }
661  // End call triggers
662  }
663 
664  // Set new ref and define current status
665  if (!$error) {
666  $this->statut = self::STATUS_DRAFT;
667  $this->status = self::STATUS_DRAFT;
668  $this->date_validation = $now;
669  }
670 
671  if (!$error) {
672  $this->db->commit();
673  return 1;
674  } else {
675  $this->db->rollback();
676  return -1;
677  }
678  }
679 
691  public function fetch($id, $ref = '', $ref_customer = '', $ref_supplier = '', $noextrafields = 0, $nolines = 0)
692  {
693  $sql = "SELECT rowid, statut as status, ref, fk_soc as thirdpartyid,";
694  $sql .= " ref_supplier, ref_customer,";
695  $sql .= " ref_ext,";
696  $sql .= " entity,";
697  $sql .= " date_contrat as datecontrat,";
698  $sql .= " fk_user_author,";
699  $sql .= " fk_projet as fk_project,";
700  $sql .= " fk_commercial_signature, fk_commercial_suivi,";
701  $sql .= " note_private, note_public, model_pdf, last_main_doc, extraparams";
702  $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
703  if (!$id) {
704  $sql .= " WHERE entity IN (".getEntity('contract').")";
705  } else {
706  $sql .= " WHERE rowid = ".(int) $id;
707  }
708  if ($ref_customer) {
709  $sql .= " AND ref_customer = '".$this->db->escape($ref_customer)."'";
710  }
711  if ($ref_supplier) {
712  $sql .= " AND ref_supplier = '".$this->db->escape($ref_supplier)."'";
713  }
714  if ($ref) {
715  $sql .= " AND ref = '".$this->db->escape($ref)."'";
716  }
717 
718  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
719  $resql = $this->db->query($sql);
720  if ($resql) {
721  $num = $this->db->num_rows($resql);
722  if ($num > 1) {
723  $this->error = 'Fetch found several records.';
724  dol_syslog($this->error, LOG_ERR);
725  $result = -2;
726  return 0;
727  } elseif ($num) { // $num = 1
728  $obj = $this->db->fetch_object($resql);
729  if ($obj) {
730  $this->id = $obj->rowid;
731  $this->ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
732  $this->ref_customer = $obj->ref_customer;
733  $this->ref_supplier = $obj->ref_supplier;
734  $this->ref_ext = $obj->ref_ext;
735  $this->entity = $obj->entity;
736  $this->statut = $obj->status;
737  $this->status = $obj->status;
738 
739  $this->date_contrat = $this->db->jdate($obj->datecontrat);
740  $this->date_creation = $this->db->jdate($obj->datecontrat);
741 
742  $this->user_author_id = $obj->fk_user_author;
743 
744  $this->commercial_signature_id = $obj->fk_commercial_signature;
745  $this->commercial_suivi_id = $obj->fk_commercial_suivi;
746 
747  $this->note_private = $obj->note_private;
748  $this->note_public = $obj->note_public;
749  $this->model_pdf = $obj->model_pdf;
750 
751  $this->fk_projet = $obj->fk_project; // deprecated
752  $this->fk_project = $obj->fk_project;
753 
754  $this->socid = $obj->thirdpartyid;
755  $this->fk_soc = $obj->thirdpartyid;
756  $this->last_main_doc = $obj->last_main_doc;
757  $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : null);
758 
759  $this->db->free($resql);
760 
761  // Retrieve all extrafields
762  // fetch optionals attributes and labels
763  if (empty($noextrafields)) {
764  $result = $this->fetch_optionals();
765  if ($result < 0) {
766  $this->error = $this->db->lasterror();
767  return -4;
768  }
769  }
770 
771  // Lines
772  if (empty($nolines)) {
773  if ($result >= 0 && !empty($this->table_element_line)) {
774  $result = $this->fetch_lines();
775  if ($result < 0) {
776  $this->error = $this->db->lasterror();
777  return -3;
778  }
779  }
780  }
781 
782  return $this->id;
783  } else {
784  dol_syslog(get_class($this)."::fetch Contract failed");
785  $this->error = "Fetch contract failed";
786  return -1;
787  }
788  } else {
789  dol_syslog(get_class($this)."::fetch Contract not found");
790  $this->error = "Contract not found";
791  return 0;
792  }
793  } else {
794  dol_syslog(get_class($this)."::fetch Error searching contract");
795  $this->error = $this->db->error();
796  return -1;
797  }
798  }
799 
800  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
810  public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0)
811  {
812  // phpcs:enable
813  $this->nbofservices = 0;
814  $this->nbofserviceswait = 0;
815  $this->nbofservicesopened = 0;
816  $this->nbofservicesexpired = 0;
817  $this->nbofservicesclosed = 0;
818 
819  $total_ttc = 0;
820  $total_vat = 0;
821  $total_ht = 0;
822 
823  $now = dol_now();
824 
825  $this->lines = array();
826  $pos = 0;
827 
828  // Selects contract lines related to a product
829  $sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
830  $sql .= " d.rowid, d.fk_contrat, d.statut as status, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
831  $sql .= " d.total_ht,";
832  $sql .= " d.total_tva,";
833  $sql .= " d.total_localtax1,";
834  $sql .= " d.total_localtax2,";
835  $sql .= " d.total_ttc,";
836  $sql .= " d.info_bits, d.fk_product,";
837  $sql .= " d.date_ouverture_prevue as date_start,";
838  $sql .= " d.date_ouverture as date_start_real,";
839  $sql .= " d.date_fin_validite as date_end,";
840  $sql .= " d.date_cloture as date_end_real,";
841  $sql .= " d.fk_user_author,";
842  $sql .= " d.fk_user_ouverture,";
843  $sql .= " d.fk_user_cloture,";
844  $sql .= " d.fk_unit,";
845  $sql .= " d.product_type as type,";
846  $sql .= " d.rang";
847  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
848  $sql .= " WHERE d.fk_contrat = ".((int) $this->id);
849  if ($only_services == 1) {
850  $sql .= " AND d.product_type = 1";
851  }
852  $sql .= " ORDER by d.rang ASC";
853 
854  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
855  $result = $this->db->query($sql);
856  if ($result) {
857  $num = $this->db->num_rows($result);
858  $i = 0;
859 
860  while ($i < $num) {
861  $objp = $this->db->fetch_object($result);
862 
863  $line = new ContratLigne($this->db);
864 
865  $line->id = $objp->rowid;
866  $line->ref = $objp->rowid;
867  $line->fk_contrat = $objp->fk_contrat;
868  $line->desc = $objp->description; // Description line
869  $line->qty = $objp->qty;
870  $line->vat_src_code = $objp->vat_src_code;
871  $line->tva_tx = $objp->tva_tx;
872  $line->localtax1_tx = $objp->localtax1_tx;
873  $line->localtax2_tx = $objp->localtax2_tx;
874  $line->localtax1_type = $objp->localtax1_type;
875  $line->localtax2_type = $objp->localtax2_type;
876  $line->subprice = $objp->subprice;
877  $line->statut = $objp->status;
878  $line->status = $objp->status;
879  $line->remise_percent = $objp->remise_percent;
880  $line->price_ht = $objp->price_ht;
881  $line->price = $objp->price_ht; // For backward compatibility
882  $line->total_ht = $objp->total_ht;
883  $line->total_tva = $objp->total_tva;
884  $line->total_localtax1 = $objp->total_localtax1;
885  $line->total_localtax2 = $objp->total_localtax2;
886  $line->total_ttc = $objp->total_ttc;
887  $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
888  $line->info_bits = $objp->info_bits;
889  $line->type = $objp->type;
890 
891  $line->fk_fournprice = $objp->fk_fournprice;
892  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
893  $line->pa_ht = $marginInfos[0];
894 
895  $line->fk_user_author = $objp->fk_user_author;
896  $line->fk_user_ouverture = $objp->fk_user_ouverture;
897  $line->fk_user_cloture = $objp->fk_user_cloture;
898  $line->fk_unit = $objp->fk_unit;
899 
900  $line->ref = $objp->product_ref; // deprecated
901  $line->product_ref = $objp->product_ref; // Product Ref
902  $line->product_type = $objp->product_type; // Product Type
903  $line->product_desc = $objp->product_desc; // Product Description
904  $line->product_label = $objp->product_label; // Product Label
905 
906  $line->description = $objp->description;
907 
908  $line->date_start = $this->db->jdate($objp->date_start);
909  $line->date_start_real = $this->db->jdate($objp->date_start_real);
910  $line->date_end = $this->db->jdate($objp->date_end);
911  $line->date_end_real = $this->db->jdate($objp->date_end_real);
912  // For backward compatibility
913  //$line->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue);
914  //$line->date_ouverture = $this->db->jdate($objp->date_ouverture);
915  //$line->date_fin_validite = $this->db->jdate($objp->date_fin_validite);
916  //$line->date_cloture = $this->db->jdate($objp->date_cloture);
917  //$line->date_debut_prevue = $this->db->jdate($objp->date_ouverture_prevue);
918  //$line->date_debut_reel = $this->db->jdate($objp->date_ouverture);
919  //$line->date_fin_prevue = $this->db->jdate($objp->date_fin_validite);
920  //$line->date_fin_reel = $this->db->jdate($objp->date_cloture);
921 
922  $line->rang = $objp->rang;
923 
924  // Retrieve all extrafields for contract line
925  // fetch optionals attributes and labels
926  if (empty($noextrafields)) {
927  $line->fetch_optionals();
928  }
929 
930  // multilangs
931  if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
932  $tmpproduct = new Product($this->db);
933  $tmpproduct->fetch($objp->fk_product);
934  $tmpproduct->getMultiLangs();
935 
936  $line->multilangs = $tmpproduct->multilangs;
937  }
938 
939  $this->lines[$pos] = $line;
940 
941  $this->lines_id_index_mapper[$line->id] = $pos;
942 
943  //dol_syslog("1 ".$line->desc);
944  //dol_syslog("2 ".$line->product_desc);
945 
946  if ($line->statut == ContratLigne::STATUS_INITIAL) {
947  $this->nbofserviceswait++;
948  }
949  if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
950  $this->nbofservicesopened++;
951  }
952  if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
953  $this->nbofservicesexpired++;
954  }
955  if ($line->statut == ContratLigne::STATUS_CLOSED) {
956  $this->nbofservicesclosed++;
957  }
958 
959  $total_ttc += $objp->total_ttc; // TODO Not saved into database
960  $total_vat += $objp->total_tva;
961  $total_ht += $objp->total_ht;
962 
963  $i++;
964  $pos++;
965  }
966  $this->db->free($result);
967  } else {
968  dol_syslog(get_class($this)."::Fetch Error when reading lines of contracts linked to products");
969  return -3;
970  }
971 
972  // Now set the global properties on contract not stored into database.
973  $this->nbofservices = count($this->lines);
974  $this->total_ttc = (float) price2num($total_ttc);
975  $this->total_tva = (float) price2num($total_vat);
976  $this->total_ht = (float) price2num($total_ht);
977 
978  return $this->lines;
979  }
980 
987  public function create($user)
988  {
989  global $conf, $langs, $mysoc;
990 
991  // Check parameters
992  $paramsok = 1;
993  if ($this->commercial_signature_id <= 0) {
994  $langs->load("commercial");
995  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeSignature"));
996  $paramsok = 0;
997  }
998  if ($this->commercial_suivi_id <= 0) {
999  $langs->load("commercial");
1000  $this->error .= ($this->error ? "<br>" : '');
1001  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeFollowUp"));
1002  $paramsok = 0;
1003  }
1004  if (!$paramsok) {
1005  return -1;
1006  }
1007 
1008 
1009  $this->db->begin();
1010 
1011  $now = dol_now();
1012 
1013  // Insert contract
1014  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,";
1015  $sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet,";
1016  $sql .= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
1017  $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->socid).", ".((int) $user->id);
1018  $sql .= ", ".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL");
1019  $sql .= ",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) : "NULL");
1020  $sql .= ",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) : "NULL");
1021  $sql .= ",".($this->fk_project > 0 ? ((int) $this->fk_project) : "NULL");
1022  $sql .= ", ".(dol_strlen($this->ref) <= 0 ? "null" : "'".$this->db->escape($this->ref)."'");
1023  $sql .= ", ".((int) $conf->entity);
1024  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
1025  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
1026  $sql .= ", ".(!empty($this->ref_customer) ? ("'".$this->db->escape($this->ref_customer)."'") : "NULL");
1027  $sql .= ", ".(!empty($this->ref_supplier) ? ("'".$this->db->escape($this->ref_supplier)."'") : "NULL");
1028  $sql .= ", ".(!empty($this->ref_ext) ? ("'".$this->db->escape($this->ref_ext)."'") : "NULL");
1029  $sql .= ")";
1030  $resql = $this->db->query($sql);
1031 
1032  if ($resql) {
1033  $error = 0;
1034 
1035  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat");
1036 
1037  // Load object modContract
1038  $module = (getDolGlobalString('CONTRACT_ADDON') ? $conf->global->CONTRACT_ADDON : 'mod_contract_serpis');
1039  if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') {
1040  $module = substr($module, 0, dol_strlen($module) - 4);
1041  }
1042  $result = dol_include_once('/core/modules/contract/'.$module.'.php');
1043  if ($result > 0) {
1044  $modCodeContract = new $module();
1045  '@phan-var-force CommonNumRefGenerator $modCodeContrat';
1046 
1047  if (!empty($modCodeContract->code_auto)) {
1048  // Force the ref to a draft value if numbering module is an automatic numbering
1049  $sql = 'UPDATE '.MAIN_DB_PREFIX."contrat SET ref='(PROV".$this->id.")' WHERE rowid=".((int) $this->id);
1050  if ($this->db->query($sql)) {
1051  if ($this->id) {
1052  $this->ref = "(PROV".$this->id.")";
1053  }
1054  }
1055  }
1056  }
1057 
1058  if (!$error) {
1059  $result = $this->insertExtraFields();
1060  if ($result < 0) {
1061  $error++;
1062  }
1063  }
1064 
1065  // Insert business contacts ('SALESREPSIGN','contrat')
1066  if (!$error) {
1067  $result = $this->add_contact($this->commercial_signature_id, 'SALESREPSIGN', 'internal');
1068  if ($result < 0) {
1069  $error++;
1070  }
1071  }
1072 
1073  // Insert business contacts ('SALESREPFOLL','contrat')
1074  if (!$error) {
1075  $result = $this->add_contact($this->commercial_suivi_id, 'SALESREPFOLL', 'internal');
1076  if ($result < 0) {
1077  $error++;
1078  }
1079  }
1080 
1081  if (!$error) {
1082  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1083  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1084  }
1085 
1086  // Add object linked
1087  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1088  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1089  if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
1090  foreach ($tmp_origin_id as $origin_id) {
1091  $ret = $this->add_object_linked($origin, $origin_id);
1092  if (!$ret) {
1093  $this->error = $this->db->lasterror();
1094  $error++;
1095  }
1096  }
1097  } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1098  $origin_id = $tmp_origin_id;
1099  $ret = $this->add_object_linked($origin, $origin_id);
1100  if (!$ret) {
1101  $this->error = $this->db->lasterror();
1102  $error++;
1103  }
1104  }
1105  }
1106  }
1107 
1108  if (!$error && $this->id && getDolGlobalString('MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) { // Get contact from origin object
1109  $originforcontact = $this->origin;
1110  $originidforcontact = $this->origin_id;
1111  if ($originforcontact == 'shipping') { // shipment and order share the same contacts. If creating from shipment we take data of order
1112  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1113  $exp = new Expedition($this->db);
1114  $exp->fetch($this->origin_id);
1115  $exp->fetchObjectLinked();
1116  if (count($exp->linkedObjectsIds['commande']) > 0) {
1117  foreach ($exp->linkedObjectsIds['commande'] as $key => $value) {
1118  $originforcontact = 'commande';
1119  $originidforcontact = $value;
1120  break; // We take first one
1121  }
1122  }
1123  }
1124 
1125  $sqlcontact = "SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as ctc";
1126  $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'";
1127 
1128  $resqlcontact = $this->db->query($sqlcontact);
1129  if ($resqlcontact) {
1130  while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1131  if ($objcontact->source == 'internal' && in_array($objcontact->code, array('SALESREPSIGN', 'SALESREPFOLL'))) {
1132  continue; // ignore this, already forced previously
1133  }
1134 
1135  $this->add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source); // May failed because of duplicate key or because code of contact type does not exists for new object
1136  }
1137  } else {
1138  dol_print_error($resqlcontact);
1139  }
1140  }
1141  }
1142 
1143  if (!$error) {
1144  // Call trigger
1145  $result = $this->call_trigger('CONTRACT_CREATE', $user);
1146  if ($result < 0) {
1147  $error++;
1148  }
1149  // End call triggers
1150 
1151  if (!$error) {
1152  $this->db->commit();
1153  return $this->id;
1154  } else {
1155  dol_syslog(get_class($this)."::create - 30 - ".$this->error, LOG_ERR);
1156  $this->db->rollback();
1157  return -3;
1158  }
1159  } else {
1160  $this->error = "Failed to add contract";
1161  dol_syslog(get_class($this)."::create - 20 - ".$this->error, LOG_ERR);
1162  $this->db->rollback();
1163  return -2;
1164  }
1165  } else {
1166  $this->error = $langs->trans("UnknownError").": ".$this->db->error();
1167  dol_syslog(get_class($this)."::create - 10 - ".$this->error, LOG_ERR);
1168 
1169  $this->db->rollback();
1170  return -1;
1171  }
1172  }
1173 
1174 
1181  public function delete($user)
1182  {
1183  global $conf;
1184 
1185  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1186 
1187  $error = 0;
1188 
1189  $this->db->begin();
1190 
1191  // Call trigger
1192  $result = $this->call_trigger('CONTRACT_DELETE', $user);
1193  if ($result < 0) {
1194  $error++;
1195  }
1196  // End call triggers
1197 
1198  if (!$error) {
1199  // Delete linked contacts
1200  $res = $this->delete_linked_contact();
1201  if ($res < 0) {
1202  dol_syslog(get_class($this)."::delete error", LOG_ERR);
1203  $error++;
1204  }
1205  }
1206 
1207  if (!$error) {
1208  // Delete linked object
1209  $res = $this->deleteObjectLinked();
1210  if ($res < 0) {
1211  $error++;
1212  }
1213  }
1214 
1215  // Delete lines
1216  if (!$error) {
1217  // Delete contratdet extrafields
1218  $main = MAIN_DB_PREFIX.'contratdet';
1219  $ef = $main."_extrafields";
1220  $sql = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_contrat = ".((int) $this->id).")";
1221 
1222  dol_syslog(get_class($this)."::delete contratdet_extrafields", LOG_DEBUG);
1223  $resql = $this->db->query($sql);
1224  if (!$resql) {
1225  $this->error = $this->db->error();
1226  $error++;
1227  }
1228  }
1229 
1230  if (!$error) {
1231  // Delete contratdet
1232  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet";
1233  $sql .= " WHERE fk_contrat=".((int) $this->id);
1234 
1235  dol_syslog(get_class($this)."::delete contratdet", LOG_DEBUG);
1236  $resql = $this->db->query($sql);
1237  if (!$resql) {
1238  $this->error = $this->db->error();
1239  $error++;
1240  }
1241  }
1242 
1243  // Delete llx_ecm_files
1244  if (!$error) {
1245  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."ecm_files WHERE src_object_type = '".$this->db->escape($this->table_element.(empty($this->module) ? "" : "@".$this->module))."' AND src_object_id = ".((int) $this->id);
1246  $resql = $this->db->query($sql);
1247  if (!$resql) {
1248  $this->error = $this->db->lasterror();
1249  $this->errors[] = $this->error;
1250  $error++;
1251  }
1252  }
1253 
1254  // Delete contract
1255  if (!$error) {
1256  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat";
1257  $sql .= " WHERE rowid=".((int) $this->id);
1258 
1259  dol_syslog(get_class($this)."::delete contrat", LOG_DEBUG);
1260  $resql = $this->db->query($sql);
1261  if (!$resql) {
1262  $this->error = $this->db->error();
1263  $error++;
1264  }
1265  }
1266 
1267  // Removed extrafields
1268  if (!$error) {
1269  $result = $this->deleteExtraFields();
1270  if ($result < 0) {
1271  $error++;
1272  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1273  }
1274  }
1275 
1276  if (!$error) {
1277  // We remove directory
1278  $ref = dol_sanitizeFileName($this->ref);
1279  if ($conf->contrat->dir_output) {
1280  $dir = $conf->contrat->multidir_output[$this->entity]."/".$ref;
1281  if (file_exists($dir)) {
1282  $res = @dol_delete_dir_recursive($dir);
1283  if (!$res) {
1284  $this->error = 'ErrorFailToDeleteDir';
1285  $error++;
1286  }
1287  }
1288  }
1289  }
1290 
1291  if (!$error) {
1292  $this->db->commit();
1293  return 1;
1294  } else {
1295  $this->error = $this->db->lasterror();
1296  $this->db->rollback();
1297  return -1;
1298  }
1299  }
1300 
1308  public function update($user, $notrigger = 0)
1309  {
1310  global $conf;
1311  $error = 0;
1312 
1313  // Clean parameters
1314  if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1315  $this->fk_commercial_signature = $this->commercial_signature_id;
1316  }
1317  if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1318  $this->fk_commercial_suivi = $this->commercial_suivi_id;
1319  }
1320  if (empty($this->socid) && $this->fk_soc > 0) {
1321  $this->socid = (int) $this->fk_soc;
1322  }
1323  if (empty($this->fk_project) && $this->projet > 0) {
1324  $this->fk_project = (int) $this->projet;
1325  }
1326 
1327  if (isset($this->ref)) {
1328  $this->ref = trim($this->ref);
1329  }
1330  if (isset($this->ref_customer)) {
1331  $this->ref_customer = trim($this->ref_customer);
1332  }
1333  if (isset($this->ref_supplier)) {
1334  $this->ref_supplier = trim($this->ref_supplier);
1335  }
1336  if (isset($this->ref_ext)) {
1337  $this->ref_ext = trim($this->ref_ext);
1338  }
1339  if (isset($this->entity)) {
1340  $this->entity = (int) $this->entity;
1341  }
1342  if (isset($this->statut)) {
1343  $this->statut = (int) $this->statut;
1344  }
1345  if (isset($this->status)) {
1346  $this->status = (int) $this->status;
1347  }
1348  if (isset($this->socid)) {
1349  $this->socid = (int) $this->socid;
1350  }
1351  if (isset($this->fk_commercial_signature)) {
1352  $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1353  }
1354  if (isset($this->fk_commercial_suivi)) {
1355  $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1356  }
1357  if (isset($this->note_private)) {
1358  $this->note_private = trim($this->note_private);
1359  }
1360  if (isset($this->note_public)) {
1361  $this->note_public = trim($this->note_public);
1362  }
1363  if (isset($this->import_key)) {
1364  $this->import_key = trim($this->import_key);
1365  }
1366  //if (isset($this->extraparams)) $this->extraparams=trim($this->extraparams);
1367 
1368  // $this->oldcopy must have been set by the caller of update
1369 
1370  // Update request
1371  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET";
1372  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1373  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1374  $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1375  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1376  $sql .= " entity=".$conf->entity.",";
1377  $sql .= " date_contrat=".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : 'null').",";
1378  $sql .= " statut=".(isset($this->statut) ? $this->statut : (isset($this->status) ? $this->status : "null")).",";
1379  $sql .= " fk_soc=".($this->socid > 0 ? $this->socid : "null").",";
1380  $sql .= " fk_projet=".($this->fk_project > 0 ? $this->fk_project : "null").",";
1381  $sql .= " fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature : "null").",";
1382  $sql .= " fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi : "null").",";
1383  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1384  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1385  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
1386  //$sql.= " extraparams=".(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null");
1387  $sql .= " WHERE rowid=".((int) $this->id);
1388 
1389  $this->db->begin();
1390 
1391  $resql = $this->db->query($sql);
1392  if (!$resql) {
1393  $error++;
1394  $this->errors[] = "Error ".$this->db->lasterror();
1395  }
1396 
1397  if (!$error) {
1398  $result = $this->insertExtraFields(); // This delete and reinsert extrafields
1399  if ($result < 0) {
1400  $error++;
1401  }
1402  }
1403 
1404  if (!$error && !$notrigger) {
1405  // Call triggers
1406  $result = $this->call_trigger('CONTRACT_MODIFY', $user);
1407  if ($result < 0) {
1408  $error++;
1409  }
1410  // End call triggers
1411  }
1412 
1413  // Commit or rollback
1414  if ($error) {
1415  foreach ($this->errors as $errmsg) {
1416  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1417  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1418  }
1419  $this->db->rollback();
1420  return -1 * $error;
1421  } else {
1422  $this->db->commit();
1423  return 1;
1424  }
1425  }
1426 
1427 
1451  public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type = 'HT', $pu_ttc = 0.0, $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = array(), $fk_unit = null, $rang = 0)
1452  {
1453  global $user, $langs, $conf, $mysoc;
1454  $error = 0;
1455 
1456  dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type, $pu_ttc, $info_bits, $rang");
1457 
1458  // Check parameters
1459  if ($fk_product <= 0 && empty($desc)) {
1460  $this->error = "ErrorDescRequiredForFreeProductLines";
1461  return -1;
1462  }
1463 
1464  if ($this->statut >= 0) {
1465  // Clean parameters
1466  $pu_ht = price2num($pu_ht);
1467  $pu_ttc = price2num($pu_ttc);
1468  $pa_ht = price2num($pa_ht);
1469 
1470  // Clean vat code
1471  $reg = array();
1472  $vat_src_code = '';
1473  if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
1474  $vat_src_code = $reg[1];
1475  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code into vatrate.
1476  }
1477  $txtva = price2num($txtva);
1478  $txlocaltax1 = price2num($txlocaltax1);
1479  $txlocaltax2 = price2num($txlocaltax2);
1480 
1481  $remise_percent = price2num($remise_percent);
1482  $qty = price2num($qty);
1483  if (empty($qty)) {
1484  $qty = 1;
1485  }
1486  if (empty($info_bits)) {
1487  $info_bits = 0;
1488  }
1489  if (empty($pu_ht) || !is_numeric($pu_ht)) {
1490  $pu_ht = 0;
1491  }
1492  if (empty($pu_ttc)) {
1493  $pu_ttc = 0;
1494  }
1495  if (empty($txtva) || !is_numeric($txtva)) {
1496  $txtva = 0;
1497  }
1498  if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1499  $txlocaltax1 = 0;
1500  }
1501  if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1502  $txlocaltax2 = 0;
1503  }
1504 
1505  if ($price_base_type == 'HT') {
1506  $pu = $pu_ht;
1507  } else {
1508  $pu = $pu_ttc;
1509  }
1510 
1511  // Check parameters
1512  if (empty($remise_percent)) {
1513  $remise_percent = 0;
1514  }
1515  if (empty($rang)) {
1516  $rang = 0;
1517  }
1518 
1519  if ($date_start && $date_end && $date_start > $date_end) {
1520  $langs->load("errors");
1521  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1522  return -1;
1523  }
1524 
1525  $this->db->begin();
1526 
1527  $localtaxes_type = getLocalTaxesFromRate($txtva.($vat_src_code ? ' ('.$vat_src_code.')' : ''), 0, $this->societe, $mysoc);
1528 
1529  // Calcul du total TTC et de la TVA pour la ligne a partir de
1530  // qty, pu, remise_percent et txtva
1531  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1532  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1533 
1534  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1535  $total_ht = $tabprice[0];
1536  $total_tva = $tabprice[1];
1537  $total_ttc = $tabprice[2];
1538  $total_localtax1 = $tabprice[9];
1539  $total_localtax2 = $tabprice[10];
1540 
1541  $localtax1_type = $localtaxes_type[0];
1542  $localtax2_type = $localtaxes_type[2];
1543 
1544  // TODO A virer
1545  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1546  $remise = 0;
1547  $price = price2num(round($pu_ht, 2));
1548  if (dol_strlen($remise_percent) > 0) {
1549  $remise = round(($pu_ht * $remise_percent / 100), 2);
1550  $price = $pu_ht - $remise;
1551  }
1552 
1553  if (empty($pa_ht)) {
1554  $pa_ht = 0;
1555  }
1556 
1557 
1558  // if buy price not defined, define buyprice as configured in margin admin
1559  if ($pa_ht == 0) {
1560  $result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product);
1561  if ($result < 0) {
1562  return -1;
1563  } else {
1564  $pa_ht = $result;
1565  }
1566  }
1567 
1568  // Insertion dans la base
1569  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
1570  $sql .= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1571  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1572  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1573  $sql .= " info_bits,";
1574  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1575  if ($date_start > 0) {
1576  $sql .= ",date_ouverture_prevue";
1577  }
1578  if ($date_end > 0) {
1579  $sql .= ",date_fin_validite";
1580  }
1581  $sql .= ", fk_unit";
1582  $sql .= ", rang";
1583  $sql .= ") VALUES (";
1584  $sql .= $this->id.", '', '".$this->db->escape($desc)."',";
1585  $sql .= ($fk_product > 0 ? $fk_product : "null").",";
1586  $sql .= " ".((float) $qty).",";
1587  $sql .= " ".((float) $txtva).",";
1588  $sql .= " ".($vat_src_code ? "'".$this->db->escape($vat_src_code)."'" : "null").",";
1589  $sql .= " ".((float) $txlocaltax1).",";
1590  $sql .= " ".((float) $txlocaltax2).",";
1591  $sql .= " '".$this->db->escape($localtax1_type)."',";
1592  $sql .= " '".$this->db->escape($localtax2_type)."',";
1593  $sql .= " ".price2num($remise_percent).",";
1594  $sql .= " ".price2num($pu_ht).",";
1595  $sql .= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).",";
1596  $sql .= " ".((int) $info_bits).",";
1597  $sql .= " ".price2num($price).",".price2num($remise).",";
1598  if (isset($fk_fournprice)) {
1599  $sql .= ' '.((int) $fk_fournprice).',';
1600  } else {
1601  $sql .= ' null,';
1602  }
1603  if (isset($pa_ht)) {
1604  $sql .= ' '.price2num($pa_ht);
1605  } else {
1606  $sql .= ' null';
1607  }
1608  if ($date_start > 0) {
1609  $sql .= ",'".$this->db->idate($date_start)."'";
1610  }
1611  if ($date_end > 0) {
1612  $sql .= ",'".$this->db->idate($date_end)."'";
1613  }
1614  $sql .= ", ".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1615  $sql .= ", ".(!empty($rang) ? (int) $rang : "0");
1616  $sql .= ")";
1617 
1618  $resql = $this->db->query($sql);
1619  if ($resql) {
1620  $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX."contratdet");
1621 
1622  if (!$error) {
1623  $contractline = new ContratLigne($this->db);
1624  $contractline->array_options = $array_options;
1625  $contractline->id = $contractlineid;
1626  $result = $contractline->insertExtraFields();
1627  if ($result < 0) {
1628  $this->errors = array_merge($this->errors, $contractline->errors);
1629  $this->error = $contractline->error;
1630  $error++;
1631  }
1632  }
1633 
1634  if (empty($error)) {
1635  // Call trigger
1636  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
1637  if ($result < 0) {
1638  $error++;
1639  }
1640  // End call triggers
1641  }
1642 
1643  if ($error) {
1644  $this->db->rollback();
1645  return -1;
1646  } else {
1647  $this->db->commit();
1648  return $contractlineid;
1649  }
1650  } else {
1651  $this->db->rollback();
1652  $this->error = $this->db->error()." sql=".$sql;
1653  return -1;
1654  }
1655  } else {
1656  dol_syslog(get_class($this)."::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1657  return -2;
1658  }
1659  }
1660 
1685  public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx = 0.0, $localtax2tx = 0.0, $date_start_real = '', $date_end_real = '', $price_base_type = 'HT', $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = array(), $fk_unit = null, $rang = 0)
1686  {
1687  global $user, $conf, $langs, $mysoc;
1688 
1689  $error = 0;
1690 
1691  // Clean parameters
1692  $qty = trim((string) $qty);
1693  $desc = trim($desc);
1694  $desc = trim($desc);
1695  $price = price2num($pu);
1696  $tvatx = price2num($tvatx);
1697  $localtax1tx = price2num($localtax1tx);
1698  $localtax2tx = price2num($localtax2tx);
1699  $pa_ht = price2num($pa_ht);
1700  if (empty($fk_fournprice)) {
1701  $fk_fournprice = 0;
1702  }
1703  if (empty($rang)) {
1704  $rang = 0;
1705  }
1706 
1707  $subprice = $price;
1708  $remise = 0;
1709  if (dol_strlen($remise_percent) > 0) {
1710  $remise = round(($pu * $remise_percent / 100), 2);
1711  $price = $pu - $remise;
1712  } else {
1713  $remise_percent = 0;
1714  }
1715 
1716  if ($date_start && $date_end && $date_start > $date_end) {
1717  $langs->load("errors");
1718  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1719  return -1;
1720  }
1721 
1722  dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $date_start_real, $date_end_real, $tvatx, $localtax1tx, $localtax2tx, $price_base_type, $info_bits, $rang");
1723 
1724  $this->db->begin();
1725 
1726  // Calcul du total TTC et de la TVA pour la ligne a partir de
1727  // qty, pu, remise_percent et tvatx
1728  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1729  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1730 
1731  $localtaxes_type = getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc);
1732  $tvatx = preg_replace('/\s*\‍(.*\‍)/', '', $tvatx); // Remove code into vatrate.
1733 
1734  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1735  $total_ht = $tabprice[0];
1736  $total_tva = $tabprice[1];
1737  $total_ttc = $tabprice[2];
1738  $total_localtax1 = $tabprice[9];
1739  $total_localtax2 = $tabprice[10];
1740 
1741  $localtax1_type = (empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]);
1742  $localtax2_type = (empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]);
1743 
1744  // TODO A virer
1745  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1746  $remise = 0;
1747  $price = price2num(round($pu, 2));
1748  if (dol_strlen($remise_percent) > 0) {
1749  $remise = round(($pu * $remise_percent / 100), 2);
1750  $price = $pu - $remise;
1751  }
1752 
1753  if (empty($pa_ht)) {
1754  $pa_ht = 0;
1755  }
1756 
1757  // if buy price not defined, define buyprice as configured in margin admin
1758  if ($pa_ht == 0) {
1759  $result = $this->defineBuyPrice($pu, $remise_percent);
1760  if ($result < 0) {
1761  return -1;
1762  } else {
1763  $pa_ht = $result;
1764  }
1765  }
1766 
1767  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description = '".$this->db->escape($desc)."'";
1768  $sql .= ",price_ht = ".((float) price2num($price));
1769  $sql .= ",subprice = ".((float) price2num($subprice));
1770  $sql .= ",remise = ".((float) price2num($remise));
1771  $sql .= ",remise_percent = ".((float) price2num($remise_percent));
1772  $sql .= ",qty = ".((float) $qty);
1773  $sql .= ",tva_tx = ".((float) price2num($tvatx));
1774  $sql .= ",localtax1_tx = ".((float) price2num($localtax1tx));
1775  $sql .= ",localtax2_tx = ".((float) price2num($localtax2tx));
1776  $sql .= ",localtax1_type='".$this->db->escape($localtax1_type)."'";
1777  $sql .= ",localtax2_type='".$this->db->escape($localtax2_type)."'";
1778  $sql .= ", total_ht = ".((float) price2num($total_ht));
1779  $sql .= ", total_tva = ".((float) price2num($total_tva));
1780  $sql .= ", total_localtax1 = ".((float) price2num($total_localtax1));
1781  $sql .= ", total_localtax2 = ".((float) price2num($total_localtax2));
1782  $sql .= ", total_ttc = ".((float) price2num($total_ttc));
1783  $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null");
1784  $sql .= ", buy_price_ht = ".((float) price2num($pa_ht));
1785  if ($date_start > 0) {
1786  $sql .= ",date_ouverture_prevue = '".$this->db->idate($date_start)."'";
1787  } else {
1788  $sql .= ",date_ouverture_prevue = null";
1789  }
1790  if ($date_end > 0) {
1791  $sql .= ",date_fin_validite = '".$this->db->idate($date_end)."'";
1792  } else {
1793  $sql .= ",date_fin_validite = null";
1794  }
1795  if ($date_start_real > 0) {
1796  $sql .= ",date_ouverture = '".$this->db->idate($date_start_real)."'";
1797  } else {
1798  $sql .= ",date_ouverture = null";
1799  }
1800  if ($date_end_real > 0) {
1801  $sql .= ",date_cloture = '".$this->db->idate($date_end_real)."'";
1802  } else {
1803  $sql .= ",date_cloture = null";
1804  }
1805  $sql .= ", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) : "null");
1806  $sql .= ", rang = ".(!empty($rang) ? ((int) $rang) : "0");
1807  $sql .= " WHERE rowid = ".((int) $rowid);
1808 
1809  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1810  $result = $this->db->query($sql);
1811  if ($result) {
1812  if (is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1813  $contractline = new ContratLigne($this->db);
1814  $contractline->fetch($rowid);
1815 
1816  // We replace values in $contractline->array_options only for entries defined into $array_options
1817  foreach ($array_options as $key => $value) {
1818  $contractline->array_options[$key] = $array_options[$key];
1819  }
1820 
1821  $result = $contractline->insertExtraFields();
1822  if ($result < 0) {
1823  $this->errors[] = $contractline->error;
1824  $error++;
1825  }
1826  }
1827 
1828  if (empty($error)) {
1829  // Call trigger
1830  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
1831  if ($result < 0) {
1832  $this->db->rollback();
1833  return -3;
1834  }
1835  // End call triggers
1836 
1837  $this->db->commit();
1838  return 1;
1839  } else {
1840  $this->db->rollback();
1841  return -1;
1842  }
1843  } else {
1844  $this->db->rollback();
1845  $this->error = $this->db->error();
1846  dol_syslog(get_class($this)."::updateline Erreur -1");
1847  return -1;
1848  }
1849  }
1850 
1858  public function deleteLine($idline, User $user)
1859  {
1860  $error = 0;
1861 
1862  if ($this->statut >= 0) {
1863  // Call trigger
1864  $result = $this->call_trigger('LINECONTRACT_DELETE', $user);
1865  if ($result < 0) {
1866  return -1;
1867  }
1868  // End call triggers
1869 
1870  $this->db->begin();
1871 
1872  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1873  $sql .= " WHERE rowid = ".((int) $idline);
1874 
1875  dol_syslog(get_class($this)."::deleteline", LOG_DEBUG);
1876  $resql = $this->db->query($sql);
1877  if (!$resql) {
1878  $this->error = "Error ".$this->db->lasterror();
1879  $error++;
1880  }
1881 
1882  if (!$error) {
1883  // Remove extrafields
1884  $contractline = new ContratLigne($this->db);
1885  $contractline->id = $idline;
1886  $result = $contractline->deleteExtraFields();
1887  if ($result < 0) {
1888  $error++;
1889  $this->error = "Error ".get_class($this)."::deleteline deleteExtraFields error -4 ".$contractline->error;
1890  }
1891  }
1892 
1893  if (empty($error)) {
1894  $this->db->commit();
1895  return 1;
1896  } else {
1897  dol_syslog(get_class($this)."::deleteline ERROR:".$this->error, LOG_ERR);
1898  $this->db->rollback();
1899  return -1;
1900  }
1901  } else {
1902  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1903  return -2;
1904  }
1905  }
1906 
1907 
1908  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1916  public function update_statut($user)
1917  {
1918  // phpcs:enable
1919  dol_syslog(__METHOD__." is deprecated", LOG_WARNING);
1920 
1921  // If draft, we keep it (should not happen)
1922  if ($this->statut == 0) {
1923  return 1;
1924  }
1925 
1926  // Load $this->lines array
1927  // $this->fetch_lines();
1928 
1929  // $newstatut=1;
1930  // foreach($this->lines as $key => $contractline)
1931  // {
1932  // // if ($contractline) // Loop on each service
1933  // }
1934 
1935  return 1;
1936  }
1937 
1938 
1945  public function getLibStatut($mode)
1946  {
1947  return $this->LibStatut($this->statut, $mode);
1948  }
1949 
1950  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1958  public function LibStatut($status, $mode)
1959  {
1960  // phpcs:enable
1961  global $langs;
1962 
1963  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1964  global $langs;
1965  $langs->load("contracts");
1966  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1967  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1968  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1969  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1970  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1971  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1972  }
1973 
1974  $statusType = 'status'.$status;
1975  if ($status == self::STATUS_VALIDATED) {
1976  $statusType = 'status6';
1977  }
1978 
1979  if ($mode == 4 || $mode == 6 || $mode == 7) {
1980  $text = '';
1981  if ($mode == 4) {
1982  $text = '<span class="hideonsmartphone">';
1983  $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1984  $text .= ' '.$langs->trans("Services");
1985  $text .= ': &nbsp; &nbsp; ';
1986  $text .= '</span>';
1987  }
1988  $text .= ($mode == 7 ? '<span class="nowraponall">' : '');
1989  $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1990  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1991  $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1992  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1993  $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1994  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1995  $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="marginleft2"')) : '';
1996  $text .= ($mode == 7 ? '</span>' : '');
1997  return $text;
1998  } else {
1999  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
2000  }
2001  }
2002 
2009  public function getTooltipContentArray($params)
2010  {
2011  global $conf, $langs, $user;
2012 
2013  $langs->load('contracts');
2014 
2015  $datas = [];
2016  $nofetch = !empty($params['nofetch']);
2017 
2018  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2019  return ['optimize' => $langs->trans("ShowContract")];
2020  }
2021  if ($user->hasRight('contrat', 'lire')) {
2022  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contract").'</u>';
2023  /* Status of a contract is status of all services, so disabled
2024  if (isset($this->statut)) {
2025  $label .= ' '.$this->getLibStatut(5);
2026  }*/
2027  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
2028  if (!$nofetch) {
2029  $langs->load('companies');
2030  if (empty($this->thirdparty)) {
2031  $this->fetch_thirdparty();
2032  }
2033  $datas['customer'] = '<br><b>'.$langs->trans('Customer').':</b> '.$this->thirdparty->getNomUrl(1, '', 0, 1);
2034  }
2035  $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '. $this->ref_customer;
2036  if (!$nofetch) {
2037  $langs->load('project');
2038  if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
2039  $res = $this->fetch_project();
2040  if ($res > 0 && $this->project instanceof Project) {
2041  $datas['project'] = '<br><b>'.$langs->trans('Project').':</b> '.$this->project->getNomUrl(1, '', 0, 1);
2042  }
2043  }
2044  }
2045  $datas['refsupplier'] = '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
2046  if (!empty($this->total_ht)) {
2047  $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2048  }
2049  if (!empty($this->total_tva)) {
2050  $datas['vatamount'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2051  }
2052  if (!empty($this->total_ttc)) {
2053  $datas['amounttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2054  }
2055  }
2056  return $datas;
2057  }
2058 
2068  public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2069  {
2070  global $conf, $langs, $user, $hookmanager;
2071 
2072  $result = '';
2073 
2074  $url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id;
2075 
2076  //if ($option !== 'nolink')
2077  //{
2078  // Add param to save lastsearch_values or not
2079  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2080  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2081  $add_save_lastsearch_values = 1;
2082  }
2083  if ($add_save_lastsearch_values) {
2084  $url .= '&save_lastsearch_values=1';
2085  }
2086  //}
2087  $params = [
2088  'id' => $this->id,
2089  'objecttype' => $this->element,
2090  'nofetch' => 1,
2091  ];
2092  $classfortooltip = 'classfortooltip';
2093  $dataparams = '';
2094  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
2095  $classfortooltip = 'classforajaxtooltip';
2096  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
2097  $label = '';
2098  } else {
2099  $label = implode($this->getTooltipContentArray($params));
2100  }
2101 
2102  $linkclose = '';
2103  if (empty($notooltip) && $user->hasRight('contrat', 'lire')) {
2104  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2105  $label = $langs->trans("ShowContract");
2106  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
2107  }
2108  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
2109  $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
2110  }
2111  $linkstart = '<a href="'.$url.'"';
2112  $linkstart .= $linkclose.'>';
2113  $linkend = '</a>';
2114 
2115  $result .= $linkstart;
2116  if ($withpicto) {
2117  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
2118  }
2119  if ($withpicto != 2) {
2120  $result .= ($this->ref ? $this->ref : $this->id);
2121  }
2122  $result .= $linkend;
2123 
2124  global $action;
2125  $hookmanager->initHooks(array('contractdao'));
2126  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
2127  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
2128  if ($reshook > 0) {
2129  $result = $hookmanager->resPrint;
2130  } else {
2131  $result .= $hookmanager->resPrint;
2132  }
2133 
2134  return $result;
2135  }
2136 
2143  public function info($id)
2144  {
2145  $sql = "SELECT c.rowid, c.ref, c.datec,";
2146  $sql .= " c.tms as date_modification,";
2147  $sql .= " fk_user_author";
2148  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2149  $sql .= " WHERE c.rowid = ".((int) $id);
2150 
2151  $result = $this->db->query($sql);
2152  if ($result) {
2153  if ($this->db->num_rows($result)) {
2154  $obj = $this->db->fetch_object($result);
2155 
2156  $this->id = $obj->rowid;
2157  $this->ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2158 
2159  $this->user_creation_id = $obj->fk_user_author;
2160  $this->date_creation = $this->db->jdate($obj->datec);
2161  $this->date_modification = $this->db->jdate($obj->date_modification);
2162  }
2163 
2164  $this->db->free($result);
2165  } else {
2166  dol_print_error($this->db);
2167  }
2168  }
2169 
2170  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2177  public function array_detail($status = -1)
2178  {
2179  // phpcs:enable
2180  $tab = array();
2181 
2182  $sql = "SELECT cd.rowid";
2183  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
2184  $sql .= " WHERE fk_contrat =".((int) $this->id);
2185  if ($status >= 0) {
2186  $sql .= " AND statut = ".((int) $status);
2187  }
2188 
2189  dol_syslog(get_class($this)."::array_detail()", LOG_DEBUG);
2190  $resql = $this->db->query($sql);
2191  if ($resql) {
2192  $num = $this->db->num_rows($resql);
2193  $i = 0;
2194  while ($i < $num) {
2195  $obj = $this->db->fetch_object($resql);
2196  $tab[$i] = $obj->rowid;
2197  $i++;
2198  }
2199  return $tab;
2200  } else {
2201  $this->error = $this->db->error();
2202  return -1;
2203  }
2204  }
2205 
2215  public function getListOfContracts($option = 'all', $status = [], $product_categories = [], $line_status = [])
2216  {
2217  $tab = array();
2218 
2219  $sql = "SELECT c.rowid";
2220  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2221  if (!empty($product_categories)) {
2222  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON cd.fk_contrat = c.rowid";
2223  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = cd.fk_product AND cp.fk_categorie IN (".$this->db->sanitize(implode(', ', $product_categories)).")";
2224  }
2225  $sql .= " WHERE c.fk_soc =".((int) $this->socid);
2226  $sql .= ($option == 'others') ? " AND c.rowid <> ".((int) $this->id) : "";
2227  $sql .= (!empty($status)) ? " AND c.statut IN (".$this->db->sanitize(implode(', ', $status)).")" : "";
2228  $sql .= (!empty($line_status)) ? " AND cd.statut IN (".$this->db->sanitize(implode(', ', $line_status)).")" : "";
2229  $sql .= " GROUP BY c.rowid";
2230 
2231  dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG);
2232  $resql = $this->db->query($sql);
2233  if ($resql) {
2234  $num = $this->db->num_rows($resql);
2235  $i = 0;
2236  while ($i < $num) {
2237  $obj = $this->db->fetch_object($resql);
2238  $contrat = new Contrat($this->db);
2239  $contrat->fetch($obj->rowid);
2240  $tab[$contrat->id] = $contrat;
2241  $i++;
2242  }
2243  return $tab;
2244  } else {
2245  $this->error = $this->db->lasterror();
2246  return -1;
2247  }
2248  }
2249 
2250 
2251  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2259  public function load_board($user, $mode)
2260  {
2261  // phpcs:enable
2262  global $conf, $langs;
2263 
2264  $this->from = " FROM ".MAIN_DB_PREFIX."contrat as c";
2265  $this->from .= ", ".MAIN_DB_PREFIX."contratdet as cd";
2266  $this->from .= ", ".MAIN_DB_PREFIX."societe as s";
2267  if (!$user->hasRight('societe', 'client', 'voir')) {
2268  $this->from .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2269  }
2270 
2271  if ($mode == 'inactive') {
2272  $sql = "SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2273  $sql .= $this->from;
2274  $sql .= " WHERE c.statut = 1";
2275  $sql .= " AND c.rowid = cd.fk_contrat";
2276  $sql .= " AND cd.statut = 0";
2277  } elseif ($mode == 'expired') {
2278  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2279  $sql .= $this->from;
2280  $sql .= " WHERE c.statut = 1";
2281  $sql .= " AND c.rowid = cd.fk_contrat";
2282  $sql .= " AND cd.statut = 4";
2283  $sql .= " AND cd.date_fin_validite < '".$this->db->idate(dol_now())."'";
2284  } elseif ($mode == 'active') {
2285  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2286  $sql .= $this->from;
2287  $sql .= " WHERE c.statut = 1";
2288  $sql .= " AND c.rowid = cd.fk_contrat";
2289  $sql .= " AND cd.statut = 4";
2290  //$datetouse = dol_now();
2291  //$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'";
2292  }
2293  $sql .= " AND c.fk_soc = s.rowid";
2294  $sql .= " AND c.entity = ".((int) $conf->entity);
2295  if ($user->socid) {
2296  $sql .= " AND c.fk_soc = ".((int) $user->socid);
2297  }
2298  if (!$user->hasRight('societe', 'client', 'voir')) {
2299  $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2300  }
2301 
2302  $resql = $this->db->query($sql);
2303  if ($resql) {
2304  $langs->load("contracts");
2305  $now = dol_now();
2306 
2307  if ($mode == 'inactive') {
2308  $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2309  $label = $langs->trans("BoardNotActivatedServices");
2310  $labelShort = $langs->trans("BoardNotActivatedServicesShort");
2311  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2312  } elseif ($mode == 'expired') {
2313  $warning_delay = $conf->contrat->services->expires->warning_delay;
2314  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2315  $label = $langs->trans("BoardExpiredServices");
2316  $labelShort = $langs->trans("BoardExpiredServicesShort");
2317  } else {
2318  $warning_delay = $conf->contrat->services->expires->warning_delay;
2319  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2320  //$url.= '&op2day='.$arraydatetouse['mday'].'&op2month='.$arraydatetouse['mon'].'&op2year='.$arraydatetouse['year'];
2321  //if ($warning_delay >= 0) $url.='&amp;filter=expired';
2322  $label = $langs->trans("BoardRunningServices");
2323  $labelShort = $langs->trans("BoardRunningServicesShort");
2324  }
2325 
2326  $response = new WorkboardResponse();
2327  $response->warning_delay = $warning_delay / 60 / 60 / 24;
2328  $response->label = $label;
2329  $response->labelShort = $labelShort;
2330  $response->url = $url;
2331  $response->img = img_object('', "contract");
2332 
2333  while ($obj = $this->db->fetch_object($resql)) {
2334  $response->nbtodo++;
2335 
2336  if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2337  $response->nbtodolate++;
2338  }
2339  }
2340 
2341  return $response;
2342  } else {
2343  dol_print_error($this->db);
2344  $this->error = $this->db->error();
2345  return -1;
2346  }
2347  }
2348 
2354  public function loadStateBoard()
2355  {
2356  global $conf, $user;
2357 
2358  $this->nb = array();
2359  $clause = "WHERE";
2360 
2361  $sql = "SELECT count(c.rowid) as nb";
2362  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2363  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
2364  if (!$user->hasRight('societe', 'client', 'voir')) {
2365  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2366  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
2367  $clause = "AND";
2368  }
2369  $sql .= " ".$clause." c.entity = ".$conf->entity;
2370 
2371  $resql = $this->db->query($sql);
2372  if ($resql) {
2373  while ($obj = $this->db->fetch_object($resql)) {
2374  $this->nb["contracts"] = $obj->nb;
2375  }
2376  $this->db->free($resql);
2377  return 1;
2378  } else {
2379  dol_print_error($this->db);
2380  $this->error = $this->db->error();
2381  return -1;
2382  }
2383  }
2384 
2385 
2386  /* gestion des contacts d'un contrat */
2387 
2393  public function getIdBillingContact()
2394  {
2395  return $this->getIdContact('external', 'BILLING');
2396  }
2397 
2403  public function getIdServiceContact()
2404  {
2405  return $this->getIdContact('external', 'SERVICE');
2406  }
2407 
2408 
2416  public function initAsSpecimen()
2417  {
2418  global $user, $langs, $conf;
2419 
2420  // Load array of products prodids
2421  $num_prods = 0;
2422  $prodids = array();
2423  $sql = "SELECT rowid";
2424  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2425  $sql .= " WHERE entity IN (".getEntity('product').")";
2426  $sql .= " AND tosell = 1";
2427  $sql .= $this->db->plimit(100);
2428 
2429  $resql = $this->db->query($sql);
2430  if ($resql) {
2431  $num_prods = $this->db->num_rows($resql);
2432  $i = 0;
2433  while ($i < $num_prods) {
2434  $i++;
2435  $row = $this->db->fetch_row($resql);
2436  $prodids[$i] = $row[0];
2437  }
2438  }
2439 
2440  // Initialise parameters
2441  $this->id = 0;
2442  $this->specimen = 1;
2443 
2444  $this->ref = 'SPECIMEN';
2445  $this->ref_customer = 'SPECIMENCUST';
2446  $this->ref_supplier = 'SPECIMENSUPP';
2447  $this->socid = 1;
2448  $this->status = 0;
2449  $this->date_creation = (dol_now() - 3600 * 24 * 7);
2450  $this->date_contrat = dol_now();
2451  $this->commercial_signature_id = 1;
2452  $this->commercial_suivi_id = 1;
2453  $this->note_private = 'This is a comment (private)';
2454  $this->note_public = 'This is a comment (public)';
2455  $this->fk_project = 0;
2456  // Lines
2457  $nbp = 5;
2458  $xnbp = 0;
2459  while ($xnbp < $nbp) {
2460  $line = new ContratLigne($this->db);
2461  $line->qty = 1;
2462  $line->subprice = 100;
2463  $line->tva_tx = 19.6;
2464  $line->remise_percent = 10;
2465  $line->total_ht = 90;
2466  $line->total_ttc = 107.64; // 90 * 1.196
2467  $line->total_tva = 17.64;
2468  $line->date_start = dol_now() - 500000;
2469  $line->date_start_real = dol_now() - 200000;
2470  $line->date_end = dol_now() + 500000;
2471  $line->date_end_real = dol_now() - 100000;
2472  if ($num_prods > 0) {
2473  $prodid = mt_rand(1, $num_prods);
2474  $line->fk_product = $prodids[$prodid];
2475  }
2476  $this->lines[$xnbp] = $line;
2477  $xnbp++;
2478  }
2479 
2480  return 1;
2481  }
2482 
2488  public function getLinesArray()
2489  {
2490  return $this->fetch_lines();
2491  }
2492 
2498  public function getTicketsArray()
2499  {
2500  global $user;
2501 
2502  $ticket = new Ticket($this->db);
2503  $nbTicket = $ticket->fetchAll($user, 'ASC', 't.datec', '', 0, '', array('t.fk_contract' => $this->id));
2504 
2505  return ($nbTicket < 0 ? $nbTicket : $ticket->lines);
2506  }
2507 
2508 
2520  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2521  {
2522  global $conf, $langs;
2523 
2524  if (!dol_strlen($modele)) {
2525  $modele = ''; // No doc template/generation by default
2526 
2527  if (!empty($this->model_pdf)) {
2528  $modele = $this->model_pdf;
2529  } elseif (getDolGlobalString('CONTRACT_ADDON_PDF')) {
2530  $modele = getDolGlobalString('CONTRACT_ADDON_PDF');
2531  }
2532  }
2533 
2534  if (empty($modele)) {
2535  return 0;
2536  } else {
2537  $langs->load("contracts");
2538  $outputlangs->load("products");
2539 
2540  $modelpath = "core/modules/contract/doc/";
2541  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2542  }
2543  }
2544 
2553  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2554  {
2555  $tables = array(
2556  'contrat'
2557  );
2558 
2559  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2560  }
2561 
2570  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
2571  {
2572  $tables = array(
2573  'contratdet'
2574  );
2575 
2576  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
2577  }
2578 
2587  public function createFromClone(User $user, $socid = 0, $notrigger = 0)
2588  {
2589  global $db, $langs, $conf, $hookmanager, $extrafields;
2590 
2591  dol_include_once('/projet/class/project.class.php');
2592 
2593  $error = 0;
2594 
2595  $this->fetch($this->id);
2596 
2597  // Load dest object
2598  $clonedObj = clone $this;
2599  $clonedObj->socid = $socid;
2600 
2601  $this->db->begin();
2602 
2603  $objsoc = new Societe($this->db);
2604 
2605  $objsoc->fetch($clonedObj->socid);
2606 
2607  // Clean data
2608  $clonedObj->statut = 0;
2609  // Clean extrafields
2610  if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2611  $extrafields->fetch_name_optionals_label($this->table_element);
2612  foreach ($clonedObj->array_options as $key => $option) {
2613  $shortkey = preg_replace('/options_/', '', $key);
2614  //var_dump($shortkey); var_dump($extrafields->attributes[$this->element]['unique'][$shortkey]);
2615  if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
2616  //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
2617  unset($clonedObj->array_options[$key]);
2618  }
2619  }
2620  }
2621 
2622  if (!getDolGlobalString('CONTRACT_ADDON') || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').".php")) {
2623  $this->error = 'ErrorSetupNotComplete';
2624  dol_syslog($this->error);
2625  return -1;
2626  }
2627 
2628  // Set ref
2629  require_once DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').'.php';
2630  $obj = getDolGlobalString('CONTRACT_ADDON');
2631  $modContract = new $obj();
2632  '@phan-var-force CommonNumRefGenerator $modContrat';
2633  $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2634 
2635  // get extrafields so they will be clone
2636  foreach ($this->lines as $line) {
2637  $line->fetch_optionals($line->id);
2638  }
2639 
2640  // Create clone
2641  $clonedObj->context['createfromclone'] = 'createfromclone';
2642  $result = $clonedObj->create($user);
2643  if ($result < 0) {
2644  $error++;
2645  $this->error = $clonedObj->error;
2646  $this->errors[] = $clonedObj->error;
2647  } else {
2648  // copy external contacts if same company
2649  if ($this->socid == $clonedObj->socid) {
2650  if ($clonedObj->copy_linked_contact($this, 'external') < 0) {
2651  $error++;
2652  }
2653  }
2654  }
2655 
2656  if (!$error) {
2657  foreach ($this->lines as $line) {
2658  $result = $clonedObj->addline($line->description, $line->subprice, $line->qty, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->fk_product, $line->remise_percent, $line->date_start, $line->date_cloture, 'HT', 0, $line->info_bits, $line->fk_fournprice, $line->pa_ht, $line->array_options, $line->fk_unit, $line->rang);
2659  if ($result < 0) {
2660  $error++;
2661  $this->error = $clonedObj->error;
2662  $this->errors[] = $clonedObj->error;
2663  }
2664  }
2665  }
2666 
2667  if (!$error) {
2668  // Hook of thirdparty module
2669  if (is_object($hookmanager)) {
2670  $parameters = array(
2671  'objFrom' => $this,
2672  'clonedObj' => $clonedObj
2673  );
2674  $action = '';
2675  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $clonedObj, $action); // Note that $action and $object may have been modified by some hooks
2676  if ($reshook < 0) {
2677  $this->setErrorsFromObject($hookmanager);
2678  $error++;
2679  }
2680  }
2681  }
2682 
2683  unset($clonedObj->context['createfromclone']);
2684 
2685  // End
2686  if (!$error) {
2687  $this->db->commit();
2688  return $clonedObj->id;
2689  } else {
2690  $this->db->rollback();
2691  return -1;
2692  }
2693  }
2694 
2695 
2705  public function doAutoRenewContracts($thirdparty_id = 0, $delayindaysshort = 0)
2706  {
2707  global $langs, $user;
2708 
2709  $langs->load("agenda");
2710 
2711  $now = dol_now();
2712 
2713  $enddatetoscan = dol_time_plus_duree($now, -1 * abs($delayindaysshort), 'd');
2714 
2715  $error = 0;
2716  $this->output = '';
2717  $this->error = '';
2718 
2719  $contractlineprocessed = array();
2720  $contractignored = array();
2721  $contracterror = array();
2722 
2723  dol_syslog(__METHOD__, LOG_DEBUG);
2724 
2725  $sql = 'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2726  $sql .= ' FROM '.MAIN_DB_PREFIX.'contrat as c, '.MAIN_DB_PREFIX.'contratdet as cd';
2727  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = cd.fk_product';
2728  $sql .= ' WHERE cd.fk_contrat = c.rowid';
2729  $sql .= " AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan)."', '%Y-%m-%d')";
2730  $sql .= " AND cd.statut = 4";
2731  if ($thirdparty_id > 0) {
2732  $sql .= " AND c.fk_soc = ".((int) $thirdparty_id);
2733  }
2734  //print $sql;
2735 
2736  $resql = $this->db->query($sql);
2737  if ($resql) {
2738  $num = $this->db->num_rows($resql);
2739 
2740  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2741 
2742  $i = 0;
2743  while ($i < $num) {
2744  $obj = $this->db->fetch_object($resql);
2745  if ($obj) {
2746  if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2747  continue;
2748  }
2749 
2750  // Load contract
2751  $object = new Contrat($this->db);
2752  $object->fetch($obj->rowid); // fetch also lines
2753  //$object->fetch_thirdparty();
2754 
2755  if ($object->id <= 0) {
2756  $error++;
2757  $this->errors[] = 'Failed to load contract with id='.$obj->rowid;
2758  continue;
2759  }
2760 
2761  dol_syslog("* Process contract line in doRenewalContracts for contract id=".$object->id." ref=".$object->ref." ref_customer=".$object->ref_customer." contract line id=".$obj->lid);
2762 
2763  // Update expiration date of line
2764  $expirationdate = $this->db->jdate($obj->date_fin_validite);
2765  $duration_value = preg_replace('/[^0-9]/', '', $obj->duration);
2766  $duration_unit = preg_replace('/\d/', '', $obj->duration);
2767  //var_dump($expirationdate.' '.$enddatetoscan);
2768 
2769  // Load linked ->linkedObjects (objects linked)
2770  // @TODO Comment this line and then make the search if there is n open invoice(s) by doing a dedicated SQL COUNT request to fill $contractcanceled.
2771  $object->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
2772 
2773  // Test if there is at least 1 open invoice
2774  if (isset($object->linkedObjects['facture']) && is_array($object->linkedObjects['facture']) && count($object->linkedObjects['facture']) > 0) {
2775  // Sort array of linked invoices by ascending date
2776  usort($object->linkedObjects['facture'], array('Contrat', 'contractCmpDate'));
2777  //dol_sort_array($object->linkedObjects['facture'], 'date');
2778 
2779  $someinvoicenotpaid = 0;
2780  foreach ($object->linkedObjects['facture'] as $idinvoice => $invoice) {
2781  if ($invoice->statut == Facture::STATUS_DRAFT) {
2782  continue;
2783  } // Draft invoice are not invoice not paid
2784 
2785  if (empty($invoice->paye)) {
2786  $someinvoicenotpaid++;
2787  }
2788  }
2789  if ($someinvoicenotpaid) {
2790  $this->output .= 'Contract '.$object->ref.' is qualified for renewal but there is '.$someinvoicenotpaid.' invoice(s) unpayed so we cancel renewal'."\n";
2791  $contractignored[$object->id] = $object->ref;
2792  continue;
2793  }
2794  }
2795 
2796  if ($expirationdate && $expirationdate < $enddatetoscan) {
2797  dol_syslog("Define the newdate of end of services from expirationdate=".$expirationdate);
2798  $newdate = $expirationdate;
2799  $protecti = 0; //$protecti is to avoid infinite loop
2800  while ($newdate < $enddatetoscan && $protecti < 1000) {
2801  $newdate = dol_time_plus_duree($newdate, $duration_value, $duration_unit);
2802  $protecti++;
2803  }
2804 
2805  if ($protecti < 1000) { // If not, there is a pb
2806  // We will update the end of date of contrat, so first we refresh contract data
2807  dol_syslog("We will update the end of date of contract with newdate = ".dol_print_date($newdate, 'dayhourrfc'));
2808 
2809  $this->db->begin();
2810 
2811  $errorforlocaltransaction = 0;
2812 
2813  $label = 'Renewal of contrat '.$object->ref.' line '.$obj->lid;
2814  $comment = 'Renew date of contract '.$object->ref.' line '.$obj->lid.' by doAutoRenewContracts';
2815 
2816  $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."contratdet SET date_fin_validite = '".$this->db->idate($newdate)."'";
2817  $sqlupdate .= ' WHERE rowid = '.((int) $obj->lid);
2818  $resqlupdate = $this->db->query($sqlupdate);
2819  if ($resqlupdate) {
2820  $contractlineprocessed[$obj->lid] = $object->ref;
2821 
2822  $actioncode = 'RENEW_CONTRACT';
2823  $now = dol_now();
2824 
2825  // Create an event
2826  $actioncomm = new ActionComm($this->db);
2827  $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2828  $actioncomm->code = 'AC_'.$actioncode;
2829  $actioncomm->label = $label;
2830  $actioncomm->datep = $now;
2831  $actioncomm->datef = $now;
2832  $actioncomm->percentage = -1; // Not applicable
2833  $actioncomm->socid = $object->socid;
2834  $actioncomm->authorid = $user->id; // User saving action
2835  $actioncomm->userownerid = $user->id; // Owner of action
2836  $actioncomm->fk_element = $object->id;
2837  $actioncomm->elementtype = 'contract';
2838  $actioncomm->note_private = $comment;
2839 
2840  $ret = $actioncomm->create($user); // User creating action
2841  } else {
2842  $contracterror[$object->id] = $object->ref;
2843 
2844  $error++;
2845  $errorforlocaltransaction++;
2846  $this->error = $this->db->lasterror();
2847  }
2848 
2849  if (! $errorforlocaltransaction) {
2850  $this->db->commit();
2851  } else {
2852  $this->db->rollback();
2853  }
2854  } else {
2855  $error++;
2856  $this->error = "Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate." enddatetoscan=".$enddatetoscan." duration_value=".$duration_value." duration_unit=".$duration_value;
2857  dol_syslog($this->error, LOG_ERR);
2858  }
2859  }
2860  }
2861  $i++;
2862  }
2863  } else {
2864  $error++;
2865  $this->error = $this->db->lasterror();
2866  }
2867 
2868  $this->output .= count($contractlineprocessed).' contract line(s) with end date before '.dol_print_date($enddatetoscan, 'day').' were renewed'.(count($contractlineprocessed) > 0 ? ' : '.implode(',', $contractlineprocessed) : '');
2869 
2870  return ($error ? 1 : 0);
2871  }
2872 
2880  public static function contractCmpDate($a, $b)
2881  {
2882  if ($a->date == $b->date) {
2883  return strcmp((string) $a->id, (string) $b->id);
2884  }
2885  return ($a->date < $b->date) ? -1 : 1;
2886  }
2887 
2895  public function getKanbanView($option = '', $arraydata = null)
2896  {
2897  global $langs;
2898 
2899  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2900 
2901  $return = '<div class="box-flex-item box-flex-grow-zero">';
2902  $return .= '<div class="info-box info-box-sm">';
2903  $return .= '<span class="info-box-icon bg-infobox-action">';
2904  $return .= img_picto('', $this->picto);
2905  $return .= '</span>';
2906  $return .= '<div class="info-box-content">';
2907  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2908  if ($selected >= 0) {
2909  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2910  }
2911  if (!empty($arraydata['thirdparty'])) {
2912  $tmpthirdparty = $arraydata['thirdparty'];
2913  $return .= '<br><div class="info-box-label inline-block valignmiddle">'.$tmpthirdparty->getNomUrl(1).'</div>';
2914  }
2915  if (property_exists($this, 'date_contrat')) {
2916  $return .= '<br><span class="opacitymedium valignmiddle">'.$langs->trans("DateContract").' : </span><span class="info-box-label valignmiddle">'.dol_print_date($this->date_contrat, 'day').'</span>';
2917  }
2918  if (method_exists($this, 'getLibStatut')) {
2919  $return .= '<br><div class="info-box-status valignmiddle">'.$this->getLibStatut(7).'</div>';
2920  }
2921  $return .= '</div>';
2922  $return .= '</div>';
2923  $return .= '</div>';
2924 
2925  return $return;
2926  }
2927 
2928  // @Todo getLibSignedStatus, LibSignedStatus
2929 
2939  public function setSignedStatus(User $user, int $status = 0, int $notrigger = 0, $triggercode = ''): int
2940  {
2941  return $this->setSignedStatusCommon($user, $status, $notrigger, $triggercode);
2942  }
2943 }
2944 
2945 
2950 {
2954  public $element = 'contratdet';
2955 
2959  public $table_element = 'contratdet';
2960 
2964  public $parent_element = 'contrat';
2965 
2969  public $fk_parent_attribute = 'fk_contrat';
2970 
2975  public $element_for_permission = 'contrat';
2976 
2980  public $id;
2981 
2985  public $ref;
2986 
2990  public $fk_contrat;
2991 
2995  public $fk_product;
2996 
2997  public $statut; // 0 inactive, 4 active, 5 closed
2998  public $type; // 0 for product, 1 for service
2999 
3004  public $label;
3005 
3010  public $libelle;
3011 
3015  public $description;
3016 
3017  public $product_type; // 0 for product, 1 for service
3018  public $product_ref;
3019  public $product_label;
3020 
3021  public $date_commande;
3022 
3023  public $date_start; // date start planned
3024  public $date_start_real; // date start real
3025  public $date_end; // date end planned
3026  public $date_end_real; // date end real
3027 
3028  public $tva_tx;
3029  public $vat_src_code;
3030  public $localtax1_tx;
3031  public $localtax2_tx;
3032  public $localtax1_type; // Local tax 1 type
3033  public $localtax2_type; // Local tax 2 type
3034  public $qty;
3035  public $remise_percent;
3036  public $remise;
3037 
3041  public $fk_remise_except;
3042 
3047  public $subprice;
3048 
3054  public $price;
3055 
3059  public $price_ht;
3060 
3061  public $total_ht;
3062  public $total_tva;
3063  public $total_localtax1;
3064  public $total_localtax2;
3065  public $total_ttc;
3066 
3070  public $fk_fournprice;
3071 
3072  public $pa_ht;
3073 
3077  public $info_bits;
3078 
3082  public $fk_user_author;
3083 
3087  public $fk_user_ouverture;
3088 
3092  public $fk_user_cloture;
3093 
3097  public $commentaire;
3098 
3099 
3103  public $rang = 0;
3104 
3105 
3106  const STATUS_INITIAL = 0;
3107  const STATUS_OPEN = 4;
3108  const STATUS_CLOSED = 5;
3109 
3110 
3111  // BEGIN MODULEBUILDER PROPERTIES
3115  public $fields = array(
3116  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
3117  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
3118  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
3119  'qty' => array('type' => 'integer', 'label' => 'Quantity', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 35, 'isameasure' => 1),
3120  'total_ht' => array('type' => 'integer', 'label' => 'AmountHT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 36, 'isameasure' => 1),
3121  'total_tva' => array('type' => 'integer', 'label' => 'AmountVAT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 37, 'isameasure' => 1),
3122  'total_ttc' => array('type' => 'integer', 'label' => 'AmountTTC', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 38, 'isameasure' => 1),
3123  //'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
3124  //'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
3125  'fk_contrat' => array('type' => 'integer:Contrat:contrat/class/contrat.class.php', 'label' => 'Contract', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
3126  'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'position' => 75),
3127  //'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
3128  'note_private' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105),
3129  'note_public' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110),
3130  //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
3131  //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
3132  //'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
3133  'fk_user_ouverture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserStartingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
3134  'fk_user_cloture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserClosingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
3135  'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'arrayofkeyval' => array(0 => 'Draft', 4 => 'Open', 5 => 'Closed')),
3136  'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'position' => 500, 'default' => '0')
3137  );
3138  // END MODULEBUILDER PROPERTIES
3139 
3140 
3146  public function __construct($db)
3147  {
3148  $this->db = $db;
3149  }
3150 
3151 
3158  public function getLibStatut($mode)
3159  {
3160  return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1));
3161  }
3162 
3163  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3174  public static function LibStatut($status, $mode, $expired = -1, $moreatt = '', $morelabel = '')
3175  {
3176  // phpcs:enable
3177  global $langs;
3178  $langs->load("contracts");
3179 
3180  if ($status == self::STATUS_INITIAL) {
3181  $labelStatus = $langs->transnoentities("ServiceStatusInitial");
3182  $labelStatusShort = $langs->transnoentities("ServiceStatusInitial");
3183  } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3184  $labelStatus = $langs->transnoentities("ServiceStatusRunning");
3185  $labelStatusShort = $langs->transnoentities("ServiceStatusRunning");
3186  } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3187  $labelStatus = $langs->transnoentities("ServiceStatusNotLate");
3188  $labelStatusShort = $langs->transnoentities("ServiceStatusNotLateShort");
3189  } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3190  $labelStatus = $langs->transnoentities("ServiceStatusLate");
3191  $labelStatusShort = $langs->transnoentities("ServiceStatusLateShort");
3192  } elseif ($status == self::STATUS_CLOSED) {
3193  $labelStatus = $langs->transnoentities("ServiceStatusClosed");
3194  $labelStatusShort = $langs->transnoentities("ServiceStatusClosed");
3195  }
3196 
3197  $statusType = 'status'.$status;
3198  if ($status == self::STATUS_OPEN && $expired == 1) {
3199  $statusType = 'status1';
3200  }
3201  if ($status == self::STATUS_CLOSED) {
3202  $statusType = 'status6';
3203  }
3204 
3205  $params = array();
3206  $reg = array();
3207  if (preg_match('/class="(.*)"/', $moreatt, $reg)) {
3208  $params = array('badgeParams' => array('css' => $reg[1]));
3209  }
3210  return dolGetStatus($labelStatus.($morelabel ? ' '.$morelabel : ''), $labelStatusShort.($morelabel ? ' '.$morelabel : ''), '', $statusType, $mode, '', $params);
3211  }
3212 
3219  public function getTooltipContentArray($params)
3220  {
3221  global $conf, $langs, $user;
3222 
3223  $datas = [];
3224  $datas['label'] = $langs->trans("ShowContractOfService").': '.$this->label;
3225  if (empty($datas['label'])) {
3226  $datas['label'] = $this->description;
3227  }
3228 
3229  return $datas;
3230  }
3231 
3239  public function getNomUrl($withpicto = 0, $maxlength = 0)
3240  {
3241  global $langs;
3242 
3243  $result = '';
3244  $label = $langs->trans("ShowContractOfService").': '.$this->label;
3245  if (empty($label)) {
3246  $label = $this->description;
3247  }
3248  $classfortooltip = 'classfortooltip';
3249  $dataparams = '';
3250  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
3251  $params = [
3252  'id' => $this->fk_contrat,
3253  'objecttype' => $this->element,
3254  ];
3255  $classfortooltip = 'classforajaxtooltip';
3256  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
3257  $label = '';
3258  }
3259 
3260  $link = '<a href="'.DOL_URL_ROOT.'/contrat/card.php?id='.$this->fk_contrat.'"';
3261  $link .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
3262  $link .= $dataparams.' class="'.$classfortooltip.'">';
3263  $linkend = '</a>';
3264 
3265  $picto = 'service';
3266  if ($this->type == 0) {
3267  $picto = 'product';
3268  }
3269 
3270  if ($withpicto) {
3271  $result .= ($link.img_object($label, $picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
3272  }
3273  if ($withpicto && $withpicto != 2) {
3274  $result .= ' ';
3275  }
3276  if ($withpicto != 2) {
3277  $result .= $link.($this->product_ref ? $this->product_ref.' ' : '').($this->label ? $this->label : $this->description).$linkend;
3278  }
3279  return $result;
3280  }
3281 
3289  public function fetch($id, $ref = '')
3290  {
3291  // Check parameters
3292  if (empty($id) && empty($ref)) {
3293  return -1;
3294  }
3295 
3296  $sql = "SELECT";
3297  $sql .= " t.rowid,";
3298  $sql .= " t.tms,";
3299  $sql .= " t.fk_contrat,";
3300  $sql .= " t.fk_product,";
3301  $sql .= " t.statut,";
3302  $sql .= " t.label,"; // This field is not used. Only label of product
3303  $sql .= " p.ref as product_ref,";
3304  $sql .= " p.label as product_label,";
3305  $sql .= " p.description as product_desc,";
3306  $sql .= " p.fk_product_type as product_type,";
3307  $sql .= " t.description,";
3308  $sql .= " t.date_commande,";
3309  $sql .= " t.date_ouverture_prevue as date_start,";
3310  $sql .= " t.date_ouverture as date_start_real,";
3311  $sql .= " t.date_fin_validite as date_end,";
3312  $sql .= " t.date_cloture as date_end_real,";
3313  $sql .= " t.tva_tx,";
3314  $sql .= " t.vat_src_code,";
3315  $sql .= " t.localtax1_tx,";
3316  $sql .= " t.localtax2_tx,";
3317  $sql .= " t.localtax1_type,";
3318  $sql .= " t.localtax2_type,";
3319  $sql .= " t.qty,";
3320  $sql .= " t.remise_percent,";
3321  $sql .= " t.remise,";
3322  $sql .= " t.fk_remise_except,";
3323  $sql .= " t.subprice,";
3324  $sql .= " t.price_ht,";
3325  $sql .= " t.total_ht,";
3326  $sql .= " t.total_tva,";
3327  $sql .= " t.total_localtax1,";
3328  $sql .= " t.total_localtax2,";
3329  $sql .= " t.total_ttc,";
3330  $sql .= " t.fk_product_fournisseur_price as fk_fournprice,";
3331  $sql .= " t.buy_price_ht as pa_ht,";
3332  $sql .= " t.info_bits,";
3333  $sql .= " t.fk_user_author,";
3334  $sql .= " t.fk_user_ouverture,";
3335  $sql .= " t.fk_user_cloture,";
3336  $sql .= " t.commentaire,";
3337  $sql .= " t.fk_unit,";
3338  $sql .= " t.rang";
3339  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as t LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = t.fk_product";
3340  if ($id) {
3341  $sql .= " WHERE t.rowid = ".((int) $id);
3342  }
3343  if ($ref) {
3344  $sql .= " WHERE t.rowid = '".$this->db->escape($ref)."'";
3345  }
3346 
3347  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
3348  $resql = $this->db->query($sql);
3349  if ($resql) {
3350  if ($this->db->num_rows($resql)) {
3351  $obj = $this->db->fetch_object($resql);
3352 
3353  $this->id = $obj->rowid;
3354  $this->ref = $obj->rowid;
3355 
3356  $this->tms = $this->db->jdate($obj->tms);
3357  $this->fk_contrat = $obj->fk_contrat;
3358  $this->fk_product = $obj->fk_product;
3359  $this->statut = $obj->statut;
3360  $this->product_ref = $obj->product_ref;
3361  $this->product_label = $obj->product_label;
3362  $this->product_type = $obj->product_type;
3363  $this->label = $obj->label; // deprecated. We do not use this field. Only ref and label of product, and description of contract line
3364  $this->description = $obj->description;
3365  $this->date_commande = $this->db->jdate($obj->date_commande);
3366 
3367  $this->date_start = $this->db->jdate($obj->date_start);
3368  $this->date_start_real = $this->db->jdate($obj->date_start_real);
3369  $this->date_end = $this->db->jdate($obj->date_end);
3370  $this->date_end_real = $this->db->jdate($obj->date_end_real);
3371  // For backward compatibility
3372  //$this->date_ouverture_prevue = $this->db->jdate($obj->date_ouverture_prevue);
3373  //$this->date_ouverture = $this->db->jdate($obj->date_ouverture);
3374  //$this->date_fin_validite = $this->db->jdate($obj->date_fin_validite);
3375  //$this->date_cloture = $this->db->jdate($obj->date_cloture);
3376 
3377  $this->tva_tx = $obj->tva_tx;
3378  $this->vat_src_code = $obj->vat_src_code;
3379  $this->localtax1_tx = $obj->localtax1_tx;
3380  $this->localtax2_tx = $obj->localtax2_tx;
3381  $this->localtax1_type = $obj->localtax1_type;
3382  $this->localtax2_type = $obj->localtax2_type;
3383  $this->qty = $obj->qty;
3384  $this->remise_percent = $obj->remise_percent;
3385  $this->fk_remise_except = $obj->fk_remise_except;
3386  $this->subprice = $obj->subprice;
3387  $this->price_ht = $obj->price_ht;
3388  $this->total_ht = $obj->total_ht;
3389  $this->total_tva = $obj->total_tva;
3390  $this->total_localtax1 = $obj->total_localtax1;
3391  $this->total_localtax2 = $obj->total_localtax2;
3392  $this->total_ttc = $obj->total_ttc;
3393  $this->info_bits = $obj->info_bits;
3394  $this->fk_user_author = $obj->fk_user_author;
3395  $this->fk_user_ouverture = $obj->fk_user_ouverture;
3396  $this->fk_user_cloture = $obj->fk_user_cloture;
3397  $this->commentaire = $obj->commentaire;
3398  $this->fk_fournprice = $obj->fk_fournprice;
3399 
3400  $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3401  $this->pa_ht = $marginInfos[0];
3402  $this->fk_unit = $obj->fk_unit;
3403 
3404  $this->rang = $obj->rang;
3405 
3406  $this->fetch_optionals();
3407  }
3408 
3409  $this->db->free($resql);
3410 
3411  return 1;
3412  } else {
3413  $this->error = "Error ".$this->db->lasterror();
3414  return -1;
3415  }
3416  }
3417 
3418 
3426  public function update($user, $notrigger = 0)
3427  {
3428  global $mysoc;
3429 
3430  $error = 0;
3431 
3432  // Clean parameters
3433  $this->fk_contrat = (int) $this->fk_contrat;
3434  $this->fk_product = (int) $this->fk_product;
3435  $this->statut = (int) $this->statut;
3436  $this->label = trim($this->label);
3437  $this->description = trim($this->description);
3438  $this->vat_src_code = trim($this->vat_src_code);
3439  $this->tva_tx = trim((string) $this->tva_tx);
3440  $this->localtax1_tx = trim($this->localtax1_tx);
3441  $this->localtax2_tx = trim($this->localtax2_tx);
3442  $this->qty = (float) $this->qty;
3443  $this->remise_percent = trim((string) $this->remise_percent);
3444  $this->fk_remise_except = (int) $this->fk_remise_except;
3445  $this->subprice = (float) price2num($this->subprice);
3446  $this->price_ht = (float) price2num($this->price_ht);
3447  $this->info_bits = (int) $this->info_bits;
3448  $this->fk_user_author = (int) $this->fk_user_author;
3449  $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3450  $this->fk_user_cloture = (int) $this->fk_user_cloture;
3451  $this->commentaire = trim($this->commentaire);
3452  $this->rang = (int) $this->rang;
3453  //if (empty($this->subprice)) $this->subprice = 0;
3454  if (empty($this->price_ht)) {
3455  $this->price_ht = 0;
3456  }
3457  if (empty($this->total_ht)) {
3458  $this->total_ht = 0;
3459  }
3460  if (empty($this->total_tva)) {
3461  $this->total_tva = 0;
3462  }
3463  if (empty($this->total_ttc)) {
3464  $this->total_ttc = 0;
3465  }
3466  if (empty($this->localtax1_tx)) {
3467  $this->localtax1_tx = 0;
3468  }
3469  if (empty($this->localtax2_tx)) {
3470  $this->localtax2_tx = 0;
3471  }
3472  if (empty($this->remise_percent)) {
3473  $this->remise_percent = 0;
3474  }
3475 
3476  // Calcul du total TTC et de la TVA pour la ligne a partir de
3477  // qty, pu, remise_percent et txtva
3478  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
3479  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
3480  $localtaxes_type = getLocalTaxesFromRate($this->tva_tx, 0, $this->thirdparty, $mysoc);
3481 
3482  $tabprice = calcul_price_total($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, 'HT', 0, 1, $mysoc, $localtaxes_type);
3483  $this->total_ht = $tabprice[0];
3484  $this->total_tva = $tabprice[1];
3485  $this->total_ttc = $tabprice[2];
3486  $this->total_localtax1 = $tabprice[9];
3487  $this->total_localtax2 = $tabprice[10];
3488 
3489  if (empty($this->pa_ht)) {
3490  $this->pa_ht = 0;
3491  }
3492 
3493  // if buy price not defined, define buyprice as configured in margin admin
3494  if ($this->pa_ht == 0) {
3495  $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
3496  if ($result < 0) {
3497  return -1;
3498  } else {
3499  $this->pa_ht = $result;
3500  }
3501  }
3502 
3503  // $this->oldcopy should have been set by the caller of update (here properties were already modified)
3504  if (empty($this->oldcopy)) {
3505  dol_syslog("this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
3506  $this->oldcopy = dol_clone($this, 2);
3507  }
3508 
3509  $this->db->begin();
3510 
3511  // Update request
3512  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3513  $sql .= " fk_contrat = ".((int) $this->fk_contrat).",";
3514  $sql .= " fk_product = ".($this->fk_product ? ((int) $this->fk_product) : 'null').",";
3515  $sql .= " statut = ".((int) $this->statut).",";
3516  $sql .= " label = '".$this->db->escape($this->label)."',";
3517  $sql .= " description = '".$this->db->escape($this->description)."',";
3518  $sql .= " date_commande = ".($this->date_commande != '' ? "'".$this->db->idate($this->date_commande)."'" : "null").",";
3519  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3520  $sql .= " date_ouverture = ".($this->date_start_real != '' ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3521  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3522  $sql .= " date_cloture = ".($this->date_end_real != '' ? "'".$this->db->idate($this->date_end_real)."'" : "null").",";
3523  $sql .= " vat_src_code = '".$this->db->escape($this->vat_src_code)."',";
3524  $sql .= " tva_tx = ".price2num($this->tva_tx).",";
3525  $sql .= " localtax1_tx = ".price2num($this->localtax1_tx).",";
3526  $sql .= " localtax2_tx = ".price2num($this->localtax2_tx).",";
3527  $sql .= " qty = ".price2num($this->qty).",";
3528  $sql .= " remise_percent = ".price2num($this->remise_percent).",";
3529  $sql .= " remise = ".($this->remise ? price2num($this->remise) : "null").",";
3530  $sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except : "null").",";
3531  $sql .= " subprice = ".($this->subprice != '' ? $this->subprice : "null").",";
3532  $sql .= " price_ht = ".($this->price_ht != '' ? $this->price_ht : "null").",";
3533  $sql .= " total_ht = ".$this->total_ht.",";
3534  $sql .= " total_tva = ".$this->total_tva.",";
3535  $sql .= " total_localtax1 = ".$this->total_localtax1.",";
3536  $sql .= " total_localtax2 = ".$this->total_localtax2.",";
3537  $sql .= " total_ttc = ".$this->total_ttc.",";
3538  $sql .= " fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "NULL").",";
3539  $sql .= " buy_price_ht = '".price2num($this->pa_ht)."',";
3540  $sql .= " info_bits = '".$this->db->escape($this->info_bits)."',";
3541  $sql .= " fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author : "NULL").",";
3542  $sql .= " fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture : "NULL").",";
3543  $sql .= " fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture : "NULL").",";
3544  $sql .= " commentaire = '".$this->db->escape($this->commentaire)."',";
3545  $sql .= " fk_unit = ".(!$this->fk_unit ? 'NULL' : $this->fk_unit).",";
3546  $sql .= " rang = ".(empty($this->rang) ? '0' : (int) $this->rang);
3547  $sql .= " WHERE rowid = ".((int) $this->id);
3548 
3549  dol_syslog(get_class($this)."::update", LOG_DEBUG);
3550  $resql = $this->db->query($sql);
3551  if (!$resql) {
3552  $this->error = "Error ".$this->db->lasterror();
3553  $error++;
3554  }
3555 
3556  if (!$error) { // For avoid conflicts if trigger used
3557  $result = $this->insertExtraFields();
3558  if ($result < 0) {
3559  $error++;
3560  }
3561  }
3562 
3563  // If we change a planned date (start or end) of one contract line, sync dates for all other services too
3564  if (!$error && getDolGlobalString('CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES')) {
3565  dol_syslog(get_class($this)."::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3566 
3567  if ($this->date_start != $this->oldcopy->date_start) {
3568  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3569  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null");
3570  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3571 
3572  $resql = $this->db->query($sql);
3573  if (!$resql) {
3574  $error++;
3575  $this->error = "Error ".$this->db->lasterror();
3576  }
3577  }
3578  if ($this->date_end != $this->oldcopy->date_end) {
3579  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3580  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null");
3581  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3582 
3583  $resql = $this->db->query($sql);
3584  if (!$resql) {
3585  $error++;
3586  $this->error = "Error ".$this->db->lasterror();
3587  }
3588  }
3589  }
3590 
3591  if (!$error && !$notrigger) {
3592  // Call trigger
3593  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
3594  if ($result < 0) {
3595  $error++;
3596  $this->db->rollback();
3597  }
3598  // End call triggers
3599  }
3600 
3601  if (!$error) {
3602  $this->db->commit();
3603  return 1;
3604  } else {
3605  $this->db->rollback();
3606  $this->errors[] = $this->error;
3607  return -1;
3608  }
3609  }
3610 
3611 
3612  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3619  public function update_total()
3620  {
3621  // phpcs:enable
3622  $this->db->begin();
3623 
3624  // Mise a jour ligne en base
3625  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3626  $sql .= " total_ht=".price2num($this->total_ht, 'MT');
3627  $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
3628  $sql .= ",total_localtax1=".price2num($this->total_localtax1, 'MT');
3629  $sql .= ",total_localtax2=".price2num($this->total_localtax2, 'MT');
3630  $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
3631  $sql .= " WHERE rowid = ".((int) $this->id);
3632 
3633  dol_syslog(get_class($this)."::update_total", LOG_DEBUG);
3634 
3635  $resql = $this->db->query($sql);
3636  if ($resql) {
3637  $this->db->commit();
3638  return 1;
3639  } else {
3640  $this->error = $this->db->error();
3641  $this->db->rollback();
3642  return -2;
3643  }
3644  }
3645 
3646 
3653  public function insert($notrigger = 0)
3654  {
3655  global $user;
3656 
3657  $error = 0;
3658 
3659  // Insertion dans la base
3660  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
3661  $sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3662  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3663  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3664  $sql .= " info_bits,";
3665  $sql .= " rang,";
3666  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3667  if ($this->date_start > 0) {
3668  $sql .= ",date_ouverture_prevue";
3669  }
3670  if ($this->date_end > 0) {
3671  $sql .= ",date_fin_validite";
3672  }
3673  $sql .= ") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->description)."',";
3674  $sql .= ($this->fk_product > 0 ? $this->fk_product : "null").",";
3675  $sql .= " '".$this->db->escape($this->qty)."',";
3676  $sql .= " '".$this->db->escape($this->vat_src_code)."',";
3677  $sql .= " '".$this->db->escape($this->tva_tx)."',";
3678  $sql .= " '".$this->db->escape($this->localtax1_tx)."',";
3679  $sql .= " '".$this->db->escape($this->localtax2_tx)."',";
3680  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3681  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3682  $sql .= " ".price2num($this->remise_percent).",".price2num($this->subprice).",";
3683  $sql .= " ".price2num($this->total_ht).",".price2num($this->total_tva).",".price2num($this->total_localtax1).",".price2num($this->total_localtax2).",".price2num($this->total_ttc).",";
3684  $sql .= " '".$this->db->escape($this->info_bits)."',";
3685  $sql .= " ".(empty($this->rang) ? '0' : (int) $this->rang).",";
3686  $sql .= " ".price2num($this->price_ht).",".price2num($this->remise).",";
3687  if ($this->fk_fournprice > 0) {
3688  $sql .= ' '.((int) $this->fk_fournprice).',';
3689  } else {
3690  $sql .= ' null,';
3691  }
3692  if ($this->pa_ht > 0) {
3693  $sql .= ' '.((float) price2num($this->pa_ht));
3694  } else {
3695  $sql .= ' null';
3696  }
3697  if ($this->date_start > 0) {
3698  $sql .= ",'".$this->db->idate($this->date_start)."'";
3699  }
3700  if ($this->date_end > 0) {
3701  $sql .= ",'".$this->db->idate($this->date_end)."'";
3702  }
3703  $sql .= ")";
3704 
3705  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3706 
3707  $resql = $this->db->query($sql);
3708  if ($resql) {
3709  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'contratdet');
3710 
3711  // Insert of extrafields
3712  if (!$error) {
3713  $result = $this->insertExtraFields();
3714  if ($result < 0) {
3715  $this->db->rollback();
3716  return -1;
3717  }
3718  }
3719 
3720  if (!$notrigger) {
3721  // Call trigger
3722  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
3723  if ($result < 0) {
3724  $this->db->rollback();
3725  return -1;
3726  }
3727  // End call triggers
3728  }
3729 
3730  $this->db->commit();
3731  return 1;
3732  } else {
3733  $this->db->rollback();
3734  $this->error = $this->db->error()." sql=".$sql;
3735  return -1;
3736  }
3737  }
3738 
3739  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3749  public function active_line($user, $date, $date_end = '', $comment = '')
3750  {
3751  // phpcs:enable
3752  $error = 0;
3753 
3754  $this->db->begin();
3755 
3756  $this->statut = ContratLigne::STATUS_OPEN;
3757  $this->date_start_real = $date;
3758  $this->date_end = $date_end;
3759  $this->fk_user_ouverture = $user->id;
3760  $this->date_end_real = null;
3761  $this->commentaire = $comment;
3762 
3763  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) $this->statut).",";
3764  $sql .= " date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3765  if ($date_end >= 0) {
3766  $sql .= " date_fin_validite = ".(dol_strlen($this->date_end) != 0 ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3767  }
3768  $sql .= " fk_user_ouverture = ".((int) $this->fk_user_ouverture).",";
3769  $sql .= " date_cloture = null,";
3770  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3771  $sql .= " WHERE rowid = ".((int) $this->id)." AND (statut = ".ContratLigne::STATUS_INITIAL." OR statut = ".ContratLigne::STATUS_CLOSED.")";
3772 
3773  dol_syslog(get_class($this)."::active_line", LOG_DEBUG);
3774  $resql = $this->db->query($sql);
3775  if ($resql) {
3776  // Call trigger
3777  $result = $this->call_trigger('LINECONTRACT_ACTIVATE', $user);
3778  if ($result < 0) {
3779  $error++;
3780  }
3781  // End call triggers
3782 
3783  if (!$error) {
3784  $this->db->commit();
3785  return 1;
3786  } else {
3787  $this->db->rollback();
3788  return -1;
3789  }
3790  } else {
3791  $this->error = $this->db->lasterror();
3792  $this->db->rollback();
3793  return -1;
3794  }
3795  }
3796 
3797  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3807  public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)
3808  {
3809  // phpcs:enable
3810  $this->date_cloture = $date_end_real;
3811  $this->date_end_real = $date_end_real;
3812  $this->user_closing_id = $user->id;
3813  $this->commentaire = $comment;
3814 
3815  $error = 0;
3816 
3817  // statut actif : 4
3818 
3819  $this->db->begin();
3820 
3821  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).",";
3822  $sql .= " date_cloture = '".$this->db->idate($date_end_real)."',";
3823  $sql .= " fk_user_cloture = ".((int) $user->id).",";
3824  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3825  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = ".((int) ContratLigne::STATUS_OPEN);
3826 
3827  $resql = $this->db->query($sql);
3828  if ($resql) {
3829  if (!$notrigger) {
3830  // Call trigger
3831  $result = $this->call_trigger('LINECONTRACT_CLOSE', $user);
3832  if ($result < 0) {
3833  $error++;
3834  $this->db->rollback();
3835  return -1;
3836  }
3837  // End call triggers
3838  }
3839 
3840  $this->db->commit();
3841  return 1;
3842  } else {
3843  $this->error = $this->db->lasterror();
3844  $this->db->rollback();
3845  return -1;
3846  }
3847  }
3848 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:624
$object ref
Definition: info.php:79
Class to manage agenda events (actions)
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...
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
getIdContact($source, $code, $status=0)
Return id of contacts for a source and a contact code.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
fetch_project()
Load the project with id $this->fk_project into this->project.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
setSignedStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a signed status.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product 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.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage contracts.
static contractCmpDate($a, $b)
Used to sort lines by date.
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
fetch($id, $ref='', $ref_customer='', $ref_supplier='', $noextrafields=0, $nolines=0)
Load a contract from database.
loadStateBoard()
Load the indicators this->nb for state board.
update_statut($user)
Update statut of contract according to services.
updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0.0, $localtax2tx=0.0, $date_start_real='', $date_end_real='', $price_base_type='HT', $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Mets a jour une ligne de contrat.
getTicketsArray()
Create an array of associated tickets.
setSignedStatus(User $user, int $status=0, int $notrigger=0, $triggercode='')
Set signed status.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
active_line($user, $line_id, $date_start, $date_end='', $comment='')
Activate a contract line.
getTooltipContentArray($params)
getTooltipContentArray
getLinesArray()
Create an array of order lines.
validate(User $user, $force_number='', $notrigger=0)
Validate a contract.
getIdBillingContact()
Return id des contacts clients de facturation.
initAsSpecimen()
Initialise an instance with random values.
create($user)
Create a contract into database.
close_line($user, $line_id, $date_end, $comment='')
Close a contract line.
reopen($user, $notrigger=0)
Unvalidate a contract.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
deleteLine($idline, User $user)
Delete a contract line.
array_detail($status=-1)
Return list of line rowid.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Ajoute une ligne de contrat en base.
update($user, $notrigger=0)
Update object into database.
closeAll(User $user, $notrigger=0, $comment='')
Close all lines of a contract.
$table_ref_field
{}
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getListOfContracts($option='all', $status=[], $product_categories=[], $line_status=[])
Return list of other contracts for the same company than current contract.
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
LibStatut($status, $mode)
Return the label of a given contrat status.
activateAll($user, $date_start='', $notrigger=0, $comment='', $date_end='')
Open all lines of a contract.
__construct($db)
Constructor.
doAutoRenewContracts($thirdparty_id=0, $delayindaysshort=0)
Action executed by scheduler CAN BE A CRON TASK Loop on each contract lines and update the end of dat...
getNomUrl($withpicto=0, $maxlength=0, $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
info($id)
Charge les information d'ordre info dans l'objet contrat.
getLibStatut($mode)
Return label of a contract status.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
fetch_lines($only_services=0, $loadalsotranslation=0, $noextrafields=0)
Load lines array into this->lines.
getIdServiceContact()
Return id des contacts clients de prestation.
getNextNumRef($soc)
Return next contract ref.
Class to manage lines of contracts.
active_line($user, $date, $date_end='', $comment='')
Activate a contract line.
getLibStatut($mode)
Return label of this contract line status.
insert($notrigger=0)
Inserts a contrat line into database.
fetch($id, $ref='')
Load object in memory from database.
close_line($user, $date_end_real, $comment='', $notrigger=0)
Close a contract line.
static LibStatut($status, $mode, $expired=-1, $moreatt='', $morelabel='')
Return label of a contract line status.
update($user, $notrigger=0)
Update database for contract line.
update_total()
Update in database the fields total_xxx of lines Used by migration process.
__construct($db)
Constructor.
getTooltipContentArray($params)
getTooltipContentArray
getNomUrl($withpicto=0, $maxlength=0)
Return clicable name (with picto eventually) for ContratLigne.
Class to manage Dolibarr database access.
Class to manage shipments.
const STATUS_DRAFT
Draft status.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:124
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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:1620
dol_dir_list($utf8_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:63
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
div float
Unit price before taxes.
Definition: style.css.php:963
Class to generate the form for creating a new ticket.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:88
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:139