325 $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';
326 $sql .=
', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
327 $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';
328 $sql .=
', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
329 $sql .=
', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
330 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_fourn_det as f';
331 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON f.fk_product = p.rowid';
332 $sql .=
' WHERE f.rowid = '.((int) $rowid);
333 $sql .=
' ORDER BY f.rang, f.rowid';
335 $query = $this->db->query($sql);
338 $this->errors[] = $this->db->error();
342 if (!$this->db->num_rows($query)) {
346 $obj = $this->db->fetch_object($query);
348 $this->
id = $obj->rowid;
349 $this->rowid = $obj->rowid;
350 $this->fk_facture_fourn = $obj->fk_facture_fourn;
352 $this->desc = $obj->line_desc;
353 $this->date_start = $obj->date_start;
354 $this->date_end = $obj->date_end;
355 $this->product_ref = $obj->product_ref;
356 $this->ref_supplier = $obj->ref_supplier;
357 $this->product_desc = $obj->product_desc;
359 $this->subprice = $obj->pu_ht;
360 $this->pu_ht = $this->subprice;
361 $this->pu_ttc = $obj->pu_ttc;
362 $this->tva_tx = $obj->tva_tx;
363 $this->localtax1_tx = $obj->localtax1_tx;
364 $this->localtax2_tx = $obj->localtax2_tx;
365 $this->localtax1_type = $obj->localtax1_type;
366 $this->localtax2_type = $obj->localtax2_type;
368 $this->qty = $obj->qty;
369 $this->remise_percent = $obj->remise_percent;
370 $this->fk_remise_except = $obj->fk_remise_except;
372 $this->total_ht = $obj->total_ht;
373 $this->total_tva = $obj->total_tva;
374 $this->total_localtax1 = $obj->total_localtax1;
375 $this->total_localtax2 = $obj->total_localtax2;
376 $this->total_ttc = $obj->total_ttc;
377 $this->fk_product = $obj->fk_product;
378 $this->product_type = $obj->product_type;
379 $this->product_label = $obj->product_label;
380 $this->label = $obj->product_label;
381 $this->info_bits = $obj->info_bits;
382 $this->fk_parent_line = $obj->fk_parent_line;
383 $this->special_code = $obj->special_code;
384 $this->rang = $obj->rang;
385 $this->fk_unit = $obj->fk_unit;
387 $this->multicurrency_subprice = $obj->multicurrency_subprice;
388 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
389 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
390 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
465 if (empty($this->qty)) {
469 if ($this->product_type < 0) {
474 if (empty($this->remise_percent)) {
475 $this->remise_percent = 0;
477 if (empty($this->tva_tx)) {
480 if (empty($this->localtax1_tx)) {
481 $this->localtax1_tx = 0;
483 if (empty($this->localtax2_tx)) {
484 $this->localtax2_tx = 0;
487 if (empty($this->pa_ht)) {
490 if (empty($this->multicurrency_subprice)) {
491 $this->multicurrency_subprice = 0;
493 if (empty($this->multicurrency_total_ht)) {
494 $this->multicurrency_total_ht = 0;
496 if (empty($this->multicurrency_total_tva)) {
497 $this->multicurrency_total_tva = 0;
499 if (empty($this->multicurrency_total_ttc)) {
500 $this->multicurrency_total_ttc = 0;
503 $fk_product = (int) $this->fk_product;
504 $fk_unit = (int) $this->fk_unit;
508 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_fourn_det SET";
509 $sql .=
" description = '".$this->db->escape(empty($this->
description) ? $this->desc : $this->description).
"'";
510 $sql .=
", ref = '".$this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->ref).
"'";
511 $sql .=
", date_start = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
512 $sql .=
", date_end = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
513 $sql .=
", pu_ht = ".price2num($this->subprice);
514 $sql .=
", pu_ttc = ".price2num($this->pu_ttc);
515 $sql .=
", qty = ".price2num($this->qty);
516 $sql .=
", remise_percent = ".price2num($this->remise_percent);
517 if ($this->fk_remise_except > 0) {
518 $sql .=
", fk_remise_except=".((int) $this->fk_remise_except);
520 $sql .=
", fk_remise_except=null";
522 $sql .=
", vat_src_code = '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
523 $sql .=
", tva_tx = ".price2num($this->tva_tx);
524 $sql .=
", localtax1_tx = ".price2num($this->localtax1_tx);
525 $sql .=
", localtax2_tx = ".price2num($this->localtax2_tx);
526 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
527 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
528 $sql .=
", total_ht = ".price2num($this->total_ht);
529 $sql .=
", tva= ".price2num($this->total_tva);
530 $sql .=
", total_localtax1= ".price2num($this->total_localtax1);
531 $sql .=
", total_localtax2= ".price2num($this->total_localtax2);
532 $sql .=
", total_ttc = ".price2num($this->total_ttc);
533 $sql .=
", fk_product = ".($fk_product > 0 ? (int) $fk_product :
'null');
534 $sql .=
", product_type = ".((int) $this->product_type);
535 $sql .=
", info_bits = ".((int) $this->info_bits);
536 $sql .=
", fk_unit = ".($fk_unit > 0 ? (int) $fk_unit :
'null');
538 if (!empty($this->rang)) {
539 $sql .=
", rang=".((int) $this->rang);
543 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
544 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
545 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
546 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
548 $sql .=
" WHERE rowid = ".((int) $this->
id);
550 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
551 $resql = $this->db->query($sql);
554 $this->db->rollback();
555 $this->error = $this->db->lasterror();
559 $this->rowid = $this->id;
569 if (!$error && !$notrigger) {
570 global $langs, $user;
573 if ($this->
call_trigger(
'LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
574 $this->db->rollback();
581 $this->db->rollback();
596 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
598 global $user, $langs;
602 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
605 $this->desc = trim($this->desc);
606 if (empty($this->tva_tx)) {
609 if (empty($this->localtax1_tx)) {
610 $this->localtax1_tx = 0;
612 if (empty($this->localtax2_tx)) {
613 $this->localtax2_tx = 0;
615 if (empty($this->localtax1_type)) {
616 $this->localtax1_type = 0.0;
618 if (empty($this->localtax2_type)) {
619 $this->localtax2_type = 0.0;
621 if (empty($this->total_tva)) {
622 $this->total_tva = 0;
624 if (empty($this->total_localtax1)) {
625 $this->total_localtax1 = 0;
627 if (empty($this->total_localtax2)) {
628 $this->total_localtax2 = 0;
630 if (empty($this->rang)) {
633 if (empty($this->remise_percent)) {
634 $this->remise_percent = 0;
636 if (empty($this->info_bits)) {
637 $this->info_bits = 0;
639 if (empty($this->subprice)) {
642 if (empty($this->special_code)) {
643 $this->special_code = 0;
645 if (empty($this->fk_parent_line)) {
646 $this->fk_parent_line = 0;
648 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
649 $this->situation_percent = 100;
652 if (empty($this->pa_ht)) {
655 if (empty($this->multicurrency_subprice)) {
656 $this->multicurrency_subprice = 0;
658 if (empty($this->multicurrency_total_ht)) {
659 $this->multicurrency_total_ht = 0;
661 if (empty($this->multicurrency_total_tva)) {
662 $this->multicurrency_total_tva = 0;
664 if (empty($this->multicurrency_total_ttc)) {
665 $this->multicurrency_total_ttc = 0;
670 if ($this->product_type < 0) {
671 $this->error =
'ErrorProductTypeMustBe0orMore';
674 if (!empty($this->fk_product) && $this->fk_product > 0) {
678 $this->error =
'ErrorProductIdDoesNotExists';
686 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
687 $sql .=
' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
688 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
689 $sql .=
' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
690 $sql .=
' date_start, date_end, fk_code_ventilation, rang, special_code,';
691 $sql .=
' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
692 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
694 $sql .=
" VALUES (".$this->fk_facture_fourn.
",";
695 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
697 = !empty($this->product_label)
698 ? $this->product_label :
699 (!empty($this->label) ? $this->label :
null);
700 $sql .=
" ".(!empty($product_label) ?
"'".$this->db->escape($product_label).
"'" :
"null").
",";
701 $sql .=
" '".$this->db->escape($this->desc ? $this->desc : $this->description).
"',";
702 $sql .=
" '".$this->db->escape($this->ref_supplier).
"',";
703 $sql .=
" ".price2num($this->qty).
",";
705 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
706 $sql .=
" ".price2num($this->tva_tx).
",";
707 $sql .=
" ".price2num($this->localtax1_tx).
",";
708 $sql .=
" ".price2num($this->localtax2_tx).
",";
709 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
710 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
711 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
712 $sql .=
" ".((int) $this->product_type).
",";
713 $sql .=
" ".price2num($this->remise_percent).
",";
714 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
715 $sql .=
" ".price2num($this->subprice).
",";
716 $sql .=
" ".(!empty($this->qty) ?
price2num($this->total_ttc / $this->qty) :
price2num($this->total_ttc)).
",";
717 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
718 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
719 $sql .=
' '.(!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0).
',';
720 $sql .=
' '.((int) $this->rang).
',';
721 $sql .=
' '.((int) $this->special_code).
',';
722 $sql .=
" ".((int) $this->info_bits).
",";
723 $sql .=
" ".price2num($this->total_ht).
",";
724 $sql .=
" ".price2num($this->total_tva).
",";
725 $sql .=
" ".price2num($this->total_ttc).
",";
726 $sql .=
" ".price2num($this->total_localtax1).
",";
727 $sql .=
" ".price2num($this->total_localtax2);
728 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
729 $sql .=
", ".(int) $this->fk_multicurrency;
730 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
731 $sql .=
", ".price2num($this->multicurrency_subprice);
732 $sql .=
", ".price2num($this->multicurrency_total_ht);
733 $sql .=
", ".price2num($this->multicurrency_total_tva);
734 $sql .=
", ".price2num($this->multicurrency_total_ttc);
737 $resql = $this->db->query($sql);
739 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
740 $this->rowid = $this->id;
751 if ($this->fk_remise_except) {
753 $result = $discount->fetch($this->fk_remise_except);
758 if ($discount->fk_facture_line > 0) {
759 if (empty($noerrorifdiscountalreadylinked)) {
760 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
761 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
762 $this->db->rollback();
766 $result = $discount->link_to_invoice($this->
id, 0);
768 $this->error = $discount->error;
769 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
770 $this->db->rollback();
775 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
776 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
777 $this->db->rollback();
781 $this->error = $discount->error;
782 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
783 $this->db->rollback();
788 if (!$error && !$notrigger) {
790 $result = $this->
call_trigger(
'LINEBILL_SUPPLIER_CREATE', $user);
792 $this->db->rollback();
801 $this->error = $this->db->error();
802 $this->db->rollback();