357 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
361 $langs->load(
"contracts");
363 if ($status == self::STATUS_INITIAL) {
364 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
365 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
366 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
367 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
368 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
369 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
370 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
371 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
372 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
373 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
374 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
375 } elseif ($status == self::STATUS_CLOSED) {
376 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
377 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
380 $labelStatusShort =
'';
383 $statusType =
'status'.$status;
384 if ($status == self::STATUS_OPEN && $expired == 1) {
385 $statusType =
'status1';
387 if ($status == self::STATUS_CLOSED) {
388 $statusType =
'status6';
393 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
394 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
396 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
486 public function fetch($id, $ref =
'')
489 if (empty($id) && empty($ref)) {
496 $sql .=
" t.fk_contrat,";
497 $sql .=
" t.fk_product,";
498 $sql .=
" t.statut,";
500 $sql .=
" p.ref as product_ref,";
501 $sql .=
" p.label as product_label,";
502 $sql .=
" p.description as product_description,";
503 $sql .=
" p.fk_product_type as product_type,";
504 $sql .=
" t.description,";
505 $sql .=
" t.date_commande,";
506 $sql .=
" t.date_ouverture_prevue as date_start,";
507 $sql .=
" t.date_ouverture as date_start_real,";
508 $sql .=
" t.date_fin_validite as date_end,";
509 $sql .=
" t.date_cloture as date_end_real,";
510 $sql .=
" t.tva_tx,";
511 $sql .=
" t.vat_src_code,";
512 $sql .=
" t.localtax1_tx,";
513 $sql .=
" t.localtax2_tx,";
514 $sql .=
" t.localtax1_type,";
515 $sql .=
" t.localtax2_type,";
517 $sql .=
" t.remise_percent,";
518 $sql .=
" t.fk_remise_except,";
519 $sql .=
" t.subprice,";
520 $sql .=
" t.subprice_ttc,";
521 $sql .=
" t.total_ht,";
522 $sql .=
" t.total_tva,";
523 $sql .=
" t.total_localtax1,";
524 $sql .=
" t.total_localtax2,";
525 $sql .=
" t.total_ttc,";
526 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
527 $sql .=
" t.buy_price_ht as pa_ht,";
528 $sql .=
" t.info_bits,";
529 $sql .=
" t.fk_user_author,";
530 $sql .=
" t.fk_user_ouverture,";
531 $sql .=
" t.fk_user_cloture,";
532 $sql .=
" t.commentaire,";
533 $sql .=
" t.fk_unit,";
534 $sql .=
" t.extraparams,";
536 $sql .=
" t.fk_multicurrency, t.multicurrency_code, t.multicurrency_subprice, t.multicurrency_subprice_ttc, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc";
537 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
539 $sql .=
" WHERE t.rowid = ".((int) $id);
542 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
545 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
546 $resql = $this->db->query($sql);
548 if ($this->db->num_rows($resql)) {
549 $obj = $this->db->fetch_object($resql);
551 $this->
id = $obj->rowid;
552 $this->
ref = $obj->rowid;
554 $this->tms = $this->db->jdate($obj->tms);
555 $this->fk_contrat = (int) $obj->fk_contrat;
556 $this->fk_product = (int) $obj->fk_product;
557 $this->statut = $obj->statut;
558 $this->product_ref = $obj->product_ref;
559 $this->product_label = $obj->product_label;
560 $this->product_type = $obj->product_type;
561 $this->label = $obj->label;
563 $this->date_commande = $this->db->jdate($obj->date_commande);
565 $this->date_start = $this->db->jdate($obj->date_start);
566 $this->date_start_real = $this->db->jdate($obj->date_start_real);
567 $this->date_end = $this->db->jdate($obj->date_end);
568 $this->date_end_real = $this->db->jdate($obj->date_end_real);
570 $this->tva_tx = $obj->tva_tx;
571 $this->vat_src_code = $obj->vat_src_code;
572 $this->localtax1_tx = $obj->localtax1_tx;
573 $this->localtax2_tx = $obj->localtax2_tx;
574 $this->localtax1_type = $obj->localtax1_type;
575 $this->localtax2_type = $obj->localtax2_type;
576 $this->qty = $obj->qty;
577 $this->remise_percent = $obj->remise_percent;
578 $this->fk_remise_except = $obj->fk_remise_except;
579 $this->subprice = $obj->subprice;
580 $this->subprice_ttc = $obj->subprice_ttc;
581 $this->total_ht = $obj->total_ht;
582 $this->total_tva = $obj->total_tva;
583 $this->total_localtax1 = $obj->total_localtax1;
584 $this->total_localtax2 = $obj->total_localtax2;
585 $this->total_ttc = $obj->total_ttc;
586 $this->info_bits = $obj->info_bits;
587 $this->fk_user_author = $obj->fk_user_author;
588 $this->fk_user_ouverture = $obj->fk_user_ouverture;
589 $this->fk_user_cloture = $obj->fk_user_cloture;
590 $this->commentaire = $obj->commentaire;
591 $this->fk_fournprice = $obj->fk_fournprice;
593 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
594 $this->pa_ht = $marginInfos[0];
595 $this->fk_unit = $obj->fk_unit;
597 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
599 $this->rang = $obj->rang;
601 $this->fk_multicurrency = $obj->fk_multicurrency;
602 $this->multicurrency_code = $obj->multicurrency_code;
603 $this->multicurrency_subprice = $obj->multicurrency_subprice;
604 $this->multicurrency_subprice_ttc = $obj->multicurrency_subprice_ttc;
605 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
606 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
607 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
612 $this->db->free($resql);
616 $this->error =
"Error ".$this->db->lasterror();
629 public function update($user, $notrigger = 0)
636 $this->fk_contrat = (int) $this->fk_contrat;
637 $this->fk_product = (int) $this->fk_product;
638 $this->statut = (int) $this->statut;
639 $this->label = trim((
string) $this->label);
641 $this->vat_src_code = trim((
string) $this->vat_src_code);
642 $this->tva_tx = trim((
string) $this->tva_tx);
643 $this->localtax1_tx = trim((
string) $this->localtax1_tx);
644 $this->localtax2_tx = trim((
string) $this->localtax2_tx);
645 $this->qty = (float) $this->qty;
646 $this->remise_percent = trim((
string) $this->remise_percent);
647 $this->fk_remise_except = (int) $this->fk_remise_except;
648 $this->subprice = (float)
price2num($this->subprice);
649 $this->info_bits = (int) $this->info_bits;
650 $this->fk_user_author = (int) $this->fk_user_author;
651 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
652 $this->fk_user_cloture = (int) $this->fk_user_cloture;
653 $this->commentaire = trim((
string) $this->commentaire);
654 $this->rang = (int) $this->rang;
655 if (empty($this->subprice)) {
658 if (empty($this->total_ht)) {
661 if (empty($this->total_tva)) {
662 $this->total_tva = 0;
664 if (empty($this->total_ttc)) {
665 $this->total_ttc = 0;
667 if (empty($this->localtax1_tx)) {
668 $this->localtax1_tx = 0;
670 if (empty($this->localtax2_tx)) {
671 $this->localtax2_tx = 0;
673 if (empty($this->remise_percent)) {
674 $this->remise_percent = 0;
688 $multicurrency_tx = 1;
689 if (
isModEnabled(
'multicurrency') && $this->fk_contrat > 0) {
690 $sql_contract_currency =
"SELECT fk_multicurrency, multicurrency_code, multicurrency_tx FROM ".MAIN_DB_PREFIX.
"contrat WHERE rowid = ".((int) $this->fk_contrat);
691 $resql_contract_currency = $this->db->query($sql_contract_currency);
692 if ($resql_contract_currency) {
693 $obj_contract_currency = $this->db->fetch_object($resql_contract_currency);
694 if ($obj_contract_currency) {
695 $this->fk_multicurrency = $obj_contract_currency->fk_multicurrency;
696 $this->multicurrency_code = $obj_contract_currency->multicurrency_code;
697 $multicurrency_tx = $obj_contract_currency->multicurrency_tx ? $obj_contract_currency->multicurrency_tx : 1;
702 $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, 100, $multicurrency_tx, (
float) $this->multicurrency_subprice);
703 $this->total_ht = (float) $tabprice[0];
704 $this->total_tva = (float) $tabprice[1];
705 $this->total_ttc = (float) $tabprice[2];
706 $this->total_localtax1 = (float) $tabprice[9];
707 $this->total_localtax2 = (float) $tabprice[10];
710 $this->multicurrency_total_ht = (float) $tabprice[16];
711 $this->multicurrency_total_tva = (float) $tabprice[17];
712 $this->multicurrency_total_ttc = (float) $tabprice[18];
713 $this->multicurrency_subprice = (float) $tabprice[19];
715 if (empty($this->pa_ht)) {
720 if ($this->pa_ht == 0) {
721 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product, $this->qty);
725 $this->pa_ht = $result;
730 if (empty($this->oldcopy)) {
731 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
738 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
739 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
740 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
741 $sql .=
" statut = ".((int) $this->statut).
",";
742 $sql .=
" label = '".$this->db->escape($this->label).
"',";
743 $sql .=
" description = '".$this->db->escape($this->description).
"',";
744 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
745 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
746 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
747 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
748 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
749 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
750 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
751 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
752 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
753 $sql .=
" qty = ".price2num($this->qty).
",";
754 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
755 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? ((int) $this->fk_remise_except) :
"null").
",";
756 $sql .=
" subprice = ".($this->subprice !=
'' ? ((float) $this->subprice) :
"null").
",";
757 $sql .=
" subprice_ttc = ".($this->subprice_ttc !=
'' ? ((float) $this->subprice_ttc) :
"0").
",";
758 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
759 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
760 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
761 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
762 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
763 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) :
"NULL").
",";
764 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
765 $sql .=
" info_bits = '".$this->db->escape((
string) $this->info_bits).
"',";
766 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? ((int) $this->fk_user_author) :
"NULL").
",";
767 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? ((int) $this->fk_user_ouverture) :
"NULL").
",";
768 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? ((int) $this->fk_user_cloture) :
"NULL").
",";
769 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
770 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit)).
",";
771 $sql .=
" fk_multicurrency = ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
"NULL").
",";
772 $sql .=
" multicurrency_code = ".(!empty($this->multicurrency_code) ?
"'".$this->db->escape($this->multicurrency_code).
"'" :
"NULL").
",";
773 $sql .=
" multicurrency_subprice = ".((float) $this->multicurrency_subprice).
",";
774 $sql .=
" multicurrency_total_ht = ".((float) $this->multicurrency_total_ht).
",";
775 $sql .=
" multicurrency_total_tva = ".((float) $this->multicurrency_total_tva).
",";
776 $sql .=
" multicurrency_total_ttc = ".((float) $this->multicurrency_total_ttc).
",";
777 $sql .=
" rang = ".(empty($this->rang) ?
'0' : ((int) $this->rang));
778 $sql .=
" WHERE rowid = ".((int) $this->
id);
780 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
781 $resql = $this->db->query($sql);
783 $this->error =
"Error ".$this->db->lasterror();
796 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
798 if ($this->date_start != $this->oldcopy->date_start) {
799 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
800 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
801 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
803 $resql = $this->db->query($sql);
806 $this->error =
"Error ".$this->db->lasterror();
809 if ($this->date_end != $this->oldcopy->date_end) {
810 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
811 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
812 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
814 $resql = $this->db->query($sql);
817 $this->error =
"Error ".$this->db->lasterror();
822 if (!$error && !$notrigger) {
824 $result = $this->call_trigger(
'LINECONTRACT_MODIFY', $user);
827 $this->db->rollback();
836 $this->db->rollback();
837 $this->errors[] = $this->error;
891 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
892 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
893 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc,";
894 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
895 $sql .=
" info_bits,";
897 $sql .=
" fk_product_fournisseur_price, buy_price_ht";
898 if ($this->date_start > 0) {
899 $sql .=
",date_ouverture_prevue";
901 if ($this->date_end > 0) {
902 $sql .=
",date_fin_validite";
904 $sql .=
") VALUES (".((int) $this->fk_contrat).
", '', '".$this->db->escape($this->
description).
"',";
905 $sql .= ($this->fk_product > 0 ? ((int) $this->fk_product) :
"null").
",";
906 $sql .=
" '".$this->db->escape((
string) $this->qty).
"',";
907 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
908 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
909 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
910 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
911 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
912 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
913 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",".((float)
price2num($this->subprice_ttc)).
",";
914 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
915 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
916 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
917 if ($this->fk_fournprice > 0) {
918 $sql .=
' '.((int) $this->fk_fournprice).
',';
922 if ($this->pa_ht > 0) {
923 $sql .=
' '.((float)
price2num($this->pa_ht));
927 if ($this->date_start > 0) {
928 $sql .=
",'".$this->db->idate($this->date_start).
"'";
930 if ($this->date_end > 0) {
931 $sql .=
",'".$this->db->idate($this->date_end).
"'";
935 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
937 $resql = $this->db->query($sql);
939 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
944 $this->db->rollback();
950 $result = $this->call_trigger(
'LINECONTRACT_INSERT', $user);
952 $this->db->rollback();
961 $this->db->rollback();
962 $this->error = $this->db->error().
" sql=".$sql;
977 public function active_line($user, $date, $date_end =
'', $comment =
'')
984 $this->statut = ContratLigne::STATUS_OPEN;
985 $this->
status = ContratLigne::STATUS_OPEN;
986 $this->date_start_real = $date;
987 $this->date_end = $date_end;
988 $this->fk_user_ouverture = $user->id;
989 $this->date_end_real =
null;
990 $this->commentaire = $comment;
992 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->
status).
",";
993 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
994 if ($date_end >= 0) {
995 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
997 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
998 $sql .=
" date_cloture = null,";
999 $sql .=
" fk_user_cloture = null,";
1000 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
1001 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
1003 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
1004 $resql = $this->db->query($sql);
1006 if ($date_end >= 0) {
1008 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
1009 $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.
")";
1010 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
1011 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1012 if (!$resqltoupdatecontract) {
1013 $this->error = $this->db->lasterror();
1014 $this->db->rollback();
1020 $result = $this->call_trigger(
'LINECONTRACT_ACTIVATE', $user);
1027 $this->db->commit();
1030 $this->db->rollback();
1034 $this->error = $this->db->lasterror();
1035 $this->db->rollback();