201 global
$conf, $extrafields;
203 $extraFieldsCheck =
false;
204 $doFetchInOneSqlRequest =
getDolGlobalInt(
'MAIN_DO_FETCH_IN_ONE_SQL_REQUEST');
206 if ($doFetchInOneSqlRequest) {
208 if (!isset($extrafields) || !is_object($extrafields)) {
209 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
214 if (empty($extrafields->attributes[$this->table_element][
'loaded'])) {
215 $extrafields->fetch_name_optionals_label($this->table_element);
218 $extraFieldsCheck = (
219 !empty($extrafields->attributes[$this->table_element][
'label'])
220 && is_array($extrafields->attributes[$this->table_element][
'label'])
221 && count($extrafields->attributes[$this->table_element][
'label']) > 0
225 $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,';
226 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.ref_ext,';
227 $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,';
228 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
229 $sql .=
' fd.fk_code_ventilation,';
230 $sql .=
' fd.batch, fd.fk_warehouse,';
231 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
232 $sql .=
' fd.situation_percent, fd.fk_prev_id, fd.extraparams,';
233 $sql .=
' fd.multicurrency_subprice,';
234 $sql .=
' fd.multicurrency_total_ht,';
235 $sql .=
' fd.multicurrency_total_tva,';
236 $sql .=
' fd.multicurrency_total_ttc,';
237 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
238 $sql .=
' p.packaging';
240 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
241 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
242 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
243 ? $extrafields->attributes[$this->table_element][
'type'][$key]
246 if ($type !==
'separate') {
247 if (in_array($type, array(
'point',
'multipts',
'linestrg',
'polygon'))) {
248 $sql .=
", ST_AsWKT(ef.".$this->db->sanitize($key).
") as ".$this->db->sanitize($key);
250 $sql .=
", ef.".$this->db->sanitize($key);
256 $sql .=
' FROM '.$this->db->prefix().
'facturedet as fd';
258 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
260 $sql .=
' LEFT JOIN '.$this->db->prefix().$this->table_element.
'_extrafields as ef ON fd.rowid = ef.fk_object';
263 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON fd.fk_product = p.rowid';
264 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
266 $result = $this->db->query($sql);
268 $objp = $this->db->fetch_object($result);
271 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.$sql;
275 $this->rowid = $objp->rowid;
276 $this->
id = $objp->rowid;
277 $this->fk_facture = $objp->fk_facture;
278 $this->fk_parent_line = $objp->fk_parent_line;
279 $this->label = $objp->custom_label;
280 $this->desc = $objp->description;
281 $this->qty = $objp->qty;
282 $this->subprice = $objp->subprice;
283 $this->ref_ext = $objp->ref_ext;
284 $this->vat_src_code = $objp->vat_src_code;
285 $this->tva_tx = $objp->tva_tx;
286 $this->localtax1_tx = $objp->localtax1_tx;
287 $this->localtax2_tx = $objp->localtax2_tx;
288 $this->remise_percent = $objp->remise_percent;
289 $this->fk_remise_except = $objp->fk_remise_except;
290 $this->fk_product = $objp->fk_product;
291 $this->product_type = $objp->product_type;
292 $this->date_start = $this->db->jdate($objp->date_start);
293 $this->date_end = $this->db->jdate($objp->date_end);
294 $this->info_bits = $objp->info_bits;
295 $this->tva_npr = ((($objp->info_bits & 1) == 1) ? 1 : 0);
296 $this->special_code = $objp->special_code;
297 $this->total_ht = $objp->total_ht;
298 $this->total_tva = $objp->total_tva;
299 $this->total_localtax1 = $objp->total_localtax1;
300 $this->total_localtax2 = $objp->total_localtax2;
301 $this->total_ttc = $objp->total_ttc;
302 $this->fk_code_ventilation = $objp->fk_code_ventilation;
303 $this->batch = $objp->batch;
304 $this->fk_warehouse = $objp->fk_warehouse;
305 $this->rang = $objp->rang;
306 $this->fk_fournprice = $objp->fk_fournprice;
307 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
308 $this->pa_ht = $marginInfos[0];
309 $this->marge_tx = (
string) $marginInfos[1];
310 $this->marque_tx = (
string) $marginInfos[2];
312 $this->
ref = $objp->product_ref;
314 $this->product_ref = $objp->product_ref;
315 $this->product_label = $objp->product_label;
316 $this->product_desc = $objp->product_desc;
318 $this->fk_unit = $objp->fk_unit;
319 $this->fk_user_modif = $objp->fk_user_modif;
320 $this->fk_user_author = $objp->fk_user_author;
322 $this->situation_percent = $objp->situation_percent;
323 $this->fk_prev_id = $objp->fk_prev_id;
325 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
327 $this->multicurrency_subprice = $objp->multicurrency_subprice;
328 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
329 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
330 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
332 $this->packaging = $objp->packaging;
334 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
335 $this->array_options = array();
337 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
338 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
339 ? $extrafields->attributes[$this->table_element][
'type'][$key]
342 if ($type !==
'separate') {
343 $rawval = $objp->$key;
346 if (in_array($type, array(
'date',
'datetime'))) {
347 $this->array_options[
'options_' . $key] = $this->db->jdate($rawval);
348 } elseif ($type ==
'password') {
349 if (!empty($rawval) && preg_match(
'/^dolcrypt:/', $rawval)) {
350 $this->array_options[
'options_' . $key] =
dolDecrypt($rawval);
352 $this->array_options[
'options_' . $key] = $rawval;
355 $this->array_options[
'options_' . $key] = $rawval;
361 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
362 if (!empty($extrafields->attributes[$this->table_element][
'computed'][$key])) {
363 if (empty(
$conf->disable_compute)) {
364 global $objectoffield;
365 $objectoffield = $this;
366 $this->array_options[
'options_' . $key] =
dol_eval((
string) $extrafields->attributes[$this->table_element][
'computed'][$key], 1, 0,
'2');
372 if (!$doFetchInOneSqlRequest) {
377 $this->db->free($result);
381 $this->error = $this->db->lasterror();
393 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
395 global $langs, $user;
399 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
400 $this->pa_ht = (float) $this->pa_ht;
402 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
405 $this->desc = trim($this->desc);
406 if (empty($this->tva_tx)) {
409 if (empty($this->localtax1_tx)) {
410 $this->localtax1_tx = 0;
412 if (empty($this->localtax2_tx)) {
413 $this->localtax2_tx = 0;
415 if (empty($this->localtax1_type)) {
416 $this->localtax1_type = 0;
418 if (empty($this->localtax2_type)) {
419 $this->localtax2_type = 0;
421 if (empty($this->total_localtax1)) {
422 $this->total_localtax1 = 0;
424 if (empty($this->total_localtax2)) {
425 $this->total_localtax2 = 0;
427 if (empty($this->rang)) {
430 if (empty($this->remise_percent)) {
431 $this->remise_percent = 0;
433 if (empty($this->info_bits)) {
434 $this->info_bits = 0;
436 if (empty($this->subprice)) {
439 if (empty($this->ref_ext)) {
442 if (empty($this->special_code)) {
443 $this->special_code = 0;
445 if (empty($this->fk_parent_line)) {
446 $this->fk_parent_line = 0;
448 if (empty($this->fk_prev_id)) {
449 $this->fk_prev_id = 0;
451 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
452 $this->situation_percent = 100;
455 if (empty($this->multicurrency_subprice)) {
456 $this->multicurrency_subprice = 0;
458 if (empty($this->multicurrency_total_ht)) {
459 $this->multicurrency_total_ht = 0;
461 if (empty($this->multicurrency_total_tva)) {
462 $this->multicurrency_total_tva = 0;
464 if (empty($this->multicurrency_total_ttc)) {
465 $this->multicurrency_total_ttc = 0;
469 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
470 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
474 $this->pa_ht = $result;
479 if ($this->product_type < 0) {
480 $this->error =
'ErrorProductTypeMustBe0orMore';
483 if (!empty($this->fk_product) && $this->fk_product > 0) {
487 $this->error =
'ErrorProductIdDoesNotExists';
488 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
496 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
497 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
498 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
499 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
500 $sql .=
' date_start, date_end, fk_code_ventilation,';
501 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
502 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
503 $sql .=
' situation_percent, fk_prev_id,';
504 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
505 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,';
506 $sql .=
' batch, fk_warehouse';
508 $sql .=
" VALUES (".$this->fk_facture.
",";
509 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
510 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
511 $sql .=
" '".$this->db->escape($this->desc).
"',";
512 $sql .=
" ".price2num($this->qty).
",";
513 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
514 $sql .=
" ".price2num($this->tva_tx).
",";
515 $sql .=
" ".price2num($this->localtax1_tx).
",";
516 $sql .=
" ".price2num($this->localtax2_tx).
",";
517 $sql .=
" '".$this->db->escape((
string) $this->localtax1_type).
"',";
518 $sql .=
" '".$this->db->escape((
string) $this->localtax2_type).
"',";
519 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
520 $sql .=
" ".((int) $this->product_type).
",";
521 $sql .=
" ".price2num($this->remise_percent).
",";
522 $sql .=
" ".price2num($this->subprice).
",";
523 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
524 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
525 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
526 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
527 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
528 $sql .=
' '.((int) $this->rang).
',';
529 $sql .=
' '.((int) $this->special_code).
',';
530 $sql .=
' '.(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) :
"null").
',';
531 $sql .=
' '.price2num($this->pa_ht).
',';
532 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
533 $sql .=
" ".price2num($this->total_ht).
",";
534 $sql .=
" ".price2num($this->total_tva).
",";
535 $sql .=
" ".price2num($this->total_ttc).
",";
536 $sql .=
" ".price2num($this->total_localtax1).
",";
537 $sql .=
" ".price2num($this->total_localtax2);
538 $sql .=
", ".((float) $this->situation_percent);
539 $sql .=
", ".(!empty($this->fk_prev_id) ? ((int) $this->fk_prev_id) :
"null");
540 $sql .=
", ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
541 $sql .=
", ".((int) $user->id);
542 $sql .=
", ".((int) $user->id);
543 $sql .=
", ".(int) $this->fk_multicurrency;
544 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
545 $sql .=
", ".price2num($this->multicurrency_subprice);
546 $sql .=
", ".price2num($this->multicurrency_total_ht);
547 $sql .=
", ".price2num($this->multicurrency_total_tva);
548 $sql .=
", ".price2num($this->multicurrency_total_ttc);
549 $sql .=
", '".$this->db->escape($this->batch).
"'";
550 $sql .=
", ".((int) $this->fk_warehouse);
553 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
554 $resql = $this->db->query($sql);
556 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
557 $this->rowid = $this->id;
568 if ($this->fk_remise_except && empty($error)) {
570 $result = $discount->fetch($this->fk_remise_except);
575 if ($discount->fk_facture_line > 0) {
576 if (empty($noerrorifdiscountalreadylinked)) {
577 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
578 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
579 $this->db->rollback();
583 $result = $discount->link_to_invoice($this->rowid, 0);
585 $this->error = $discount->error;
586 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
587 $this->db->rollback();
592 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
593 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
594 $this->db->rollback();
598 $this->error = $discount->error;
599 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
600 $this->db->rollback();
605 if (!$notrigger && empty($error)) {
607 $result = $this->call_trigger(
'LINEBILL_INSERT', $user);
609 $this->db->rollback();
620 foreach ($this->errors as $errmsg) {
621 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
622 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
624 $this->db->rollback();
627 $this->error = $this->db->lasterror();
628 $this->db->rollback();
640 public function update($user =
null, $notrigger = 0)
646 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
649 $this->desc = trim($this->desc);
650 if (empty($this->ref_ext)) {
653 if (empty($this->tva_tx)) {
656 if (empty($this->localtax1_tx)) {
657 $this->localtax1_tx = 0;
659 if (empty($this->localtax2_tx)) {
660 $this->localtax2_tx = 0;
662 if (empty($this->localtax1_type)) {
663 $this->localtax1_type = 0;
665 if (empty($this->localtax2_type)) {
666 $this->localtax2_type = 0;
668 if (empty($this->total_localtax1)) {
669 $this->total_localtax1 = 0;
671 if (empty($this->total_localtax2)) {
672 $this->total_localtax2 = 0;
674 if (empty($this->remise_percent)) {
675 $this->remise_percent = 0;
677 if (empty($this->info_bits)) {
678 $this->info_bits = 0;
680 if (empty($this->special_code)) {
681 $this->special_code = 0;
683 if (empty($this->product_type)) {
684 $this->product_type = 0;
686 if (empty($this->fk_parent_line)) {
687 $this->fk_parent_line = 0;
689 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
690 $this->situation_percent = 100;
692 if (empty($this->pa_ht)) {
696 if (empty($this->multicurrency_subprice)) {
697 $this->multicurrency_subprice = 0;
699 if (empty($this->multicurrency_total_ht)) {
700 $this->multicurrency_total_ht = 0;
702 if (empty($this->multicurrency_total_tva)) {
703 $this->multicurrency_total_tva = 0;
705 if (empty($this->multicurrency_total_ttc)) {
706 $this->multicurrency_total_ttc = 0;
710 if ($this->product_type < 0) {
715 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
717 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
721 $this->pa_ht = $result;
728 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
729 $sql .=
" description='".$this->db->escape($this->desc).
"'";
730 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
731 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
732 $sql .=
", subprice=".price2num($this->subprice);
733 $sql .=
", remise_percent=".price2num($this->remise_percent);
734 if ($this->fk_remise_except) {
735 $sql .=
", fk_remise_except = ".((int) $this->fk_remise_except);
737 $sql .=
", fk_remise_except = null";
739 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
740 $sql .=
", tva_tx=".price2num($this->tva_tx);
741 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
742 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
743 $sql .=
", localtax1_type='".$this->db->escape((
string) $this->localtax1_type).
"'";
744 $sql .=
", localtax2_type='".$this->db->escape((
string) $this->localtax2_type).
"'";
745 $sql .=
", qty = ".((float)
price2num($this->qty));
746 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
747 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
748 $sql .=
", product_type = ".((int) $this->product_type);
749 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
750 $sql .=
", special_code = " . (int) $this->special_code;
751 if (empty($this->skip_update_total)) {
752 $sql .=
", total_ht = ".price2num($this->total_ht);
753 $sql .=
", total_tva = ".price2num($this->total_tva);
754 $sql .=
", total_ttc = ".price2num($this->total_ttc);
755 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
756 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
758 $sql .=
", fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
759 $sql .=
", buy_price_ht = ".(($this->pa_ht || (
string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
760 $sql .=
", fk_parent_line = ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
761 if (!empty($this->rang)) {
762 $sql .=
", rang = ".((int) $this->rang);
764 $sql .=
", situation_percent = ".((float) $this->situation_percent);
765 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
766 $sql .=
", fk_user_modif = ".((int) $user->id);
769 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
770 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
771 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
772 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
774 $sql .=
", batch = '".$this->db->escape($this->batch).
"'";
775 $sql .=
", fk_warehouse = ".((int) $this->fk_warehouse);
777 $sql .=
" WHERE rowid = ".((int) $this->rowid);
779 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
780 $resql = $this->db->query($sql);
783 $this->
id = $this->rowid;
790 if (!$error && !$notrigger) {
792 $result = $this->call_trigger(
'LINEBILL_MODIFY', $user);
794 $this->db->rollback();
805 foreach ($this->errors as $errmsg) {
806 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
807 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
810 $this->db->rollback();
813 $this->error = $this->db->error();
814 $this->db->rollback();
942 global $invoicecache;
944 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
948 if (!isset($invoicecache[$invoiceid])) {
949 $invoicecache[$invoiceid] =
new Facture($this->db);
950 $invoicecache[$invoiceid]->fetch($invoiceid);
952 if (empty($invoicecache[$invoiceid]->situation_cycle_ref)) {
956 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet";
957 $sql .=
" WHERE rowid = ".((int) $this->fk_prev_id);
959 $resql = $this->db->query($sql);
961 if ($resql && $this->db->num_rows($resql) > 0) {
964 $obj = $this->db->fetch_object($resql);
966 $returnPercent = (float) $obj->situation_percent;
969 if ($include_credit_note) {
970 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
971 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
972 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
973 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
976 $res = $this->db->query($sql);
978 while ($obj = $this->db->fetch_object($res)) {
979 $returnPercent += (float) $obj->situation_percent;
986 return $returnPercent;
988 $this->error = $this->db->error();
989 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
990 $this->db->rollback();
1009 global $invoicecache;
1011 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
1015 if (!isset($invoicecache[$invoiceid])) {
1016 $invoicecache[$invoiceid] =
new Facture($this->db);
1017 $invoicecache[$invoiceid]->fetch($invoiceid);
1019 if (!$invoicecache[$invoiceid]->isSituationInvoice()) {
1024 $lastprevid = $this->fk_prev_id;
1025 $cumulated_percent = 0.0;
1027 while (!$all_found) {
1028 $sql =
"SELECT situation_percent, fk_prev_id FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $lastprevid);
1029 $resql = $this->db->query($sql);
1031 if ($resql && $this->db->num_rows($resql) > 0) {
1032 $obj = $this->db->fetch_object($resql);
1033 $cumulated_percent += (float) $obj->situation_percent;
1035 if ($include_credit_note) {
1036 $sql_credit_note =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
1037 $sql_credit_note .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
1038 $sql_credit_note .=
" WHERE fd.fk_prev_id = ".((int) $lastprevid);
1039 $sql_credit_note .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
1040 $sql_credit_note .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
1042 $res_credit_note = $this->db->query($sql_credit_note);
1043 if ($res_credit_note) {
1044 while ($cn = $this->db->fetch_object($res_credit_note)) {
1045 $cumulated_percent += (float) $cn->situation_percent;
1053 if ($obj->fk_prev_id) {
1054 $lastprevid = $obj->fk_prev_id;
1059 $this->error = $this->db->error();
1060 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
1061 $this->db->rollback();
1065 return $cumulated_percent;