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