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);
549 $this->tva_tx = $obj->tva_tx;
550 $this->vat_src_code = $obj->vat_src_code;
551 $this->localtax1_tx = $obj->localtax1_tx;
552 $this->localtax2_tx = $obj->localtax2_tx;
553 $this->localtax1_type = $obj->localtax1_type;
554 $this->localtax2_type = $obj->localtax2_type;
555 $this->qty = $obj->qty;
556 $this->remise_percent = $obj->remise_percent;
557 $this->fk_remise_except = $obj->fk_remise_except;
558 $this->subprice = $obj->subprice;
559 $this->total_ht = $obj->total_ht;
560 $this->total_tva = $obj->total_tva;
561 $this->total_localtax1 = $obj->total_localtax1;
562 $this->total_localtax2 = $obj->total_localtax2;
563 $this->total_ttc = $obj->total_ttc;
564 $this->info_bits = $obj->info_bits;
565 $this->fk_user_author = $obj->fk_user_author;
566 $this->fk_user_ouverture = $obj->fk_user_ouverture;
567 $this->fk_user_cloture = $obj->fk_user_cloture;
568 $this->commentaire = $obj->commentaire;
569 $this->fk_fournprice = $obj->fk_fournprice;
571 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
572 $this->pa_ht = $marginInfos[0];
573 $this->fk_unit = $obj->fk_unit;
575 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
577 $this->rang = $obj->rang;
582 $this->db->free($resql);
586 $this->error =
"Error ".$this->db->lasterror();
599 public function update($user, $notrigger = 0)
606 $this->fk_contrat = (int) $this->fk_contrat;
607 $this->fk_product = (int) $this->fk_product;
608 $this->statut = (int) $this->statut;
609 $this->label = trim($this->label);
611 $this->vat_src_code = trim($this->vat_src_code);
612 $this->tva_tx = trim((
string) $this->tva_tx);
613 $this->localtax1_tx = trim($this->localtax1_tx);
614 $this->localtax2_tx = trim($this->localtax2_tx);
615 $this->qty = (float) $this->qty;
616 $this->remise_percent = trim((
string) $this->remise_percent);
617 $this->fk_remise_except = (int) $this->fk_remise_except;
618 $this->subprice = (float)
price2num($this->subprice);
619 $this->info_bits = (int) $this->info_bits;
620 $this->fk_user_author = (int) $this->fk_user_author;
621 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
622 $this->fk_user_cloture = (int) $this->fk_user_cloture;
623 $this->commentaire = trim($this->commentaire);
624 $this->rang = (int) $this->rang;
625 if (empty($this->subprice)) {
628 if (empty($this->total_ht)) {
631 if (empty($this->total_tva)) {
632 $this->total_tva = 0;
634 if (empty($this->total_ttc)) {
635 $this->total_ttc = 0;
637 if (empty($this->localtax1_tx)) {
638 $this->localtax1_tx = 0;
640 if (empty($this->localtax2_tx)) {
641 $this->localtax2_tx = 0;
643 if (empty($this->remise_percent)) {
644 $this->remise_percent = 0;
653 $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);
654 $this->total_ht = (float) $tabprice[0];
655 $this->total_tva = (float) $tabprice[1];
656 $this->total_ttc = (float) $tabprice[2];
657 $this->total_localtax1 = (float) $tabprice[9];
658 $this->total_localtax2 = (float) $tabprice[10];
660 if (empty($this->pa_ht)) {
665 if ($this->pa_ht == 0) {
666 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
670 $this->pa_ht = $result;
675 if (empty($this->oldcopy)) {
676 dol_syslog(
"this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
683 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
684 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
685 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
686 $sql .=
" statut = ".((int) $this->statut).
",";
687 $sql .=
" label = '".$this->db->escape($this->label).
"',";
688 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
689 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
690 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
691 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
692 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
693 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
694 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
695 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
696 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
697 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
698 $sql .=
" qty = ".price2num($this->qty).
",";
699 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
700 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
701 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
702 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
703 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
704 $sql .=
" total_localtax1 = ".((float) $this->total_localtax1).
",";
705 $sql .=
" total_localtax2 = ".((float) $this->total_localtax2).
",";
706 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
707 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
708 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
709 $sql .=
" info_bits = '".$this->db->escape((
string) $this->info_bits).
"',";
710 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
711 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
712 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
713 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
714 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
715 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
716 $sql .=
" WHERE rowid = ".((int) $this->
id);
718 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
719 $resql = $this->db->query($sql);
721 $this->error =
"Error ".$this->db->lasterror();
734 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
736 if ($this->date_start != $this->oldcopy->date_start) {
737 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
738 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
739 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
741 $resql = $this->db->query($sql);
744 $this->error =
"Error ".$this->db->lasterror();
747 if ($this->date_end != $this->oldcopy->date_end) {
748 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
749 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
750 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
752 $resql = $this->db->query($sql);
755 $this->error =
"Error ".$this->db->lasterror();
760 if (!$error && !$notrigger) {
762 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
765 $this->db->rollback();
774 $this->db->rollback();
775 $this->errors[] = $this->error;
829 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
830 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
831 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
832 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
833 $sql .=
" info_bits,";
835 $sql .=
" fk_product_fournisseur_price, buy_price_ht";
836 if ($this->date_start > 0) {
837 $sql .=
",date_ouverture_prevue";
839 if ($this->date_end > 0) {
840 $sql .=
",date_fin_validite";
842 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
843 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
844 $sql .=
" '".$this->db->escape((
string) $this->qty).
"',";
845 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
846 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
847 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
848 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
849 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
850 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
851 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
852 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
853 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
854 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
855 if ($this->fk_fournprice > 0) {
856 $sql .=
' '.((int) $this->fk_fournprice).
',';
860 if ($this->pa_ht > 0) {
861 $sql .=
' '.((float)
price2num($this->pa_ht));
865 if ($this->date_start > 0) {
866 $sql .=
",'".$this->db->idate($this->date_start).
"'";
868 if ($this->date_end > 0) {
869 $sql .=
",'".$this->db->idate($this->date_end).
"'";
873 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
875 $resql = $this->db->query($sql);
877 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
883 $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->date_start_real = $date;
926 $this->date_end = $date_end;
927 $this->fk_user_ouverture = $user->id;
928 $this->date_end_real =
null;
929 $this->commentaire = $comment;
931 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->statut).
",";
932 $sql .=
" date_ouverture = ".(dol_strlen((
string) $this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
933 if ($date_end >= 0) {
934 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
936 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
937 $sql .=
" date_cloture = null,";
938 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
939 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
941 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
942 $resql = $this->db->query($sql);
945 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
955 $this->db->rollback();
959 $this->error = $this->db->lasterror();
960 $this->db->rollback();