377 $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,';
378 $sql .=
' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice, pd.subprice_ttc,';
379 $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,';
380 $sql .=
' pd.fk_unit,';
381 $sql .=
' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
382 $sql .=
' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
383 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,p.barcode as product_barcode,';
384 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units,';
385 $sql .=
' p.customcode, p.fk_country as country_id, c.code as country_code,';
386 $sql .=
' p.packaging,';
387 $sql .=
' pd.date_start, pd.date_end, pd.product_type, pd.extraparams';
388 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
389 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
390 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_country as c ON c.rowid = p.fk_country';
391 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
393 $result = $this->db->query($sql);
395 $objp = $this->db->fetch_object($result);
398 $this->
id = $objp->rowid;
399 $this->rowid = $objp->rowid;
400 $this->fk_propal = $objp->fk_propal;
401 $this->fk_parent_line = $objp->fk_parent_line;
402 $this->label = $objp->custom_label;
403 $this->desc = $objp->description;
404 $this->qty = $objp->qty;
405 $this->
price = $objp->price;
406 $this->subprice = $objp->subprice;
407 $this->subprice_ttc = $objp->subprice_ttc;
408 $this->vat_src_code = $objp->vat_src_code;
409 $this->tva_tx = $objp->tva_tx;
410 $this->remise = $objp->remise;
411 $this->remise_percent = $objp->remise_percent;
412 $this->fk_remise_except = $objp->fk_remise_except;
413 $this->fk_product = $objp->fk_product;
414 $this->info_bits = $objp->info_bits;
416 $this->total_ht = $objp->total_ht;
417 $this->total_tva = $objp->total_tva;
418 $this->total_ttc = $objp->total_ttc;
420 $this->fk_fournprice = $objp->fk_fournprice;
422 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
423 $this->pa_ht = $marginInfos[0];
424 $this->marge_tx = $marginInfos[1];
425 $this->marque_tx = $marginInfos[2];
427 $this->special_code = $objp->special_code;
428 $this->product_type = $objp->product_type;
429 $this->rang = $objp->rang;
431 $this->
ref = $objp->product_ref;
432 $this->product_ref = $objp->product_ref;
433 $this->libelle = $objp->product_label;
434 $this->product_label = $objp->product_label;
435 $this->product_desc = $objp->product_desc;
436 $this->product_barcode = $objp->product_barcode;
437 $this->product_custom_code = $objp->customcode;
438 $this->product_custom_country_id = $objp->country_id;
439 $this->product_custom_country_code = $objp->country_code;
441 $this->fk_unit = $objp->fk_unit;
442 $this->weight = $objp->weight;
443 $this->weight_units = $objp->weight_units;
444 $this->volume = $objp->volume;
445 $this->volume_units = $objp->volume_units;
447 $this->packaging = $objp->packaging;
449 $this->date_start = $this->db->jdate($objp->date_start);
450 $this->date_end = $this->db->jdate($objp->date_end);
452 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
455 $this->fk_multicurrency = $objp->fk_multicurrency;
456 $this->multicurrency_code = $objp->multicurrency_code;
457 $this->multicurrency_subprice = $objp->multicurrency_subprice;
458 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
459 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
460 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
464 $this->db->free($result);
487 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
489 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
490 $this->pa_ht = (float) $this->pa_ht;
493 if (empty($this->tva_tx)) {
496 if (empty($this->localtax1_tx)) {
497 $this->localtax1_tx = 0;
499 if (empty($this->localtax2_tx)) {
500 $this->localtax2_tx = 0;
502 if (empty($this->localtax1_type)) {
503 $this->localtax1_type = 0;
505 if (empty($this->localtax2_type)) {
506 $this->localtax2_type = 0;
508 if (empty($this->total_localtax1)) {
509 $this->total_localtax1 = 0;
511 if (empty($this->total_localtax2)) {
512 $this->total_localtax2 = 0;
514 if (empty($this->subprice_ttc)) {
515 $this->subprice_ttc = 0;
517 if (empty($this->rang)) {
520 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
521 $this->remise_percent = 0;
523 if (empty($this->info_bits)) {
524 $this->info_bits = 0;
526 if (empty($this->special_code)) {
527 $this->special_code = 0;
529 if (empty($this->fk_parent_line)) {
530 $this->fk_parent_line = 0;
532 if (empty($this->fk_fournprice)) {
533 $this->fk_fournprice = 0;
535 if (!is_numeric($this->qty)) {
538 if (empty($this->multicurrency_subprice)) {
539 $this->multicurrency_subprice = 0;
541 if (empty($this->multicurrency_total_ht)) {
542 $this->multicurrency_total_ht = 0;
544 if (empty($this->multicurrency_total_tva)) {
545 $this->multicurrency_total_tva = 0;
547 if (empty($this->multicurrency_total_ttc)) {
548 $this->multicurrency_total_ttc = 0;
552 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
553 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
556 $this->pa_ht = $result;
561 if ($this->product_type < 0) {
568 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
569 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
570 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
571 $sql .=
' subprice, subprice_ttc, remise_percent, ';
572 $sql .=
' info_bits, ';
573 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
575 $sql .=
' date_start, date_end';
576 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
577 $sql .=
" VALUES (".((int) $this->fk_propal).
",";
578 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
579 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
580 $sql .=
" '".$this->db->escape($this->desc).
"',";
581 $sql .=
" ".($this->fk_product > 0 ? (int) $this->fk_product :
"null").
",";
582 $sql .=
" ".((int) $this->product_type).
",";
583 $sql .=
" ".($this->fk_remise_except > 0 ? (int) $this->fk_remise_except :
"null").
",";
584 $sql .=
" ".price2num($this->qty,
'MS').
",";
585 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
586 $sql .=
" ".price2num($this->tva_tx).
",";
587 $sql .=
" ".price2num($this->localtax1_tx).
",";
588 $sql .=
" ".price2num($this->localtax2_tx).
",";
589 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
590 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
591 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
592 $sql .=
" ".price2num($this->subprice_ttc,
'MU').
",";
593 $sql .=
" ".price2num($this->remise_percent).
",";
594 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
595 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
596 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
597 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
598 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
599 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
600 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null").
",";
601 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
602 $sql .=
' '.((int) $this->special_code).
',';
603 $sql .=
' '.((int) $this->rang).
',';
604 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
605 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
606 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
607 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
608 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
609 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
610 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
611 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
612 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
615 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
616 $resql = $this->db->query($sql);
618 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
619 $this->rowid = $this->id;
625 if (!$error && !$notrigger) {
627 $result = $this->call_trigger(
'LINEPROPAL_INSERT', $user);
629 $this->db->rollback();
637 return (
int) $this->id;
640 foreach ($this->errors as $errmsg) {
641 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
642 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
644 $this->db->rollback();
647 $this->error = $this->db->error().
" sql=".$sql;
648 $this->db->rollback();
714 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
716 if (empty($this->
id) && !empty($this->rowid)) {
717 $this->
id = $this->rowid;
721 if (empty($this->tva_tx)) {
724 if (empty($this->localtax1_tx)) {
725 $this->localtax1_tx = 0;
727 if (empty($this->localtax2_tx)) {
728 $this->localtax2_tx = 0;
730 if (empty($this->total_localtax1)) {
731 $this->total_localtax1 = 0;
733 if (empty($this->total_localtax2)) {
734 $this->total_localtax2 = 0;
736 if (empty($this->localtax1_type)) {
737 $this->localtax1_type = 0;
739 if (empty($this->localtax2_type)) {
740 $this->localtax2_type = 0;
742 if (empty($this->marque_tx)) {
743 $this->marque_tx = 0;
745 if (empty($this->marge_tx)) {
748 if (empty($this->
price)) {
751 if (empty($this->remise_percent)) {
752 $this->remise_percent = 0;
754 if (empty($this->info_bits)) {
755 $this->info_bits = 0;
757 if (empty($this->special_code)) {
758 $this->special_code = 0;
760 if (empty($this->fk_parent_line)) {
761 $this->fk_parent_line = 0;
763 if (empty($this->fk_fournprice)) {
764 $this->fk_fournprice = 0;
766 if (empty($this->subprice)) {
769 if (empty($this->subprice_ttc)) {
770 $this->subprice_ttc = 0;
772 if (empty($this->pa_ht)) {
777 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
778 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
781 $this->pa_ht = $result;
788 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
789 $sql .=
" description = '".$this->db->escape($this->desc).
"'";
790 $sql .=
", label = ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
791 $sql .=
", product_type = ".((int) $this->product_type);
792 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
793 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
794 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
795 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
796 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
797 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
798 $sql .=
", qty = ".((float)
price2num($this->qty));
799 $sql .=
", subprice = ".price2num($this->subprice);
800 $sql .=
", subprice_ttc = ".price2num($this->subprice_ttc);
801 $sql .=
", remise_percent = ".price2num($this->remise_percent);
803 $sql .=
", remise = ".(float)
price2num($this->remise);
804 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
805 if (empty($this->skip_update_total)) {
806 $sql .=
", total_ht = ".price2num($this->total_ht);
807 $sql .=
", total_tva = ".price2num($this->total_tva);
808 $sql .=
", total_ttc = ".price2num($this->total_ttc);
809 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
810 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
812 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
813 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
814 $sql .=
", special_code=".((int) $this->special_code);
815 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? (int) $this->fk_parent_line :
"null");
816 if (!empty($this->rang)) {
817 $sql .=
", rang=".((int) $this->rang);
819 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
820 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
821 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
824 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
825 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
826 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
827 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
829 $sql .=
" WHERE rowid = ".((int) $this->
id);
831 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
832 $resql = $this->db->query($sql);
839 if (!$error && !$notrigger) {
841 $result = $this->call_trigger(
'LINEPROPAL_MODIFY', $user);
843 $this->db->rollback();
854 foreach ($this->errors as $errmsg) {
855 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
856 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
858 $this->db->rollback();
861 $this->error = $this->db->error();
862 $this->db->rollback();