37require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
38require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobjectline.class.php";
39require_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
40require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
50 public $element =
'contrat';
55 public $table_element =
'contrat';
60 public $table_element_line =
'contratdet';
65 public $fk_element =
'fk_contrat';
70 public $picto =
'contract';
76 public $restrictiononfksoc = 1;
135 public $fk_user_author;
142 public $user_author_id;
149 public $user_creation;
154 public $user_cloture;
159 public $date_contrat;
165 public $signed_status = 0;
167 public $commercial_signature_id;
168 public $fk_commercial_signature;
169 public $commercial_suivi_id;
170 public $fk_commercial_suivi;
178 public $extraparams = array();
183 public $lines = array();
185 public $nbofservices;
186 public $nbofserviceswait;
187 public $nbofservicesopened;
188 public $nbofservicesexpired;
189 public $nbofservicesclosed;
197 protected $lines_id_index_mapper = array();
228 public $fields = array(
229 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
230 'ref' => array(
'type' =>
'varchar(50)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'showoncombobox' => 1,
'position' => 15,
'searchall' => 1),
231 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'Ref ext',
'enabled' => 1,
'visible' => 0,
'position' => 20),
232 'ref_customer' => array(
'type' =>
'varchar(50)',
'label' =>
'RefCustomer',
'enabled' => 1,
'visible' => -1,
'position' => 25,
'searchall' => 1),
233 'ref_supplier' => array(
'type' =>
'varchar(50)',
'label' =>
'RefSupplier',
'enabled' => 1,
'visible' => -1,
'position' => 26,
'searchall' => 1),
234 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 30,
'index' => 1),
235 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 35),
236 'datec' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -1,
'position' => 40),
237 'date_contrat' => array(
'type' =>
'datetime',
'label' =>
'Date contrat',
'enabled' => 1,
'visible' => -1,
'position' => 45),
238 'signed_status' => array(
'type' =>
'smallint(6)',
'label' =>
'SignedStatus',
'enabled' => 1,
'visible' => -1,
'position' => 50,
'arrayofkeyval' => array(0 =>
'NoSignature', 1 =>
'SignedSender', 2 =>
'SignedReceiver', 9 =>
'SignedAll')),
239 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 70),
240 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 75),
241 'fk_commercial_signature' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'SaleRepresentative Signature',
'enabled' => 1,
'visible' => -1,
'position' => 80),
242 'fk_commercial_suivi' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'SaleRepresentative follower',
'enabled' => 1,
'visible' => -1,
'position' => 85),
243 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 90),
244 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 105,
'searchall' => 1),
245 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 110,
'searchall' => 1),
246 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'Model pdf',
'enabled' => 1,
'visible' => 0,
'position' => 115),
247 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 120),
248 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 125),
249 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 135),
250 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'Last main doc',
'enabled' => 1,
'visible' => -1,
'position' => 140),
251 'statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Statut',
'enabled' => 1,
'visible' => -1,
'position' => 500,
'notnull' => 1,
'arrayofkeyval' => array(0 =>
'Draft', 1 =>
'Validated', 2 =>
'Closed'))
255 const STATUS_DRAFT = 0;
256 const STATUS_VALIDATED = 1;
257 const STATUS_CLOSED = 2;
262 const STATUS_NO_SIGNATURE = 0;
267 const STATUS_SIGNED_SENDER = 1;
272 const STATUS_SIGNED_RECEIVER = 2;
277 const STATUS_SIGNED_ALL = 9;
289 $this->ismultientitymanaged = 1;
290 $this->isextrafieldmanaged = 1;
301 global $db, $langs, $conf;
302 $langs->load(
"contracts");
311 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
313 foreach ($dirmodels as $reldir) {
317 $mybool = ((bool) @include_once $dir.$file) || $mybool;
325 $obj =
new $classname();
326 '@phan-var-force CommonNumRefGenerator $obj';
327 $numref = $obj->getNextValue($soc, $this);
332 $this->error = $obj->error;
337 $langs->load(
"errors");
338 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Contract"));
354 public function active_line($user, $line_id, $date_start, $date_end =
'', $comment =
'')
357 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
359 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
360 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
376 public function close_line($user, $line_id, $date_end, $comment =
'')
379 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
381 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
382 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
399 public function activateAll($user, $date_start =
'', $notrigger = 0, $comment =
'', $date_end =
'')
401 if (empty($date_start)) {
412 foreach ($this->lines as $contratline) {
414 if ($contratline->statut != ContratLigne::STATUS_OPEN) {
415 $contratline->context = $this->context;
417 $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment);
420 $this->error = $contratline->error;
421 $this->errors = $contratline->errors;
427 if (!$error && $this->
statut == 0) {
428 $result = $this->
validate($user,
'', $notrigger);
438 $this->db->rollback();
463 foreach ($this->lines as $contratline) {
465 if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
466 $contratline->date_end_real = $now;
467 $contratline->date_cloture = $now;
468 $contratline->user_closing_id = $user->id;
469 $contratline->statut = ContratLigne::STATUS_CLOSED;
470 $result = $contratline->close_line($user, $now, $comment, $notrigger);
473 $this->error = $contratline->error;
474 $this->errors = $contratline->errors;
480 if (!$error && $this->
statut == 0) {
481 $result = $this->
validate($user,
'', $notrigger);
491 $this->db->rollback();
504 public function validate(
User $user, $force_number =
'', $notrigger = 0)
506 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
512 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number);
520 if (!
getDolGlobalString(
'CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION') && $this->thirdparty->fournisseur == 0) {
521 $result = $this->thirdparty->setAsCustomer();
526 $num = $force_number;
527 } elseif (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
535 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET ref = '".$this->db->escape($num).
"', statut = 1";
537 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 0";
539 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
540 $resql = $this->db->query($sql);
544 $this->error = $this->db->lasterror();
548 if (!$error && !$notrigger) {
550 $result = $this->
call_trigger(
'CONTRACT_VALIDATE', $user);
558 $this->oldref = $this->ref;
561 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
563 $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).
"'";
564 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'contract/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
565 $resql = $this->db->query($sql);
568 $this->error = $this->db->lasterror();
570 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'contract/".$this->db->escape($this->newref).
"'";
571 $sql .=
" WHERE filepath = 'contract/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
572 $resql = $this->db->query($sql);
575 $this->error = $this->db->lasterror();
581 $dirsource = $conf->contract->dir_output.
'/'.$oldref;
582 $dirdest = $conf->contract->dir_output.
'/'.$newref;
583 if (!$error && file_exists($dirsource)) {
584 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
586 if (@rename($dirsource, $dirdest)) {
589 $listoffiles =
dol_dir_list($conf->contract->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
590 foreach ($listoffiles as $fileentry) {
591 $dirsource = $fileentry[
'name'];
592 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
593 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
594 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
595 @rename($dirsource, $dirdest);
605 $this->
status = self::STATUS_VALIDATED;
606 $this->
statut = self::STATUS_VALIDATED;
607 $this->date_validation = $now;
617 $this->db->rollback();
629 public function reopen($user, $notrigger = 0)
631 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
636 dol_syslog(get_class($this).
'::reopen user='.$user->id);
642 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET statut = 0";
644 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 1";
646 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
647 $resql = $this->db->query($sql);
651 $this->error = $this->db->lasterror();
655 if (!$error && !$notrigger) {
657 $result = $this->
call_trigger(
'CONTRACT_REOPEN', $user);
666 $this->
statut = self::STATUS_DRAFT;
667 $this->
status = self::STATUS_DRAFT;
668 $this->date_validation = $now;
675 $this->db->rollback();
691 public function fetch($id, $ref =
'', $ref_customer =
'', $ref_supplier =
'', $noextrafields = 0, $nolines = 0)
693 $sql =
"SELECT rowid, statut as status, ref, fk_soc as thirdpartyid,";
694 $sql .=
" ref_supplier, ref_customer,";
697 $sql .=
" date_contrat as datecontrat,";
698 $sql .=
" fk_user_author,";
699 $sql .=
" fk_projet as fk_project,";
700 $sql .=
" fk_commercial_signature, fk_commercial_suivi,";
701 $sql .=
" note_private, note_public, model_pdf, last_main_doc, extraparams";
702 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat";
704 $sql .=
" WHERE entity IN (".getEntity(
'contract').
")";
706 $sql .=
" WHERE rowid = ".(int) $id;
709 $sql .=
" AND ref_customer = '".$this->db->escape($ref_customer).
"'";
712 $sql .=
" AND ref_supplier = '".$this->db->escape($ref_supplier).
"'";
715 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
718 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
719 $resql = $this->db->query($sql);
721 $num = $this->db->num_rows($resql);
723 $this->error =
'Fetch found several records.';
728 $obj = $this->db->fetch_object($resql);
730 $this->
id = $obj->rowid;
731 $this->
ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
732 $this->ref_customer = $obj->ref_customer;
733 $this->ref_supplier = $obj->ref_supplier;
734 $this->ref_ext = $obj->ref_ext;
735 $this->entity = $obj->entity;
736 $this->
statut = $obj->status;
737 $this->
status = $obj->status;
739 $this->date_contrat = $this->db->jdate($obj->datecontrat);
740 $this->date_creation = $this->db->jdate($obj->datecontrat);
742 $this->user_author_id = $obj->fk_user_author;
744 $this->commercial_signature_id = $obj->fk_commercial_signature;
745 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
747 $this->note_private = $obj->note_private;
748 $this->note_public = $obj->note_public;
749 $this->model_pdf = $obj->model_pdf;
751 $this->fk_projet = $obj->fk_project;
752 $this->fk_project = $obj->fk_project;
754 $this->socid = $obj->thirdpartyid;
755 $this->fk_soc = $obj->thirdpartyid;
756 $this->last_main_doc = $obj->last_main_doc;
757 $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) :
null);
759 $this->db->free($resql);
763 if (empty($noextrafields)) {
766 $this->error = $this->db->lasterror();
772 if (empty($nolines)) {
773 if ($result >= 0 && !empty($this->table_element_line)) {
776 $this->error = $this->db->lasterror();
784 dol_syslog(get_class($this).
"::fetch Contract failed");
785 $this->error =
"Fetch contract failed";
789 dol_syslog(get_class($this).
"::fetch Contract not found");
790 $this->error =
"Contract not found";
794 dol_syslog(get_class($this).
"::fetch Error searching contract");
795 $this->error = $this->db->error();
810 public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0)
813 $this->nbofservices = 0;
814 $this->nbofserviceswait = 0;
815 $this->nbofservicesopened = 0;
816 $this->nbofservicesexpired = 0;
817 $this->nbofservicesclosed = 0;
825 $this->lines = array();
829 $sql =
"SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
830 $sql .=
" d.rowid, d.fk_contrat, d.statut as status, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
831 $sql .=
" d.total_ht,";
832 $sql .=
" d.total_tva,";
833 $sql .=
" d.total_localtax1,";
834 $sql .=
" d.total_localtax2,";
835 $sql .=
" d.total_ttc,";
836 $sql .=
" d.info_bits, d.fk_product,";
837 $sql .=
" d.date_ouverture_prevue as date_start,";
838 $sql .=
" d.date_ouverture as date_start_real,";
839 $sql .=
" d.date_fin_validite as date_end,";
840 $sql .=
" d.date_cloture as date_end_real,";
841 $sql .=
" d.fk_user_author,";
842 $sql .=
" d.fk_user_ouverture,";
843 $sql .=
" d.fk_user_cloture,";
844 $sql .=
" d.fk_unit,";
845 $sql .=
" d.product_type as type,";
847 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as d LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON d.fk_product = p.rowid";
848 $sql .=
" WHERE d.fk_contrat = ".((int) $this->
id);
849 if ($only_services == 1) {
850 $sql .=
" AND d.product_type = 1";
852 $sql .=
" ORDER by d.rang ASC";
854 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
855 $result = $this->db->query($sql);
857 $num = $this->db->num_rows($result);
861 $objp = $this->db->fetch_object($result);
865 $line->id = $objp->rowid;
866 $line->ref = $objp->rowid;
867 $line->fk_contrat = $objp->fk_contrat;
868 $line->desc = $objp->description;
869 $line->qty = $objp->qty;
870 $line->vat_src_code = $objp->vat_src_code;
871 $line->tva_tx = $objp->tva_tx;
872 $line->localtax1_tx = $objp->localtax1_tx;
873 $line->localtax2_tx = $objp->localtax2_tx;
874 $line->localtax1_type = $objp->localtax1_type;
875 $line->localtax2_type = $objp->localtax2_type;
876 $line->subprice = $objp->subprice;
877 $line->statut = $objp->status;
878 $line->status = $objp->status;
879 $line->remise_percent = $objp->remise_percent;
880 $line->price_ht = $objp->price_ht;
881 $line->price = $objp->price_ht;
882 $line->total_ht = $objp->total_ht;
883 $line->total_tva = $objp->total_tva;
884 $line->total_localtax1 = $objp->total_localtax1;
885 $line->total_localtax2 = $objp->total_localtax2;
886 $line->total_ttc = $objp->total_ttc;
887 $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
888 $line->info_bits = $objp->info_bits;
889 $line->type = $objp->type;
891 $line->fk_fournprice = $objp->fk_fournprice;
892 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
893 $line->pa_ht = $marginInfos[0];
895 $line->fk_user_author = $objp->fk_user_author;
896 $line->fk_user_ouverture = $objp->fk_user_ouverture;
897 $line->fk_user_cloture = $objp->fk_user_cloture;
898 $line->fk_unit = $objp->fk_unit;
900 $line->ref = $objp->product_ref;
901 $line->product_ref = $objp->product_ref;
902 $line->product_type = $objp->product_type;
903 $line->product_desc = $objp->product_desc;
904 $line->product_label = $objp->product_label;
906 $line->description = $objp->description;
908 $line->date_start = $this->db->jdate($objp->date_start);
909 $line->date_start_real = $this->db->jdate($objp->date_start_real);
910 $line->date_end = $this->db->jdate($objp->date_end);
911 $line->date_end_real = $this->db->jdate($objp->date_end_real);
922 $line->rang = $objp->rang;
926 if (empty($noextrafields)) {
927 $line->fetch_optionals();
931 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
932 $tmpproduct =
new Product($this->db);
933 $tmpproduct->fetch($objp->fk_product);
934 $tmpproduct->getMultiLangs();
936 $line->multilangs = $tmpproduct->multilangs;
939 $this->lines[$pos] = $line;
941 $this->lines_id_index_mapper[$line->id] = $pos;
946 if ($line->statut == ContratLigne::STATUS_INITIAL) {
947 $this->nbofserviceswait++;
949 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
950 $this->nbofservicesopened++;
952 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
953 $this->nbofservicesexpired++;
955 if ($line->statut == ContratLigne::STATUS_CLOSED) {
956 $this->nbofservicesclosed++;
959 $total_ttc += $objp->total_ttc;
960 $total_vat += $objp->total_tva;
961 $total_ht += $objp->total_ht;
966 $this->db->free($result);
968 dol_syslog(get_class($this).
"::Fetch Error when reading lines of contracts linked to products");
973 $this->nbofservices = count($this->lines);
974 $this->total_ttc = (float)
price2num($total_ttc);
975 $this->total_tva = (float)
price2num($total_vat);
976 $this->total_ht = (float)
price2num($total_ht);
989 global $conf, $langs, $mysoc;
993 if ($this->commercial_signature_id <= 0) {
994 $langs->load(
"commercial");
995 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeSignature"));
998 if ($this->commercial_suivi_id <= 0) {
999 $langs->load(
"commercial");
1000 $this->error .= ($this->error ?
"<br>" :
'');
1001 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeFollowUp"));
1014 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contrat (datec, fk_soc, fk_user_author, date_contrat,";
1015 $sql .=
" fk_commercial_signature, fk_commercial_suivi, fk_projet,";
1016 $sql .=
" ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
1017 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $this->socid).
", ".((int) $user->id);
1018 $sql .=
", ".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
"NULL");
1019 $sql .=
",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) :
"NULL");
1020 $sql .=
",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) :
"NULL");
1021 $sql .=
",".($this->fk_project > 0 ? ((int) $this->fk_project) :
"NULL");
1022 $sql .=
", ".(dol_strlen($this->
ref) <= 0 ?
"null" :
"'".$this->db->escape($this->
ref).
"'");
1023 $sql .=
", ".((int) $conf->entity);
1024 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
1025 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
1026 $sql .=
", ".(!empty($this->ref_customer) ? (
"'".$this->db->escape($this->ref_customer).
"'") :
"NULL");
1027 $sql .=
", ".(!empty($this->ref_supplier) ? (
"'".$this->db->escape($this->ref_supplier).
"'") :
"NULL");
1028 $sql .=
", ".(!empty($this->ref_ext) ? (
"'".$this->db->escape($this->ref_ext).
"'") :
"NULL");
1030 $resql = $this->db->query($sql);
1035 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contrat");
1038 $module = (
getDolGlobalString(
'CONTRACT_ADDON') ? $conf->global->CONTRACT_ADDON :
'mod_contract_serpis');
1039 if (substr($module, 0, 13) ==
'mod_contract_' && substr($module, -3) ==
'php') {
1040 $module = substr($module, 0,
dol_strlen($module) - 4);
1044 $modCodeContract =
new $module();
1045 '@phan-var-force CommonNumRefGenerator $modCodeContrat';
1047 if (!empty($modCodeContract->code_auto)) {
1049 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"contrat SET ref='(PROV".$this->
id.
")' WHERE rowid=".((int) $this->
id);
1050 if ($this->db->query($sql)) {
1052 $this->
ref =
"(PROV".$this->id.
")";
1067 $result = $this->
add_contact($this->commercial_signature_id,
'SALESREPSIGN',
'internal');
1075 $result = $this->
add_contact($this->commercial_suivi_id,
'SALESREPFOLL',
'internal');
1082 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1083 $this->linked_objects = $this->linkedObjectsIds;
1087 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1088 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1089 if (is_array($tmp_origin_id)) {
1090 foreach ($tmp_origin_id as $origin_id) {
1093 $this->error = $this->db->lasterror();
1098 $origin_id = $tmp_origin_id;
1101 $this->error = $this->db->lasterror();
1108 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1109 $originforcontact = $this->origin;
1110 $originidforcontact = $this->origin_id;
1111 if ($originforcontact ==
'shipping') {
1112 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1114 $exp->fetch($this->origin_id);
1115 $exp->fetchObjectLinked();
1116 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1117 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1118 $originforcontact =
'commande';
1119 $originidforcontact = $value;
1125 $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";
1126 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1128 $resqlcontact = $this->db->query($sqlcontact);
1129 if ($resqlcontact) {
1130 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1131 if ($objcontact->source ==
'internal' && in_array($objcontact->code, array(
'SALESREPSIGN',
'SALESREPFOLL'))) {
1135 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1145 $result = $this->
call_trigger(
'CONTRACT_CREATE', $user);
1152 $this->db->commit();
1155 dol_syslog(get_class($this).
"::create - 30 - ".$this->error, LOG_ERR);
1156 $this->db->rollback();
1160 $this->error =
"Failed to add contract";
1161 dol_syslog(get_class($this).
"::create - 20 - ".$this->error, LOG_ERR);
1162 $this->db->rollback();
1166 $this->error = $langs->trans(
"UnknownError").
": ".$this->db->error();
1167 dol_syslog(get_class($this).
"::create - 10 - ".$this->error, LOG_ERR);
1169 $this->db->rollback();
1181 public function delete($user)
1185 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1192 $result = $this->
call_trigger(
'CONTRACT_DELETE', $user);
1202 dol_syslog(get_class($this).
"::delete error", LOG_ERR);
1218 $main = MAIN_DB_PREFIX.
'contratdet';
1219 $ef = $main.
"_extrafields";
1220 $sql =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_contrat = ".((int) $this->
id).
")";
1222 dol_syslog(get_class($this).
"::delete contratdet_extrafields", LOG_DEBUG);
1223 $resql = $this->db->query($sql);
1225 $this->error = $this->db->error();
1232 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet";
1233 $sql .=
" WHERE fk_contrat=".((int) $this->
id);
1235 dol_syslog(get_class($this).
"::delete contratdet", LOG_DEBUG);
1236 $resql = $this->db->query($sql);
1238 $this->error = $this->db->error();
1245 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"ecm_files WHERE src_object_type = '".$this->db->escape($this->table_element.(empty($this->module) ?
"" :
"@".$this->module)).
"' AND src_object_id = ".((int) $this->
id);
1246 $resql = $this->db->query($sql);
1248 $this->error = $this->db->lasterror();
1249 $this->errors[] = $this->error;
1256 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contrat";
1257 $sql .=
" WHERE rowid=".((int) $this->
id);
1259 dol_syslog(get_class($this).
"::delete contrat", LOG_DEBUG);
1260 $resql = $this->db->query($sql);
1262 $this->error = $this->db->error();
1272 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
1279 if ($conf->contrat->dir_output) {
1280 $dir = $conf->contrat->multidir_output[$this->entity].
"/".$ref;
1281 if (file_exists($dir)) {
1284 $this->error =
'ErrorFailToDeleteDir';
1292 $this->db->commit();
1295 $this->error = $this->db->lasterror();
1296 $this->db->rollback();
1308 public function update($user, $notrigger = 0)
1314 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1315 $this->fk_commercial_signature = $this->commercial_signature_id;
1317 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1318 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1320 if (empty($this->socid) && $this->fk_soc > 0) {
1321 $this->socid = (int) $this->fk_soc;
1323 if (empty($this->fk_project) && $this->projet > 0) {
1324 $this->fk_project = (int) $this->projet;
1327 if (isset($this->
ref)) {
1328 $this->
ref = trim($this->
ref);
1330 if (isset($this->ref_customer)) {
1331 $this->ref_customer = trim($this->ref_customer);
1333 if (isset($this->ref_supplier)) {
1334 $this->ref_supplier = trim($this->ref_supplier);
1336 if (isset($this->ref_ext)) {
1337 $this->ref_ext = trim($this->ref_ext);
1339 if (isset($this->entity)) {
1340 $this->entity = (int) $this->entity;
1342 if (isset($this->
statut)) {
1345 if (isset($this->
status)) {
1348 if (isset($this->socid)) {
1349 $this->socid = (int) $this->socid;
1351 if (isset($this->fk_commercial_signature)) {
1352 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1354 if (isset($this->fk_commercial_suivi)) {
1355 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1357 if (isset($this->note_private)) {
1358 $this->note_private = trim($this->note_private);
1360 if (isset($this->note_public)) {
1361 $this->note_public = trim($this->note_public);
1363 if (isset($this->import_key)) {
1364 $this->import_key = trim($this->import_key);
1371 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET";
1372 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1373 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1374 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1375 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1376 $sql .=
" entity=".$conf->entity.
",";
1377 $sql .=
" date_contrat=".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
'null').
",";
1379 $sql .=
" fk_soc=".($this->socid > 0 ? $this->socid :
"null").
",";
1380 $sql .=
" fk_projet=".($this->fk_project > 0 ? $this->fk_project :
"null").
",";
1381 $sql .=
" fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature :
"null").
",";
1382 $sql .=
" fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi :
"null").
",";
1383 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1384 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1385 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1387 $sql .=
" WHERE rowid=".((int) $this->
id);
1391 $resql = $this->db->query($sql);
1394 $this->errors[] =
"Error ".$this->db->lasterror();
1404 if (!$error && !$notrigger) {
1406 $result = $this->
call_trigger(
'CONTRACT_MODIFY', $user);
1415 foreach ($this->errors as $errmsg) {
1416 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1417 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1419 $this->db->rollback();
1422 $this->db->commit();
1451 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)
1453 global $user, $langs, $conf, $mysoc;
1456 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");
1459 if ($fk_product <= 0 && empty($desc)) {
1460 $this->error =
"ErrorDescRequiredForFreeProductLines";
1464 if ($this->
statut >= 0) {
1473 if (preg_match(
'/\((.*)\)/', (
string) $txtva, $reg)) {
1474 $vat_src_code = $reg[1];
1475 $txtva = preg_replace(
'/\s*\(.*\)/',
'', (
string) $txtva);
1481 $remise_percent =
price2num($remise_percent);
1486 if (empty($info_bits)) {
1489 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1492 if (empty($pu_ttc)) {
1495 if (empty($txtva) || !is_numeric($txtva)) {
1498 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1501 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1505 if ($price_base_type ==
'HT') {
1512 if (empty($remise_percent)) {
1513 $remise_percent = 0;
1519 if ($date_start && $date_end && $date_start > $date_end) {
1520 $langs->load(
"errors");
1521 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1527 $localtaxes_type =
getLocalTaxesFromRate($txtva.($vat_src_code ?
' ('.$vat_src_code.
')' :
''), 0, $this->societe, $mysoc);
1534 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1535 $total_ht = $tabprice[0];
1536 $total_tva = $tabprice[1];
1537 $total_ttc = $tabprice[2];
1538 $total_localtax1 = $tabprice[9];
1539 $total_localtax2 = $tabprice[10];
1541 $localtax1_type = $localtaxes_type[0];
1542 $localtax2_type = $localtaxes_type[2];
1549 $remise = round(($pu_ht * $remise_percent / 100), 2);
1550 $price = $pu_ht - $remise;
1553 if (empty($pa_ht)) {
1560 $result = $this->
defineBuyPrice($pu_ht, $remise_percent, $fk_product);
1569 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
1570 $sql .=
" (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1571 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1572 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1573 $sql .=
" info_bits,";
1574 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1575 if ($date_start > 0) {
1576 $sql .=
",date_ouverture_prevue";
1578 if ($date_end > 0) {
1579 $sql .=
",date_fin_validite";
1581 $sql .=
", fk_unit";
1583 $sql .=
") VALUES (";
1584 $sql .= $this->
id.
", '', '".$this->db->escape($desc).
"',";
1585 $sql .= ($fk_product > 0 ? $fk_product :
"null").
",";
1586 $sql .=
" ".((float) $qty).
",";
1587 $sql .=
" ".((float) $txtva).
",";
1588 $sql .=
" ".($vat_src_code ?
"'".$this->db->escape($vat_src_code).
"'" :
"null").
",";
1589 $sql .=
" ".((float) $txlocaltax1).
",";
1590 $sql .=
" ".((float) $txlocaltax2).
",";
1591 $sql .=
" '".$this->db->escape($localtax1_type).
"',";
1592 $sql .=
" '".$this->db->escape($localtax2_type).
"',";
1593 $sql .=
" ".price2num($remise_percent).
",";
1594 $sql .=
" ".price2num($pu_ht).
",";
1596 $sql .=
" ".((int) $info_bits).
",";
1597 $sql .=
" ".price2num($price).
",".
price2num($remise).
",";
1598 if (isset($fk_fournprice)) {
1599 $sql .=
' '.((int) $fk_fournprice).
',';
1603 if (isset($pa_ht)) {
1604 $sql .=
' '.price2num($pa_ht);
1608 if ($date_start > 0) {
1609 $sql .=
",'".$this->db->idate($date_start).
"'";
1611 if ($date_end > 0) {
1612 $sql .=
",'".$this->db->idate($date_end).
"'";
1614 $sql .=
", ".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1615 $sql .=
", ".(!empty($rang) ? (int) $rang :
"0");
1618 $resql = $this->db->query($sql);
1620 $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contratdet");
1624 $contractline->array_options = $array_options;
1625 $contractline->id = $contractlineid;
1626 $result = $contractline->insertExtraFields();
1628 $this->errors = array_merge($this->errors, $contractline->errors);
1629 $this->error = $contractline->error;
1634 if (empty($error)) {
1636 $this->context[
'line_id'] = $contractlineid;
1637 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
1645 $this->db->rollback();
1648 $this->db->commit();
1649 return $contractlineid;
1652 $this->db->rollback();
1653 $this->error = $this->db->error().
" sql=".$sql;
1657 dol_syslog(get_class($this).
"::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1686 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)
1688 global $user, $conf, $langs, $mysoc;
1693 $qty = trim((
string) $qty);
1694 $desc = trim($desc);
1695 $desc = trim($desc);
1701 if (empty($fk_fournprice)) {
1711 $remise = round(($pu * $remise_percent / 100), 2);
1712 $price = $pu - $remise;
1714 $remise_percent = 0;
1717 if ($date_start && $date_end && $date_start > $date_end) {
1718 $langs->load(
"errors");
1719 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1723 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");
1733 $tvatx = preg_replace(
'/\s*\(.*\)/',
'', $tvatx);
1735 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, (
float)
price2num($tvatx), $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1736 $total_ht = $tabprice[0];
1737 $total_tva = $tabprice[1];
1738 $total_ttc = $tabprice[2];
1739 $total_localtax1 = $tabprice[9];
1740 $total_localtax2 = $tabprice[10];
1742 $localtax1_type = (empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0]);
1743 $localtax2_type = (empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2]);
1750 $remise = round(($pu * $remise_percent / 100), 2);
1751 $price = $pu - $remise;
1754 if (empty($pa_ht)) {
1768 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet set description = '".$this->db->escape($desc).
"'";
1769 $sql .=
",price_ht = ".((float)
price2num($price));
1770 $sql .=
",subprice = ".((float)
price2num($subprice));
1771 $sql .=
",remise = ".((float)
price2num($remise));
1772 $sql .=
",remise_percent = ".((float)
price2num($remise_percent));
1773 $sql .=
",qty = ".((float) $qty);
1774 $sql .=
",tva_tx = ".((float)
price2num($tvatx));
1775 $sql .=
",localtax1_tx = ".((float)
price2num($localtax1tx));
1776 $sql .=
",localtax2_tx = ".((float)
price2num($localtax2tx));
1777 $sql .=
",localtax1_type='".$this->db->escape($localtax1_type).
"'";
1778 $sql .=
",localtax2_type='".$this->db->escape($localtax2_type).
"'";
1779 $sql .=
", total_ht = ".((float)
price2num($total_ht));
1780 $sql .=
", total_tva = ".((float)
price2num($total_tva));
1781 $sql .=
", total_localtax1 = ".((float)
price2num($total_localtax1));
1782 $sql .=
", total_localtax2 = ".((float)
price2num($total_localtax2));
1783 $sql .=
", total_ttc = ".((float)
price2num($total_ttc));
1784 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
"null");
1785 $sql .=
", buy_price_ht = ".((float)
price2num($pa_ht));
1786 if ($date_start > 0) {
1787 $sql .=
",date_ouverture_prevue = '".$this->db->idate($date_start).
"'";
1789 $sql .=
",date_ouverture_prevue = null";
1791 if ($date_end > 0) {
1792 $sql .=
",date_fin_validite = '".$this->db->idate($date_end).
"'";
1794 $sql .=
",date_fin_validite = null";
1796 if ($date_start_real > 0) {
1797 $sql .=
",date_ouverture = '".$this->db->idate($date_start_real).
"'";
1799 $sql .=
",date_ouverture = null";
1801 if ($date_end_real > 0) {
1802 $sql .=
",date_cloture = '".$this->db->idate($date_end_real).
"'";
1804 $sql .=
",date_cloture = null";
1806 $sql .=
", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) :
"null");
1807 $sql .=
", rang = ".(!empty($rang) ? ((int) $rang) :
"0");
1808 $sql .=
" WHERE rowid = ".((int) $rowid);
1810 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1811 $result = $this->db->query($sql);
1813 if (is_array($array_options) && count($array_options) > 0) {
1815 $contractline->fetch($rowid);
1818 foreach ($array_options as $key => $value) {
1819 $contractline->array_options[$key] = $array_options[$key];
1822 $result = $contractline->insertExtraFields();
1824 $this->errors[] = $contractline->error;
1829 if (empty($error)) {
1831 $this->context[
'line_id'] = $rowid;
1832 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
1834 $this->db->rollback();
1839 $this->db->commit();
1842 $this->db->rollback();
1846 $this->db->rollback();
1847 $this->error = $this->db->error();
1848 dol_syslog(get_class($this).
"::updateline Erreur -1");
1864 if ($this->
statut >= 0) {
1866 $this->context[
'line_id'] = $idline;
1867 $result = $this->
call_trigger(
'LINECONTRACT_DELETE', $user);
1875 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1876 $sql .=
" WHERE rowid = ".((int) $idline);
1878 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
1879 $resql = $this->db->query($sql);
1881 $this->error =
"Error ".$this->db->lasterror();
1888 $contractline->id = $idline;
1889 $result = $contractline->deleteExtraFields();
1892 $this->error =
"Error ".get_class($this).
"::deleteline deleteExtraFields error -4 ".$contractline->error;
1896 if (empty($error)) {
1897 $this->db->commit();
1900 dol_syslog(get_class($this).
"::deleteline ERROR:".$this->error, LOG_ERR);
1901 $this->db->rollback();
1905 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1922 dol_syslog(__METHOD__.
" is deprecated", LOG_WARNING);
1925 if ($this->
statut == 0) {
1966 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1968 $langs->load(
"contracts");
1969 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1970 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1971 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1972 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1973 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1974 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1977 $statusType =
'status'.$status;
1978 if ($status == self::STATUS_VALIDATED) {
1979 $statusType =
'status6';
1982 if ($mode == 4 || $mode == 6 || $mode == 7) {
1985 $text =
'<span class="hideonsmartphone">';
1986 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1987 $text .=
' '.$langs->trans(
"Services");
1988 $text .=
': ';
1991 $text .= ($mode == 7 ?
'<span class="nowraponall">' :
'');
1992 $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1993 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1994 $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1995 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1996 $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1997 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1998 $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1,
'class="marginleft2"')) :
'';
1999 $text .= ($mode == 7 ?
'</span>' :
'');
2002 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
2014 global $conf, $langs, $user;
2016 $langs->load(
'contracts');
2019 $nofetch = !empty($params[
'nofetch']);
2022 return [
'optimize' => $langs->trans(
"ShowContract")];
2024 if ($user->hasRight(
'contrat',
'lire')) {
2025 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Contract").
'</u>';
2030 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.($this->
ref ? $this->
ref : $this->id);
2032 $langs->load(
'companies');
2033 if (empty($this->thirdparty)) {
2036 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
2038 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '. $this->ref_customer;
2040 $langs->load(
'project');
2041 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
2043 if ($res > 0 && $this->project instanceof
Project) {
2044 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
2048 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2049 if (!empty($this->total_ht)) {
2050 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2052 if (!empty($this->total_tva)) {
2053 $datas[
'vatamount'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2055 if (!empty($this->total_ttc)) {
2056 $datas[
'amounttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2071 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2073 global $conf, $langs, $user, $hookmanager;
2077 $url = DOL_URL_ROOT.
'/contrat/card.php?id='.$this->id;
2082 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2083 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2084 $add_save_lastsearch_values = 1;
2086 if ($add_save_lastsearch_values) {
2087 $url .=
'&save_lastsearch_values=1';
2092 'objecttype' => $this->element,
2095 $classfortooltip =
'classfortooltip';
2098 $classfortooltip =
'classforajaxtooltip';
2099 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
2106 if (empty($notooltip) && $user->hasRight(
'contrat',
'lire')) {
2108 $label = $langs->trans(
"ShowContract");
2109 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2111 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
2112 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
2114 $linkstart =
'<a href="'.$url.
'"';
2115 $linkstart .= $linkclose.
'>';
2118 $result .= $linkstart;
2120 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
2122 if ($withpicto != 2) {
2123 $result .= ($this->
ref ? $this->
ref : $this->id);
2125 $result .= $linkend;
2128 $hookmanager->initHooks(array(
'contractdao'));
2129 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
2130 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2132 $result = $hookmanager->resPrint;
2134 $result .= $hookmanager->resPrint;
2148 $sql =
"SELECT c.rowid, c.ref, c.datec,";
2149 $sql .=
" c.tms as date_modification,";
2150 $sql .=
" fk_user_author";
2151 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2152 $sql .=
" WHERE c.rowid = ".((int) $id);
2154 $result = $this->db->query($sql);
2156 if ($this->db->num_rows($result)) {
2157 $obj = $this->db->fetch_object($result);
2159 $this->
id = $obj->rowid;
2160 $this->
ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2162 $this->user_creation_id = $obj->fk_user_author;
2163 $this->date_creation = $this->db->jdate($obj->datec);
2164 $this->date_modification = $this->db->jdate($obj->date_modification);
2167 $this->db->free($result);
2185 $sql =
"SELECT cd.rowid";
2186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as cd";
2187 $sql .=
" WHERE fk_contrat =".((int) $this->
id);
2189 $sql .=
" AND statut = ".((int) $status);
2192 dol_syslog(get_class($this).
"::array_detail()", LOG_DEBUG);
2193 $resql = $this->db->query($sql);
2195 $num = $this->db->num_rows($resql);
2198 $obj = $this->db->fetch_object($resql);
2199 $tab[$i] = $obj->rowid;
2204 $this->error = $this->db->error();
2218 public function getListOfContracts($option =
'all', $status = [], $product_categories = [], $line_status = [])
2222 $sql =
"SELECT c.rowid";
2223 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2224 if (!empty($product_categories)) {
2225 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contratdet as cd ON cd.fk_contrat = c.rowid";
2226 $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)).
")";
2228 $sql .=
" WHERE c.fk_soc =".((int) $this->socid);
2229 $sql .= ($option ==
'others') ?
" AND c.rowid <> ".((
int) $this->id) :
"";
2230 $sql .= (!empty($status)) ?
" AND c.statut IN (".$this->db->sanitize(implode(
', ', $status)).
")" :
"";
2231 $sql .= (!empty($line_status)) ?
" AND cd.statut IN (".$this->db->sanitize(implode(
', ', $line_status)).
")" :
"";
2232 $sql .=
" GROUP BY c.rowid";
2234 dol_syslog(get_class($this).
"::getOtherContracts()", LOG_DEBUG);
2235 $resql = $this->db->query($sql);
2237 $num = $this->db->num_rows($resql);
2240 $obj = $this->db->fetch_object($resql);
2241 $contrat =
new Contrat($this->db);
2242 $contrat->fetch($obj->rowid);
2243 $tab[$contrat->id] = $contrat;
2248 $this->error = $this->db->lasterror();
2265 global $conf, $langs;
2267 $this->from =
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2268 $this->from .=
", ".MAIN_DB_PREFIX.
"contratdet as cd";
2269 $this->from .=
", ".MAIN_DB_PREFIX.
"societe as s";
2270 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2271 $this->from .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2274 if ($mode ==
'inactive') {
2275 $sql =
"SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2276 $sql .= $this->from;
2277 $sql .=
" WHERE c.statut = 1";
2278 $sql .=
" AND c.rowid = cd.fk_contrat";
2279 $sql .=
" AND cd.statut = 0";
2280 } elseif ($mode ==
'expired') {
2281 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2282 $sql .= $this->from;
2283 $sql .=
" WHERE c.statut = 1";
2284 $sql .=
" AND c.rowid = cd.fk_contrat";
2285 $sql .=
" AND cd.statut = 4";
2286 $sql .=
" AND cd.date_fin_validite < '".$this->db->idate(
dol_now()).
"'";
2287 } elseif ($mode ==
'active') {
2288 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2289 $sql .= $this->from;
2290 $sql .=
" WHERE c.statut = 1";
2291 $sql .=
" AND c.rowid = cd.fk_contrat";
2292 $sql .=
" AND cd.statut = 4";
2296 $sql .=
" AND c.fk_soc = s.rowid";
2297 $sql .=
" AND c.entity = ".((int) $conf->entity);
2299 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
2301 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2302 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2305 $resql = $this->db->query($sql);
2307 $langs->load(
"contracts");
2310 if ($mode ==
'inactive') {
2311 $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2312 $label = $langs->trans(
"BoardNotActivatedServices");
2313 $labelShort = $langs->trans(
"BoardNotActivatedServicesShort");
2314 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2315 } elseif ($mode ==
'expired') {
2316 $warning_delay = $conf->contrat->services->expires->warning_delay;
2317 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2318 $label = $langs->trans(
"BoardExpiredServices");
2319 $labelShort = $langs->trans(
"BoardExpiredServicesShort");
2321 $warning_delay = $conf->contrat->services->expires->warning_delay;
2322 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2325 $label = $langs->trans(
"BoardRunningServices");
2326 $labelShort = $langs->trans(
"BoardRunningServicesShort");
2330 $response->warning_delay = $warning_delay / 60 / 60 / 24;
2331 $response->label = $label;
2332 $response->labelShort = $labelShort;
2333 $response->url = $url;
2336 while ($obj = $this->db->fetch_object($resql)) {
2337 $response->nbtodo++;
2339 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2340 $response->nbtodolate++;
2347 $this->error = $this->db->error();
2359 global $conf, $user;
2361 $this->nb = array();
2364 $sql =
"SELECT count(c.rowid) as nb";
2365 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2366 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON c.fk_soc = s.rowid";
2367 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2368 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2369 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
2372 $sql .=
" ".$clause.
" c.entity = ".$conf->entity;
2374 $resql = $this->db->query($sql);
2376 while ($obj = $this->db->fetch_object($resql)) {
2377 $this->nb[
"contracts"] = $obj->nb;
2379 $this->db->free($resql);
2383 $this->error = $this->db->error();
2421 global $user, $langs, $conf;
2426 $sql =
"SELECT rowid";
2427 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2428 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2429 $sql .=
" AND tosell = 1";
2430 $sql .= $this->db->plimit(100);
2432 $resql = $this->db->query($sql);
2434 $num_prods = $this->db->num_rows($resql);
2436 while ($i < $num_prods) {
2438 $row = $this->db->fetch_row($resql);
2439 $prodids[$i] = $row[0];
2445 $this->specimen = 1;
2447 $this->
ref =
'SPECIMEN';
2448 $this->ref_customer =
'SPECIMENCUST';
2449 $this->ref_supplier =
'SPECIMENSUPP';
2452 $this->date_creation = (
dol_now() - 3600 * 24 * 7);
2453 $this->date_contrat =
dol_now();
2454 $this->commercial_signature_id = 1;
2455 $this->commercial_suivi_id = 1;
2456 $this->note_private =
'This is a comment (private)';
2457 $this->note_public =
'This is a comment (public)';
2458 $this->fk_project = 0;
2462 while ($xnbp < $nbp) {
2465 $line->subprice = 100;
2466 $line->tva_tx = 19.6;
2467 $line->remise_percent = 10;
2468 $line->total_ht = 90;
2469 $line->total_ttc = 107.64;
2470 $line->total_tva = 17.64;
2471 $line->date_start =
dol_now() - 500000;
2472 $line->date_start_real =
dol_now() - 200000;
2473 $line->date_end =
dol_now() + 500000;
2474 $line->date_end_real =
dol_now() - 100000;
2475 if ($num_prods > 0) {
2476 $prodid = mt_rand(1, $num_prods);
2477 $line->fk_product = $prodids[$prodid];
2479 $this->lines[$xnbp] = $line;
2505 $ticket =
new Ticket($this->db);
2506 $nbTicket = $ticket->fetchAll($user,
'ASC',
't.datec',
'', 0,
'', array(
't.fk_contract' => $this->
id));
2508 return ($nbTicket < 0 ? $nbTicket : $ticket->lines);
2523 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2525 global $conf, $langs;
2530 if (!empty($this->model_pdf)) {
2531 $modele = $this->model_pdf;
2537 if (empty($modele)) {
2540 $langs->load(
"contracts");
2541 $outputlangs->load(
"products");
2543 $modelpath =
"core/modules/contract/doc/";
2544 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2592 global $db, $langs, $conf, $hookmanager, $extrafields;
2598 $this->
fetch($this->
id);
2601 $clonedObj = clone $this;
2602 $clonedObj->socid = $socid;
2606 $objsoc =
new Societe($this->db);
2608 $objsoc->fetch($clonedObj->socid);
2611 $clonedObj->statut = 0;
2613 if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2614 $extrafields->fetch_name_optionals_label($this->table_element);
2615 foreach ($clonedObj->array_options as $key => $option) {
2616 $shortkey = preg_replace(
'/options_/',
'', $key);
2618 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
2620 unset($clonedObj->array_options[$key]);
2626 $this->error =
'ErrorSetupNotComplete';
2632 require_once DOL_DOCUMENT_ROOT.
"/core/modules/contract/" .
getDolGlobalString(
'CONTRACT_ADDON').
'.php';
2634 $modContract =
new $obj();
2635 '@phan-var-force CommonNumRefGenerator $modContrat';
2636 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2639 foreach ($this->lines as $line) {
2640 $line->fetch_optionals($line->id);
2644 $clonedObj->context[
'createfromclone'] =
'createfromclone';
2645 $result = $clonedObj->create($user);
2648 $this->error = $clonedObj->error;
2649 $this->errors[] = $clonedObj->error;
2652 if ($this->socid == $clonedObj->socid) {
2653 if ($clonedObj->copy_linked_contact($this,
'external') < 0) {
2660 foreach ($this->lines as $line) {
2661 $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);
2664 $this->error = $clonedObj->error;
2665 $this->errors[] = $clonedObj->error;
2672 if (is_object($hookmanager)) {
2673 $parameters = array(
2675 'clonedObj' => $clonedObj
2678 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $clonedObj, $action);
2686 unset($clonedObj->context[
'createfromclone']);
2690 $this->db->commit();
2691 return $clonedObj->id;
2693 $this->db->rollback();
2710 global $langs, $user;
2712 $langs->load(
"agenda");
2722 $contractlineprocessed = array();
2723 $contractignored = array();
2724 $contracterror = array();
2728 $sql =
'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2729 $sql .=
' FROM '.MAIN_DB_PREFIX.
'contrat as c, '.MAIN_DB_PREFIX.
'contratdet as cd';
2730 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON p.rowid = cd.fk_product';
2731 $sql .=
' WHERE cd.fk_contrat = c.rowid';
2732 $sql .=
" AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan).
"', '%Y-%m-%d')";
2733 $sql .=
" AND cd.statut = 4";
2734 if ($thirdparty_id > 0) {
2735 $sql .=
" AND c.fk_soc = ".((int) $thirdparty_id);
2739 $resql = $this->db->query($sql);
2741 $num = $this->db->num_rows($resql);
2743 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
2747 $obj = $this->db->fetch_object($resql);
2749 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2760 $this->errors[] =
'Failed to load contract with id='.$obj->rowid;
2764 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);
2767 $expirationdate = $this->db->jdate($obj->date_fin_validite);
2768 $duration_value = preg_replace(
'/[^0-9]/',
'', $obj->duration);
2769 $duration_unit = preg_replace(
'/\d/',
'', $obj->duration);
2774 $object->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 1);
2777 if (isset(
$object->linkedObjects[
'facture']) && is_array(
$object->linkedObjects[
'facture']) && count(
$object->linkedObjects[
'facture']) > 0) {
2779 usort(
$object->linkedObjects[
'facture'], array(
'Contrat',
'contractCmpDate'));
2782 $someinvoicenotpaid = 0;
2783 foreach (
$object->linkedObjects[
'facture'] as $idinvoice => $invoice) {
2788 if (empty($invoice->paye)) {
2789 $someinvoicenotpaid++;
2792 if ($someinvoicenotpaid) {
2793 $this->output .=
'Contract '.$object->ref.
' is qualified for renewal but there is '.$someinvoicenotpaid.
' invoice(s) unpayed so we cancel renewal'.
"\n";
2799 if ($expirationdate && $expirationdate < $enddatetoscan) {
2800 dol_syslog(
"Define the newdate of end of services from expirationdate=".$expirationdate);
2801 $newdate = $expirationdate;
2803 while ($newdate < $enddatetoscan && $protecti < 1000) {
2808 if ($protecti < 1000) {
2814 $errorforlocaltransaction = 0;
2816 $label =
'Renewal of contrat '.$object->ref.
' line '.$obj->lid;
2817 $comment =
'Renew date of contract '.$object->ref.
' line '.$obj->lid.
' by doAutoRenewContracts';
2819 $sqlupdate =
'UPDATE '.MAIN_DB_PREFIX.
"contratdet SET date_fin_validite = '".$this->db->idate($newdate).
"'";
2820 $sqlupdate .=
' WHERE rowid = '.((int) $obj->lid);
2821 $resqlupdate = $this->db->query($sqlupdate);
2823 $contractlineprocessed[$obj->lid] =
$object->ref;
2825 $actioncode =
'RENEW_CONTRACT';
2830 $actioncomm->type_code =
'AC_OTH_AUTO';
2831 $actioncomm->code =
'AC_'.$actioncode;
2832 $actioncomm->label = $label;
2833 $actioncomm->datep = $now;
2834 $actioncomm->datef = $now;
2835 $actioncomm->percentage = -1;
2836 $actioncomm->socid =
$object->socid;
2837 $actioncomm->authorid = $user->id;
2838 $actioncomm->userownerid = $user->id;
2839 $actioncomm->fk_element =
$object->id;
2840 $actioncomm->elementtype =
'contract';
2841 $actioncomm->note_private = $comment;
2843 $ret = $actioncomm->create($user);
2848 $errorforlocaltransaction++;
2849 $this->error = $this->db->lasterror();
2852 if (! $errorforlocaltransaction) {
2853 $this->db->commit();
2855 $this->db->rollback();
2859 $this->error =
"Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate.
" enddatetoscan=".$enddatetoscan.
" duration_value=".$duration_value.
" duration_unit=".$duration_value;
2868 $this->error = $this->db->lasterror();
2871 $this->output .= count($contractlineprocessed).
' contract line(s) with end date before '.
dol_print_date($enddatetoscan,
'day').
' were renewed'.(count($contractlineprocessed) > 0 ?
' : '.implode(
',', $contractlineprocessed) :
'');
2873 return ($error ? 1 : 0);
2885 if ($a->date == $b->date) {
2886 return strcmp((
string) $a->id, (
string) $b->id);
2888 return ($a->date < $b->date) ? -1 : 1;
2902 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
2904 $return =
'<div class="box-flex-item box-flex-grow-zero">';
2905 $return .=
'<div class="info-box info-box-sm">';
2906 $return .=
'<span class="info-box-icon bg-infobox-action">';
2908 $return .=
'</span>';
2909 $return .=
'<div class="info-box-content">';
2910 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
2911 if ($selected >= 0) {
2912 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
2914 if (!empty($arraydata[
'thirdparty'])) {
2915 $tmpthirdparty = $arraydata[
'thirdparty'];
2916 $return .=
'<br><div class="info-box-label inline-block valignmiddle">'.$tmpthirdparty->getNomUrl(1).
'</div>';
2918 if (property_exists($this,
'date_contrat')) {
2919 $return .=
'<br><span class="opacitymedium valignmiddle">'.$langs->trans(
"DateContract").
' : </span><span class="info-box-label valignmiddle">'.
dol_print_date($this->date_contrat,
'day').
'</span>';
2921 if (method_exists($this,
'getLibStatut')) {
2922 $return .=
'<br><div class="info-box-status valignmiddle">'.$this->getLibStatut(7).
'</div>';
2924 $return .=
'</div>';
2925 $return .=
'</div>';
2926 $return .=
'</div>';
2957 public $element =
'contratdet';
2962 public $table_element =
'contratdet';
2978 public $element_for_permission =
'contrat';
3018 public $description;
3020 public $product_type;
3021 public $product_ref;
3022 public $product_label;
3024 public $date_commande;
3027 public $date_start_real;
3029 public $date_end_real;
3032 public $vat_src_code;
3033 public $localtax1_tx;
3034 public $localtax2_tx;
3035 public $localtax1_type;
3036 public $localtax2_type;
3038 public $remise_percent;
3044 public $fk_remise_except;
3066 public $total_localtax1;
3067 public $total_localtax2;
3073 public $fk_fournprice;
3085 public $fk_user_author;
3090 public $fk_user_ouverture;
3095 public $fk_user_cloture;
3100 public $commentaire;
3109 const STATUS_INITIAL = 0;
3110 const STATUS_OPEN = 4;
3111 const STATUS_CLOSED = 5;
3118 public $fields = array(
3119 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
3120 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 30,
'index' => 1),
3121 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 35),
3122 'qty' => array(
'type' =>
'integer',
'label' =>
'Quantity',
'enabled' => 1,
'visible' => 1,
'notnull' => 1,
'position' => 35,
'isameasure' => 1),
3123 'total_ht' => array(
'type' =>
'integer',
'label' =>
'AmountHT',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 36,
'isameasure' => 1),
3124 'total_tva' => array(
'type' =>
'integer',
'label' =>
'AmountVAT',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 37,
'isameasure' => 1),
3125 'total_ttc' => array(
'type' =>
'integer',
'label' =>
'AmountTTC',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 38,
'isameasure' => 1),
3128 'fk_contrat' => array(
'type' =>
'integer:Contrat:contrat/class/contrat.class.php',
'label' =>
'Contract',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 70),
3129 'fk_product' => array(
'type' =>
'integer:Product:product/class/product.class.php:1',
'label' =>
'Product',
'enabled' => 1,
'visible' => -1,
'position' => 75),
3131 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 105),
3132 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 110),
3136 'fk_user_ouverture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserStartingService',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 135),
3137 'fk_user_cloture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserClosingService',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 135),
3138 'statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Statut',
'enabled' => 1,
'visible' => -1,
'position' => 500,
'arrayofkeyval' => array(0 =>
'Draft', 4 =>
'Open', 5 =>
'Closed')),
3139 'rang' => array(
'type' =>
'integer',
'label' =>
'Rank',
'enabled' => 1,
'visible' => 0,
'position' => 500,
'default' =>
'0')
3163 return $this->
LibStatut($this->
statut, $mode, ((!empty($this->date_end)) ? ($this->date_end <
dol_now() ? 1 : 0) : -1));
3177 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
3181 $langs->load(
"contracts");
3183 if ($status == self::STATUS_INITIAL) {
3184 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
3185 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
3186 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3187 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
3188 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
3189 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3190 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
3191 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
3192 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3193 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
3194 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
3195 } elseif ($status == self::STATUS_CLOSED) {
3196 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
3197 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
3200 $statusType =
'status'.$status;
3201 if ($status == self::STATUS_OPEN && $expired == 1) {
3202 $statusType =
'status1';
3204 if ($status == self::STATUS_CLOSED) {
3205 $statusType =
'status6';
3210 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
3211 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
3213 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
3224 global $conf, $langs, $user;
3227 $datas[
'label'] = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3228 if (empty($datas[
'label'])) {
3229 $datas[
'label'] = $this->description;
3247 $label = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3248 if (empty($label)) {
3249 $label = $this->description;
3251 $classfortooltip =
'classfortooltip';
3255 'id' => $this->fk_contrat,
3256 'objecttype' => $this->element,
3258 $classfortooltip =
'classforajaxtooltip';
3259 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3263 $link =
'<a href="'.DOL_URL_ROOT.
'/contrat/card.php?id='.$this->fk_contrat.
'"';
3264 $link .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
3265 $link .= $dataparams.
' class="'.$classfortooltip.
'">';
3269 if ($this->
type == 0) {
3274 $result .= ($link.img_object($label, $picto, $dataparams.
' class="'.$classfortooltip.
'"').$linkend);
3276 if ($withpicto && $withpicto != 2) {
3279 if ($withpicto != 2) {
3280 $result .= $link.($this->product_ref ? $this->product_ref.
' ' :
'').($this->label ? $this->label : $this->description).$linkend;
3295 if (empty($id) && empty($ref)) {
3300 $sql .=
" t.rowid,";
3302 $sql .=
" t.fk_contrat,";
3303 $sql .=
" t.fk_product,";
3304 $sql .=
" t.statut,";
3305 $sql .=
" t.label,";
3306 $sql .=
" p.ref as product_ref,";
3307 $sql .=
" p.label as product_label,";
3308 $sql .=
" p.description as product_desc,";
3309 $sql .=
" p.fk_product_type as product_type,";
3310 $sql .=
" t.description,";
3311 $sql .=
" t.date_commande,";
3312 $sql .=
" t.date_ouverture_prevue as date_start,";
3313 $sql .=
" t.date_ouverture as date_start_real,";
3314 $sql .=
" t.date_fin_validite as date_end,";
3315 $sql .=
" t.date_cloture as date_end_real,";
3316 $sql .=
" t.tva_tx,";
3317 $sql .=
" t.vat_src_code,";
3318 $sql .=
" t.localtax1_tx,";
3319 $sql .=
" t.localtax2_tx,";
3320 $sql .=
" t.localtax1_type,";
3321 $sql .=
" t.localtax2_type,";
3323 $sql .=
" t.remise_percent,";
3324 $sql .=
" t.remise,";
3325 $sql .=
" t.fk_remise_except,";
3326 $sql .=
" t.subprice,";
3327 $sql .=
" t.price_ht,";
3328 $sql .=
" t.total_ht,";
3329 $sql .=
" t.total_tva,";
3330 $sql .=
" t.total_localtax1,";
3331 $sql .=
" t.total_localtax2,";
3332 $sql .=
" t.total_ttc,";
3333 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
3334 $sql .=
" t.buy_price_ht as pa_ht,";
3335 $sql .=
" t.info_bits,";
3336 $sql .=
" t.fk_user_author,";
3337 $sql .=
" t.fk_user_ouverture,";
3338 $sql .=
" t.fk_user_cloture,";
3339 $sql .=
" t.commentaire,";
3340 $sql .=
" t.fk_unit,";
3342 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
3344 $sql .=
" WHERE t.rowid = ".((int) $id);
3347 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
3350 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
3351 $resql = $this->db->query($sql);
3353 if ($this->db->num_rows($resql)) {
3354 $obj = $this->db->fetch_object($resql);
3356 $this->
id = $obj->rowid;
3357 $this->
ref = $obj->rowid;
3359 $this->tms = $this->db->jdate($obj->tms);
3360 $this->fk_contrat = $obj->fk_contrat;
3361 $this->fk_product = $obj->fk_product;
3362 $this->
statut = $obj->statut;
3363 $this->product_ref = $obj->product_ref;
3364 $this->product_label = $obj->product_label;
3365 $this->product_type = $obj->product_type;
3366 $this->label = $obj->label;
3368 $this->date_commande = $this->db->jdate($obj->date_commande);
3370 $this->date_start = $this->db->jdate($obj->date_start);
3371 $this->date_start_real = $this->db->jdate($obj->date_start_real);
3372 $this->date_end = $this->db->jdate($obj->date_end);
3373 $this->date_end_real = $this->db->jdate($obj->date_end_real);
3380 $this->tva_tx = $obj->tva_tx;
3381 $this->vat_src_code = $obj->vat_src_code;
3382 $this->localtax1_tx = $obj->localtax1_tx;
3383 $this->localtax2_tx = $obj->localtax2_tx;
3384 $this->localtax1_type = $obj->localtax1_type;
3385 $this->localtax2_type = $obj->localtax2_type;
3386 $this->qty = $obj->qty;
3387 $this->remise_percent = $obj->remise_percent;
3388 $this->fk_remise_except = $obj->fk_remise_except;
3389 $this->subprice = $obj->subprice;
3390 $this->price_ht = $obj->price_ht;
3391 $this->total_ht = $obj->total_ht;
3392 $this->total_tva = $obj->total_tva;
3393 $this->total_localtax1 = $obj->total_localtax1;
3394 $this->total_localtax2 = $obj->total_localtax2;
3395 $this->total_ttc = $obj->total_ttc;
3396 $this->info_bits = $obj->info_bits;
3397 $this->fk_user_author = $obj->fk_user_author;
3398 $this->fk_user_ouverture = $obj->fk_user_ouverture;
3399 $this->fk_user_cloture = $obj->fk_user_cloture;
3400 $this->commentaire = $obj->commentaire;
3401 $this->fk_fournprice = $obj->fk_fournprice;
3403 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3404 $this->pa_ht = $marginInfos[0];
3405 $this->fk_unit = $obj->fk_unit;
3407 $this->rang = $obj->rang;
3412 $this->db->free($resql);
3416 $this->error =
"Error ".$this->db->lasterror();
3429 public function update($user, $notrigger = 0)
3436 $this->fk_contrat = (int) $this->fk_contrat;
3437 $this->fk_product = (int) $this->fk_product;
3439 $this->label = trim($this->label);
3441 $this->vat_src_code = trim($this->vat_src_code);
3442 $this->tva_tx = trim((
string) $this->tva_tx);
3443 $this->localtax1_tx = trim($this->localtax1_tx);
3444 $this->localtax2_tx = trim($this->localtax2_tx);
3445 $this->qty = (float) $this->qty;
3446 $this->remise_percent = trim((
string) $this->remise_percent);
3447 $this->fk_remise_except = (int) $this->fk_remise_except;
3448 $this->subprice = (float)
price2num($this->subprice);
3449 $this->price_ht = (float)
price2num($this->price_ht);
3450 $this->info_bits = (int) $this->info_bits;
3451 $this->fk_user_author = (int) $this->fk_user_author;
3452 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3453 $this->fk_user_cloture = (int) $this->fk_user_cloture;
3454 $this->commentaire = trim($this->commentaire);
3455 $this->rang = (int) $this->rang;
3457 if (empty($this->price_ht)) {
3458 $this->price_ht = 0;
3460 if (empty($this->total_ht)) {
3461 $this->total_ht = 0;
3463 if (empty($this->total_tva)) {
3464 $this->total_tva = 0;
3466 if (empty($this->total_ttc)) {
3467 $this->total_ttc = 0;
3469 if (empty($this->localtax1_tx)) {
3470 $this->localtax1_tx = 0;
3472 if (empty($this->localtax2_tx)) {
3473 $this->localtax2_tx = 0;
3475 if (empty($this->remise_percent)) {
3476 $this->remise_percent = 0;
3485 $tabprice =
calcul_price_total($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0,
'HT', 0, 1, $mysoc, $localtaxes_type);
3486 $this->total_ht = $tabprice[0];
3487 $this->total_tva = $tabprice[1];
3488 $this->total_ttc = $tabprice[2];
3489 $this->total_localtax1 = $tabprice[9];
3490 $this->total_localtax2 = $tabprice[10];
3492 if (empty($this->pa_ht)) {
3497 if ($this->pa_ht == 0) {
3498 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
3502 $this->pa_ht = $result;
3507 if (empty($this->oldcopy)) {
3508 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
3515 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3516 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
3517 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
3518 $sql .=
" statut = ".((int) $this->
statut).
",";
3519 $sql .=
" label = '".$this->db->escape($this->label).
"',";
3520 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
3521 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
3522 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3523 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3524 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3525 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
3526 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
3527 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
3528 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
3529 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
3530 $sql .=
" qty = ".price2num($this->qty).
",";
3531 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
3532 $sql .=
" remise = ".($this->remise ?
price2num($this->remise) :
"null").
",";
3533 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
3534 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
3535 $sql .=
" price_ht = ".($this->price_ht !=
'' ? $this->price_ht :
"null").
",";
3536 $sql .=
" total_ht = ".$this->total_ht.
",";
3537 $sql .=
" total_tva = ".$this->total_tva.
",";
3538 $sql .=
" total_localtax1 = ".$this->total_localtax1.
",";
3539 $sql .=
" total_localtax2 = ".$this->total_localtax2.
",";
3540 $sql .=
" total_ttc = ".$this->total_ttc.
",";
3541 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
3542 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
3543 $sql .=
" info_bits = '".$this->db->escape($this->info_bits).
"',";
3544 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
3545 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
3546 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
3547 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
3548 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
3549 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
3550 $sql .=
" WHERE rowid = ".((int) $this->
id);
3552 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3553 $resql = $this->db->query($sql);
3555 $this->error =
"Error ".$this->db->lasterror();
3568 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3570 if ($this->date_start != $this->oldcopy->date_start) {
3571 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3572 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3573 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3575 $resql = $this->db->query($sql);
3578 $this->error =
"Error ".$this->db->lasterror();
3581 if ($this->date_end != $this->oldcopy->date_end) {
3582 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3583 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3584 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3586 $resql = $this->db->query($sql);
3589 $this->error =
"Error ".$this->db->lasterror();
3594 if (!$error && !$notrigger) {
3596 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
3599 $this->db->rollback();
3605 $this->db->commit();
3608 $this->db->rollback();
3609 $this->errors[] = $this->error;
3628 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3629 $sql .=
" total_ht=".price2num($this->total_ht,
'MT');
3630 $sql .=
",total_tva=".price2num($this->total_tva,
'MT');
3631 $sql .=
",total_localtax1=".price2num($this->total_localtax1,
'MT');
3632 $sql .=
",total_localtax2=".price2num($this->total_localtax2,
'MT');
3633 $sql .=
",total_ttc=".price2num($this->total_ttc,
'MT');
3634 $sql .=
" WHERE rowid = ".((int) $this->
id);
3636 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
3638 $resql = $this->db->query($sql);
3640 $this->db->commit();
3643 $this->error = $this->db->error();
3644 $this->db->rollback();
3663 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
3664 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3665 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3666 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3667 $sql .=
" info_bits,";
3669 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3670 if ($this->date_start > 0) {
3671 $sql .=
",date_ouverture_prevue";
3673 if ($this->date_end > 0) {
3674 $sql .=
",date_fin_validite";
3676 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
3677 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
3678 $sql .=
" '".$this->db->escape($this->qty).
"',";
3679 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
3680 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
3681 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
3682 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
3683 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3684 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3685 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
3686 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
3687 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
3688 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
3689 $sql .=
" ".price2num($this->price_ht).
",".
price2num($this->remise).
",";
3690 if ($this->fk_fournprice > 0) {
3691 $sql .=
' '.((int) $this->fk_fournprice).
',';
3695 if ($this->pa_ht > 0) {
3696 $sql .=
' '.((float)
price2num($this->pa_ht));
3700 if ($this->date_start > 0) {
3701 $sql .=
",'".$this->db->idate($this->date_start).
"'";
3703 if ($this->date_end > 0) {
3704 $sql .=
",'".$this->db->idate($this->date_end).
"'";
3708 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3710 $resql = $this->db->query($sql);
3712 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
3718 $this->db->rollback();
3725 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
3727 $this->db->rollback();
3733 $this->db->commit();
3736 $this->db->rollback();
3737 $this->error = $this->db->error().
" sql=".$sql;
3752 public function active_line($user, $date, $date_end =
'', $comment =
'')
3759 $this->
statut = ContratLigne::STATUS_OPEN;
3760 $this->date_start_real = $date;
3761 $this->date_end = $date_end;
3762 $this->fk_user_ouverture = $user->id;
3763 $this->date_end_real =
null;
3764 $this->commentaire = $comment;
3766 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->
statut).
",";
3767 $sql .=
" date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3768 if ($date_end >= 0) {
3769 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3771 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
3772 $sql .=
" date_cloture = null,";
3773 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3774 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
3776 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
3777 $resql = $this->db->query($sql);
3780 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
3787 $this->db->commit();
3790 $this->db->rollback();
3794 $this->error = $this->db->lasterror();
3795 $this->db->rollback();
3810 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
3813 $this->date_cloture = $date_end_real;
3814 $this->date_end_real = $date_end_real;
3815 $this->user_closing_id = $user->id;
3816 $this->commentaire = $comment;
3824 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
3825 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
3826 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
3827 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3828 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = ".((int) ContratLigne::STATUS_OPEN);
3830 $resql = $this->db->query($sql);
3834 $result = $this->
call_trigger(
'LINECONTRACT_CLOSE', $user);
3837 $this->db->rollback();
3843 $this->db->commit();
3846 $this->error = $this->db->lasterror();
3847 $this->db->rollback();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Class to manage agenda events (actions)
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
getIdContact($source, $code, $status=0)
Return id of contacts for a source and a contact code.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
fetch_project()
Load the project with id $this->fk_project into this->project.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
setSignedStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a signed status.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage contracts.
static contractCmpDate($a, $b)
Used to sort lines by date.
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
fetch($id, $ref='', $ref_customer='', $ref_supplier='', $noextrafields=0, $nolines=0)
Load a contract from database.
loadStateBoard()
Load the indicators this->nb for state board.
update_statut($user)
Update statut of contract according to services.
updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0.0, $localtax2tx=0.0, $date_start_real='', $date_end_real='', $price_base_type='HT', $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Mets a jour une ligne de contrat.
getTicketsArray()
Create an array of associated tickets.
setSignedStatus(User $user, int $status=0, int $notrigger=0, $triggercode='')
Set signed status.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
active_line($user, $line_id, $date_start, $date_end='', $comment='')
Activate a contract line.
getTooltipContentArray($params)
getTooltipContentArray
getLinesArray()
Create an array of order lines.
validate(User $user, $force_number='', $notrigger=0)
Validate a contract.
getIdBillingContact()
Return id des contacts clients de facturation.
initAsSpecimen()
Initialise an instance with random values.
create($user)
Create a contract into database.
close_line($user, $line_id, $date_end, $comment='')
Close a contract line.
reopen($user, $notrigger=0)
Unvalidate a contract.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
deleteLine($idline, User $user)
Delete a contract line.
array_detail($status=-1)
Return list of line rowid.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $fk_fournprice=null, $pa_ht=0, $array_options=array(), $fk_unit=null, $rang=0)
Ajoute une ligne de contrat en base.
update($user, $notrigger=0)
Update object into database.
closeAll(User $user, $notrigger=0, $comment='')
Close all lines of a contract.
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.
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
LibStatut($status, $mode)
Return the label of a given contrat status.
activateAll($user, $date_start='', $notrigger=0, $comment='', $date_end='')
Open all lines of a contract.
__construct($db)
Constructor.
doAutoRenewContracts($thirdparty_id=0, $delayindaysshort=0)
Action executed by scheduler CAN BE A CRON TASK Loop on each contract lines and update the end of dat...
getNomUrl($withpicto=0, $maxlength=0, $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
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.
active_line($user, $date, $date_end='', $comment='')
Activate a contract line.
getLibStatut($mode)
Return label of this contract line status.
insert($notrigger=0)
Inserts a contrat line into database.
fetch($id, $ref='')
Load object in memory from database.
close_line($user, $date_end_real, $comment='', $notrigger=0)
Close a contract line.
static LibStatut($status, $mode, $expired=-1, $moreatt='', $morelabel='')
Return label of a contract line status.
update($user, $notrigger=0)
Update database for contract line.
update_total()
Update in database the fields total_xxx of lines Used by migration process.
__construct($db)
Constructor.
getTooltipContentArray($params)
getTooltipContentArray
getNomUrl($withpicto=0, $maxlength=0)
Return clicable name (with picto eventually) for ContratLigne.
Class to manage Dolibarr database access.
Class to manage shipments.
const STATUS_DRAFT
Draft status.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
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.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
Class to generate the form for creating a new ticket.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut