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,p.barcode as product_barcode,';
376 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units,';
377 $sql .=
' p.customcode, p.fk_country as country_id, c.code as country_code,';
378 $sql .=
' p.packaging,';
379 $sql .=
' pd.date_start, pd.date_end, pd.product_type, pd.extraparams';
380 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
381 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
382 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_country as c ON c.rowid = p.fk_country';
383 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
385 $result = $this->db->query($sql);
387 $objp = $this->db->fetch_object($result);
390 $this->
id = $objp->rowid;
391 $this->rowid = $objp->rowid;
392 $this->fk_propal = $objp->fk_propal;
393 $this->fk_parent_line = $objp->fk_parent_line;
394 $this->label = $objp->custom_label;
395 $this->desc = $objp->description;
396 $this->qty = $objp->qty;
397 $this->
price = $objp->price;
398 $this->subprice = $objp->subprice;
399 $this->vat_src_code = $objp->vat_src_code;
400 $this->tva_tx = $objp->tva_tx;
401 $this->remise = $objp->remise;
402 $this->remise_percent = $objp->remise_percent;
403 $this->fk_remise_except = $objp->fk_remise_except;
404 $this->fk_product = $objp->fk_product;
405 $this->info_bits = $objp->info_bits;
407 $this->total_ht = $objp->total_ht;
408 $this->total_tva = $objp->total_tva;
409 $this->total_ttc = $objp->total_ttc;
411 $this->fk_fournprice = $objp->fk_fournprice;
413 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
414 $this->pa_ht = $marginInfos[0];
415 $this->marge_tx = $marginInfos[1];
416 $this->marque_tx = $marginInfos[2];
418 $this->special_code = $objp->special_code;
419 $this->product_type = $objp->product_type;
420 $this->rang = $objp->rang;
422 $this->
ref = $objp->product_ref;
423 $this->product_ref = $objp->product_ref;
424 $this->libelle = $objp->product_label;
425 $this->product_label = $objp->product_label;
426 $this->product_desc = $objp->product_desc;
427 $this->product_barcode = $objp->product_barcode;
428 $this->product_custom_code = $objp->customcode;
429 $this->product_custom_country_id = $objp->country_id;
430 $this->product_custom_country_code = $objp->country_code;
432 $this->fk_unit = $objp->fk_unit;
433 $this->weight = $objp->weight;
434 $this->weight_units = $objp->weight_units;
435 $this->volume = $objp->volume;
436 $this->volume_units = $objp->volume_units;
438 $this->packaging = $objp->packaging;
440 $this->date_start = $this->db->jdate($objp->date_start);
441 $this->date_end = $this->db->jdate($objp->date_end);
443 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
446 $this->fk_multicurrency = $objp->fk_multicurrency;
447 $this->multicurrency_code = $objp->multicurrency_code;
448 $this->multicurrency_subprice = $objp->multicurrency_subprice;
449 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
450 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
451 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
455 $this->db->free($result);
478 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
480 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
481 $this->pa_ht = (float) $this->pa_ht;
484 if (empty($this->tva_tx)) {
487 if (empty($this->localtax1_tx)) {
488 $this->localtax1_tx = 0;
490 if (empty($this->localtax2_tx)) {
491 $this->localtax2_tx = 0;
493 if (empty($this->localtax1_type)) {
494 $this->localtax1_type = 0;
496 if (empty($this->localtax2_type)) {
497 $this->localtax2_type = 0;
499 if (empty($this->total_localtax1)) {
500 $this->total_localtax1 = 0;
502 if (empty($this->total_localtax2)) {
503 $this->total_localtax2 = 0;
505 if (empty($this->rang)) {
508 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
509 $this->remise_percent = 0;
511 if (empty($this->info_bits)) {
512 $this->info_bits = 0;
514 if (empty($this->special_code)) {
515 $this->special_code = 0;
517 if (empty($this->fk_parent_line)) {
518 $this->fk_parent_line = 0;
520 if (empty($this->fk_fournprice)) {
521 $this->fk_fournprice = 0;
523 if (!is_numeric($this->qty)) {
526 if (empty($this->multicurrency_subprice)) {
527 $this->multicurrency_subprice = 0;
529 if (empty($this->multicurrency_total_ht)) {
530 $this->multicurrency_total_ht = 0;
532 if (empty($this->multicurrency_total_tva)) {
533 $this->multicurrency_total_tva = 0;
535 if (empty($this->multicurrency_total_ttc)) {
536 $this->multicurrency_total_ttc = 0;
540 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
541 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
544 $this->pa_ht = $result;
549 if ($this->product_type < 0) {
556 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
557 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
558 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
559 $sql .=
' subprice, remise_percent, ';
560 $sql .=
' info_bits, ';
561 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
563 $sql .=
' date_start, date_end';
564 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
565 $sql .=
" VALUES (".$this->fk_propal.
",";
566 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
567 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
568 $sql .=
" '".$this->db->escape($this->desc).
"',";
569 $sql .=
" ".($this->fk_product > 0 ? (int) $this->fk_product :
"null").
",";
570 $sql .=
" ".((int) $this->product_type).
",";
571 $sql .=
" ".($this->fk_remise_except > 0 ? (int) $this->fk_remise_except :
"null").
",";
572 $sql .=
" ".price2num($this->qty,
'MS').
",";
573 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
574 $sql .=
" ".price2num($this->tva_tx).
",";
575 $sql .=
" ".price2num($this->localtax1_tx).
",";
576 $sql .=
" ".price2num($this->localtax2_tx).
",";
577 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
578 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
579 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
580 $sql .=
" ".price2num($this->remise_percent).
",";
581 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
582 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
583 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
584 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
585 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
586 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
587 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null").
",";
588 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
589 $sql .=
' '.((int) $this->special_code).
',';
590 $sql .=
' '.((int) $this->rang).
',';
591 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
592 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
593 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
594 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
595 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
596 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
597 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
598 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
599 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
602 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
603 $resql = $this->db->query($sql);
605 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
606 $this->rowid = $this->id;
612 if (!$error && !$notrigger) {
614 $result = $this->call_trigger(
'LINEPROPAL_INSERT', $user);
616 $this->db->rollback();
624 return (
int) $this->id;
627 foreach ($this->errors as $errmsg) {
628 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
629 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
631 $this->db->rollback();
634 $this->error = $this->db->error().
" sql=".$sql;
635 $this->db->rollback();
701 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
703 if (empty($this->
id) && !empty($this->rowid)) {
704 $this->
id = $this->rowid;
708 if (empty($this->tva_tx)) {
711 if (empty($this->localtax1_tx)) {
712 $this->localtax1_tx = 0;
714 if (empty($this->localtax2_tx)) {
715 $this->localtax2_tx = 0;
717 if (empty($this->total_localtax1)) {
718 $this->total_localtax1 = 0;
720 if (empty($this->total_localtax2)) {
721 $this->total_localtax2 = 0;
723 if (empty($this->localtax1_type)) {
724 $this->localtax1_type = 0;
726 if (empty($this->localtax2_type)) {
727 $this->localtax2_type = 0;
729 if (empty($this->marque_tx)) {
730 $this->marque_tx = 0;
732 if (empty($this->marge_tx)) {
735 if (empty($this->
price)) {
738 if (empty($this->remise_percent)) {
739 $this->remise_percent = 0;
741 if (empty($this->info_bits)) {
742 $this->info_bits = 0;
744 if (empty($this->special_code)) {
745 $this->special_code = 0;
747 if (empty($this->fk_parent_line)) {
748 $this->fk_parent_line = 0;
750 if (empty($this->fk_fournprice)) {
751 $this->fk_fournprice = 0;
753 if (empty($this->subprice)) {
756 if (empty($this->pa_ht)) {
761 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
762 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
765 $this->pa_ht = $result;
772 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
773 $sql .=
" description = '".$this->db->escape($this->desc).
"'";
774 $sql .=
", label = ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
775 $sql .=
", product_type = ".((int) $this->product_type);
776 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
777 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
778 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
779 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
780 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
781 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
782 $sql .=
", qty = ".((float)
price2num($this->qty));
783 $sql .=
", subprice = ".price2num($this->subprice);
784 $sql .=
", remise_percent = ".price2num($this->remise_percent);
786 $sql .=
", remise = ".(float)
price2num($this->remise);
787 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
788 if (empty($this->skip_update_total)) {
789 $sql .=
", total_ht = ".price2num($this->total_ht);
790 $sql .=
", total_tva = ".price2num($this->total_tva);
791 $sql .=
", total_ttc = ".price2num($this->total_ttc);
792 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
793 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
795 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
796 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
797 $sql .=
", special_code=".((int) $this->special_code);
798 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? (int) $this->fk_parent_line :
"null");
799 if (!empty($this->rang)) {
800 $sql .=
", rang=".((int) $this->rang);
802 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
803 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
804 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
807 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
808 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
809 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
810 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
812 $sql .=
" WHERE rowid = ".((int) $this->
id);
814 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
815 $resql = $this->db->query($sql);
822 if (!$error && !$notrigger) {
824 $result = $this->call_trigger(
'LINEPROPAL_MODIFY', $user);
826 $this->db->rollback();
837 foreach ($this->errors as $errmsg) {
838 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
839 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
841 $this->db->rollback();
844 $this->error = $this->db->error();
845 $this->db->rollback();