389 $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,';
390 $sql .=
' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice, pd.subprice_ttc,';
391 $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,';
392 $sql .=
' pd.fk_unit,';
393 $sql .=
' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
394 $sql .=
' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
395 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,p.barcode as product_barcode,';
396 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units,';
397 $sql .=
' p.customcode, p.fk_country as country_id, c.code as country_code,';
398 $sql .=
' p.packaging,';
399 $sql .=
' pd.date_start, pd.date_end, pd.product_type, pd.extraparams';
400 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
401 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
402 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_country as c ON c.rowid = p.fk_country';
403 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
405 $result = $this->db->query($sql);
407 $objp = $this->db->fetch_object($result);
410 $this->
id = $objp->rowid;
411 $this->rowid = $objp->rowid;
412 $this->fk_propal = $objp->fk_propal;
413 $this->fk_parent_line = $objp->fk_parent_line;
414 $this->label = $objp->custom_label;
415 $this->desc = $objp->description;
416 $this->qty = $objp->qty;
417 $this->
price = $objp->price;
418 $this->subprice = $objp->subprice;
419 $this->subprice_ttc = $objp->subprice_ttc;
420 $this->vat_src_code = $objp->vat_src_code;
421 $this->tva_tx = $objp->tva_tx;
422 $this->remise = $objp->remise;
423 $this->remise_percent = $objp->remise_percent;
424 $this->fk_remise_except = $objp->fk_remise_except;
425 $this->fk_product = $objp->fk_product;
426 $this->info_bits = $objp->info_bits;
428 $this->total_ht = $objp->total_ht;
429 $this->total_tva = $objp->total_tva;
430 $this->total_ttc = $objp->total_ttc;
432 $this->fk_fournprice = $objp->fk_fournprice;
434 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
435 $this->pa_ht = $marginInfos[0];
436 $this->marge_tx = $marginInfos[1];
437 $this->marque_tx = $marginInfos[2];
439 $this->special_code = $objp->special_code;
440 $this->product_type = $objp->product_type;
441 $this->rang = $objp->rang;
443 $this->
ref = $objp->product_ref;
444 $this->product_ref = $objp->product_ref;
445 $this->libelle = $objp->product_label;
446 $this->product_label = $objp->product_label;
447 $this->product_desc = $objp->product_desc;
448 $this->product_barcode = $objp->product_barcode;
449 $this->product_custom_code = $objp->customcode;
450 $this->product_custom_country_id = $objp->country_id;
451 $this->product_custom_country_code = $objp->country_code;
453 $this->fk_unit = $objp->fk_unit;
454 $this->weight = $objp->weight;
455 $this->weight_units = $objp->weight_units;
456 $this->volume = $objp->volume;
457 $this->volume_units = $objp->volume_units;
459 $this->packaging = $objp->packaging;
461 $this->date_start = $this->db->jdate($objp->date_start);
462 $this->date_end = $this->db->jdate($objp->date_end);
464 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
467 $this->fk_multicurrency = $objp->fk_multicurrency;
468 $this->multicurrency_code = $objp->multicurrency_code;
469 $this->multicurrency_subprice = $objp->multicurrency_subprice;
470 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
471 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
472 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
476 $this->db->free($result);
499 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
501 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
502 $this->pa_ht = (float) $this->pa_ht;
505 if (empty($this->tva_tx)) {
508 if (empty($this->localtax1_tx)) {
509 $this->localtax1_tx = 0;
511 if (empty($this->localtax2_tx)) {
512 $this->localtax2_tx = 0;
514 if (empty($this->localtax1_type)) {
515 $this->localtax1_type = 0;
517 if (empty($this->localtax2_type)) {
518 $this->localtax2_type = 0;
520 if (empty($this->total_localtax1)) {
521 $this->total_localtax1 = 0;
523 if (empty($this->total_localtax2)) {
524 $this->total_localtax2 = 0;
526 if (empty($this->rang)) {
529 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
530 $this->remise_percent = 0;
532 if (empty($this->info_bits)) {
533 $this->info_bits = 0;
535 if (empty($this->special_code)) {
536 $this->special_code = 0;
538 if (empty($this->fk_parent_line)) {
539 $this->fk_parent_line = 0;
541 if (empty($this->fk_fournprice)) {
542 $this->fk_fournprice = 0;
544 if (!is_numeric($this->qty)) {
547 if (empty($this->multicurrency_subprice)) {
548 $this->multicurrency_subprice = 0;
550 if (empty($this->multicurrency_total_ht)) {
551 $this->multicurrency_total_ht = 0;
553 if (empty($this->multicurrency_total_tva)) {
554 $this->multicurrency_total_tva = 0;
556 if (empty($this->multicurrency_total_ttc)) {
557 $this->multicurrency_total_ttc = 0;
561 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
562 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
565 $this->pa_ht = $result;
570 if ($this->product_type < 0) {
577 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
578 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
579 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
580 $sql .=
' subprice, subprice_ttc, remise_percent, ';
581 $sql .=
' info_bits, ';
582 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
584 $sql .=
' date_start, date_end';
585 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
586 $sql .=
" VALUES (".$this->fk_propal.
",";
587 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
588 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
589 $sql .=
" '".$this->db->escape($this->desc).
"',";
590 $sql .=
" ".($this->fk_product > 0 ? (int) $this->fk_product :
"null").
",";
591 $sql .=
" ".((int) $this->product_type).
",";
592 $sql .=
" ".($this->fk_remise_except > 0 ? (int) $this->fk_remise_except :
"null").
",";
593 $sql .=
" ".price2num($this->qty,
'MS').
",";
594 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
595 $sql .=
" ".price2num($this->tva_tx).
",";
596 $sql .=
" ".price2num($this->localtax1_tx).
",";
597 $sql .=
" ".price2num($this->localtax2_tx).
",";
598 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
599 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
600 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
601 $sql .=
" ".price2num($this->subprice_ttc,
'MU').
",";
602 $sql .=
" ".price2num($this->remise_percent).
",";
603 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
604 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
605 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
606 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
607 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
608 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
609 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null").
",";
610 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
611 $sql .=
' '.((int) $this->special_code).
',';
612 $sql .=
' '.((int) $this->rang).
',';
613 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
614 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
615 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
616 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
617 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
618 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
619 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
620 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
621 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
624 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
625 $resql = $this->db->query($sql);
627 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
628 $this->rowid = $this->id;
634 if (!$error && !$notrigger) {
636 $result = $this->call_trigger(
'LINEPROPAL_INSERT', $user);
638 $this->db->rollback();
646 return (
int) $this->id;
649 foreach ($this->errors as $errmsg) {
650 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
651 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
653 $this->db->rollback();
656 $this->error = $this->db->error().
" sql=".$sql;
657 $this->db->rollback();
723 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
725 if (empty($this->
id) && !empty($this->rowid)) {
726 $this->
id = $this->rowid;
730 if (empty($this->tva_tx)) {
733 if (empty($this->localtax1_tx)) {
734 $this->localtax1_tx = 0;
736 if (empty($this->localtax2_tx)) {
737 $this->localtax2_tx = 0;
739 if (empty($this->total_localtax1)) {
740 $this->total_localtax1 = 0;
742 if (empty($this->total_localtax2)) {
743 $this->total_localtax2 = 0;
745 if (empty($this->localtax1_type)) {
746 $this->localtax1_type = 0;
748 if (empty($this->localtax2_type)) {
749 $this->localtax2_type = 0;
751 if (empty($this->marque_tx)) {
752 $this->marque_tx = 0;
754 if (empty($this->marge_tx)) {
757 if (empty($this->
price)) {
760 if (empty($this->remise_percent)) {
761 $this->remise_percent = 0;
763 if (empty($this->info_bits)) {
764 $this->info_bits = 0;
766 if (empty($this->special_code)) {
767 $this->special_code = 0;
769 if (empty($this->fk_parent_line)) {
770 $this->fk_parent_line = 0;
772 if (empty($this->fk_fournprice)) {
773 $this->fk_fournprice = 0;
775 if (empty($this->subprice)) {
778 if (empty($this->pa_ht)) {
783 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
784 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
787 $this->pa_ht = $result;
794 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
795 $sql .=
" description = '".$this->db->escape($this->desc).
"'";
796 $sql .=
", label = ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
797 $sql .=
", product_type = ".((int) $this->product_type);
798 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
799 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
800 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
801 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
802 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
803 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
804 $sql .=
", qty = ".((float)
price2num($this->qty));
805 $sql .=
", subprice = ".price2num($this->subprice);
806 $sql .=
", subprice_ttc = ".price2num($this->subprice_ttc);
807 $sql .=
", remise_percent = ".price2num($this->remise_percent);
809 $sql .=
", remise = ".(float)
price2num($this->remise);
810 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
811 if (empty($this->skip_update_total)) {
812 $sql .=
", total_ht = ".price2num($this->total_ht);
813 $sql .=
", total_tva = ".price2num($this->total_tva);
814 $sql .=
", total_ttc = ".price2num($this->total_ttc);
815 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
816 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
818 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
819 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
820 $sql .=
", special_code=".((int) $this->special_code);
821 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? (int) $this->fk_parent_line :
"null");
822 if (!empty($this->rang)) {
823 $sql .=
", rang=".((int) $this->rang);
825 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
826 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
827 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
830 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
831 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
832 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
833 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
835 $sql .=
" WHERE rowid = ".((int) $this->
id);
837 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
838 $resql = $this->db->query($sql);
845 if (!$error && !$notrigger) {
847 $result = $this->call_trigger(
'LINEPROPAL_MODIFY', $user);
849 $this->db->rollback();
860 foreach ($this->errors as $errmsg) {
861 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
862 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
864 $this->db->rollback();
867 $this->error = $this->db->error();
868 $this->db->rollback();