dolibarr  18.0.0-alpha
contrat.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2012 Destailleur Laurent <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
6  * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
7  * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
8  * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
9  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
11  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
12  * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
13  * Copyright (C) 2015-2018 Ferran Marcet <fmarcet@2byte.es>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <https://www.gnu.org/licenses/>.
27  */
28 
35 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
36 require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
37 require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
38 require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
39 
43 class Contrat extends CommonObject
44 {
48  public $element = 'contrat';
49 
53  public $table_element = 'contrat';
54 
58  public $table_element_line = 'contratdet';
59 
63  public $fk_element = 'fk_contrat';
64 
68  public $picto = 'contract';
69 
74  public $ismultientitymanaged = 1;
75 
79  public $isextrafieldmanaged = 1;
80 
85  public $restrictiononfksoc = 1;
86 
90  protected $table_ref_field = 'ref';
91 
96  public $ref_customer;
97 
102  public $ref_supplier;
103 
108  public $entity;
109 
114  public $socid;
115 
116  public $societe; // Objet societe
117 
122  public $statut = 0; // 0=Draft,
123 
124  public $product;
125 
129  public $fk_user_author;
130 
136  public $user_author_id;
137 
141  public $user_creation;
142 
146  public $user_cloture;
147 
151  public $date_creation;
152 
156  public $date_modification;
157 
161  public $date_validation;
162 
166  public $date_contrat;
167 
168  public $commercial_signature_id;
169  public $commercial_suivi_id;
170 
175  public $fk_projet;
176 
177  public $extraparams = array();
178 
182  public $lines = array();
183 
184  public $nbofservices;
185  public $nbofserviceswait;
186  public $nbofservicesopened;
187  public $nbofservicesexpired;
188  public $nbofservicesclosed;
189  //public $lower_planned_end_date;
190  //public $higher_planner_end_date;
191 
196  protected $lines_id_index_mapper = array();
197 
198 
223  // BEGIN MODULEBUILDER PROPERTIES
227  public $fields = array(
228  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
229  'ref' =>array('type'=>'varchar(50)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'showoncombobox'=>1, 'position'=>15, 'searchall'=>1),
230  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>20),
231  'ref_customer' =>array('type'=>'varchar(50)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>25, 'searchall'=>1),
232  'ref_supplier' =>array('type'=>'varchar(50)', 'label'=>'RefSupplier', 'enabled'=>1, 'visible'=>-1, 'position'=>26, 'searchall'=>1),
233  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>30, 'index'=>1),
234  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>35),
235  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
236  'date_contrat' =>array('type'=>'datetime', 'label'=>'Date contrat', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
237  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'isModEnabled("societe")', 'visible'=>-1, 'notnull'=>1, 'position'=>70),
238  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label'=>'Project', 'enabled'=>"isModEnabled('project')", 'visible'=>-1, 'position'=>75),
239  'fk_commercial_signature' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'SaleRepresentative Signature', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
240  'fk_commercial_suivi' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'SaleRepresentative follower', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
241  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
242  'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>105, 'searchall'=>1),
243  'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110, 'searchall'=>1),
244  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
245  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
246  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
247  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>135),
248  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'Last main doc', 'enabled'=>1, 'visible'=>-1, 'position'=>140),
249  'statut' =>array('type'=>'smallint(6)', 'label'=>'Statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500, 'notnull'=>1, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Validated', 2=>'Closed'))
250  );
251  // END MODULEBUILDER PROPERTIES
252 
253  const STATUS_DRAFT = 0;
254  const STATUS_VALIDATED = 1;
255  const STATUS_CLOSED = 2;
256 
257 
258 
264  public function __construct($db)
265  {
266  $this->db = $db;
267  }
268 
275  public function getNextNumRef($soc)
276  {
277  global $db, $langs, $conf;
278  $langs->load("contracts");
279 
280  if (!empty($conf->global->CONTRACT_ADDON)) {
281  $mybool = false;
282 
283  $file = $conf->global->CONTRACT_ADDON.".php";
284  $classname = $conf->global->CONTRACT_ADDON;
285 
286  // Include file with class
287  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
288 
289  foreach ($dirmodels as $reldir) {
290  $dir = dol_buildpath($reldir."core/modules/contract/");
291 
292  // Load file with numbering class (if found)
293  $mybool |= @include_once $dir.$file;
294  }
295 
296  if (!$mybool) {
297  dol_print_error('', "Failed to include file ".$file);
298  return '';
299  }
300 
301  $obj = new $classname();
302  $numref = $obj->getNextValue($soc, $this);
303 
304  if ($numref != "") {
305  return $numref;
306  } else {
307  $this->error = $obj->error;
308  dol_print_error($db, get_class($this)."::getNextValue ".$obj->error);
309  return "";
310  }
311  } else {
312  $langs->load("errors");
313  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Contract"));
314  return "";
315  }
316  }
317 
318  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
329  public function active_line($user, $line_id, $date_start, $date_end = '', $comment = '')
330  {
331  // phpcs:enable
332  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
333  if ($result < 0) {
334  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
335  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
336  }
337  return $result;
338  }
339 
340 
341  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
351  public function close_line($user, $line_id, $date_end, $comment = '')
352  {
353  // phpcs:enable
354  $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
355  if ($result < 0) {
356  $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
357  $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
358  }
359  return $result;
360  }
361 
362 
374  public function activateAll($user, $date_start = '', $notrigger = 0, $comment = '', $date_end = '')
375  {
376  if (empty($date_start)) {
377  $date_start = dol_now();
378  }
379 
380  $this->db->begin();
381 
382  $error = 0;
383 
384  // Load lines
385  $this->fetch_lines();
386 
387  foreach ($this->lines as $contratline) {
388  // Open lines not already open
389  if ($contratline->statut != ContratLigne::STATUS_OPEN) {
390  $contratline->context = $this->context;
391 
392  $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment); // This call trigger LINECONTRACT_ACTIVATE
393  if ($result < 0) {
394  $error++;
395  $this->error = $contratline->error;
396  $this->errors = $contratline->errors;
397  break;
398  }
399  }
400  }
401 
402  if (!$error && $this->statut == 0) {
403  $result = $this->validate($user, '', $notrigger);
404  if ($result < 0) {
405  $error++;
406  }
407  }
408 
409  if (!$error) {
410  $this->db->commit();
411  return 1;
412  } else {
413  $this->db->rollback();
414  return -1;
415  }
416  }
417 
427  public function closeAll(User $user, $notrigger = 0, $comment = '')
428  {
429  $this->db->begin();
430 
431  // Load lines
432  $this->fetch_lines();
433 
434  $now = dol_now();
435 
436  $error = 0;
437 
438  foreach ($this->lines as $contratline) {
439  // Close lines not already closed
440  if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
441  $contratline->date_end_real = $now;
442  $contratline->date_cloture = $now; // For backward compatibility
443  $contratline->fk_user_cloture = $user->id;
444  $contratline->statut = ContratLigne::STATUS_CLOSED;
445  $result = $contratline->close_line($user, $now, $comment, $notrigger);
446  if ($result < 0) {
447  $error++;
448  $this->error = $contratline->error;
449  $this->errors = $contratline->errors;
450  break;
451  }
452  }
453  }
454 
455  if (!$error && $this->statut == 0) {
456  $result = $this->validate($user, '', $notrigger);
457  if ($result < 0) {
458  $error++;
459  }
460  }
461 
462  if (!$error) {
463  $this->db->commit();
464  return 1;
465  } else {
466  $this->db->rollback();
467  return -1;
468  }
469  }
470 
479  public function validate(User $user, $force_number = '', $notrigger = 0)
480  {
481  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
482  global $langs, $conf;
483 
484  $now = dol_now();
485 
486  $error = 0;
487  dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number);
488 
489 
490  $this->db->begin();
491 
492  $this->fetch_thirdparty();
493 
494  // A contract is validated so we can move thirdparty to status customer
495  if (empty($conf->global->CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION)) {
496  $result = $this->thirdparty->set_as_client();
497  }
498 
499  // Define new ref
500  if ($force_number) {
501  $num = $force_number;
502  } elseif (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
503  $num = $this->getNextNumRef($this->thirdparty);
504  } else {
505  $num = $this->ref;
506  }
507  $this->newref = dol_sanitizeFileName($num);
508 
509  if ($num) {
510  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$this->db->escape($num)."', statut = 1";
511  //$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
512  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 0";
513 
514  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
515  $resql = $this->db->query($sql);
516  if (!$resql) {
517  dol_print_error($this->db);
518  $error++;
519  $this->error = $this->db->lasterror();
520  }
521 
522  // Trigger calls
523  if (!$error && !$notrigger) {
524  // Call trigger
525  $result = $this->call_trigger('CONTRACT_VALIDATE', $user);
526  if ($result < 0) {
527  $error++;
528  }
529  // End call triggers
530  }
531 
532  if (!$error) {
533  $this->oldref = $this->ref;
534 
535  // Rename directory if dir was a temporary ref
536  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
537  // Now we rename also files into index
538  $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)."'";
539  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
540  $resql = $this->db->query($sql);
541  if (!$resql) {
542  $error++; $this->error = $this->db->lasterror();
543  }
544 
545  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
546  $oldref = dol_sanitizeFileName($this->ref);
547  $newref = dol_sanitizeFileName($num);
548  $dirsource = $conf->contract->dir_output.'/'.$oldref;
549  $dirdest = $conf->contract->dir_output.'/'.$newref;
550  if (!$error && file_exists($dirsource)) {
551  dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
552 
553  if (@rename($dirsource, $dirdest)) {
554  dol_syslog("Rename ok");
555  // Rename docs starting with $oldref with $newref
556  $listoffiles = dol_dir_list($conf->contract->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
557  foreach ($listoffiles as $fileentry) {
558  $dirsource = $fileentry['name'];
559  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
560  $dirsource = $fileentry['path'].'/'.$dirsource;
561  $dirdest = $fileentry['path'].'/'.$dirdest;
562  @rename($dirsource, $dirdest);
563  }
564  }
565  }
566  }
567  }
568 
569  // Set new ref and define current statut
570  if (!$error) {
571  $this->ref = $num;
572  $this->statut = 1;
573  $this->brouillon = 0;
574  $this->date_validation = $now;
575  }
576  } else {
577  $error++;
578  }
579 
580  if (!$error) {
581  $this->db->commit();
582  return 1;
583  } else {
584  $this->db->rollback();
585  return -1;
586  }
587  }
588 
596  public function reopen($user, $notrigger = 0)
597  {
598  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
599  global $langs, $conf;
600 
601  $now = dol_now();
602 
603  $error = 0;
604  dol_syslog(get_class($this).'::reopen user='.$user->id);
605 
606  $this->db->begin();
607 
608  $this->fetch_thirdparty();
609 
610  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0";
611  //$sql.= ", fk_user_valid = null, date_valid = null";
612  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 1";
613 
614  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
615  $resql = $this->db->query($sql);
616  if (!$resql) {
617  dol_print_error($this->db);
618  $error++;
619  $this->error = $this->db->lasterror();
620  }
621 
622  // Trigger calls
623  if (!$error && !$notrigger) {
624  // Call trigger
625  $result = $this->call_trigger('CONTRACT_REOPEN', $user);
626  if ($result < 0) {
627  $error++;
628  }
629  // End call triggers
630  }
631 
632  // Set new ref and define current status
633  if (!$error) {
634  $this->statut = 0;
635  $this->brouillon = 1;
636  $this->date_validation = $now;
637  }
638 
639  if (!$error) {
640  $this->db->commit();
641  return 1;
642  } else {
643  $this->db->rollback();
644  return -1;
645  }
646  }
647 
657  public function fetch($id, $ref = '', $ref_customer = '', $ref_supplier = '')
658  {
659  $sql = "SELECT rowid, statut, ref, fk_soc,";
660  $sql .= " ref_supplier, ref_customer,";
661  $sql .= " ref_ext,";
662  $sql .= " entity,";
663  $sql .= " date_contrat as datecontrat,";
664  $sql .= " fk_user_author,";
665  $sql .= " fk_projet as fk_project,";
666  $sql .= " fk_commercial_signature, fk_commercial_suivi,";
667  $sql .= " note_private, note_public, model_pdf, last_main_doc, extraparams";
668  $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
669  if (!$id) {
670  $sql .= " WHERE entity IN (".getEntity('contract').")";
671  } else {
672  $sql .= " WHERE rowid = ".(int) $id;
673  }
674  if ($ref_customer) {
675  $sql .= " AND ref_customer = '".$this->db->escape($ref_customer)."'";
676  }
677  if ($ref_supplier) {
678  $sql .= " AND ref_supplier = '".$this->db->escape($ref_supplier)."'";
679  }
680  if ($ref) {
681  $sql .= " AND ref = '".$this->db->escape($ref)."'";
682  }
683 
684  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
685  $resql = $this->db->query($sql);
686  if ($resql) {
687  $num = $this->db->num_rows($resql);
688  if ($num > 1) {
689  $this->error = 'Fetch found several records.';
690  dol_syslog($this->error, LOG_ERR);
691  $result = -2;
692  } elseif ($num) { // $num = 1
693  $obj = $this->db->fetch_object($resql);
694  if ($obj) {
695  $this->id = $obj->rowid;
696  $this->ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
697  $this->ref_customer = $obj->ref_customer;
698  $this->ref_supplier = $obj->ref_supplier;
699  $this->ref_ext = $obj->ref_ext;
700  $this->entity = $obj->entity;
701  $this->statut = $obj->statut;
702 
703  $this->date_contrat = $this->db->jdate($obj->datecontrat);
704  $this->date_creation = $this->db->jdate($obj->datecontrat);
705 
706  $this->user_author_id = $obj->fk_user_author;
707 
708  $this->commercial_signature_id = $obj->fk_commercial_signature;
709  $this->commercial_suivi_id = $obj->fk_commercial_suivi;
710 
711  $this->note_private = $obj->note_private;
712  $this->note_public = $obj->note_public;
713  $this->model_pdf = $obj->model_pdf;
714  $this->modelpdf = $obj->model_pdf; // deprecated
715 
716  $this->fk_projet = $obj->fk_project; // deprecated
717  $this->fk_project = $obj->fk_project;
718 
719  $this->socid = $obj->fk_soc;
720  $this->fk_soc = $obj->fk_soc;
721  $this->last_main_doc = $obj->last_main_doc;
722  $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : null);
723 
724  $this->db->free($resql);
725 
726  // Retrieve all extrafields
727  // fetch optionals attributes and labels
728  $result = $this->fetch_optionals();
729 
730  // Lines
731  if ($result >= 0 && !empty($this->table_element_line)) {
732  $result = $this->fetch_lines();
733  }
734 
735  if ($result < 0) {
736  $this->error = $this->db->lasterror();
737  return -3;
738  }
739 
740  return $this->id;
741  }
742  } else {
743  dol_syslog(get_class($this)."::fetch Contract not found");
744  $this->error = "Contract not found";
745  return 0;
746  }
747  } else {
748  dol_syslog(get_class($this)."::fetch Error searching contract");
749  $this->error = $this->db->error();
750  return -1;
751  }
752  }
753 
754  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
763  public function fetch_lines($only_services = 0, $loadalsotranslation = 0)
764  {
765  // phpcs:enable
766  global $langs, $conf;
767 
768  $this->nbofservices = 0;
769  $this->nbofserviceswait = 0;
770  $this->nbofservicesopened = 0;
771  $this->nbofservicesexpired = 0;
772  $this->nbofservicesclosed = 0;
773 
774  $total_ttc = 0;
775  $total_vat = 0;
776  $total_ht = 0;
777 
778  $now = dol_now();
779 
780  $this->lines = array();
781  $pos = 0;
782 
783  // Selects contract lines related to a product
784  $sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
785  $sql .= " d.rowid, d.fk_contrat, d.statut, 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,";
786  $sql .= " d.total_ht,";
787  $sql .= " d.total_tva,";
788  $sql .= " d.total_localtax1,";
789  $sql .= " d.total_localtax2,";
790  $sql .= " d.total_ttc,";
791  $sql .= " d.info_bits, d.fk_product,";
792  $sql .= " d.date_ouverture_prevue as date_start,";
793  $sql .= " d.date_ouverture as date_start_real,";
794  $sql .= " d.date_fin_validite as date_end,";
795  $sql .= " d.date_cloture as date_end_real,";
796  $sql .= " d.fk_user_author,";
797  $sql .= " d.fk_user_ouverture,";
798  $sql .= " d.fk_user_cloture,";
799  $sql .= " d.fk_unit,";
800  $sql .= " d.product_type as type,";
801  $sql .= " d.rang";
802  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
803  $sql .= " WHERE d.fk_contrat = ".((int) $this->id);
804  if ($only_services == 1) {
805  $sql .= " AND d.product_type = 1";
806  }
807  $sql .= " ORDER by d.rang ASC";
808 
809  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
810  $result = $this->db->query($sql);
811  if ($result) {
812  $num = $this->db->num_rows($result);
813  $i = 0;
814 
815  while ($i < $num) {
816  $objp = $this->db->fetch_object($result);
817 
818  $line = new ContratLigne($this->db);
819 
820  $line->id = $objp->rowid;
821  $line->ref = $objp->rowid;
822  $line->fk_contrat = $objp->fk_contrat;
823  $line->desc = $objp->description; // Description line
824  $line->qty = $objp->qty;
825  $line->vat_src_code = $objp->vat_src_code;
826  $line->tva_tx = $objp->tva_tx;
827  $line->localtax1_tx = $objp->localtax1_tx;
828  $line->localtax2_tx = $objp->localtax2_tx;
829  $line->localtax1_type = $objp->localtax1_type;
830  $line->localtax2_type = $objp->localtax2_type;
831  $line->subprice = $objp->subprice;
832  $line->statut = $objp->statut;
833  $line->remise_percent = $objp->remise_percent;
834  $line->price_ht = $objp->price_ht;
835  $line->price = $objp->price_ht; // For backward compatibility
836  $line->total_ht = $objp->total_ht;
837  $line->total_tva = $objp->total_tva;
838  $line->total_localtax1 = $objp->total_localtax1;
839  $line->total_localtax2 = $objp->total_localtax2;
840  $line->total_ttc = $objp->total_ttc;
841  $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
842  $line->info_bits = $objp->info_bits;
843  $line->type = $objp->type;
844 
845  $line->fk_fournprice = $objp->fk_fournprice;
846  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
847  $line->pa_ht = $marginInfos[0];
848 
849  $line->fk_user_author = $objp->fk_user_author;
850  $line->fk_user_ouverture = $objp->fk_user_ouverture;
851  $line->fk_user_cloture = $objp->fk_user_cloture;
852  $line->fk_unit = $objp->fk_unit;
853 
854  $line->ref = $objp->product_ref; // deprecated
855  $line->product_ref = $objp->product_ref; // Product Ref
856  $line->product_type = $objp->product_type; // Product Type
857  $line->product_desc = $objp->product_desc; // Product Description
858  $line->product_label = $objp->product_label; // Product Label
859 
860  $line->description = $objp->description;
861 
862  $line->date_start = $this->db->jdate($objp->date_start);
863  $line->date_start_real = $this->db->jdate($objp->date_start_real);
864  $line->date_end = $this->db->jdate($objp->date_end);
865  $line->date_end_real = $this->db->jdate($objp->date_end_real);
866  // For backward compatibility
867  //$line->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue);
868  //$line->date_ouverture = $this->db->jdate($objp->date_ouverture);
869  //$line->date_fin_validite = $this->db->jdate($objp->date_fin_validite);
870  //$line->date_cloture = $this->db->jdate($objp->date_cloture);
871  //$line->date_debut_prevue = $this->db->jdate($objp->date_ouverture_prevue);
872  //$line->date_debut_reel = $this->db->jdate($objp->date_ouverture);
873  //$line->date_fin_prevue = $this->db->jdate($objp->date_fin_validite);
874  //$line->date_fin_reel = $this->db->jdate($objp->date_cloture);
875 
876  $line->rang = $objp->rang;
877 
878  // Retrieve all extrafields for contract line
879  // fetch optionals attributes and labels
880  $line->fetch_optionals();
881 
882  // multilangs
883  if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
884  $tmpproduct = new Product($this->db);
885  $tmpproduct->fetch($objp->fk_product);
886  $tmpproduct->getMultiLangs();
887 
888  $line->multilangs = $tmpproduct->multilangs;
889  }
890 
891  $this->lines[$pos] = $line;
892 
893  $this->lines_id_index_mapper[$line->id] = $pos;
894 
895  //dol_syslog("1 ".$line->desc);
896  //dol_syslog("2 ".$line->product_desc);
897 
898  if ($line->statut == ContratLigne::STATUS_INITIAL) {
899  $this->nbofserviceswait++;
900  }
901  if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
902  $this->nbofservicesopened++;
903  }
904  if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
905  $this->nbofservicesexpired++;
906  }
907  if ($line->statut == ContratLigne::STATUS_CLOSED) {
908  $this->nbofservicesclosed++;
909  }
910 
911  $total_ttc += $objp->total_ttc; // TODO Not saved into database
912  $total_vat += $objp->total_tva;
913  $total_ht += $objp->total_ht;
914 
915  $i++;
916  $pos++;
917  }
918  $this->db->free($result);
919  } else {
920  dol_syslog(get_class($this)."::Fetch Error when reading lines of contracts linked to products");
921  return -3;
922  }
923 
924  // Now set the global properties on contract not stored into database.
925  $this->nbofservices = count($this->lines);
926  $this->total_ttc = price2num($total_ttc);
927  $this->total_tva = price2num($total_vat);
928  $this->total_ht = price2num($total_ht);
929 
930  return $this->lines;
931  }
932 
939  public function create($user)
940  {
941  global $conf, $langs, $mysoc;
942 
943  // Check parameters
944  $paramsok = 1;
945  if ($this->commercial_signature_id <= 0) {
946  $langs->load("commercial");
947  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeSignature"));
948  $paramsok = 0;
949  }
950  if ($this->commercial_suivi_id <= 0) {
951  $langs->load("commercial");
952  $this->error .= ($this->error ? "<br>" : '');
953  $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeFollowUp"));
954  $paramsok = 0;
955  }
956  if (!$paramsok) {
957  return -1;
958  }
959 
960 
961  $this->db->begin();
962 
963  $now = dol_now();
964 
965  // Insert contract
966  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,";
967  $sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet,";
968  $sql .= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
969  $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->socid).", ".((int) $user->id);
970  $sql .= ", ".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL");
971  $sql .= ",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) : "NULL");
972  $sql .= ",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) : "NULL");
973  $sql .= ",".($this->fk_project > 0 ? ((int) $this->fk_project) : "NULL");
974  $sql .= ", ".(dol_strlen($this->ref) <= 0 ? "null" : "'".$this->db->escape($this->ref)."'");
975  $sql .= ", ".((int) $conf->entity);
976  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
977  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
978  $sql .= ", ".(!empty($this->ref_customer) ? ("'".$this->db->escape($this->ref_customer)."'") : "NULL");
979  $sql .= ", ".(!empty($this->ref_supplier) ? ("'".$this->db->escape($this->ref_supplier)."'") : "NULL");
980  $sql .= ", ".(!empty($this->ref_ext) ? ("'".$this->db->escape($this->ref_ext)."'") : "NULL");
981  $sql .= ")";
982  $resql = $this->db->query($sql);
983 
984  if ($resql) {
985  $error = 0;
986 
987  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat");
988 
989  // Load object modContract
990  $module = (!empty($conf->global->CONTRACT_ADDON) ? $conf->global->CONTRACT_ADDON : 'mod_contract_serpis');
991  if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') {
992  $module = substr($module, 0, dol_strlen($module) - 4);
993  }
994  $result = dol_include_once('/core/modules/contract/'.$module.'.php');
995  if ($result > 0) {
996  $modCodeContract = new $module();
997 
998  if (!empty($modCodeContract->code_auto)) {
999  // Force the ref to a draft value if numbering module is an automatic numbering
1000  $sql = 'UPDATE '.MAIN_DB_PREFIX."contrat SET ref='(PROV".$this->id.")' WHERE rowid=".((int) $this->id);
1001  if ($this->db->query($sql)) {
1002  if ($this->id) {
1003  $this->ref = "(PROV".$this->id.")";
1004  }
1005  }
1006  }
1007  }
1008 
1009  if (!$error) {
1010  $result = $this->insertExtraFields();
1011  if ($result < 0) {
1012  $error++;
1013  }
1014  }
1015 
1016  // Insert business contacts ('SALESREPSIGN','contrat')
1017  if (!$error) {
1018  $result = $this->add_contact($this->commercial_signature_id, 'SALESREPSIGN', 'internal');
1019  if ($result < 0) {
1020  $error++;
1021  }
1022  }
1023 
1024  // Insert business contacts ('SALESREPFOLL','contrat')
1025  if (!$error) {
1026  $result = $this->add_contact($this->commercial_suivi_id, 'SALESREPFOLL', 'internal');
1027  if ($result < 0) {
1028  $error++;
1029  }
1030  }
1031 
1032  if (!$error) {
1033  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1034  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1035  }
1036 
1037  // Add object linked
1038  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1039  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1040  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, ...))
1041  foreach ($tmp_origin_id as $origin_id) {
1042  $ret = $this->add_object_linked($origin, $origin_id);
1043  if (!$ret) {
1044  $this->error = $this->db->lasterror();
1045  $error++;
1046  }
1047  }
1048  } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1049  {
1050  $origin_id = $tmp_origin_id;
1051  $ret = $this->add_object_linked($origin, $origin_id);
1052  if (!$ret) {
1053  $this->error = $this->db->lasterror();
1054  $error++;
1055  }
1056  }
1057  }
1058  }
1059 
1060  if (!$error && $this->id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) { // Get contact from origin object
1061  $originforcontact = $this->origin;
1062  $originidforcontact = $this->origin_id;
1063  if ($originforcontact == 'shipping') { // shipment and order share the same contacts. If creating from shipment we take data of order
1064  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1065  $exp = new Expedition($this->db);
1066  $exp->fetch($this->origin_id);
1067  $exp->fetchObjectLinked();
1068  if (count($exp->linkedObjectsIds['commande']) > 0) {
1069  foreach ($exp->linkedObjectsIds['commande'] as $key => $value) {
1070  $originforcontact = 'commande';
1071  $originidforcontact = $value;
1072  break; // We take first one
1073  }
1074  }
1075  }
1076 
1077  $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";
1078  $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'";
1079 
1080  $resqlcontact = $this->db->query($sqlcontact);
1081  if ($resqlcontact) {
1082  while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1083  if ($objcontact->source == 'internal' && in_array($objcontact->code, array('SALESREPSIGN', 'SALESREPFOLL'))) {
1084  continue; // ignore this, already forced previously
1085  }
1086 
1087  //print $objcontact->code.'-'.$objcontact->source.'-'.$objcontact->fk_socpeople."\n";
1088  $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
1089  }
1090  } else {
1091  dol_print_error($resqlcontact);
1092  }
1093  }
1094  }
1095 
1096  if (!$error) {
1097  // Call trigger
1098  $result = $this->call_trigger('CONTRACT_CREATE', $user);
1099  if ($result < 0) {
1100  $error++;
1101  }
1102  // End call triggers
1103 
1104  if (!$error) {
1105  $this->db->commit();
1106  return $this->id;
1107  } else {
1108  dol_syslog(get_class($this)."::create - 30 - ".$this->error, LOG_ERR);
1109  $this->db->rollback();
1110  return -3;
1111  }
1112  } else {
1113  $this->error = "Failed to add contract";
1114  dol_syslog(get_class($this)."::create - 20 - ".$this->error, LOG_ERR);
1115  $this->db->rollback();
1116  return -2;
1117  }
1118  } else {
1119  $this->error = $langs->trans("UnknownError: ".$this->db->error()." -", LOG_DEBUG);
1120 
1121  $this->db->rollback();
1122  return -1;
1123  }
1124  }
1125 
1126 
1133  public function delete($user)
1134  {
1135  global $conf, $langs;
1136  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1137 
1138  $error = 0;
1139 
1140  $this->db->begin();
1141 
1142  // Call trigger
1143  $result = $this->call_trigger('CONTRACT_DELETE', $user);
1144  if ($result < 0) {
1145  $error++;
1146  }
1147  // End call triggers
1148 
1149  if (!$error) {
1150  // Delete linked contacts
1151  $res = $this->delete_linked_contact();
1152  if ($res < 0) {
1153  dol_syslog(get_class($this)."::delete error", LOG_ERR);
1154  $error++;
1155  }
1156  }
1157 
1158  if (!$error) {
1159  // Delete linked object
1160  $res = $this->deleteObjectLinked();
1161  if ($res < 0) {
1162  $error++;
1163  }
1164  }
1165 
1166  if (!$error) {
1167  // Delete contratdet_log
1168  /*
1169  $sql = "DELETE cdl";
1170  $sql.= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd";
1171  $sql.= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->id);
1172  */
1173  $sql = "SELECT cdl.rowid as cdlrowid ";
1174  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd";
1175  $sql .= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->id);
1176 
1177  dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG);
1178  $resql = $this->db->query($sql);
1179  if (!$resql) {
1180  $this->error = $this->db->error();
1181  $error++;
1182  }
1183  $numressql = $this->db->num_rows($resql);
1184  if (!$error && $numressql) {
1185  $tab_resql = array();
1186  for ($i = 0; $i < $numressql; $i++) {
1187  $objresql = $this->db->fetch_object($resql);
1188  $tab_resql[] = $objresql->cdlrowid;
1189  }
1190  $this->db->free($resql);
1191 
1192  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet_log ";
1193  $sql .= " WHERE ".MAIN_DB_PREFIX."contratdet_log.rowid IN (".$this->db->sanitize(implode(",", $tab_resql)).")";
1194 
1195  dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG);
1196  $resql = $this->db->query($sql);
1197  if (!$resql) {
1198  $this->error = $this->db->error();
1199  $error++;
1200  }
1201  }
1202  }
1203 
1204  // Delete lines
1205  if (!$error) {
1206  // Delete contratdet extrafields
1207  $main = MAIN_DB_PREFIX.'contratdet';
1208  $ef = $main."_extrafields";
1209  $sql = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_contrat = ".((int) $this->id).")";
1210 
1211  dol_syslog(get_class($this)."::delete contratdet_extrafields", LOG_DEBUG);
1212  $resql = $this->db->query($sql);
1213  if (!$resql) {
1214  $this->error = $this->db->error();
1215  $error++;
1216  }
1217  }
1218 
1219  if (!$error) {
1220  // Delete contratdet
1221  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet";
1222  $sql .= " WHERE fk_contrat=".((int) $this->id);
1223 
1224  dol_syslog(get_class($this)."::delete contratdet", LOG_DEBUG);
1225  $resql = $this->db->query($sql);
1226  if (!$resql) {
1227  $this->error = $this->db->error();
1228  $error++;
1229  }
1230  }
1231 
1232  // Delete llx_ecm_files
1233  if (!$error) {
1234  $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);
1235  $resql = $this->db->query($sql);
1236  if (!$resql) {
1237  $this->error = $this->db->lasterror();
1238  $this->errors[] = $this->error;
1239  $error++;
1240  }
1241  }
1242 
1243  // Delete contract
1244  if (!$error) {
1245  $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat";
1246  $sql .= " WHERE rowid=".((int) $this->id);
1247 
1248  dol_syslog(get_class($this)."::delete contrat", LOG_DEBUG);
1249  $resql = $this->db->query($sql);
1250  if (!$resql) {
1251  $this->error = $this->db->error();
1252  $error++;
1253  }
1254  }
1255 
1256  // Removed extrafields
1257  if (!$error) {
1258  $result = $this->deleteExtraFields();
1259  if ($result < 0) {
1260  $error++;
1261  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1262  }
1263  }
1264 
1265  if (!$error) {
1266  // We remove directory
1267  $ref = dol_sanitizeFileName($this->ref);
1268  if ($conf->contrat->dir_output) {
1269  $dir = $conf->contrat->multidir_output[$this->entity]."/".$ref;
1270  if (file_exists($dir)) {
1271  $res = @dol_delete_dir_recursive($dir);
1272  if (!$res) {
1273  $this->error = 'ErrorFailToDeleteDir';
1274  $error++;
1275  }
1276  }
1277  }
1278  }
1279 
1280  if (!$error) {
1281  $this->db->commit();
1282  return 1;
1283  } else {
1284  $this->error = $this->db->lasterror();
1285  $this->db->rollback();
1286  return -1;
1287  }
1288  }
1289 
1297  public function update($user, $notrigger = 0)
1298  {
1299  global $conf, $langs;
1300  $error = 0;
1301 
1302  // Clean parameters
1303  if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1304  $this->fk_commercial_signature = $this->commercial_signature_id;
1305  }
1306  if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1307  $this->fk_commercial_suivi = $this->commercial_suivi_id;
1308  }
1309  if (empty($this->fk_soc) && $this->socid > 0) {
1310  $this->fk_soc = (int) $this->socid;
1311  }
1312  if (empty($this->fk_project) && $this->projet > 0) {
1313  $this->fk_project = (int) $this->projet;
1314  }
1315 
1316  if (isset($this->ref)) {
1317  $this->ref = trim($this->ref);
1318  }
1319  if (isset($this->ref_customer)) {
1320  $this->ref_customer = trim($this->ref_customer);
1321  }
1322  if (isset($this->ref_supplier)) {
1323  $this->ref_supplier = trim($this->ref_supplier);
1324  }
1325  if (isset($this->ref_ext)) {
1326  $this->ref_ext = trim($this->ref_ext);
1327  }
1328  if (isset($this->entity)) {
1329  $this->entity = (int) $this->entity;
1330  }
1331  if (isset($this->statut)) {
1332  $this->statut = (int) $this->statut;
1333  }
1334  if (isset($this->fk_soc)) {
1335  $this->fk_soc = (int) $this->fk_soc;
1336  }
1337  if (isset($this->fk_commercial_signature)) {
1338  $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1339  }
1340  if (isset($this->fk_commercial_suivi)) {
1341  $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1342  }
1343  if (isset($this->note_private)) {
1344  $this->note_private = trim($this->note_private);
1345  }
1346  if (isset($this->note_public)) {
1347  $this->note_public = trim($this->note_public);
1348  }
1349  if (isset($this->import_key)) {
1350  $this->import_key = trim($this->import_key);
1351  }
1352  //if (isset($this->extraparams)) $this->extraparams=trim($this->extraparams);
1353 
1354  // Check parameters
1355  // Put here code to add a control on parameters values
1356 
1357  // Update request
1358  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET";
1359  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1360  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1361  $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1362  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1363  $sql .= " entity=".$conf->entity.",";
1364  $sql .= " date_contrat=".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : 'null').",";
1365  $sql .= " statut=".(isset($this->statut) ? $this->statut : "null").",";
1366  $sql .= " fk_soc=".($this->fk_soc > 0 ? $this->fk_soc : "null").",";
1367  $sql .= " fk_projet=".($this->fk_project > 0 ? $this->fk_project : "null").",";
1368  $sql .= " fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature : "null").",";
1369  $sql .= " fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi : "null").",";
1370  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1371  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1372  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
1373  //$sql.= " extraparams=".(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null");
1374  $sql .= " WHERE rowid=".((int) $this->id);
1375 
1376  $this->db->begin();
1377 
1378  $resql = $this->db->query($sql);
1379  if (!$resql) {
1380  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1381  }
1382 
1383  if (!$error) {
1384  $result = $this->insertExtraFields();
1385  if ($result < 0) {
1386  $error++;
1387  }
1388  }
1389 
1390  if (!$error && !$notrigger) {
1391  // Call triggers
1392  $result = $this->call_trigger('CONTRACT_MODIFY', $user);
1393  if ($result < 0) {
1394  $error++;
1395  }
1396  // End call triggers
1397  }
1398 
1399  // Commit or rollback
1400  if ($error) {
1401  foreach ($this->errors as $errmsg) {
1402  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1403  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1404  }
1405  $this->db->rollback();
1406  return -1 * $error;
1407  } else {
1408  $this->db->commit();
1409  return 1;
1410  }
1411  }
1412 
1413 
1437  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 = 0, $fk_unit = null, $rang = 0)
1438  {
1439  global $user, $langs, $conf, $mysoc;
1440  $error = 0;
1441 
1442  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");
1443 
1444  // Check parameters
1445  if ($fk_product <= 0 && empty($desc)) {
1446  $this->error = "ErrorDescRequiredForFreeProductLines";
1447  return -1;
1448  }
1449 
1450  if ($this->statut >= 0) {
1451  // Clean parameters
1452  $pu_ht = price2num($pu_ht);
1453  $pu_ttc = price2num($pu_ttc);
1454  $pa_ht = price2num($pa_ht);
1455 
1456  // Clean vat code
1457  $reg = array();
1458  $vat_src_code = '';
1459  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
1460  $vat_src_code = $reg[1];
1461  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
1462  }
1463  $txtva = price2num($txtva);
1464  $txlocaltax1 = price2num($txlocaltax1);
1465  $txlocaltax2 = price2num($txlocaltax2);
1466 
1467  $remise_percent = price2num($remise_percent);
1468  $qty = price2num($qty);
1469  if (empty($qty)) {
1470  $qty = 1;
1471  }
1472  if (empty($info_bits)) {
1473  $info_bits = 0;
1474  }
1475  if (empty($pu_ht) || !is_numeric($pu_ht)) {
1476  $pu_ht = 0;
1477  }
1478  if (empty($pu_ttc)) {
1479  $pu_ttc = 0;
1480  }
1481  if (empty($txtva) || !is_numeric($txtva)) {
1482  $txtva = 0;
1483  }
1484  if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1485  $txlocaltax1 = 0;
1486  }
1487  if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1488  $txlocaltax2 = 0;
1489  }
1490 
1491  if ($price_base_type == 'HT') {
1492  $pu = $pu_ht;
1493  } else {
1494  $pu = $pu_ttc;
1495  }
1496 
1497  // Check parameters
1498  if (empty($remise_percent)) {
1499  $remise_percent = 0;
1500  }
1501  if (empty($rang)) {
1502  $rang = 0;
1503  }
1504 
1505  if ($date_start && $date_end && $date_start > $date_end) {
1506  $langs->load("errors");
1507  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1508  return -1;
1509  }
1510 
1511  $this->db->begin();
1512 
1513  $localtaxes_type = getLocalTaxesFromRate($txtva.($vat_src_code ? ' ('.$vat_src_code.')' : ''), 0, $this->societe, $mysoc);
1514 
1515  // Calcul du total TTC et de la TVA pour la ligne a partir de
1516  // qty, pu, remise_percent et txtva
1517  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1518  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1519 
1520  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1521  $total_ht = $tabprice[0];
1522  $total_tva = $tabprice[1];
1523  $total_ttc = $tabprice[2];
1524  $total_localtax1 = $tabprice[9];
1525  $total_localtax2 = $tabprice[10];
1526 
1527  $localtax1_type = $localtaxes_type[0];
1528  $localtax2_type = $localtaxes_type[2];
1529 
1530  // TODO A virer
1531  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1532  $remise = 0;
1533  $price = price2num(round($pu_ht, 2));
1534  if (dol_strlen($remise_percent) > 0) {
1535  $remise = round(($pu_ht * $remise_percent / 100), 2);
1536  $price = $pu_ht - $remise;
1537  }
1538 
1539  if (empty($pa_ht)) {
1540  $pa_ht = 0;
1541  }
1542 
1543 
1544  // if buy price not defined, define buyprice as configured in margin admin
1545  if ($this->pa_ht == 0) {
1546  if (($result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) {
1547  return $result;
1548  } else {
1549  $pa_ht = $result;
1550  }
1551  }
1552 
1553  // Insertion dans la base
1554  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
1555  $sql .= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1556  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1557  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1558  $sql .= " info_bits,";
1559  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1560  if ($date_start > 0) {
1561  $sql .= ",date_ouverture_prevue";
1562  }
1563  if ($date_end > 0) {
1564  $sql .= ",date_fin_validite";
1565  }
1566  $sql .= ", fk_unit";
1567  $sql .= ", rang";
1568  $sql .= ") VALUES (";
1569  $sql .= $this->id.", '', '".$this->db->escape($desc)."',";
1570  $sql .= ($fk_product > 0 ? $fk_product : "null").",";
1571  $sql .= " ".((float) $qty).",";
1572  $sql .= " ".((float) $txtva).",";
1573  $sql .= " ".($vat_src_code ? "'".$this->db->escape($vat_src_code)."'" : "null").",";
1574  $sql .= " ".((float) $txlocaltax1).",";
1575  $sql .= " ".((float) $txlocaltax2).",";
1576  $sql .= " '".$this->db->escape($localtax1_type)."',";
1577  $sql .= " '".$this->db->escape($localtax2_type)."',";
1578  $sql .= " ".price2num($remise_percent).",";
1579  $sql .= " ".price2num($pu_ht).",";
1580  $sql .= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).",";
1581  $sql .= " '".$this->db->escape($info_bits)."',";
1582  $sql .= " ".price2num($price).",".price2num($remise).",";
1583  if (isset($fk_fournprice)) {
1584  $sql .= ' '.((int) $fk_fournprice).',';
1585  } else {
1586  $sql .= ' null,';
1587  }
1588  if (isset($pa_ht)) {
1589  $sql .= ' '.price2num($pa_ht);
1590  } else {
1591  $sql .= ' null';
1592  }
1593  if ($date_start > 0) {
1594  $sql .= ",'".$this->db->idate($date_start)."'";
1595  }
1596  if ($date_end > 0) {
1597  $sql .= ",'".$this->db->idate($date_end)."'";
1598  }
1599  $sql .= ", ".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1600  $sql .= ", ".(!empty($rang) ? (int) $rang : "0");
1601  $sql .= ")";
1602 
1603  $resql = $this->db->query($sql);
1604  if ($resql) {
1605  $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX."contratdet");
1606 
1607  if (!$error) {
1608  $contractline = new ContratLigne($this->db);
1609  $contractline->array_options = $array_options;
1610  $contractline->id = $contractlineid;
1611  $result = $contractline->insertExtraFields();
1612  if ($result < 0) {
1613  $this->error[] = $contractline->error;
1614  $error++;
1615  }
1616  }
1617 
1618  if (empty($error)) {
1619  // Call trigger
1620  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
1621  if ($result < 0) {
1622  $error++;
1623  }
1624  // End call triggers
1625  }
1626 
1627  if ($error) {
1628  $this->db->rollback();
1629  return -1;
1630  } else {
1631  $this->db->commit();
1632  return $contractlineid;
1633  }
1634  } else {
1635  $this->db->rollback();
1636  $this->error = $this->db->error()." sql=".$sql;
1637  return -1;
1638  }
1639  } else {
1640  dol_syslog(get_class($this)."::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1641  return -2;
1642  }
1643  }
1644 
1669  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 = 0, $fk_unit = null, $rang = 0)
1670  {
1671  global $user, $conf, $langs, $mysoc;
1672 
1673  $error = 0;
1674 
1675  // Clean parameters
1676  $qty = trim($qty);
1677  $desc = trim($desc);
1678  $desc = trim($desc);
1679  $price = price2num($pu);
1680  $tvatx = price2num($tvatx);
1681  $localtax1tx = price2num($localtax1tx);
1682  $localtax2tx = price2num($localtax2tx);
1683  $pa_ht = price2num($pa_ht);
1684  if (empty($fk_fournprice)) {
1685  $fk_fournprice = 0;
1686  }
1687  if (empty($rang)) {
1688  $rang = 0;
1689  }
1690 
1691  $subprice = $price;
1692  $remise = 0;
1693  if (dol_strlen($remise_percent) > 0) {
1694  $remise = round(($pu * $remise_percent / 100), 2);
1695  $price = $pu - $remise;
1696  } else {
1697  $remise_percent = 0;
1698  }
1699 
1700  if ($date_start && $date_end && $date_start > $date_end) {
1701  $langs->load("errors");
1702  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1703  return -1;
1704  }
1705 
1706  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");
1707 
1708  $this->db->begin();
1709 
1710  // Calcul du total TTC et de la TVA pour la ligne a partir de
1711  // qty, pu, remise_percent et tvatx
1712  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1713  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1714 
1715  $localtaxes_type = getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc);
1716  $tvatx = preg_replace('/\s*\(.*\)/', '', $tvatx); // Remove code into vatrate.
1717 
1718  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1719  $total_ht = $tabprice[0];
1720  $total_tva = $tabprice[1];
1721  $total_ttc = $tabprice[2];
1722  $total_localtax1 = $tabprice[9];
1723  $total_localtax2 = $tabprice[10];
1724 
1725  $localtax1_type = (empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]);
1726  $localtax2_type = (empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]);
1727 
1728  // TODO A virer
1729  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
1730  $remise = 0;
1731  $price = price2num(round($pu, 2));
1732  if (dol_strlen($remise_percent) > 0) {
1733  $remise = round(($pu * $remise_percent / 100), 2);
1734  $price = $pu - $remise;
1735  }
1736 
1737  if (empty($pa_ht)) {
1738  $pa_ht = 0;
1739  }
1740 
1741  // if buy price not defined, define buyprice as configured in margin admin
1742  if ($this->pa_ht == 0) {
1743  if (($result = $this->defineBuyPrice($pu, $remise_percent)) < 0) {
1744  return $result;
1745  } else {
1746  $pa_ht = $result;
1747  }
1748  }
1749 
1750  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description = '".$this->db->escape($desc)."'";
1751  $sql .= ",price_ht = ".((float) price2num($price));
1752  $sql .= ",subprice = ".((float) price2num($subprice));
1753  $sql .= ",remise = ".((float) price2num($remise));
1754  $sql .= ",remise_percent = ".((float) price2num($remise_percent));
1755  $sql .= ",qty = ".((float) $qty);
1756  $sql .= ",tva_tx = ".((float) price2num($tvatx));
1757  $sql .= ",localtax1_tx = ".((float) price2num($localtax1tx));
1758  $sql .= ",localtax2_tx = ".((float) price2num($localtax2tx));
1759  $sql .= ",localtax1_type='".$this->db->escape($localtax1_type)."'";
1760  $sql .= ",localtax2_type='".$this->db->escape($localtax2_type)."'";
1761  $sql .= ", total_ht = ".((float) price2num($total_ht));
1762  $sql .= ", total_tva = ".((float) price2num($total_tva));
1763  $sql .= ", total_localtax1 = ".((float) price2num($total_localtax1));
1764  $sql .= ", total_localtax2 = ".((float) price2num($total_localtax2));
1765  $sql .= ", total_ttc = ".((float) price2num($total_ttc));
1766  $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null");
1767  $sql .= ", buy_price_ht = ".((float) price2num($pa_ht));
1768  if ($date_start > 0) {
1769  $sql .= ",date_ouverture_prevue = '".$this->db->idate($date_start)."'";
1770  } else {
1771  $sql .= ",date_ouverture_prevue = null";
1772  }
1773  if ($date_end > 0) {
1774  $sql .= ",date_fin_validite = '".$this->db->idate($date_end)."'";
1775  } else {
1776  $sql .= ",date_fin_validite = null";
1777  }
1778  if ($date_start_real > 0) {
1779  $sql .= ",date_ouverture = '".$this->db->idate($date_start_real)."'";
1780  } else {
1781  $sql .= ",date_ouverture = null";
1782  }
1783  if ($date_end_real > 0) {
1784  $sql .= ",date_cloture = '".$this->db->idate($date_end_real)."'";
1785  } else {
1786  $sql .= ",date_cloture = null";
1787  }
1788  $sql .= ", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) : "null");
1789  $sql .= ", rang = ".(!empty($rang) ? ((int) $rang) : "0");
1790  $sql .= " WHERE rowid = ".((int) $rowid);
1791 
1792  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1793  $result = $this->db->query($sql);
1794  if ($result) {
1795  if (is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1796  $contractline = new ContratLigne($this->db);
1797  $contractline->fetch($rowid);
1798 
1799  // We replace values in $contractline->array_options only for entries defined into $array_options
1800  foreach ($array_options as $key => $value) {
1801  $contractline->array_options[$key] = $array_options[$key];
1802  }
1803 
1804  $result = $contractline->insertExtraFields();
1805  if ($result < 0) {
1806  $this->error[] = $contractline->error;
1807  $error++;
1808  }
1809  }
1810 
1811  if (empty($error)) {
1812  // Call trigger
1813  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
1814  if ($result < 0) {
1815  $this->db->rollback();
1816  return -3;
1817  }
1818  // End call triggers
1819 
1820  $this->db->commit();
1821  return 1;
1822  }
1823  } else {
1824  $this->db->rollback();
1825  $this->error = $this->db->error();
1826  dol_syslog(get_class($this)."::updateline Erreur -1");
1827  return -1;
1828  }
1829  }
1830 
1838  public function deleteline($idline, User $user)
1839  {
1840  global $conf, $langs;
1841 
1842  $error = 0;
1843 
1844  if ($this->statut >= 0) {
1845  // Call trigger
1846  $result = $this->call_trigger('LINECONTRACT_DELETE', $user);
1847  if ($result < 0) {
1848  return -1;
1849  }
1850  // End call triggers
1851 
1852  $this->db->begin();
1853 
1854  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1855  $sql .= " WHERE rowid = ".((int) $idline);
1856 
1857  dol_syslog(get_class($this)."::deleteline", LOG_DEBUG);
1858  $resql = $this->db->query($sql);
1859  if (!$resql) {
1860  $this->error = "Error ".$this->db->lasterror();
1861  $error++;
1862  }
1863 
1864  if (!$error) {
1865  // Remove extrafields
1866  $contractline = new ContratLigne($this->db);
1867  $contractline->id = $idline;
1868  $result = $contractline->deleteExtraFields();
1869  if ($result < 0) {
1870  $error++;
1871  $this->error = "Error ".get_class($this)."::deleteline deleteExtraFields error -4 ".$contractline->error;
1872  }
1873  }
1874 
1875  if (empty($error)) {
1876  $this->db->commit();
1877  return 1;
1878  } else {
1879  dol_syslog(get_class($this)."::deleteline ERROR:".$this->error, LOG_ERR);
1880  $this->db->rollback();
1881  return -1;
1882  }
1883  } else {
1884  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1885  return -2;
1886  }
1887  }
1888 
1889 
1890  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1898  public function update_statut($user)
1899  {
1900  // phpcs:enable
1901  dol_syslog(__METHOD__." is deprecated", LOG_WARNING);
1902 
1903  // If draft, we keep it (should not happen)
1904  if ($this->statut == 0) {
1905  return 1;
1906  }
1907 
1908  // Load $this->lines array
1909  // $this->fetch_lines();
1910 
1911  // $newstatut=1;
1912  // foreach($this->lines as $key => $contractline)
1913  // {
1914  // // if ($contractline) // Loop on each service
1915  // }
1916 
1917  return 1;
1918  }
1919 
1920 
1927  public function getLibStatut($mode)
1928  {
1929  return $this->LibStatut($this->statut, $mode);
1930  }
1931 
1932  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1940  public function LibStatut($status, $mode)
1941  {
1942  // phpcs:enable
1943  global $langs;
1944 
1945  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1946  global $langs;
1947  $langs->load("contracts");
1948  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1949  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1950  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1951  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1952  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1953  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1954  }
1955 
1956  $statusType = 'status'.$status;
1957  if ($status == self::STATUS_VALIDATED) {
1958  $statusType = 'status6';
1959  }
1960 
1961  if ($mode == 4 || $mode == 6 || $mode == 7) {
1962  $text = '';
1963  if ($mode == 4) {
1964  $text = '<span class="hideonsmartphone">';
1965  $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1966  $text .= ' '.$langs->trans("Services");
1967  $text .= ': &nbsp; &nbsp; ';
1968  $text .= '</span>';
1969  }
1970  $text .= ($mode == 7 ? '<span class="nowraponall">' : '');
1971  $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1972  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1973  $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1974  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1975  $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
1976  $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
1977  $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="marginleft2"')) : '';
1978  $text .= ($mode == 7 ? '</span>' : '');
1979  return $text;
1980  } else {
1981  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1982  }
1983  }
1984 
1991  public function getTooltipContentArray($params)
1992  {
1993  global $conf, $langs, $user;
1994 
1995  $langs->load('contracts');
1996 
1997  $datas = [];
1998 
1999  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2000  return ['optimize' => $langs->trans("ShowContract")];
2001  }
2002  if ($user->hasRight('contrat', 'lire')) {
2003  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contract").'</u>';
2004  /* Status of a contract is status of all services, so disabled
2005  if (isset($this->statut)) {
2006  $label .= ' '.$this->getLibStatut(5);
2007  }*/
2008  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
2009  $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '. $this->ref_customer;
2010  $datas['refsupplier'] = '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
2011  if (!empty($this->total_ht)) {
2012  $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2013  }
2014  if (!empty($this->total_tva)) {
2015  $datas['vatamount'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2016  }
2017  if (!empty($this->total_ttc)) {
2018  $datas['amounttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2019  }
2020  }
2021  return $datas;
2022  }
2023 
2033  public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2034  {
2035  global $conf, $langs, $user, $hookmanager;
2036 
2037  $result = '';
2038 
2039  $url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id;
2040 
2041  //if ($option !== 'nolink')
2042  //{
2043  // Add param to save lastsearch_values or not
2044  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2045  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2046  $add_save_lastsearch_values = 1;
2047  }
2048  if ($add_save_lastsearch_values) {
2049  $url .= '&save_lastsearch_values=1';
2050  }
2051  //}
2052  $params = [
2053  'id' => $this->id,
2054  'objecttype' => $this->element,
2055  ];
2056  $classfortooltip = 'classfortooltip';
2057  $dataparams = '';
2058  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
2059  $classfortooltip = 'classforajaxtooltip';
2060  $dataparams = ' data-params='.json_encode($params);
2061  // $label = $langs->trans('Loading');
2062  }
2063 
2064  $label = implode($this->getTooltipContentArray($params));
2065 
2066  $linkclose = '';
2067  if (empty($notooltip) && $user->hasRight('contrat', 'lire')) {
2068  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2069  $label = $langs->trans("ShowContract");
2070  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
2071  }
2072  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
2073  $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
2074  }
2075  $linkstart = '<a href="'.$url.'"';
2076  $linkstart .= $linkclose.'>';
2077  $linkend = '</a>';
2078 
2079  $result .= $linkstart;
2080  if ($withpicto) {
2081  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
2082  }
2083  if ($withpicto != 2) {
2084  $result .= ($this->ref ? $this->ref : $this->id);
2085  }
2086  $result .= $linkend;
2087 
2088  global $action;
2089  $hookmanager->initHooks(array('contractdao'));
2090  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
2091  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
2092  if ($reshook > 0) {
2093  $result = $hookmanager->resPrint;
2094  } else {
2095  $result .= $hookmanager->resPrint;
2096  }
2097 
2098  return $result;
2099  }
2100 
2107  public function info($id)
2108  {
2109  $sql = "SELECT c.rowid, c.ref, c.datec,";
2110  $sql .= " c.tms as date_modification,";
2111  $sql .= " fk_user_author";
2112  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2113  $sql .= " WHERE c.rowid = ".((int) $id);
2114 
2115  $result = $this->db->query($sql);
2116  if ($result) {
2117  if ($this->db->num_rows($result)) {
2118  $obj = $this->db->fetch_object($result);
2119 
2120  $this->id = $obj->rowid;
2121 
2122  if ($obj->fk_user_author) {
2123  $cuser = new User($this->db);
2124  $cuser->fetch($obj->fk_user_author);
2125  $this->user_creation = $cuser;
2126  }
2127 
2128  $this->ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2129  $this->date_creation = $this->db->jdate($obj->datec);
2130  $this->date_modification = $this->db->jdate($obj->date_modification);
2131  }
2132 
2133  $this->db->free($result);
2134  } else {
2135  dol_print_error($this->db);
2136  }
2137  }
2138 
2139  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2146  public function array_detail($status = -1)
2147  {
2148  // phpcs:enable
2149  $tab = array();
2150 
2151  $sql = "SELECT cd.rowid";
2152  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
2153  $sql .= " WHERE fk_contrat =".((int) $this->id);
2154  if ($status >= 0) {
2155  $sql .= " AND statut = ".((int) $status);
2156  }
2157 
2158  dol_syslog(get_class($this)."::array_detail()", LOG_DEBUG);
2159  $resql = $this->db->query($sql);
2160  if ($resql) {
2161  $num = $this->db->num_rows($resql);
2162  $i = 0;
2163  while ($i < $num) {
2164  $obj = $this->db->fetch_object($resql);
2165  $tab[$i] = $obj->rowid;
2166  $i++;
2167  }
2168  return $tab;
2169  } else {
2170  $this->error = $this->db->error();
2171  return -1;
2172  }
2173  }
2174 
2184  public function getListOfContracts($option = 'all', $status = [], $product_categories = [], $line_status = [])
2185  {
2186  $tab = array();
2187 
2188  $sql = "SELECT c.rowid";
2189  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2190  if (!empty($product_categories)) {
2191  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON cd.fk_contrat = c.rowid";
2192  $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)).")";
2193  }
2194  $sql .= " WHERE c.fk_soc =".((int) $this->socid);
2195  $sql .= ($option == 'others') ? " AND c.rowid <> ".((int) $this->id) : "";
2196  $sql .= (!empty($status)) ? " AND c.statut IN (".$this->db->sanitize(implode(', ', $status)).")" : "";
2197  $sql .= (!empty($line_status)) ? " AND cd.statut IN (".$this->db->sanitize(implode(', ', $line_status)).")" : "";
2198  $sql .= " GROUP BY c.rowid";
2199 
2200  dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG);
2201  $resql = $this->db->query($sql);
2202  if ($resql) {
2203  $num = $this->db->num_rows($resql);
2204  $i = 0;
2205  while ($i < $num) {
2206  $obj = $this->db->fetch_object($resql);
2207  $contrat = new Contrat($this->db);
2208  $contrat->fetch($obj->rowid);
2209  $tab[$contrat->id] = $contrat;
2210  $i++;
2211  }
2212  return $tab;
2213  } else {
2214  $this->error = $this->db->lasterror();
2215  return -1;
2216  }
2217  }
2218 
2219 
2220  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2228  public function load_board($user, $mode)
2229  {
2230  // phpcs:enable
2231  global $conf, $langs;
2232 
2233  $this->from = " FROM ".MAIN_DB_PREFIX."contrat as c";
2234  $this->from .= ", ".MAIN_DB_PREFIX."contratdet as cd";
2235  $this->from .= ", ".MAIN_DB_PREFIX."societe as s";
2236  if (empty($user->rights->societe->client->voir) && !$user->socid) {
2237  $this->from .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2238  }
2239 
2240  if ($mode == 'inactive') {
2241  $sql = "SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2242  $sql .= $this->from;
2243  $sql .= " WHERE c.statut = 1";
2244  $sql .= " AND c.rowid = cd.fk_contrat";
2245  $sql .= " AND cd.statut = 0";
2246  } elseif ($mode == 'expired') {
2247  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2248  $sql .= $this->from;
2249  $sql .= " WHERE c.statut = 1";
2250  $sql .= " AND c.rowid = cd.fk_contrat";
2251  $sql .= " AND cd.statut = 4";
2252  $sql .= " AND cd.date_fin_validite < '".$this->db->idate(dol_now())."'";
2253  } elseif ($mode == 'active') {
2254  $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2255  $sql .= $this->from;
2256  $sql .= " WHERE c.statut = 1";
2257  $sql .= " AND c.rowid = cd.fk_contrat";
2258  $sql .= " AND cd.statut = 4";
2259  //$datetouse = dol_now();
2260  //$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'";
2261  }
2262  $sql .= " AND c.fk_soc = s.rowid";
2263  $sql .= " AND c.entity = ".((int) $conf->entity);
2264  if ($user->socid) {
2265  $sql .= " AND c.fk_soc = ".((int) $user->socid);
2266  }
2267  if (empty($user->rights->societe->client->voir) && !$user->socid) {
2268  $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2269  }
2270 
2271  $resql = $this->db->query($sql);
2272  if ($resql) {
2273  $langs->load("contracts");
2274  $now = dol_now();
2275 
2276  if ($mode == 'inactive') {
2277  $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2278  $label = $langs->trans("BoardNotActivatedServices");
2279  $labelShort = $langs->trans("BoardNotActivatedServicesShort");
2280  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2281  } elseif ($mode == 'expired') {
2282  $warning_delay = $conf->contrat->services->expires->warning_delay;
2283  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2284  $label = $langs->trans("BoardExpiredServices");
2285  $labelShort = $langs->trans("BoardExpiredServicesShort");
2286  } else {
2287  $warning_delay = $conf->contrat->services->expires->warning_delay;
2288  $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2289  //$url.= '&op2day='.$arraydatetouse['mday'].'&op2month='.$arraydatetouse['mon'].'&op2year='.$arraydatetouse['year'];
2290  //if ($warning_delay >= 0) $url.='&amp;filter=expired';
2291  $label = $langs->trans("BoardRunningServices");
2292  $labelShort = $langs->trans("BoardRunningServicesShort");
2293  }
2294 
2295  $response = new WorkboardResponse();
2296  $response->warning_delay = $warning_delay / 60 / 60 / 24;
2297  $response->label = $label;
2298  $response->labelShort = $labelShort;
2299  $response->url = $url;
2300  $response->img = img_object('', "contract");
2301 
2302  while ($obj = $this->db->fetch_object($resql)) {
2303  $response->nbtodo++;
2304 
2305  if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2306  $response->nbtodolate++;
2307  }
2308  }
2309 
2310  return $response;
2311  } else {
2312  dol_print_error($this->db);
2313  $this->error = $this->db->error();
2314  return -1;
2315  }
2316  }
2317 
2318  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2324  public function load_state_board()
2325  {
2326  // phpcs:enable
2327  global $conf, $user;
2328 
2329  $this->nb = array();
2330  $clause = "WHERE";
2331 
2332  $sql = "SELECT count(c.rowid) as nb";
2333  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2334  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
2335  if (empty($user->rights->societe->client->voir) && !$user->socid) {
2336  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2337  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
2338  $clause = "AND";
2339  }
2340  $sql .= " ".$clause." c.entity = ".$conf->entity;
2341 
2342  $resql = $this->db->query($sql);
2343  if ($resql) {
2344  while ($obj = $this->db->fetch_object($resql)) {
2345  $this->nb["contracts"] = $obj->nb;
2346  }
2347  $this->db->free($resql);
2348  return 1;
2349  } else {
2350  dol_print_error($this->db);
2351  $this->error = $this->db->error();
2352  return -1;
2353  }
2354  }
2355 
2356 
2357  /* gestion des contacts d'un contrat */
2358 
2364  public function getIdBillingContact()
2365  {
2366  return $this->getIdContact('external', 'BILLING');
2367  }
2368 
2374  public function getIdServiceContact()
2375  {
2376  return $this->getIdContact('external', 'SERVICE');
2377  }
2378 
2379 
2387  public function initAsSpecimen()
2388  {
2389  global $user, $langs, $conf;
2390 
2391  // Load array of products prodids
2392  $num_prods = 0;
2393  $prodids = array();
2394  $sql = "SELECT rowid";
2395  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2396  $sql .= " WHERE entity IN (".getEntity('product').")";
2397  $sql .= " AND tosell = 1";
2398  $sql .= $this->db->plimit(100);
2399 
2400  $resql = $this->db->query($sql);
2401  if ($resql) {
2402  $num_prods = $this->db->num_rows($resql);
2403  $i = 0;
2404  while ($i < $num_prods) {
2405  $i++;
2406  $row = $this->db->fetch_row($resql);
2407  $prodids[$i] = $row[0];
2408  }
2409  }
2410 
2411  // Initialise parametres
2412  $this->id = 0;
2413  $this->specimen = 1;
2414 
2415  $this->ref = 'SPECIMEN';
2416  $this->ref_customer = 'SPECIMENCUST';
2417  $this->ref_supplier = 'SPECIMENSUPP';
2418  $this->socid = 1;
2419  $this->statut = 0;
2420  $this->date_creation = (dol_now() - 3600 * 24 * 7);
2421  $this->date_contrat = dol_now();
2422  $this->commercial_signature_id = 1;
2423  $this->commercial_suivi_id = 1;
2424  $this->note_private = 'This is a comment (private)';
2425  $this->note_public = 'This is a comment (public)';
2426  $this->fk_projet = 0;
2427  // Lines
2428  $nbp = 5;
2429  $xnbp = 0;
2430  while ($xnbp < $nbp) {
2431  $line = new ContratLigne($this->db);
2432  $line->qty = 1;
2433  $line->subprice = 100;
2434  $line->price = 100;
2435  $line->tva_tx = 19.6;
2436  $line->remise_percent = 10;
2437  $line->total_ht = 90;
2438  $line->total_ttc = 107.64; // 90 * 1.196
2439  $line->total_tva = 17.64;
2440  $line->date_start = dol_now() - 500000;
2441  $line->date_start_real = dol_now() - 200000;
2442  $line->date_end = dol_now() + 500000;
2443  $line->date_end_real = dol_now() - 100000;
2444  if ($num_prods > 0) {
2445  $prodid = mt_rand(1, $num_prods);
2446  $line->fk_product = $prodids[$prodid];
2447  }
2448  $this->lines[$xnbp] = $line;
2449  $xnbp++;
2450  }
2451  }
2452 
2458  public function getLinesArray()
2459  {
2460  return $this->fetch_lines();
2461  }
2462 
2463 
2475  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2476  {
2477  global $conf, $langs;
2478 
2479  if (!dol_strlen($modele)) {
2480  $modele = ''; // No doc template/generation by default
2481 
2482  if (!empty($this->model_pdf)) {
2483  $modele = $this->model_pdf;
2484  } elseif (!empty($this->modelpdf)) { // deprecated
2485  $modele = $this->modelpdf;
2486  } elseif (!empty($conf->global->CONTRACT_ADDON_PDF)) {
2487  $modele = $conf->global->CONTRACT_ADDON_PDF;
2488  }
2489  }
2490 
2491  if (empty($modele)) {
2492  return 0;
2493  } else {
2494  $langs->load("contracts");
2495  $outputlangs->load("products");
2496 
2497  $modelpath = "core/modules/contract/doc/";
2498  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2499  }
2500  }
2501 
2510  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2511  {
2512  $tables = array(
2513  'contrat'
2514  );
2515 
2516  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2517  }
2518 
2527  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
2528  {
2529  $tables = array(
2530  'contratdet'
2531  );
2532 
2533  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
2534  }
2535 
2544  public function createFromClone(User $user, $socid = 0, $notrigger = 0)
2545  {
2546  global $db, $langs, $conf, $hookmanager, $extrafields;
2547 
2548  dol_include_once('/projet/class/project.class.php');
2549 
2550  $error = 0;
2551 
2552  $this->fetch($this->id);
2553 
2554  // Load dest object
2555  $clonedObj = clone $this;
2556  $clonedObj->socid = $socid;
2557 
2558  $this->db->begin();
2559 
2560  $objsoc = new Societe($this->db);
2561 
2562  $objsoc->fetch($clonedObj->socid);
2563 
2564  // Clean data
2565  $clonedObj->statut = 0;
2566  // Clean extrafields
2567  if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2568  $extrafields->fetch_name_optionals_label($this->table_element);
2569  foreach ($clonedObj->array_options as $key => $option) {
2570  $shortkey = preg_replace('/options_/', '', $key);
2571  //var_dump($shortkey); var_dump($extrafields->attributes[$this->element]['unique'][$shortkey]);
2572  if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
2573  //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
2574  unset($clonedObj->array_options[$key]);
2575  }
2576  }
2577  }
2578 
2579  if (empty($conf->global->CONTRACT_ADDON) || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/contract/".$conf->global->CONTRACT_ADDON.".php")) {
2580  $this->error = 'ErrorSetupNotComplete';
2581  dol_syslog($this->error);
2582  return -1;
2583  }
2584 
2585  // Set ref
2586  require_once DOL_DOCUMENT_ROOT."/core/modules/contract/".$conf->global->CONTRACT_ADDON.'.php';
2587  $obj = $conf->global->CONTRACT_ADDON;
2588  $modContract = new $obj();
2589  $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2590 
2591  // get extrafields so they will be clone
2592  foreach ($this->lines as $line) {
2593  $line->fetch_optionals($line->id);
2594  }
2595 
2596  // Create clone
2597  $clonedObj->context['createfromclone'] = 'createfromclone';
2598  $result = $clonedObj->create($user);
2599  if ($result < 0) {
2600  $error++;
2601  $this->error = $clonedObj->error;
2602  $this->errors[] = $clonedObj->error;
2603  } else {
2604  // copy external contacts if same company
2605  if ($this->socid == $clonedObj->socid) {
2606  if ($clonedObj->copy_linked_contact($this, 'external') < 0) {
2607  $error++;
2608  }
2609  }
2610  }
2611 
2612  if (!$error) {
2613  foreach ($this->lines as $line) {
2614  $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_ouverture, $line->date_cloture, 'HT', 0, $line->info_bits, $line->fk_fournprice, $line->pa_ht, $line->array_options, $line->fk_unit, $line->rang);
2615  if ($result < 0) {
2616  $error++;
2617  $this->error = $clonedObj->error;
2618  $this->errors[] = $clonedObj->error;
2619  }
2620  }
2621  }
2622 
2623  if (!$error) {
2624  // Hook of thirdparty module
2625  if (is_object($hookmanager)) {
2626  $parameters = array(
2627  'objFrom' => $this,
2628  'clonedObj' => $clonedObj
2629  );
2630  $action = '';
2631  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $clonedObj, $action); // Note that $action and $object may have been modified by some hooks
2632  if ($reshook < 0) {
2633  $this->setErrorsFromObject($hookmanager);
2634  $error++;
2635  }
2636  }
2637  }
2638 
2639  unset($clonedObj->context['createfromclone']);
2640 
2641  // End
2642  if (!$error) {
2643  $this->db->commit();
2644  return $clonedObj->id;
2645  } else {
2646  $this->db->rollback();
2647  return -1;
2648  }
2649  }
2650 
2651 
2661  public function doAutoRenewContracts($thirdparty_id = 0, $delayindaysshort = 0)
2662  {
2663  global $langs, $user;
2664 
2665  $langs->load("agenda");
2666 
2667  $now = dol_now();
2668 
2669  $enddatetoscan = dol_time_plus_duree($now, -1 * abs($delayindaysshort), 'd');
2670 
2671  $error = 0;
2672  $this->output = '';
2673  $this->error='';
2674 
2675  $contractlineprocessed = array();
2676  $contractignored = array();
2677  $contracterror = array();
2678 
2679  dol_syslog(__METHOD__, LOG_DEBUG);
2680 
2681  $sql = 'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2682  $sql.= ' FROM '.MAIN_DB_PREFIX.'contrat as c, '.MAIN_DB_PREFIX.'contratdet as cd';
2683  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = cd.fk_product';
2684  $sql.= ' WHERE cd.fk_contrat = c.rowid';
2685  $sql.= " AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan)."', '%Y-%m-%d')";
2686  $sql.= " AND cd.statut = 4";
2687  if ($thirdparty_id > 0) $sql.=" AND c.fk_soc = ".((int) $thirdparty_id);
2688  //print $sql;
2689 
2690  $resql = $this->db->query($sql);
2691  if ($resql) {
2692  $num = $this->db->num_rows($resql);
2693 
2694  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2695 
2696  $i=0;
2697  while ($i < $num) {
2698  $obj = $this->db->fetch_object($resql);
2699  if ($obj) {
2700  if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2701  continue;
2702  }
2703 
2704  // Load contract
2705  $object = new Contrat($this->db);
2706  $object->fetch($obj->rowid); // fetch also lines
2707  $object->fetch_thirdparty();
2708 
2709  if ($object->id <= 0) {
2710  $error++;
2711  $this->errors[] = 'Failed to load contract with id='.$obj->rowid;
2712  continue;
2713  }
2714 
2715  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);
2716 
2717  // Update expiration date of line
2718  $expirationdate = $this->db->jdate($obj->date_fin_validite);
2719  $duration_value = preg_replace('/[^0-9]/', '', $obj->duration);
2720  $duration_unit = preg_replace('/\d/', '', $obj->duration);
2721  //var_dump($expirationdate.' '.$enddatetoscan);
2722 
2723  // Test if there is pending invoice
2724  $object->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
2725 
2726  if (is_array($object->linkedObjects['facture']) && count($object->linkedObjects['facture']) > 0) {
2727  usort($object->linkedObjects['facture'], "cmp");
2728 
2729  //dol_sort_array($contract->linkedObjects['facture'], 'date');
2730  $someinvoicenotpaid=0;
2731  foreach ($object->linkedObjects['facture'] as $idinvoice => $invoice) {
2732  if ($invoice->statut == Facture::STATUS_DRAFT) continue; // Draft invoice are not invoice not paid
2733 
2734  if (empty($invoice->paye)) {
2735  $someinvoicenotpaid++;
2736  }
2737  }
2738  if ($someinvoicenotpaid) {
2739  $this->output .= 'Contract '.$object->ref.' is qualified for renewal but there is '.$someinvoicenotpaid.' invoice(s) unpayed so we cancel renewal'."\n";
2740  $contractignored[$object->id]=$object->ref;
2741  continue;
2742  }
2743  }
2744 
2745  if ($expirationdate && $expirationdate < $enddatetoscan) {
2746  dol_syslog("Define the newdate of end of services from expirationdate=".$expirationdate);
2747  $newdate = $expirationdate;
2748  $protecti=0; //$protecti is to avoid infinite loop
2749  while ($newdate < $enddatetoscan && $protecti < 1000) {
2750  $newdate = dol_time_plus_duree($newdate, $duration_value, $duration_unit);
2751  $protecti++;
2752  }
2753 
2754  if ($protecti < 1000) { // If not, there is a pb
2755  // We will update the end of date of contrat, so first we refresh contract data
2756  dol_syslog("We will update the end of date of contract with newdate = ".dol_print_date($newdate, 'dayhourrfc'));
2757 
2758  $this->db->begin();
2759 
2760  $errorforlocaltransaction = 0;
2761 
2762  $label = 'Renewal of contrat '.$object->ref.' line '.$obj->lid;
2763  $comment = 'Renew date of contract '.$object->ref.' line '.$obj->lid.' by doAutoRenewContracts';
2764 
2765  $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."contratdet SET date_fin_validite = '".$this->db->idate($newdate)."'";
2766  $sqlupdate.= ' WHERE rowid = '.((int) $obj->lid);
2767  $resqlupdate = $this->db->query($sqlupdate);
2768  if ($resqlupdate) {
2769  $contractlineprocessed[$obj->lid]=$object->ref;
2770 
2771  $actioncode = 'RENEW_CONTRACT';
2772  $now = dol_now();
2773 
2774  // Create an event
2775  $actioncomm = new ActionComm($this->db);
2776  $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2777  $actioncomm->code = 'AC_'.$actioncode;
2778  $actioncomm->label = $label;
2779  $actioncomm->datep = $now;
2780  $actioncomm->datef = $now;
2781  $actioncomm->percentage = -1; // Not applicable
2782  $actioncomm->socid = $object->thirdparty->id;
2783  $actioncomm->authorid = $user->id; // User saving action
2784  $actioncomm->userownerid = $user->id; // Owner of action
2785  $actioncomm->fk_element = $object->id;
2786  $actioncomm->elementtype = 'contract';
2787  $actioncomm->note_private = $comment;
2788 
2789  $ret = $actioncomm->create($user); // User creating action
2790  } else {
2791  $contracterror[$object->id]=$object->ref;
2792 
2793  $error++;
2794  $errorforlocaltransaction++;
2795  $this->error = $this->db->lasterror();
2796  }
2797 
2798  if (! $errorforlocaltransaction) {
2799  $this->db->commit();
2800  } else {
2801  $this->db->rollback();
2802  }
2803  } else {
2804  $error++;
2805  $this->error = "Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate." enddatetoscan=".$enddatetoscan." duration_value=".$duration_value." duration_unit=".$duration_value;
2806  dol_syslog($this->error, LOG_ERR);
2807  }
2808  }
2809  }
2810  $i++;
2811  }
2812  } else {
2813  $error++;
2814  $this->error = $this->db->lasterror();
2815  }
2816 
2817  $this->output .= count($contractlineprocessed).' contract line(s) with end date before '.dol_print_date($enddatetoscan, 'day').' were renewed'.(count($contractlineprocessed)>0 ? ' : '.join(',', $contractlineprocessed) : '');
2818 
2819  return ($error ? 1: 0);
2820  }
2821 
2829  public function getKanbanView($option = '', $arraydata = null)
2830  {
2831  global $langs;
2832  $return = '<div class="box-flex-item box-flex-grow-zero">';
2833  $return .= '<div class="info-box info-box-sm">';
2834  $return .= '<span class="info-box-icon bg-infobox-action">';
2835  $return .= img_picto('', $this->picto);
2836  //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2837  $return .= '</span>';
2838  $return .= '<div class="info-box-content">';
2839  $return .= '<span class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2840  if (property_exists($this, 'societe')) {
2841  $return .= '<br><span class="info-box-label ">'.$this->societe.'</span>';
2842  }
2843  if (property_exists($this, 'date_contrat')) {
2844  $return .= '<br><span class="opacitymedium">'.$langs->trans("DateContract").' : </span><span class="info-box-label">'.dol_print_date($this->date_contrat, 'day').'</span>';
2845  }
2846  if (method_exists($this, 'getLibStatut')) {
2847  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(5).'</div>';
2848  }
2849  $return .= '</div>';
2850  $return .= '</div>';
2851  $return .= '</div>';
2852  return $return;
2853  }
2854 }
2855 
2856 
2861 {
2865  public $element = 'contratdet';
2866 
2870  public $table_element = 'contratdet';
2871 
2876  public $element_for_permission = 'contrat';
2877 
2881  public $id;
2882 
2886  public $ref;
2887 
2888  public $tms;
2889 
2893  public $fk_contrat;
2894 
2898  public $fk_product;
2899 
2900  public $statut; // 0 inactive, 4 active, 5 closed
2901  public $type; // 0 for product, 1 for service
2902 
2907  public $label;
2908 
2913  public $libelle;
2914 
2918  public $description;
2919 
2920  public $product_type; // 0 for product, 1 for service
2921  public $product_ref;
2922  public $product_label;
2923 
2924  public $date_commande;
2925 
2926  public $date_start; // date start planned
2927  public $date_start_real; // date start real
2928  public $date_end; // date end planned
2929  public $date_end_real; // date end real
2930 
2931  public $tva_tx;
2932  public $vat_src_code;
2933  public $localtax1_tx;
2934  public $localtax2_tx;
2935  public $localtax1_type; // Local tax 1 type
2936  public $localtax2_type; // Local tax 2 type
2937  public $qty;
2938  public $remise_percent;
2939  public $remise;
2940 
2944  public $fk_remise_except;
2945 
2946  public $subprice; // Unit price HT
2947 
2953  public $price;
2954 
2955  public $price_ht;
2956 
2957  public $total_ht;
2958  public $total_tva;
2959  public $total_localtax1;
2960  public $total_localtax2;
2961  public $total_ttc;
2962 
2966  public $fk_fournprice;
2967 
2968  public $pa_ht;
2969 
2970  public $info_bits;
2971 
2975  public $fk_user_author;
2976 
2980  public $fk_user_ouverture;
2981 
2985  public $fk_user_cloture;
2986 
2987  public $commentaire;
2988 
2989 
2993  public $rang = 0;
2994 
2995 
2996  const STATUS_INITIAL = 0;
2997  const STATUS_OPEN = 4;
2998  const STATUS_CLOSED = 5;
2999 
3000 
3001  // BEGIN MODULEBUILDER PROPERTIES
3005  public $fields = array(
3006  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
3007  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>30, 'index'=>1),
3008  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>35),
3009  'qty' =>array('type'=>'integer', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>35, 'isameasure'=>1),
3010  'total_ht' =>array('type'=>'integer', 'label'=>'AmountHT', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>36, 'isameasure'=>1),
3011  'total_tva' =>array('type'=>'integer', 'label'=>'AmountVAT', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>37, 'isameasure'=>1),
3012  'total_ttc' =>array('type'=>'integer', 'label'=>'AmountTTC', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>38, 'isameasure'=>1),
3013  //'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
3014  //'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
3015  'fk_contrat' =>array('type'=>'integer:Contrat:contrat/class/contrat.class.php', 'label'=>'Contract', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
3016  'fk_product' =>array('type'=>'integer:Product:product/class/product.class.php:1', 'label'=>'Product', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
3017  //'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
3018  'note_private' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>105),
3019  'note_public' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
3020  //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
3021  //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
3022  //'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
3023  'fk_user_ouverture' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserStartingService', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>135),
3024  'fk_user_cloture' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserClosingService', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>135),
3025  'statut' =>array('type'=>'smallint(6)', 'label'=>'Statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500, 'arrayofkeyval'=>array(0=>'Draft', 4=>'Open', 5=>'Closed')),
3026  'rang' =>array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'position'=>500, 'default' =>0)
3027  );
3028  // END MODULEBUILDER PROPERTIES
3029 
3030 
3036  public function __construct($db)
3037  {
3038  $this->db = $db;
3039  }
3040 
3041 
3048  public function getLibStatut($mode)
3049  {
3050  return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1));
3051  }
3052 
3053  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3063  public static function LibStatut($status, $mode, $expired = -1, $moreatt = '')
3064  {
3065  // phpcs:enable
3066  global $langs;
3067  $langs->load("contracts");
3068 
3069  if ($status == self::STATUS_INITIAL) {
3070  $labelStatus = $langs->transnoentities("ServiceStatusInitial");
3071  $labelStatusShort = $langs->transnoentities("ServiceStatusInitial");
3072  } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3073  $labelStatus = $langs->transnoentities("ServiceStatusRunning");
3074  $labelStatusShort = $langs->transnoentities("ServiceStatusRunning");
3075  } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3076  $labelStatus = $langs->transnoentities("ServiceStatusNotLate");
3077  $labelStatusShort = $langs->transnoentities("ServiceStatusNotLateShort");
3078  } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3079  $labelStatus = $langs->transnoentities("ServiceStatusLate");
3080  $labelStatusShort = $langs->transnoentities("ServiceStatusLateShort");
3081  } elseif ($status == self::STATUS_CLOSED) {
3082  $labelStatus = $langs->transnoentities("ServiceStatusClosed");
3083  $labelStatusShort = $langs->transnoentities("ServiceStatusClosed");
3084  }
3085 
3086  $statusType = 'status'.$status;
3087  if ($status == self::STATUS_OPEN && $expired == 1) {
3088  $statusType = 'status1';
3089  }
3090  if ($status == self::STATUS_CLOSED) {
3091  $statusType = 'status6';
3092  }
3093 
3094  $params = array(); $reg = array();
3095  if (preg_match('/class="(.*)"/', $moreatt, $reg)) {
3096  $params = array('badgeParams'=>array('css' => $reg[1]));
3097  }
3098  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode, '', $params);
3099  }
3100 
3107  public function getTooltipContentArray($params)
3108  {
3109  global $conf, $langs, $user;
3110 
3111  $datas = [];
3112  $datas['label'] = $langs->trans("ShowContractOfService").': '.$this->label;
3113  if (empty($datas['label'])) {
3114  $datas['label'] = $this->description;
3115  }
3116 
3117  return $datas;
3118  }
3119 
3127  public function getNomUrl($withpicto = 0, $maxlength = 0)
3128  {
3129  global $langs;
3130 
3131  $result = '';
3132  $label = $langs->trans("ShowContractOfService").': '.$this->label;
3133  if (empty($label)) {
3134  $label = $this->description;
3135  }
3136  $classfortooltip = 'classfortooltip';
3137  $dataparams = '';
3138  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
3139  $params = [
3140  'id' => $this->fk_contrat,
3141  'objecttype' => $this->element,
3142  ];
3143  $classfortooltip = 'classforajaxtooltip';
3144  $dataparams = ' data-params='.json_encode($params);
3145  // $label = $langs->trans('Loading');
3146  }
3147  $link = '<a href="'.DOL_URL_ROOT.'/contrat/card.php?id='.$this->fk_contrat;
3148  $link = '"'.$dataparams.' title="'.dol_escape_htmltag($label, 1).'" class="'.$classfortooltip.'">';
3149  $linkend = '</a>';
3150 
3151  $picto = 'service';
3152  if ($this->type == 0) {
3153  $picto = 'product';
3154  }
3155 
3156  if ($withpicto) {
3157  $result .= ($link.img_object($label, $picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
3158  }
3159  if ($withpicto && $withpicto != 2) {
3160  $result .= ' ';
3161  }
3162  if ($withpicto != 2) {
3163  $result .= $link.($this->product_ref ? $this->product_ref.' ' : '').($this->label ? $this->label : $this->description).$linkend;
3164  }
3165  return $result;
3166  }
3167 
3175  public function fetch($id, $ref = '')
3176  {
3177  // Check parameters
3178  if (empty($id) && empty($ref)) {
3179  return -1;
3180  }
3181 
3182  $sql = "SELECT";
3183  $sql .= " t.rowid,";
3184  $sql .= " t.tms,";
3185  $sql .= " t.fk_contrat,";
3186  $sql .= " t.fk_product,";
3187  $sql .= " t.statut,";
3188  $sql .= " t.label,"; // This field is not used. Only label of product
3189  $sql .= " p.ref as product_ref,";
3190  $sql .= " p.label as product_label,";
3191  $sql .= " p.description as product_desc,";
3192  $sql .= " p.fk_product_type as product_type,";
3193  $sql .= " t.description,";
3194  $sql .= " t.date_commande,";
3195  $sql .= " t.date_ouverture_prevue as date_start,";
3196  $sql .= " t.date_ouverture as date_start_real,";
3197  $sql .= " t.date_fin_validite as date_end,";
3198  $sql .= " t.date_cloture as date_end_real,";
3199  $sql .= " t.tva_tx,";
3200  $sql .= " t.vat_src_code,";
3201  $sql .= " t.localtax1_tx,";
3202  $sql .= " t.localtax2_tx,";
3203  $sql .= " t.localtax1_type,";
3204  $sql .= " t.localtax2_type,";
3205  $sql .= " t.qty,";
3206  $sql .= " t.remise_percent,";
3207  $sql .= " t.remise,";
3208  $sql .= " t.fk_remise_except,";
3209  $sql .= " t.subprice,";
3210  $sql .= " t.price_ht,";
3211  $sql .= " t.total_ht,";
3212  $sql .= " t.total_tva,";
3213  $sql .= " t.total_localtax1,";
3214  $sql .= " t.total_localtax2,";
3215  $sql .= " t.total_ttc,";
3216  $sql .= " t.fk_product_fournisseur_price as fk_fournprice,";
3217  $sql .= " t.buy_price_ht as pa_ht,";
3218  $sql .= " t.info_bits,";
3219  $sql .= " t.fk_user_author,";
3220  $sql .= " t.fk_user_ouverture,";
3221  $sql .= " t.fk_user_cloture,";
3222  $sql .= " t.commentaire,";
3223  $sql .= " t.fk_unit,";
3224  $sql .= " t.rang";
3225  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as t LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = t.fk_product";
3226  if ($id) {
3227  $sql .= " WHERE t.rowid = ".((int) $id);
3228  }
3229  if ($ref) {
3230  $sql .= " WHERE t.rowid = '".$this->db->escape($ref)."'";
3231  }
3232 
3233  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
3234  $resql = $this->db->query($sql);
3235  if ($resql) {
3236  if ($this->db->num_rows($resql)) {
3237  $obj = $this->db->fetch_object($resql);
3238 
3239  $this->id = $obj->rowid;
3240  $this->ref = $obj->rowid;
3241 
3242  $this->tms = $this->db->jdate($obj->tms);
3243  $this->fk_contrat = $obj->fk_contrat;
3244  $this->fk_product = $obj->fk_product;
3245  $this->statut = $obj->statut;
3246  $this->product_ref = $obj->product_ref;
3247  $this->product_label = $obj->product_label;
3248  $this->product_description = $obj->product_description;
3249  $this->product_type = $obj->product_type;
3250  $this->label = $obj->label; // deprecated. We do not use this field. Only ref and label of product, and description of contract line
3251  $this->description = $obj->description;
3252  $this->date_commande = $this->db->jdate($obj->date_commande);
3253 
3254  $this->date_start = $this->db->jdate($obj->date_start);
3255  $this->date_start_real = $this->db->jdate($obj->date_start_real);
3256  $this->date_end = $this->db->jdate($obj->date_end);
3257  $this->date_end_real = $this->db->jdate($obj->date_end_real);
3258  // For backward compatibility
3259  //$this->date_ouverture_prevue = $this->db->jdate($obj->date_ouverture_prevue);
3260  //$this->date_ouverture = $this->db->jdate($obj->date_ouverture);
3261  //$this->date_fin_validite = $this->db->jdate($obj->date_fin_validite);
3262  //$this->date_cloture = $this->db->jdate($obj->date_cloture);
3263 
3264  $this->tva_tx = $obj->tva_tx;
3265  $this->vat_src_code = $obj->vat_src_code;
3266  $this->localtax1_tx = $obj->localtax1_tx;
3267  $this->localtax2_tx = $obj->localtax2_tx;
3268  $this->localtax1_type = $obj->localtax1_type;
3269  $this->localtax2_type = $obj->localtax2_type;
3270  $this->qty = $obj->qty;
3271  $this->remise_percent = $obj->remise_percent;
3272  $this->fk_remise_except = $obj->fk_remise_except;
3273  $this->subprice = $obj->subprice;
3274  $this->price_ht = $obj->price_ht;
3275  $this->total_ht = $obj->total_ht;
3276  $this->total_tva = $obj->total_tva;
3277  $this->total_localtax1 = $obj->total_localtax1;
3278  $this->total_localtax2 = $obj->total_localtax2;
3279  $this->total_ttc = $obj->total_ttc;
3280  $this->info_bits = $obj->info_bits;
3281  $this->fk_user_author = $obj->fk_user_author;
3282  $this->fk_user_ouverture = $obj->fk_user_ouverture;
3283  $this->fk_user_cloture = $obj->fk_user_cloture;
3284  $this->commentaire = $obj->commentaire;
3285  $this->fk_fournprice = $obj->fk_fournprice;
3286 
3287  $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3288  $this->pa_ht = $marginInfos[0];
3289  $this->fk_unit = $obj->fk_unit;
3290 
3291  $this->rang = $obj->rang;
3292 
3293  $this->fetch_optionals();
3294  }
3295 
3296  $this->db->free($resql);
3297 
3298  return 1;
3299  } else {
3300  $this->error = "Error ".$this->db->lasterror();
3301  return -1;
3302  }
3303  }
3304 
3305 
3313  public function update($user, $notrigger = 0)
3314  {
3315  global $conf, $langs, $mysoc;
3316 
3317  $error = 0;
3318 
3319  // Clean parameters
3320  $this->fk_contrat = (int) $this->fk_contrat;
3321  $this->fk_product = (int) $this->fk_product;
3322  $this->statut = (int) $this->statut;
3323  $this->label = trim($this->label);
3324  $this->description = trim($this->description);
3325  $this->vat_src_code = trim($this->vat_src_code);
3326  $this->tva_tx = trim($this->tva_tx);
3327  $this->localtax1_tx = trim($this->localtax1_tx);
3328  $this->localtax2_tx = trim($this->localtax2_tx);
3329  $this->qty = trim($this->qty);
3330  $this->remise_percent = trim($this->remise_percent);
3331  $this->fk_remise_except = (int) $this->fk_remise_except;
3332  $this->subprice = price2num($this->subprice);
3333  $this->price_ht = price2num($this->price_ht);
3334  $this->total_ht = trim($this->total_ht);
3335  $this->total_tva = trim($this->total_tva);
3336  $this->total_localtax1 = trim($this->total_localtax1);
3337  $this->total_localtax2 = trim($this->total_localtax2);
3338  $this->total_ttc = trim($this->total_ttc);
3339  $this->info_bits = trim($this->info_bits);
3340  $this->fk_user_author = (int) $this->fk_user_author;
3341  $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3342  $this->fk_user_cloture = (int) $this->fk_user_cloture;
3343  $this->commentaire = trim($this->commentaire);
3344  $this->rang = (int) $this->rang;
3345  //if (empty($this->subprice)) $this->subprice = 0;
3346  if (empty($this->price_ht)) {
3347  $this->price_ht = 0;
3348  }
3349  if (empty($this->total_ht)) {
3350  $this->total_ht = 0;
3351  }
3352  if (empty($this->total_tva)) {
3353  $this->total_tva = 0;
3354  }
3355  if (empty($this->total_ttc)) {
3356  $this->total_ttc = 0;
3357  }
3358  if (empty($this->localtax1_tx)) {
3359  $this->localtax1_tx = 0;
3360  }
3361  if (empty($this->localtax2_tx)) {
3362  $this->localtax2_tx = 0;
3363  }
3364  if (empty($this->remise_percent)) {
3365  $this->remise_percent = 0;
3366  }
3367  // For backward compatibility
3368  if (empty($this->date_start)) {
3369  $this->date_start = $this->date_start;
3370  }
3371  if (empty($this->date_start_real)) {
3372  $this->date_start_real = $this->date_start_real;
3373  }
3374  if (empty($this->date_end)) {
3375  $this->date_end = $this->date_end;
3376  }
3377  if (empty($this->date_end_real)) {
3378  $this->date_end_real = $this->date_end_real;
3379  }
3380 
3381  // Calcul du total TTC et de la TVA pour la ligne a partir de
3382  // qty, pu, remise_percent et txtva
3383  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
3384  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
3385  $localtaxes_type = getLocalTaxesFromRate($this->txtva, 0, $this->thirdparty, $mysoc);
3386 
3387  $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);
3388  $this->total_ht = $tabprice[0];
3389  $this->total_tva = $tabprice[1];
3390  $this->total_ttc = $tabprice[2];
3391  $this->total_localtax1 = $tabprice[9];
3392  $this->total_localtax2 = $tabprice[10];
3393 
3394  if (empty($this->pa_ht)) {
3395  $this->pa_ht = 0;
3396  }
3397 
3398  // if buy price not defined, define buyprice as configured in margin admin
3399  if ($this->pa_ht == 0) {
3400  if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
3401  return $result;
3402  } else {
3403  $this->pa_ht = $result;
3404  }
3405  }
3406 
3407  // $this->oldcopy should have been set by the caller of update (here properties were already modified)
3408  if (empty($this->oldcopy)) {
3409  $this->oldcopy = dol_clone($this);
3410  }
3411 
3412  $this->db->begin();
3413 
3414  // Update request
3415  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3416  $sql .= " fk_contrat = ".((int) $this->fk_contrat).",";
3417  $sql .= " fk_product = ".($this->fk_product ? ((int) $this->fk_product) : 'null').",";
3418  $sql .= " statut = ".((int) $this->statut).",";
3419  $sql .= " label = '".$this->db->escape($this->label)."',";
3420  $sql .= " description = '".$this->db->escape($this->description)."',";
3421  $sql .= " date_commande = ".($this->date_commande != '' ? "'".$this->db->idate($this->date_commande)."'" : "null").",";
3422  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3423  $sql .= " date_ouverture = ".($this->date_start_real != '' ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3424  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3425  $sql .= " date_cloture = ".($this->date_end_real != '' ? "'".$this->db->idate($this->date_end_real)."'" : "null").",";
3426  $sql .= " vat_src_code = '".$this->db->escape($this->vat_src_code)."',";
3427  $sql .= " tva_tx = ".price2num($this->tva_tx).",";
3428  $sql .= " localtax1_tx = ".price2num($this->localtax1_tx).",";
3429  $sql .= " localtax2_tx = ".price2num($this->localtax2_tx).",";
3430  $sql .= " qty = ".price2num($this->qty).",";
3431  $sql .= " remise_percent = ".price2num($this->remise_percent).",";
3432  $sql .= " remise = ".($this->remise ?price2num($this->remise) : "null").",";
3433  $sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except : "null").",";
3434  $sql .= " subprice = ".($this->subprice != '' ? $this->subprice : "null").",";
3435  $sql .= " price_ht = ".($this->price_ht != '' ? $this->price_ht : "null").",";
3436  $sql .= " total_ht = ".$this->total_ht.",";
3437  $sql .= " total_tva = ".$this->total_tva.",";
3438  $sql .= " total_localtax1 = ".$this->total_localtax1.",";
3439  $sql .= " total_localtax2 = ".$this->total_localtax2.",";
3440  $sql .= " total_ttc = ".$this->total_ttc.",";
3441  $sql .= " fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "NULL").",";
3442  $sql .= " buy_price_ht = '".price2num($this->pa_ht)."',";
3443  $sql .= " info_bits = '".$this->db->escape($this->info_bits)."',";
3444  $sql .= " fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author : "NULL").",";
3445  $sql .= " fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture : "NULL").",";
3446  $sql .= " fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture : "NULL").",";
3447  $sql .= " commentaire = '".$this->db->escape($this->commentaire)."',";
3448  $sql .= " fk_unit = ".(!$this->fk_unit ? 'NULL' : $this->fk_unit).",";
3449  $sql .= " rang = ".(empty($this->rang) ? '0' : (int) $this->rang);
3450  $sql .= " WHERE rowid = ".((int) $this->id);
3451 
3452  dol_syslog(get_class($this)."::update", LOG_DEBUG);
3453  $resql = $this->db->query($sql);
3454  if (!$resql) {
3455  $this->error = "Error ".$this->db->lasterror();
3456  $error++;
3457  }
3458 
3459  if (!$error) { // For avoid conflicts if trigger used
3460  $result = $this->insertExtraFields();
3461  if ($result < 0) {
3462  $error++;
3463  }
3464  }
3465 
3466  // If we change a planned date (start or end) of one contract line, sync dates for all other services too
3467  if (!$error && !empty($conf->global->CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES)) {
3468  dol_syslog(get_class($this)."::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3469 
3470  if ($this->date_start != $this->oldcopy->date_start) {
3471  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3472  $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null");
3473  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3474 
3475  $resql = $this->db->query($sql);
3476  if (!$resql) {
3477  $error++;
3478  $this->error = "Error ".$this->db->lasterror();
3479  }
3480  }
3481  if ($this->date_end != $this->oldcopy->date_end) {
3482  $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
3483  $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null");
3484  $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
3485 
3486  $resql = $this->db->query($sql);
3487  if (!$resql) {
3488  $error++;
3489  $this->error = "Error ".$this->db->lasterror();
3490  }
3491  }
3492  }
3493 
3494  if (!$error && !$notrigger) {
3495  // Call trigger
3496  $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
3497  if ($result < 0) {
3498  $error++;
3499  $this->db->rollback();
3500  }
3501  // End call triggers
3502  }
3503 
3504  if (!$error) {
3505  $this->db->commit();
3506  return 1;
3507  } else {
3508  $this->db->rollback();
3509  $this->errors[] = $this->error;
3510  return -1;
3511  }
3512  }
3513 
3514 
3515  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3522  public function update_total()
3523  {
3524  // phpcs:enable
3525  $this->db->begin();
3526 
3527  // Mise a jour ligne en base
3528  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
3529  $sql .= " total_ht=".price2num($this->total_ht, 'MT');
3530  $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
3531  $sql .= ",total_localtax1=".price2num($this->total_localtax1, 'MT');
3532  $sql .= ",total_localtax2=".price2num($this->total_localtax2, 'MT');
3533  $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
3534  $sql .= " WHERE rowid = ".((int) $this->id);
3535 
3536  dol_syslog(get_class($this)."::update_total", LOG_DEBUG);
3537 
3538  $resql = $this->db->query($sql);
3539  if ($resql) {
3540  $this->db->commit();
3541  return 1;
3542  } else {
3543  $this->error = $this->db->error();
3544  $this->db->rollback();
3545  return -2;
3546  }
3547  }
3548 
3549 
3556  public function insert($notrigger = 0)
3557  {
3558  global $conf, $user;
3559 
3560  $error = 0;
3561 
3562  // Insertion dans la base
3563  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
3564  $sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3565  $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3566  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3567  $sql .= " info_bits,";
3568  $sql .= " rang,";
3569  $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3570  if ($this->date_start > 0) {
3571  $sql .= ",date_ouverture_prevue";
3572  }
3573  if ($this->date_end > 0) {
3574  $sql .= ",date_fin_validite";
3575  }
3576  $sql .= ") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->description)."',";
3577  $sql .= ($this->fk_product > 0 ? $this->fk_product : "null").",";
3578  $sql .= " '".$this->db->escape($this->qty)."',";
3579  $sql .= " '".$this->db->escape($this->vat_src_code)."',";
3580  $sql .= " '".$this->db->escape($this->tva_tx)."',";
3581  $sql .= " '".$this->db->escape($this->localtax1_tx)."',";
3582  $sql .= " '".$this->db->escape($this->localtax2_tx)."',";
3583  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3584  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3585  $sql .= " ".price2num($this->remise_percent).",".price2num($this->subprice).",";
3586  $sql .= " ".price2num($this->total_ht).",".price2num($this->total_tva).",".price2num($this->total_localtax1).",".price2num($this->total_localtax2).",".price2num($this->total_ttc).",";
3587  $sql .= " '".$this->db->escape($this->info_bits)."',";
3588  $sql .= " ".(empty($this->rang) ? '0' : (int) $this->rang).",";
3589  $sql .= " ".price2num($this->price_ht).",".price2num($this->remise).",";
3590  if ($this->fk_fournprice > 0) {
3591  $sql .= ' '.((int) $this->fk_fournprice).',';
3592  } else {
3593  $sql .= ' null,';
3594  }
3595  if ($this->pa_ht > 0) {
3596  $sql .= ' '.((float) price2num($this->pa_ht));
3597  } else {
3598  $sql .= ' null';
3599  }
3600  if ($this->date_start > 0) {
3601  $sql .= ",'".$this->db->idate($this->date_start)."'";
3602  }
3603  if ($this->date_end > 0) {
3604  $sql .= ",'".$this->db->idate($this->date_end)."'";
3605  }
3606  $sql .= ")";
3607 
3608  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3609 
3610  $resql = $this->db->query($sql);
3611  if ($resql) {
3612  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'contratdet');
3613 
3614  // Insert of extrafields
3615  if (!$error) {
3616  $result = $this->insertExtraFields();
3617  if ($result < 0) {
3618  $this->db->rollback();
3619  return -1;
3620  }
3621  }
3622 
3623  if (!$notrigger) {
3624  // Call trigger
3625  $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
3626  if ($result < 0) {
3627  $this->db->rollback();
3628  return -1;
3629  }
3630  // End call triggers
3631  }
3632 
3633  $this->db->commit();
3634  return 1;
3635  } else {
3636  $this->db->rollback();
3637  $this->error = $this->db->error()." sql=".$sql;
3638  return -1;
3639  }
3640  }
3641 
3642  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3652  public function active_line($user, $date, $date_end = '', $comment = '')
3653  {
3654  // phpcs:enable
3655  global $langs, $conf;
3656 
3657  $error = 0;
3658 
3659  $this->db->begin();
3660 
3661  $this->statut = ContratLigne::STATUS_OPEN;
3662  $this->date_start_real = $date;
3663  $this->date_end = $date_end;
3664  $this->fk_user_ouverture = $user->id;
3665  $this->date_end_real = null;
3666  $this->commentaire = $comment;
3667 
3668  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) $this->statut).",";
3669  $sql .= " date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
3670  if ($date_end >= 0) {
3671  $sql .= " date_fin_validite = ".(dol_strlen($this->date_end) != 0 ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3672  }
3673  $sql .= " fk_user_ouverture = ".((int) $this->fk_user_ouverture).",";
3674  $sql .= " date_cloture = null,";
3675  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3676  $sql .= " WHERE rowid = ".((int) $this->id)." AND (statut = ".ContratLigne::STATUS_INITIAL." OR statut = ".ContratLigne::STATUS_CLOSED.")";
3677 
3678  dol_syslog(get_class($this)."::active_line", LOG_DEBUG);
3679  $resql = $this->db->query($sql);
3680  if ($resql) {
3681  // Call trigger
3682  $result = $this->call_trigger('LINECONTRACT_ACTIVATE', $user);
3683  if ($result < 0) {
3684  $error++;
3685  }
3686  // End call triggers
3687 
3688  if (!$error) {
3689  $this->db->commit();
3690  return 1;
3691  } else {
3692  $this->db->rollback();
3693  return -1;
3694  }
3695  } else {
3696  $this->error = $this->db->lasterror();
3697  $this->db->rollback();
3698  return -1;
3699  }
3700  }
3701 
3702  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3712  public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)
3713  {
3714  // phpcs:enable
3715  global $langs, $conf;
3716 
3717  // Update object
3718  $this->date_cloture = $date_end_real;
3719  $this->date_end_real = $date_end_real;
3720  $this->fk_user_cloture = $user->id;
3721  $this->commentaire = $comment;
3722 
3723  $error = 0;
3724 
3725  // statut actif : 4
3726 
3727  $this->db->begin();
3728 
3729  $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).",";
3730  $sql .= " date_cloture = '".$this->db->idate($date_end_real)."',";
3731  $sql .= " fk_user_cloture = ".((int) $user->id).",";
3732  $sql .= " commentaire = '".$this->db->escape($comment)."'";
3733  $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = ".((int) ContratLigne::STATUS_OPEN);
3734 
3735  $resql = $this->db->query($sql);
3736  if ($resql) {
3737  if (!$notrigger) {
3738  // Call trigger
3739  $result = $this->call_trigger('LINECONTRACT_CLOSE', $user);
3740  if ($result < 0) {
3741  $error++;
3742  $this->db->rollback();
3743  return -1;
3744  }
3745  // End call triggers
3746  }
3747 
3748  $this->db->commit();
3749  return 1;
3750  } else {
3751  $this->error = $this->db->lasterror();
3752  $this->db->rollback();
3753  return -1;
3754  }
3755  }
3756 }
Facture\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: facture.class.php:407
Contrat\getListOfContracts
getListOfContracts($option='all', $status=[], $product_categories=[], $line_status=[])
Return list of other contracts for the same company than current contract.
Definition: contrat.class.php:2184
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:50
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6275
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1504
CommonObject\getIdContact
getIdContact($source, $code, $status=0)
Return id of contacts for a source and a contact code.
Definition: commonobject.class.php:1692
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1236
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
Expedition
Class to manage shipments.
Definition: expedition.class.php:52
Contrat\deleteline
deleteline($idline, User $user)
Delete a contract line.
Definition: contrat.class.php:1838
ActionComm
Class to manage agenda events (actions)
Definition: actioncomm.class.php:38
ContratLigne\close_line
close_line($user, $date_end_real, $comment='', $notrigger=0)
Close a contract line.
Definition: contrat.class.php:3712
dol_delete_dir_recursive
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1412
ContratLigne\getTooltipContentArray
getTooltipContentArray($params)
getTooltipContentArray
Definition: contrat.class.php:3107
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Contrat\getNextNumRef
getNextNumRef($soc)
Return next contract ref.
Definition: contrat.class.php:275
Contrat\$table_ref_field
$table_ref_field
{}
Definition: contrat.class.php:90
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4994
CommonObject\setErrorsFromObject
setErrorsFromObject($object)
setErrorsFromObject
Definition: commonobject.class.php:679
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1043
ContratLigne\fetch
fetch($id, $ref='')
Load object in memory from database.
Definition: contrat.class.php:3175
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1072
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:61
Contrat\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: contrat.class.php:2387
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4275
Contrat\update_statut
update_statut($user)
Update statut of contract according to services.
Definition: contrat.class.php:1898
ContratLigne\update
update($user, $notrigger=0)
Update database for contract line.
Definition: contrat.class.php:3313
Contrat\validate
validate(User $user, $force_number='', $notrigger=0)
Validate a contract.
Definition: contrat.class.php:479
Contrat\activateAll
activateAll($user, $date_start='', $notrigger=0, $comment='', $date_end='')
Open all lines of a contract.
Definition: contrat.class.php:374
dol_clone
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
Definition: functions.lib.php:1168
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:45
ContratLigne\getNomUrl
getNomUrl($withpicto=0, $maxlength=0)
Return clicable name (with picto eventually) for ContratLigne.
Definition: contrat.class.php:3127
Contrat\getIdBillingContact
getIdBillingContact()
Return id des contacts clients de facturation.
Definition: contrat.class.php:2364
Contrat\close_line
close_line($user, $line_id, $date_end, $comment='')
Close a contract line.
Definition: contrat.class.php:351
Contrat\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: contrat.class.php:2510
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5834
Contrat\getLinesArray
getLinesArray()
Create an array of order lines.
Definition: contrat.class.php:2458
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2566
WorkboardResponse
Definition: workboardresponse.class.php:24
CommonObject\fetch_thirdparty
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
Definition: commonobject.class.php:1785
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4025
Contrat\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: contrat.class.php:2475
ContratLigne\__construct
__construct($db)
Constructor.
Definition: contrat.class.php:3036
Contrat\updateline
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=0, $fk_unit=null, $rang=0)
Mets a jour une ligne de contrat.
Definition: contrat.class.php:1669
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5460
Contrat\getNomUrl
getNomUrl($withpicto=0, $maxlength=0, $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: contrat.class.php:2033
Contrat\getIdServiceContact
getIdServiceContact()
Return id des contacts clients de prestation.
Definition: contrat.class.php:2374
calcul_price_total
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:86
Contrat\addline
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=0, $fk_unit=null, $rang=0)
Ajoute une ligne de contrat en base.
Definition: contrat.class.php:1437
Contrat\create
create($user)
Create a contract into database.
Definition: contrat.class.php:939
ContratLigne\LibStatut
static LibStatut($status, $mode, $expired=-1, $moreatt='')
Return label of a contract line status.
Definition: contrat.class.php:3063
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6229
CommonObject\delete_linked_contact
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
Definition: commonobject.class.php:1361
ContratLigne\update_total
update_total()
Mise a jour en base des champs total_xxx de ligne Used by migration process.
Definition: contrat.class.php:3522
CommonObject\defineBuyPrice
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
Definition: commonobject.class.php:8628
Contrat\doAutoRenewContracts
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...
Definition: contrat.class.php:2661
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8599
CommonObject\add_contact
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Definition: commonobject.class.php:1162
Contrat\LibStatut
LibStatut($status, $mode)
Renvoi label of a given contrat status.
Definition: contrat.class.php:1940
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
Contrat\getKanbanView
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: contrat.class.php:2829
ContratLigne\getLibStatut
getLibStatut($mode)
Return label of this contract line status.
Definition: contrat.class.php:3048
CommonObject\fetch_optionals
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Definition: commonobject.class.php:6080
ContratLigne\insert
insert($notrigger=0)
Inserts a contrat line into database.
Definition: contrat.class.php:3556
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3888
Contrat\createFromClone
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
Definition: contrat.class.php:2544
ContratLigne\active_line
active_line($user, $date, $date_end='', $comment='')
Activate a contract line.
Definition: contrat.class.php:3652
Contrat\getTooltipContentArray
getTooltipContentArray($params)
getTooltipContentArray
Definition: contrat.class.php:1991
ref
$object ref
Definition: info.php:78
Contrat
Class to manage contracts.
Definition: contrat.class.php:43
dol_time_plus_duree
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:122
Contrat\reopen
reopen($user, $notrigger=0)
Unvalidate a contract.
Definition: contrat.class.php:596
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Contrat\closeAll
closeAll(User $user, $notrigger=0, $comment='')
Close all lines of a contract.
Definition: contrat.class.php:427
Contrat\update
update($user, $notrigger=0)
Update object into database.
Definition: contrat.class.php:1297
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6189
Contrat\info
info($id)
Charge les informations d'ordre info dans l'objet contrat.
Definition: contrat.class.php:2107
Contrat\fetch_lines
fetch_lines($only_services=0, $loadalsotranslation=0)
Load lines array into this->lines.
Definition: contrat.class.php:763
Contrat\active_line
active_line($user, $line_id, $date_start, $date_end='', $comment='')
Activate a contract line.
Definition: contrat.class.php:329
Product
Class to manage products or services.
Definition: product.class.php:46
ContratLigne
Class to manage lines of contracts.
Definition: contrat.class.php:2860
getMarginInfos
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
Definition: margins.lib.php:118
Contrat\__construct
__construct($db)
Constructor.
Definition: contrat.class.php:264
Contrat\array_detail
array_detail($status=-1)
Return list of line rowid.
Definition: contrat.class.php:2146
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3865
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10767
Contrat\getLibStatut
getLibStatut($mode)
Return label of a contract status.
Definition: contrat.class.php:1927
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4361
Contrat\replaceProduct
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: contrat.class.php:2527
Contrat\fetch
fetch($id, $ref='', $ref_customer='', $ref_supplier='')
Load a contract from database.
Definition: contrat.class.php:657
Contrat\load_state_board
load_state_board()
Charge indicateurs this->nb de tableau de bord.
Definition: contrat.class.php:2324
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2947
price
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.
Definition: functions.lib.php:5708
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5864
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:96
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8570
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
Contrat\$fk_projet
$fk_projet
Definition: contrat.class.php:175
Contrat\load_board
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: contrat.class.php:2228