333 $sql =
'SELECT f.rowid, f.ref as ref_supplier, f.description as line_desc, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx';
334 $sql .=
', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
335 $sql .=
', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_facture_fourn, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit, f.extraparams';
336 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
337 $sql .=
', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
338 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
339 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
340 $sql .=
' WHERE f.rowid = '.((int) $rowid);
341 $sql .=
' ORDER BY f.rang, f.rowid';
343 $query = $this->db->query($sql);
346 $this->errors[] = $this->db->error();
350 if (!$this->db->num_rows($query)) {
354 $obj = $this->db->fetch_object($query);
356 $this->
id = $obj->rowid;
357 $this->rowid = $obj->rowid;
358 $this->fk_facture_fourn = $obj->fk_facture_fourn;
360 $this->desc = $obj->line_desc;
361 $this->date_start = $obj->date_start;
362 $this->date_end = $obj->date_end;
363 $this->product_ref = $obj->product_ref;
364 $this->ref_supplier = $obj->ref_supplier;
365 $this->product_desc = $obj->product_desc;
367 $this->subprice = $obj->pu_ht;
368 $this->pu_ht = $obj->pu_ht;
369 $this->subprice_ttc = $obj->pu_ttc;
370 $this->pu_ttc = $obj->pu_ttc;
371 $this->tva_tx = $obj->tva_tx;
372 $this->localtax1_tx = $obj->localtax1_tx;
373 $this->localtax2_tx = $obj->localtax2_tx;
374 $this->localtax1_type = $obj->localtax1_type;
375 $this->localtax2_type = $obj->localtax2_type;
377 $this->qty = $obj->qty;
378 $this->remise_percent = $obj->remise_percent;
379 $this->fk_remise_except = $obj->fk_remise_except;
381 $this->total_ht = $obj->total_ht;
382 $this->total_tva = $obj->total_tva;
383 $this->total_localtax1 = $obj->total_localtax1;
384 $this->total_localtax2 = $obj->total_localtax2;
385 $this->total_ttc = $obj->total_ttc;
386 $this->fk_product = $obj->fk_product;
387 $this->product_type = $obj->product_type;
388 $this->product_label = $obj->product_label;
389 $this->label = $obj->product_label;
390 $this->info_bits = $obj->info_bits;
391 $this->fk_parent_line = $obj->fk_parent_line;
392 $this->special_code = $obj->special_code;
393 $this->rang = $obj->rang;
394 $this->fk_unit = $obj->fk_unit;
396 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
398 $this->multicurrency_subprice = $obj->multicurrency_subprice;
399 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
400 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
401 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
476 if (empty($this->qty)) {
480 if ($this->product_type < 0) {
485 if (empty($this->subprice_ttc)) {
486 $this->subprice_ttc = 0;
488 if (empty($this->remise_percent)) {
489 $this->remise_percent = 0;
491 if (empty($this->tva_tx)) {
494 if (empty($this->localtax1_tx)) {
495 $this->localtax1_tx = 0;
497 if (empty($this->localtax2_tx)) {
498 $this->localtax2_tx = 0;
501 if (empty($this->pa_ht)) {
504 if (empty($this->multicurrency_subprice)) {
505 $this->multicurrency_subprice = 0;
507 if (empty($this->multicurrency_total_ht)) {
508 $this->multicurrency_total_ht = 0;
510 if (empty($this->multicurrency_total_tva)) {
511 $this->multicurrency_total_tva = 0;
513 if (empty($this->multicurrency_total_ttc)) {
514 $this->multicurrency_total_ttc = 0;
517 $fk_product = (int) $this->fk_product;
518 $fk_unit = (int) $this->fk_unit;
522 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
523 $sql .=
" description = '".$this->db->escape(empty($this->
description) ? $this->desc : $this->description).
"'";
524 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->ref).
"'";
525 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
526 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
527 $sql .=
", pu_ht = ".price2num($this->subprice);
528 $sql .=
", pu_ttc = ".price2num($this->subprice_ttc);
529 $sql .=
", qty = ".price2num($this->qty);
530 $sql .=
", remise_percent = ".price2num($this->remise_percent);
531 if ($this->fk_remise_except > 0) {
532 $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
534 $sql .=
", fk_remise_except=null";
536 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
537 $sql .=
", tva_tx = ".price2num($this->tva_tx);
538 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
539 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
540 $sql .=
", localtax1_type = '".$this->db->escape((
string) $this->localtax1_type).
"'";
541 $sql .=
", localtax2_type = '".$this->db->escape((
string) $this->localtax2_type).
"'";
542 $sql .=
", total_ht = ".price2num($this->total_ht);
543 $sql .=
", tva= ".price2num($this->total_tva);
544 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
545 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
546 $sql .=
", total_ttc = ".price2num($this->total_ttc);
547 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
548 $sql .=
", product_type = ".((int) $this->product_type);
549 $sql .=
", info_bits = ".((int) $this->info_bits);
550 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
552 if (!empty($this->rang)) {
553 $sql .=
", rang=".((int) $this->rang);
557 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
558 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
559 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
560 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
562 $sql .=
" WHERE rowid = ".((int) $this->
id);
564 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
565 $resql = $this->db->query($sql);
568 $this->db->rollback();
569 $this->error = $this->db->lasterror();
573 $this->rowid = $this->id;
583 if (!$error && !$notrigger) {
585 if ($this->call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
586 $this->db->rollback();
593 $this->db->rollback();
608 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
610 global $user, $langs;
614 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
617 $this->desc = trim($this->desc);
618 if (empty($this->tva_tx)) {
621 if (empty($this->localtax1_tx)) {
622 $this->localtax1_tx = 0;
624 if (empty($this->localtax2_tx)) {
625 $this->localtax2_tx = 0;
627 if (empty($this->localtax1_type)) {
628 $this->localtax1_type = 0.0;
630 if (empty($this->localtax2_type)) {
631 $this->localtax2_type = 0.0;
633 if (empty($this->total_tva)) {
634 $this->total_tva = 0;
636 if (empty($this->total_localtax1)) {
637 $this->total_localtax1 = 0;
639 if (empty($this->total_localtax2)) {
640 $this->total_localtax2 = 0;
642 if (empty($this->rang)) {
645 if (empty($this->remise_percent)) {
646 $this->remise_percent = 0;
648 if (empty($this->info_bits)) {
649 $this->info_bits = 0;
651 if (empty($this->subprice)) {
654 if (empty($this->subprice_ttc)) {
655 $this->subprice_ttc = 0;
657 if (empty($this->special_code)) {
658 $this->special_code = 0;
660 if (empty($this->fk_parent_line)) {
661 $this->fk_parent_line = 0;
663 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
664 $this->situation_percent = 100;
667 if (empty($this->pa_ht)) {
670 if (empty($this->multicurrency_subprice)) {
671 $this->multicurrency_subprice = 0;
673 if (empty($this->multicurrency_total_ht)) {
674 $this->multicurrency_total_ht = 0;
676 if (empty($this->multicurrency_total_tva)) {
677 $this->multicurrency_total_tva = 0;
679 if (empty($this->multicurrency_total_ttc)) {
680 $this->multicurrency_total_ttc = 0;
685 if ($this->product_type < 0) {
686 $this->error =
'ErrorProductTypeMustBe0orMore';
689 if (!empty($this->fk_product) && $this->fk_product > 0) {
693 $this->error =
'ErrorProductIdDoesNotExists';
701 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
702 $sql .=
' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
703 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
704 $sql .=
' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
705 $sql .=
' date_start, date_end, fk_code_ventilation, rang, special_code,';
706 $sql .=
' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
707 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
709 $sql .=
" VALUES (".((int) $this->fk_facture_fourn).
",";
710 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
712 = !empty($this->product_label)
713 ? $this->product_label :
714 (!empty($this->label) ? $this->label :
null);
715 $sql .=
" ".(!empty($product_label) ?
"'".$this->db->escape($product_label).
"'" :
"null").
",";
716 $sql .=
" '".$this->db->escape($this->desc ? $this->desc : $this->description).
"',";
717 $sql .=
" '".$this->db->escape($this->ref_supplier).
"',";
718 $sql .=
" ".price2num($this->qty).
",";
720 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
721 $sql .=
" ".price2num($this->tva_tx).
",";
722 $sql .=
" ".price2num($this->localtax1_tx).
",";
723 $sql .=
" ".price2num($this->localtax2_tx).
",";
724 $sql .=
" '".$this->db->escape((
string) $this->localtax1_type).
"',";
725 $sql .=
" '".$this->db->escape((
string) $this->localtax2_type).
"',";
726 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? ((
int) $this->fk_product) :
"null").
',';
727 $sql .=
" ".((int) $this->product_type).
",";
728 $sql .=
" ".price2num($this->remise_percent).
",";
729 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
730 $sql .=
" ".price2num($this->subprice).
",";
732 $sql .=
" ".price2num($this->subprice_ttc).
",";
733 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
734 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
735 $sql .=
' '.(!empty($this->fk_code_ventilation) ? ((int) $this->fk_code_ventilation) : 0).
',';
736 $sql .=
' '.((int) $this->rang).
',';
737 $sql .=
' '.((int) $this->special_code).
',';
738 $sql .=
" ".((int) $this->info_bits).
",";
739 $sql .=
" ".price2num($this->total_ht).
",";
740 $sql .=
" ".price2num($this->total_tva).
",";
741 $sql .=
" ".price2num($this->total_ttc).
",";
742 $sql .=
" ".price2num($this->total_localtax1).
",";
743 $sql .=
" ".price2num($this->total_localtax2);
744 $sql .=
", ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
745 $sql .=
", ".(int) $this->fk_multicurrency;
746 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
747 $sql .=
", ".price2num($this->multicurrency_subprice);
748 $sql .=
", ".price2num($this->multicurrency_total_ht);
749 $sql .=
", ".price2num($this->multicurrency_total_tva);
750 $sql .=
", ".price2num($this->multicurrency_total_ttc);
753 $resql = $this->db->query($sql);
755 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
756 $this->rowid = $this->id;
767 if ($this->fk_remise_except) {
769 $result = $discount->fetch($this->fk_remise_except);
774 if ($discount->fk_facture_line > 0) {
775 if (empty($noerrorifdiscountalreadylinked)) {
776 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
777 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
778 $this->db->rollback();
782 $result = $discount->link_to_invoice($this->
id, 0);
784 $this->error = $discount->error;
785 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
786 $this->db->rollback();
791 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
792 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
793 $this->db->rollback();
797 $this->error = $discount->error;
798 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
799 $this->db->rollback();
804 if (!$error && !$notrigger) {
806 $result = $this->call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
808 $this->db->rollback();
819 foreach ($this->errors as $errmsg) {
820 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
821 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
824 $this->db->rollback();
827 $this->error = $this->db->error();
828 $this->db->rollback();