345 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
349 $langs->load(
"contracts");
351 if ($status == self::STATUS_INITIAL) {
352 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
353 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
354 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
355 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
356 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
357 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
358 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
359 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
360 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
361 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
362 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
363 } elseif ($status == self::STATUS_CLOSED) {
364 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
365 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
368 $labelStatusShort =
'';
371 $statusType =
'status'.$status;
372 if ($status == self::STATUS_OPEN && $expired == 1) {
373 $statusType =
'status1';
375 if ($status == self::STATUS_CLOSED) {
376 $statusType =
'status6';
381 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
382 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
384 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
463 public function fetch($id, $ref =
'')
466 if (empty($id) && empty($ref)) {
473 $sql .=
" t.fk_contrat,";
474 $sql .=
" t.fk_product,";
475 $sql .=
" t.statut,";
477 $sql .=
" p.ref as product_ref,";
478 $sql .=
" p.label as product_label,";
479 $sql .=
" p.description as product_description,";
480 $sql .=
" p.fk_product_type as product_type,";
481 $sql .=
" t.description,";
482 $sql .=
" t.date_commande,";
483 $sql .=
" t.date_ouverture_prevue as date_start,";
484 $sql .=
" t.date_ouverture as date_start_real,";
485 $sql .=
" t.date_fin_validite as date_end,";
486 $sql .=
" t.date_cloture as date_end_real,";
487 $sql .=
" t.tva_tx,";
488 $sql .=
" t.vat_src_code,";
489 $sql .=
" t.localtax1_tx,";
490 $sql .=
" t.localtax2_tx,";
491 $sql .=
" t.localtax1_type,";
492 $sql .=
" t.localtax2_type,";
494 $sql .=
" t.remise_percent,";
495 $sql .=
" t.fk_remise_except,";
496 $sql .=
" t.subprice,";
497 $sql .=
" t.subprice_ttc,";
498 $sql .=
" t.total_ht,";
499 $sql .=
" t.total_tva,";
500 $sql .=
" t.total_localtax1,";
501 $sql .=
" t.total_localtax2,";
502 $sql .=
" t.total_ttc,";
503 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
504 $sql .=
" t.buy_price_ht as pa_ht,";
505 $sql .=
" t.info_bits,";
506 $sql .=
" t.fk_user_author,";
507 $sql .=
" t.fk_user_ouverture,";
508 $sql .=
" t.fk_user_cloture,";
509 $sql .=
" t.commentaire,";
510 $sql .=
" t.fk_unit,";
511 $sql .=
" t.extraparams,";
513 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
515 $sql .=
" WHERE t.rowid = ".((int) $id);
518 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
521 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
522 $resql = $this->db->query($sql);
524 if ($this->db->num_rows($resql)) {
525 $obj = $this->db->fetch_object($resql);
527 $this->
id = $obj->rowid;
528 $this->
ref = $obj->rowid;
530 $this->tms = $this->db->jdate($obj->tms);
531 $this->fk_contrat = (int) $obj->fk_contrat;
532 $this->fk_product = (int) $obj->fk_product;
533 $this->statut = $obj->statut;
534 $this->product_ref = $obj->product_ref;
535 $this->product_label = $obj->product_label;
536 $this->product_type = $obj->product_type;
537 $this->label = $obj->label;
539 $this->date_commande = $this->db->jdate($obj->date_commande);
541 $this->date_start = $this->db->jdate($obj->date_start);
542 $this->date_start_real = $this->db->jdate($obj->date_start_real);
543 $this->date_end = $this->db->jdate($obj->date_end);
544 $this->date_end_real = $this->db->jdate($obj->date_end_real);
546 $this->tva_tx = $obj->tva_tx;
547 $this->vat_src_code = $obj->vat_src_code;
548 $this->localtax1_tx = $obj->localtax1_tx;
549 $this->localtax2_tx = $obj->localtax2_tx;
550 $this->localtax1_type = $obj->localtax1_type;
551 $this->localtax2_type = $obj->localtax2_type;
552 $this->qty = $obj->qty;
553 $this->remise_percent = $obj->remise_percent;
554 $this->fk_remise_except = $obj->fk_remise_except;
555 $this->subprice = $obj->subprice;
556 $this->subprice_ttc = $obj->subprice_ttc;
557 $this->total_ht = $obj->total_ht;
558 $this->total_tva = $obj->total_tva;
559 $this->total_localtax1 = $obj->total_localtax1;
560 $this->total_localtax2 = $obj->total_localtax2;
561 $this->total_ttc = $obj->total_ttc;
562 $this->info_bits = $obj->info_bits;
563 $this->fk_user_author = $obj->fk_user_author;
564 $this->fk_user_ouverture = $obj->fk_user_ouverture;
565 $this->fk_user_cloture = $obj->fk_user_cloture;
566 $this->commentaire = $obj->commentaire;
567 $this->fk_fournprice = $obj->fk_fournprice;
569 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
570 $this->pa_ht = $marginInfos[0];
571 $this->fk_unit = $obj->fk_unit;
573 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
575 $this->rang = $obj->rang;
580 $this->db->free($resql);
584 $this->error =
"Error ".$this->db->lasterror();
597 public function update($user, $notrigger = 0)
604 $this->fk_contrat = (int) $this->fk_contrat;
605 $this->fk_product = (int) $this->fk_product;
606 $this->statut = (int) $this->statut;
607 $this->label = trim($this->label);
609 $this->vat_src_code = trim($this->vat_src_code);
610 $this->tva_tx = trim((
string) $this->tva_tx);
611 $this->localtax1_tx = trim($this->localtax1_tx);
612 $this->localtax2_tx = trim($this->localtax2_tx);
613 $this->qty = (float) $this->qty;
614 $this->remise_percent = trim((
string) $this->remise_percent);
615 $this->fk_remise_except = (int) $this->fk_remise_except;
616 $this->subprice = (float)
price2num($this->subprice);
617 $this->info_bits = (int) $this->info_bits;
618 $this->fk_user_author = (int) $this->fk_user_author;
619 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
620 $this->fk_user_cloture = (int) $this->fk_user_cloture;
621 $this->commentaire = trim($this->commentaire);
622 $this->rang = (int) $this->rang;
623 if (empty($this->subprice)) {
626 if (empty($this->total_ht)) {
629 if (empty($this->total_tva)) {
630 $this->total_tva = 0;
632 if (empty($this->total_ttc)) {
633 $this->total_ttc = 0;
635 if (empty($this->localtax1_tx)) {
636 $this->localtax1_tx = 0;
638 if (empty($this->localtax2_tx)) {
639 $this->localtax2_tx = 0;
641 if (empty($this->remise_percent)) {
642 $this->remise_percent = 0;
654 $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);
655 $this->total_ht = (float) $tabprice[0];
656 $this->total_tva = (float) $tabprice[1];
657 $this->total_ttc = (float) $tabprice[2];
658 $this->total_localtax1 = (float) $tabprice[9];
659 $this->total_localtax2 = (float) $tabprice[10];
661 if (empty($this->pa_ht)) {
666 if ($this->pa_ht == 0) {
667 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
671 $this->pa_ht = $result;
676 if (empty($this->oldcopy)) {
677 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
684 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
685 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
686 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
687 $sql .=
" statut = ".((int) $this->statut).
",";
688 $sql .=
" label = '".$this->db->escape($this->label).
"',";
689 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
690 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
691 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
692 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
693 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
694 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
695 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
696 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
697 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
698 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
699 $sql .=
" qty = ".price2num($this->qty).
",";
700 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
701 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? ((int) $this->fk_remise_except) :
"null").
",";
702 $sql .=
" subprice = ".($this->subprice !=
'' ? ((float) $this->subprice) :
"null").
",";
703 $sql .=
" subprice_ttc = ".($this->subprice_ttc !=
'' ? ((float) $this->subprice_ttc) :
"0").
",";
704 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
705 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
706 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
707 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
708 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
709 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) :
"NULL").
",";
710 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
711 $sql .=
" info_bits = '".$this->db->escape((
string) $this->info_bits).
"',";
712 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? ((int) $this->fk_user_author) :
"NULL").
",";
713 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? ((int) $this->fk_user_ouverture) :
"NULL").
",";
714 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? ((int) $this->fk_user_cloture) :
"NULL").
",";
715 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
716 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit)).
",";
717 $sql .=
" rang = ".(empty($this->rang) ?
'0' : ((int) $this->rang));
718 $sql .=
" WHERE rowid = ".((int) $this->
id);
720 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
721 $resql = $this->db->query($sql);
723 $this->error =
"Error ".$this->db->lasterror();
736 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
738 if ($this->date_start != $this->oldcopy->date_start) {
739 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
740 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
741 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
743 $resql = $this->db->query($sql);
746 $this->error =
"Error ".$this->db->lasterror();
749 if ($this->date_end != $this->oldcopy->date_end) {
750 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
751 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
752 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
754 $resql = $this->db->query($sql);
757 $this->error =
"Error ".$this->db->lasterror();
762 if (!$error && !$notrigger) {
764 $result = $this->call_trigger(
'LINECONTRACT_MODIFY', $user);
767 $this->db->rollback();
776 $this->db->rollback();
777 $this->errors[] = $this->error;
831 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
832 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
833 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc,";
834 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
835 $sql .=
" info_bits,";
837 $sql .=
" fk_product_fournisseur_price, buy_price_ht";
838 if ($this->date_start > 0) {
839 $sql .=
",date_ouverture_prevue";
841 if ($this->date_end > 0) {
842 $sql .=
",date_fin_validite";
844 $sql .=
") VALUES (".((int) $this->fk_contrat).
", '', '".$this->db->escape($this->
description).
"',";
845 $sql .= ($this->fk_product > 0 ? ((int) $this->fk_product) :
"null").
",";
846 $sql .=
" '".$this->db->escape((
string) $this->qty).
"',";
847 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
848 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
849 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
850 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
851 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
852 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
853 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",".((float)
price2num($this->subprice_ttc)).
",";
854 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
855 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
856 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
857 if ($this->fk_fournprice > 0) {
858 $sql .=
' '.((int) $this->fk_fournprice).
',';
862 if ($this->pa_ht > 0) {
863 $sql .=
' '.((float)
price2num($this->pa_ht));
867 if ($this->date_start > 0) {
868 $sql .=
",'".$this->db->idate($this->date_start).
"'";
870 if ($this->date_end > 0) {
871 $sql .=
",'".$this->db->idate($this->date_end).
"'";
875 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
877 $resql = $this->db->query($sql);
879 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
884 $this->db->rollback();
890 $result = $this->call_trigger(
'LINECONTRACT_INSERT', $user);
892 $this->db->rollback();
901 $this->db->rollback();
902 $this->error = $this->db->error().
" sql=".$sql;
917 public function active_line($user, $date, $date_end =
'', $comment =
'')
924 $this->statut = ContratLigne::STATUS_OPEN;
925 $this->
status = ContratLigne::STATUS_OPEN;
926 $this->date_start_real = $date;
927 $this->date_end = $date_end;
928 $this->fk_user_ouverture = $user->id;
929 $this->date_end_real =
null;
930 $this->commentaire = $comment;
932 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->
status).
",";
933 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
934 if ($date_end >= 0) {
935 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
937 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
938 $sql .=
" date_cloture = null,";
939 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
940 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
942 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
943 $resql = $this->db->query($sql);
945 if ($date_end >= 0) {
947 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
948 $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.
")";
949 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
950 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
951 if (!$resqltoupdatecontract) {
952 $this->error = $this->db->lasterror();
953 $this->db->rollback();
959 $result = $this->call_trigger(
'LINECONTRACT_ACTIVATE', $user);
969 $this->db->rollback();
973 $this->error = $this->db->lasterror();
974 $this->db->rollback();
989 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
992 $this->date_cloture = $date_end_real;
993 $this->date_end_real = $date_end_real;
994 $this->user_closing_id = $user->id;
995 $this->commentaire = $comment;
1003 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
1004 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
1005 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
1006 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
1007 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut <> ".((int) ContratLigne::STATUS_CLOSED);
1009 $resql = $this->db->query($sql);
1012 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
1013 $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.
")";
1014 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
1015 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1016 if (!$resqltoupdatecontract) {
1017 $this->error = $this->db->lasterror();
1018 $this->db->rollback();
1024 $result = $this->call_trigger(
'LINECONTRACT_CLOSE', $user);
1027 $this->db->rollback();
1033 $this->db->commit();
1036 $this->error = $this->db->lasterror();
1037 $this->db->rollback();