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->remise_percent)) {
486 $this->remise_percent = 0;
488 if (empty($this->tva_tx)) {
491 if (empty($this->localtax1_tx)) {
492 $this->localtax1_tx = 0;
494 if (empty($this->localtax2_tx)) {
495 $this->localtax2_tx = 0;
498 if (empty($this->pa_ht)) {
501 if (empty($this->multicurrency_subprice)) {
502 $this->multicurrency_subprice = 0;
504 if (empty($this->multicurrency_total_ht)) {
505 $this->multicurrency_total_ht = 0;
507 if (empty($this->multicurrency_total_tva)) {
508 $this->multicurrency_total_tva = 0;
510 if (empty($this->multicurrency_total_ttc)) {
511 $this->multicurrency_total_ttc = 0;
514 $fk_product = (int) $this->fk_product;
515 $fk_unit = (int) $this->fk_unit;
519 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
520 $sql .=
" description = '".$this->db->escape(empty($this->
description) ? $this->desc : $this->description).
"'";
521 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->ref).
"'";
522 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
523 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
524 $sql .=
", pu_ht = ".price2num($this->subprice);
525 $sql .=
", pu_ttc = ".price2num($this->subprice_ttc);
526 $sql .=
", qty = ".price2num($this->qty);
527 $sql .=
", remise_percent = ".price2num($this->remise_percent);
528 if ($this->fk_remise_except > 0) {
529 $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
531 $sql .=
", fk_remise_except=null";
533 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
534 $sql .=
", tva_tx = ".price2num($this->tva_tx);
535 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
536 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
537 $sql .=
", localtax1_type = '".$this->db->escape((
string) $this->localtax1_type).
"'";
538 $sql .=
", localtax2_type = '".$this->db->escape((
string) $this->localtax2_type).
"'";
539 $sql .=
", total_ht = ".price2num($this->total_ht);
540 $sql .=
", tva= ".price2num($this->total_tva);
541 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
542 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
543 $sql .=
", total_ttc = ".price2num($this->total_ttc);
544 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
545 $sql .=
", product_type = ".((int) $this->product_type);
546 $sql .=
", info_bits = ".((int) $this->info_bits);
547 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
549 if (!empty($this->rang)) {
550 $sql .=
", rang=".((int) $this->rang);
554 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
555 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
556 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
557 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
559 $sql .=
" WHERE rowid = ".((int) $this->
id);
561 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
562 $resql = $this->db->query($sql);
565 $this->db->rollback();
566 $this->error = $this->db->lasterror();
570 $this->rowid = $this->id;
580 if (!$error && !$notrigger) {
582 if ($this->call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
583 $this->db->rollback();
590 $this->db->rollback();
605 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
607 global $user, $langs;
611 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
614 $this->desc = trim($this->desc);
615 if (empty($this->tva_tx)) {
618 if (empty($this->localtax1_tx)) {
619 $this->localtax1_tx = 0;
621 if (empty($this->localtax2_tx)) {
622 $this->localtax2_tx = 0;
624 if (empty($this->localtax1_type)) {
625 $this->localtax1_type = 0.0;
627 if (empty($this->localtax2_type)) {
628 $this->localtax2_type = 0.0;
630 if (empty($this->total_tva)) {
631 $this->total_tva = 0;
633 if (empty($this->total_localtax1)) {
634 $this->total_localtax1 = 0;
636 if (empty($this->total_localtax2)) {
637 $this->total_localtax2 = 0;
639 if (empty($this->rang)) {
642 if (empty($this->remise_percent)) {
643 $this->remise_percent = 0;
645 if (empty($this->info_bits)) {
646 $this->info_bits = 0;
648 if (empty($this->subprice)) {
651 if (empty($this->special_code)) {
652 $this->special_code = 0;
654 if (empty($this->fk_parent_line)) {
655 $this->fk_parent_line = 0;
657 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
658 $this->situation_percent = 100;
661 if (empty($this->pa_ht)) {
664 if (empty($this->multicurrency_subprice)) {
665 $this->multicurrency_subprice = 0;
667 if (empty($this->multicurrency_total_ht)) {
668 $this->multicurrency_total_ht = 0;
670 if (empty($this->multicurrency_total_tva)) {
671 $this->multicurrency_total_tva = 0;
673 if (empty($this->multicurrency_total_ttc)) {
674 $this->multicurrency_total_ttc = 0;
679 if ($this->product_type < 0) {
680 $this->error =
'ErrorProductTypeMustBe0orMore';
683 if (!empty($this->fk_product) && $this->fk_product > 0) {
687 $this->error =
'ErrorProductIdDoesNotExists';
695 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
696 $sql .=
' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
697 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
698 $sql .=
' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
699 $sql .=
' date_start, date_end, fk_code_ventilation, rang, special_code,';
700 $sql .=
' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
701 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
703 $sql .=
" VALUES (".$this->fk_facture_fourn.
",";
704 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape((
string) $this->fk_parent_line).
"'" :
"null").
",";
706 = !empty($this->product_label)
707 ? $this->product_label :
708 (!empty($this->label) ? $this->label :
null);
709 $sql .=
" ".(!empty($product_label) ?
"'".$this->db->escape($product_label).
"'" :
"null").
",";
710 $sql .=
" '".$this->db->escape($this->desc ? $this->desc : $this->description).
"',";
711 $sql .=
" '".$this->db->escape($this->ref_supplier).
"',";
712 $sql .=
" ".price2num($this->qty).
",";
714 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
715 $sql .=
" ".price2num($this->tva_tx).
",";
716 $sql .=
" ".price2num($this->localtax1_tx).
",";
717 $sql .=
" ".price2num($this->localtax2_tx).
",";
718 $sql .=
" '".$this->db->escape((
string) $this->localtax1_type).
"',";
719 $sql .=
" '".$this->db->escape((
string) $this->localtax2_type).
"',";
720 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
721 $sql .=
" ".((int) $this->product_type).
",";
722 $sql .=
" ".price2num($this->remise_percent).
",";
723 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
724 $sql .=
" ".price2num($this->subprice).
",";
725 $sql .=
" ".(!empty($this->qty) ?
price2num($this->total_ttc / $this->qty) :
price2num($this->total_ttc)).
",";
726 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
727 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
728 $sql .=
' '.(!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0).
',';
729 $sql .=
' '.((int) $this->rang).
',';
730 $sql .=
' '.((int) $this->special_code).
',';
731 $sql .=
" ".((int) $this->info_bits).
",";
732 $sql .=
" ".price2num($this->total_ht).
",";
733 $sql .=
" ".price2num($this->total_tva).
",";
734 $sql .=
" ".price2num($this->total_ttc).
",";
735 $sql .=
" ".price2num($this->total_localtax1).
",";
736 $sql .=
" ".price2num($this->total_localtax2);
737 $sql .=
", ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
738 $sql .=
", ".(int) $this->fk_multicurrency;
739 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
740 $sql .=
", ".price2num($this->multicurrency_subprice);
741 $sql .=
", ".price2num($this->multicurrency_total_ht);
742 $sql .=
", ".price2num($this->multicurrency_total_tva);
743 $sql .=
", ".price2num($this->multicurrency_total_ttc);
746 $resql = $this->db->query($sql);
748 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
749 $this->rowid = $this->id;
760 if ($this->fk_remise_except) {
762 $result = $discount->fetch($this->fk_remise_except);
767 if ($discount->fk_facture_line > 0) {
768 if (empty($noerrorifdiscountalreadylinked)) {
769 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
770 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
771 $this->db->rollback();
775 $result = $discount->link_to_invoice($this->
id, 0);
777 $this->error = $discount->error;
778 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
779 $this->db->rollback();
784 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
785 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
786 $this->db->rollback();
790 $this->error = $discount->error;
791 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
792 $this->db->rollback();
797 if (!$error && !$notrigger) {
799 $result = $this->call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
801 $this->db->rollback();
812 foreach ($this->errors as $errmsg) {
813 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
814 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
817 $this->db->rollback();
820 $this->error = $this->db->error();
821 $this->db->rollback();