339 $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';
340 $sql .=
', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
341 $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';
342 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
343 $sql .=
', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
344 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
345 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
346 $sql .=
' WHERE f.rowid = '.((int) $rowid);
347 $sql .=
' ORDER BY f.rang, f.rowid';
349 $query = $this->db->query($sql);
352 $this->errors[] = $this->db->error();
356 if (!$this->db->num_rows($query)) {
360 $obj = $this->db->fetch_object($query);
362 $this->
id = $obj->rowid;
363 $this->rowid = $obj->rowid;
364 $this->fk_facture_fourn = $obj->fk_facture_fourn;
366 $this->desc = $obj->line_desc;
367 $this->date_start = $obj->date_start;
368 $this->date_end = $obj->date_end;
369 $this->product_ref = $obj->product_ref;
370 $this->ref_supplier = $obj->ref_supplier;
371 $this->product_desc = $obj->product_desc;
373 $this->subprice = $obj->pu_ht;
374 $this->pu_ht = $obj->pu_ht;
375 $this->subprice_ttc = $obj->pu_ttc;
376 $this->pu_ttc = $obj->pu_ttc;
377 $this->tva_tx = $obj->tva_tx;
378 $this->localtax1_tx = $obj->localtax1_tx;
379 $this->localtax2_tx = $obj->localtax2_tx;
380 $this->localtax1_type = $obj->localtax1_type;
381 $this->localtax2_type = $obj->localtax2_type;
383 $this->qty = $obj->qty;
384 $this->remise_percent = $obj->remise_percent;
385 $this->fk_remise_except = $obj->fk_remise_except;
387 $this->total_ht = $obj->total_ht;
388 $this->total_tva = $obj->total_tva;
389 $this->total_localtax1 = $obj->total_localtax1;
390 $this->total_localtax2 = $obj->total_localtax2;
391 $this->total_ttc = $obj->total_ttc;
392 $this->fk_product = $obj->fk_product;
393 $this->product_type = $obj->product_type;
394 $this->product_label = $obj->product_label;
395 $this->label = $obj->product_label;
396 $this->info_bits = $obj->info_bits;
397 $this->fk_parent_line = $obj->fk_parent_line;
398 $this->special_code = $obj->special_code;
399 $this->rang = $obj->rang;
400 $this->fk_unit = $obj->fk_unit;
402 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
404 $this->multicurrency_subprice = $obj->multicurrency_subprice;
405 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
406 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
407 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
482 if (empty($this->qty)) {
486 if ($this->product_type < 0) {
491 if (empty($this->remise_percent)) {
492 $this->remise_percent = 0;
494 if (empty($this->tva_tx)) {
497 if (empty($this->localtax1_tx)) {
498 $this->localtax1_tx = 0;
500 if (empty($this->localtax2_tx)) {
501 $this->localtax2_tx = 0;
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;
520 $fk_product = (int) $this->fk_product;
521 $fk_unit = (int) $this->fk_unit;
525 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
526 $sql .=
" description = '".$this->db->escape(empty($this->
description) ? $this->desc : $this->description).
"'";
527 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->ref).
"'";
528 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
529 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
530 $sql .=
", pu_ht = ".price2num($this->subprice);
531 $sql .=
", pu_ttc = ".price2num($this->subprice_ttc);
532 $sql .=
", qty = ".price2num($this->qty);
533 $sql .=
", remise_percent = ".price2num($this->remise_percent);
534 if ($this->fk_remise_except > 0) {
535 $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
537 $sql .=
", fk_remise_except=null";
539 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
540 $sql .=
", tva_tx = ".price2num($this->tva_tx);
541 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
542 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
543 $sql .=
", localtax1_type = '".$this->db->escape((
string) $this->localtax1_type).
"'";
544 $sql .=
", localtax2_type = '".$this->db->escape((
string) $this->localtax2_type).
"'";
545 $sql .=
", total_ht = ".price2num($this->total_ht);
546 $sql .=
", tva= ".price2num($this->total_tva);
547 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
548 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
549 $sql .=
", total_ttc = ".price2num($this->total_ttc);
550 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
551 $sql .=
", product_type = ".((int) $this->product_type);
552 $sql .=
", info_bits = ".((int) $this->info_bits);
553 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
555 if (!empty($this->rang)) {
556 $sql .=
", rang=".((int) $this->rang);
560 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
561 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
562 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
563 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
565 $sql .=
" WHERE rowid = ".((int) $this->
id);
567 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
568 $resql = $this->db->query($sql);
571 $this->db->rollback();
572 $this->error = $this->db->lasterror();
576 $this->rowid = $this->id;
586 if (!$error && !$notrigger) {
588 if ($this->call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
589 $this->db->rollback();
596 $this->db->rollback();
611 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
613 global $user, $langs;
617 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
620 $this->desc = trim($this->desc);
621 if (empty($this->tva_tx)) {
624 if (empty($this->localtax1_tx)) {
625 $this->localtax1_tx = 0;
627 if (empty($this->localtax2_tx)) {
628 $this->localtax2_tx = 0;
630 if (empty($this->localtax1_type)) {
631 $this->localtax1_type = 0.0;
633 if (empty($this->localtax2_type)) {
634 $this->localtax2_type = 0.0;
636 if (empty($this->total_tva)) {
637 $this->total_tva = 0;
639 if (empty($this->total_localtax1)) {
640 $this->total_localtax1 = 0;
642 if (empty($this->total_localtax2)) {
643 $this->total_localtax2 = 0;
645 if (empty($this->rang)) {
648 if (empty($this->remise_percent)) {
649 $this->remise_percent = 0;
651 if (empty($this->info_bits)) {
652 $this->info_bits = 0;
654 if (empty($this->subprice)) {
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 (".$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) ? $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).
",";
731 $sql .=
" ".(!empty($this->qty) ?
price2num($this->total_ttc / $this->qty) :
price2num($this->total_ttc)).
",";
732 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
733 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
734 $sql .=
' '.(!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0).
',';
735 $sql .=
' '.((int) $this->rang).
',';
736 $sql .=
' '.((int) $this->special_code).
',';
737 $sql .=
" ".((int) $this->info_bits).
",";
738 $sql .=
" ".price2num($this->total_ht).
",";
739 $sql .=
" ".price2num($this->total_tva).
",";
740 $sql .=
" ".price2num($this->total_ttc).
",";
741 $sql .=
" ".price2num($this->total_localtax1).
",";
742 $sql .=
" ".price2num($this->total_localtax2);
743 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
744 $sql .=
", ".(int) $this->fk_multicurrency;
745 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
746 $sql .=
", ".price2num($this->multicurrency_subprice);
747 $sql .=
", ".price2num($this->multicurrency_total_ht);
748 $sql .=
", ".price2num($this->multicurrency_total_tva);
749 $sql .=
", ".price2num($this->multicurrency_total_ttc);
752 $resql = $this->db->query($sql);
754 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
755 $this->rowid = $this->id;
766 if ($this->fk_remise_except) {
768 $result = $discount->fetch($this->fk_remise_except);
773 if ($discount->fk_facture_line > 0) {
774 if (empty($noerrorifdiscountalreadylinked)) {
775 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
776 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
777 $this->db->rollback();
781 $result = $discount->link_to_invoice($this->
id, 0);
783 $this->error = $discount->error;
784 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
785 $this->db->rollback();
790 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
791 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
792 $this->db->rollback();
796 $this->error = $discount->error;
797 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
798 $this->db->rollback();
803 if (!$error && !$notrigger) {
805 $result = $this->call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
807 $this->db->rollback();
818 foreach ($this->errors as $errmsg) {
819 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
820 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
823 $this->db->rollback();
826 $this->error = $this->db->error();
827 $this->db->rollback();