350 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
354 $langs->load(
"contracts");
356 if ($status == self::STATUS_INITIAL) {
357 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
358 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
359 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
360 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
361 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
362 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
363 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
364 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
365 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
366 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
367 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
368 } elseif ($status == self::STATUS_CLOSED) {
369 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
370 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
373 $labelStatusShort =
'';
376 $statusType =
'status'.$status;
377 if ($status == self::STATUS_OPEN && $expired == 1) {
378 $statusType =
'status1';
380 if ($status == self::STATUS_CLOSED) {
381 $statusType =
'status6';
386 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
387 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
389 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
418 public function getNomUrl($withpicto = 0, $maxlength = 0)
423 $label = $langs->trans(
"ShowContractOfService").
': '.$this->label;
424 if (empty($this->label)) {
425 $label = $this->description;
427 $classfortooltip =
'classfortooltip';
431 'id' => $this->fk_contrat,
432 'objecttype' => $this->element,
434 $classfortooltip =
'classforajaxtooltip';
435 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
439 $link =
'<a href="'.DOL_URL_ROOT.
'/contrat/card.php?id='.$this->fk_contrat.
'"';
440 $link .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
441 $link .= $dataparams.
' class="'.$classfortooltip.
'">';
445 if ($this->
type == 0) {
450 $result .= ($link.img_object($label, $picto, $dataparams.
' class="'.$classfortooltip.
'"').$linkend);
452 if ($withpicto && $withpicto != 2) {
455 if ($withpicto != 2) {
456 $result .= $link.($this->product_ref ? $this->product_ref.
' ' :
'').($this->label ? $this->label : $this->description).$linkend;
459 global $action, $hookmanager;
460 $hookmanager->initHooks(array($this->element .
'dao'));
461 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
462 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
464 $result = $hookmanager->resPrint;
466 $result .= $hookmanager->resPrint;
479 public function fetch($id, $ref =
'')
482 if (empty($id) && empty($ref)) {
489 $sql .=
" t.fk_contrat,";
490 $sql .=
" t.fk_product,";
491 $sql .=
" t.statut,";
493 $sql .=
" p.ref as product_ref,";
494 $sql .=
" p.label as product_label,";
495 $sql .=
" p.description as product_description,";
496 $sql .=
" p.fk_product_type as product_type,";
497 $sql .=
" t.description,";
498 $sql .=
" t.date_commande,";
499 $sql .=
" t.date_ouverture_prevue as date_start,";
500 $sql .=
" t.date_ouverture as date_start_real,";
501 $sql .=
" t.date_fin_validite as date_end,";
502 $sql .=
" t.date_cloture as date_end_real,";
503 $sql .=
" t.tva_tx,";
504 $sql .=
" t.vat_src_code,";
505 $sql .=
" t.localtax1_tx,";
506 $sql .=
" t.localtax2_tx,";
507 $sql .=
" t.localtax1_type,";
508 $sql .=
" t.localtax2_type,";
510 $sql .=
" t.remise_percent,";
511 $sql .=
" t.fk_remise_except,";
512 $sql .=
" t.subprice,";
513 $sql .=
" t.subprice_ttc,";
514 $sql .=
" t.total_ht,";
515 $sql .=
" t.total_tva,";
516 $sql .=
" t.total_localtax1,";
517 $sql .=
" t.total_localtax2,";
518 $sql .=
" t.total_ttc,";
519 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
520 $sql .=
" t.buy_price_ht as pa_ht,";
521 $sql .=
" t.info_bits,";
522 $sql .=
" t.fk_user_author,";
523 $sql .=
" t.fk_user_ouverture,";
524 $sql .=
" t.fk_user_cloture,";
525 $sql .=
" t.commentaire,";
526 $sql .=
" t.fk_unit,";
527 $sql .=
" t.extraparams,";
529 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
531 $sql .=
" WHERE t.rowid = ".((int) $id);
534 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
537 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
538 $resql = $this->db->query($sql);
540 if ($this->db->num_rows($resql)) {
541 $obj = $this->db->fetch_object($resql);
543 $this->
id = $obj->rowid;
544 $this->
ref = $obj->rowid;
546 $this->tms = $this->db->jdate($obj->tms);
547 $this->fk_contrat = (int) $obj->fk_contrat;
548 $this->fk_product = (int) $obj->fk_product;
549 $this->statut = $obj->statut;
550 $this->product_ref = $obj->product_ref;
551 $this->product_label = $obj->product_label;
552 $this->product_type = $obj->product_type;
553 $this->label = $obj->label;
555 $this->date_commande = $this->db->jdate($obj->date_commande);
557 $this->date_start = $this->db->jdate($obj->date_start);
558 $this->date_start_real = $this->db->jdate($obj->date_start_real);
559 $this->date_end = $this->db->jdate($obj->date_end);
560 $this->date_end_real = $this->db->jdate($obj->date_end_real);
562 $this->tva_tx = $obj->tva_tx;
563 $this->vat_src_code = $obj->vat_src_code;
564 $this->localtax1_tx = $obj->localtax1_tx;
565 $this->localtax2_tx = $obj->localtax2_tx;
566 $this->localtax1_type = $obj->localtax1_type;
567 $this->localtax2_type = $obj->localtax2_type;
568 $this->qty = $obj->qty;
569 $this->remise_percent = $obj->remise_percent;
570 $this->fk_remise_except = $obj->fk_remise_except;
571 $this->subprice = $obj->subprice;
572 $this->subprice_ttc = $obj->subprice_ttc;
573 $this->total_ht = $obj->total_ht;
574 $this->total_tva = $obj->total_tva;
575 $this->total_localtax1 = $obj->total_localtax1;
576 $this->total_localtax2 = $obj->total_localtax2;
577 $this->total_ttc = $obj->total_ttc;
578 $this->info_bits = $obj->info_bits;
579 $this->fk_user_author = $obj->fk_user_author;
580 $this->fk_user_ouverture = $obj->fk_user_ouverture;
581 $this->fk_user_cloture = $obj->fk_user_cloture;
582 $this->commentaire = $obj->commentaire;
583 $this->fk_fournprice = $obj->fk_fournprice;
585 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
586 $this->pa_ht = $marginInfos[0];
587 $this->fk_unit = $obj->fk_unit;
589 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
591 $this->rang = $obj->rang;
596 $this->db->free($resql);
600 $this->error =
"Error ".$this->db->lasterror();
613 public function update($user, $notrigger = 0)
620 $this->fk_contrat = (int) $this->fk_contrat;
621 $this->fk_product = (int) $this->fk_product;
622 $this->statut = (int) $this->statut;
623 $this->label = trim((
string) $this->label);
625 $this->vat_src_code = trim((
string) $this->vat_src_code);
626 $this->tva_tx = trim((
string) $this->tva_tx);
627 $this->localtax1_tx = trim((
string) $this->localtax1_tx);
628 $this->localtax2_tx = trim((
string) $this->localtax2_tx);
629 $this->qty = (float) $this->qty;
630 $this->remise_percent = trim((
string) $this->remise_percent);
631 $this->fk_remise_except = (int) $this->fk_remise_except;
632 $this->subprice = (float)
price2num($this->subprice);
633 $this->info_bits = (int) $this->info_bits;
634 $this->fk_user_author = (int) $this->fk_user_author;
635 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
636 $this->fk_user_cloture = (int) $this->fk_user_cloture;
637 $this->commentaire = trim((
string) $this->commentaire);
638 $this->rang = (int) $this->rang;
639 if (empty($this->subprice)) {
642 if (empty($this->total_ht)) {
645 if (empty($this->total_tva)) {
646 $this->total_tva = 0;
648 if (empty($this->total_ttc)) {
649 $this->total_ttc = 0;
651 if (empty($this->localtax1_tx)) {
652 $this->localtax1_tx = 0;
654 if (empty($this->localtax2_tx)) {
655 $this->localtax2_tx = 0;
657 if (empty($this->remise_percent)) {
658 $this->remise_percent = 0;
670 $tabprice =
calcul_price_total($this->qty, $pu_for_calc, $this->remise_percent, (
float) $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, $line_price_base_type, 0, 1,
$mysoc, $localtaxes_type);
671 $this->total_ht = (float) $tabprice[0];
672 $this->total_tva = (float) $tabprice[1];
673 $this->total_ttc = (float) $tabprice[2];
674 $this->total_localtax1 = (float) $tabprice[9];
675 $this->total_localtax2 = (float) $tabprice[10];
677 if (empty($this->pa_ht)) {
682 if ($this->pa_ht == 0) {
683 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product, $this->qty);
687 $this->pa_ht = $result;
692 if (empty($this->oldcopy)) {
693 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
700 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
701 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
702 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
703 $sql .=
" statut = ".((int) $this->statut).
",";
704 $sql .=
" label = '".$this->db->escape($this->label).
"',";
705 $sql .=
" description = '".$this->db->escape($this->description).
"',";
706 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
707 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
708 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
709 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
710 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
711 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
712 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
713 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
714 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
715 $sql .=
" qty = ".price2num($this->qty).
",";
716 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
717 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? ((int) $this->fk_remise_except) :
"null").
",";
718 $sql .=
" subprice = ".($this->subprice !=
'' ? ((float) $this->subprice) :
"null").
",";
719 $sql .=
" subprice_ttc = ".($this->subprice_ttc !=
'' ? ((float) $this->subprice_ttc) :
"0").
",";
720 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
721 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
722 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
723 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
724 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
725 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) :
"NULL").
",";
726 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
727 $sql .=
" info_bits = '".$this->db->escape((
string) $this->info_bits).
"',";
728 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? ((int) $this->fk_user_author) :
"NULL").
",";
729 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? ((int) $this->fk_user_ouverture) :
"NULL").
",";
730 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? ((int) $this->fk_user_cloture) :
"NULL").
",";
731 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
732 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit)).
",";
733 $sql .=
" rang = ".(empty($this->rang) ?
'0' : ((int) $this->rang));
734 $sql .=
" WHERE rowid = ".((int) $this->
id);
736 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
737 $resql = $this->db->query($sql);
739 $this->error =
"Error ".$this->db->lasterror();
752 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
754 if ($this->date_start != $this->oldcopy->date_start) {
755 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
756 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
757 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
759 $resql = $this->db->query($sql);
762 $this->error =
"Error ".$this->db->lasterror();
765 if ($this->date_end != $this->oldcopy->date_end) {
766 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
767 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
768 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
770 $resql = $this->db->query($sql);
773 $this->error =
"Error ".$this->db->lasterror();
778 if (!$error && !$notrigger) {
780 $result = $this->call_trigger(
'LINECONTRACT_MODIFY', $user);
783 $this->db->rollback();
792 $this->db->rollback();
793 $this->errors[] = $this->error;
847 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
848 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
849 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc,";
850 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
851 $sql .=
" info_bits,";
853 $sql .=
" fk_product_fournisseur_price, buy_price_ht";
854 if ($this->date_start > 0) {
855 $sql .=
",date_ouverture_prevue";
857 if ($this->date_end > 0) {
858 $sql .=
",date_fin_validite";
860 $sql .=
") VALUES (".((int) $this->fk_contrat).
", '', '".$this->db->escape($this->
description).
"',";
861 $sql .= ($this->fk_product > 0 ? ((int) $this->fk_product) :
"null").
",";
862 $sql .=
" '".$this->db->escape((
string) $this->qty).
"',";
863 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
864 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
865 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
866 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
867 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
868 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
869 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",".((float)
price2num($this->subprice_ttc)).
",";
870 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
871 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
872 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
873 if ($this->fk_fournprice > 0) {
874 $sql .=
' '.((int) $this->fk_fournprice).
',';
878 if ($this->pa_ht > 0) {
879 $sql .=
' '.((float)
price2num($this->pa_ht));
883 if ($this->date_start > 0) {
884 $sql .=
",'".$this->db->idate($this->date_start).
"'";
886 if ($this->date_end > 0) {
887 $sql .=
",'".$this->db->idate($this->date_end).
"'";
891 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
893 $resql = $this->db->query($sql);
895 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
900 $this->db->rollback();
906 $result = $this->call_trigger(
'LINECONTRACT_INSERT', $user);
908 $this->db->rollback();
917 $this->db->rollback();
918 $this->error = $this->db->error().
" sql=".$sql;
933 public function active_line($user, $date, $date_end =
'', $comment =
'')
940 $this->statut = ContratLigne::STATUS_OPEN;
941 $this->
status = ContratLigne::STATUS_OPEN;
942 $this->date_start_real = $date;
943 $this->date_end = $date_end;
944 $this->fk_user_ouverture = $user->id;
945 $this->date_end_real =
null;
946 $this->commentaire = $comment;
948 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->
status).
",";
949 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
950 if ($date_end >= 0) {
951 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
953 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
954 $sql .=
" date_cloture = null,";
955 $sql .=
" fk_user_cloture = null,";
956 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
957 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
959 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
960 $resql = $this->db->query($sql);
962 if ($date_end >= 0) {
964 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
965 $sqltoupdatecontract .=
" SET denormalized_lower_planned_end_date = (SELECT MIN(date_fin_validite) FROM ".MAIN_DB_PREFIX.
"contratdet as cd WHERE cd.fk_contrat = ".((int) $this->fk_contrat).
" AND cd.statut = ".ContratLigne::STATUS_OPEN.
")";
966 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
967 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
968 if (!$resqltoupdatecontract) {
969 $this->error = $this->db->lasterror();
970 $this->db->rollback();
976 $result = $this->call_trigger(
'LINECONTRACT_ACTIVATE', $user);
986 $this->db->rollback();
990 $this->error = $this->db->lasterror();
991 $this->db->rollback();
1006 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
1009 $this->date_cloture = $date_end_real;
1010 $this->date_end_real = $date_end_real;
1011 $this->user_closing_id = $user->id;
1012 $this->commentaire = $comment;
1020 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
1021 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
1022 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
1023 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
1024 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut <> ".((int) ContratLigne::STATUS_CLOSED);
1026 $resql = $this->db->query($sql);
1029 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
1030 $sqltoupdatecontract .=
" SET denormalized_lower_planned_end_date = (SELECT MIN(date_fin_validite) FROM ".MAIN_DB_PREFIX.
"contratdet as cd WHERE cd.fk_contrat = ".((int) $this->fk_contrat).
" AND cd.statut = ".ContratLigne::STATUS_OPEN.
")";
1031 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
1032 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1033 if (!$resqltoupdatecontract) {
1034 $this->error = $this->db->lasterror();
1035 $this->db->rollback();
1041 $result = $this->call_trigger(
'LINECONTRACT_CLOSE', $user);
1044 $this->db->rollback();
1050 $this->db->commit();
1053 $this->error = $this->db->lasterror();
1054 $this->db->rollback();