dolibarr  20.0.0-beta
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_creation;
160 
164  public $date_modification;
165 
169  public $date_validation;
170 
174  public $date_contrat;
175 
180  public $signed_status = 0;
181 
182  public $commercial_signature_id;
183  public $fk_commercial_signature;
184  public $commercial_suivi_id;
185  public $fk_commercial_suivi;
186 
191  public $fk_projet;
192 
193  public $extraparams = array();
194 
198  public $lines = array();
199 
200  public $nbofservices;
201  public $nbofserviceswait;
202  public $nbofservicesopened;
203  public $nbofservicesexpired;
204  public $nbofservicesclosed;
205  //public $lower_planned_end_date;
206  //public $higher_planner_end_date;
207 
212  protected $lines_id_index_mapper = array();
213 
214 
239  // BEGIN MODULEBUILDER PROPERTIES
243  public $fields = array(
244  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
245  'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
246  'ref_ext' => array('type' => 'varchar(255)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 0, 'position' => 20),
247  'ref_customer' => array('type' => 'varchar(50)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => -1, 'position' => 25, 'searchall' => 1),
248  'ref_supplier' => array('type' => 'varchar(50)', 'label' => 'RefSupplier', 'enabled' => 1, 'visible' => -1, 'position' => 26, 'searchall' => 1),
249  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
250  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
251  'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 40),
252  'date_contrat' => array('type' => 'datetime', 'label' => 'Date contrat', 'enabled' => 1, 'visible' => -1, 'position' => 45),
253  'signed_status' => array('type' => 'smallint(6)', 'label' => 'SignedStatus', 'enabled' => 1, 'visible' => -1, 'position' => 50, 'arrayofkeyval' => array(0 => 'NoSignature', 1 => 'SignedSender', 2 => 'SignedReceiver', 9 => 'SignedAll')),
254  'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 70),
255  'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 75),
256  'fk_commercial_signature' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative Signature', 'enabled' => 1, 'visible' => -1, 'position' => 80),
257  'fk_commercial_suivi' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative follower', 'enabled' => 1, 'visible' => -1, 'position' => 85),
258  'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 90),
259  'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105, 'searchall' => 1),
260  'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110, 'searchall' => 1),
261  'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 115),
262  'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 120),
263  'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 125),
264  'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
265  'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'Last main doc', 'enabled' => 1, 'visible' => -1, 'position' => 140),
266  'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'notnull' => 1, 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed'))
267  );
268  // END MODULEBUILDER PROPERTIES
269 
270  const STATUS_DRAFT = 0;
271  const STATUS_VALIDATED = 1;
272  const STATUS_CLOSED = 2;
273 
274  /*
275  * No signature
276  */
277  const STATUS_NO_SIGNATURE = 0;
278 
279  /*
280  * Signed by sender
281  */
282  const STATUS_SIGNED_SENDER = 1;
283 
284  /*
285  * Signed by receiver
286  */
287  const STATUS_SIGNED_RECEIVER = 2;
288 
289  /*
290  * Signed by all
291  */
292  const STATUS_SIGNED_ALL = 9; // To handle future kind of signature (ex: tripartite contract)
293 
294 
300  public function __construct($db)
301  {
302  $this->db = $db;
303 
304  $this->ismultientitymanaged = 1;
305  $this->isextrafieldmanaged = 1;
306  }
307 
314  public function getNextNumRef($soc)
315  {
316  global $db, $langs, $conf;
317  $langs->load("contracts");
318 
319  if (getDolGlobalString('CONTRACT_ADDON')) {
320  $mybool = false;
321 
322  $file = getDolGlobalString('CONTRACT_ADDON') . ".php";
323  $classname = getDolGlobalString('CONTRACT_ADDON');
324 
325  // Include file with class
326  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
327 
328  foreach ($dirmodels as $reldir) {
329  $dir = dol_buildpath($reldir."core/modules/contract/");
330 
331  // Load file with numbering class (if found)
332  $mybool = ((bool) @include_once $dir.$file) || $mybool;
333  }
334 
335  if (!$mybool) {
336  dol_print_error(null, "Failed to include file ".$file);
337  return '';
338  }
339 
340  $obj = new $classname();
341  '@phan-var-force CommonNumRefGenerator $obj';
342  $numref = $obj->getNextValue($soc, $this);
343 
344  if ($numref != "") {
345  return $numref;
346  } else {
347  $this->error = $obj->error;
348  dol_print_error($db, get_class($this)."::getNextValue ".$obj->error);
349  return "";
350  }
351  } else {
352  $langs->load("errors");
353  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Contract"));
354  return "";
355  }
356  }
357 
358  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
369  public function active_line($user, $line_id, $date_start, $date_end = '', $comment = '')
370  {
371  // phpcs:enable
372  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
373  if ($result < 0) {
374  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
375  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
376  }
377  return $result;
378  }
379 
380 
381  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
391  public function close_line($user, $line_id, $date_end, $comment = '')
392  {
393  // phpcs:enable
394  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
395  if ($result < 0) {
396  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
397  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
398  }
399  return $result;
400  }
401 
402 
414  public function activateAll($user, $date_start = '', $notrigger = 0, $comment = '', $date_end = '')
415  {
416  if (empty($date_start)) {
417  $date_start = dol_now();
418  }
419 
420  $this->db->begin();
421 
422  $error = 0;
423 
424  // Load lines
425  $this->fetch_lines();
426 
427  foreach ($this->lines as $contratline) {
428  // Open lines not already open
429  if ($contratline->statut != ContratLigne::STATUS_OPEN) {
430  $contratline->context = $this->context;
431 
432  $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment); // This call trigger LINECONTRACT_ACTIVATE
433  if ($result < 0) {
434  $error++;
435  $this->error = $contratline->error;
436  $this->errors = $contratline->errors;
437  break;
438  }
439  }
440  }
441 
442  if (!$error && $this->statut == 0) {
443  $result = $this->validate($user, '', $notrigger);
444  if ($result < 0) {
445  $error++;
446  }
447  }
448 
449  if (!$error) {
450  $this->db->commit();
451  return 1;
452  } else {
453  $this->db->rollback();
454  return -1;
455  }
456  }
457 
467  public function closeAll(User $user, $notrigger = 0, $comment = '')
468  {
469  $this->db->begin();
470 
471  // Load lines
472  $this->fetch_lines();
473 
474  $now = dol_now();
475 
476  $error = 0;
477 
478  foreach ($this->lines as $contratline) {
479  // Close lines not already closed
480  if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
481  $contratline->date_end_real = $now;
482  $contratline->date_cloture = $now; // For backward compatibility
483  $contratline->user_closing_id = $user->id;
484  $contratline->statut = ContratLigne::STATUS_CLOSED;
485  $result = $contratline->close_line($user, $now, $comment, $notrigger);
486  if ($result < 0) {
487  $error++;
488  $this->error = $contratline->error;
489  $this->errors = $contratline->errors;
490  break;
491  }
492  }
493  }
494 
495  if (!$error && $this->statut == 0) {
496  $result = $this->validate($user, '', $notrigger);
497  if ($result < 0) {
498  $error++;
499  }
500  }
501 
502  if (!$error) {
503  $this->db->commit();
504  return 1;
505  } else {
506  $this->db->rollback();
507  return -1;
508  }
509  }
510 
519  public function validate(User $user, $force_number = '', $notrigger = 0)
520  {
521  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
522  global $conf;
523 
524  $now = dol_now();
525 
526  $error = 0;
527  dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number);
528 
529 
530  $this->db->begin();
531 
532  $this->fetch_thirdparty();
533 
534  // A contract is validated so we can move thirdparty to status customer
535  if (!getDolGlobalString('CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION') && $this->thirdparty->fournisseur == 0) {
536  $result = $this->thirdparty->setAsCustomer();
537  }
538 
539  // Define new ref
540  if ($force_number) {
541  $num = $force_number;
542  } elseif (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
543  $num = $this->getNextNumRef($this->thirdparty);
544  } else {
545  $num = $this->ref;
546  }
547  $this->newref = dol_sanitizeFileName($num);
548 
549  if ($num) {
550  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$this->db->escape($num)."', statut = 1";
551  //$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
552  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 0";
553 
554  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
555  $resql = $this->db->query($sql);
556  if (!$resql) {
557  dol_print_error($this->db);
558  $error++;
559  $this->error = $this->db->lasterror();
560  }
561 
562  // Trigger calls
563  if (!$error && !$notrigger) {
564  // Call trigger
565  $result = $this->call_trigger('CONTRACT_VALIDATE', $user);
566  if ($result < 0) {
567  $error++;
568  }
569  // End call triggers
570  }
571 
572  if (!$error) {
573  $this->oldref = $this->ref;
574 
575  // Rename directory if dir was a temporary ref
576  if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
577  // Now we rename also files into index
578  $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)."'";
579  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
580  $resql = $this->db->query($sql);
581  if (!$resql) {
582  $error++;
583  $this->error = $this->db->lasterror();
584  }
585  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'contract/".$this->db->escape($this->newref)."'";
586  $sql .= " WHERE filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
587  $resql = $this->db->query($sql);
588  if (!$resql) {
589  $error++;
590  $this->error = $this->db->lasterror();
591  }
592 
593  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
594  $oldref = dol_sanitizeFileName($this->ref);
595  $newref = dol_sanitizeFileName($num);
596  $dirsource = $conf->contract->dir_output.'/'.$oldref;
597  $dirdest = $conf->contract->dir_output.'/'.$newref;
598  if (!$error && file_exists($dirsource)) {
599  dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
600 
601  if (@rename($dirsource, $dirdest)) {
602  dol_syslog("Rename ok");
603  // Rename docs starting with $oldref with $newref
604  $listoffiles = dol_dir_list($conf->contract->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
605  foreach ($listoffiles as $fileentry) {
606  $dirsource = $fileentry['name'];
607  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
608  $dirsource = $fileentry['path'].'/'.$dirsource;
609  $dirdest = $fileentry['path'].'/'.$dirdest;
610  @rename($dirsource, $dirdest);
611  }
612  }
613  }
614  }
615  }
616 
617  // Set new ref and define current statut
618  if (!$error) {
619  $this->ref = $num;
620  $this->status = self::STATUS_VALIDATED;
621  $this->statut = self::STATUS_VALIDATED; // deprecated
622  $this->date_validation = $now;
623  }
624  } else {
625  $error++;
626  }
627 
628  if (!$error) {
629  $this->db->commit();
630  return 1;
631  } else {
632  $this->db->rollback();
633  return -1;
634  }
635  }
636 
644  public function reopen($user, $notrigger = 0)
645  {
646  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
647 
648  $now = dol_now();
649 
650  $error = 0;
651  dol_syslog(get_class($this).'::reopen user='.$user->id);
652 
653  $this->db->begin();
654 
655  $this->fetch_thirdparty();
656 
657  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0";
658  //$sql.= ", fk_user_valid = null, date_valid = null";
659  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 1";
660 
661  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
662  $resql = $this->db->query($sql);
663  if (!$resql) {
664  dol_print_error($this->db);
665  $error++;
666  $this->error = $this->db->lasterror();
667  }
668 
669  // Trigger calls
670  if (!$error && !$notrigger) {
671  // Call trigger
672  $result = $this->call_trigger('CONTRACT_REOPEN', $user);
673  if ($result < 0) {
674  $error++;
675  }
676  // End call triggers
677  }
678 
679  // Set new ref and define current status
680  if (!$error) {
681  $this->statut = self::STATUS_DRAFT;
682  $this->status = self::STATUS_DRAFT;
683  $this->date_validation = $now;
684  }
685 
686  if (!$error) {
687  $this->db->commit();
688  return 1;
689  } else {
690  $this->db->rollback();
691  return -1;
692  }
693  }
694 
706  public function fetch($id, $ref = '', $ref_customer = '', $ref_supplier = '', $noextrafields = 0, $nolines = 0)
707  {
708  $sql = "SELECT rowid, statut as status, ref, fk_soc as thirdpartyid,";
709  $sql .= " ref_supplier, ref_customer,";
710  $sql .= " ref_ext,";
711  $sql .= " entity,";
712  $sql .= " date_contrat as datecontrat,";
713  $sql .= " fk_user_author,";
714  $sql .= " fk_projet as fk_project,";
715  $sql .= " fk_commercial_signature, fk_commercial_suivi,";
716  $sql .= " note_private, note_public, model_pdf, last_main_doc, extraparams";
717  $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
718  if (!$id) {
719  $sql .= " WHERE entity IN (".getEntity('contract').")";
720  } else {
721  $sql .= " WHERE rowid = ".(int) $id;
722  }
723  if ($ref_customer) {
724  $sql .= " AND ref_customer = '".$this->db->escape($ref_customer)."'";
725  }
726  if ($ref_supplier) {
727  $sql .= " AND ref_supplier = '".$this->db->escape($ref_supplier)."'";
728  }
729  if ($ref) {
730  $sql .= " AND ref = '".$this->db->escape($ref)."'";
731  }
732 
733  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
734  $resql = $this->db->query($sql);
735  if ($resql) {
736  $num = $this->db->num_rows($resql);
737  if ($num > 1) {
738  $this->error = 'Fetch found several records.';
739  dol_syslog($this->error, LOG_ERR);
740  $result = -2;
741  return 0;
742  } elseif ($num) { // $num = 1
743  $obj = $this->db->fetch_object($resql);
744  if ($obj) {
745  $this->id = $obj->rowid;
746  $this->ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
747  $this->ref_customer = $obj->ref_customer;
748  $this->ref_supplier = $obj->ref_supplier;
749  $this->ref_ext = $obj->ref_ext;
750  $this->entity = $obj->entity;
751  $this->statut = $obj->status;
752  $this->status = $obj->status;
753 
754  $this->date_contrat = $this->db->jdate($obj->datecontrat);
755  $this->date_creation = $this->db->jdate($obj->datecontrat);
756 
757  $this->user_author_id = $obj->fk_user_author;
758 
759  $this->commercial_signature_id = $obj->fk_commercial_signature;
760  $this->commercial_suivi_id = $obj->fk_commercial_suivi;
761 
762  $this->note_private = $obj->note_private;
763  $this->note_public = $obj->note_public;
764  $this->model_pdf = $obj->model_pdf;
765 
766  $this->fk_projet = $obj->fk_project; // deprecated
767  $this->fk_project = $obj->fk_project;
768 
769  $this->socid = $obj->thirdpartyid;
770  $this->fk_soc = $obj->thirdpartyid;
771  $this->last_main_doc = $obj->last_main_doc;
772  $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : null);
773 
774  $this->db->free($resql);
775 
776  // Retrieve all extrafields
777  // fetch optionals attributes and labels
778  if (empty($noextrafields)) {
779  $result = $this->fetch_optionals();
780  if ($result < 0) {
781  $this->error = $this->db->lasterror();
782  return -4;
783  }
784  }
785 
786  // Lines
787  if (empty($nolines)) {
788  if ($result >= 0 && !empty($this->table_element_line)) {
789  $result = $this->fetch_lines();
790  if ($result < 0) {
791  $this->error = $this->db->lasterror();
792  return -3;
793  }
794  }
795  }
796 
797  return $this->id;
798  } else {
799  dol_syslog(get_class($this)."::fetch Contract failed");
800  $this->error = "Fetch contract failed";
801  return -1;
802  }
803  } else {
804  dol_syslog(get_class($this)."::fetch Contract not found");
805  $this->error = "Contract not found";
806  return 0;
807  }
808  } else {
809  dol_syslog(get_class($this)."::fetch Error searching contract");
810  $this->error = $this->db->error();
811  return -1;
812  }
813  }
814 
815  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
825  public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0)
826  {
827  // phpcs:enable
828  $this->nbofservices = 0;
829  $this->nbofserviceswait = 0;
830  $this->nbofservicesopened = 0;
831  $this->nbofservicesexpired = 0;
832  $this->nbofservicesclosed = 0;
833 
834  $total_ttc = 0;
835  $total_vat = 0;
836  $total_ht = 0;
837 
838  $now = dol_now();
839 
840  $this->lines = array();
841  $pos = 0;
842 
843  // Selects contract lines related to a product
844  $sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
845  $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,";
846  $sql .= " d.total_ht,";
847  $sql .= " d.total_tva,";
848  $sql .= " d.total_localtax1,";
849  $sql .= " d.total_localtax2,";
850  $sql .= " d.total_ttc,";
851  $sql .= " d.info_bits, d.fk_product,";
852  $sql .= " d.date_ouverture_prevue as date_start,";
853  $sql .= " d.date_ouverture as date_start_real,";
854  $sql .= " d.date_fin_validite as date_end,";
855  $sql .= " d.date_cloture as date_end_real,";
856  $sql .= " d.fk_user_author,";
857  $sql .= " d.fk_user_ouverture,";
858  $sql .= " d.fk_user_cloture,";
859  $sql .= " d.fk_unit,";
860  $sql .= " d.product_type as type,";
861  $sql .= " d.rang";
862  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
863  $sql .= " WHERE d.fk_contrat = ".((int) $this->id);
864  if ($only_services == 1) {
865  $sql .= " AND d.product_type = 1";
866  }
867  $sql .= " ORDER by d.rang ASC";
868 
869  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
870  $result = $this->db->query($sql);
871  if ($result) {
872  $num = $this->db->num_rows($result);
873  $i = 0;
874 
875  while ($i < $num) {
876  $objp = $this->db->fetch_object($result);
877 
878  $line = new ContratLigne($this->db);
879 
880  $line->id = $objp->rowid;
881  $line->ref = $objp->rowid;
882  $line->fk_contrat = $objp->fk_contrat;
883  $line->desc = $objp->description; // Description line
884  $line->qty = $objp->qty;
885  $line->vat_src_code = $objp->vat_src_code;
886  $line->tva_tx = $objp->tva_tx;
887  $line->localtax1_tx = $objp->localtax1_tx;
888  $line->localtax2_tx = $objp->localtax2_tx;
889  $line->localtax1_type = $objp->localtax1_type;
890  $line->localtax2_type = $objp->localtax2_type;
891  $line->subprice = $objp->subprice;
892  $line->statut = $objp->status;
893  $line->status = $objp->status;
894  $line->remise_percent = $objp->remise_percent;
895  $line->price_ht = $objp->price_ht;
896  $line->price = $objp->price_ht; // For backward compatibility
897  $line->total_ht = $objp->total_ht;
898  $line->total_tva = $objp->total_tva;
899  $line->total_localtax1 = $objp->total_localtax1;
900  $line->total_localtax2 = $objp->total_localtax2;
901  $line->total_ttc = $objp->total_ttc;
902  $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
903  $line->info_bits = $objp->info_bits;
904  $line->type = $objp->type;
905 
906  $line->fk_fournprice = $objp->fk_fournprice;
907  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
908  $line->pa_ht = $marginInfos[0];
909 
910  $line->fk_user_author = $objp->fk_user_author;
911  $line->fk_user_ouverture = $objp->fk_user_ouverture;
912  $line->fk_user_cloture = $objp->fk_user_cloture;
913  $line->fk_unit = $objp->fk_unit;
914 
915  $line->ref = $objp->product_ref; // deprecated
916  $line->product_ref = $objp->product_ref; // Product Ref
917  $line->product_type = $objp->product_type; // Product Type
918  $line->product_desc = $objp->product_desc; // Product Description
919  $line->product_label = $objp->product_label; // Product Label
920 
921  $line->description = $objp->description;
922 
923  $line->date_start = $this->db->jdate($objp->date_start);
924  $line->date_start_real = $this->db->jdate($objp->date_start_real);
925  $line->date_end = $this->db->jdate($objp->date_end);
926  $line->date_end_real = $this->db->jdate($objp->date_end_real);
927  // For backward compatibility
928  //$line->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue);
929  //$line->date_ouverture = $this->db->jdate($objp->date_ouverture);
930  //$line->date_fin_validite = $this->db->jdate($objp->date_fin_validite);
931  //$line->date_cloture = $this->db->jdate($objp->date_cloture);
932  //$line->date_debut_prevue = $this->db->jdate($objp->date_ouverture_prevue);
933  //$line->date_debut_reel = $this->db->jdate($objp->date_ouverture);
934  //$line->date_fin_prevue = $this->db->jdate($objp->date_fin_validite);
935  //$line->date_fin_reel = $this->db->jdate($objp->date_cloture);
936 
937  $line->rang = $objp->rang;
938 
939  // Retrieve all extrafields for contract line
940  // fetch optionals attributes and labels
941  if (empty($noextrafields)) {
942  $line->fetch_optionals();
943  }
944 
945  // multilangs
946  if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
947  $tmpproduct = new Product($this->db);
948  $tmpproduct->fetch($objp->fk_product);
949  $tmpproduct->getMultiLangs();
950 
951  $line->multilangs = $tmpproduct->multilangs;
952  }
953 
954  $this->lines[$pos] = $line;
955 
956  $this->lines_id_index_mapper[$line->id] = $pos;
957 
958  //dol_syslog("1 ".$line->desc);
959  //dol_syslog("2 ".$line->product_desc);
960 
961  if ($line->statut == ContratLigne::STATUS_INITIAL) {
962  $this->nbofserviceswait++;
963  }
964  if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
965  $this->nbofservicesopened++;
966  }
967  if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
968  $this->nbofservicesexpired++;
969  }
970  if ($line->statut == ContratLigne::STATUS_CLOSED) {
971  $this->nbofservicesclosed++;
972  }
973 
974  $total_ttc += $objp->total_ttc; // TODO Not saved into database
975  $total_vat += $objp->total_tva;
976  $total_ht += $objp->total_ht;
977 
978  $i++;
979  $pos++;
980  }
981  $this->db->free($result);
982  } else {
983  dol_syslog(get_class($this)."::Fetch Error when reading lines of contracts linked to products");
984  return -3;
985  }
986 
987  // Now set the global properties on contract not stored into database.
988  $this->nbofservices = count($this->lines);
989  $this->total_ttc = (float) price2num($total_ttc);
990  $this->total_tva = (float) price2num($total_vat);
991  $this->total_ht = (float) price2num($total_ht);
992 
993  return $this->lines;
994  }
995 
1002  public function create($user)
1003  {
1004  global $conf, $langs, $mysoc;
1005 
1006  // Check parameters
1007  $paramsok = 1;
1008  if ($this->commercial_signature_id <= 0) {
1009  $langs->load("commercial");
1010  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeSignature"));
1011  $paramsok = 0;
1012  }
1013  if ($this->commercial_suivi_id <= 0) {
1014  $langs->load("commercial");
1015  $this->error .= ($this->error ? "<br>" : '');
1016  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeFollowUp"));
1017  $paramsok = 0;
1018  }
1019  if (!$paramsok) {
1020  return -1;
1021  }
1022 
1023 
1024  $this->db->begin();
1025 
1026  $now = dol_now();
1027 
1028  // Insert contract
1029  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,";
1030  $sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet,";
1031  $sql .= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
1032  $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->socid).", ".((int) $user->id);
1033  $sql .= ", ".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL");
1034  $sql .= ",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) : "NULL");
1035  $sql .= ",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) : "NULL");
1036  $sql .= ",".($this->fk_project > 0 ? ((int) $this->fk_project) : "NULL");
1037  $sql .= ", ".(dol_strlen($this->ref) <= 0 ? "null" : "'".$this->db->escape($this->ref)."'");
1038  $sql .= ", ".((int) $conf->entity);
1039  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
1040  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
1041  $sql .= ", ".(!empty($this->ref_customer) ? ("'".$this->db->escape($this->ref_customer)."'") : "NULL");
1042  $sql .= ", ".(!empty($this->ref_supplier) ? ("'".$this->db->escape($this->ref_supplier)."'") : "NULL");
1043  $sql .= ", ".(!empty($this->ref_ext) ? ("'".$this->db->escape($this->ref_ext)."'") : "NULL");
1044  $sql .= ")";
1045  $resql = $this->db->query($sql);
1046 
1047  if ($resql) {
1048  $error = 0;
1049 
1050  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat");
1051 
1052  // Load object modContract
1053  $module = (getDolGlobalString('CONTRACT_ADDON') ? $conf->global->CONTRACT_ADDON : 'mod_contract_serpis');
1054  if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') {
1055  $module = substr($module, 0, dol_strlen($module) - 4);
1056  }
1057  $result = dol_include_once('/core/modules/contract/'.$module.'.php');
1058  if ($result > 0) {
1059  $modCodeContract = new $module();
1060  '@phan-var-force CommonNumRefGenerator $modCodeContrat';
1061 
1062  if (!empty($modCodeContract->code_auto)) {
1063  // Force the ref to a draft value if numbering module is an automatic numbering
1064  $sql = 'UPDATE '.MAIN_DB_PREFIX."contrat SET ref='(PROV".$this->id.")' WHERE rowid=".((int) $this->id);
1065  if ($this->db->query($sql)) {
1066  if ($this->id) {
1067  $this->ref = "(PROV".$this->id.")";
1068  }
1069  }
1070  }
1071  }
1072 
1073  if (!$error) {
1074  $result = $this->insertExtraFields();
1075  if ($result < 0) {
1076  $error++;
1077  }
1078  }
1079 
1080  // Insert business contacts ('SALESREPSIGN','contrat')
1081  if (!$error) {
1082  $result = $this->add_contact($this->commercial_signature_id, 'SALESREPSIGN', 'internal');
1083  if ($result < 0) {
1084  $error++;
1085  }
1086  }
1087 
1088  // Insert business contacts ('SALESREPFOLL','contrat')
1089  if (!$error) {
1090  $result = $this->add_contact($this->commercial_suivi_id, 'SALESREPFOLL', 'internal');
1091  if ($result < 0) {
1092  $error++;
1093  }
1094  }
1095 
1096  if (!$error) {
1097  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1098  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1099  }
1100 
1101  // Add object linked
1102  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1103  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1104  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, ...))
1105  foreach ($tmp_origin_id as $origin_id) {
1106  $ret = $this->add_object_linked($origin, $origin_id);
1107  if (!$ret) {
1108  $this->error = $this->db->lasterror();
1109  $error++;
1110  }
1111  }
1112  } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1113  $origin_id = $tmp_origin_id;
1114  $ret = $this->add_object_linked($origin, $origin_id);
1115  if (!$ret) {
1116  $this->error = $this->db->lasterror();
1117  $error++;
1118  }
1119  }
1120  }
1121  }
1122 
1123  if (!$error && $this->id && getDolGlobalString('MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) { // Get contact from origin object
1124  $originforcontact = $this->origin;
1125  $originidforcontact = $this->origin_id;
1126  if ($originforcontact == 'shipping') { // shipment and order share the same contacts. If creating from shipment we take data of order
1127  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1128  $exp = new Expedition($this->db);
1129  $exp->fetch($this->origin_id);
1130  $exp->fetchObjectLinked();
1131  if (count($exp->linkedObjectsIds['commande']) > 0) {
1132  foreach ($exp->linkedObjectsIds['commande'] as $key => $value) {
1133  $originforcontact = 'commande';
1134  $originidforcontact = $value;
1135  break; // We take first one
1136  }
1137  }
1138  }
1139 
1140  $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";
1141  $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'";
1142 
1143  $resqlcontact = $this->db->query($sqlcontact);
1144  if ($resqlcontact) {
1145  while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1146  if ($objcontact->source == 'internal' && in_array($objcontact->code, array('SALESREPSIGN', 'SALESREPFOLL'))) {
1147  continue; // ignore this, already forced previously
1148  }
1149 
1150  $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
1151  }
1152  } else {
1153  dol_print_error($resqlcontact);
1154  }
1155  }
1156  }
1157 
1158  if (!$error) {
1159  // Call trigger
1160  $result = $this->call_trigger('CONTRACT_CREATE', $user);
1161  if ($result < 0) {
1162  $error++;
1163  }
1164  // End call triggers
1165 
1166  if (!$error) {
1167  $this->db->commit();
1168  return $this->id;
1169  } else {
1170  dol_syslog(get_class($this)."::create - 30 - ".$this->error, LOG_ERR);
1171  $this->db->rollback();
1172  return -3;
1173  }
1174  } else {
1175  $this->error = "Failed to add contract";
1176  dol_syslog(get_class($this)."::create - 20 - ".$this->error, LOG_ERR);
1177  $this->db->rollback();
1178  return -2;
1179  }
1180  } else {
1181  $this->error = $langs->trans("UnknownError").": ".$this->db->error();
1182  dol_syslog(get_class($this)."::create - 10 - ".$this->error, LOG_ERR);
1183 
1184  $this->db->rollback();
1185  return -1;
1186  }
1187  }
1188 
1189 
1196  public function delete($user)
1197  {
1198  global $conf, $langs;
1199  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1200 
1201  $error = 0;
1202 
1203  $this->db->begin();
1204 
1205  // Call trigger
1206  $result = $this->call_trigger('CONTRACT_DELETE', $user);
1207  if ($result < 0) {
1208  $error++;
1209  }
1210  // End call triggers
1211 
1212  if (!$error) {
1213  // Delete linked contacts
1214  $res = $this->delete_linked_contact();
1215  if ($res < 0) {
1216  dol_syslog(get_class($this)."::delete error", LOG_ERR);
1217  $error++;
1218  }
1219  }
1220 
1221  if (!$error) {
1222  // Delete linked object
1223  $res = $this->deleteObjectLinked();
1224  if ($res < 0) {
1225  $error++;
1226  }
1227  }
1228 
1229  if (!$error) {
1230  // Delete contratdet_log
1231  /*
1232  $sql = "DELETE cdl";
1233  $sql.= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd";
1234  $sql.= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->id);
1235  */
1236  $sql = "SELECT cdl.rowid as cdlrowid ";
1237  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd";
1238  $sql .= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->id);
1239 
1240  dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG);
1241  $resql = $this->db->query($sql);
1242  if (!$resql) {
1243  $this->error = $this->db->error();
1244  $error++;
1245  }
1246  $numressql = $this->db->num_rows($resql);
1247  if (!$error && $numressql) {
1248  $tab_resql = array();
1249  for ($i = 0; $i < $numressql; $i++) {
1250  $objresql = $this->db->fetch_object($resql);
1251  $tab_resql[] = $objresql->cdlrowid;
1252  }
1253  $this->db->free($resql);
1254 
1255  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet_log ";
1256  $sql .= " WHERE ".MAIN_DB_PREFIX."contratdet_log.rowid IN (".$this->db->sanitize(implode(",", $tab_resql)).")";
1257 
1258  dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG);
1259  $resql = $this->db->query($sql);
1260  if (!$resql) {
1261  $this->error = $this->db->error();
1262  $error++;
1263  }
1264  }
1265  }
1266 
1267  // Delete lines
1268  if (!$error) {
1269  // Delete contratdet extrafields
1270  $main = MAIN_DB_PREFIX.'contratdet';
1271  $ef = $main."_extrafields";
1272  $sql = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_contrat = ".((int) $this->id).")";
1273 
1274  dol_syslog(get_class($this)."::delete contratdet_extrafields", LOG_DEBUG);
1275  $resql = $this->db->query($sql);
1276  if (!$resql) {
1277  $this->error = $this->db->error();
1278  $error++;
1279  }
1280  }
1281 
1282  if (!$error) {
1283  // Delete contratdet
1284  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet";
1285  $sql .= " WHERE fk_contrat=".((int) $this->id);
1286 
1287  dol_syslog(get_class($this)."::delete contratdet", LOG_DEBUG);
1288  $resql = $this->db->query($sql);
1289  if (!$resql) {
1290  $this->error = $this->db->error();
1291  $error++;
1292  }
1293  }
1294 
1295  // Delete llx_ecm_files
1296  if (!$error) {
1297  $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);
1298  $resql = $this->db->query($sql);
1299  if (!$resql) {
1300  $this->error = $this->db->lasterror();
1301  $this->errors[] = $this->error;
1302  $error++;
1303  }
1304  }
1305 
1306  // Delete contract
1307  if (!$error) {
1308  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat";
1309  $sql .= " WHERE rowid=".((int) $this->id);
1310 
1311  dol_syslog(get_class($this)."::delete contrat", LOG_DEBUG);
1312  $resql = $this->db->query($sql);
1313  if (!$resql) {
1314  $this->error = $this->db->error();
1315  $error++;
1316  }
1317  }
1318 
1319  // Removed extrafields
1320  if (!$error) {
1321  $result = $this->deleteExtraFields();
1322  if ($result < 0) {
1323  $error++;
1324  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1325  }
1326  }
1327 
1328  if (!$error) {
1329  // We remove directory
1330  $ref = dol_sanitizeFileName($this->ref);
1331  if ($conf->contrat->dir_output) {
1332  $dir = $conf->contrat->multidir_output[$this->entity]."/".$ref;
1333  if (file_exists($dir)) {
1334  $res = @dol_delete_dir_recursive($dir);
1335  if (!$res) {
1336  $this->error = 'ErrorFailToDeleteDir';
1337  $error++;
1338  }
1339  }
1340  }
1341  }
1342 
1343  if (!$error) {
1344  $this->db->commit();
1345  return 1;
1346  } else {
1347  $this->error = $this->db->lasterror();
1348  $this->db->rollback();
1349  return -1;
1350  }
1351  }
1352 
1360  public function update($user, $notrigger = 0)
1361  {
1362  global $conf;
1363  $error = 0;
1364 
1365  // Clean parameters
1366  if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1367  $this->fk_commercial_signature = $this->commercial_signature_id;
1368  }
1369  if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1370  $this->fk_commercial_suivi = $this->commercial_suivi_id;
1371  }
1372  if (empty($this->socid) && $this->fk_soc > 0) {
1373  $this->socid = (int) $this->fk_soc;
1374  }
1375  if (empty($this->fk_project) && $this->projet > 0) {
1376  $this->fk_project = (int) $this->projet;
1377  }
1378 
1379  if (isset($this->ref)) {
1380  $this->ref = trim($this->ref);
1381  }
1382  if (isset($this->ref_customer)) {
1383  $this->ref_customer = trim($this->ref_customer);
1384  }
1385  if (isset($this->ref_supplier)) {
1386  $this->ref_supplier = trim($this->ref_supplier);
1387  }
1388  if (isset($this->ref_ext)) {
1389  $this->ref_ext = trim($this->ref_ext);
1390  }
1391  if (isset($this->entity)) {
1392  $this->entity = (int) $this->entity;
1393  }
1394  if (isset($this->statut)) {
1395  $this->statut = (int) $this->statut;
1396  }
1397  if (isset($this->status)) {
1398  $this->status = (int) $this->status;
1399  }
1400  if (isset($this->socid)) {
1401  $this->socid = (int) $this->socid;
1402  }
1403  if (isset($this->fk_commercial_signature)) {
1404  $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1405  }
1406  if (isset($this->fk_commercial_suivi)) {
1407  $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1408  }
1409  if (isset($this->note_private)) {
1410  $this->note_private = trim($this->note_private);
1411  }
1412  if (isset($this->note_public)) {
1413  $this->note_public = trim($this->note_public);
1414  }
1415  if (isset($this->import_key)) {
1416  $this->import_key = trim($this->import_key);
1417  }
1418  //if (isset($this->extraparams)) $this->extraparams=trim($this->extraparams);
1419 
1420  // $this->oldcopy must have been set by the caller of update
1421 
1422  // Update request
1423  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET";
1424  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1425  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1426  $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1427  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1428  $sql .= " entity=".$conf->entity.",";
1429  $sql .= " date_contrat=".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : 'null').",";
1430  $sql .= " statut=".(isset($this->statut) ? $this->statut : (isset($this->status) ? $this->status : "null")).",";
1431  $sql .= " fk_soc=".($this->socid > 0 ? $this->socid : "null").",";
1432  $sql .= " fk_projet=".($this->fk_project > 0 ? $this->fk_project : "null").",";
1433  $sql .= " fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature : "null").",";
1434  $sql .= " fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi : "null").",";
1435  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1436  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1437  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
1438  //$sql.= " extraparams=".(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null");
1439  $sql .= " WHERE rowid=".((int) $this->id);
1440 
1441  $this->db->begin();
1442 
1443  $resql = $this->db->query($sql);
1444  if (!$resql) {
1445  $error++;
1446  $this->errors[] = "Error ".$this->db->lasterror();
1447  }
1448 
1449  if (!$error) {
1450  $result = $this->insertExtraFields(); // This delete and reinsert extrafields
1451  if ($result < 0) {
1452  $error++;
1453  }
1454  }
1455 
1456  if (!$error && !$notrigger) {
1457  // Call triggers
1458  $result = $this->call_trigger('CONTRACT_MODIFY', $user);
1459  if ($result < 0) {
1460  $error++;
1461  }
1462  // End call triggers
1463  }
1464 
1465  // Commit or rollback
1466  if ($error) {
1467  foreach ($this->errors as $errmsg) {
1468  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1469  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1470  }
1471  $this->db->rollback();
1472  return -1 * $error;
1473  } else {
1474  $this->db->commit();
1475  return 1;
1476  }
1477  }
1478 
1479 
1503  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)
1504  {
1505  global $user, $langs, $conf, $mysoc;
1506  $error = 0;
1507 
1508  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");
1509 
1510  // Check parameters
1511  if ($fk_product <= 0 && empty($desc)) {
1512  $this->error = "ErrorDescRequiredForFreeProductLines";
1513  return -1;
1514  }
1515 
1516  if ($this->statut >= 0) {
1517  // Clean parameters
1518  $pu_ht = price2num($pu_ht);
1519  $pu_ttc = price2num($pu_ttc);
1520  $pa_ht = price2num($pa_ht);
1521 
1522  // Clean vat code
1523  $reg = array();
1524  $vat_src_code = '';
1525  if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
1526  $vat_src_code = $reg[1];
1527  $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code into vatrate.
1528  }
1529  $txtva = price2num($txtva);
1530  $txlocaltax1 = price2num($txlocaltax1);
1531  $txlocaltax2 = price2num($txlocaltax2);
1532 
1533  $remise_percent = price2num($remise_percent);
1534  $qty = price2num($qty);
1535  if (empty($qty)) {
1536  $qty = 1;
1537  }
1538  if (empty($info_bits)) {
1539  $info_bits = 0;
1540  }
1541  if (empty($pu_ht) || !is_numeric($pu_ht)) {
1542  $pu_ht = 0;
1543  }
1544  if (empty($pu_ttc)) {
1545  $pu_ttc = 0;
1546  }
1547  if (empty($txtva) || !is_numeric($txtva)) {
1548  $txtva = 0;
1549  }
1550  if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1551  $txlocaltax1 = 0;
1552  }
1553  if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1554  $txlocaltax2 = 0;
1555  }
1556 
1557  if ($price_base_type == 'HT') {
1558  $pu = $pu_ht;
1559  } else {
1560  $pu = $pu_ttc;
1561  }
1562 
1563  // Check parameters
1564  if (empty($remise_percent)) {
1565  $remise_percent = 0;
1566  }
1567  if (empty($rang)) {
1568  $rang = 0;
1569  }
1570 
1571  if ($date_start && $date_end && $date_start > $date_end) {
1572  $langs->load("errors");
1573  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1574  return -1;
1575  }
1576 
1577  $this->db->begin();
1578 
1579  $localtaxes_type = getLocalTaxesFromRate($txtva.($vat_src_code ? ' ('.$vat_src_code.')' : ''), 0, $this->societe, $mysoc);
1580 
1581  // Calcul du total TTC et de la TVA pour la ligne a partir de
1582  // qty, pu, remise_percent et txtva
1583  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1584  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1585 
1586  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1587  $total_ht = $tabprice[0];
1588  $total_tva = $tabprice[1];
1589  $total_ttc = $tabprice[2];
1590  $total_localtax1 = $tabprice[9];
1591  $total_localtax2 = $tabprice[10];
1592 
1593  $localtax1_type = $localtaxes_type[0];
1594  $localtax2_type = $localtaxes_type[2];
1595 
1596  // TODO A virer
1597  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1598  $remise = 0;
1599  $price = price2num(round($pu_ht, 2));
1600  if (dol_strlen($remise_percent) > 0) {
1601  $remise = round(($pu_ht * $remise_percent / 100), 2);
1602  $price = $pu_ht - $remise;
1603  }
1604 
1605  if (empty($pa_ht)) {
1606  $pa_ht = 0;
1607  }
1608 
1609 
1610  // if buy price not defined, define buyprice as configured in margin admin
1611  if ($pa_ht == 0) {
1612  $result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product);
1613  if ($result < 0) {
1614  return -1;
1615  } else {
1616  $pa_ht = $result;
1617  }
1618  }
1619 
1620  // Insertion dans la base
1621  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
1622  $sql .= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1623  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1624  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1625  $sql .= " info_bits,";
1626  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1627  if ($date_start > 0) {
1628  $sql .= ",date_ouverture_prevue";
1629  }
1630  if ($date_end > 0) {
1631  $sql .= ",date_fin_validite";
1632  }
1633  $sql .= ", fk_unit";
1634  $sql .= ", rang";
1635  $sql .= ") VALUES (";
1636  $sql .= $this->id.", '', '".$this->db->escape($desc)."',";
1637  $sql .= ($fk_product > 0 ? $fk_product : "null").",";
1638  $sql .= " ".((float) $qty).",";
1639  $sql .= " ".((float) $txtva).",";
1640  $sql .= " ".($vat_src_code ? "'".$this->db->escape($vat_src_code)."'" : "null").",";
1641  $sql .= " ".((float) $txlocaltax1).",";
1642  $sql .= " ".((float) $txlocaltax2).",";
1643  $sql .= " '".$this->db->escape($localtax1_type)."',";
1644  $sql .= " '".$this->db->escape($localtax2_type)."',";
1645  $sql .= " ".price2num($remise_percent).",";
1646  $sql .= " ".price2num($pu_ht).",";
1647  $sql .= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).",";
1648  $sql .= " ".((int) $info_bits).",";
1649  $sql .= " ".price2num($price).",".price2num($remise).",";
1650  if (isset($fk_fournprice)) {
1651  $sql .= ' '.((int) $fk_fournprice).',';
1652  } else {
1653  $sql .= ' null,';
1654  }
1655  if (isset($pa_ht)) {
1656  $sql .= ' '.price2num($pa_ht);
1657  } else {
1658  $sql .= ' null';
1659  }
1660  if ($date_start > 0) {
1661  $sql .= ",'".$this->db->idate($date_start)."'";
1662  }
1663  if ($date_end > 0) {
1664  $sql .= ",'".$this->db->idate($date_end)."'";
1665  }
1666  $sql .= ", ".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1667  $sql .= ", ".(!empty($rang) ? (int) $rang : "0");
1668  $sql .= ")";
1669 
1670  $resql = $this->db->query($sql);
1671  if ($resql) {
1672  $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX."contratdet");
1673 
1674  if (!$error) {
1675  $contractline = new ContratLigne($this->db);
1676  $contractline->array_options = $array_options;
1677  $contractline->id = $contractlineid;
1678  $result = $contractline->insertExtraFields();
1679  if ($result < 0) {
1680  $this->errors[] = $contractline->error;
1681  $error++;
1682  }
1683  }
1684 
1685  if (empty($error)) {
1686  // Call trigger
1687  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
1688  if ($result < 0) {
1689  $error++;
1690  }
1691  // End call triggers
1692  }
1693 
1694  if ($error) {
1695  $this->db->rollback();
1696  return -1;
1697  } else {
1698  $this->db->commit();
1699  return $contractlineid;
1700  }
1701  } else {
1702  $this->db->rollback();
1703  $this->error = $this->db->error()." sql=".$sql;
1704  return -1;
1705  }
1706  } else {
1707  dol_syslog(get_class($this)."::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1708  return -2;
1709  }
1710  }
1711 
1736  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)
1737  {
1738  global $user, $conf, $langs, $mysoc;
1739 
1740  $error = 0;
1741 
1742  // Clean parameters
1743  $qty = trim((string) $qty);
1744  $desc = trim($desc);
1745  $desc = trim($desc);
1746  $price = price2num($pu);
1747  $tvatx = price2num($tvatx);
1748  $localtax1tx = price2num($localtax1tx);
1749  $localtax2tx = price2num($localtax2tx);
1750  $pa_ht = price2num($pa_ht);
1751  if (empty($fk_fournprice)) {
1752  $fk_fournprice = 0;
1753  }
1754  if (empty($rang)) {
1755  $rang = 0;
1756  }
1757 
1758  $subprice = $price;
1759  $remise = 0;
1760  if (dol_strlen($remise_percent) > 0) {
1761  $remise = round(($pu * $remise_percent / 100), 2);
1762  $price = $pu - $remise;
1763  } else {
1764  $remise_percent = 0;
1765  }
1766 
1767  if ($date_start && $date_end && $date_start > $date_end) {
1768  $langs->load("errors");
1769  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1770  return -1;
1771  }
1772 
1773  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");
1774 
1775  $this->db->begin();
1776 
1777  // Calcul du total TTC et de la TVA pour la ligne a partir de
1778  // qty, pu, remise_percent et tvatx
1779  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1780  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1781 
1782  $localtaxes_type = getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc);
1783  $tvatx = preg_replace('/\s*\‍(.*\‍)/', '', $tvatx); // Remove code into vatrate.
1784 
1785  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1786  $total_ht = $tabprice[0];
1787  $total_tva = $tabprice[1];
1788  $total_ttc = $tabprice[2];
1789  $total_localtax1 = $tabprice[9];
1790  $total_localtax2 = $tabprice[10];
1791 
1792  $localtax1_type = (empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]);
1793  $localtax2_type = (empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]);
1794 
1795  // TODO A virer
1796  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1797  $remise = 0;
1798  $price = price2num(round($pu, 2));
1799  if (dol_strlen($remise_percent) > 0) {
1800  $remise = round(($pu * $remise_percent / 100), 2);
1801  $price = $pu - $remise;
1802  }
1803 
1804  if (empty($pa_ht)) {
1805  $pa_ht = 0;
1806  }
1807 
1808  // if buy price not defined, define buyprice as configured in margin admin
1809  if ($pa_ht == 0) {
1810  $result = $this->defineBuyPrice($pu, $remise_percent);
1811  if ($result < 0) {
1812  return -1;
1813  } else {
1814  $pa_ht = $result;
1815  }
1816  }
1817 
1818  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description = '".$this->db->escape($desc)."'";
1819  $sql .= ",price_ht = ".((float) price2num($price));
1820  $sql .= ",subprice = ".((float) price2num($subprice));
1821  $sql .= ",remise = ".((float) price2num($remise));
1822  $sql .= ",remise_percent = ".((float) price2num($remise_percent));
1823  $sql .= ",qty = ".((float) $qty);
1824  $sql .= ",tva_tx = ".((float) price2num($tvatx));
1825  $sql .= ",localtax1_tx = ".((float) price2num($localtax1tx));
1826  $sql .= ",localtax2_tx = ".((float) price2num($localtax2tx));
1827  $sql .= ",localtax1_type='".$this->db->escape($localtax1_type)."'";
1828  $sql .= ",localtax2_type='".$this->db->escape($localtax2_type)."'";
1829  $sql .= ", total_ht = ".((float) price2num($total_ht));
1830  $sql .= ", total_tva = ".((float) price2num($total_tva));
1831  $sql .= ", total_localtax1 = ".((float) price2num($total_localtax1));
1832  $sql .= ", total_localtax2 = ".((float) price2num($total_localtax2));
1833  $sql .= ", total_ttc = ".((float) price2num($total_ttc));
1834  $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null");
1835  $sql .= ", buy_price_ht = ".((float) price2num($pa_ht));
1836  if ($date_start > 0) {
1837  $sql .= ",date_ouverture_prevue = '".$this->db->idate($date_start)."'";
1838  } else {
1839  $sql .= ",date_ouverture_prevue = null";
1840  }
1841  if ($date_end > 0) {
1842  $sql .= ",date_fin_validite = '".$this->db->idate($date_end)."'";
1843  } else {
1844  $sql .= ",date_fin_validite = null";
1845  }
1846  if ($date_start_real > 0) {
1847  $sql .= ",date_ouverture = '".$this->db->idate($date_start_real)."'";
1848  } else {
1849  $sql .= ",date_ouverture = null";
1850  }
1851  if ($date_end_real > 0) {
1852  $sql .= ",date_cloture = '".$this->db->idate($date_end_real)."'";
1853  } else {
1854  $sql .= ",date_cloture = null";
1855  }
1856  $sql .= ", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) : "null");
1857  $sql .= ", rang = ".(!empty($rang) ? ((int) $rang) : "0");
1858  $sql .= " WHERE rowid = ".((int) $rowid);
1859 
1860  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1861  $result = $this->db->query($sql);
1862  if ($result) {
1863  if (is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1864  $contractline = new ContratLigne($this->db);
1865  $contractline->fetch($rowid);
1866 
1867  // We replace values in $contractline->array_options only for entries defined into $array_options
1868  foreach ($array_options as $key => $value) {
1869  $contractline->array_options[$key] = $array_options[$key];
1870  }
1871 
1872  $result = $contractline->insertExtraFields();
1873  if ($result < 0) {
1874  $this->errors[] = $contractline->error;
1875  $error++;
1876  }
1877  }
1878 
1879  if (empty($error)) {
1880  // Call trigger
1881  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
1882  if ($result < 0) {
1883  $this->db->rollback();
1884  return -3;
1885  }
1886  // End call triggers
1887 
1888  $this->db->commit();
1889  return 1;
1890  } else {
1891  $this->db->rollback();
1892  return -1;
1893  }
1894  } else {
1895  $this->db->rollback();
1896  $this->error = $this->db->error();
1897  dol_syslog(get_class($this)."::updateline Erreur -1");
1898  return -1;
1899  }
1900  }
1901 
1909  public function deleteLine($idline, User $user)
1910  {
1911  $error = 0;
1912 
1913  if ($this->statut >= 0) {
1914  // Call trigger
1915  $result = $this->call_trigger('LINECONTRACT_DELETE', $user);
1916  if ($result < 0) {
1917  return -1;
1918  }
1919  // End call triggers
1920 
1921  $this->db->begin();
1922 
1923  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1924  $sql .= " WHERE rowid = ".((int) $idline);
1925 
1926  dol_syslog(get_class($this)."::deleteline", LOG_DEBUG);
1927  $resql = $this->db->query($sql);
1928  if (!$resql) {
1929  $this->error = "Error ".$this->db->lasterror();
1930  $error++;
1931  }
1932 
1933  if (!$error) {
1934  // Remove extrafields
1935  $contractline = new ContratLigne($this->db);
1936  $contractline->id = $idline;
1937  $result = $contractline->deleteExtraFields();
1938  if ($result < 0) {
1939  $error++;
1940  $this->error = "Error ".get_class($this)."::deleteline deleteExtraFields error -4 ".$contractline->error;
1941  }
1942  }
1943 
1944  if (empty($error)) {
1945  $this->db->commit();
1946  return 1;
1947  } else {
1948  dol_syslog(get_class($this)."::deleteline ERROR:".$this->error, LOG_ERR);
1949  $this->db->rollback();
1950  return -1;
1951  }
1952  } else {
1953  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1954  return -2;
1955  }
1956  }
1957 
1958 
1959  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1967  public function update_statut($user)
1968  {
1969  // phpcs:enable
1970  dol_syslog(__METHOD__." is deprecated", LOG_WARNING);
1971 
1972  // If draft, we keep it (should not happen)
1973  if ($this->statut == 0) {
1974  return 1;
1975  }
1976 
1977  // Load $this->lines array
1978  // $this->fetch_lines();
1979 
1980  // $newstatut=1;
1981  // foreach($this->lines as $key => $contractline)
1982  // {
1983  // // if ($contractline) // Loop on each service
1984  // }
1985 
1986  return 1;
1987  }
1988 
1989 
1996  public function getLibStatut($mode)
1997  {
1998  return $this->LibStatut($this->statut, $mode);
1999  }
2000 
2001  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009  public function LibStatut($status, $mode)
2010  {
2011  // phpcs:enable
2012  global $langs;
2013 
2014  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
2015  global $langs;
2016  $langs->load("contracts");
2017  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
2018  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
2019  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
2020  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
2021  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
2022  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
2023  }
2024 
2025  $statusType = 'status'.$status;
2026  if ($status == self::STATUS_VALIDATED) {
2027  $statusType = 'status6';
2028  }
2029 
2030  if ($mode == 4 || $mode == 6 || $mode == 7) {
2031  $text = '';
2032  if ($mode == 4) {
2033  $text = '<span class="hideonsmartphone">';
2034  $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
2035  $text .= ' '.$langs->trans("Services");
2036  $text .= ': &nbsp; &nbsp; ';
2037  $text .= '</span>';
2038  }
2039  $text .= ($mode == 7 ? '<span class="nowraponall">' : '');
2040  $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2041  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2042  $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2043  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2044  $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2045  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2046  $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="marginleft2"')) : '';
2047  $text .= ($mode == 7 ? '</span>' : '');
2048  return $text;
2049  } else {
2050  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
2051  }
2052  }
2053 
2060  public function getTooltipContentArray($params)
2061  {
2062  global $conf, $langs, $user;
2063 
2064  $langs->load('contracts');
2065 
2066  $datas = [];
2067  $nofetch = !empty($params['nofetch']);
2068 
2069  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2070  return ['optimize' => $langs->trans("ShowContract")];
2071  }
2072  if ($user->hasRight('contrat', 'lire')) {
2073  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contract").'</u>';
2074  /* Status of a contract is status of all services, so disabled
2075  if (isset($this->statut)) {
2076  $label .= ' '.$this->getLibStatut(5);
2077  }*/
2078  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
2079  if (!$nofetch) {
2080  $langs->load('companies');
2081  if (empty($this->thirdparty)) {
2082  $this->fetch_thirdparty();
2083  }
2084  $datas['customer'] = '<br><b>'.$langs->trans('Customer').':</b> '.$this->thirdparty->getNomUrl(1, '', 0, 1);
2085  }
2086  $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '. $this->ref_customer;
2087  if (!$nofetch) {
2088  $langs->load('project');
2089  if (empty($this->project) && $this->project instanceof Project) {
2090  $res = $this->fetch_project();
2091  if ($res > 0) {
2092  $datas['project'] = '<br><b>'.$langs->trans('Project').':</b> '.$this->project->getNomUrl(1, '', 0, 1);
2093  }
2094  }
2095  }
2096  $datas['refsupplier'] = '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
2097  if (!empty($this->total_ht)) {
2098  $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2099  }
2100  if (!empty($this->total_tva)) {
2101  $datas['vatamount'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2102  }
2103  if (!empty($this->total_ttc)) {
2104  $datas['amounttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2105  }
2106  }
2107  return $datas;
2108  }
2109 
2119  public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2120  {
2121  global $conf, $langs, $user, $hookmanager;
2122 
2123  $result = '';
2124 
2125  $url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id;
2126 
2127  //if ($option !== 'nolink')
2128  //{
2129  // Add param to save lastsearch_values or not
2130  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2131  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2132  $add_save_lastsearch_values = 1;
2133  }
2134  if ($add_save_lastsearch_values) {
2135  $url .= '&save_lastsearch_values=1';
2136  }
2137  //}
2138  $params = [
2139  'id' => $this->id,
2140  'objecttype' => $this->element,
2141  'nofetch' => 1,
2142  ];
2143  $classfortooltip = 'classfortooltip';
2144  $dataparams = '';
2145  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
2146  $classfortooltip = 'classforajaxtooltip';
2147  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
2148  $label = '';
2149  } else {
2150  $label = implode($this->getTooltipContentArray($params));
2151  }
2152 
2153  $linkclose = '';
2154  if (empty($notooltip) && $user->hasRight('contrat', 'lire')) {
2155  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2156  $label = $langs->trans("ShowContract");
2157  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
2158  }
2159  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
2160  $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
2161  }
2162  $linkstart = '<a href="'.$url.'"';
2163  $linkstart .= $linkclose.'>';
2164  $linkend = '</a>';
2165 
2166  $result .= $linkstart;
2167  if ($withpicto) {
2168  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
2169  }
2170  if ($withpicto != 2) {
2171  $result .= ($this->ref ? $this->ref : $this->id);
2172  }
2173  $result .= $linkend;
2174 
2175  global $action;
2176  $hookmanager->initHooks(array('contractdao'));
2177  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
2178  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
2179  if ($reshook > 0) {
2180  $result = $hookmanager->resPrint;
2181  } else {
2182  $result .= $hookmanager->resPrint;
2183  }
2184 
2185  return $result;
2186  }
2187 
2194  public function info($id)
2195  {
2196  $sql = "SELECT c.rowid, c.ref, c.datec,";
2197  $sql .= " c.tms as date_modification,";
2198  $sql .= " fk_user_author";
2199  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2200  $sql .= " WHERE c.rowid = ".((int) $id);
2201 
2202  $result = $this->db->query($sql);
2203  if ($result) {
2204  if ($this->db->num_rows($result)) {
2205  $obj = $this->db->fetch_object($result);
2206 
2207  $this->id = $obj->rowid;
2208  $this->ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2209 
2210  $this->user_creation_id = $obj->fk_user_author;
2211  $this->date_creation = $this->db->jdate($obj->datec);
2212  $this->date_modification = $this->db->jdate($obj->date_modification);
2213  }
2214 
2215  $this->db->free($result);
2216  } else {
2217  dol_print_error($this->db);
2218  }
2219  }
2220 
2221  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2228  public function array_detail($status = -1)
2229  {
2230  // phpcs:enable
2231  $tab = array();
2232 
2233  $sql = "SELECT cd.rowid";
2234  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
2235  $sql .= " WHERE fk_contrat =".((int) $this->id);
2236  if ($status >= 0) {
2237  $sql .= " AND statut = ".((int) $status);
2238  }
2239 
2240  dol_syslog(get_class($this)."::array_detail()", LOG_DEBUG);
2241  $resql = $this->db->query($sql);
2242  if ($resql) {
2243  $num = $this->db->num_rows($resql);
2244  $i = 0;
2245  while ($i < $num) {
2246  $obj = $this->db->fetch_object($resql);
2247  $tab[$i] = $obj->rowid;
2248  $i++;
2249  }
2250  return $tab;
2251  } else {
2252  $this->error = $this->db->error();
2253  return -1;
2254  }
2255  }
2256 
2266  public function getListOfContracts($option = 'all', $status = [], $product_categories = [], $line_status = [])
2267  {
2268  $tab = array();
2269 
2270  $sql = "SELECT c.rowid";
2271  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2272  if (!empty($product_categories)) {
2273  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON cd.fk_contrat = c.rowid";
2274  $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)).")";
2275  }
2276  $sql .= " WHERE c.fk_soc =".((int) $this->socid);
2277  $sql .= ($option == 'others') ? " AND c.rowid <> ".((int) $this->id) : "";
2278  $sql .= (!empty($status)) ? " AND c.statut IN (".$this->db->sanitize(implode(', ', $status)).")" : "";
2279  $sql .= (!empty($line_status)) ? " AND cd.statut IN (".$this->db->sanitize(implode(', ', $line_status)).")" : "";
2280  $sql .= " GROUP BY c.rowid";
2281 
2282  dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG);
2283  $resql = $this->db->query($sql);
2284  if ($resql) {
2285  $num = $this->db->num_rows($resql);
2286  $i = 0;
2287  while ($i < $num) {
2288  $obj = $this->db->fetch_object($resql);
2289  $contrat = new Contrat($this->db);
2290  $contrat->fetch($obj->rowid);
2291  $tab[$contrat->id] = $contrat;
2292  $i++;
2293  }
2294  return $tab;
2295  } else {
2296  $this->error = $this->db->lasterror();
2297  return -1;
2298  }
2299  }
2300 
2301 
2302  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2310  public function load_board($user, $mode)
2311  {
2312  // phpcs:enable
2313  global $conf, $langs;
2314 
2315  $this->from = " FROM ".MAIN_DB_PREFIX."contrat as c";
2316  $this->from .= ", ".MAIN_DB_PREFIX."contratdet as cd";
2317  $this->from .= ", ".MAIN_DB_PREFIX."societe as s";
2318  if (!$user->hasRight('societe', 'client', 'voir')) {
2319  $this->from .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2320  }
2321 
2322  if ($mode == 'inactive') {
2323  $sql = "SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2324  $sql .= $this->from;
2325  $sql .= " WHERE c.statut = 1";
2326  $sql .= " AND c.rowid = cd.fk_contrat";
2327  $sql .= " AND cd.statut = 0";
2328  } elseif ($mode == 'expired') {
2329  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2330  $sql .= $this->from;
2331  $sql .= " WHERE c.statut = 1";
2332  $sql .= " AND c.rowid = cd.fk_contrat";
2333  $sql .= " AND cd.statut = 4";
2334  $sql .= " AND cd.date_fin_validite < '".$this->db->idate(dol_now())."'";
2335  } elseif ($mode == 'active') {
2336  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2337  $sql .= $this->from;
2338  $sql .= " WHERE c.statut = 1";
2339  $sql .= " AND c.rowid = cd.fk_contrat";
2340  $sql .= " AND cd.statut = 4";
2341  //$datetouse = dol_now();
2342  //$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'";
2343  }
2344  $sql .= " AND c.fk_soc = s.rowid";
2345  $sql .= " AND c.entity = ".((int) $conf->entity);
2346  if ($user->socid) {
2347  $sql .= " AND c.fk_soc = ".((int) $user->socid);
2348  }
2349  if (!$user->hasRight('societe', 'client', 'voir')) {
2350  $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2351  }
2352 
2353  $resql = $this->db->query($sql);
2354  if ($resql) {
2355  $langs->load("contracts");
2356  $now = dol_now();
2357 
2358  if ($mode == 'inactive') {
2359  $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2360  $label = $langs->trans("BoardNotActivatedServices");
2361  $labelShort = $langs->trans("BoardNotActivatedServicesShort");
2362  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2363  } elseif ($mode == 'expired') {
2364  $warning_delay = $conf->contrat->services->expires->warning_delay;
2365  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2366  $label = $langs->trans("BoardExpiredServices");
2367  $labelShort = $langs->trans("BoardExpiredServicesShort");
2368  } else {
2369  $warning_delay = $conf->contrat->services->expires->warning_delay;
2370  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2371  //$url.= '&op2day='.$arraydatetouse['mday'].'&op2month='.$arraydatetouse['mon'].'&op2year='.$arraydatetouse['year'];
2372  //if ($warning_delay >= 0) $url.='&amp;filter=expired';
2373  $label = $langs->trans("BoardRunningServices");
2374  $labelShort = $langs->trans("BoardRunningServicesShort");
2375  }
2376 
2377  $response = new WorkboardResponse();
2378  $response->warning_delay = $warning_delay / 60 / 60 / 24;
2379  $response->label = $label;
2380  $response->labelShort = $labelShort;
2381  $response->url = $url;
2382  $response->img = img_object('', "contract");
2383 
2384  while ($obj = $this->db->fetch_object($resql)) {
2385  $response->nbtodo++;
2386 
2387  if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2388  $response->nbtodolate++;
2389  }
2390  }
2391 
2392  return $response;
2393  } else {
2394  dol_print_error($this->db);
2395  $this->error = $this->db->error();
2396  return -1;
2397  }
2398  }
2399 
2405  public function loadStateBoard()
2406  {
2407  global $conf, $user;
2408 
2409  $this->nb = array();
2410  $clause = "WHERE";
2411 
2412  $sql = "SELECT count(c.rowid) as nb";
2413  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2414  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
2415  if (!$user->hasRight('societe', 'client', 'voir')) {
2416  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2417  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
2418  $clause = "AND";
2419  }
2420  $sql .= " ".$clause." c.entity = ".$conf->entity;
2421 
2422  $resql = $this->db->query($sql);
2423  if ($resql) {
2424  while ($obj = $this->db->fetch_object($resql)) {
2425  $this->nb["contracts"] = $obj->nb;
2426  }
2427  $this->db->free($resql);
2428  return 1;
2429  } else {
2430  dol_print_error($this->db);
2431  $this->error = $this->db->error();
2432  return -1;
2433  }
2434  }
2435 
2436 
2437  /* gestion des contacts d'un contrat */
2438 
2444  public function getIdBillingContact()
2445  {
2446  return $this->getIdContact('external', 'BILLING');
2447  }
2448 
2454  public function getIdServiceContact()
2455  {
2456  return $this->getIdContact('external', 'SERVICE');
2457  }
2458 
2459 
2467  public function initAsSpecimen()
2468  {
2469  global $user, $langs, $conf;
2470 
2471  // Load array of products prodids
2472  $num_prods = 0;
2473  $prodids = array();
2474  $sql = "SELECT rowid";
2475  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2476  $sql .= " WHERE entity IN (".getEntity('product').")";
2477  $sql .= " AND tosell = 1";
2478  $sql .= $this->db->plimit(100);
2479 
2480  $resql = $this->db->query($sql);
2481  if ($resql) {
2482  $num_prods = $this->db->num_rows($resql);
2483  $i = 0;
2484  while ($i < $num_prods) {
2485  $i++;
2486  $row = $this->db->fetch_row($resql);
2487  $prodids[$i] = $row[0];
2488  }
2489  }
2490 
2491  // Initialise parameters
2492  $this->id = 0;
2493  $this->specimen = 1;
2494 
2495  $this->ref = 'SPECIMEN';
2496  $this->ref_customer = 'SPECIMENCUST';
2497  $this->ref_supplier = 'SPECIMENSUPP';
2498  $this->socid = 1;
2499  $this->status = 0;
2500  $this->date_creation = (dol_now() - 3600 * 24 * 7);
2501  $this->date_contrat = dol_now();
2502  $this->commercial_signature_id = 1;
2503  $this->commercial_suivi_id = 1;
2504  $this->note_private = 'This is a comment (private)';
2505  $this->note_public = 'This is a comment (public)';
2506  $this->fk_project = 0;
2507  // Lines
2508  $nbp = 5;
2509  $xnbp = 0;
2510  while ($xnbp < $nbp) {
2511  $line = new ContratLigne($this->db);
2512  $line->qty = 1;
2513  $line->subprice = 100;
2514  $line->tva_tx = 19.6;
2515  $line->remise_percent = 10;
2516  $line->total_ht = 90;
2517  $line->total_ttc = 107.64; // 90 * 1.196
2518  $line->total_tva = 17.64;
2519  $line->date_start = dol_now() - 500000;
2520  $line->date_start_real = dol_now() - 200000;
2521  $line->date_end = dol_now() + 500000;
2522  $line->date_end_real = dol_now() - 100000;
2523  if ($num_prods > 0) {
2524  $prodid = mt_rand(1, $num_prods);
2525  $line->fk_product = $prodids[$prodid];
2526  }
2527  $this->lines[$xnbp] = $line;
2528  $xnbp++;
2529  }
2530 
2531  return 1;
2532  }
2533 
2539  public function getLinesArray()
2540  {
2541  return $this->fetch_lines();
2542  }
2543 
2549  public function getTicketsArray()
2550  {
2551  global $user;
2552 
2553  $ticket = new Ticket($this->db);
2554  $nbTicket = $ticket->fetchAll($user, 'ASC', 't.datec', '', 0, '', array('t.fk_contract' => $this->id));
2555 
2556  return ($nbTicket < 0 ? $nbTicket : $ticket->lines);
2557  }
2558 
2559 
2571  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2572  {
2573  global $conf, $langs;
2574 
2575  if (!dol_strlen($modele)) {
2576  $modele = ''; // No doc template/generation by default
2577 
2578  if (!empty($this->model_pdf)) {
2579  $modele = $this->model_pdf;
2580  } elseif (getDolGlobalString('CONTRACT_ADDON_PDF')) {
2581  $modele = getDolGlobalString('CONTRACT_ADDON_PDF');
2582  }
2583  }
2584 
2585  if (empty($modele)) {
2586  return 0;
2587  } else {
2588  $langs->load("contracts");
2589  $outputlangs->load("products");
2590 
2591  $modelpath = "core/modules/contract/doc/";
2592  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2593  }
2594  }
2595 
2604  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2605  {
2606  $tables = array(
2607  'contrat'
2608  );
2609 
2610  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2611  }
2612 
2621  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
2622  {
2623  $tables = array(
2624  'contratdet'
2625  );
2626 
2627  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
2628  }
2629 
2638  public function createFromClone(User $user, $socid = 0, $notrigger = 0)
2639  {
2640  global $db, $langs, $conf, $hookmanager, $extrafields;
2641 
2642  dol_include_once('/projet/class/project.class.php');
2643 
2644  $error = 0;
2645 
2646  $this->fetch($this->id);
2647 
2648  // Load dest object
2649  $clonedObj = clone $this;
2650  $clonedObj->socid = $socid;
2651 
2652  $this->db->begin();
2653 
2654  $objsoc = new Societe($this->db);
2655 
2656  $objsoc->fetch($clonedObj->socid);
2657 
2658  // Clean data
2659  $clonedObj->statut = 0;
2660  // Clean extrafields
2661  if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2662  $extrafields->fetch_name_optionals_label($this->table_element);
2663  foreach ($clonedObj->array_options as $key => $option) {
2664  $shortkey = preg_replace('/options_/', '', $key);
2665  //var_dump($shortkey); var_dump($extrafields->attributes[$this->element]['unique'][$shortkey]);
2666  if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
2667  //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
2668  unset($clonedObj->array_options[$key]);
2669  }
2670  }
2671  }
2672 
2673  if (!getDolGlobalString('CONTRACT_ADDON') || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').".php")) {
2674  $this->error = 'ErrorSetupNotComplete';
2675  dol_syslog($this->error);
2676  return -1;
2677  }
2678 
2679  // Set ref
2680  require_once DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').'.php';
2681  $obj = getDolGlobalString('CONTRACT_ADDON');
2682  $modContract = new $obj();
2683  '@phan-var-force CommonNumRefGenerator $modContrat';
2684  $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2685 
2686  // get extrafields so they will be clone
2687  foreach ($this->lines as $line) {
2688  $line->fetch_optionals($line->id);
2689  }
2690 
2691  // Create clone
2692  $clonedObj->context['createfromclone'] = 'createfromclone';
2693  $result = $clonedObj->create($user);
2694  if ($result < 0) {
2695  $error++;
2696  $this->error = $clonedObj->error;
2697  $this->errors[] = $clonedObj->error;
2698  } else {
2699  // copy external contacts if same company
2700  if ($this->socid == $clonedObj->socid) {
2701  if ($clonedObj->copy_linked_contact($this, 'external') < 0) {
2702  $error++;
2703  }
2704  }
2705  }
2706 
2707  if (!$error) {
2708  foreach ($this->lines as $line) {
2709  $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);
2710  if ($result < 0) {
2711  $error++;
2712  $this->error = $clonedObj->error;
2713  $this->errors[] = $clonedObj->error;
2714  }
2715  }
2716  }
2717 
2718  if (!$error) {
2719  // Hook of thirdparty module
2720  if (is_object($hookmanager)) {
2721  $parameters = array(
2722  'objFrom' => $this,
2723  'clonedObj' => $clonedObj
2724  );
2725  $action = '';
2726  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $clonedObj, $action); // Note that $action and $object may have been modified by some hooks
2727  if ($reshook < 0) {
2728  $this->setErrorsFromObject($hookmanager);
2729  $error++;
2730  }
2731  }
2732  }
2733 
2734  unset($clonedObj->context['createfromclone']);
2735 
2736  // End
2737  if (!$error) {
2738  $this->db->commit();
2739  return $clonedObj->id;
2740  } else {
2741  $this->db->rollback();
2742  return -1;
2743  }
2744  }
2745 
2746 
2756  public function doAutoRenewContracts($thirdparty_id = 0, $delayindaysshort = 0)
2757  {
2758  global $langs, $user;
2759 
2760  $langs->load("agenda");
2761 
2762  $now = dol_now();
2763 
2764  $enddatetoscan = dol_time_plus_duree($now, -1 * abs($delayindaysshort), 'd');
2765 
2766  $error = 0;
2767  $this->output = '';
2768  $this->error = '';
2769 
2770  $contractlineprocessed = array();
2771  $contractignored = array();
2772  $contracterror = array();
2773 
2774  dol_syslog(__METHOD__, LOG_DEBUG);
2775 
2776  $sql = 'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2777  $sql .= ' FROM '.MAIN_DB_PREFIX.'contrat as c, '.MAIN_DB_PREFIX.'contratdet as cd';
2778  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = cd.fk_product';
2779  $sql .= ' WHERE cd.fk_contrat = c.rowid';
2780  $sql .= " AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan)."', '%Y-%m-%d')";
2781  $sql .= " AND cd.statut = 4";
2782  if ($thirdparty_id > 0) {
2783  $sql .= " AND c.fk_soc = ".((int) $thirdparty_id);
2784  }
2785  //print $sql;
2786 
2787  $resql = $this->db->query($sql);
2788  if ($resql) {
2789  $num = $this->db->num_rows($resql);
2790 
2791  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2792 
2793  $i = 0;
2794  while ($i < $num) {
2795  $obj = $this->db->fetch_object($resql);
2796  if ($obj) {
2797  if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2798  continue;
2799  }
2800 
2801  // Load contract
2802  $object = new Contrat($this->db);
2803  $object->fetch($obj->rowid); // fetch also lines
2804  //$object->fetch_thirdparty();
2805 
2806  if ($object->id <= 0) {
2807  $error++;
2808  $this->errors[] = 'Failed to load contract with id='.$obj->rowid;
2809  continue;
2810  }
2811 
2812  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);
2813 
2814  // Update expiration date of line
2815  $expirationdate = $this->db->jdate($obj->date_fin_validite);
2816  $duration_value = preg_replace('/[^0-9]/', '', $obj->duration);
2817  $duration_unit = preg_replace('/\d/', '', $obj->duration);
2818  //var_dump($expirationdate.' '.$enddatetoscan);
2819 
2820  // Load linked ->linkedObjects (objects linked)
2821  // @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.
2822  $object->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
2823 
2824  // Test if there is at least 1 open invoice
2825  if (isset($object->linkedObjects['facture']) && is_array($object->linkedObjects['facture']) && count($object->linkedObjects['facture']) > 0) {
2826  // Sort array of linked invoices by ascending date
2827  usort($object->linkedObjects['facture'], array('Contrat', 'contractCmpDate'));
2828  //dol_sort_array($object->linkedObjects['facture'], 'date');
2829 
2830  $someinvoicenotpaid = 0;
2831  foreach ($object->linkedObjects['facture'] as $idinvoice => $invoice) {
2832  if ($invoice->statut == Facture::STATUS_DRAFT) {
2833  continue;
2834  } // Draft invoice are not invoice not paid
2835 
2836  if (empty($invoice->paye)) {
2837  $someinvoicenotpaid++;
2838  }
2839  }
2840  if ($someinvoicenotpaid) {
2841  $this->output .= 'Contract '.$object->ref.' is qualified for renewal but there is '.$someinvoicenotpaid.' invoice(s) unpayed so we cancel renewal'."\n";
2842  $contractignored[$object->id] = $object->ref;
2843  continue;
2844  }
2845  }
2846 
2847  if ($expirationdate && $expirationdate < $enddatetoscan) {
2848  dol_syslog("Define the newdate of end of services from expirationdate=".$expirationdate);
2849  $newdate = $expirationdate;
2850  $protecti = 0; //$protecti is to avoid infinite loop
2851  while ($newdate < $enddatetoscan && $protecti < 1000) {
2852  $newdate = dol_time_plus_duree($newdate, $duration_value, $duration_unit);
2853  $protecti++;
2854  }
2855 
2856  if ($protecti < 1000) { // If not, there is a pb
2857  // We will update the end of date of contrat, so first we refresh contract data
2858  dol_syslog("We will update the end of date of contract with newdate = ".dol_print_date($newdate, 'dayhourrfc'));
2859 
2860  $this->db->begin();
2861 
2862  $errorforlocaltransaction = 0;
2863 
2864  $label = 'Renewal of contrat '.$object->ref.' line '.$obj->lid;
2865  $comment = 'Renew date of contract '.$object->ref.' line '.$obj->lid.' by doAutoRenewContracts';
2866 
2867  $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."contratdet SET date_fin_validite = '".$this->db->idate($newdate)."'";
2868  $sqlupdate .= ' WHERE rowid = '.((int) $obj->lid);
2869  $resqlupdate = $this->db->query($sqlupdate);
2870  if ($resqlupdate) {
2871  $contractlineprocessed[$obj->lid] = $object->ref;
2872 
2873  $actioncode = 'RENEW_CONTRACT';
2874  $now = dol_now();
2875 
2876  // Create an event
2877  $actioncomm = new ActionComm($this->db);
2878  $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2879  $actioncomm->code = 'AC_'.$actioncode;
2880  $actioncomm->label = $label;
2881  $actioncomm->datep = $now;
2882  $actioncomm->datef = $now;
2883  $actioncomm->percentage = -1; // Not applicable
2884  $actioncomm->socid = $object->socid;
2885  $actioncomm->authorid = $user->id; // User saving action
2886  $actioncomm->userownerid = $user->id; // Owner of action
2887  $actioncomm->fk_element = $object->id;
2888  $actioncomm->elementtype = 'contract';
2889  $actioncomm->note_private = $comment;
2890 
2891  $ret = $actioncomm->create($user); // User creating action
2892  } else {
2893  $contracterror[$object->id] = $object->ref;
2894 
2895  $error++;
2896  $errorforlocaltransaction++;
2897  $this->error = $this->db->lasterror();
2898  }
2899 
2900  if (! $errorforlocaltransaction) {
2901  $this->db->commit();
2902  } else {
2903  $this->db->rollback();
2904  }
2905  } else {
2906  $error++;
2907  $this->error = "Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate." enddatetoscan=".$enddatetoscan." duration_value=".$duration_value." duration_unit=".$duration_value;
2908  dol_syslog($this->error, LOG_ERR);
2909  }
2910  }
2911  }
2912  $i++;
2913  }
2914  } else {
2915  $error++;
2916  $this->error = $this->db->lasterror();
2917  }
2918 
2919  $this->output .= count($contractlineprocessed).' contract line(s) with end date before '.dol_print_date($enddatetoscan, 'day').' were renewed'.(count($contractlineprocessed) > 0 ? ' : '.implode(',', $contractlineprocessed) : '');
2920 
2921  return ($error ? 1 : 0);
2922  }
2923 
2931  public static function contractCmpDate($a, $b)
2932  {
2933  if ($a->date == $b->date) {
2934  return strcmp((string) $a->id, (string) $b->id);
2935  }
2936  return ($a->date < $b->date) ? -1 : 1;
2937  }
2938 
2946  public function getKanbanView($option = '', $arraydata = null)
2947  {
2948  global $langs;
2949 
2950  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2951 
2952  $return = '<div class="box-flex-item box-flex-grow-zero">';
2953  $return .= '<div class="info-box info-box-sm">';
2954  $return .= '<span class="info-box-icon bg-infobox-action">';
2955  $return .= img_picto('', $this->picto);
2956  $return .= '</span>';
2957  $return .= '<div class="info-box-content">';
2958  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2959  if ($selected >= 0) {
2960  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2961  }
2962  if (!empty($arraydata['thirdparty'])) {
2963  $tmpthirdparty = $arraydata['thirdparty'];
2964  $return .= '<br><div class="info-box-label inline-block valignmiddle">'.$tmpthirdparty->getNomUrl(1).'</div>';
2965  }
2966  if (property_exists($this, 'date_contrat')) {
2967  $return .= '<br><span class="opacitymedium valignmiddle">'.$langs->trans("DateContract").' : </span><span class="info-box-label valignmiddle">'.dol_print_date($this->date_contrat, 'day').'</span>';
2968  }
2969  if (method_exists($this, 'getLibStatut')) {
2970  $return .= '<br><div class="info-box-status valignmiddle">'.$this->getLibStatut(7).'</div>';
2971  }
2972  $return .= '</div>';
2973  $return .= '</div>';
2974  $return .= '</div>';
2975 
2976  return $return;
2977  }
2978 
2979  // @Todo getLibSignedStatus, LibSignedStatus
2980 
2990  public function setSignedStatus(User $user, int $status = 0, int $notrigger = 0, $triggercode = ''): int
2991  {
2992  return $this->setSignedStatusCommon($user, $status, $notrigger, $triggercode);
2993  }
2994 }
2995 
2996 
3001 {
3005  public $element = 'contratdet';
3006 
3010  public $table_element = 'contratdet';
3011 
3016  public $element_for_permission = 'contrat';
3017 
3021  public $id;
3022 
3026  public $ref;
3027 
3031  public $fk_contrat;
3032 
3036  public $fk_product;
3037 
3038  public $statut; // 0 inactive, 4 active, 5 closed
3039  public $type; // 0 for product, 1 for service
3040 
3045  public $label;
3046 
3051  public $libelle;
3052 
3056  public $description;
3057 
3058  public $product_type; // 0 for product, 1 for service
3059  public $product_ref;
3060  public $product_label;
3061 
3062  public $date_commande;
3063 
3064  public $date_start; // date start planned
3065  public $date_start_real; // date start real
3066  public $date_end; // date end planned
3067  public $date_end_real; // date end real
3068 
3069  public $tva_tx;
3070  public $vat_src_code;
3071  public $localtax1_tx;
3072  public $localtax2_tx;
3073  public $localtax1_type; // Local tax 1 type
3074  public $localtax2_type; // Local tax 2 type
3075  public $qty;
3076  public $remise_percent;
3077  public $remise;
3078 
3082  public $fk_remise_except;
3083 
3084  public $subprice; // Unit price HT
3085 
3091  public $price;
3092 
3093  public $price_ht;
3094 
3095  public $total_ht;
3096  public $total_tva;
3097  public $total_localtax1;
3098  public $total_localtax2;
3099  public $total_ttc;
3100 
3104  public $fk_fournprice;
3105 
3106  public $pa_ht;
3107 
3111  public $info_bits;
3112 
3116  public $fk_user_author;
3117 
3121  public $fk_user_ouverture;
3122 
3126  public $fk_user_cloture;
3127 
3131  public $commentaire;
3132 
3133 
3137  public $rang = 0;
3138 
3139 
3140  const STATUS_INITIAL = 0;
3141  const STATUS_OPEN = 4;
3142  const STATUS_CLOSED = 5;
3143 
3144 
3145  // BEGIN MODULEBUILDER PROPERTIES
3149  public $fields = array(
3150  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
3151  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
3152  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
3153  'qty' => array('type' => 'integer', 'label' => 'Quantity', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 35, 'isameasure' => 1),
3154  'total_ht' => array('type' => 'integer', 'label' => 'AmountHT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 36, 'isameasure' => 1),
3155  'total_tva' => array('type' => 'integer', 'label' => 'AmountVAT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 37, 'isameasure' => 1),
3156  'total_ttc' => array('type' => 'integer', 'label' => 'AmountTTC', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 38, 'isameasure' => 1),
3157  //'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
3158  //'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
3159  'fk_contrat' => array('type' => 'integer:Contrat:contrat/class/contrat.class.php', 'label' => 'Contract', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
3160  'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'position' => 75),
3161  //'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
3162  'note_private' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105),
3163  'note_public' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110),
3164  //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
3165  //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
3166  //'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
3167  'fk_user_ouverture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserStartingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
3168  'fk_user_cloture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserClosingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
3169  'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'arrayofkeyval' => array(0 => 'Draft', 4 => 'Open', 5 => 'Closed')),
3170  'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'position' => 500, 'default' => '0')
3171  );
3172  // END MODULEBUILDER PROPERTIES
3173 
3174 
3180  public function __construct($db)
3181  {
3182  $this->db = $db;
3183  }
3184 
3185 
3192  public function getLibStatut($mode)
3193  {
3194  return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1));
3195  }
3196 
3197  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3208  public static function LibStatut($status, $mode, $expired = -1, $moreatt = '', $morelabel = '')
3209  {
3210  // phpcs:enable
3211  global $langs;
3212  $langs->load("contracts");
3213 
3214  if ($status == self::STATUS_INITIAL) {
3215  $labelStatus = $langs->transnoentities("ServiceStatusInitial");
3216  $labelStatusShort = $langs->transnoentities("ServiceStatusInitial");
3217  } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3218  $labelStatus = $langs->transnoentities("ServiceStatusRunning");
3219  $labelStatusShort = $langs->transnoentities("ServiceStatusRunning");
3220  } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3221  $labelStatus = $langs->transnoentities("ServiceStatusNotLate");
3222  $labelStatusShort = $langs->transnoentities("ServiceStatusNotLateShort");
3223  } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3224  $labelStatus = $langs->transnoentities("ServiceStatusLate");
3225  $labelStatusShort = $langs->transnoentities("ServiceStatusLateShort");
3226  } elseif ($status == self::STATUS_CLOSED) {
3227  $labelStatus = $langs->transnoentities("ServiceStatusClosed");
3228  $labelStatusShort = $langs->transnoentities("ServiceStatusClosed");
3229  }
3230 
3231  $statusType = 'status'.$status;
3232  if ($status == self::STATUS_OPEN && $expired == 1) {
3233  $statusType = 'status1';
3234  }
3235  if ($status == self::STATUS_CLOSED) {
3236  $statusType = 'status6';
3237  }
3238 
3239  $params = array();
3240  $reg = array();
3241  if (preg_match('/class="(.*)"/', $moreatt, $reg)) {
3242  $params = array('badgeParams' => array('css' => $reg[1]));
3243  }
3244  return dolGetStatus($labelStatus.($morelabel ? ' '.$morelabel : ''), $labelStatusShort.($morelabel ? ' '.$morelabel : ''), '', $statusType, $mode, '', $params);
3245  }
3246 
3253  public function getTooltipContentArray($params)
3254  {
3255  global $conf, $langs, $user;
3256 
3257  $datas = [];
3258  $datas['label'] = $langs->trans("ShowContractOfService").': '.$this->label;
3259  if (empty($datas['label'])) {
3260  $datas['label'] = $this->description;
3261  }
3262 
3263  return $datas;
3264  }
3265 
3273  public function getNomUrl($withpicto = 0, $maxlength = 0)
3274  {
3275  global $langs;
3276 
3277  $result = '';
3278  $label = $langs->trans("ShowContractOfService").': '.$this->label;
3279  if (empty($label)) {
3280  $label = $this->description;
3281  }
3282  $classfortooltip = 'classfortooltip';
3283  $dataparams = '';
3284  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
3285  $params = [
3286  'id' => $this->fk_contrat,
3287  'objecttype' => $this->element,
3288  ];
3289  $classfortooltip = 'classforajaxtooltip';
3290  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
3291  $label = '';
3292  }
3293 
3294  $link = '<a href="'.DOL_URL_ROOT.'/contrat/card.php?id='.$this->fk_contrat.'"';
3295  $link .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
3296  $link .= $dataparams.' class="'.$classfortooltip.'">';
3297  $linkend = '</a>';
3298 
3299  $picto = 'service';
3300  if ($this->type == 0) {
3301  $picto = 'product';
3302  }
3303 
3304  if ($withpicto) {
3305  $result .= ($link.img_object($label, $picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
3306  }
3307  if ($withpicto && $withpicto != 2) {
3308  $result .= ' ';
3309  }
3310  if ($withpicto != 2) {
3311  $result .= $link.($this->product_ref ? $this->product_ref.' ' : '').($this->label ? $this->label : $this->description).$linkend;
3312  }
3313  return $result;
3314  }
3315 
3323  public function fetch($id, $ref = '')
3324  {
3325  // Check parameters
3326  if (empty($id) && empty($ref)) {
3327  return -1;
3328  }
3329 
3330  $sql = "SELECT";
3331  $sql .= " t.rowid,";
3332  $sql .= " t.tms,";
3333  $sql .= " t.fk_contrat,";
3334  $sql .= " t.fk_product,";
3335  $sql .= " t.statut,";
3336  $sql .= " t.label,"; // This field is not used. Only label of product
3337  $sql .= " p.ref as product_ref,";
3338  $sql .= " p.label as product_label,";
3339  $sql .= " p.description as product_desc,";
3340  $sql .= " p.fk_product_type as product_type,";
3341  $sql .= " t.description,";
3342  $sql .= " t.date_commande,";
3343  $sql .= " t.date_ouverture_prevue as date_start,";
3344  $sql .= " t.date_ouverture as date_start_real,";
3345  $sql .= " t.date_fin_validite as date_end,";
3346  $sql .= " t.date_cloture as date_end_real,";
3347  $sql .= " t.tva_tx,";
3348  $sql .= " t.vat_src_code,";
3349  $sql .= " t.localtax1_tx,";
3350  $sql .= " t.localtax2_tx,";
3351  $sql .= " t.localtax1_type,";
3352  $sql .= " t.localtax2_type,";
3353  $sql .= " t.qty,";
3354  $sql .= " t.remise_percent,";
3355  $sql .= " t.remise,";
3356  $sql .= " t.fk_remise_except,";
3357  $sql .= " t.subprice,";
3358  $sql .= " t.price_ht,";
3359  $sql .= " t.total_ht,";
3360  $sql .= " t.total_tva,";
3361  $sql .= " t.total_localtax1,";
3362  $sql .= " t.total_localtax2,";
3363  $sql .= " t.total_ttc,";
3364  $sql .= " t.fk_product_fournisseur_price as fk_fournprice,";
3365  $sql .= " t.buy_price_ht as pa_ht,";
3366  $sql .= " t.info_bits,";
3367  $sql .= " t.fk_user_author,";
3368  $sql .= " t.fk_user_ouverture,";
3369  $sql .= " t.fk_user_cloture,";
3370  $sql .= " t.commentaire,";
3371  $sql .= " t.fk_unit,";
3372  $sql .= " t.rang";
3373  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as t LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = t.fk_product";
3374  if ($id) {
3375  $sql .= " WHERE t.rowid = ".((int) $id);
3376  }
3377  if ($ref) {
3378  $sql .= " WHERE t.rowid = '".$this->db->escape($ref)."'";
3379  }
3380 
3381  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
3382  $resql = $this->db->query($sql);
3383  if ($resql) {
3384  if ($this->db->num_rows($resql)) {
3385  $obj = $this->db->fetch_object($resql);
3386 
3387  $this->id = $obj->rowid;
3388  $this->ref = $obj->rowid;
3389 
3390  $this->tms = $this->db->jdate($obj->tms);
3391  $this->fk_contrat = $obj->fk_contrat;
3392  $this->fk_product = $obj->fk_product;
3393  $this->statut = $obj->statut;
3394  $this->product_ref = $obj->product_ref;
3395  $this->product_label = $obj->product_label;
3396  $this->product_type = $obj->product_type;
3397  $this->label = $obj->label; // deprecated. We do not use this field. Only ref and label of product, and description of contract line
3398  $this->description = $obj->description;
3399  $this->date_commande = $this->db->jdate($obj->date_commande);
3400 
3401  $this->date_start = $this->db->jdate($obj->date_start);
3402  $this->date_start_real = $this->db->jdate($obj->date_start_real);
3403  $this->date_end = $this->db->jdate($obj->date_end);
3404  $this->date_end_real = $this->db->jdate($obj->date_end_real);
3405  // For backward compatibility
3406  //$this->date_ouverture_prevue = $this->db->jdate($obj->date_ouverture_prevue);
3407  //$this->date_ouverture = $this->db->jdate($obj->date_ouverture);
3408  //$this->date_fin_validite = $this->db->jdate($obj->date_fin_validite);
3409  //$this->date_cloture = $this->db->jdate($obj->date_cloture);
3410 
3411  $this->tva_tx = $obj->tva_tx;
3412  $this->vat_src_code = $obj->vat_src_code;
3413  $this->localtax1_tx = $obj->localtax1_tx;
3414  $this->localtax2_tx = $obj->localtax2_tx;
3415  $this->localtax1_type = $obj->localtax1_type;
3416  $this->localtax2_type = $obj->localtax2_type;
3417  $this->qty = $obj->qty;
3418  $this->remise_percent = $obj->remise_percent;
3419  $this->fk_remise_except = $obj->fk_remise_except;
3420  $this->subprice = $obj->subprice;
3421  $this->price_ht = $obj->price_ht;
3422  $this->total_ht = $obj->total_ht;
3423  $this->total_tva = $obj->total_tva;
3424  $this->total_localtax1 = $obj->total_localtax1;
3425  $this->total_localtax2 = $obj->total_localtax2;
3426  $this->total_ttc = $obj->total_ttc;
3427  $this->info_bits = $obj->info_bits;
3428  $this->fk_user_author = $obj->fk_user_author;
3429  $this->fk_user_ouverture = $obj->fk_user_ouverture;
3430  $this->fk_user_cloture = $obj->fk_user_cloture;
3431  $this->commentaire = $obj->commentaire;
3432  $this->fk_fournprice = $obj->fk_fournprice;
3433 
3434  $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3435  $this->pa_ht = $marginInfos[0];
3436  $this->fk_unit = $obj->fk_unit;
3437 
3438  $this->rang = $obj->rang;
3439 
3440  $this->fetch_optionals();
3441  }
3442 
3443  $this->db->free($resql);
3444 
3445  return 1;
3446  } else {
3447  $this->error = "Error ".$this->db->lasterror();
3448  return -1;
3449  }
3450  }
3451 
3452 
3460  public function update($user, $notrigger = 0)
3461  {
3462  global $mysoc;
3463 
3464  $error = 0;
3465 
3466  // Clean parameters
3467  $this->fk_contrat = (int) $this->fk_contrat;
3468  $this->fk_product = (int) $this->fk_product;
3469  $this->statut = (int) $this->statut;
3470  $this->label = trim($this->label);
3471  $this->description = trim($this->description);
3472  $this->vat_src_code = trim($this->vat_src_code);
3473  $this->tva_tx = trim((string) $this->tva_tx);
3474  $this->localtax1_tx = trim($this->localtax1_tx);
3475  $this->localtax2_tx = trim($this->localtax2_tx);
3476  $this->qty = trim((string) $this->qty);
3477  $this->remise_percent = trim((string) $this->remise_percent);
3478  $this->fk_remise_except = (int) $this->fk_remise_except;
3479  $this->subprice = price2num($this->subprice);
3480  $this->price_ht = price2num($this->price_ht);
3481  $this->info_bits = (int) $this->info_bits;
3482  $this->fk_user_author = (int) $this->fk_user_author;
3483  $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3484  $this->fk_user_cloture = (int) $this->fk_user_cloture;
3485  $this->commentaire = trim($this->commentaire);
3486  $this->rang = (int) $this->rang;
3487  //if (empty($this->subprice)) $this->subprice = 0;
3488  if (empty($this->price_ht)) {
3489  $this->price_ht = 0;
3490  }
3491  if (empty($this->total_ht)) {
3492  $this->total_ht = 0;
3493  }
3494  if (empty($this->total_tva)) {
3495  $this->total_tva = 0;
3496  }
3497  if (empty($this->total_ttc)) {
3498  $this->total_ttc = 0;
3499  }
3500  if (empty($this->localtax1_tx)) {
3501  $this->localtax1_tx = 0;
3502  }
3503  if (empty($this->localtax2_tx)) {
3504  $this->localtax2_tx = 0;
3505  }
3506  if (empty($this->remise_percent)) {
3507  $this->remise_percent = 0;
3508  }
3509 
3510  // Calcul du total TTC et de la TVA pour la ligne a partir de
3511  // qty, pu, remise_percent et txtva
3512  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
3513  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
3514  $localtaxes_type = getLocalTaxesFromRate($this->tva_tx, 0, $this->thirdparty, $mysoc);
3515 
3516  $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);
3517  $this->total_ht = $tabprice[0];
3518  $this->total_tva = $tabprice[1];
3519  $this->total_ttc = $tabprice[2];
3520  $this->total_localtax1 = $tabprice[9];
3521  $this->total_localtax2 = $tabprice[10];
3522 
3523  if (empty($this->pa_ht)) {
3524  $this->pa_ht = 0;
3525  }
3526 
3527  // if buy price not defined, define buyprice as configured in margin admin
3528  if ($this->pa_ht == 0) {
3529  $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
3530  if ($result < 0) {
3531  return -1;
3532  } else {
3533  $this->pa_ht = $result;
3534  }
3535  }
3536 
3537  // $this->oldcopy should have been set by the caller of update (here properties were already modified)
3538  if (empty($this->oldcopy)) {
3539  dol_syslog("this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
3540  $this->oldcopy = dol_clone($this, 2);
3541  }
3542 
3543  $this->db->begin();
3544 
3545  // Update request
3546  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3547  $sql .= " fk_contrat = ".((int) $this->fk_contrat).",";
3548  $sql .= " fk_product = ".($this->fk_product ? ((int) $this->fk_product) : 'null').",";
3549  $sql .= " statut = ".((int) $this->statut).",";
3550  $sql .= " label = '".$this->db->escape($this->label)."',";
3551  $sql .= " description = '".$this->db->escape($this->description)."',";
3552  $sql .= " date_commande = ".($this->date_commande != '' ? "'".$this->db->idate($this->date_commande)."'" : "null").",";
3553  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3554  $sql .= " date_ouverture = ".($this->date_start_real != '' ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3555  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3556  $sql .= " date_cloture = ".($this->date_end_real != '' ? "'".$this->db->idate($this->date_end_real)."'" : "null").",";
3557  $sql .= " vat_src_code = '".$this->db->escape($this->vat_src_code)."',";
3558  $sql .= " tva_tx = ".price2num($this->tva_tx).",";
3559  $sql .= " localtax1_tx = ".price2num($this->localtax1_tx).",";
3560  $sql .= " localtax2_tx = ".price2num($this->localtax2_tx).",";
3561  $sql .= " qty = ".price2num($this->qty).",";
3562  $sql .= " remise_percent = ".price2num($this->remise_percent).",";
3563  $sql .= " remise = ".($this->remise ? price2num($this->remise) : "null").",";
3564  $sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except : "null").",";
3565  $sql .= " subprice = ".($this->subprice != '' ? $this->subprice : "null").",";
3566  $sql .= " price_ht = ".($this->price_ht != '' ? $this->price_ht : "null").",";
3567  $sql .= " total_ht = ".$this->total_ht.",";
3568  $sql .= " total_tva = ".$this->total_tva.",";
3569  $sql .= " total_localtax1 = ".$this->total_localtax1.",";
3570  $sql .= " total_localtax2 = ".$this->total_localtax2.",";
3571  $sql .= " total_ttc = ".$this->total_ttc.",";
3572  $sql .= " fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "NULL").",";
3573  $sql .= " buy_price_ht = '".price2num($this->pa_ht)."',";
3574  $sql .= " info_bits = '".$this->db->escape($this->info_bits)."',";
3575  $sql .= " fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author : "NULL").",";
3576  $sql .= " fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture : "NULL").",";
3577  $sql .= " fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture : "NULL").",";
3578  $sql .= " commentaire = '".$this->db->escape($this->commentaire)."',";
3579  $sql .= " fk_unit = ".(!$this->fk_unit ? 'NULL' : $this->fk_unit).",";
3580  $sql .= " rang = ".(empty($this->rang) ? '0' : (int) $this->rang);
3581  $sql .= " WHERE rowid = ".((int) $this->id);
3582 
3583  dol_syslog(get_class($this)."::update", LOG_DEBUG);
3584  $resql = $this->db->query($sql);
3585  if (!$resql) {
3586  $this->error = "Error ".$this->db->lasterror();
3587  $error++;
3588  }
3589 
3590  if (!$error) { // For avoid conflicts if trigger used
3591  $result = $this->insertExtraFields();
3592  if ($result < 0) {
3593  $error++;
3594  }
3595  }
3596 
3597  // If we change a planned date (start or end) of one contract line, sync dates for all other services too
3598  if (!$error && getDolGlobalString('CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES')) {
3599  dol_syslog(get_class($this)."::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3600 
3601  if ($this->date_start != $this->oldcopy->date_start) {
3602  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3603  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null");
3604  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3605 
3606  $resql = $this->db->query($sql);
3607  if (!$resql) {
3608  $error++;
3609  $this->error = "Error ".$this->db->lasterror();
3610  }
3611  }
3612  if ($this->date_end != $this->oldcopy->date_end) {
3613  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3614  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null");
3615  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3616 
3617  $resql = $this->db->query($sql);
3618  if (!$resql) {
3619  $error++;
3620  $this->error = "Error ".$this->db->lasterror();
3621  }
3622  }
3623  }
3624 
3625  if (!$error && !$notrigger) {
3626  // Call trigger
3627  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
3628  if ($result < 0) {
3629  $error++;
3630  $this->db->rollback();
3631  }
3632  // End call triggers
3633  }
3634 
3635  if (!$error) {
3636  $this->db->commit();
3637  return 1;
3638  } else {
3639  $this->db->rollback();
3640  $this->errors[] = $this->error;
3641  return -1;
3642  }
3643  }
3644 
3645 
3646  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3653  public function update_total()
3654  {
3655  // phpcs:enable
3656  $this->db->begin();
3657 
3658  // Mise a jour ligne en base
3659  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3660  $sql .= " total_ht=".price2num($this->total_ht, 'MT');
3661  $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
3662  $sql .= ",total_localtax1=".price2num($this->total_localtax1, 'MT');
3663  $sql .= ",total_localtax2=".price2num($this->total_localtax2, 'MT');
3664  $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
3665  $sql .= " WHERE rowid = ".((int) $this->id);
3666 
3667  dol_syslog(get_class($this)."::update_total", LOG_DEBUG);
3668 
3669  $resql = $this->db->query($sql);
3670  if ($resql) {
3671  $this->db->commit();
3672  return 1;
3673  } else {
3674  $this->error = $this->db->error();
3675  $this->db->rollback();
3676  return -2;
3677  }
3678  }
3679 
3680 
3687  public function insert($notrigger = 0)
3688  {
3689  global $user;
3690 
3691  $error = 0;
3692 
3693  // Insertion dans la base
3694  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
3695  $sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3696  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3697  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3698  $sql .= " info_bits,";
3699  $sql .= " rang,";
3700  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3701  if ($this->date_start > 0) {
3702  $sql .= ",date_ouverture_prevue";
3703  }
3704  if ($this->date_end > 0) {
3705  $sql .= ",date_fin_validite";
3706  }
3707  $sql .= ") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->description)."',";
3708  $sql .= ($this->fk_product > 0 ? $this->fk_product : "null").",";
3709  $sql .= " '".$this->db->escape($this->qty)."',";
3710  $sql .= " '".$this->db->escape($this->vat_src_code)."',";
3711  $sql .= " '".$this->db->escape($this->tva_tx)."',";
3712  $sql .= " '".$this->db->escape($this->localtax1_tx)."',";
3713  $sql .= " '".$this->db->escape($this->localtax2_tx)."',";
3714  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3715  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3716  $sql .= " ".price2num($this->remise_percent).",".price2num($this->subprice).",";
3717  $sql .= " ".price2num($this->total_ht).",".price2num($this->total_tva).",".price2num($this->total_localtax1).",".price2num($this->total_localtax2).",".price2num($this->total_ttc).",";
3718  $sql .= " '".$this->db->escape($this->info_bits)."',";
3719  $sql .= " ".(empty($this->rang) ? '0' : (int) $this->rang).",";
3720  $sql .= " ".price2num($this->price_ht).",".price2num($this->remise).",";
3721  if ($this->fk_fournprice > 0) {
3722  $sql .= ' '.((int) $this->fk_fournprice).',';
3723  } else {
3724  $sql .= ' null,';
3725  }
3726  if ($this->pa_ht > 0) {
3727  $sql .= ' '.((float) price2num($this->pa_ht));
3728  } else {
3729  $sql .= ' null';
3730  }
3731  if ($this->date_start > 0) {
3732  $sql .= ",'".$this->db->idate($this->date_start)."'";
3733  }
3734  if ($this->date_end > 0) {
3735  $sql .= ",'".$this->db->idate($this->date_end)."'";
3736  }
3737  $sql .= ")";
3738 
3739  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3740 
3741  $resql = $this->db->query($sql);
3742  if ($resql) {
3743  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'contratdet');
3744 
3745  // Insert of extrafields
3746  if (!$error) {
3747  $result = $this->insertExtraFields();
3748  if ($result < 0) {
3749  $this->db->rollback();
3750  return -1;
3751  }
3752  }
3753 
3754  if (!$notrigger) {
3755  // Call trigger
3756  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
3757  if ($result < 0) {
3758  $this->db->rollback();
3759  return -1;
3760  }
3761  // End call triggers
3762  }
3763 
3764  $this->db->commit();
3765  return 1;
3766  } else {
3767  $this->db->rollback();
3768  $this->error = $this->db->error()." sql=".$sql;
3769  return -1;
3770  }
3771  }
3772 
3773  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3783  public function active_line($user, $date, $date_end = '', $comment = '')
3784  {
3785  // phpcs:enable
3786  $error = 0;
3787 
3788  $this->db->begin();
3789 
3790  $this->statut = ContratLigne::STATUS_OPEN;
3791  $this->date_start_real = $date;
3792  $this->date_end = $date_end;
3793  $this->fk_user_ouverture = $user->id;
3794  $this->date_end_real = null;
3795  $this->commentaire = $comment;
3796 
3797  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) $this->statut).",";
3798  $sql .= " date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3799  if ($date_end >= 0) {
3800  $sql .= " date_fin_validite = ".(dol_strlen($this->date_end) != 0 ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3801  }
3802  $sql .= " fk_user_ouverture = ".((int) $this->fk_user_ouverture).",";
3803  $sql .= " date_cloture = null,";
3804  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3805  $sql .= " WHERE rowid = ".((int) $this->id)." AND (statut = ".ContratLigne::STATUS_INITIAL." OR statut = ".ContratLigne::STATUS_CLOSED.")";
3806 
3807  dol_syslog(get_class($this)."::active_line", LOG_DEBUG);
3808  $resql = $this->db->query($sql);
3809  if ($resql) {
3810  // Call trigger
3811  $result = $this->call_trigger('LINECONTRACT_ACTIVATE', $user);
3812  if ($result < 0) {
3813  $error++;
3814  }
3815  // End call triggers
3816 
3817  if (!$error) {
3818  $this->db->commit();
3819  return 1;
3820  } else {
3821  $this->db->rollback();
3822  return -1;
3823  }
3824  } else {
3825  $this->error = $this->db->lasterror();
3826  $this->db->rollback();
3827  return -1;
3828  }
3829  }
3830 
3831  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3841  public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)
3842  {
3843  // phpcs:enable
3844  $this->date_cloture = $date_end_real;
3845  $this->date_end_real = $date_end_real;
3846  $this->user_closing_id = $user->id;
3847  $this->commentaire = $comment;
3848 
3849  $error = 0;
3850 
3851  // statut actif : 4
3852 
3853  $this->db->begin();
3854 
3855  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).",";
3856  $sql .= " date_cloture = '".$this->db->idate($date_end_real)."',";
3857  $sql .= " fk_user_cloture = ".((int) $user->id).",";
3858  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3859  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = ".((int) ContratLigne::STATUS_OPEN);
3860 
3861  $resql = $this->db->query($sql);
3862  if ($resql) {
3863  if (!$notrigger) {
3864  // Call trigger
3865  $result = $this->call_trigger('LINECONTRACT_CLOSE', $user);
3866  if ($result < 0) {
3867  $error++;
3868  $this->db->rollback();
3869  return -1;
3870  }
3871  // End call triggers
3872  }
3873 
3874  $this->db->commit();
3875  return 1;
3876  } else {
3877  $this->error = $this->db->lasterror();
3878  $this->db->rollback();
3879  return -1;
3880  }
3881  }
3882 }
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:607
$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.
info($id)
Charge les information d'ordre info dans l'objet contrat.
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)
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:745
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:1609
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
Buy price without taxes.
Definition: style.css.php:960
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:123