202 $sql =
'SELECT fd.rowid, fd.fk_facture, fd.fk_parent_line, fd.fk_product, fd.product_type, fd.label as custom_label, fd.description, fd.price, fd.qty, fd.vat_src_code, fd.tva_tx,';
203 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.ref_ext,';
204 $sql .=
' fd.date_start as date_start, fd.date_end as date_end, fd.fk_product_fournisseur_price as fk_fournprice, fd.buy_price_ht as pa_ht,';
205 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
206 $sql .=
' fd.fk_code_ventilation,';
207 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
208 $sql .=
' fd.situation_percent, fd.fk_prev_id,';
209 $sql .=
' fd.multicurrency_subprice,';
210 $sql .=
' fd.multicurrency_total_ht,';
211 $sql .=
' fd.multicurrency_total_tva,';
212 $sql .=
' fd.multicurrency_total_ttc,';
213 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc';
214 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as fd';
215 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON fd.fk_product = p.rowid';
216 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
218 $result = $this->db->query($sql);
220 $objp = $this->db->fetch_object($result);
223 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.$sql;
227 $this->rowid = $objp->rowid;
228 $this->
id = $objp->rowid;
229 $this->fk_facture = $objp->fk_facture;
230 $this->fk_parent_line = $objp->fk_parent_line;
231 $this->label = $objp->custom_label;
232 $this->desc = $objp->description;
233 $this->qty = $objp->qty;
234 $this->subprice = $objp->subprice;
235 $this->ref_ext = $objp->ref_ext;
236 $this->vat_src_code = $objp->vat_src_code;
237 $this->tva_tx = $objp->tva_tx;
238 $this->localtax1_tx = $objp->localtax1_tx;
239 $this->localtax2_tx = $objp->localtax2_tx;
240 $this->remise_percent = $objp->remise_percent;
241 $this->fk_remise_except = $objp->fk_remise_except;
242 $this->fk_product = $objp->fk_product;
243 $this->product_type = $objp->product_type;
244 $this->date_start = $this->db->jdate($objp->date_start);
245 $this->date_end = $this->db->jdate($objp->date_end);
246 $this->info_bits = $objp->info_bits;
247 $this->tva_npr = (($objp->info_bits & 1) == 1) ? 1 : 0;
248 $this->special_code = $objp->special_code;
249 $this->total_ht = $objp->total_ht;
250 $this->total_tva = $objp->total_tva;
251 $this->total_localtax1 = $objp->total_localtax1;
252 $this->total_localtax2 = $objp->total_localtax2;
253 $this->total_ttc = $objp->total_ttc;
254 $this->fk_code_ventilation = $objp->fk_code_ventilation;
255 $this->rang = $objp->rang;
256 $this->fk_fournprice = $objp->fk_fournprice;
257 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
258 $this->pa_ht = $marginInfos[0];
259 $this->marge_tx = $marginInfos[1];
260 $this->marque_tx = $marginInfos[2];
262 $this->
ref = $objp->product_ref;
264 $this->product_ref = $objp->product_ref;
265 $this->product_label = $objp->product_label;
266 $this->product_desc = $objp->product_desc;
268 $this->fk_unit = $objp->fk_unit;
269 $this->fk_user_modif = $objp->fk_user_modif;
270 $this->fk_user_author = $objp->fk_user_author;
272 $this->situation_percent = $objp->situation_percent;
273 $this->fk_prev_id = $objp->fk_prev_id;
275 $this->multicurrency_subprice = $objp->multicurrency_subprice;
276 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
277 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
278 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
282 $this->db->free($result);
286 $this->error = $this->db->lasterror();
298 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
300 global $langs, $user;
304 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
306 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
309 $this->desc = trim($this->desc);
310 if (empty($this->tva_tx)) {
313 if (empty($this->localtax1_tx)) {
314 $this->localtax1_tx = 0;
316 if (empty($this->localtax2_tx)) {
317 $this->localtax2_tx = 0;
319 if (empty($this->localtax1_type)) {
320 $this->localtax1_type = 0;
322 if (empty($this->localtax2_type)) {
323 $this->localtax2_type = 0;
325 if (empty($this->total_localtax1)) {
326 $this->total_localtax1 = 0;
328 if (empty($this->total_localtax2)) {
329 $this->total_localtax2 = 0;
331 if (empty($this->rang)) {
334 if (empty($this->remise_percent)) {
335 $this->remise_percent = 0;
337 if (empty($this->info_bits)) {
338 $this->info_bits = 0;
340 if (empty($this->subprice)) {
343 if (empty($this->ref_ext)) {
346 if (empty($this->special_code)) {
347 $this->special_code = 0;
349 if (empty($this->fk_parent_line)) {
350 $this->fk_parent_line = 0;
352 if (empty($this->fk_prev_id)) {
353 $this->fk_prev_id = 0;
355 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
356 $this->situation_percent = 100;
359 if (empty($this->pa_ht)) {
362 if (empty($this->multicurrency_subprice)) {
363 $this->multicurrency_subprice = 0;
365 if (empty($this->multicurrency_total_ht)) {
366 $this->multicurrency_total_ht = 0;
368 if (empty($this->multicurrency_total_tva)) {
369 $this->multicurrency_total_tva = 0;
371 if (empty($this->multicurrency_total_ttc)) {
372 $this->multicurrency_total_ttc = 0;
376 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
377 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
381 $this->pa_ht = $result;
386 if ($this->product_type < 0) {
387 $this->error =
'ErrorProductTypeMustBe0orMore';
390 if (!empty($this->fk_product) && $this->fk_product > 0) {
394 $this->error =
'ErrorProductIdDoesNotExists';
395 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
403 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
404 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
405 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
406 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
407 $sql .=
' date_start, date_end, fk_code_ventilation,';
408 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
409 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
410 $sql .=
' situation_percent, fk_prev_id,';
411 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
412 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,';
413 $sql .=
' batch, fk_warehouse';
415 $sql .=
" VALUES (".$this->fk_facture.
",";
416 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
417 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
418 $sql .=
" '".$this->db->escape($this->desc).
"',";
419 $sql .=
" ".price2num($this->qty).
",";
420 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
421 $sql .=
" ".price2num($this->tva_tx).
",";
422 $sql .=
" ".price2num($this->localtax1_tx).
",";
423 $sql .=
" ".price2num($this->localtax2_tx).
",";
424 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
425 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
426 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
427 $sql .=
" ".((int) $this->product_type).
",";
428 $sql .=
" ".price2num($this->remise_percent).
",";
429 $sql .=
" ".price2num($this->subprice).
",";
430 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
431 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
432 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
433 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
434 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
435 $sql .=
' '.((int) $this->rang).
',';
436 $sql .=
' '.((int) $this->special_code).
',';
437 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
438 $sql .=
' '.price2num($this->pa_ht).
',';
439 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
440 $sql .=
" ".price2num($this->total_ht).
",";
441 $sql .=
" ".price2num($this->total_tva).
",";
442 $sql .=
" ".price2num($this->total_ttc).
",";
443 $sql .=
" ".price2num($this->total_localtax1).
",";
444 $sql .=
" ".price2num($this->total_localtax2);
445 $sql .=
", ".((float) $this->situation_percent);
446 $sql .=
", ".(!empty($this->fk_prev_id) ? $this->fk_prev_id :
"null");
447 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
448 $sql .=
", ".((int) $user->id);
449 $sql .=
", ".((int) $user->id);
450 $sql .=
", ".(int) $this->fk_multicurrency;
451 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
452 $sql .=
", ".price2num($this->multicurrency_subprice);
453 $sql .=
", ".price2num($this->multicurrency_total_ht);
454 $sql .=
", ".price2num($this->multicurrency_total_tva);
455 $sql .=
", ".price2num($this->multicurrency_total_ttc);
456 $sql .=
", '".$this->db->escape($this->batch).
"'";
457 $sql .=
", ".((int) $this->fk_warehouse);
460 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
461 $resql = $this->db->query($sql);
463 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
464 $this->rowid = $this->id;
475 if ($this->fk_remise_except) {
477 $result = $discount->fetch($this->fk_remise_except);
482 if ($discount->fk_facture_line > 0) {
483 if (empty($noerrorifdiscountalreadylinked)) {
484 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
485 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
486 $this->db->rollback();
490 $result = $discount->link_to_invoice($this->rowid, 0);
492 $this->error = $discount->error;
493 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
494 $this->db->rollback();
499 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
500 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
501 $this->db->rollback();
505 $this->error = $discount->error;
506 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
507 $this->db->rollback();
514 $result = $this->
call_trigger(
'LINEBILL_INSERT', $user);
516 $this->db->rollback();
525 $this->error = $this->db->lasterror();
526 $this->db->rollback();
538 public function update($user =
null, $notrigger = 0)
544 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
547 $this->desc = trim($this->desc);
548 if (empty($this->ref_ext)) {
551 if (empty($this->tva_tx)) {
554 if (empty($this->localtax1_tx)) {
555 $this->localtax1_tx = 0;
557 if (empty($this->localtax2_tx)) {
558 $this->localtax2_tx = 0;
560 if (empty($this->localtax1_type)) {
561 $this->localtax1_type = 0;
563 if (empty($this->localtax2_type)) {
564 $this->localtax2_type = 0;
566 if (empty($this->total_localtax1)) {
567 $this->total_localtax1 = 0;
569 if (empty($this->total_localtax2)) {
570 $this->total_localtax2 = 0;
572 if (empty($this->remise_percent)) {
573 $this->remise_percent = 0;
575 if (empty($this->info_bits)) {
576 $this->info_bits = 0;
578 if (empty($this->special_code)) {
579 $this->special_code = 0;
581 if (empty($this->product_type)) {
582 $this->product_type = 0;
584 if (empty($this->fk_parent_line)) {
585 $this->fk_parent_line = 0;
587 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
588 $this->situation_percent = 100;
590 if (empty($this->pa_ht)) {
594 if (empty($this->multicurrency_subprice)) {
595 $this->multicurrency_subprice = 0;
597 if (empty($this->multicurrency_total_ht)) {
598 $this->multicurrency_total_ht = 0;
600 if (empty($this->multicurrency_total_tva)) {
601 $this->multicurrency_total_tva = 0;
603 if (empty($this->multicurrency_total_ttc)) {
604 $this->multicurrency_total_ttc = 0;
608 if ($this->product_type < 0) {
613 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
615 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
619 $this->pa_ht = $result;
626 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
627 $sql .=
" description='".$this->db->escape($this->desc).
"'";
628 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
629 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
630 $sql .=
", subprice=".price2num($this->subprice);
631 $sql .=
", remise_percent=".price2num($this->remise_percent);
632 if ($this->fk_remise_except) {
633 $sql .=
", fk_remise_except=".$this->fk_remise_except;
635 $sql .=
", fk_remise_except=null";
637 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
638 $sql .=
", tva_tx=".price2num($this->tva_tx);
639 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
640 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
641 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
642 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
643 $sql .=
", qty=".price2num($this->qty);
644 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
645 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
646 $sql .=
", product_type=".$this->product_type;
647 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
648 $sql .=
", special_code=" . (int) $this->special_code;
649 if (empty($this->skip_update_total)) {
650 $sql .=
", total_ht=".price2num($this->total_ht);
651 $sql .=
", total_tva=".price2num($this->total_tva);
652 $sql .=
", total_ttc=".price2num($this->total_ttc);
653 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
654 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
656 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
657 $sql .=
", buy_price_ht=".(($this->pa_ht || (string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
658 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
659 if (!empty($this->rang)) {
660 $sql .=
", rang=".((int) $this->rang);
662 $sql .=
", situation_percent = ".((float) $this->situation_percent);
663 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
664 $sql .=
", fk_user_modif = ".((int) $user->id);
667 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
668 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
669 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
670 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
672 $sql .=
", batch = '".$this->db->escape($this->batch).
"'";
673 $sql .=
", fk_warehouse = ".((int) $this->fk_warehouse);
675 $sql .=
" WHERE rowid = ".((int) $this->rowid);
677 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
678 $resql = $this->db->query($sql);
681 $this->
id = $this->rowid;
688 if (!$error && !$notrigger) {
690 $result = $this->
call_trigger(
'LINEBILL_MODIFY', $user);
692 $this->db->rollback();
700 $this->error = $this->db->error();
701 $this->db->rollback();
829 global $invoicecache;
831 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
835 if (!isset($invoicecache[$invoiceid])) {
836 $invoicecache[$invoiceid] =
new Facture($this->db);
837 $invoicecache[$invoiceid]->fetch($invoiceid);
843 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet";
844 $sql .=
" WHERE rowid = ".((int) $this->fk_prev_id);
846 $resql = $this->db->query($sql);
848 if ($resql && $this->db->num_rows($resql) > 0) {
851 $obj = $this->db->fetch_object($resql);
853 $returnPercent = (float) $obj->situation_percent;
856 if ($include_credit_note) {
857 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
858 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
859 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
860 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
861 $sql .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
863 $res = $this->db->query($sql);
865 while ($obj = $this->db->fetch_object($res)) {
866 $returnPercent += (float) $obj->situation_percent;
873 return $returnPercent;
875 $this->error = $this->db->error();
876 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
877 $this->db->rollback();
896 global $invoicecache;
898 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
902 if (!isset($invoicecache[$invoiceid])) {
903 $invoicecache[$invoiceid] =
new Facture($this->db);
904 $invoicecache[$invoiceid]->fetch($invoiceid);
911 $lastprevid = $this->fk_prev_id;
912 $cumulated_percent = 0.0;
914 while (!$all_found) {
915 $sql =
"SELECT situation_percent, fk_prev_id FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $lastprevid);
916 $resql = $this->db->query($sql);
918 if ($resql && $this->db->num_rows($resql) > 0) {
919 $obj = $this->db->fetch_object($resql);
920 $cumulated_percent += floatval($obj->situation_percent);
922 if ($include_credit_note) {
923 $sql_credit_note =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
924 $sql_credit_note .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
925 $sql_credit_note .=
" WHERE fd.fk_prev_id = ".((int) $lastprevid);
926 $sql_credit_note .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
927 $sql_credit_note .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
929 $res_credit_note = $this->db->query($sql_credit_note);
930 if ($res_credit_note) {
931 while ($cn = $this->db->fetch_object($res_credit_note)) {
932 $cumulated_percent += floatval($cn->situation_percent);
940 if ($obj->fk_prev_id) {
941 $lastprevid = $obj->fk_prev_id;
946 $this->error = $this->db->error();
947 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
948 $this->db->rollback();
952 return $cumulated_percent;