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.batch, fd.fk_warehouse,';
208 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
209 $sql .=
' fd.situation_percent, fd.fk_prev_id,';
210 $sql .=
' fd.multicurrency_subprice,';
211 $sql .=
' fd.multicurrency_total_ht,';
212 $sql .=
' fd.multicurrency_total_tva,';
213 $sql .=
' fd.multicurrency_total_ttc,';
214 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc';
215 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as fd';
216 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON fd.fk_product = p.rowid';
217 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
219 $result = $this->db->query($sql);
221 $objp = $this->db->fetch_object($result);
224 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.$sql;
228 $this->rowid = $objp->rowid;
229 $this->
id = $objp->rowid;
230 $this->fk_facture = $objp->fk_facture;
231 $this->fk_parent_line = $objp->fk_parent_line;
232 $this->label = $objp->custom_label;
233 $this->desc = $objp->description;
234 $this->qty = $objp->qty;
235 $this->subprice = $objp->subprice;
236 $this->ref_ext = $objp->ref_ext;
237 $this->vat_src_code = $objp->vat_src_code;
238 $this->tva_tx = $objp->tva_tx;
239 $this->localtax1_tx = $objp->localtax1_tx;
240 $this->localtax2_tx = $objp->localtax2_tx;
241 $this->remise_percent = $objp->remise_percent;
242 $this->fk_remise_except = $objp->fk_remise_except;
243 $this->fk_product = $objp->fk_product;
244 $this->product_type = $objp->product_type;
245 $this->date_start = $this->db->jdate($objp->date_start);
246 $this->date_end = $this->db->jdate($objp->date_end);
247 $this->info_bits = $objp->info_bits;
248 $this->tva_npr = ((($objp->info_bits & 1) == 1) ? 1 : 0);
249 $this->special_code = $objp->special_code;
250 $this->total_ht = $objp->total_ht;
251 $this->total_tva = $objp->total_tva;
252 $this->total_localtax1 = $objp->total_localtax1;
253 $this->total_localtax2 = $objp->total_localtax2;
254 $this->total_ttc = $objp->total_ttc;
255 $this->fk_code_ventilation = $objp->fk_code_ventilation;
256 $this->batch = $objp->batch;
257 $this->fk_warehouse = $objp->fk_warehouse;
258 $this->rang = $objp->rang;
259 $this->fk_fournprice = $objp->fk_fournprice;
260 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
261 $this->pa_ht = $marginInfos[0];
262 $this->marge_tx = (string) $marginInfos[1];
263 $this->marque_tx = (string) $marginInfos[2];
265 $this->
ref = $objp->product_ref;
267 $this->product_ref = $objp->product_ref;
268 $this->product_label = $objp->product_label;
269 $this->product_desc = $objp->product_desc;
271 $this->fk_unit = $objp->fk_unit;
272 $this->fk_user_modif = $objp->fk_user_modif;
273 $this->fk_user_author = $objp->fk_user_author;
275 $this->situation_percent = $objp->situation_percent;
276 $this->fk_prev_id = $objp->fk_prev_id;
278 $this->multicurrency_subprice = $objp->multicurrency_subprice;
279 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
280 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
281 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
285 $this->db->free($result);
289 $this->error = $this->db->lasterror();
301 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
303 global $langs, $user;
307 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
308 $this->pa_ht = (float) $this->pa_ht;
310 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
313 $this->desc = trim($this->desc);
314 if (empty($this->tva_tx)) {
317 if (empty($this->localtax1_tx)) {
318 $this->localtax1_tx = 0;
320 if (empty($this->localtax2_tx)) {
321 $this->localtax2_tx = 0;
323 if (empty($this->localtax1_type)) {
324 $this->localtax1_type = 0;
326 if (empty($this->localtax2_type)) {
327 $this->localtax2_type = 0;
329 if (empty($this->total_localtax1)) {
330 $this->total_localtax1 = 0;
332 if (empty($this->total_localtax2)) {
333 $this->total_localtax2 = 0;
335 if (empty($this->rang)) {
338 if (empty($this->remise_percent)) {
339 $this->remise_percent = 0;
341 if (empty($this->info_bits)) {
342 $this->info_bits = 0;
344 if (empty($this->subprice)) {
347 if (empty($this->ref_ext)) {
350 if (empty($this->special_code)) {
351 $this->special_code = 0;
353 if (empty($this->fk_parent_line)) {
354 $this->fk_parent_line = 0;
356 if (empty($this->fk_prev_id)) {
357 $this->fk_prev_id = 0;
359 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
360 $this->situation_percent = 100;
363 if (empty($this->multicurrency_subprice)) {
364 $this->multicurrency_subprice = 0;
366 if (empty($this->multicurrency_total_ht)) {
367 $this->multicurrency_total_ht = 0;
369 if (empty($this->multicurrency_total_tva)) {
370 $this->multicurrency_total_tva = 0;
372 if (empty($this->multicurrency_total_ttc)) {
373 $this->multicurrency_total_ttc = 0;
377 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
378 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
382 $this->pa_ht = $result;
387 if ($this->product_type < 0) {
388 $this->error =
'ErrorProductTypeMustBe0orMore';
391 if (!empty($this->fk_product) && $this->fk_product > 0) {
395 $this->error =
'ErrorProductIdDoesNotExists';
396 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
404 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
405 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
406 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
407 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
408 $sql .=
' date_start, date_end, fk_code_ventilation,';
409 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
410 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
411 $sql .=
' situation_percent, fk_prev_id,';
412 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
413 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,';
414 $sql .=
' batch, fk_warehouse';
416 $sql .=
" VALUES (".$this->fk_facture.
",";
417 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
418 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
419 $sql .=
" '".$this->db->escape($this->desc).
"',";
420 $sql .=
" ".price2num($this->qty).
",";
421 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
422 $sql .=
" ".price2num($this->tva_tx).
",";
423 $sql .=
" ".price2num($this->localtax1_tx).
",";
424 $sql .=
" ".price2num($this->localtax2_tx).
",";
425 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
426 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
427 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
428 $sql .=
" ".((int) $this->product_type).
",";
429 $sql .=
" ".price2num($this->remise_percent).
",";
430 $sql .=
" ".price2num($this->subprice).
",";
431 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
432 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
433 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
434 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
435 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
436 $sql .=
' '.((int) $this->rang).
',';
437 $sql .=
' '.((int) $this->special_code).
',';
438 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
439 $sql .=
' '.price2num($this->pa_ht).
',';
440 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
441 $sql .=
" ".price2num($this->total_ht).
",";
442 $sql .=
" ".price2num($this->total_tva).
",";
443 $sql .=
" ".price2num($this->total_ttc).
",";
444 $sql .=
" ".price2num($this->total_localtax1).
",";
445 $sql .=
" ".price2num($this->total_localtax2);
446 $sql .=
", ".((float) $this->situation_percent);
447 $sql .=
", ".(!empty($this->fk_prev_id) ? $this->fk_prev_id :
"null");
448 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
449 $sql .=
", ".((int) $user->id);
450 $sql .=
", ".((int) $user->id);
451 $sql .=
", ".(int) $this->fk_multicurrency;
452 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
453 $sql .=
", ".price2num($this->multicurrency_subprice);
454 $sql .=
", ".price2num($this->multicurrency_total_ht);
455 $sql .=
", ".price2num($this->multicurrency_total_tva);
456 $sql .=
", ".price2num($this->multicurrency_total_ttc);
457 $sql .=
", '".$this->db->escape($this->batch).
"'";
458 $sql .=
", ".((int) $this->fk_warehouse);
461 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
462 $resql = $this->db->query($sql);
464 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
465 $this->rowid = $this->id;
476 if ($this->fk_remise_except && empty($error)) {
478 $result = $discount->fetch($this->fk_remise_except);
483 if ($discount->fk_facture_line > 0) {
484 if (empty($noerrorifdiscountalreadylinked)) {
485 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
486 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
487 $this->db->rollback();
491 $result = $discount->link_to_invoice($this->rowid, 0);
493 $this->error = $discount->error;
494 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
495 $this->db->rollback();
500 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
501 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
502 $this->db->rollback();
506 $this->error = $discount->error;
507 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
508 $this->db->rollback();
513 if (!$notrigger && empty($error)) {
515 $result = $this->
call_trigger(
'LINEBILL_INSERT', $user);
517 $this->db->rollback();
528 foreach ($this->errors as $errmsg) {
529 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
530 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
532 $this->db->rollback();
535 $this->error = $this->db->lasterror();
536 $this->db->rollback();
548 public function update($user =
null, $notrigger = 0)
554 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
557 $this->desc = trim($this->desc);
558 if (empty($this->ref_ext)) {
561 if (empty($this->tva_tx)) {
564 if (empty($this->localtax1_tx)) {
565 $this->localtax1_tx = 0;
567 if (empty($this->localtax2_tx)) {
568 $this->localtax2_tx = 0;
570 if (empty($this->localtax1_type)) {
571 $this->localtax1_type = 0;
573 if (empty($this->localtax2_type)) {
574 $this->localtax2_type = 0;
576 if (empty($this->total_localtax1)) {
577 $this->total_localtax1 = 0;
579 if (empty($this->total_localtax2)) {
580 $this->total_localtax2 = 0;
582 if (empty($this->remise_percent)) {
583 $this->remise_percent = 0;
585 if (empty($this->info_bits)) {
586 $this->info_bits = 0;
588 if (empty($this->special_code)) {
589 $this->special_code = 0;
591 if (empty($this->product_type)) {
592 $this->product_type = 0;
594 if (empty($this->fk_parent_line)) {
595 $this->fk_parent_line = 0;
597 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
598 $this->situation_percent = 100;
600 if (empty($this->pa_ht)) {
604 if (empty($this->multicurrency_subprice)) {
605 $this->multicurrency_subprice = 0;
607 if (empty($this->multicurrency_total_ht)) {
608 $this->multicurrency_total_ht = 0;
610 if (empty($this->multicurrency_total_tva)) {
611 $this->multicurrency_total_tva = 0;
613 if (empty($this->multicurrency_total_ttc)) {
614 $this->multicurrency_total_ttc = 0;
618 if ($this->product_type < 0) {
623 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
625 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
629 $this->pa_ht = $result;
636 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
637 $sql .=
" description='".$this->db->escape($this->desc).
"'";
638 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
639 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
640 $sql .=
", subprice=".price2num($this->subprice);
641 $sql .=
", remise_percent=".price2num($this->remise_percent);
642 if ($this->fk_remise_except) {
643 $sql .=
", fk_remise_except=".$this->fk_remise_except;
645 $sql .=
", fk_remise_except=null";
647 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
648 $sql .=
", tva_tx=".price2num($this->tva_tx);
649 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
650 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
651 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
652 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
653 $sql .=
", qty=".price2num($this->qty);
654 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
655 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
656 $sql .=
", product_type=".$this->product_type;
657 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
658 $sql .=
", special_code=" . (int) $this->special_code;
659 if (empty($this->skip_update_total)) {
660 $sql .=
", total_ht=".price2num($this->total_ht);
661 $sql .=
", total_tva=".price2num($this->total_tva);
662 $sql .=
", total_ttc=".price2num($this->total_ttc);
663 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
664 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
666 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
667 $sql .=
", buy_price_ht=".(($this->pa_ht || (string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
668 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
669 if (!empty($this->rang)) {
670 $sql .=
", rang=".((int) $this->rang);
672 $sql .=
", situation_percent = ".((float) $this->situation_percent);
673 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
674 $sql .=
", fk_user_modif = ".((int) $user->id);
677 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
678 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
679 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
680 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
682 $sql .=
", batch = '".$this->db->escape($this->batch).
"'";
683 $sql .=
", fk_warehouse = ".((int) $this->fk_warehouse);
685 $sql .=
" WHERE rowid = ".((int) $this->rowid);
687 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
688 $resql = $this->db->query($sql);
691 $this->
id = $this->rowid;
698 if (!$error && !$notrigger) {
700 $result = $this->
call_trigger(
'LINEBILL_MODIFY', $user);
702 $this->db->rollback();
713 foreach ($this->errors as $errmsg) {
714 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
715 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
718 $this->db->rollback();
721 $this->error = $this->db->error();
722 $this->db->rollback();
850 global $invoicecache;
852 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
856 if (!isset($invoicecache[$invoiceid])) {
857 $invoicecache[$invoiceid] =
new Facture($this->db);
858 $invoicecache[$invoiceid]->fetch($invoiceid);
864 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet";
865 $sql .=
" WHERE rowid = ".((int) $this->fk_prev_id);
867 $resql = $this->db->query($sql);
869 if ($resql && $this->db->num_rows($resql) > 0) {
872 $obj = $this->db->fetch_object($resql);
874 $returnPercent = (float) $obj->situation_percent;
877 if ($include_credit_note) {
878 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
879 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
880 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
881 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
882 $sql .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
884 $res = $this->db->query($sql);
886 while ($obj = $this->db->fetch_object($res)) {
887 $returnPercent += (float) $obj->situation_percent;
894 return $returnPercent;
896 $this->error = $this->db->error();
897 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
898 $this->db->rollback();
917 global $invoicecache;
919 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
923 if (!isset($invoicecache[$invoiceid])) {
924 $invoicecache[$invoiceid] =
new Facture($this->db);
925 $invoicecache[$invoiceid]->fetch($invoiceid);
932 $lastprevid = $this->fk_prev_id;
933 $cumulated_percent = 0.0;
935 while (!$all_found) {
936 $sql =
"SELECT situation_percent, fk_prev_id FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $lastprevid);
937 $resql = $this->db->query($sql);
939 if ($resql && $this->db->num_rows($resql) > 0) {
940 $obj = $this->db->fetch_object($resql);
941 $cumulated_percent += floatval($obj->situation_percent);
943 if ($include_credit_note) {
944 $sql_credit_note =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
945 $sql_credit_note .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
946 $sql_credit_note .=
" WHERE fd.fk_prev_id = ".((int) $lastprevid);
947 $sql_credit_note .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
948 $sql_credit_note .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
950 $res_credit_note = $this->db->query($sql_credit_note);
951 if ($res_credit_note) {
952 while ($cn = $this->db->fetch_object($res_credit_note)) {
953 $cumulated_percent += floatval($cn->situation_percent);
961 if ($obj->fk_prev_id) {
962 $lastprevid = $obj->fk_prev_id;
967 $this->error = $this->db->error();
968 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
969 $this->db->rollback();
973 return $cumulated_percent;