365 $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,';
366 $sql .=
' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
367 $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,';
368 $sql .=
' pd.fk_unit,';
369 $sql .=
' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
370 $sql .=
' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
371 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
372 $sql .=
' pd.date_start, pd.date_end, pd.product_type';
373 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
374 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
375 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
377 $result = $this->db->query($sql);
379 $objp = $this->db->fetch_object($result);
382 $this->
id = $objp->rowid;
383 $this->
rowid = $objp->rowid;
384 $this->fk_propal = $objp->fk_propal;
385 $this->fk_parent_line = $objp->fk_parent_line;
386 $this->label = $objp->custom_label;
387 $this->desc = $objp->description;
388 $this->qty = $objp->qty;
389 $this->
price = $objp->price;
390 $this->subprice = $objp->subprice;
391 $this->vat_src_code = $objp->vat_src_code;
392 $this->tva_tx = $objp->tva_tx;
393 $this->remise = $objp->remise;
394 $this->remise_percent = $objp->remise_percent;
395 $this->fk_remise_except = $objp->fk_remise_except;
396 $this->fk_product = $objp->fk_product;
397 $this->info_bits = $objp->info_bits;
399 $this->total_ht = $objp->total_ht;
400 $this->total_tva = $objp->total_tva;
401 $this->total_ttc = $objp->total_ttc;
403 $this->fk_fournprice = $objp->fk_fournprice;
405 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
406 $this->pa_ht = $marginInfos[0];
407 $this->marge_tx = $marginInfos[1];
408 $this->marque_tx = $marginInfos[2];
410 $this->special_code = $objp->special_code;
411 $this->product_type = $objp->product_type;
412 $this->rang = $objp->rang;
414 $this->
ref = $objp->product_ref;
415 $this->product_ref = $objp->product_ref;
416 $this->libelle = $objp->product_label;
417 $this->product_label = $objp->product_label;
418 $this->product_desc = $objp->product_desc;
419 $this->fk_unit = $objp->fk_unit;
421 $this->date_start = $this->db->jdate($objp->date_start);
422 $this->date_end = $this->db->jdate($objp->date_end);
425 $this->fk_multicurrency = $objp->fk_multicurrency;
426 $this->multicurrency_code = $objp->multicurrency_code;
427 $this->multicurrency_subprice = $objp->multicurrency_subprice;
428 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
429 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
430 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
434 $this->db->free($result);
457 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
459 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
462 if (empty($this->tva_tx)) {
465 if (empty($this->localtax1_tx)) {
466 $this->localtax1_tx = 0;
468 if (empty($this->localtax2_tx)) {
469 $this->localtax2_tx = 0;
471 if (empty($this->localtax1_type)) {
472 $this->localtax1_type = 0;
474 if (empty($this->localtax2_type)) {
475 $this->localtax2_type = 0;
477 if (empty($this->total_localtax1)) {
478 $this->total_localtax1 = 0;
480 if (empty($this->total_localtax2)) {
481 $this->total_localtax2 = 0;
483 if (empty($this->rang)) {
486 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
487 $this->remise_percent = 0;
489 if (empty($this->info_bits)) {
490 $this->info_bits = 0;
492 if (empty($this->special_code)) {
493 $this->special_code = 0;
495 if (empty($this->fk_parent_line)) {
496 $this->fk_parent_line = 0;
498 if (empty($this->fk_fournprice)) {
499 $this->fk_fournprice = 0;
501 if (!is_numeric($this->qty)) {
504 if (empty($this->pa_ht)) {
507 if (empty($this->multicurrency_subprice)) {
508 $this->multicurrency_subprice = 0;
510 if (empty($this->multicurrency_total_ht)) {
511 $this->multicurrency_total_ht = 0;
513 if (empty($this->multicurrency_total_tva)) {
514 $this->multicurrency_total_tva = 0;
516 if (empty($this->multicurrency_total_ttc)) {
517 $this->multicurrency_total_ttc = 0;
521 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
522 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
525 $this->pa_ht = $result;
530 if ($this->product_type < 0) {
537 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
538 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
539 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
540 $sql .=
' subprice, remise_percent, ';
541 $sql .=
' info_bits, ';
542 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
544 $sql .=
' date_start, date_end';
545 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
546 $sql .=
" VALUES (".$this->fk_propal.
",";
547 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
548 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
549 $sql .=
" '".$this->db->escape($this->desc).
"',";
550 $sql .=
" ".($this->fk_product ?
"'".$this->db->escape($this->fk_product).
"'" :
"null").
",";
551 $sql .=
" '".$this->db->escape($this->product_type).
"',";
552 $sql .=
" ".($this->fk_remise_except ?
"'".$this->db->escape($this->fk_remise_except).
"'" :
"null").
",";
553 $sql .=
" ".price2num($this->qty,
'MS').
",";
554 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
555 $sql .=
" ".price2num($this->tva_tx).
",";
556 $sql .=
" ".price2num($this->localtax1_tx).
",";
557 $sql .=
" ".price2num($this->localtax2_tx).
",";
558 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
559 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
560 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
561 $sql .=
" ".price2num($this->remise_percent).
",";
562 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
563 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
564 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
565 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
566 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
567 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
568 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null").
",";
569 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
570 $sql .=
' '.((int) $this->special_code).
',';
571 $sql .=
' '.((int) $this->rang).
',';
572 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
573 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
574 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
575 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
576 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
577 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
578 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
579 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
580 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
583 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
584 $resql = $this->db->query($sql);
586 $this->
rowid = $this->db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
589 $this->
id = $this->rowid;
596 if (!$error && !$notrigger) {
598 $result = $this->
call_trigger(
'LINEPROPAL_INSERT', $user);
600 $this->db->rollback();
609 $this->error = $this->db->error().
" sql=".$sql;
610 $this->db->rollback();
678 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
680 if (empty($this->
id) && !empty($this->
rowid)) {
681 $this->
id = $this->rowid;
685 if (empty($this->tva_tx)) {
688 if (empty($this->localtax1_tx)) {
689 $this->localtax1_tx = 0;
691 if (empty($this->localtax2_tx)) {
692 $this->localtax2_tx = 0;
694 if (empty($this->total_localtax1)) {
695 $this->total_localtax1 = 0;
697 if (empty($this->total_localtax2)) {
698 $this->total_localtax2 = 0;
700 if (empty($this->localtax1_type)) {
701 $this->localtax1_type = 0;
703 if (empty($this->localtax2_type)) {
704 $this->localtax2_type = 0;
706 if (empty($this->marque_tx)) {
707 $this->marque_tx = 0;
709 if (empty($this->marge_tx)) {
712 if (empty($this->
price)) {
715 if (empty($this->remise_percent)) {
716 $this->remise_percent = 0;
718 if (empty($this->info_bits)) {
719 $this->info_bits = 0;
721 if (empty($this->special_code)) {
722 $this->special_code = 0;
724 if (empty($this->fk_parent_line)) {
725 $this->fk_parent_line = 0;
727 if (empty($this->fk_fournprice)) {
728 $this->fk_fournprice = 0;
730 if (empty($this->subprice)) {
733 if (empty($this->pa_ht)) {
738 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
739 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
742 $this->pa_ht = $result;
749 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
750 $sql .=
" description='".$this->db->escape($this->desc).
"'";
751 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
752 $sql .=
", product_type=".$this->product_type;
753 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
754 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
755 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
756 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
757 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
758 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
759 $sql .=
", qty='".price2num($this->qty).
"'";
760 $sql .=
", subprice=".price2num($this->subprice);
761 $sql .=
", remise_percent=".price2num($this->remise_percent);
763 $sql .=
", remise=".(float)
price2num($this->remise);
764 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
765 if (empty($this->skip_update_total)) {
766 $sql .=
", total_ht=".price2num($this->total_ht);
767 $sql .=
", total_tva=".price2num($this->total_tva);
768 $sql .=
", total_ttc=".price2num($this->total_ttc);
769 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
770 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
772 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
773 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
774 $sql .=
", special_code=".((int) $this->special_code);
775 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? (int) $this->fk_parent_line :
"null");
776 if (!empty($this->rang)) {
777 $sql .=
", rang=".((int) $this->rang);
779 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
780 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
781 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
784 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
785 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
786 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
787 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
789 $sql .=
" WHERE rowid = ".((int) $this->
id);
791 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
792 $resql = $this->db->query($sql);
801 if (!$error && !$notrigger) {
803 $result = $this->
call_trigger(
'LINEPROPAL_MODIFY', $user);
805 $this->db->rollback();
814 $this->error = $this->db->error();
815 $this->db->rollback();