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';
51 use CommonSignedObject;
56 public $element =
'contrat';
61 public $table_element =
'contrat';
66 public $table_element_line =
'contratdet';
71 public $fk_element =
'fk_contrat';
76 public $picto =
'contract';
82 public $restrictiononfksoc = 1;
104 public $ref_supplier;
150 public $fk_user_author;
157 public $user_author_id;
164 public $user_creation;
169 public $user_cloture;
174 public $date_contrat;
179 public $commercial_signature_id;
183 public $fk_commercial_signature;
187 public $commercial_suivi_id;
191 public $fk_commercial_suivi;
203 public $extraparams = array();
208 public $lines = array();
213 public $nbofservices;
217 public $nbofserviceswait;
221 public $nbofservicesopened;
225 public $nbofservicesexpired;
229 public $nbofservicesclosed;
237 protected $lines_id_index_mapper = array();
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'))
295 const STATUS_DRAFT = 0;
296 const STATUS_VALIDATED = 1;
297 const STATUS_CLOSED = 2;
308 $this->ismultientitymanaged = 1;
309 $this->isextrafieldmanaged = 1;
320 global $db, $langs,
$conf;
321 $langs->load(
"contracts");
330 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
332 foreach ($dirmodels as $reldir) {
336 $mybool = ((bool) @include_once $dir.$file) || $mybool;
344 $obj =
new $classname();
345 '@phan-var-force ModelNumRefContracts $obj';
346 $numref = $obj->getNextValue($soc, $this);
351 $this->error = $obj->error;
356 $langs->load(
"errors");
357 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Contract"));
373 public function active_line($user, $line_id, $date_start, $date_end =
'', $comment =
'')
376 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
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;
395 public function close_line($user, $line_id, $date_end, $comment =
'')
398 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
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;
418 public function activateAll($user, $date_start =
'', $notrigger = 0, $comment =
'', $date_end =
'')
420 if (empty($date_start)) {
431 foreach ($this->lines as $contratline) {
433 if ($contratline->statut != ContratLigne::STATUS_OPEN) {
434 $contratline->context = $this->context;
436 $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment);
439 $this->error = $contratline->error;
440 $this->errors = $contratline->errors;
446 if (!$error && $this->statut == 0) {
447 $result = $this->
validate($user,
'', $notrigger);
457 $this->db->rollback();
485 foreach ($this->lines as $contratline) {
487 if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
488 $contratline->date_end_real = $now;
489 $contratline->date_cloture = $now;
490 $contratline->user_closing_id = $user->id;
491 $contratline->statut = ContratLigne::STATUS_CLOSED;
493 $result = $contratline->close_line($user, $now, $comment, $notrigger);
497 $this->error = $contratline->error.($contratline->error ?
' ('.$this->ref.
')' :
'');
498 $this->errors = $contratline->errors;
504 if (!$error && $this->
status == 0) {
505 $result = $this->
validate($user,
'', $notrigger);
518 $this->db->rollback();
534 public function validate(
User $user, $force_number =
'', $notrigger = 0)
536 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
542 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number);
550 if (!
getDolGlobalString(
'CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION') && $this->thirdparty->fournisseur == 0) {
551 $result = $this->thirdparty->setAsCustomer();
556 $num = $force_number;
557 } elseif (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
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";
568 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
569 $resql = $this->db->query($sql);
573 $this->error = $this->db->lasterror();
577 if (!$error && !$notrigger) {
579 $result = $this->
call_trigger(
'CONTRACT_VALIDATE', $user);
587 $this->oldref = $this->ref;
590 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
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);
597 $this->error = $this->db->lasterror();
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);
604 $this->error = $this->db->lasterror();
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);
615 if (@rename($dirsource, $dirdest)) {
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);
634 $this->
status = self::STATUS_VALIDATED;
635 $this->statut = self::STATUS_VALIDATED;
636 $this->date_validation = $now;
646 $this->db->rollback();
658 public function reopen($user, $notrigger = 0)
660 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
665 dol_syslog(get_class($this).
'::reopen user='.$user->id);
671 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET statut = 0";
673 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 1";
675 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
676 $resql = $this->db->query($sql);
680 $this->error = $this->db->lasterror();
684 if (!$error && !$notrigger) {
686 $result = $this->
call_trigger(
'CONTRACT_REOPEN', $user);
695 $this->statut = self::STATUS_DRAFT;
696 $this->
status = self::STATUS_DRAFT;
697 $this->date_validation = $now;
704 $this->db->rollback();
720 public function fetch($id, $ref =
'', $ref_customer =
'', $ref_supplier =
'', $noextrafields = 0, $nolines = 0)
724 $sql =
"SELECT rowid, statut as status, ref, fk_soc as thirdpartyid,";
725 $sql .=
" ref_supplier, ref_customer,";
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";
736 $sql .=
" WHERE entity IN (".getEntity(
'contract').
")";
738 $sql .=
" WHERE rowid = ".(int) $id;
741 $sql .=
" AND ref_customer = '".$this->db->escape($ref_customer).
"'";
744 $sql .=
" AND ref_supplier = '".$this->db->escape($ref_supplier).
"'";
747 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
750 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
752 $resql = $this->db->query($sql);
754 $num = $this->db->num_rows($resql);
756 $this->error =
'Fetch found several records.';
761 $obj = $this->db->fetch_object($resql);
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;
773 $this->date_contrat = $this->db->jdate($obj->datecontrat);
774 $this->date_creation = $this->db->jdate($obj->datecontrat);
776 $this->user_author_id = $obj->fk_user_author;
778 $this->commercial_signature_id = $obj->fk_commercial_signature;
779 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
781 $this->note_private = $obj->note_private;
782 $this->note_public = $obj->note_public;
783 $this->model_pdf = $obj->model_pdf;
785 $this->fk_projet = $obj->fk_project;
786 $this->fk_project = $obj->fk_project;
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);
793 $this->db->free($resql);
797 if (empty($noextrafields)) {
800 $this->error = $this->db->lasterror();
806 if (empty($nolines)) {
807 if ($result >= 0 && !empty($this->table_element_line)) {
810 $this->error = $this->db->lasterror();
818 dol_syslog(get_class($this).
"::fetch Contract failed");
819 $this->error =
"Fetch contract failed";
823 dol_syslog(get_class($this).
"::fetch Contract not found");
824 $this->error =
"Contract not found";
828 dol_syslog(get_class($this).
"::fetch Error searching contract");
829 $this->error = $this->db->error();
844 public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0)
847 $this->nbofservices = 0;
848 $this->nbofserviceswait = 0;
849 $this->nbofservicesopened = 0;
850 $this->nbofservicesexpired = 0;
851 $this->nbofservicesclosed = 0;
859 $this->lines = array();
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,";
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";
887 $sql .=
" ORDER by d.rang ASC";
889 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
890 $result = $this->db->query($sql);
892 $num = $this->db->num_rows($result);
896 $objp = $this->db->fetch_object($result);
900 $line->id = $objp->rowid;
901 $line->ref = $objp->rowid;
902 $line->fk_contrat = $objp->fk_contrat;
903 $line->desc = $objp->description;
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;
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;
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];
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;
933 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
935 $line->ref = $objp->product_ref;
936 $line->product_ref = $objp->product_ref;
937 $line->product_type = $objp->product_type;
938 $line->product_desc = $objp->product_desc;
939 $line->product_label = $objp->product_label;
941 $line->description = $objp->description;
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);
957 $line->rang = $objp->rang;
961 if (empty($noextrafields)) {
962 $line->fetch_optionals();
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();
971 $line->multilangs = $tmpproduct->multilangs;
974 $this->lines[$pos] = $line;
976 $this->lines_id_index_mapper[$line->id] = $pos;
981 if ($line->statut == ContratLigne::STATUS_INITIAL) {
982 $this->nbofserviceswait++;
984 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
985 $this->nbofservicesopened++;
987 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
988 $this->nbofservicesexpired++;
990 if ($line->statut == ContratLigne::STATUS_CLOSED) {
991 $this->nbofservicesclosed++;
995 $total_ttc += $objp->total_ttc;
996 $total_vat += $objp->total_tva;
997 $total_ht += $objp->total_ht;
1002 $this->db->free($result);
1004 dol_syslog(get_class($this).
"::Fetch Error when reading lines of contracts linked to products");
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);
1014 return $this->lines;
1024 public function create($user, $notrigger = 0)
1026 global
$conf, $langs;
1030 if ($this->commercial_signature_id <= 0) {
1031 $langs->load(
"commercial");
1032 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeSignature"));
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"));
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");
1069 $resql = $this->db->query($sql);
1074 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contrat");
1078 if (substr($module, 0, 13) ==
'mod_contract_' && substr($module, -3) ==
'php') {
1079 $module = substr($module, 0,
dol_strlen($module) - 4);
1083 $modCodeContract =
new $module();
1084 '@phan-var-force ModelNumRefContracts $modCodeContrat';
1086 if (!empty($modCodeContract->code_auto)) {
1088 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"contrat SET ref='(PROV".$this->
id.
")' WHERE rowid=".((int) $this->
id);
1089 if ($this->db->query($sql)) {
1091 $this->
ref =
"(PROV".$this->id.
")";
1106 $result = $this->
add_contact($this->commercial_signature_id,
'SALESREPSIGN',
'internal');
1114 $result = $this->
add_contact($this->commercial_suivi_id,
'SALESREPFOLL',
'internal');
1121 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1122 $this->linked_objects = $this->linkedObjectsIds;
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)) {
1129 foreach ($tmp_origin_id as $origin_id) {
1132 $this->error = $this->db->lasterror();
1137 $origin_id = $tmp_origin_id;
1140 $this->error = $this->db->lasterror();
1147 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1148 $originforcontact = $this->origin;
1149 $originidforcontact = $this->origin_id;
1150 if ($originforcontact ==
'shipping') {
1151 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
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;
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).
"'";
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'))) {
1174 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1182 if (!$error && !$notrigger) {
1184 $result = $this->
call_trigger(
'CONTRACT_CREATE', $user);
1192 $this->db->commit();
1195 $this->error =
"Failed to add contract";
1196 dol_syslog(get_class($this).
"::create - 20 - ".$this->error, LOG_ERR);
1197 $this->db->rollback();
1201 $this->error = $langs->trans(
"UnknownError").
": ".$this->db->error();
1202 dol_syslog(get_class($this).
"::create - 10 - ".$this->error, LOG_ERR);
1204 $this->db->rollback();
1216 public function delete($user)
1220 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1227 $result = $this->
call_trigger(
'CONTRACT_DELETE', $user);
1237 dol_syslog(get_class($this).
"::delete error", LOG_ERR);
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).
")";
1257 dol_syslog(get_class($this).
"::delete contratdet_extrafields", LOG_DEBUG);
1258 $resql = $this->db->query($sql);
1260 $this->error = $this->db->error();
1267 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet";
1268 $sql .=
" WHERE fk_contrat=".((int) $this->
id);
1270 dol_syslog(get_class($this).
"::delete contratdet", LOG_DEBUG);
1271 $resql = $this->db->query($sql);
1273 $this->error = $this->db->error();
1289 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contrat";
1290 $sql .=
" WHERE rowid=".((int) $this->
id);
1292 dol_syslog(get_class($this).
"::delete contrat", LOG_DEBUG);
1293 $resql = $this->db->query($sql);
1295 $this->error = $this->db->error();
1305 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
1312 if ($conf->contrat->dir_output) {
1313 $dir =
$conf->contrat->multidir_output[$this->entity].
"/".$ref;
1314 if (file_exists($dir)) {
1317 $this->error =
'ErrorFailToDeleteDir';
1325 $this->db->commit();
1328 $this->error = $this->db->lasterror();
1329 $this->db->rollback();
1341 public function update($user, $notrigger = 0)
1347 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1348 $this->fk_commercial_signature = $this->commercial_signature_id;
1350 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1351 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1353 if (empty($this->socid) && $this->fk_soc > 0) {
1354 $this->socid = (int) $this->fk_soc;
1356 if (empty($this->fk_project) && $this->projet > 0) {
1357 $this->fk_project = (int) $this->projet;
1360 if (isset($this->
ref)) {
1361 $this->
ref = trim($this->
ref);
1363 if (isset($this->ref_customer)) {
1364 $this->ref_customer = trim($this->ref_customer);
1366 if (isset($this->ref_supplier)) {
1367 $this->ref_supplier = trim($this->ref_supplier);
1369 if (isset($this->ref_ext)) {
1370 $this->ref_ext = trim($this->ref_ext);
1372 if (isset($this->entity)) {
1373 $this->entity = (int) $this->entity;
1375 if (isset($this->
status)) {
1378 if (isset($this->socid)) {
1379 $this->socid = (int) $this->socid;
1381 if (isset($this->fk_commercial_signature)) {
1382 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1384 if (isset($this->fk_commercial_suivi)) {
1385 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1387 if (isset($this->note_private)) {
1388 $this->note_private = trim($this->note_private);
1390 if (isset($this->note_public)) {
1391 $this->note_public = trim($this->note_public);
1393 if (isset($this->import_key)) {
1394 $this->import_key = trim($this->import_key);
1397 $extraparams = (!empty($this->extraparams) ? json_encode($this->extraparams) :
null);
1398 $extraparams =
dol_trunc($extraparams, 250);
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);
1423 $resql = $this->db->query($sql);
1426 $this->errors[] =
"Error ".$this->db->lasterror();
1436 if (!$error && !$notrigger) {
1438 $result = $this->
call_trigger(
'CONTRACT_MODIFY', $user);
1447 foreach ($this->errors as $errmsg) {
1448 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1449 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1451 $this->db->rollback();
1454 $this->db->commit();
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)
1485 global $user, $langs,
$conf, $mysoc;
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");
1491 if ($fk_product <= 0 && empty($desc)) {
1492 $this->error =
"ErrorDescRequiredForFreeProductLines";
1496 if ($this->statut >= 0) {
1505 if (preg_match(
'/\((.*)\)/', (
string) $txtva, $reg)) {
1506 $vat_src_code = $reg[1];
1507 $txtva = preg_replace(
'/\s*\(.*\)/',
'', (
string) $txtva);
1513 $remise_percent =
price2num($remise_percent);
1518 if (empty($info_bits)) {
1521 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1524 if (empty($pu_ttc)) {
1527 if (empty($txtva) || !is_numeric($txtva)) {
1530 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1533 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1537 if ($price_base_type ==
'HT') {
1544 if (empty($remise_percent)) {
1545 $remise_percent = 0;
1551 if ($date_start && $date_end && $date_start > $date_end) {
1552 $langs->load(
"errors");
1553 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1559 $localtaxes_type =
getLocalTaxesFromRate($txtva.($vat_src_code ?
' ('.$vat_src_code.
')' :
''), 0, $this->societe, $mysoc);
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];
1573 if (count($localtaxes_type) > 0) {
1574 $localtax1_type = $localtaxes_type[0];
1575 $localtax2_type = $localtaxes_type[2];
1577 $localtax1_type =
"";
1578 $localtax2_type =
"";
1581 if (empty($pa_ht)) {
1588 $result = $this->
defineBuyPrice($pu_ht, $remise_percent, $fk_product);
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";
1606 if ($date_end > 0) {
1607 $sql .=
",date_fin_validite";
1609 $sql .=
", fk_unit";
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).
",";
1624 $sql .=
" ".((int) $info_bits).
",";
1625 if (isset($fk_fournprice)) {
1626 $sql .=
' '.((int) $fk_fournprice).
',';
1630 if (isset($pa_ht)) {
1631 $sql .=
' '.price2num($pa_ht);
1635 if ($date_start > 0) {
1636 $sql .=
",'".$this->db->idate($date_start).
"'";
1638 if ($date_end > 0) {
1639 $sql .=
",'".$this->db->idate($date_end).
"'";
1641 $sql .=
", ".($fk_unit ?
"'".$this->db->escape((
string) $fk_unit).
"'" :
"null");
1642 $sql .=
", ".(!empty($rang) ? (int) $rang :
"0");
1645 $resql = $this->db->query($sql);
1647 $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contratdet");
1651 $contractline->array_options = $array_options;
1652 $contractline->id = $contractlineid;
1653 $result = $contractline->insertExtraFields();
1655 $this->errors = array_merge($this->errors, $contractline->errors);
1656 $this->error = $contractline->error;
1661 if (empty($error)) {
1663 $this->context[
'line_id'] = $contractlineid;
1664 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
1672 $this->db->rollback();
1675 $this->db->commit();
1676 return $contractlineid;
1679 $this->db->rollback();
1680 $this->error = $this->db->error().
" sql=".$sql;
1684 dol_syslog(get_class($this).
"::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
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)
1715 global $user,
$conf, $langs, $mysoc;
1720 $qty = trim((
string) $qty);
1721 $desc = trim($desc);
1722 $desc = trim($desc);
1728 if (empty($fk_fournprice)) {
1735 if ($date_start && $date_end && $date_start > $date_end) {
1736 $langs->load(
"errors");
1737 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
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");
1751 $tvatx = preg_replace(
'/\s*\(.*\)/',
'', $tvatx);
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];
1760 $localtax1_type = (empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0]);
1761 $localtax2_type = (empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2]);
1763 if (empty($pa_ht)) {
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).
"'";
1796 $sql .=
",date_ouverture_prevue = null";
1798 if ($date_end > 0) {
1799 $sql .=
",date_fin_validite = '".$this->db->idate($date_end).
"'";
1801 $sql .=
",date_fin_validite = null";
1803 if ($date_start_real > 0) {
1804 $sql .=
",date_ouverture = '".$this->db->idate($date_start_real).
"'";
1806 $sql .=
",date_ouverture = null";
1808 if ($date_end_real > 0) {
1809 $sql .=
",date_cloture = '".$this->db->idate($date_end_real).
"'";
1811 $sql .=
",date_cloture = null";
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);
1817 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1818 $result = $this->db->query($sql);
1820 if (is_array($array_options) && count($array_options) > 0) {
1822 $contractline->fetch($rowid);
1825 foreach ($array_options as $key => $value) {
1826 $contractline->array_options[$key] = $array_options[$key];
1829 $result = $contractline->insertExtraFields();
1831 $this->errors[] = $contractline->error;
1836 if (empty($error)) {
1838 $this->context[
'line_id'] = $rowid;
1839 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
1841 $this->db->rollback();
1846 $this->db->commit();
1849 $this->db->rollback();
1853 $this->db->rollback();
1854 $this->error = $this->db->error();
1855 dol_syslog(get_class($this).
"::updateline Erreur -1");
1871 if ($this->statut >= 0) {
1873 $this->context[
'line_id'] = $idline;
1874 $result = $this->
call_trigger(
'LINECONTRACT_DELETE', $user);
1882 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1883 $sql .=
" WHERE rowid = ".((int) $idline);
1885 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
1886 $resql = $this->db->query($sql);
1888 $this->error =
"Error ".$this->db->lasterror();
1895 $contractline->id = $idline;
1896 $result = $contractline->deleteExtraFields();
1899 $this->error =
"Error ".get_class($this).
"::deleteline deleteExtraFields error -4 ".$contractline->error;
1903 if (empty($error)) {
1904 $this->db->commit();
1907 dol_syslog(get_class($this).
"::deleteline ERROR:".$this->error, LOG_ERR);
1908 $this->db->rollback();
1912 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1929 dol_syslog(__METHOD__.
" is deprecated", LOG_WARNING);
1932 if ($this->statut == 0) {
1957 return $this->
LibStatut($this->statut, $mode);
1973 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
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');
1984 $statusType =
'status'.$status;
1985 if ($status == self::STATUS_VALIDATED) {
1986 $statusType =
'status6';
1989 if ($mode == 4 || $mode == 6 || $mode == 7) {
1992 $text =
'<span class="hideonsmartphone">';
1993 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1994 $text .=
' '.$langs->trans(
"Services");
1995 $text .=
': ';
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) ?
' ' :
'') :
'';
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) ?
' ' :
'') :
'';
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) ?
' ' :
'') :
'';
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>' :
'');
2008 $text .= is_null($this->signed_status) ?
'' :
' '.$this->getLibSignedStatus(5);
2012 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
2024 global
$conf, $langs, $user;
2026 $langs->load(
'contracts');
2029 $nofetch = !empty($params[
'nofetch']);
2032 return [
'optimize' => $langs->trans(
"ShowContract")];
2034 if ($user->hasRight(
'contrat',
'lire')) {
2035 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Contract").
'</u>';
2040 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.($this->
ref ? $this->
ref : $this->id);
2042 $langs->load(
'companies');
2043 if (empty($this->thirdparty)) {
2046 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
2048 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '. $this->ref_customer;
2050 $langs->load(
'project');
2051 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
2053 if ($res > 0 && $this->project instanceof
Project) {
2054 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0,
'1');
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);
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);
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);
2081 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2083 global
$conf, $langs, $user, $hookmanager;
2087 $url = DOL_URL_ROOT.
'/contrat/card.php?id='.$this->id;
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;
2096 if ($add_save_lastsearch_values) {
2097 $url .=
'&save_lastsearch_values=1';
2102 'objecttype' => $this->element,
2105 $classfortooltip =
'classfortooltip';
2108 $classfortooltip =
'classforajaxtooltip';
2109 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
2116 if (empty($notooltip) && $user->hasRight(
'contrat',
'lire')) {
2118 $label = $langs->trans(
"ShowContract");
2119 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
2121 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
2122 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
2124 $linkstart =
'<a href="'.$url.
'"';
2125 $linkstart .= $linkclose.
'>';
2128 $result .= $linkstart;
2130 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
2132 if ($withpicto != 2) {
2133 $result .= ($this->
ref ? $this->
ref : $this->id);
2135 $result .= $linkend;
2138 $hookmanager->initHooks(array(
'contractdao'));
2139 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
2140 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2142 $result = $hookmanager->resPrint;
2144 $result .= $hookmanager->resPrint;
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);
2164 $result = $this->db->query($sql);
2166 if ($this->db->num_rows($result)) {
2167 $obj = $this->db->fetch_object($result);
2169 $this->
id = $obj->rowid;
2170 $this->
ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
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);
2177 $this->db->free($result);
2195 $sql =
"SELECT cd.rowid";
2196 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as cd";
2197 $sql .=
" WHERE fk_contrat =".((int) $this->
id);
2199 $sql .=
" AND statut = ".((int) $status);
2202 dol_syslog(get_class($this).
"::array_detail()", LOG_DEBUG);
2203 $resql = $this->db->query($sql);
2205 $num = $this->db->num_rows($resql);
2208 $obj = $this->db->fetch_object($resql);
2209 $tab[$i] = $obj->rowid;
2214 $this->error = $this->db->error();
2228 public function getListOfContracts($option =
'all', $status = [], $product_categories = [], $line_status = [])
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)).
")";
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";
2244 dol_syslog(get_class($this).
"::getOtherContracts()", LOG_DEBUG);
2245 $resql = $this->db->query($sql);
2247 $num = $this->db->num_rows($resql);
2250 $obj = $this->db->fetch_object($resql);
2251 $contrat =
new Contrat($this->db);
2252 $contrat->fetch($obj->rowid);
2253 $tab[$contrat->id] = $contrat;
2258 $this->error = $this->db->lasterror();
2275 global
$conf, $langs;
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";
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";
2306 $sql .=
" AND c.fk_soc = s.rowid";
2307 $sql .=
" AND c.entity = ".((int)
$conf->entity);
2309 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
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);
2315 $resql = $this->db->query($sql);
2317 $langs->load(
"contracts");
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");
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");
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;
2348 while ($obj = $this->db->fetch_object($resql)) {
2349 $response->nbtodo++;
2351 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2352 $response->nbtodolate++;
2359 $this->error = $this->db->error();
2371 global
$conf, $user;
2373 $this->nb = array();
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);
2384 $sql .=
" ".$clause.
" c.entity = ".
$conf->entity;
2386 $resql = $this->db->query($sql);
2388 while ($obj = $this->db->fetch_object($resql)) {
2389 $this->nb[
"contracts"] = $obj->nb;
2391 $this->db->free($resql);
2395 $this->error = $this->db->error();
2433 global $user, $langs,
$conf;
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);
2444 $resql = $this->db->query($sql);
2446 $num_prods = $this->db->num_rows($resql);
2448 while ($i < $num_prods) {
2450 $row = $this->db->fetch_row($resql);
2451 $prodids[$i] = $row[0];
2457 $this->specimen = 1;
2459 $this->
ref =
'SPECIMEN';
2460 $this->ref_customer =
'SPECIMENCUST';
2461 $this->ref_supplier =
'SPECIMENSUPP';
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;
2474 while ($xnbp < $nbp) {
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;
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];
2491 $this->lines[$xnbp] = $line;
2517 $ticket =
new Ticket($this->db);
2518 $nbTicket = $ticket->fetchAll($user,
'ASC',
't.datec', 0, 0, 0, array(
't.fk_contract' => $this->
id));
2520 return ($nbTicket < 0 ? $nbTicket : $ticket->lines);
2535 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2537 global
$conf, $langs;
2542 if (!empty($this->model_pdf)) {
2543 $modele = $this->model_pdf;
2549 if (empty($modele)) {
2552 $langs->load(
"contracts");
2553 $outputlangs->load(
"products");
2555 $modelpath =
"core/modules/contract/doc/";
2556 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2604 global $db, $langs,
$conf, $hookmanager, $extrafields;
2610 $this->
fetch($this->
id);
2613 $clonedObj = clone $this;
2614 $clonedObj->socid = $socid;
2618 $objsoc =
new Societe($this->db);
2620 $objsoc->fetch($clonedObj->socid);
2623 $clonedObj->statut = 0;
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);
2630 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
2632 unset($clonedObj->array_options[$key]);
2638 $this->error =
'ErrorSetupNotComplete';
2644 require_once DOL_DOCUMENT_ROOT.
"/core/modules/contract/" .
getDolGlobalString(
'CONTRACT_ADDON').
'.php';
2646 $modContract =
new $obj();
2647 '@phan-var-force ModelNumRefContracts $modContract';
2648 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2651 foreach ($this->lines as $line) {
2652 $line->fetch_optionals($line->id);
2656 $clonedObj->context[
'createfromclone'] =
'createfromclone';
2657 $result = $clonedObj->create($user);
2660 $this->error = $clonedObj->error;
2661 $this->errors[] = $clonedObj->error;
2664 if ($this->socid == $clonedObj->socid) {
2665 if ($clonedObj->copy_linked_contact($this,
'external') < 0) {
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);
2676 $this->error = $clonedObj->error;
2677 $this->errors[] = $clonedObj->error;
2684 if (is_object($hookmanager)) {
2685 $parameters = array(
2687 'clonedObj' => $clonedObj
2690 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $clonedObj, $action);
2698 unset($clonedObj->context[
'createfromclone']);
2702 $this->db->commit();
2703 return $clonedObj->id;
2705 $this->db->rollback();
2722 global $langs, $user;
2724 $langs->load(
"agenda");
2734 $contractlineprocessed = array();
2735 $contractignored = array();
2736 $contracterror = array();
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);
2751 $resql = $this->db->query($sql);
2753 $num = $this->db->num_rows($resql);
2755 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
2759 $obj = $this->db->fetch_object($resql);
2761 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2772 $this->errors[] =
'Failed to load contract with id='.$obj->rowid;
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);
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);
2786 $object->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 1);
2789 if (isset(
$object->linkedObjects[
'facture']) && is_array(
$object->linkedObjects[
'facture']) && count(
$object->linkedObjects[
'facture']) > 0) {
2791 usort(
$object->linkedObjects[
'facture'], array(
'Contrat',
'contractCmpDate'));
2794 $someinvoicenotpaid = 0;
2795 foreach (
$object->linkedObjects[
'facture'] as $idinvoice => $invoice) {
2800 if (empty($invoice->paye)) {
2801 $someinvoicenotpaid++;
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";
2811 if ($expirationdate && $expirationdate < $enddatetoscan) {
2812 dol_syslog(
"Define the newdate of end of services from expirationdate=".$expirationdate);
2813 $newdate = $expirationdate;
2815 while ($newdate < $enddatetoscan && $protecti < 1000) {
2820 if ($protecti < 1000) {
2826 $errorforlocaltransaction = 0;
2828 $label =
'Renewal of contrat '.$object->ref.
' line '.$obj->lid;
2829 $comment =
'Renew date of contract '.$object->ref.
' line '.$obj->lid.
' by doAutoRenewContracts';
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);
2835 $contractlineprocessed[$obj->lid] =
$object->ref;
2837 $actioncode =
'RENEW_CONTRACT';
2842 $actioncomm->type_code =
'AC_OTH_AUTO';
2843 $actioncomm->code =
'AC_'.$actioncode;
2844 $actioncomm->label = $label;
2845 $actioncomm->datep = $now;
2846 $actioncomm->datef = $now;
2847 $actioncomm->percentage = -1;
2848 $actioncomm->socid =
$object->socid;
2849 $actioncomm->authorid = $user->id;
2850 $actioncomm->userownerid = $user->id;
2851 $actioncomm->fk_element =
$object->id;
2852 $actioncomm->elementid =
$object->id;
2853 $actioncomm->elementtype =
'contract';
2854 $actioncomm->note_private = $comment;
2856 $ret = $actioncomm->create($user);
2861 $errorforlocaltransaction++;
2862 $this->error = $this->db->lasterror();
2865 if (! $errorforlocaltransaction) {
2866 $this->db->commit();
2868 $this->db->rollback();
2872 $this->error =
"Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate.
" enddatetoscan=".$enddatetoscan.
" duration_value=".$duration_value.
" duration_unit=".$duration_value;
2881 $this->error = $this->db->lasterror();
2884 $this->output .= count($contractlineprocessed).
' contract line(s) with end date before '.
dol_print_date($enddatetoscan,
'day').
' were renewed'.(count($contractlineprocessed) > 0 ?
' : '.implode(
',', $contractlineprocessed) :
'');
2886 return ($error ? 1 : 0);
2898 if ($a->date == $b->date) {
2899 return strcmp((
string) $a->id, (
string) $b->id);
2901 return ($a->date < $b->date) ? -1 : 1;
2915 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
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">';
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"' :
'').
'>';
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>';
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>';
2935 if (method_exists($this,
'getLibStatut')) {
2936 $return .=
'<br><div class="info-box-status valignmiddle">'.$this->getLibStatut(7).
'</div>';
2938 $return .=
'</div>';
2939 $return .=
'</div>';
2940 $return .=
'</div>';
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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...
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.
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.
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...
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.