363 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'', $morelabel =
'')
367 $langs->load(
"contracts");
369 if ($status == self::STATUS_INITIAL) {
370 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
371 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
372 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
373 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
374 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
375 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
376 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
377 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
378 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
379 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
380 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
381 } elseif ($status == self::STATUS_CLOSED) {
382 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
383 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
386 $labelStatusShort =
'';
389 $statusType =
'status'.$status;
390 if ($status == self::STATUS_OPEN && $expired == 1) {
391 $statusType =
'status1';
393 if ($status == self::STATUS_CLOSED) {
394 $statusType =
'status6';
399 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
400 $params = array(
'badgeParams' => array(
'css' => $reg[1]));
402 return dolGetStatus($labelStatus.($morelabel ?
' '.$morelabel :
''), $labelStatusShort.($morelabel ?
' '.$morelabel :
''),
'', $statusType, $mode,
'', $params);
481 public function fetch($id, $ref =
'')
484 if (empty($id) && empty($ref)) {
491 $sql .=
" t.fk_contrat,";
492 $sql .=
" t.fk_product,";
493 $sql .=
" t.statut,";
495 $sql .=
" p.ref as product_ref,";
496 $sql .=
" p.label as product_label,";
497 $sql .=
" p.description as product_desc,";
498 $sql .=
" p.fk_product_type as product_type,";
499 $sql .=
" t.description,";
500 $sql .=
" t.date_commande,";
501 $sql .=
" t.date_ouverture_prevue as date_start,";
502 $sql .=
" t.date_ouverture as date_start_real,";
503 $sql .=
" t.date_fin_validite as date_end,";
504 $sql .=
" t.date_cloture as date_end_real,";
505 $sql .=
" t.tva_tx,";
506 $sql .=
" t.vat_src_code,";
507 $sql .=
" t.localtax1_tx,";
508 $sql .=
" t.localtax2_tx,";
509 $sql .=
" t.localtax1_type,";
510 $sql .=
" t.localtax2_type,";
512 $sql .=
" t.remise_percent,";
513 $sql .=
" t.remise,";
514 $sql .=
" t.fk_remise_except,";
515 $sql .=
" t.subprice,";
516 $sql .=
" t.price_ht,";
517 $sql .=
" t.total_ht,";
518 $sql .=
" t.total_tva,";
519 $sql .=
" t.total_localtax1,";
520 $sql .=
" t.total_localtax2,";
521 $sql .=
" t.total_ttc,";
522 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
523 $sql .=
" t.buy_price_ht as pa_ht,";
524 $sql .=
" t.info_bits,";
525 $sql .=
" t.fk_user_author,";
526 $sql .=
" t.fk_user_ouverture,";
527 $sql .=
" t.fk_user_cloture,";
528 $sql .=
" t.commentaire,";
529 $sql .=
" t.fk_unit,";
531 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
533 $sql .=
" WHERE t.rowid = ".((int) $id);
536 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
539 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
540 $resql = $this->db->query($sql);
542 if ($this->db->num_rows($resql)) {
543 $obj = $this->db->fetch_object($resql);
545 $this->
id = $obj->rowid;
546 $this->
ref = $obj->rowid;
548 $this->tms = $this->db->jdate($obj->tms);
549 $this->fk_contrat = $obj->fk_contrat;
550 $this->fk_product = $obj->fk_product;
551 $this->statut = $obj->statut;
552 $this->product_ref = $obj->product_ref;
553 $this->product_label = $obj->product_label;
554 $this->product_type = $obj->product_type;
555 $this->label = $obj->label;
557 $this->date_commande = $this->db->jdate($obj->date_commande);
559 $this->date_start = $this->db->jdate($obj->date_start);
560 $this->date_start_real = $this->db->jdate($obj->date_start_real);
561 $this->date_end = $this->db->jdate($obj->date_end);
562 $this->date_end_real = $this->db->jdate($obj->date_end_real);
569 $this->tva_tx = $obj->tva_tx;
570 $this->vat_src_code = $obj->vat_src_code;
571 $this->localtax1_tx = $obj->localtax1_tx;
572 $this->localtax2_tx = $obj->localtax2_tx;
573 $this->localtax1_type = $obj->localtax1_type;
574 $this->localtax2_type = $obj->localtax2_type;
575 $this->qty = $obj->qty;
576 $this->remise_percent = $obj->remise_percent;
577 $this->fk_remise_except = $obj->fk_remise_except;
578 $this->subprice = $obj->subprice;
579 $this->price_ht = $obj->price_ht;
580 $this->total_ht = $obj->total_ht;
581 $this->total_tva = $obj->total_tva;
582 $this->total_localtax1 = $obj->total_localtax1;
583 $this->total_localtax2 = $obj->total_localtax2;
584 $this->total_ttc = $obj->total_ttc;
585 $this->info_bits = $obj->info_bits;
586 $this->fk_user_author = $obj->fk_user_author;
587 $this->fk_user_ouverture = $obj->fk_user_ouverture;
588 $this->fk_user_cloture = $obj->fk_user_cloture;
589 $this->commentaire = $obj->commentaire;
590 $this->fk_fournprice = $obj->fk_fournprice;
592 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
593 $this->pa_ht = $marginInfos[0];
594 $this->fk_unit = $obj->fk_unit;
596 $this->rang = $obj->rang;
601 $this->db->free($resql);
605 $this->error =
"Error ".$this->db->lasterror();
618 public function update($user, $notrigger = 0)
625 $this->fk_contrat = (int) $this->fk_contrat;
626 $this->fk_product = (int) $this->fk_product;
627 $this->statut = (int) $this->statut;
628 $this->label = trim($this->label);
630 $this->vat_src_code = trim($this->vat_src_code);
631 $this->tva_tx = trim((
string) $this->tva_tx);
632 $this->localtax1_tx = trim($this->localtax1_tx);
633 $this->localtax2_tx = trim($this->localtax2_tx);
634 $this->qty = (float) $this->qty;
635 $this->remise_percent = trim((
string) $this->remise_percent);
636 $this->fk_remise_except = (int) $this->fk_remise_except;
637 $this->subprice = (float)
price2num($this->subprice);
638 $this->price_ht = (float)
price2num($this->price_ht);
639 $this->info_bits = (int) $this->info_bits;
640 $this->fk_user_author = (int) $this->fk_user_author;
641 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
642 $this->fk_user_cloture = (int) $this->fk_user_cloture;
643 $this->commentaire = trim($this->commentaire);
644 $this->rang = (int) $this->rang;
646 if (empty($this->price_ht)) {
649 if (empty($this->total_ht)) {
652 if (empty($this->total_tva)) {
653 $this->total_tva = 0;
655 if (empty($this->total_ttc)) {
656 $this->total_ttc = 0;
658 if (empty($this->localtax1_tx)) {
659 $this->localtax1_tx = 0;
661 if (empty($this->localtax2_tx)) {
662 $this->localtax2_tx = 0;
664 if (empty($this->remise_percent)) {
665 $this->remise_percent = 0;
674 $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);
675 $this->total_ht = $tabprice[0];
676 $this->total_tva = $tabprice[1];
677 $this->total_ttc = $tabprice[2];
678 $this->total_localtax1 = $tabprice[9];
679 $this->total_localtax2 = $tabprice[10];
681 if (empty($this->pa_ht)) {
686 if ($this->pa_ht == 0) {
687 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
691 $this->pa_ht = $result;
696 if (empty($this->oldcopy)) {
697 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
704 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
705 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
706 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
707 $sql .=
" statut = ".((int) $this->statut).
",";
708 $sql .=
" label = '".$this->db->escape($this->label).
"',";
709 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
710 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
711 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
712 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
713 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
714 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
715 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
716 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
717 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
718 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
719 $sql .=
" qty = ".price2num($this->qty).
",";
720 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
721 $sql .=
" remise = ".($this->remise ?
price2num($this->remise) :
"null").
",";
722 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
723 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
724 $sql .=
" price_ht = ".($this->price_ht !=
'' ? $this->price_ht :
"null").
",";
725 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
726 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
727 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
728 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
729 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
730 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
731 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
732 $sql .=
" info_bits = '".$this->db->escape($this->info_bits).
"',";
733 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
734 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
735 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
736 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
737 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
738 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
739 $sql .=
" WHERE rowid = ".((int) $this->
id);
741 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
742 $resql = $this->db->query($sql);
744 $this->error =
"Error ".$this->db->lasterror();
757 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
759 if ($this->date_start != $this->oldcopy->date_start) {
760 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
761 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
762 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
764 $resql = $this->db->query($sql);
767 $this->error =
"Error ".$this->db->lasterror();
770 if ($this->date_end != $this->oldcopy->date_end) {
771 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
772 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
773 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
775 $resql = $this->db->query($sql);
778 $this->error =
"Error ".$this->db->lasterror();
783 if (!$error && !$notrigger) {
785 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
788 $this->db->rollback();
797 $this->db->rollback();
798 $this->errors[] = $this->error;
852 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
853 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
854 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
855 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
856 $sql .=
" info_bits,";
858 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
859 if ($this->date_start > 0) {
860 $sql .=
",date_ouverture_prevue";
862 if ($this->date_end > 0) {
863 $sql .=
",date_fin_validite";
865 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
866 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
867 $sql .=
" '".$this->db->escape($this->qty).
"',";
868 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
869 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
870 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
871 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
872 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
873 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
874 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
875 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
876 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
877 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
878 $sql .=
" ".price2num($this->price_ht).
",".
price2num($this->remise).
",";
879 if ($this->fk_fournprice > 0) {
880 $sql .=
' '.((int) $this->fk_fournprice).
',';
884 if ($this->pa_ht > 0) {
885 $sql .=
' '.((float)
price2num($this->pa_ht));
889 if ($this->date_start > 0) {
890 $sql .=
",'".$this->db->idate($this->date_start).
"'";
892 if ($this->date_end > 0) {
893 $sql .=
",'".$this->db->idate($this->date_end).
"'";
897 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
899 $resql = $this->db->query($sql);
901 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
907 $this->db->rollback();
914 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
916 $this->db->rollback();
925 $this->db->rollback();
926 $this->error = $this->db->error().
" sql=".$sql;
941 public function active_line($user, $date, $date_end =
'', $comment =
'')
948 $this->statut = ContratLigne::STATUS_OPEN;
949 $this->date_start_real = $date;
950 $this->date_end = $date_end;
951 $this->fk_user_ouverture = $user->id;
952 $this->date_end_real =
null;
953 $this->commentaire = $comment;
955 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->statut).
",";
956 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
957 if ($date_end >= 0) {
958 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
960 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
961 $sql .=
" date_cloture = null,";
962 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
963 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
965 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
966 $resql = $this->db->query($sql);
969 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
979 $this->db->rollback();
983 $this->error = $this->db->lasterror();
984 $this->db->rollback();