dolibarr 22.0.5
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-2025 Frédéric France <frederic.france@free.fr>
13 * Copyright (C) 2015-2018 Ferran Marcet <fmarcet@2byte.es>
14 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
15 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
37require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38require_once DOL_DOCUMENT_ROOT.'/contrat/class/contratligne.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/class/commonsignedobject.class.php';
42
49class Contrat extends CommonObject
50{
51 use CommonSignedObject;
52
56 public $element = 'contrat';
57
61 public $table_element = 'contrat';
62
66 public $table_element_line = 'contratdet';
67
71 public $fk_element = 'fk_contrat';
72
76 public $picto = 'contract';
77
82 public $restrictiononfksoc = 1;
83
87 protected $table_ref_field = 'ref';
88
93 public $ref_customer;
94
98 public $from;
99
104 public $ref_supplier;
105
110 public $entity;
111
116 public $socid;
117
123 public $fk_soc;
124
128 public $societe;
129
135 public $statut = 0;
140 public $status = 0;
141
145 public $product;
146
150 public $fk_user_author;
151
157 public $user_author_id;
158
159
164 public $user_creation;
165
169 public $user_cloture;
170
174 public $date_contrat;
175
179 public $commercial_signature_id;
183 public $fk_commercial_signature;
187 public $commercial_suivi_id;
191 public $fk_commercial_suivi;
192
198 public $fk_projet;
199
203 public $extraparams = array();
204
208 public $lines = array();
209
213 public $nbofservices;
217 public $nbofserviceswait;
221 public $nbofservicesopened;
225 public $nbofservicesexpired;
229 public $nbofservicesclosed;
230 //public $lower_planned_end_date;
231 //public $higher_planner_end_date;
232
237 protected $lines_id_index_mapper = array();
238
239
264 // BEGIN MODULEBUILDER PROPERTIES
268 public $fields = array(
269 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
270 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
271 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 20),
272 'ref_customer' => array('type' => 'varchar(50)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => -1, 'position' => 25, 'searchall' => 1),
273 'ref_supplier' => array('type' => 'varchar(50)', 'label' => 'RefSupplier', 'enabled' => 1, 'visible' => -1, 'position' => 26, 'searchall' => 1),
274 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
275 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
276 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 40),
277 'date_contrat' => array('type' => 'datetime', 'label' => 'Date contrat', 'enabled' => 1, 'visible' => -1, 'position' => 45),
278 'signed_status' => array('type' => 'smallint(6)', 'label' => 'SignedStatus', 'enabled' => 1, 'visible' => -1, 'position' => 50, 'arrayofkeyval' => array(0 => 'NoSignature', 1 => 'SignedSender', 2 => 'SignedReceiver', 3 => 'SignedReceiverOnline', 9 => 'SignedAll')),
279 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 70),
280 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 75),
281 'fk_commercial_signature' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative Signature', 'enabled' => 1, 'visible' => -1, 'position' => 80),
282 'fk_commercial_suivi' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative follower', 'enabled' => 1, 'visible' => -1, 'position' => 85),
283 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 90),
284 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105, 'searchall' => 1),
285 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110, 'searchall' => 1),
286 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 115),
287 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 120),
288 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 125),
289 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
290 'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'Last main doc', 'enabled' => 1, 'visible' => -1, 'position' => 140),
291 'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'notnull' => 1, 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed'))
292 );
293 // END MODULEBUILDER PROPERTIES
294
295 const STATUS_DRAFT = 0;
296 const STATUS_VALIDATED = 1;
297 const STATUS_CLOSED = 2;
298
304 public function __construct($db)
305 {
306 $this->db = $db;
307
308 $this->ismultientitymanaged = 1;
309 $this->isextrafieldmanaged = 1;
310 }
311
318 public function getNextNumRef($soc)
319 {
320 global $db, $langs, $conf;
321 $langs->load("contracts");
322
323 if (getDolGlobalString('CONTRACT_ADDON')) {
324 $mybool = false;
325
326 $file = getDolGlobalString('CONTRACT_ADDON') . ".php";
327 $classname = getDolGlobalString('CONTRACT_ADDON');
328
329 // Include file with class
330 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
331
332 foreach ($dirmodels as $reldir) {
333 $dir = dol_buildpath($reldir."core/modules/contract/");
334
335 // Load file with numbering class (if found)
336 $mybool = ((bool) @include_once $dir.$file) || $mybool;
337 }
338
339 if (!$mybool) {
340 dol_print_error(null, "Failed to include file ".$file);
341 return '';
342 }
343
344 $obj = new $classname();
345 '@phan-var-force ModelNumRefContracts $obj';
346 $numref = $obj->getNextValue($soc, $this);
347
348 if ($numref != "") {
349 return $numref;
350 } else {
351 $this->error = $obj->error;
352 dol_print_error($db, get_class($this)."::getNextValue ".$obj->error);
353 return "";
354 }
355 } else {
356 $langs->load("errors");
357 print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Contract"));
358 return "";
359 }
360 }
361
362 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
373 public function active_line($user, $line_id, $date_start, $date_end = '', $comment = '')
374 {
375 // phpcs:enable
376 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
377 if ($result < 0) {
378 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
379 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
380 }
381 return $result;
382 }
383
384
385 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
395 public function close_line($user, $line_id, $date_end, $comment = '')
396 {
397 // phpcs:enable
398 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
399 if ($result < 0) {
400 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
401 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
402 }
403 return $result;
404 }
405
406
418 public function activateAll($user, $date_start = '', $notrigger = 0, $comment = '', $date_end = '')
419 {
420 if (empty($date_start)) {
421 $date_start = dol_now();
422 }
423
424 $this->db->begin();
425
426 $error = 0;
427
428 // Load lines
429 $this->fetch_lines();
430
431 foreach ($this->lines as $contratline) {
432 // Open lines not already open
433 if ($contratline->statut != ContratLigne::STATUS_OPEN) {
434 $contratline->context = $this->context;
435
436 $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment); // This call trigger LINECONTRACT_ACTIVATE
437 if ($result < 0) {
438 $error++;
439 $this->error = $contratline->error;
440 $this->errors = $contratline->errors;
441 break;
442 }
443 }
444 }
445
446 if (!$error && $this->statut == 0) {
447 $result = $this->validate($user, '', $notrigger);
448 if ($result < 0) {
449 $error++;
450 }
451 }
452
453 if (!$error) {
454 $this->db->commit();
455 return 1;
456 } else {
457 $this->db->rollback();
458 return -1;
459 }
460 }
461
471 public function closeAll(User $user, $notrigger = 0, $comment = '')
472 {
473 dol_syslog("closeAll begin", LOG_DEBUG, 1);
474
475 $this->db->begin();
476
477 // Load lines
478 // TODO Should be useless if object was fetched without the noline param.
479 $this->fetch_lines();
480
481 $now = dol_now();
482
483 $error = 0;
484
485 foreach ($this->lines as $contratline) {
486 // Close lines not already closed
487 if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
488 $contratline->date_end_real = $now;
489 $contratline->date_cloture = $now; // For backward compatibility
490 $contratline->user_closing_id = $user->id;
491 $contratline->statut = ContratLigne::STATUS_CLOSED;
492
493 $result = $contratline->close_line($user, $now, $comment, $notrigger);
494
495 if ($result < 0) {
496 $error++;
497 $this->error = $contratline->error.($contratline->error ? ' ('.$this->ref.')' : '');
498 $this->errors = $contratline->errors;
499 break;
500 }
501 }
502 }
503
504 if (!$error && $this->status == 0) {
505 $result = $this->validate($user, '', $notrigger);
506 if ($result < 0) {
507 $error++;
508 }
509 }
510
511 if (!$error) {
512 $this->db->commit();
513
514 dol_syslog("closeAll end", LOG_DEBUG, -1);
515
516 return 1;
517 } else {
518 $this->db->rollback();
519
520 dol_syslog("closeAll end", LOG_DEBUG, -1);
521
522 return -1;
523 }
524 }
525
534 public function validate(User $user, $force_number = '', $notrigger = 0)
535 {
536 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
537 global $conf;
538
539 $now = dol_now();
540
541 $error = 0;
542 dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number);
543
544
545 $this->db->begin();
546
547 $this->fetch_thirdparty();
548
549 // A contract is validated so we can move thirdparty to status customer
550 if (!getDolGlobalString('CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION') && $this->thirdparty->fournisseur == 0) {
551 $result = $this->thirdparty->setAsCustomer();
552 }
553
554 // Define new ref
555 if ($force_number) {
556 $num = $force_number;
557 } elseif (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
558 $num = $this->getNextNumRef($this->thirdparty);
559 } else {
560 $num = $this->ref;
561 }
562 $this->newref = dol_sanitizeFileName($num);
563
564 if ($num) {
565 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$this->db->escape($num)."', statut = 1";
566 $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 0";
567
568 dol_syslog(get_class($this)."::validate", LOG_DEBUG);
569 $resql = $this->db->query($sql);
570 if (!$resql) {
571 dol_print_error($this->db);
572 $error++;
573 $this->error = $this->db->lasterror();
574 }
575
576 // Trigger calls
577 if (!$error && !$notrigger) {
578 // Call trigger
579 $result = $this->call_trigger('CONTRACT_VALIDATE', $user);
580 if ($result < 0) {
581 $error++;
582 }
583 // End call triggers
584 }
585
586 if (!$error) {
587 $this->oldref = $this->ref;
588
589 // Rename directory if dir was a temporary ref
590 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
591 // Now we rename also files into index
592 $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)."'";
593 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
594 $resql = $this->db->query($sql);
595 if (!$resql) {
596 $error++;
597 $this->error = $this->db->lasterror();
598 }
599 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'contract/".$this->db->escape($this->newref)."'";
600 $sql .= " WHERE filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
601 $resql = $this->db->query($sql);
602 if (!$resql) {
603 $error++;
604 $this->error = $this->db->lasterror();
605 }
606
607 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
608 $oldref = dol_sanitizeFileName($this->ref);
609 $newref = dol_sanitizeFileName($num);
610 $dirsource = $conf->contract->dir_output.'/'.$oldref;
611 $dirdest = $conf->contract->dir_output.'/'.$newref;
612 if (!$error && file_exists($dirsource)) {
613 dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
614
615 if (@rename($dirsource, $dirdest)) {
616 dol_syslog("Rename ok");
617 // Rename docs starting with $oldref with $newref
618 $listoffiles = dol_dir_list($conf->contract->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
619 foreach ($listoffiles as $fileentry) {
620 $dirsource = $fileentry['name'];
621 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
622 $dirsource = $fileentry['path'].'/'.$dirsource;
623 $dirdest = $fileentry['path'].'/'.$dirdest;
624 @rename($dirsource, $dirdest);
625 }
626 }
627 }
628 }
629 }
630
631 // Set new ref and define current status
632 if (!$error) {
633 $this->ref = $num;
634 $this->status = self::STATUS_VALIDATED;
635 $this->statut = self::STATUS_VALIDATED; // deprecated
636 $this->date_validation = $now;
637 }
638 } else {
639 $error++;
640 }
641
642 if (!$error) {
643 $this->db->commit();
644 return 1;
645 } else {
646 $this->db->rollback();
647 return -1;
648 }
649 }
650
658 public function reopen($user, $notrigger = 0)
659 {
660 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
661
662 $now = dol_now();
663
664 $error = 0;
665 dol_syslog(get_class($this).'::reopen user='.$user->id);
666
667 $this->db->begin();
668
669 $this->fetch_thirdparty();
670
671 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0";
672 //$sql.= ", fk_user_valid = null, date_valid = null";
673 $sql .= " WHERE rowid = ".((int) $this->id)." AND statut = 1";
674
675 dol_syslog(get_class($this)."::validate", LOG_DEBUG);
676 $resql = $this->db->query($sql);
677 if (!$resql) {
678 dol_print_error($this->db);
679 $error++;
680 $this->error = $this->db->lasterror();
681 }
682
683 // Trigger calls
684 if (!$error && !$notrigger) {
685 // Call trigger
686 $result = $this->call_trigger('CONTRACT_REOPEN', $user);
687 if ($result < 0) {
688 $error++;
689 }
690 // End call triggers
691 }
692
693 // Set new ref and define current status
694 if (!$error) {
695 $this->statut = self::STATUS_DRAFT;
696 $this->status = self::STATUS_DRAFT;
697 $this->date_validation = $now;
698 }
699
700 if (!$error) {
701 $this->db->commit();
702 return 1;
703 } else {
704 $this->db->rollback();
705 return -1;
706 }
707 }
708
720 public function fetch($id, $ref = '', $ref_customer = '', $ref_supplier = '', $noextrafields = 0, $nolines = 0)
721 {
722 $result = -10;
723
724 $sql = "SELECT rowid, statut as status, ref, fk_soc as thirdpartyid,";
725 $sql .= " ref_supplier, ref_customer,";
726 $sql .= " ref_ext,";
727 $sql .= " entity,";
728 $sql .= " signed_status,";
729 $sql .= " date_contrat as datecontrat,";
730 $sql .= " fk_user_author,";
731 $sql .= " fk_projet as fk_project,";
732 $sql .= " fk_commercial_signature, fk_commercial_suivi,";
733 $sql .= " note_private, note_public, model_pdf, last_main_doc, extraparams";
734 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
735 if (!$id) {
736 $sql .= " WHERE entity IN (".getEntity('contract').")";
737 } else {
738 $sql .= " WHERE rowid = ".(int) $id;
739 }
740 if ($ref_customer) {
741 $sql .= " AND ref_customer = '".$this->db->escape($ref_customer)."'";
742 }
743 if ($ref_supplier) {
744 $sql .= " AND ref_supplier = '".$this->db->escape($ref_supplier)."'";
745 }
746 if ($ref) {
747 $sql .= " AND ref = '".$this->db->escape($ref)."'";
748 }
749
750 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
751
752 $resql = $this->db->query($sql);
753 if ($resql) {
754 $num = $this->db->num_rows($resql);
755 if ($num > 1) {
756 $this->error = 'Fetch found several records.';
757 dol_syslog($this->error, LOG_ERR);
758 $result = -2;
759 return 0;
760 } elseif ($num) { // $num = 1
761 $obj = $this->db->fetch_object($resql);
762 if ($obj) {
763 $this->id = $obj->rowid;
764 $this->ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
765 $this->ref_customer = $obj->ref_customer;
766 $this->ref_supplier = $obj->ref_supplier;
767 $this->ref_ext = $obj->ref_ext;
768 $this->entity = $obj->entity;
769 $this->statut = $obj->status;
770 $this->status = $obj->status;
771 $this->signed_status = $obj->signed_status;
772
773 $this->date_contrat = $this->db->jdate($obj->datecontrat);
774 $this->date_creation = $this->db->jdate($obj->datecontrat);
775
776 $this->user_author_id = $obj->fk_user_author;
777
778 $this->commercial_signature_id = $obj->fk_commercial_signature;
779 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
780
781 $this->note_private = $obj->note_private;
782 $this->note_public = $obj->note_public;
783 $this->model_pdf = $obj->model_pdf;
784
785 $this->fk_projet = $obj->fk_project; // deprecated
786 $this->fk_project = $obj->fk_project;
787
788 $this->socid = $obj->thirdpartyid;
789 $this->fk_soc = $obj->thirdpartyid;
790 $this->last_main_doc = $obj->last_main_doc;
791 $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : null);
792
793 $this->db->free($resql);
794
795 // Retrieve all extrafields
796 // fetch optionals attributes and labels
797 if (empty($noextrafields)) {
798 $result = $this->fetch_optionals();
799 if ($result < 0) {
800 $this->error = $this->db->lasterror();
801 return -4;
802 }
803 }
804
805 // Lines
806 if (empty($nolines)) {
807 if ($result >= 0 && !empty($this->table_element_line)) {
808 $result = $this->fetch_lines();
809 if ($result < 0) {
810 $this->error = $this->db->lasterror();
811 return -3;
812 }
813 }
814 }
815
816 return $this->id;
817 } else {
818 dol_syslog(get_class($this)."::fetch Contract failed");
819 $this->error = "Fetch contract failed";
820 return -1;
821 }
822 } else {
823 dol_syslog(get_class($this)."::fetch Contract not found");
824 $this->error = "Contract not found";
825 return 0;
826 }
827 } else {
828 dol_syslog(get_class($this)."::fetch Error searching contract");
829 $this->error = $this->db->error();
830 return -1;
831 }
832 }
833
834 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
844 public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0)
845 {
846 // phpcs:enable
847 $this->nbofservices = 0;
848 $this->nbofserviceswait = 0;
849 $this->nbofservicesopened = 0;
850 $this->nbofservicesexpired = 0;
851 $this->nbofservicesclosed = 0;
852
853 $total_ttc = 0;
854 $total_vat = 0;
855 $total_ht = 0;
856
857 $now = dol_now();
858
859 $this->lines = array();
860 $pos = 0;
861
862 // Selects contract lines related to a product
863 $sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
864 $sql .= " d.rowid, d.fk_contrat, d.statut as status, d.description, d.subprice, 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.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
865 $sql .= " d.total_ht,";
866 $sql .= " d.total_tva,";
867 $sql .= " d.total_localtax1,";
868 $sql .= " d.total_localtax2,";
869 $sql .= " d.total_ttc,";
870 $sql .= " d.info_bits, d.fk_product,";
871 $sql .= " d.date_ouverture_prevue as date_start,";
872 $sql .= " d.date_ouverture as date_start_real,";
873 $sql .= " d.date_fin_validite as date_end,";
874 $sql .= " d.date_cloture as date_end_real,";
875 $sql .= " d.fk_user_author,";
876 $sql .= " d.fk_user_ouverture,";
877 $sql .= " d.fk_user_cloture,";
878 $sql .= " d.fk_unit,";
879 $sql .= " d.extraparams,";
880 $sql .= " d.product_type as type,";
881 $sql .= " d.rang";
882 $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
883 $sql .= " WHERE d.fk_contrat = ".((int) $this->id);
884 if ($only_services == 1) {
885 $sql .= " AND d.product_type = 1";
886 }
887 $sql .= " ORDER by d.rang ASC";
888
889 dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
890 $result = $this->db->query($sql);
891 if ($result) {
892 $num = $this->db->num_rows($result);
893 $i = 0;
894
895 while ($i < $num) {
896 $objp = $this->db->fetch_object($result);
897
898 $line = new ContratLigne($this->db);
899
900 $line->id = $objp->rowid;
901 $line->ref = $objp->rowid;
902 $line->fk_contrat = $objp->fk_contrat;
903 $line->desc = $objp->description; // Description line
904 $line->qty = $objp->qty;
905 $line->vat_src_code = $objp->vat_src_code;
906 $line->tva_tx = $objp->tva_tx;
907 $line->localtax1_tx = $objp->localtax1_tx;
908 $line->localtax2_tx = $objp->localtax2_tx;
909 $line->localtax1_type = $objp->localtax1_type;
910 $line->localtax2_type = $objp->localtax2_type;
911 $line->subprice = $objp->subprice;
912 $line->statut = $objp->status; // For backward compatibility
913 $line->status = $objp->status;
914 $line->remise_percent = $objp->remise_percent;
915 $line->total_ht = $objp->total_ht;
916 $line->total_tva = $objp->total_tva;
917 $line->total_localtax1 = $objp->total_localtax1;
918 $line->total_localtax2 = $objp->total_localtax2;
919 $line->total_ttc = $objp->total_ttc;
920 $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
921 $line->info_bits = $objp->info_bits;
922 $line->type = $objp->type;
923
924 $line->fk_fournprice = $objp->fk_fournprice;
925 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
926 $line->pa_ht = $marginInfos[0];
927
928 $line->fk_user_author = $objp->fk_user_author;
929 $line->fk_user_ouverture = $objp->fk_user_ouverture;
930 $line->fk_user_cloture = $objp->fk_user_cloture;
931 $line->fk_unit = $objp->fk_unit;
932
933 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
934
935 $line->ref = $objp->product_ref; // deprecated
936 $line->product_ref = $objp->product_ref; // Product Ref
937 $line->product_type = $objp->product_type; // Product Type
938 $line->product_desc = $objp->product_desc; // Product Description
939 $line->product_label = $objp->product_label; // Product Label
940
941 $line->description = $objp->description;
942
943 $line->date_start = $this->db->jdate($objp->date_start);
944 $line->date_start_real = $this->db->jdate($objp->date_start_real);
945 $line->date_end = $this->db->jdate($objp->date_end);
946 $line->date_end_real = $this->db->jdate($objp->date_end_real);
947 // For backward compatibility
948 //$line->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue);
949 //$line->date_ouverture = $this->db->jdate($objp->date_ouverture);
950 //$line->date_fin_validite = $this->db->jdate($objp->date_fin_validite);
951 //$line->date_cloture = $this->db->jdate($objp->date_cloture);
952 //$line->date_debut_prevue = $this->db->jdate($objp->date_ouverture_prevue);
953 //$line->date_debut_reel = $this->db->jdate($objp->date_ouverture);
954 //$line->date_fin_prevue = $this->db->jdate($objp->date_fin_validite);
955 //$line->date_fin_reel = $this->db->jdate($objp->date_cloture);
956
957 $line->rang = $objp->rang;
958
959 // Retrieve all extrafields for contract line
960 // fetch optionals attributes and labels
961 if (empty($noextrafields)) {
962 $line->fetch_optionals();
963 }
964
965 // multilangs
966 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
967 $tmpproduct = new Product($this->db);
968 $tmpproduct->fetch($objp->fk_product);
969 $tmpproduct->getMultiLangs();
970
971 $line->multilangs = $tmpproduct->multilangs;
972 }
973
974 $this->lines[$pos] = $line;
975
976 $this->lines_id_index_mapper[$line->id] = $pos;
977
978 //dol_syslog("1 ".$line->desc);
979 //dol_syslog("2 ".$line->product_desc);
980
981 if ($line->statut == ContratLigne::STATUS_INITIAL) {
982 $this->nbofserviceswait++;
983 }
984 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
985 $this->nbofservicesopened++;
986 }
987 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
988 $this->nbofservicesexpired++;
989 }
990 if ($line->statut == ContratLigne::STATUS_CLOSED) {
991 $this->nbofservicesclosed++;
992 }
993
994 // TODO Not saved into database
995 $total_ttc += $objp->total_ttc;
996 $total_vat += $objp->total_tva;
997 $total_ht += $objp->total_ht;
998
999 $i++;
1000 $pos++;
1001 }
1002 $this->db->free($result);
1003 } else {
1004 dol_syslog(get_class($this)."::Fetch Error when reading lines of contracts linked to products");
1005 return -3;
1006 }
1007
1008 // Now set the global properties on contract not stored into database.
1009 $this->nbofservices = count($this->lines);
1010 $this->total_ttc = (float) price2num($total_ttc);
1011 $this->total_tva = (float) price2num($total_vat);
1012 $this->total_ht = (float) price2num($total_ht);
1013
1014 return $this->lines;
1015 }
1016
1024 public function create($user, $notrigger = 0)
1025 {
1026 global $conf, $langs;
1027
1028 // Check parameters
1029 $paramsok = 1;
1030 if ($this->commercial_signature_id <= 0) {
1031 $langs->load("commercial");
1032 $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeSignature"));
1033 $paramsok = 0;
1034 }
1035 if ($this->commercial_suivi_id <= 0) {
1036 $langs->load("commercial");
1037 $this->error .= ($this->error ? "<br>" : '');
1038 $this->error .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeFollowUp"));
1039 $paramsok = 0;
1040 }
1041 $this->entity = setEntity($this);
1042 if (!$paramsok) {
1043 return -1;
1044 }
1045
1046
1047 $this->db->begin();
1048
1049 $now = dol_now();
1050
1051 // Insert contract
1052 $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,";
1053 $sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet,";
1054 $sql .= " ref, entity, signed_status, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
1055 $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->socid).", ".((int) $user->id);
1056 $sql .= ", ".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL");
1057 $sql .= ",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) : "NULL");
1058 $sql .= ",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) : "NULL");
1059 $sql .= ",".($this->fk_project > 0 ? ((int) $this->fk_project) : "NULL");
1060 $sql .= ", ".(dol_strlen($this->ref) <= 0 ? "null" : "'".$this->db->escape($this->ref)."'");
1061 $sql .= ", ".((int) $this->entity);
1062 $sql .= ", ".((int) $this->signed_status);
1063 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
1064 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
1065 $sql .= ", ".(!empty($this->ref_customer) ? ("'".$this->db->escape($this->ref_customer)."'") : "NULL");
1066 $sql .= ", ".(!empty($this->ref_supplier) ? ("'".$this->db->escape($this->ref_supplier)."'") : "NULL");
1067 $sql .= ", ".(!empty($this->ref_ext) ? ("'".$this->db->escape($this->ref_ext)."'") : "NULL");
1068 $sql .= ")";
1069 $resql = $this->db->query($sql);
1070
1071 if ($resql) {
1072 $error = 0;
1073
1074 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat");
1075
1076 // Load object modContract
1077 $module = (getDolGlobalString('CONTRACT_ADDON') ? $conf->global->CONTRACT_ADDON : 'mod_contract_serpis');
1078 if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') {
1079 $module = substr($module, 0, dol_strlen($module) - 4);
1080 }
1081 $result = dol_include_once('/core/modules/contract/'.$module.'.php');
1082 if ($result > 0) {
1083 $modCodeContract = new $module();
1084 '@phan-var-force ModelNumRefContracts $modCodeContrat';
1085
1086 if (!empty($modCodeContract->code_auto)) {
1087 // Force the ref to a draft value if numbering module is an automatic numbering
1088 $sql = 'UPDATE '.MAIN_DB_PREFIX."contrat SET ref='(PROV".$this->id.")' WHERE rowid=".((int) $this->id);
1089 if ($this->db->query($sql)) {
1090 if ($this->id) {
1091 $this->ref = "(PROV".$this->id.")";
1092 }
1093 }
1094 }
1095 }
1096
1097 if (!$error) {
1098 $result = $this->insertExtraFields();
1099 if ($result < 0) {
1100 $error++;
1101 }
1102 }
1103
1104 // Insert business contacts ('SALESREPSIGN','contrat')
1105 if (!$error) {
1106 $result = $this->add_contact($this->commercial_signature_id, 'SALESREPSIGN', 'internal');
1107 if ($result < 0) {
1108 $error++;
1109 }
1110 }
1111
1112 // Insert business contacts ('SALESREPFOLL','contrat')
1113 if (!$error) {
1114 $result = $this->add_contact($this->commercial_suivi_id, 'SALESREPFOLL', 'internal');
1115 if ($result < 0) {
1116 $error++;
1117 }
1118 }
1119
1120 if (!$error) {
1121 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1122 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1123 }
1124
1125 // Add object linked
1126 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1127 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1128 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, ...))
1129 foreach ($tmp_origin_id as $origin_id) {
1130 $ret = $this->add_object_linked($origin, $origin_id);
1131 if (!$ret) {
1132 $this->error = $this->db->lasterror();
1133 $error++;
1134 }
1135 }
1136 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1137 $origin_id = $tmp_origin_id;
1138 $ret = $this->add_object_linked($origin, $origin_id);
1139 if (!$ret) {
1140 $this->error = $this->db->lasterror();
1141 $error++;
1142 }
1143 }
1144 }
1145 }
1146
1147 if (!$error && $this->id && getDolGlobalString('MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) { // Get contact from origin object
1148 $originforcontact = $this->origin;
1149 $originidforcontact = $this->origin_id;
1150 if ($originforcontact == 'shipping') { // shipment and order share the same contacts. If creating from shipment we take data of order
1151 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1152 $exp = new Expedition($this->db);
1153 $exp->fetch($this->origin_id);
1154 $exp->fetchObjectLinked();
1155 if (count($exp->linkedObjectsIds['commande']) > 0) {
1156 foreach ($exp->linkedObjectsIds['commande'] as $key => $value) {
1157 $originforcontact = 'commande';
1158 $originidforcontact = $value;
1159 break; // We take first one
1160 }
1161 }
1162 }
1163
1164 $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";
1165 $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'";
1166
1167 $resqlcontact = $this->db->query($sqlcontact);
1168 if ($resqlcontact) {
1169 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1170 if ($objcontact->source == 'internal' && in_array($objcontact->code, array('SALESREPSIGN', 'SALESREPFOLL'))) {
1171 continue; // ignore this, already forced previously
1172 }
1173
1174 $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
1175 }
1176 } else {
1177 dol_print_error($this->db);
1178 }
1179 }
1180 }
1181
1182 if (!$error && !$notrigger) {
1183 // Call trigger
1184 $result = $this->call_trigger('CONTRACT_CREATE', $user);
1185 if ($result < 0) {
1186 $error++;
1187 }
1188 // End call triggers
1189 }
1190
1191 if (!$error) {
1192 $this->db->commit();
1193 return $this->id;
1194 } else {
1195 $this->error = "Failed to add contract";
1196 dol_syslog(get_class($this)."::create - 20 - ".$this->error, LOG_ERR);
1197 $this->db->rollback();
1198 return -2;
1199 }
1200 } else {
1201 $this->error = $langs->trans("UnknownError").": ".$this->db->error();
1202 dol_syslog(get_class($this)."::create - 10 - ".$this->error, LOG_ERR);
1203
1204 $this->db->rollback();
1205 return -1;
1206 }
1207 }
1208
1209
1216 public function delete($user)
1217 {
1218 global $conf;
1219
1220 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1221
1222 $error = 0;
1223
1224 $this->db->begin();
1225
1226 // Call trigger
1227 $result = $this->call_trigger('CONTRACT_DELETE', $user);
1228 if ($result < 0) {
1229 $error++;
1230 }
1231 // End call triggers
1232
1233 if (!$error) {
1234 // Delete linked contacts
1235 $res = $this->delete_linked_contact();
1236 if ($res < 0) {
1237 dol_syslog(get_class($this)."::delete error", LOG_ERR);
1238 $error++;
1239 }
1240 }
1241
1242 if (!$error) {
1243 // Delete linked object
1244 $res = $this->deleteObjectLinked();
1245 if ($res < 0) {
1246 $error++;
1247 }
1248 }
1249
1250 // Delete lines
1251 if (!$error) {
1252 // Delete contratdet extrafields
1253 $main = MAIN_DB_PREFIX.'contratdet';
1254 $ef = $main."_extrafields";
1255 $sql = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_contrat = ".((int) $this->id).")";
1256
1257 dol_syslog(get_class($this)."::delete contratdet_extrafields", LOG_DEBUG);
1258 $resql = $this->db->query($sql);
1259 if (!$resql) {
1260 $this->error = $this->db->error();
1261 $error++;
1262 }
1263 }
1264
1265 if (!$error) {
1266 // Delete contratdet
1267 $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet";
1268 $sql .= " WHERE fk_contrat=".((int) $this->id);
1269
1270 dol_syslog(get_class($this)."::delete contratdet", LOG_DEBUG);
1271 $resql = $this->db->query($sql);
1272 if (!$resql) {
1273 $this->error = $this->db->error();
1274 $error++;
1275 }
1276 }
1277
1278 // Delete record into ECM index and physically
1279 if (!$error) {
1280 $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
1281 if ($res) $res = $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive
1282 if (!$res) {
1283 $error++;
1284 }
1285 }
1286
1287 // Delete contract
1288 if (!$error) {
1289 $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat";
1290 $sql .= " WHERE rowid=".((int) $this->id);
1291
1292 dol_syslog(get_class($this)."::delete contrat", LOG_DEBUG);
1293 $resql = $this->db->query($sql);
1294 if (!$resql) {
1295 $this->error = $this->db->error();
1296 $error++;
1297 }
1298 }
1299
1300 // Removed extrafields
1301 if (!$error) {
1302 $result = $this->deleteExtraFields();
1303 if ($result < 0) {
1304 $error++;
1305 dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1306 }
1307 }
1308
1309 if (!$error) {
1310 // We remove directory
1311 $ref = dol_sanitizeFileName($this->ref);
1312 if ($conf->contrat->dir_output) {
1313 $dir = $conf->contrat->multidir_output[$this->entity]."/".$ref;
1314 if (file_exists($dir)) {
1315 $res = @dol_delete_dir_recursive($dir);
1316 if (!$res) {
1317 $this->error = 'ErrorFailToDeleteDir';
1318 $error++;
1319 }
1320 }
1321 }
1322 }
1323
1324 if (!$error) {
1325 $this->db->commit();
1326 return 1;
1327 } else {
1328 $this->error = $this->db->lasterror();
1329 $this->db->rollback();
1330 return -1;
1331 }
1332 }
1333
1341 public function update($user, $notrigger = 0)
1342 {
1343 global $conf;
1344 $error = 0;
1345
1346 // Clean parameters
1347 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1348 $this->fk_commercial_signature = $this->commercial_signature_id;
1349 }
1350 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1351 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1352 }
1353 if (empty($this->socid) && $this->fk_soc > 0) {
1354 $this->socid = (int) $this->fk_soc;
1355 }
1356 if (empty($this->fk_project) && $this->projet > 0) {
1357 $this->fk_project = (int) $this->projet;
1358 }
1359
1360 if (isset($this->ref)) {
1361 $this->ref = trim($this->ref);
1362 }
1363 if (isset($this->ref_customer)) {
1364 $this->ref_customer = trim($this->ref_customer);
1365 }
1366 if (isset($this->ref_supplier)) {
1367 $this->ref_supplier = trim($this->ref_supplier);
1368 }
1369 if (isset($this->ref_ext)) {
1370 $this->ref_ext = trim($this->ref_ext);
1371 }
1372 if (isset($this->entity)) {
1373 $this->entity = (int) $this->entity;
1374 }
1375 if (isset($this->status)) {
1376 $this->status = (int) $this->status;
1377 }
1378 if (isset($this->socid)) {
1379 $this->socid = (int) $this->socid;
1380 }
1381 if (isset($this->fk_commercial_signature)) {
1382 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1383 }
1384 if (isset($this->fk_commercial_suivi)) {
1385 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1386 }
1387 if (isset($this->note_private)) {
1388 $this->note_private = trim($this->note_private);
1389 }
1390 if (isset($this->note_public)) {
1391 $this->note_public = trim($this->note_public);
1392 }
1393 if (isset($this->import_key)) {
1394 $this->import_key = trim($this->import_key);
1395 }
1396
1397 $extraparams = (!empty($this->extraparams) ? json_encode($this->extraparams) : null);
1398 $extraparams = dol_trunc($extraparams, 250);
1399
1400 // $this->oldcopy must have been set by the caller of update
1401
1402 // Update request
1403 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET";
1404 $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1405 $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1406 $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1407 $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1408 $sql .= " entity=".((int) $conf->entity).",";
1409 $sql .= " date_contrat=".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : 'null').",";
1410 $sql .= " statut=".(isset($this->statut) ? $this->statut : (isset($this->status) ? $this->status : "null")).",";
1411 $sql .= " fk_soc=".($this->socid > 0 ? $this->socid : "null").",";
1412 $sql .= " fk_projet=".($this->fk_project > 0 ? $this->fk_project : "null").",";
1413 $sql .= " fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature : "null").",";
1414 $sql .= " fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi : "null").",";
1415 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1416 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1417 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").",";
1418 $sql .= " extraparams=".(isset($extraparams) ? "'".$this->db->escape($extraparams)."'" : "null");
1419 $sql .= " WHERE rowid=".((int) $this->id);
1420
1421 $this->db->begin();
1422
1423 $resql = $this->db->query($sql);
1424 if (!$resql) {
1425 $error++;
1426 $this->errors[] = "Error ".$this->db->lasterror();
1427 }
1428
1429 if (!$error) {
1430 $result = $this->insertExtraFields(); // This delete and reinsert extrafields
1431 if ($result < 0) {
1432 $error++;
1433 }
1434 }
1435
1436 if (!$error && !$notrigger) {
1437 // Call triggers
1438 $result = $this->call_trigger('CONTRACT_MODIFY', $user);
1439 if ($result < 0) {
1440 $error++;
1441 }
1442 // End call triggers
1443 }
1444
1445 // Commit or rollback
1446 if ($error) {
1447 foreach ($this->errors as $errmsg) {
1448 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1449 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1450 }
1451 $this->db->rollback();
1452 return -1 * $error;
1453 } else {
1454 $this->db->commit();
1455 return 1;
1456 }
1457 }
1458
1459
1483 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type = 'HT', $pu_ttc = 0.0, $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = array(), $fk_unit = null, $rang = 0)
1484 {
1485 global $user, $langs, $conf, $mysoc;
1486 $error = 0;
1487
1488 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");
1489
1490 // Check parameters
1491 if ($fk_product <= 0 && empty($desc)) {
1492 $this->error = "ErrorDescRequiredForFreeProductLines";
1493 return -1;
1494 }
1495
1496 if ($this->statut >= 0) {
1497 // Clean parameters
1498 $pu_ht = price2num($pu_ht);
1499 $pu_ttc = price2num($pu_ttc);
1500 $pa_ht = price2num($pa_ht);
1501
1502 // Clean vat code
1503 $reg = array();
1504 $vat_src_code = '';
1505 if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
1506 $vat_src_code = $reg[1];
1507 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code into vatrate.
1508 }
1509 $txtva = price2num($txtva);
1510 $txlocaltax1 = price2num($txlocaltax1);
1511 $txlocaltax2 = price2num($txlocaltax2);
1512
1513 $remise_percent = price2num($remise_percent);
1514 $qty = price2num($qty);
1515 if (empty($qty)) {
1516 $qty = 1;
1517 }
1518 if (empty($info_bits)) {
1519 $info_bits = 0;
1520 }
1521 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1522 $pu_ht = 0;
1523 }
1524 if (empty($pu_ttc)) {
1525 $pu_ttc = 0;
1526 }
1527 if (empty($txtva) || !is_numeric($txtva)) {
1528 $txtva = 0;
1529 }
1530 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1531 $txlocaltax1 = 0;
1532 }
1533 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1534 $txlocaltax2 = 0;
1535 }
1536
1537 if ($price_base_type == 'HT') {
1538 $pu = $pu_ht;
1539 } else {
1540 $pu = $pu_ttc;
1541 }
1542
1543 // Check parameters
1544 if (empty($remise_percent)) {
1545 $remise_percent = 0;
1546 }
1547 if (empty($rang)) {
1548 $rang = 0;
1549 }
1550
1551 if ($date_start && $date_end && $date_start > $date_end) {
1552 $langs->load("errors");
1553 $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1554 return -1;
1555 }
1556
1557 $this->db->begin();
1558
1559 $localtaxes_type = getLocalTaxesFromRate($txtva.($vat_src_code ? ' ('.$vat_src_code.')' : ''), 0, $this->societe, $mysoc);
1560
1561 // Calcul du total TTC et de la TVA pour la ligne a partir de
1562 // qty, pu, remise_percent et txtva
1563 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1564 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1565
1566 $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1567 $total_ht = $tabprice[0];
1568 $total_tva = $tabprice[1];
1569 $total_ttc = $tabprice[2];
1570 $total_localtax1 = $tabprice[9];
1571 $total_localtax2 = $tabprice[10];
1572
1573 if (count($localtaxes_type) > 0) {
1574 $localtax1_type = $localtaxes_type[0];
1575 $localtax2_type = $localtaxes_type[2];
1576 } else {
1577 $localtax1_type = "";
1578 $localtax2_type = "";
1579 }
1580
1581 if (empty($pa_ht)) {
1582 $pa_ht = 0;
1583 }
1584
1585
1586 // if buy price not defined, define buyprice as configured in margin admin
1587 if ($pa_ht == 0) {
1588 $result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product);
1589 if ($result < 0) {
1590 return -1;
1591 } else {
1592 $pa_ht = $result;
1593 }
1594 }
1595
1596 // Insertion dans la base
1597 $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
1598 $sql .= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1599 $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1600 $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1601 $sql .= " info_bits,";
1602 $sql .= " fk_product_fournisseur_price, buy_price_ht";
1603 if ($date_start > 0) {
1604 $sql .= ",date_ouverture_prevue";
1605 }
1606 if ($date_end > 0) {
1607 $sql .= ",date_fin_validite";
1608 }
1609 $sql .= ", fk_unit";
1610 $sql .= ", rang";
1611 $sql .= ") VALUES (";
1612 $sql .= $this->id.", '', '".$this->db->escape($desc)."',";
1613 $sql .= ($fk_product > 0 ? $fk_product : "null").",";
1614 $sql .= " ".((float) $qty).",";
1615 $sql .= " ".((float) $txtva).",";
1616 $sql .= " ".($vat_src_code ? "'".$this->db->escape($vat_src_code)."'" : "null").",";
1617 $sql .= " ".((float) $txlocaltax1).",";
1618 $sql .= " ".((float) $txlocaltax2).",";
1619 $sql .= " '".$this->db->escape($localtax1_type)."',";
1620 $sql .= " '".$this->db->escape($localtax2_type)."',";
1621 $sql .= " ".price2num($remise_percent).",";
1622 $sql .= " ".price2num($pu_ht).",";
1623 $sql .= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).",";
1624 $sql .= " ".((int) $info_bits).",";
1625 if (isset($fk_fournprice)) {
1626 $sql .= ' '.((int) $fk_fournprice).',';
1627 } else {
1628 $sql .= ' null,';
1629 }
1630 if (isset($pa_ht)) {
1631 $sql .= ' '.price2num($pa_ht);
1632 } else {
1633 $sql .= ' null';
1634 }
1635 if ($date_start > 0) {
1636 $sql .= ",'".$this->db->idate($date_start)."'";
1637 }
1638 if ($date_end > 0) {
1639 $sql .= ",'".$this->db->idate($date_end)."'";
1640 }
1641 $sql .= ", ".($fk_unit ? "'".$this->db->escape((string) $fk_unit)."'" : "null");
1642 $sql .= ", ".(!empty($rang) ? (int) $rang : "0");
1643 $sql .= ")";
1644
1645 $resql = $this->db->query($sql);
1646 if ($resql) {
1647 $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX."contratdet");
1648
1649 if (!$error) {
1650 $contractline = new ContratLigne($this->db);
1651 $contractline->array_options = $array_options;
1652 $contractline->id = $contractlineid;
1653 $result = $contractline->insertExtraFields();
1654 if ($result < 0) {
1655 $this->errors = array_merge($this->errors, $contractline->errors);
1656 $this->error = $contractline->error;
1657 $error++;
1658 }
1659 }
1660
1661 if (empty($error)) {
1662 // Call trigger
1663 $this->context['line_id'] = $contractlineid;
1664 $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
1665 if ($result < 0) {
1666 $error++;
1667 }
1668 // End call triggers
1669 }
1670
1671 if ($error) {
1672 $this->db->rollback();
1673 return -1;
1674 } else {
1675 $this->db->commit();
1676 return $contractlineid;
1677 }
1678 } else {
1679 $this->db->rollback();
1680 $this->error = $this->db->error()." sql=".$sql;
1681 return -1;
1682 }
1683 } else {
1684 dol_syslog(get_class($this)."::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1685 return -2;
1686 }
1687 }
1688
1713 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx = 0.0, $localtax2tx = 0.0, $date_start_real = '', $date_end_real = '', $price_base_type = 'HT', $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = array(), $fk_unit = null, $rang = 0)
1714 {
1715 global $user, $conf, $langs, $mysoc;
1716
1717 $error = 0;
1718
1719 // Clean parameters
1720 $qty = trim((string) $qty);
1721 $desc = trim($desc);
1722 $desc = trim($desc);
1723 $subprice = price2num($pu);
1724 $tvatx = price2num($tvatx);
1725 $localtax1tx = price2num($localtax1tx);
1726 $localtax2tx = price2num($localtax2tx);
1727 $pa_ht = price2num($pa_ht);
1728 if (empty($fk_fournprice)) {
1729 $fk_fournprice = 0;
1730 }
1731 if (empty($rang)) {
1732 $rang = 0;
1733 }
1734
1735 if ($date_start && $date_end && $date_start > $date_end) {
1736 $langs->load("errors");
1737 $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1738 return -1;
1739 }
1740
1741 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");
1742
1743 $this->db->begin();
1744
1745 // Calcul du total TTC et de la TVA pour la ligne a partir de
1746 // qty, pu, remise_percent et tvatx
1747 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1748 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1749
1750 $localtaxes_type = getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc);
1751 $tvatx = preg_replace('/\s*\‍(.*\‍)/', '', $tvatx); // Remove code into vatrate.
1752
1753 $tabprice = calcul_price_total((float) $qty, $pu, $remise_percent, (float) price2num($tvatx), (float) $localtax1tx, (float) $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1754 $total_ht = $tabprice[0];
1755 $total_tva = $tabprice[1];
1756 $total_ttc = $tabprice[2];
1757 $total_localtax1 = $tabprice[9];
1758 $total_localtax2 = $tabprice[10];
1759
1760 $localtax1_type = (empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]);
1761 $localtax2_type = (empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]);
1762
1763 if (empty($pa_ht)) {
1764 $pa_ht = 0;
1765 }
1766
1767 // if buy price not defined, define buyprice as configured in margin admin
1768 if ($pa_ht == 0) {
1769 $result = $this->defineBuyPrice($pu, $remise_percent);
1770 if ($result < 0) {
1771 return -1;
1772 } else {
1773 $pa_ht = $result;
1774 }
1775 }
1776
1777 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description = '".$this->db->escape($desc)."'";
1778 $sql .= ",subprice = ".((float) price2num($subprice));
1779 $sql .= ",remise_percent = ".((float) price2num($remise_percent));
1780 $sql .= ",qty = ".((float) $qty);
1781 $sql .= ",tva_tx = ".((float) price2num($tvatx));
1782 $sql .= ",localtax1_tx = ".((float) price2num($localtax1tx));
1783 $sql .= ",localtax2_tx = ".((float) price2num($localtax2tx));
1784 $sql .= ",localtax1_type='".$this->db->escape($localtax1_type)."'";
1785 $sql .= ",localtax2_type='".$this->db->escape($localtax2_type)."'";
1786 $sql .= ", total_ht = ".((float) price2num($total_ht));
1787 $sql .= ", total_tva = ".((float) price2num($total_tva));
1788 $sql .= ", total_localtax1 = ".((float) price2num($total_localtax1));
1789 $sql .= ", total_localtax2 = ".((float) price2num($total_localtax2));
1790 $sql .= ", total_ttc = ".((float) price2num($total_ttc));
1791 $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null");
1792 $sql .= ", buy_price_ht = ".((float) price2num($pa_ht));
1793 if ($date_start > 0) {
1794 $sql .= ",date_ouverture_prevue = '".$this->db->idate($date_start)."'";
1795 } else {
1796 $sql .= ",date_ouverture_prevue = null";
1797 }
1798 if ($date_end > 0) {
1799 $sql .= ",date_fin_validite = '".$this->db->idate($date_end)."'";
1800 } else {
1801 $sql .= ",date_fin_validite = null";
1802 }
1803 if ($date_start_real > 0) {
1804 $sql .= ",date_ouverture = '".$this->db->idate($date_start_real)."'";
1805 } else {
1806 $sql .= ",date_ouverture = null";
1807 }
1808 if ($date_end_real > 0) {
1809 $sql .= ",date_cloture = '".$this->db->idate($date_end_real)."'";
1810 } else {
1811 $sql .= ",date_cloture = null";
1812 }
1813 $sql .= ", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) : "null");
1814 $sql .= ", rang = ".(!empty($rang) ? ((int) $rang) : "0");
1815 $sql .= " WHERE rowid = ".((int) $rowid);
1816
1817 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1818 $result = $this->db->query($sql);
1819 if ($result) {
1820 if (is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1821 $contractline = new ContratLigne($this->db);
1822 $contractline->fetch($rowid);
1823
1824 // We replace values in $contractline->array_options only for entries defined into $array_options
1825 foreach ($array_options as $key => $value) {
1826 $contractline->array_options[$key] = $array_options[$key];
1827 }
1828
1829 $result = $contractline->insertExtraFields();
1830 if ($result < 0) {
1831 $this->errors[] = $contractline->error;
1832 $error++;
1833 }
1834 }
1835
1836 if (empty($error)) {
1837 // Call trigger
1838 $this->context['line_id'] = $rowid;
1839 $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
1840 if ($result < 0) {
1841 $this->db->rollback();
1842 return -3;
1843 }
1844 // End call triggers
1845
1846 $this->db->commit();
1847 return 1;
1848 } else {
1849 $this->db->rollback();
1850 return -1;
1851 }
1852 } else {
1853 $this->db->rollback();
1854 $this->error = $this->db->error();
1855 dol_syslog(get_class($this)."::updateline Erreur -1");
1856 return -1;
1857 }
1858 }
1859
1867 public function deleteLine($idline, User $user)
1868 {
1869 $error = 0;
1870
1871 if ($this->statut >= 0) {
1872 // Call trigger
1873 $this->context['line_id'] = $idline;
1874 $result = $this->call_trigger('LINECONTRACT_DELETE', $user);
1875 if ($result < 0) {
1876 return -1;
1877 }
1878 // End call triggers
1879
1880 $this->db->begin();
1881
1882 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1883 $sql .= " WHERE rowid = ".((int) $idline);
1884
1885 dol_syslog(get_class($this)."::deleteline", LOG_DEBUG);
1886 $resql = $this->db->query($sql);
1887 if (!$resql) {
1888 $this->error = "Error ".$this->db->lasterror();
1889 $error++;
1890 }
1891
1892 if (!$error) {
1893 // Remove extrafields
1894 $contractline = new ContratLigne($this->db);
1895 $contractline->id = $idline;
1896 $result = $contractline->deleteExtraFields();
1897 if ($result < 0) {
1898 $error++;
1899 $this->error = "Error ".get_class($this)."::deleteline deleteExtraFields error -4 ".$contractline->error;
1900 }
1901 }
1902
1903 if (empty($error)) {
1904 $this->db->commit();
1905 return 1;
1906 } else {
1907 dol_syslog(get_class($this)."::deleteline ERROR:".$this->error, LOG_ERR);
1908 $this->db->rollback();
1909 return -1;
1910 }
1911 } else {
1912 $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1913 return -2;
1914 }
1915 }
1916
1917
1918 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1926 public function update_statut($user)
1927 {
1928 // phpcs:enable
1929 dol_syslog(__METHOD__." is deprecated", LOG_WARNING);
1930
1931 // If draft, we keep it (should not happen)
1932 if ($this->statut == 0) {
1933 return 1;
1934 }
1935
1936 // Load $this->lines array
1937 // $this->fetch_lines();
1938
1939 // $newstatut=1;
1940 // foreach($this->lines as $key => $contractline)
1941 // {
1942 // // if ($contractline) // Loop on each service
1943 // }
1944
1945 return 1;
1946 }
1947
1948
1955 public function getLibStatut($mode)
1956 {
1957 return $this->LibStatut($this->statut, $mode);
1958 }
1959
1960 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1968 public function LibStatut($status, $mode)
1969 {
1970 // phpcs:enable
1971 global $langs;
1972
1973 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1974 global $langs;
1975 $langs->load("contracts");
1976 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1977 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1978 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1979 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('ContractStatusDraft');
1980 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ContractStatusValidated');
1981 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('ContractStatusClosed');
1982 }
1983
1984 $statusType = 'status'.$status;
1985 if ($status == self::STATUS_VALIDATED) {
1986 $statusType = 'status6';
1987 }
1988
1989 if ($mode == 4 || $mode == 6 || $mode == 7) {
1990 $text = '';
1991 if ($mode == 4) {
1992 $text = '<span class="hideonsmartphone">';
1993 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1994 $text .= ' '.$langs->trans("Services");
1995 $text .= ': &nbsp; &nbsp; ';
1996 $text .= '</span>';
1997 }
1998 $text .= ($mode == 7 ? '<span class="nowraponall">' : '');
1999 $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2000 $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2001 $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2002 $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2003 $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1, 'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ? ' &nbsp; ' : '') : '';
2004 $text .= ($mode == 7 ? '</span><span class="nowraponall">' : '');
2005 $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="marginleft2"')) : '';
2006 $text .= ($mode == 7 ? '</span>' : '');
2007 if (getDolGlobalString('CONTRACT_SHOW_SIGNATURE_STATUS_WITH_SERVICE_STATUS')) {
2008 $text .= is_null($this->signed_status) ? '' : ' '.$this->getLibSignedStatus(5);
2009 }
2010 return $text;
2011 } else {
2012 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
2013 }
2014 }
2015
2022 public function getTooltipContentArray($params)
2023 {
2024 global $conf, $langs, $user;
2025
2026 $langs->load('contracts');
2027
2028 $datas = [];
2029 $nofetch = !empty($params['nofetch']);
2030
2031 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2032 return ['optimize' => $langs->trans("ShowContract")];
2033 }
2034 if ($user->hasRight('contrat', 'lire')) {
2035 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contract").'</u>';
2036 /* Status of a contract is status of all services, so disabled
2037 if (isset($this->statut)) {
2038 $label .= ' '.$this->getLibStatut(5);
2039 }*/
2040 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
2041 if (!$nofetch) {
2042 $langs->load('companies');
2043 if (empty($this->thirdparty)) {
2044 $this->fetch_thirdparty();
2045 }
2046 $datas['customer'] = '<br><b>'.$langs->trans('Customer').':</b> '.$this->thirdparty->getNomUrl(1, '', 0, 1);
2047 }
2048 $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '. $this->ref_customer;
2049 if (!$nofetch) {
2050 $langs->load('project');
2051 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
2052 $res = $this->fetchProject();
2053 if ($res > 0 && $this->project instanceof Project) {
2054 $datas['project'] = '<br><b>'.$langs->trans('Project').':</b> '.$this->project->getNomUrl(1, '', 0, '1');
2055 }
2056 }
2057 }
2058 $datas['refsupplier'] = '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
2059 if (!empty($this->total_ht)) {
2060 $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2061 }
2062 if (!empty($this->total_tva)) {
2063 $datas['vatamount'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2064 }
2065 if (!empty($this->total_ttc)) {
2066 $datas['amounttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2067 }
2068 }
2069 return $datas;
2070 }
2071
2081 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2082 {
2083 global $conf, $langs, $user, $hookmanager;
2084
2085 $result = '';
2086
2087 $url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id;
2088
2089 //if ($option !== 'nolink')
2090 //{
2091 // Add param to save lastsearch_values or not
2092 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2093 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2094 $add_save_lastsearch_values = 1;
2095 }
2096 if ($add_save_lastsearch_values) {
2097 $url .= '&save_lastsearch_values=1';
2098 }
2099 //}
2100 $params = [
2101 'id' => $this->id,
2102 'objecttype' => $this->element,
2103 'nofetch' => 1,
2104 ];
2105 $classfortooltip = 'classfortooltip';
2106 $dataparams = '';
2107 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
2108 $classfortooltip = 'classforajaxtooltip';
2109 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
2110 $label = '';
2111 } else {
2112 $label = implode($this->getTooltipContentArray($params));
2113 }
2114
2115 $linkclose = '';
2116 if (empty($notooltip) && $user->hasRight('contrat', 'lire')) {
2117 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2118 $label = $langs->trans("ShowContract");
2119 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
2120 }
2121 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
2122 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
2123 }
2124 $linkstart = '<a href="'.$url.'"';
2125 $linkstart .= $linkclose.'>';
2126 $linkend = '</a>';
2127
2128 $result .= $linkstart;
2129 if ($withpicto) {
2130 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
2131 }
2132 if ($withpicto != 2) {
2133 $result .= ($this->ref ? $this->ref : $this->id);
2134 }
2135 $result .= $linkend;
2136
2137 global $action;
2138 $hookmanager->initHooks(array('contractdao'));
2139 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
2140 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
2141 if ($reshook > 0) {
2142 $result = $hookmanager->resPrint;
2143 } else {
2144 $result .= $hookmanager->resPrint;
2145 }
2146
2147 return $result;
2148 }
2149
2156 public function info($id)
2157 {
2158 $sql = "SELECT c.rowid, c.ref, c.datec,";
2159 $sql .= " c.tms as date_modification,";
2160 $sql .= " fk_user_author";
2161 $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2162 $sql .= " WHERE c.rowid = ".((int) $id);
2163
2164 $result = $this->db->query($sql);
2165 if ($result) {
2166 if ($this->db->num_rows($result)) {
2167 $obj = $this->db->fetch_object($result);
2168
2169 $this->id = $obj->rowid;
2170 $this->ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2171
2172 $this->user_creation_id = $obj->fk_user_author;
2173 $this->date_creation = $this->db->jdate($obj->datec);
2174 $this->date_modification = $this->db->jdate($obj->date_modification);
2175 }
2176
2177 $this->db->free($result);
2178 } else {
2179 dol_print_error($this->db);
2180 }
2181 }
2182
2183 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2190 public function array_detail($status = -1)
2191 {
2192 // phpcs:enable
2193 $tab = array();
2194
2195 $sql = "SELECT cd.rowid";
2196 $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
2197 $sql .= " WHERE fk_contrat =".((int) $this->id);
2198 if ($status >= 0) {
2199 $sql .= " AND statut = ".((int) $status);
2200 }
2201
2202 dol_syslog(get_class($this)."::array_detail()", LOG_DEBUG);
2203 $resql = $this->db->query($sql);
2204 if ($resql) {
2205 $num = $this->db->num_rows($resql);
2206 $i = 0;
2207 while ($i < $num) {
2208 $obj = $this->db->fetch_object($resql);
2209 $tab[$i] = $obj->rowid;
2210 $i++;
2211 }
2212 return $tab;
2213 } else {
2214 $this->error = $this->db->error();
2215 return -1;
2216 }
2217 }
2218
2228 public function getListOfContracts($option = 'all', $status = [], $product_categories = [], $line_status = [])
2229 {
2230 $tab = array();
2231
2232 $sql = "SELECT c.rowid";
2233 $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2234 if (!empty($product_categories)) {
2235 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON cd.fk_contrat = c.rowid";
2236 $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)).")";
2237 }
2238 $sql .= " WHERE c.fk_soc =".((int) $this->socid);
2239 $sql .= ($option == 'others') ? " AND c.rowid <> ".((int) $this->id) : "";
2240 $sql .= (!empty($status)) ? " AND c.statut IN (".$this->db->sanitize(implode(', ', $status)).")" : "";
2241 $sql .= (!empty($line_status)) ? " AND cd.statut IN (".$this->db->sanitize(implode(', ', $line_status)).")" : "";
2242 $sql .= " GROUP BY c.rowid";
2243
2244 dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG);
2245 $resql = $this->db->query($sql);
2246 if ($resql) {
2247 $num = $this->db->num_rows($resql);
2248 $i = 0;
2249 while ($i < $num) {
2250 $obj = $this->db->fetch_object($resql);
2251 $contrat = new Contrat($this->db);
2252 $contrat->fetch($obj->rowid);
2253 $tab[$contrat->id] = $contrat;
2254 $i++;
2255 }
2256 return $tab;
2257 } else {
2258 $this->error = $this->db->lasterror();
2259 return -1;
2260 }
2261 }
2262
2263
2264 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2272 public function load_board($user, $mode)
2273 {
2274 // phpcs:enable
2275 global $conf, $langs;
2276
2277 $this->from = " FROM ".MAIN_DB_PREFIX."contrat as c";
2278 $this->from .= ", ".MAIN_DB_PREFIX."contratdet as cd";
2279 $this->from .= ", ".MAIN_DB_PREFIX."societe as s";
2280 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
2281 $this->from .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2282 }
2283
2284 if ($mode == 'inactive') {
2285 $sql = "SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2286 $sql .= $this->from;
2287 $sql .= " WHERE c.statut = 1";
2288 $sql .= " AND c.rowid = cd.fk_contrat";
2289 $sql .= " AND cd.statut = 0";
2290 } elseif ($mode == 'expired') {
2291 $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2292 $sql .= $this->from;
2293 $sql .= " WHERE c.statut = 1";
2294 $sql .= " AND c.rowid = cd.fk_contrat";
2295 $sql .= " AND cd.statut = 4";
2296 $sql .= " AND cd.date_fin_validite < '".$this->db->idate(dol_now())."'";
2297 } elseif ($mode == 'active') {
2298 $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin";
2299 $sql .= $this->from;
2300 $sql .= " WHERE c.statut = 1";
2301 $sql .= " AND c.rowid = cd.fk_contrat";
2302 $sql .= " AND cd.statut = 4";
2303 //$datetouse = dol_now();
2304 //$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'";
2305 }
2306 $sql .= " AND c.fk_soc = s.rowid";
2307 $sql .= " AND c.entity = ".((int) $conf->entity);
2308 if ($user->socid) {
2309 $sql .= " AND c.fk_soc = ".((int) $user->socid);
2310 }
2311 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
2312 $sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2313 }
2314
2315 $resql = $this->db->query($sql);
2316 if ($resql) {
2317 $langs->load("contracts");
2318 $now = dol_now();
2319
2320 if ($mode == 'inactive') {
2321 $warning_delay = $conf->contract->services->inactifs->warning_delay;
2322 $label = $langs->trans("BoardNotActivatedServices");
2323 $labelShort = $langs->trans("BoardNotActivatedServicesShort");
2324 $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2325 $url_late = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&search_option=late';
2326 } elseif ($mode == 'expired') {
2327 $warning_delay = $conf->contract->services->expires->warning_delay;
2328 $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2329 $url_late = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&search_option=late';
2330 $label = $langs->trans("BoardExpiredServices");
2331 $labelShort = $langs->trans("BoardExpiredServicesShort");
2332 } else {
2333 $warning_delay = $conf->contract->services->expires->warning_delay;
2334 $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2335 $url_late = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_option=late';
2336 $label = $langs->trans("BoardRunningServices");
2337 $labelShort = $langs->trans("BoardRunningServicesShort");
2338 }
2339
2340 $response = new WorkboardResponse();
2341 $response->warning_delay = $warning_delay / 60 / 60 / 24;
2342 $response->label = $label;
2343 $response->labelShort = $labelShort;
2344 $response->url = $url;
2345 $response->url_late = $url_late;
2346 $response->img = img_object('', "contract");
2347
2348 while ($obj = $this->db->fetch_object($resql)) {
2349 $response->nbtodo++;
2350
2351 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2352 $response->nbtodolate++;
2353 }
2354 }
2355
2356 return $response;
2357 } else {
2358 dol_print_error($this->db);
2359 $this->error = $this->db->error();
2360 return -1;
2361 }
2362 }
2363
2369 public function loadStateBoard()
2370 {
2371 global $conf, $user;
2372
2373 $this->nb = array();
2374 $clause = "WHERE";
2375
2376 $sql = "SELECT count(c.rowid) as nb";
2377 $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
2378 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
2379 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
2380 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2381 $sql .= " WHERE sc.fk_user = ".((int) $user->id);
2382 $clause = "AND";
2383 }
2384 $sql .= " ".$clause." c.entity = ".$conf->entity;
2385
2386 $resql = $this->db->query($sql);
2387 if ($resql) {
2388 while ($obj = $this->db->fetch_object($resql)) {
2389 $this->nb["contracts"] = $obj->nb;
2390 }
2391 $this->db->free($resql);
2392 return 1;
2393 } else {
2394 dol_print_error($this->db);
2395 $this->error = $this->db->error();
2396 return -1;
2397 }
2398 }
2399
2400
2401 /* gestion des contacts d'un contrat */
2402
2408 public function getIdBillingContact()
2409 {
2410 return $this->getIdContact('external', 'BILLING');
2411 }
2412
2418 public function getIdServiceContact()
2419 {
2420 return $this->getIdContact('external', 'SERVICE');
2421 }
2422
2423
2431 public function initAsSpecimen()
2432 {
2433 global $user, $langs, $conf;
2434
2435 // Load array of products prodids
2436 $num_prods = 0;
2437 $prodids = array();
2438 $sql = "SELECT rowid";
2439 $sql .= " FROM ".MAIN_DB_PREFIX."product";
2440 $sql .= " WHERE entity IN (".getEntity('product').")";
2441 $sql .= " AND tosell = 1";
2442 $sql .= $this->db->plimit(100);
2443
2444 $resql = $this->db->query($sql);
2445 if ($resql) {
2446 $num_prods = $this->db->num_rows($resql);
2447 $i = 0;
2448 while ($i < $num_prods) {
2449 $i++;
2450 $row = $this->db->fetch_row($resql);
2451 $prodids[$i] = $row[0];
2452 }
2453 }
2454
2455 // Initialise parameters
2456 $this->id = 0;
2457 $this->specimen = 1;
2458
2459 $this->ref = 'SPECIMEN';
2460 $this->ref_customer = 'SPECIMENCUST';
2461 $this->ref_supplier = 'SPECIMENSUPP';
2462 $this->socid = 1;
2463 $this->status = 0;
2464 $this->date_creation = (dol_now() - 3600 * 24 * 7);
2465 $this->date_contrat = dol_now();
2466 $this->commercial_signature_id = 1;
2467 $this->commercial_suivi_id = 1;
2468 $this->note_private = 'This is a comment (private)';
2469 $this->note_public = 'This is a comment (public)';
2470 $this->fk_project = 0;
2471 // Lines
2472 $nbp = min(1000, GETPOSTINT('nblines') ? GETPOSTINT('nblines') : 5); // We can force the nb of lines to test from command line (but not more than 1000)
2473 $xnbp = 0;
2474 while ($xnbp < $nbp) {
2475 $line = new ContratLigne($this->db);
2476 $line->qty = 1;
2477 $line->subprice = 100;
2478 $line->tva_tx = 19.6;
2479 $line->remise_percent = 10;
2480 $line->total_ht = 90;
2481 $line->total_ttc = 107.64; // 90 * 1.196
2482 $line->total_tva = 17.64;
2483 $line->date_start = dol_now() - 500000;
2484 $line->date_start_real = dol_now() - 200000;
2485 $line->date_end = dol_now() + 500000;
2486 $line->date_end_real = dol_now() - 100000;
2487 if ($num_prods > 0) {
2488 $prodid = mt_rand(1, $num_prods);
2489 $line->fk_product = $prodids[$prodid];
2490 }
2491 $this->lines[$xnbp] = $line;
2492 $xnbp++;
2493 }
2494
2495 return 1;
2496 }
2497
2503 public function getLinesArray()
2504 {
2505 return $this->fetch_lines();
2506 }
2507
2513 public function getTicketsArray()
2514 {
2515 global $user;
2516
2517 $ticket = new Ticket($this->db);
2518 $nbTicket = $ticket->fetchAll($user, 'ASC', 't.datec', 0, 0, 0, array('t.fk_contract' => $this->id));
2519
2520 return ($nbTicket < 0 ? $nbTicket : $ticket->lines);
2521 }
2522
2523
2535 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2536 {
2537 global $conf, $langs;
2538
2539 if (!dol_strlen($modele)) {
2540 $modele = ''; // No doc template/generation by default
2541
2542 if (!empty($this->model_pdf)) {
2543 $modele = $this->model_pdf;
2544 } elseif (getDolGlobalString('CONTRACT_ADDON_PDF')) {
2545 $modele = getDolGlobalString('CONTRACT_ADDON_PDF');
2546 }
2547 }
2548
2549 if (empty($modele)) {
2550 return 0;
2551 } else {
2552 $langs->load("contracts");
2553 $outputlangs->load("products");
2554
2555 $modelpath = "core/modules/contract/doc/";
2556 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2557 }
2558 }
2559
2568 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2569 {
2570 $tables = array(
2571 'contrat'
2572 );
2573
2574 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2575 }
2576
2585 public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
2586 {
2587 $tables = array(
2588 'contratdet'
2589 );
2590
2591 return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
2592 }
2593
2602 public function createFromClone(User $user, $socid = 0, $notrigger = 0)
2603 {
2604 global $db, $langs, $conf, $hookmanager, $extrafields;
2605
2606 dol_include_once('/projet/class/project.class.php');
2607
2608 $error = 0;
2609
2610 $this->fetch($this->id);
2611
2612 // Load dest object
2613 $clonedObj = clone $this;
2614 $clonedObj->socid = $socid;
2615
2616 $this->db->begin();
2617
2618 $objsoc = new Societe($this->db);
2619
2620 $objsoc->fetch($clonedObj->socid);
2621
2622 // Clean data
2623 $clonedObj->statut = 0;
2624 // Clean extrafields
2625 if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2626 $extrafields->fetch_name_optionals_label($this->table_element);
2627 foreach ($clonedObj->array_options as $key => $option) {
2628 $shortkey = preg_replace('/options_/', '', $key);
2629 //var_dump($shortkey); var_dump($extrafields->attributes[$this->element]['unique'][$shortkey]);
2630 if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
2631 //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
2632 unset($clonedObj->array_options[$key]);
2633 }
2634 }
2635 }
2636
2637 if (!getDolGlobalString('CONTRACT_ADDON') || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').".php")) {
2638 $this->error = 'ErrorSetupNotComplete';
2639 dol_syslog($this->error);
2640 return -1;
2641 }
2642
2643 // Set ref
2644 require_once DOL_DOCUMENT_ROOT."/core/modules/contract/" . getDolGlobalString('CONTRACT_ADDON').'.php';
2645 $obj = getDolGlobalString('CONTRACT_ADDON');
2646 $modContract = new $obj();
2647 '@phan-var-force ModelNumRefContracts $modContract';
2648 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2649
2650 // get extrafields so they will be clone
2651 foreach ($this->lines as $line) {
2652 $line->fetch_optionals($line->id);
2653 }
2654
2655 // Create clone
2656 $clonedObj->context['createfromclone'] = 'createfromclone';
2657 $result = $clonedObj->create($user);
2658 if ($result < 0) {
2659 $error++;
2660 $this->error = $clonedObj->error;
2661 $this->errors[] = $clonedObj->error;
2662 } else {
2663 // copy external contacts if same company
2664 if ($this->socid == $clonedObj->socid) {
2665 if ($clonedObj->copy_linked_contact($this, 'external') < 0) {
2666 $error++;
2667 }
2668 }
2669 }
2670
2671 if (!$error) {
2672 foreach ($this->lines as $line) {
2673 $result = $clonedObj->addline($line->description, $line->subprice, $line->qty, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->fk_product, $line->remise_percent, $line->date_start, $line->date_cloture, 'HT', 0, $line->info_bits, $line->fk_fournprice, $line->pa_ht, $line->array_options, $line->fk_unit, $line->rang);
2674 if ($result < 0) {
2675 $error++;
2676 $this->error = $clonedObj->error;
2677 $this->errors[] = $clonedObj->error;
2678 }
2679 }
2680 }
2681
2682 if (!$error) {
2683 // Hook of thirdparty module
2684 if (is_object($hookmanager)) {
2685 $parameters = array(
2686 'objFrom' => $this,
2687 'clonedObj' => $clonedObj
2688 );
2689 $action = '';
2690 $reshook = $hookmanager->executeHooks('createFrom', $parameters, $clonedObj, $action); // Note that $action and $object may have been modified by some hooks
2691 if ($reshook < 0) {
2692 $this->setErrorsFromObject($hookmanager);
2693 $error++;
2694 }
2695 }
2696 }
2697
2698 unset($clonedObj->context['createfromclone']);
2699
2700 // End
2701 if (!$error) {
2702 $this->db->commit();
2703 return $clonedObj->id;
2704 } else {
2705 $this->db->rollback();
2706 return -1;
2707 }
2708 }
2709
2710
2720 public function doAutoRenewContracts($thirdparty_id = 0, $delayindaysshort = 0)
2721 {
2722 global $langs, $user;
2723
2724 $langs->load("agenda");
2725
2726 $now = dol_now();
2727
2728 $enddatetoscan = dol_time_plus_duree($now, -1 * abs($delayindaysshort), 'd');
2729
2730 $error = 0;
2731 $this->output = '';
2732 $this->error = '';
2733
2734 $contractlineprocessed = array();
2735 $contractignored = array();
2736 $contracterror = array();
2737
2738 dol_syslog(__METHOD__, LOG_DEBUG);
2739
2740 $sql = 'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2741 $sql .= ' FROM '.MAIN_DB_PREFIX.'contrat as c, '.MAIN_DB_PREFIX.'contratdet as cd';
2742 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = cd.fk_product';
2743 $sql .= ' WHERE cd.fk_contrat = c.rowid';
2744 $sql .= " AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan)."', '%Y-%m-%d')";
2745 $sql .= " AND cd.statut = 4";
2746 if ($thirdparty_id > 0) {
2747 $sql .= " AND c.fk_soc = ".((int) $thirdparty_id);
2748 }
2749 //print $sql;
2750
2751 $resql = $this->db->query($sql);
2752 if ($resql) {
2753 $num = $this->db->num_rows($resql);
2754
2755 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2756
2757 $i = 0;
2758 while ($i < $num) {
2759 $obj = $this->db->fetch_object($resql);
2760 if ($obj) {
2761 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2762 continue;
2763 }
2764
2765 // Load contract
2766 $object = new Contrat($this->db);
2767 $object->fetch($obj->rowid); // fetch also lines
2768 //$object->fetch_thirdparty();
2769
2770 if ($object->id <= 0) {
2771 $error++;
2772 $this->errors[] = 'Failed to load contract with id='.$obj->rowid;
2773 continue;
2774 }
2775
2776 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);
2777
2778 // Update expiration date of line
2779 $expirationdate = $this->db->jdate($obj->date_fin_validite);
2780 $duration_value = preg_replace('/[^0-9]/', '', $obj->duration);
2781 $duration_unit = preg_replace('/\d/', '', $obj->duration);
2782 //var_dump($expirationdate.' '.$enddatetoscan);
2783
2784 // Load linked ->linkedObjects (objects linked)
2785 // @TODO Comment this line and then make the search if there is n open invoice(s) by doing a dedicated SQL COUNT request to fill $contractcanceled.
2786 $object->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
2787
2788 // Test if there is at least 1 open invoice
2789 if (isset($object->linkedObjects['facture']) && is_array($object->linkedObjects['facture']) && count($object->linkedObjects['facture']) > 0) {
2790 // Sort array of linked invoices by ascending date
2791 usort($object->linkedObjects['facture'], array('Contrat', 'contractCmpDate'));
2792 //dol_sort_array($object->linkedObjects['facture'], 'date');
2793
2794 $someinvoicenotpaid = 0;
2795 foreach ($object->linkedObjects['facture'] as $idinvoice => $invoice) {
2796 if ($invoice->statut == Facture::STATUS_DRAFT) {
2797 continue;
2798 } // Draft invoice are not invoice not paid
2799
2800 if (empty($invoice->paye)) {
2801 $someinvoicenotpaid++;
2802 }
2803 }
2804 if ($someinvoicenotpaid) {
2805 $this->output .= 'Contract '.$object->ref.' is qualified for renewal but there is '.$someinvoicenotpaid.' invoice(s) unpayed so we cancel renewal'."\n";
2806 $contractignored[$object->id] = $object->ref;
2807 continue;
2808 }
2809 }
2810
2811 if ($expirationdate && $expirationdate < $enddatetoscan) {
2812 dol_syslog("Define the newdate of end of services from expirationdate=".$expirationdate);
2813 $newdate = $expirationdate;
2814 $protecti = 0; // $protecti is to avoid infinite loop
2815 while ($newdate < $enddatetoscan && $protecti < 1000) {
2816 $newdate = dol_time_plus_duree($newdate, (int) $duration_value, $duration_unit);
2817 $protecti++;
2818 }
2819
2820 if ($protecti < 1000) { // If not, there is a pb
2821 // We will update the end of date of contrat, so first we refresh contract data
2822 dol_syslog("We will update the end of date of contract with newdate = ".dol_print_date($newdate, 'dayhourrfc'));
2823
2824 $this->db->begin();
2825
2826 $errorforlocaltransaction = 0;
2827
2828 $label = 'Renewal of contrat '.$object->ref.' line '.$obj->lid;
2829 $comment = 'Renew date of contract '.$object->ref.' line '.$obj->lid.' by doAutoRenewContracts';
2830
2831 $sqlupdate = 'UPDATE '.MAIN_DB_PREFIX."contratdet SET date_fin_validite = '".$this->db->idate($newdate)."'";
2832 $sqlupdate .= ' WHERE rowid = '.((int) $obj->lid);
2833 $resqlupdate = $this->db->query($sqlupdate);
2834 if ($resqlupdate) {
2835 $contractlineprocessed[$obj->lid] = $object->ref;
2836
2837 $actioncode = 'RENEW_CONTRACT';
2838 $now = dol_now();
2839
2840 // Create an event
2841 $actioncomm = new ActionComm($this->db);
2842 $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2843 $actioncomm->code = 'AC_'.$actioncode;
2844 $actioncomm->label = $label;
2845 $actioncomm->datep = $now;
2846 $actioncomm->datef = $now;
2847 $actioncomm->percentage = -1; // Not applicable
2848 $actioncomm->socid = $object->socid;
2849 $actioncomm->authorid = $user->id; // User saving action
2850 $actioncomm->userownerid = $user->id; // Owner of action
2851 $actioncomm->fk_element = $object->id;
2852 $actioncomm->elementid = $object->id;
2853 $actioncomm->elementtype = 'contract';
2854 $actioncomm->note_private = $comment;
2855
2856 $ret = $actioncomm->create($user); // User creating action
2857 } else {
2858 $contracterror[$object->id] = $object->ref;
2859
2860 $error++;
2861 $errorforlocaltransaction++;
2862 $this->error = $this->db->lasterror();
2863 }
2864
2865 if (! $errorforlocaltransaction) {
2866 $this->db->commit();
2867 } else {
2868 $this->db->rollback();
2869 }
2870 } else {
2871 $error++;
2872 $this->error = "Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate." enddatetoscan=".$enddatetoscan." duration_value=".$duration_value." duration_unit=".$duration_value;
2873 dol_syslog($this->error, LOG_ERR);
2874 }
2875 }
2876 }
2877 $i++;
2878 }
2879 } else {
2880 $error++;
2881 $this->error = $this->db->lasterror();
2882 }
2883
2884 $this->output .= count($contractlineprocessed).' contract line(s) with end date before '.dol_print_date($enddatetoscan, 'day').' were renewed'.(count($contractlineprocessed) > 0 ? ' : '.implode(',', $contractlineprocessed) : '');
2885
2886 return ($error ? 1 : 0);
2887 }
2888
2896 public static function contractCmpDate($a, $b)
2897 {
2898 if ($a->date == $b->date) {
2899 return strcmp((string) $a->id, (string) $b->id);
2900 }
2901 return ($a->date < $b->date) ? -1 : 1;
2902 }
2903
2911 public function getKanbanView($option = '', $arraydata = null)
2912 {
2913 global $langs;
2914
2915 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2916
2917 $return = '<div class="box-flex-item box-flex-grow-zero">';
2918 $return .= '<div class="info-box info-box-sm">';
2919 $return .= '<span class="info-box-icon bg-infobox-action">';
2920 $return .= img_picto('', $this->picto);
2921 $return .= '</span>';
2922 $return .= '<div class="info-box-content">';
2923 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2924 if ($selected >= 0) {
2925 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2926 }
2927 if (!empty($arraydata['thirdparty'])) {
2928 $tmpthirdparty = $arraydata['thirdparty'];
2929 '@phan-var-force Societe $tmpthirdparty';
2930 $return .= '<br><div class="info-box-label inline-block valignmiddle">'.$tmpthirdparty->getNomUrl(1).'</div>';
2931 }
2932 if (property_exists($this, 'date_contrat')) {
2933 $return .= '<br><span class="opacitymedium valignmiddle">'.$langs->trans("DateContract").' : </span><span class="info-box-label valignmiddle">'.dol_print_date($this->date_contrat, 'day').'</span>';
2934 }
2935 if (method_exists($this, 'getLibStatut')) {
2936 $return .= '<br><div class="info-box-status valignmiddle">'.$this->getLibStatut(7).'</div>';
2937 }
2938 $return .= '</div>';
2939 $return .= '</div>';
2940 $return .= '</div>';
2941
2942 return $return;
2943 }
2944
2945 // @Todo getLibSignedStatus, LibSignedStatus
2946
2956 public function setSignedStatus(User $user, int $status = 0, int $notrigger = 0, $triggercode = ''): int
2957 {
2958 return $this->setSignedStatusCommon($user, $status, $notrigger, $triggercode);
2959 }
2960}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
$object ref
Definition info.php:90
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
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.
fetchProject()
Load the project with id $this->fk_project into this->project.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
getIdContact($source, $code, $status=0)
Return id of contacts for a source and a contact code.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
static contractCmpDate($a, $b)
Used to sort lines by date.
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
fetch($id, $ref='', $ref_customer='', $ref_supplier='', $noextrafields=0, $nolines=0)
Load a contract from database.
loadStateBoard()
Load the indicators this->nb for state board.
update_statut($user)
Update statut of contract according to services.
updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0.0, $localtax2tx=0.0, $date_start_real='', $date_end_real='', $price_base_type='HT', $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Mets a jour une ligne de contrat.
getTicketsArray()
Create an array of associated tickets.
setSignedStatus(User $user, int $status=0, int $notrigger=0, $triggercode='')
Set signed status.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
active_line($user, $line_id, $date_start, $date_end='', $comment='')
Activate a contract line.
getTooltipContentArray($params)
getTooltipContentArray
getLinesArray()
Create an array of order lines.
validate(User $user, $force_number='', $notrigger=0)
Validate a contract.
getIdBillingContact()
Return id des contacts clients de facturation.
initAsSpecimen()
Initialise an instance with random values.
close_line($user, $line_id, $date_end, $comment='')
Close a contract line.
reopen($user, $notrigger=0)
Unvalidate a contract.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
deleteLine($idline, User $user)
Delete a contract line.
array_detail($status=-1)
Return list of line rowid.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Ajoute une ligne de contrat en base.
update($user, $notrigger=0)
Update object into database.
closeAll(User $user, $notrigger=0, $comment='')
Close all lines of a contract.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getListOfContracts($option='all', $status=[], $product_categories=[], $line_status=[])
Return list of other contracts for the same company than current contract.
create($user, $notrigger=0)
Create a contract into database.
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
LibStatut($status, $mode)
Return the label of a given contrat status.
activateAll($user, $date_start='', $notrigger=0, $comment='', $date_end='')
Open all lines of a contract.
__construct($db)
Constructor.
doAutoRenewContracts($thirdparty_id=0, $delayindaysshort=0)
Action executed by scheduler CAN BE A CRON TASK Loop on each contract lines and update the end of dat...
getNomUrl($withpicto=0, $maxlength=0, $notooltip=0, $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
info($id)
Charge les information d'ordre info dans l'objet contrat.
getLibStatut($mode)
Return label of a contract status.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
fetch_lines($only_services=0, $loadalsotranslation=0, $noextrafields=0)
Load lines array into this->lines.
getIdServiceContact()
Return id des contacts clients de prestation.
getNextNumRef($soc)
Return next contract ref.
Class to manage lines of contracts.
Class to manage Dolibarr database access.
const STATUS_DRAFT
Draft status.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
setSignedStatusCommon(User $user, int $status, int $notrigger=0, string $triggercode='')
Set signed status & call trigger with context message.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
setEntity($currentobject)
Set entity id to use when to create an object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
Class to generate the form for creating a new ticket.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:90