206 global $conf, $extrafields;
208 $extraFieldsCheck =
false;
209 $doFetchInOneSqlRequest =
getDolGlobalInt(
'MAIN_DO_FETCH_IN_ONE_SQL_REQUEST');
211 if ($doFetchInOneSqlRequest) {
213 if (!isset($extrafields) || !is_object($extrafields)) {
214 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
219 if (empty($extrafields->attributes[$this->table_element][
'loaded'])) {
220 $extrafields->fetch_name_optionals_label($this->table_element);
223 $extraFieldsCheck = (
224 !empty($extrafields->attributes[$this->table_element][
'label'])
225 && is_array($extrafields->attributes[$this->table_element][
'label'])
226 && count($extrafields->attributes[$this->table_element][
'label']) > 0
230 $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,';
231 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.ref_ext,';
232 $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,';
233 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
234 $sql .=
' fd.fk_code_ventilation,';
235 $sql .=
' fd.batch, fd.fk_warehouse,';
236 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
237 $sql .=
' fd.situation_percent, fd.fk_prev_id, fd.extraparams,';
238 $sql .=
' fd.multicurrency_subprice,';
239 $sql .=
' fd.multicurrency_total_ht,';
240 $sql .=
' fd.multicurrency_total_tva,';
241 $sql .=
' fd.multicurrency_total_ttc,';
242 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
243 $sql .=
' p.packaging';
245 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
246 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
247 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
248 ? $extrafields->attributes[$this->table_element][
'type'][$key]
251 if ($type !==
'separate') {
252 if (in_array($type, array(
'point',
'multipts',
'linestrg',
'polygon'))) {
253 $sql .=
", ST_AsWKT(ef.".$key.
") as ".$key;
255 $sql .=
", ef.".$key;
261 $sql .=
' FROM '.$this->db->prefix().
'facturedet as fd';
263 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
265 $sql .=
' LEFT JOIN '.$this->db->prefix().$this->table_element.
'_extrafields as ef ON fd.rowid = ef.fk_object';
268 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON fd.fk_product = p.rowid';
269 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
271 $result = $this->db->query($sql);
273 $objp = $this->db->fetch_object($result);
276 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.$sql;
280 $this->rowid = $objp->rowid;
281 $this->
id = $objp->rowid;
282 $this->fk_facture = $objp->fk_facture;
283 $this->fk_parent_line = $objp->fk_parent_line;
284 $this->label = $objp->custom_label;
285 $this->desc = $objp->description;
286 $this->qty = $objp->qty;
287 $this->subprice = $objp->subprice;
288 $this->ref_ext = $objp->ref_ext;
289 $this->vat_src_code = $objp->vat_src_code;
290 $this->tva_tx = $objp->tva_tx;
291 $this->localtax1_tx = $objp->localtax1_tx;
292 $this->localtax2_tx = $objp->localtax2_tx;
293 $this->remise_percent = $objp->remise_percent;
294 $this->fk_remise_except = $objp->fk_remise_except;
295 $this->fk_product = $objp->fk_product;
296 $this->product_type = $objp->product_type;
297 $this->date_start = $this->db->jdate($objp->date_start);
298 $this->date_end = $this->db->jdate($objp->date_end);
299 $this->info_bits = $objp->info_bits;
300 $this->tva_npr = ((($objp->info_bits & 1) == 1) ? 1 : 0);
301 $this->special_code = $objp->special_code;
302 $this->total_ht = $objp->total_ht;
303 $this->total_tva = $objp->total_tva;
304 $this->total_localtax1 = $objp->total_localtax1;
305 $this->total_localtax2 = $objp->total_localtax2;
306 $this->total_ttc = $objp->total_ttc;
307 $this->fk_code_ventilation = $objp->fk_code_ventilation;
308 $this->batch = $objp->batch;
309 $this->fk_warehouse = $objp->fk_warehouse;
310 $this->rang = $objp->rang;
311 $this->fk_fournprice = $objp->fk_fournprice;
312 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
313 $this->pa_ht = $marginInfos[0];
314 $this->marge_tx = (string) $marginInfos[1];
315 $this->marque_tx = (string) $marginInfos[2];
317 $this->
ref = $objp->product_ref;
319 $this->product_ref = $objp->product_ref;
320 $this->product_label = $objp->product_label;
321 $this->product_desc = $objp->product_desc;
323 $this->fk_unit = $objp->fk_unit;
324 $this->fk_user_modif = $objp->fk_user_modif;
325 $this->fk_user_author = $objp->fk_user_author;
327 $this->situation_percent = $objp->situation_percent;
328 $this->fk_prev_id = $objp->fk_prev_id;
330 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
332 $this->multicurrency_subprice = $objp->multicurrency_subprice;
333 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
334 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
335 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
337 $this->packaging = $objp->packaging;
339 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
340 $this->array_options = array();
342 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
343 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
344 ? $extrafields->attributes[$this->table_element][
'type'][$key]
347 if ($type !==
'separate') {
348 $rawval = $objp->$key;
351 if (in_array($type, array(
'date',
'datetime'))) {
352 $this->array_options[
'options_' . $key] = $this->db->jdate($rawval);
353 } elseif ($type ==
'password') {
354 if (!empty($rawval) && preg_match(
'/^dolcrypt:/', $rawval)) {
355 $this->array_options[
'options_' . $key] =
dolDecrypt($rawval);
357 $this->array_options[
'options_' . $key] = $rawval;
360 $this->array_options[
'options_' . $key] = $rawval;
366 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
367 if (!empty($extrafields->attributes[$this->table_element][
'computed'][$key])) {
368 if (empty($conf->disable_compute)) {
369 global $objectoffield;
370 $objectoffield = $this;
371 $this->array_options[
'options_' . $key] =
dol_eval((
string) $extrafields->attributes[$this->table_element][
'computed'][$key], 1, 0,
'2');
377 if (!$doFetchInOneSqlRequest) {
382 $this->db->free($result);
386 $this->error = $this->db->lasterror();
398 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
400 global $langs, $user;
404 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
405 $this->pa_ht = (float) $this->pa_ht;
407 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
410 $this->desc = trim($this->desc);
411 if (empty($this->tva_tx)) {
414 if (empty($this->localtax1_tx)) {
415 $this->localtax1_tx = 0;
417 if (empty($this->localtax2_tx)) {
418 $this->localtax2_tx = 0;
420 if (empty($this->localtax1_type)) {
421 $this->localtax1_type = 0;
423 if (empty($this->localtax2_type)) {
424 $this->localtax2_type = 0;
426 if (empty($this->total_localtax1)) {
427 $this->total_localtax1 = 0;
429 if (empty($this->total_localtax2)) {
430 $this->total_localtax2 = 0;
432 if (empty($this->rang)) {
435 if (empty($this->remise_percent)) {
436 $this->remise_percent = 0;
438 if (empty($this->info_bits)) {
439 $this->info_bits = 0;
441 if (empty($this->subprice)) {
444 if (empty($this->ref_ext)) {
447 if (empty($this->special_code)) {
448 $this->special_code = 0;
450 if (empty($this->fk_parent_line)) {
451 $this->fk_parent_line = 0;
453 if (empty($this->fk_prev_id)) {
454 $this->fk_prev_id = 0;
456 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
457 $this->situation_percent = 100;
460 if (empty($this->multicurrency_subprice)) {
461 $this->multicurrency_subprice = 0;
463 if (empty($this->multicurrency_total_ht)) {
464 $this->multicurrency_total_ht = 0;
466 if (empty($this->multicurrency_total_tva)) {
467 $this->multicurrency_total_tva = 0;
469 if (empty($this->multicurrency_total_ttc)) {
470 $this->multicurrency_total_ttc = 0;
474 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
475 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
479 $this->pa_ht = $result;
484 if ($this->product_type < 0) {
485 $this->error =
'ErrorProductTypeMustBe0orMore';
488 if (!empty($this->fk_product) && $this->fk_product > 0) {
492 $this->error =
'ErrorProductIdDoesNotExists';
493 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
501 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
502 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
503 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
504 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
505 $sql .=
' date_start, date_end, fk_code_ventilation,';
506 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
507 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
508 $sql .=
' situation_percent, fk_prev_id,';
509 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
510 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,';
511 $sql .=
' batch, fk_warehouse';
513 $sql .=
" VALUES (".$this->fk_facture.
",";
514 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
515 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
516 $sql .=
" '".$this->db->escape($this->desc).
"',";
517 $sql .=
" ".price2num($this->qty).
",";
518 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
519 $sql .=
" ".price2num($this->tva_tx).
",";
520 $sql .=
" ".price2num($this->localtax1_tx).
",";
521 $sql .=
" ".price2num($this->localtax2_tx).
",";
522 $sql .=
" '".$this->db->escape((
string) $this->localtax1_type).
"',";
523 $sql .=
" '".$this->db->escape((
string) $this->localtax2_type).
"',";
524 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
525 $sql .=
" ".((int) $this->product_type).
",";
526 $sql .=
" ".price2num($this->remise_percent).
",";
527 $sql .=
" ".price2num($this->subprice).
",";
528 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
529 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
530 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
531 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
532 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
533 $sql .=
' '.((int) $this->rang).
',';
534 $sql .=
' '.((int) $this->special_code).
',';
535 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
536 $sql .=
' '.price2num($this->pa_ht).
',';
537 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
538 $sql .=
" ".price2num($this->total_ht).
",";
539 $sql .=
" ".price2num($this->total_tva).
",";
540 $sql .=
" ".price2num($this->total_ttc).
",";
541 $sql .=
" ".price2num($this->total_localtax1).
",";
542 $sql .=
" ".price2num($this->total_localtax2);
543 $sql .=
", ".((float) $this->situation_percent);
544 $sql .=
", ".(!empty($this->fk_prev_id) ? $this->fk_prev_id :
"null");
545 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
546 $sql .=
", ".((int) $user->id);
547 $sql .=
", ".((int) $user->id);
548 $sql .=
", ".(int) $this->fk_multicurrency;
549 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
550 $sql .=
", ".price2num($this->multicurrency_subprice);
551 $sql .=
", ".price2num($this->multicurrency_total_ht);
552 $sql .=
", ".price2num($this->multicurrency_total_tva);
553 $sql .=
", ".price2num($this->multicurrency_total_ttc);
554 $sql .=
", '".$this->db->escape($this->batch).
"'";
555 $sql .=
", ".((int) $this->fk_warehouse);
558 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
559 $resql = $this->db->query($sql);
561 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
562 $this->rowid = $this->id;
573 if ($this->fk_remise_except && empty($error)) {
575 $result = $discount->fetch($this->fk_remise_except);
580 if ($discount->fk_facture_line > 0) {
581 if (empty($noerrorifdiscountalreadylinked)) {
582 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
583 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
584 $this->db->rollback();
588 $result = $discount->link_to_invoice($this->rowid, 0);
590 $this->error = $discount->error;
591 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
592 $this->db->rollback();
597 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
598 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
599 $this->db->rollback();
603 $this->error = $discount->error;
604 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
605 $this->db->rollback();
610 if (!$notrigger && empty($error)) {
612 $result = $this->call_trigger(
'LINEBILL_INSERT', $user);
614 $this->db->rollback();
625 foreach ($this->errors as $errmsg) {
626 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
627 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
629 $this->db->rollback();
632 $this->error = $this->db->lasterror();
633 $this->db->rollback();
645 public function update($user =
null, $notrigger = 0)
651 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
654 $this->desc = trim($this->desc);
655 if (empty($this->ref_ext)) {
658 if (empty($this->tva_tx)) {
661 if (empty($this->localtax1_tx)) {
662 $this->localtax1_tx = 0;
664 if (empty($this->localtax2_tx)) {
665 $this->localtax2_tx = 0;
667 if (empty($this->localtax1_type)) {
668 $this->localtax1_type = 0;
670 if (empty($this->localtax2_type)) {
671 $this->localtax2_type = 0;
673 if (empty($this->total_localtax1)) {
674 $this->total_localtax1 = 0;
676 if (empty($this->total_localtax2)) {
677 $this->total_localtax2 = 0;
679 if (empty($this->remise_percent)) {
680 $this->remise_percent = 0;
682 if (empty($this->info_bits)) {
683 $this->info_bits = 0;
685 if (empty($this->special_code)) {
686 $this->special_code = 0;
688 if (empty($this->product_type)) {
689 $this->product_type = 0;
691 if (empty($this->fk_parent_line)) {
692 $this->fk_parent_line = 0;
694 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
695 $this->situation_percent = 100;
697 if (empty($this->pa_ht)) {
701 if (empty($this->multicurrency_subprice)) {
702 $this->multicurrency_subprice = 0;
704 if (empty($this->multicurrency_total_ht)) {
705 $this->multicurrency_total_ht = 0;
707 if (empty($this->multicurrency_total_tva)) {
708 $this->multicurrency_total_tva = 0;
710 if (empty($this->multicurrency_total_ttc)) {
711 $this->multicurrency_total_ttc = 0;
715 if ($this->product_type < 0) {
720 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
722 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
726 $this->pa_ht = $result;
733 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
734 $sql .=
" description='".$this->db->escape($this->desc).
"'";
735 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
736 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
737 $sql .=
", subprice=".price2num($this->subprice);
738 $sql .=
", remise_percent=".price2num($this->remise_percent);
739 if ($this->fk_remise_except) {
740 $sql .=
", fk_remise_except = ".((int) $this->fk_remise_except);
742 $sql .=
", fk_remise_except = null";
744 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
745 $sql .=
", tva_tx=".price2num($this->tva_tx);
746 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
747 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
748 $sql .=
", localtax1_type='".$this->db->escape((
string) $this->localtax1_type).
"'";
749 $sql .=
", localtax2_type='".$this->db->escape((
string) $this->localtax2_type).
"'";
750 $sql .=
", qty = ".((float)
price2num($this->qty));
751 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
752 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
753 $sql .=
", product_type = ".((int) $this->product_type);
754 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
755 $sql .=
", special_code = " . (int) $this->special_code;
756 if (empty($this->skip_update_total)) {
757 $sql .=
", total_ht = ".price2num($this->total_ht);
758 $sql .=
", total_tva = ".price2num($this->total_tva);
759 $sql .=
", total_ttc = ".price2num($this->total_ttc);
760 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
761 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
763 $sql .=
", fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
764 $sql .=
", buy_price_ht = ".(($this->pa_ht || (string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
765 $sql .=
", fk_parent_line = ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
766 if (!empty($this->rang)) {
767 $sql .=
", rang = ".((int) $this->rang);
769 $sql .=
", situation_percent = ".((float) $this->situation_percent);
770 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
771 $sql .=
", fk_user_modif = ".((int) $user->id);
774 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
775 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
776 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
777 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
779 $sql .=
", batch = '".$this->db->escape($this->batch).
"'";
780 $sql .=
", fk_warehouse = ".((int) $this->fk_warehouse);
782 $sql .=
" WHERE rowid = ".((int) $this->rowid);
784 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
785 $resql = $this->db->query($sql);
788 $this->
id = $this->rowid;
795 if (!$error && !$notrigger) {
797 $result = $this->call_trigger(
'LINEBILL_MODIFY', $user);
799 $this->db->rollback();
810 foreach ($this->errors as $errmsg) {
811 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
812 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
815 $this->db->rollback();
818 $this->error = $this->db->error();
819 $this->db->rollback();
947 global $invoicecache;
949 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
953 if (!isset($invoicecache[$invoiceid])) {
954 $invoicecache[$invoiceid] =
new Facture($this->db);
955 $invoicecache[$invoiceid]->fetch($invoiceid);
961 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet";
962 $sql .=
" WHERE rowid = ".((int) $this->fk_prev_id);
964 $resql = $this->db->query($sql);
966 if ($resql && $this->db->num_rows($resql) > 0) {
969 $obj = $this->db->fetch_object($resql);
971 $returnPercent = (float) $obj->situation_percent;
974 if ($include_credit_note) {
975 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
976 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
977 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
978 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
979 $sql .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
981 $res = $this->db->query($sql);
983 while ($obj = $this->db->fetch_object($res)) {
984 $returnPercent += (float) $obj->situation_percent;
991 return $returnPercent;
993 $this->error = $this->db->error();
994 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
995 $this->db->rollback();
1014 global $invoicecache;
1016 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
1020 if (!isset($invoicecache[$invoiceid])) {
1021 $invoicecache[$invoiceid] =
new Facture($this->db);
1022 $invoicecache[$invoiceid]->fetch($invoiceid);
1024 if (!$invoicecache[$invoiceid]->isSituationInvoice()) {
1029 $lastprevid = $this->fk_prev_id;
1030 $cumulated_percent = 0.0;
1032 while (!$all_found) {
1033 $sql =
"SELECT situation_percent, fk_prev_id FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $lastprevid);
1034 $resql = $this->db->query($sql);
1036 if ($resql && $this->db->num_rows($resql) > 0) {
1037 $obj = $this->db->fetch_object($resql);
1038 $cumulated_percent += (float) $obj->situation_percent;
1040 if ($include_credit_note) {
1041 $sql_credit_note =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
1042 $sql_credit_note .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
1043 $sql_credit_note .=
" WHERE fd.fk_prev_id = ".((int) $lastprevid);
1044 $sql_credit_note .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
1045 $sql_credit_note .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
1047 $res_credit_note = $this->db->query($sql_credit_note);
1048 if ($res_credit_note) {
1049 while ($cn = $this->db->fetch_object($res_credit_note)) {
1050 $cumulated_percent += (float) $cn->situation_percent;
1058 if ($obj->fk_prev_id) {
1059 $lastprevid = $obj->fk_prev_id;
1064 $this->error = $this->db->error();
1065 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
1066 $this->db->rollback();
1070 return $cumulated_percent;