369 $sql =
'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,';
370 $sql .=
' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
371 $sql .=
' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
372 $sql .=
' pd.fk_unit,';
373 $sql .=
' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
374 $sql .=
' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
375 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
376 $sql .=
' p.packaging,';
377 $sql .=
' pd.date_start, pd.date_end, pd.product_type, pd.extraparams';
378 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
379 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
380 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
382 $result = $this->db->query($sql);
384 $objp = $this->db->fetch_object($result);
387 $this->
id = $objp->rowid;
388 $this->rowid = $objp->rowid;
389 $this->fk_propal = $objp->fk_propal;
390 $this->fk_parent_line = $objp->fk_parent_line;
391 $this->label = $objp->custom_label;
392 $this->desc = $objp->description;
393 $this->qty = $objp->qty;
394 $this->
price = $objp->price;
395 $this->subprice = $objp->subprice;
396 $this->vat_src_code = $objp->vat_src_code;
397 $this->tva_tx = $objp->tva_tx;
398 $this->remise = $objp->remise;
399 $this->remise_percent = $objp->remise_percent;
400 $this->fk_remise_except = $objp->fk_remise_except;
401 $this->fk_product = $objp->fk_product;
402 $this->info_bits = $objp->info_bits;
404 $this->total_ht = $objp->total_ht;
405 $this->total_tva = $objp->total_tva;
406 $this->total_ttc = $objp->total_ttc;
408 $this->fk_fournprice = $objp->fk_fournprice;
410 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
411 $this->pa_ht = $marginInfos[0];
412 $this->marge_tx = $marginInfos[1];
413 $this->marque_tx = $marginInfos[2];
415 $this->special_code = $objp->special_code;
416 $this->product_type = $objp->product_type;
417 $this->rang = $objp->rang;
419 $this->
ref = $objp->product_ref;
420 $this->product_ref = $objp->product_ref;
421 $this->libelle = $objp->product_label;
422 $this->product_label = $objp->product_label;
423 $this->product_desc = $objp->product_desc;
424 $this->fk_unit = $objp->fk_unit;
426 $this->packaging = $objp->packaging;
428 $this->date_start = $this->db->jdate($objp->date_start);
429 $this->date_end = $this->db->jdate($objp->date_end);
431 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
434 $this->fk_multicurrency = $objp->fk_multicurrency;
435 $this->multicurrency_code = $objp->multicurrency_code;
436 $this->multicurrency_subprice = $objp->multicurrency_subprice;
437 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
438 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
439 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
443 $this->db->free($result);
466 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
468 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
469 $this->pa_ht = (float) $this->pa_ht;
472 if (empty($this->tva_tx)) {
475 if (empty($this->localtax1_tx)) {
476 $this->localtax1_tx = 0;
478 if (empty($this->localtax2_tx)) {
479 $this->localtax2_tx = 0;
481 if (empty($this->localtax1_type)) {
482 $this->localtax1_type = 0;
484 if (empty($this->localtax2_type)) {
485 $this->localtax2_type = 0;
487 if (empty($this->total_localtax1)) {
488 $this->total_localtax1 = 0;
490 if (empty($this->total_localtax2)) {
491 $this->total_localtax2 = 0;
493 if (empty($this->rang)) {
496 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
497 $this->remise_percent = 0;
499 if (empty($this->info_bits)) {
500 $this->info_bits = 0;
502 if (empty($this->special_code)) {
503 $this->special_code = 0;
505 if (empty($this->fk_parent_line)) {
506 $this->fk_parent_line = 0;
508 if (empty($this->fk_fournprice)) {
509 $this->fk_fournprice = 0;
511 if (!is_numeric($this->qty)) {
514 if (empty($this->multicurrency_subprice)) {
515 $this->multicurrency_subprice = 0;
517 if (empty($this->multicurrency_total_ht)) {
518 $this->multicurrency_total_ht = 0;
520 if (empty($this->multicurrency_total_tva)) {
521 $this->multicurrency_total_tva = 0;
523 if (empty($this->multicurrency_total_ttc)) {
524 $this->multicurrency_total_ttc = 0;
528 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
529 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
532 $this->pa_ht = $result;
537 if ($this->product_type < 0) {
544 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
545 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
546 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
547 $sql .=
' subprice, remise_percent, ';
548 $sql .=
' info_bits, ';
549 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
551 $sql .=
' date_start, date_end';
552 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
553 $sql .=
" VALUES (".$this->fk_propal.
",";
554 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
555 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
556 $sql .=
" '".$this->db->escape($this->desc).
"',";
557 $sql .=
" ".($this->fk_product > 0 ? (int) $this->fk_product :
"null").
",";
558 $sql .=
" ".((int) $this->product_type).
",";
559 $sql .=
" ".($this->fk_remise_except > 0 ? (int) $this->fk_remise_except :
"null").
",";
560 $sql .=
" ".price2num($this->qty,
'MS').
",";
561 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
562 $sql .=
" ".price2num($this->tva_tx).
",";
563 $sql .=
" ".price2num($this->localtax1_tx).
",";
564 $sql .=
" ".price2num($this->localtax2_tx).
",";
565 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
566 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
567 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
568 $sql .=
" ".price2num($this->remise_percent).
",";
569 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
570 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
571 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
572 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
573 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
574 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
575 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null").
",";
576 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
577 $sql .=
' '.((int) $this->special_code).
',';
578 $sql .=
' '.((int) $this->rang).
',';
579 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
580 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
581 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
582 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
583 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
584 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
585 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
586 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
587 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
590 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
591 $resql = $this->db->query($sql);
593 $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
594 $this->
id = $this->rowid;
600 if (!$error && !$notrigger) {
602 $result = $this->
call_trigger(
'LINEPROPAL_INSERT', $user);
604 $this->db->rollback();
612 return (
int) $this->id;
615 foreach ($this->errors as $errmsg) {
616 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
617 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
619 $this->db->rollback();
622 $this->error = $this->db->error().
" sql=".$sql;
623 $this->db->rollback();
691 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
693 if (empty($this->
id) && !empty($this->rowid)) {
694 $this->
id = $this->rowid;
698 if (empty($this->tva_tx)) {
701 if (empty($this->localtax1_tx)) {
702 $this->localtax1_tx = 0;
704 if (empty($this->localtax2_tx)) {
705 $this->localtax2_tx = 0;
707 if (empty($this->total_localtax1)) {
708 $this->total_localtax1 = 0;
710 if (empty($this->total_localtax2)) {
711 $this->total_localtax2 = 0;
713 if (empty($this->localtax1_type)) {
714 $this->localtax1_type = 0;
716 if (empty($this->localtax2_type)) {
717 $this->localtax2_type = 0;
719 if (empty($this->marque_tx)) {
720 $this->marque_tx = 0;
722 if (empty($this->marge_tx)) {
725 if (empty($this->
price)) {
728 if (empty($this->remise_percent)) {
729 $this->remise_percent = 0;
731 if (empty($this->info_bits)) {
732 $this->info_bits = 0;
734 if (empty($this->special_code)) {
735 $this->special_code = 0;
737 if (empty($this->fk_parent_line)) {
738 $this->fk_parent_line = 0;
740 if (empty($this->fk_fournprice)) {
741 $this->fk_fournprice = 0;
743 if (empty($this->subprice)) {
746 if (empty($this->pa_ht)) {
751 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
752 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
755 $this->pa_ht = $result;
762 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
763 $sql .=
" description = '".$this->db->escape($this->desc).
"'";
764 $sql .=
", label = ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
765 $sql .=
", product_type = ".((int) $this->product_type);
766 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
767 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
768 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
769 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
770 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
771 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
772 $sql .=
", qty = ".((float)
price2num($this->qty));
773 $sql .=
", subprice = ".price2num($this->subprice);
774 $sql .=
", remise_percent = ".price2num($this->remise_percent);
776 $sql .=
", remise = ".(float)
price2num($this->remise);
777 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
778 if (empty($this->skip_update_total)) {
779 $sql .=
", total_ht = ".price2num($this->total_ht);
780 $sql .=
", total_tva = ".price2num($this->total_tva);
781 $sql .=
", total_ttc = ".price2num($this->total_ttc);
782 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
783 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
785 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
786 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
787 $sql .=
", special_code=".((int) $this->special_code);
788 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? (int) $this->fk_parent_line :
"null");
789 if (!empty($this->rang)) {
790 $sql .=
", rang=".((int) $this->rang);
792 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
793 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
794 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
797 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
798 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
799 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
800 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
802 $sql .=
" WHERE rowid = ".((int) $this->
id);
804 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
805 $resql = $this->db->query($sql);
814 if (!$error && !$notrigger) {
816 $result = $this->
call_trigger(
'LINEPROPAL_MODIFY', $user);
818 $this->db->rollback();
829 foreach ($this->errors as $errmsg) {
830 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
831 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
833 $this->db->rollback();
836 $this->error = $this->db->error();
837 $this->db->rollback();