344 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
348 $langs->load(
"contracts");
350 if ($status == self::STATUS_INITIAL) {
351 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
352 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
353 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
354 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
355 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
356 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
357 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
358 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
359 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
360 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
361 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
362 } elseif ($status == self::STATUS_CLOSED) {
363 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
364 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
367 $labelStatusShort =
'';
370 $statusType =
'status'.$status;
371 if ($status == self::STATUS_OPEN && $expired == 1) {
372 $statusType =
'status1';
374 if ($status == self::STATUS_CLOSED) {
375 $statusType =
'status6';
380 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
381 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
383 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
462 public function fetch($id, $ref =
'')
465 if (empty($id) && empty($ref)) {
472 $sql .=
" t.fk_contrat,";
473 $sql .=
" t.fk_product,";
474 $sql .=
" t.statut,";
476 $sql .=
" p.ref as product_ref,";
477 $sql .=
" p.label as product_label,";
478 $sql .=
" p.description as product_description,";
479 $sql .=
" p.fk_product_type as product_type,";
480 $sql .=
" t.description,";
481 $sql .=
" t.date_commande,";
482 $sql .=
" t.date_ouverture_prevue as date_start,";
483 $sql .=
" t.date_ouverture as date_start_real,";
484 $sql .=
" t.date_fin_validite as date_end,";
485 $sql .=
" t.date_cloture as date_end_real,";
486 $sql .=
" t.tva_tx,";
487 $sql .=
" t.vat_src_code,";
488 $sql .=
" t.localtax1_tx,";
489 $sql .=
" t.localtax2_tx,";
490 $sql .=
" t.localtax1_type,";
491 $sql .=
" t.localtax2_type,";
493 $sql .=
" t.remise_percent,";
494 $sql .=
" t.fk_remise_except,";
495 $sql .=
" t.subprice,";
496 $sql .=
" t.total_ht,";
497 $sql .=
" t.total_tva,";
498 $sql .=
" t.total_localtax1,";
499 $sql .=
" t.total_localtax2,";
500 $sql .=
" t.total_ttc,";
501 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
502 $sql .=
" t.buy_price_ht as pa_ht,";
503 $sql .=
" t.info_bits,";
504 $sql .=
" t.fk_user_author,";
505 $sql .=
" t.fk_user_ouverture,";
506 $sql .=
" t.fk_user_cloture,";
507 $sql .=
" t.commentaire,";
508 $sql .=
" t.fk_unit,";
509 $sql .=
" t.extraparams,";
511 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
513 $sql .=
" WHERE t.rowid = ".((int) $id);
516 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
519 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
520 $resql = $this->db->query($sql);
522 if ($this->db->num_rows($resql)) {
523 $obj = $this->db->fetch_object($resql);
525 $this->
id = $obj->rowid;
526 $this->
ref = $obj->rowid;
528 $this->tms = $this->db->jdate($obj->tms);
529 $this->fk_contrat = $obj->fk_contrat;
530 $this->fk_product = $obj->fk_product;
531 $this->statut = $obj->statut;
532 $this->product_ref = $obj->product_ref;
533 $this->product_label = $obj->product_label;
534 $this->product_type = $obj->product_type;
535 $this->label = $obj->label;
537 $this->date_commande = $this->db->jdate($obj->date_commande);
539 $this->date_start = $this->db->jdate($obj->date_start);
540 $this->date_start_real = $this->db->jdate($obj->date_start_real);
541 $this->date_end = $this->db->jdate($obj->date_end);
542 $this->date_end_real = $this->db->jdate($obj->date_end_real);
544 $this->tva_tx = $obj->tva_tx;
545 $this->vat_src_code = $obj->vat_src_code;
546 $this->localtax1_tx = $obj->localtax1_tx;
547 $this->localtax2_tx = $obj->localtax2_tx;
548 $this->localtax1_type = $obj->localtax1_type;
549 $this->localtax2_type = $obj->localtax2_type;
550 $this->qty = $obj->qty;
551 $this->remise_percent = $obj->remise_percent;
552 $this->fk_remise_except = $obj->fk_remise_except;
553 $this->subprice = $obj->subprice;
554 $this->total_ht = $obj->total_ht;
555 $this->total_tva = $obj->total_tva;
556 $this->total_localtax1 = $obj->total_localtax1;
557 $this->total_localtax2 = $obj->total_localtax2;
558 $this->total_ttc = $obj->total_ttc;
559 $this->info_bits = $obj->info_bits;
560 $this->fk_user_author = $obj->fk_user_author;
561 $this->fk_user_ouverture = $obj->fk_user_ouverture;
562 $this->fk_user_cloture = $obj->fk_user_cloture;
563 $this->commentaire = $obj->commentaire;
564 $this->fk_fournprice = $obj->fk_fournprice;
566 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
567 $this->pa_ht = $marginInfos[0];
568 $this->fk_unit = $obj->fk_unit;
570 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
572 $this->rang = $obj->rang;
577 $this->db->free($resql);
581 $this->error =
"Error ".$this->db->lasterror();
594 public function update($user, $notrigger = 0)
601 $this->fk_contrat = (int) $this->fk_contrat;
602 $this->fk_product = (int) $this->fk_product;
603 $this->statut = (int) $this->statut;
604 $this->label = trim($this->label);
606 $this->vat_src_code = trim($this->vat_src_code);
607 $this->tva_tx = trim((
string) $this->tva_tx);
608 $this->localtax1_tx = trim($this->localtax1_tx);
609 $this->localtax2_tx = trim($this->localtax2_tx);
610 $this->qty = (float) $this->qty;
611 $this->remise_percent = trim((
string) $this->remise_percent);
612 $this->fk_remise_except = (int) $this->fk_remise_except;
613 $this->subprice = (float)
price2num($this->subprice);
614 $this->info_bits = (int) $this->info_bits;
615 $this->fk_user_author = (int) $this->fk_user_author;
616 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
617 $this->fk_user_cloture = (int) $this->fk_user_cloture;
618 $this->commentaire = trim($this->commentaire);
619 $this->rang = (int) $this->rang;
620 if (empty($this->subprice)) {
623 if (empty($this->total_ht)) {
626 if (empty($this->total_tva)) {
627 $this->total_tva = 0;
629 if (empty($this->total_ttc)) {
630 $this->total_ttc = 0;
632 if (empty($this->localtax1_tx)) {
633 $this->localtax1_tx = 0;
635 if (empty($this->localtax2_tx)) {
636 $this->localtax2_tx = 0;
638 if (empty($this->remise_percent)) {
639 $this->remise_percent = 0;
647 $tabprice =
calcul_price_total($this->qty, $this->subprice, $this->remise_percent, (
float) $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0,
'HT', 0, 1,
$mysoc, $localtaxes_type);
648 $this->total_ht = (float) $tabprice[0];
649 $this->total_tva = (float) $tabprice[1];
650 $this->total_ttc = (float) $tabprice[2];
651 $this->total_localtax1 = (float) $tabprice[9];
652 $this->total_localtax2 = (float) $tabprice[10];
654 if (empty($this->pa_ht)) {
659 if ($this->pa_ht == 0) {
660 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
664 $this->pa_ht = $result;
669 if (empty($this->oldcopy)) {
670 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
677 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
678 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
679 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
680 $sql .=
" statut = ".((int) $this->statut).
",";
681 $sql .=
" label = '".$this->db->escape($this->label).
"',";
682 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
683 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
684 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
685 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
686 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
687 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
688 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
689 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
690 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
691 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
692 $sql .=
" qty = ".price2num($this->qty).
",";
693 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
694 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
695 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
696 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
697 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
698 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
699 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
700 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
701 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
702 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
703 $sql .=
" info_bits = '".$this->db->escape((
string) $this->info_bits).
"',";
704 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
705 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
706 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
707 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
708 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit)).
",";
709 $sql .=
" rang = ".(empty($this->rang) ?
'0' : ((int) $this->rang));
710 $sql .=
" WHERE rowid = ".((int) $this->
id);
712 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
713 $resql = $this->db->query($sql);
715 $this->error =
"Error ".$this->db->lasterror();
728 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
730 if ($this->date_start != $this->oldcopy->date_start) {
731 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
732 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
733 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
735 $resql = $this->db->query($sql);
738 $this->error =
"Error ".$this->db->lasterror();
741 if ($this->date_end != $this->oldcopy->date_end) {
742 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
743 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
744 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
746 $resql = $this->db->query($sql);
749 $this->error =
"Error ".$this->db->lasterror();
754 if (!$error && !$notrigger) {
756 $result = $this->call_trigger(
'LINECONTRACT_MODIFY', $user);
759 $this->db->rollback();
768 $this->db->rollback();
769 $this->errors[] = $this->error;
823 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
824 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
825 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
826 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
827 $sql .=
" info_bits,";
829 $sql .=
" fk_product_fournisseur_price, buy_price_ht";
830 if ($this->date_start > 0) {
831 $sql .=
",date_ouverture_prevue";
833 if ($this->date_end > 0) {
834 $sql .=
",date_fin_validite";
836 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
837 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
838 $sql .=
" '".$this->db->escape((
string) $this->qty).
"',";
839 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
840 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
841 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
842 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
843 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
844 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
845 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
846 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
847 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
848 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
849 if ($this->fk_fournprice > 0) {
850 $sql .=
' '.((int) $this->fk_fournprice).
',';
854 if ($this->pa_ht > 0) {
855 $sql .=
' '.((float)
price2num($this->pa_ht));
859 if ($this->date_start > 0) {
860 $sql .=
",'".$this->db->idate($this->date_start).
"'";
862 if ($this->date_end > 0) {
863 $sql .=
",'".$this->db->idate($this->date_end).
"'";
867 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
869 $resql = $this->db->query($sql);
871 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
876 $this->db->rollback();
882 $result = $this->call_trigger(
'LINECONTRACT_INSERT', $user);
884 $this->db->rollback();
893 $this->db->rollback();
894 $this->error = $this->db->error().
" sql=".$sql;
909 public function active_line($user, $date, $date_end =
'', $comment =
'')
916 $this->statut = ContratLigne::STATUS_OPEN;
917 $this->
status = ContratLigne::STATUS_OPEN;
918 $this->date_start_real = $date;
919 $this->date_end = $date_end;
920 $this->fk_user_ouverture = $user->id;
921 $this->date_end_real =
null;
922 $this->commentaire = $comment;
924 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->
status).
",";
925 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
926 if ($date_end >= 0) {
927 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
929 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
930 $sql .=
" date_cloture = null,";
931 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
932 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
934 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
935 $resql = $this->db->query($sql);
937 if ($date_end >= 0) {
939 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
940 $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.
")";
941 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
942 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
943 if (!$resqltoupdatecontract) {
944 $this->error = $this->db->lasterror();
945 $this->db->rollback();
951 $result = $this->call_trigger(
'LINECONTRACT_ACTIVATE', $user);
961 $this->db->rollback();
965 $this->error = $this->db->lasterror();
966 $this->db->rollback();
981 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
984 $this->date_cloture = $date_end_real;
985 $this->date_end_real = $date_end_real;
986 $this->user_closing_id = $user->id;
987 $this->commentaire = $comment;
995 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
996 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
997 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
998 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
999 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut <> ".((int) ContratLigne::STATUS_CLOSED);
1001 $resql = $this->db->query($sql);
1004 $sqltoupdatecontract =
"UPDATE ".MAIN_DB_PREFIX.
"contrat";
1005 $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.
")";
1006 $sqltoupdatecontract .=
" WHERE rowid = ".((int) $this->fk_contrat);
1007 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1008 if (!$resqltoupdatecontract) {
1009 $this->error = $this->db->lasterror();
1010 $this->db->rollback();
1016 $result = $this->call_trigger(
'LINECONTRACT_CLOSE', $user);
1019 $this->db->rollback();
1025 $this->db->commit();
1028 $this->error = $this->db->lasterror();
1029 $this->db->rollback();