202 global
$conf, $extrafields;
204 $extraFieldsCheck =
false;
205 $doFetchInOneSqlRequest =
getDolGlobalInt(
'MAIN_DO_FETCH_IN_ONE_SQL_REQUEST');
207 if ($doFetchInOneSqlRequest) {
209 if (!isset($extrafields) || !is_object($extrafields)) {
210 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
215 if (empty($extrafields->attributes[$this->table_element][
'loaded'])) {
216 $extrafields->fetch_name_optionals_label($this->table_element);
219 $extraFieldsCheck = (
220 !empty($extrafields->attributes[$this->table_element][
'label'])
221 && is_array($extrafields->attributes[$this->table_element][
'label'])
222 && count($extrafields->attributes[$this->table_element][
'label']) > 0
226 $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,';
227 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.subprice_ttc, fd.ref_ext,';
228 $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,';
229 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
230 $sql .=
' fd.fk_code_ventilation,';
231 $sql .=
' fd.batch, fd.fk_warehouse,';
232 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
233 $sql .=
' fd.situation_percent, fd.fk_prev_id, fd.extraparams,';
234 $sql .=
' fd.multicurrency_subprice,';
235 $sql .=
' fd.multicurrency_total_ht,';
236 $sql .=
' fd.multicurrency_total_tva,';
237 $sql .=
' fd.multicurrency_total_ttc,';
238 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
239 $sql .=
' p.packaging';
241 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
242 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
243 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
244 ? $extrafields->attributes[$this->table_element][
'type'][$key]
247 if ($type !==
'separate') {
248 if (in_array($type, array(
'point',
'multipts',
'linestrg',
'polygon'))) {
249 $sql .=
", ST_AsWKT(ef.".$this->db->sanitize($key).
") as ".$this->db->sanitize($key);
251 $sql .=
", ef.".$this->db->sanitize($key);
257 $sql .=
' FROM '.$this->db->prefix().
'facturedet as fd';
259 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
261 $sql .=
' LEFT JOIN '.$this->db->prefix().$this->table_element.
'_extrafields as ef ON fd.rowid = ef.fk_object';
264 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON fd.fk_product = p.rowid';
265 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
267 $result = $this->db->query($sql);
269 $objp = $this->db->fetch_object($result);
272 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.$sql;
276 $this->rowid = $objp->rowid;
277 $this->
id = $objp->rowid;
278 $this->fk_facture = $objp->fk_facture;
279 $this->fk_parent_line = $objp->fk_parent_line;
280 $this->label = $objp->custom_label;
281 $this->desc = $objp->description;
282 $this->qty = $objp->qty;
283 $this->subprice = $objp->subprice;
284 $this->subprice_ttc = $objp->subprice_ttc;
285 $this->ref_ext = $objp->ref_ext;
286 $this->vat_src_code = $objp->vat_src_code;
287 $this->tva_tx = $objp->tva_tx;
288 $this->localtax1_tx = $objp->localtax1_tx;
289 $this->localtax2_tx = $objp->localtax2_tx;
290 $this->remise_percent = $objp->remise_percent;
291 $this->fk_remise_except = $objp->fk_remise_except;
292 $this->fk_product = $objp->fk_product;
293 $this->product_type = $objp->product_type;
294 $this->date_start = $this->db->jdate($objp->date_start);
295 $this->date_end = $this->db->jdate($objp->date_end);
296 $this->info_bits = $objp->info_bits;
297 $this->tva_npr = ((($objp->info_bits & 1) == 1) ? 1 : 0);
298 $this->special_code = $objp->special_code;
299 $this->total_ht = $objp->total_ht;
300 $this->total_tva = $objp->total_tva;
301 $this->total_localtax1 = $objp->total_localtax1;
302 $this->total_localtax2 = $objp->total_localtax2;
303 $this->total_ttc = $objp->total_ttc;
304 $this->fk_code_ventilation = $objp->fk_code_ventilation;
305 $this->batch = $objp->batch;
306 $this->fk_warehouse = $objp->fk_warehouse;
307 $this->rang = $objp->rang;
308 $this->fk_fournprice = $objp->fk_fournprice;
309 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
310 $this->pa_ht = $marginInfos[0];
311 $this->marge_tx = (
string) $marginInfos[1];
312 $this->marque_tx = (
string) $marginInfos[2];
314 $this->
ref = $objp->product_ref;
316 $this->product_ref = $objp->product_ref;
317 $this->product_label = $objp->product_label;
318 $this->product_desc = $objp->product_desc;
320 $this->fk_unit = $objp->fk_unit;
321 $this->fk_user_modif = $objp->fk_user_modif;
322 $this->fk_user_author = $objp->fk_user_author;
324 $this->situation_percent = $objp->situation_percent;
325 $this->fk_prev_id = $objp->fk_prev_id;
327 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
329 $this->multicurrency_subprice = $objp->multicurrency_subprice;
330 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
331 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
332 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
334 $this->packaging = $objp->packaging;
336 if ($doFetchInOneSqlRequest && $extraFieldsCheck) {
337 $this->array_options = array();
339 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
340 $type = !empty($extrafields->attributes[$this->table_element][
'type'][$key])
341 ? $extrafields->attributes[$this->table_element][
'type'][$key]
344 if ($type !==
'separate') {
345 $rawval = $objp->$key;
348 if (in_array($type, array(
'date',
'datetime'))) {
349 $this->array_options[
'options_' . $key] = $this->db->jdate($rawval);
350 } elseif ($type ==
'password') {
351 if (!empty($rawval) && preg_match(
'/^dolcrypt:/', $rawval)) {
352 $this->array_options[
'options_' . $key] =
dolDecrypt($rawval);
354 $this->array_options[
'options_' . $key] = $rawval;
357 $this->array_options[
'options_' . $key] = $rawval;
363 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
364 if (!empty($extrafields->attributes[$this->table_element][
'computed'][$key])) {
365 if (empty(
$conf->disable_compute)) {
366 global $objectoffield;
367 $objectoffield = $this;
368 $this->array_options[
'options_' . $key] =
dol_eval((
string) $extrafields->attributes[$this->table_element][
'computed'][$key], 1, 0,
'2');
374 if (!$doFetchInOneSqlRequest) {
379 $this->db->free($result);
383 $this->error = $this->db->lasterror();
395 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
397 global $langs, $user;
401 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
402 $this->pa_ht = (float) $this->pa_ht;
404 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
407 $this->desc = trim($this->desc);
408 if (empty($this->tva_tx)) {
411 if (empty($this->localtax1_tx)) {
412 $this->localtax1_tx = 0;
414 if (empty($this->localtax2_tx)) {
415 $this->localtax2_tx = 0;
417 if (empty($this->localtax1_type)) {
418 $this->localtax1_type = 0;
420 if (empty($this->localtax2_type)) {
421 $this->localtax2_type = 0;
423 if (empty($this->total_localtax1)) {
424 $this->total_localtax1 = 0;
426 if (empty($this->total_localtax2)) {
427 $this->total_localtax2 = 0;
429 if (empty($this->rang)) {
432 if (empty($this->remise_percent)) {
433 $this->remise_percent = 0;
435 if (empty($this->info_bits)) {
436 $this->info_bits = 0;
438 if (empty($this->subprice)) {
441 if (empty($this->subprice_ttc)) {
442 $this->subprice_ttc = 0;
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, subprice_ttc, 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 (".((int) $this->fk_facture).
",";
514 $sql .=
" ".($this->fk_parent_line > 0 ? ((int) $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) ? ((
int) $this->fk_product) :
"null").
',';
525 $sql .=
" ".((int) $this->product_type).
",";
526 $sql .=
" ".price2num($this->remise_percent).
",";
527 $sql .=
" ".price2num($this->subprice).
",";
528 $sql .=
" ".price2num($this->subprice_ttc).
",";
529 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
530 $sql .=
' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) :
"null").
',';
531 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
532 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
533 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
534 $sql .=
' '.((int) $this->rang).
',';
535 $sql .=
' '.((int) $this->special_code).
',';
536 $sql .=
' '.(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) :
"null").
',';
537 $sql .=
' '.price2num($this->pa_ht).
',';
538 $sql .=
" '".$this->db->escape((
string) $this->info_bits).
"',";
539 $sql .=
" ".price2num($this->total_ht).
",";
540 $sql .=
" ".price2num($this->total_tva).
",";
541 $sql .=
" ".price2num($this->total_ttc).
",";
542 $sql .=
" ".price2num($this->total_localtax1).
",";
543 $sql .=
" ".price2num($this->total_localtax2);
544 $sql .=
", ".((float) $this->situation_percent);
545 $sql .=
", ".(!empty($this->fk_prev_id) ? ((int) $this->fk_prev_id) :
"null");
546 $sql .=
", ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
547 $sql .=
", ".((int) $user->id);
548 $sql .=
", ".((int) $user->id);
549 $sql .=
", ".(int) $this->fk_multicurrency;
550 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
551 $sql .=
", ".price2num($this->multicurrency_subprice);
552 $sql .=
", ".price2num($this->multicurrency_total_ht);
553 $sql .=
", ".price2num($this->multicurrency_total_tva);
554 $sql .=
", ".price2num($this->multicurrency_total_ttc);
555 $sql .=
", '".$this->db->escape($this->batch).
"'";
556 $sql .=
", ".((int) $this->fk_warehouse);
559 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
560 $resql = $this->db->query($sql);
562 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
563 $this->rowid = $this->id;
574 if ($this->fk_remise_except && empty($error)) {
576 $result = $discount->fetch($this->fk_remise_except);
581 if ($discount->fk_facture_line > 0) {
582 if (empty($noerrorifdiscountalreadylinked)) {
583 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
584 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
585 $this->db->rollback();
589 $result = $discount->link_to_invoice($this->rowid, 0);
591 $this->error = $discount->error;
592 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
593 $this->db->rollback();
598 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
599 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
600 $this->db->rollback();
604 $this->error = $discount->error;
605 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
606 $this->db->rollback();
611 if (!$notrigger && empty($error)) {
613 $result = $this->call_trigger(
'LINEBILL_INSERT', $user);
615 $this->db->rollback();
626 foreach ($this->errors as $errmsg) {
627 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
628 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
630 $this->db->rollback();
633 $this->error = $this->db->lasterror();
634 $this->db->rollback();
646 public function update($user =
null, $notrigger = 0)
652 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
655 $this->desc = trim($this->desc);
656 if (empty($this->ref_ext)) {
659 if (empty($this->subprice_ttc)) {
660 $this->subprice_ttc = 0;
662 if (empty($this->tva_tx)) {
665 if (empty($this->localtax1_tx)) {
666 $this->localtax1_tx = 0;
668 if (empty($this->localtax2_tx)) {
669 $this->localtax2_tx = 0;
671 if (empty($this->localtax1_type)) {
672 $this->localtax1_type = 0;
674 if (empty($this->localtax2_type)) {
675 $this->localtax2_type = 0;
677 if (empty($this->total_localtax1)) {
678 $this->total_localtax1 = 0;
680 if (empty($this->total_localtax2)) {
681 $this->total_localtax2 = 0;
683 if (empty($this->remise_percent)) {
684 $this->remise_percent = 0;
686 if (empty($this->info_bits)) {
687 $this->info_bits = 0;
689 if (empty($this->special_code)) {
690 $this->special_code = 0;
692 if (empty($this->product_type)) {
693 $this->product_type = 0;
695 if (empty($this->fk_parent_line)) {
696 $this->fk_parent_line = 0;
698 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
699 $this->situation_percent = 100;
701 if (empty($this->pa_ht)) {
705 if (empty($this->multicurrency_subprice)) {
706 $this->multicurrency_subprice = 0;
708 if (empty($this->multicurrency_total_ht)) {
709 $this->multicurrency_total_ht = 0;
711 if (empty($this->multicurrency_total_tva)) {
712 $this->multicurrency_total_tva = 0;
714 if (empty($this->multicurrency_total_ttc)) {
715 $this->multicurrency_total_ttc = 0;
719 if ($this->product_type < 0) {
724 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
726 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
730 $this->pa_ht = $result;
737 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
738 $sql .=
" description='".$this->db->escape($this->desc).
"'";
739 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
740 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
741 $sql .=
", subprice=".price2num($this->subprice);
742 $sql .=
", subprice_ttc=".price2num($this->subprice_ttc);
743 $sql .=
", remise_percent=".price2num($this->remise_percent);
744 if ($this->fk_remise_except) {
745 $sql .=
", fk_remise_except = ".((int) $this->fk_remise_except);
747 $sql .=
", fk_remise_except = null";
749 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
750 $sql .=
", tva_tx=".price2num($this->tva_tx);
751 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
752 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
753 $sql .=
", localtax1_type='".$this->db->escape((
string) $this->localtax1_type).
"'";
754 $sql .=
", localtax2_type='".$this->db->escape((
string) $this->localtax2_type).
"'";
755 $sql .=
", qty = ".((float)
price2num($this->qty));
756 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
757 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
758 $sql .=
", product_type = ".((int) $this->product_type);
759 $sql .=
", info_bits = '".$this->db->escape((
string) $this->info_bits).
"'";
760 $sql .=
", special_code = " . (int) $this->special_code;
761 if (empty($this->skip_update_total)) {
762 $sql .=
", total_ht = ".price2num($this->total_ht);
763 $sql .=
", total_tva = ".price2num($this->total_tva);
764 $sql .=
", total_ttc = ".price2num($this->total_ttc);
765 $sql .=
", total_localtax1 = ".price2num($this->total_localtax1);
766 $sql .=
", total_localtax2 = ".price2num($this->total_localtax2);
768 $sql .=
", fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape((
string) $this->fk_fournprice).
"'" :
"null");
769 $sql .=
", buy_price_ht = ".(($this->pa_ht || (
string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
770 $sql .=
", fk_parent_line = ".($this->fk_parent_line > 0 ? ((int) $this->fk_parent_line) :
"null");
771 if (!empty($this->rang)) {
772 $sql .=
", rang = ".((int) $this->rang);
774 $sql .=
", situation_percent = ".((float) $this->situation_percent);
775 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
776 $sql .=
", fk_user_modif = ".((int) $user->id);
779 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
780 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
781 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
782 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
784 $sql .=
", batch = '".$this->db->escape($this->batch).
"'";
785 $sql .=
", fk_warehouse = ".((int) $this->fk_warehouse);
787 $sql .=
" WHERE rowid = ".((int) $this->rowid);
789 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
790 $resql = $this->db->query($sql);
793 $this->
id = $this->rowid;
800 if (!$error && !$notrigger) {
802 $result = $this->call_trigger(
'LINEBILL_MODIFY', $user);
804 $this->db->rollback();
815 foreach ($this->errors as $errmsg) {
816 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
817 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
820 $this->db->rollback();
823 $this->error = $this->db->error();
824 $this->db->rollback();
952 global $invoicecache;
954 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
958 if (!isset($invoicecache[$invoiceid])) {
959 $invoicecache[$invoiceid] =
new Facture($this->db);
960 $invoicecache[$invoiceid]->fetch($invoiceid);
962 if (empty($invoicecache[$invoiceid]->situation_cycle_ref)) {
966 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet";
967 $sql .=
" WHERE rowid = ".((int) $this->fk_prev_id);
969 $resql = $this->db->query($sql);
971 if ($resql && $this->db->num_rows($resql) > 0) {
974 $obj = $this->db->fetch_object($resql);
976 $returnPercent = (float) $obj->situation_percent;
979 if ($include_credit_note) {
980 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
981 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
982 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
983 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
986 $res = $this->db->query($sql);
988 while ($obj = $this->db->fetch_object($res)) {
989 $returnPercent += (float) $obj->situation_percent;
996 return $returnPercent;
998 $this->error = $this->db->error();
999 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
1000 $this->db->rollback();
1019 global $invoicecache;
1021 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
1025 if (!isset($invoicecache[$invoiceid])) {
1026 $invoicecache[$invoiceid] =
new Facture($this->db);
1027 $invoicecache[$invoiceid]->fetch($invoiceid);
1029 if (!$invoicecache[$invoiceid]->isSituationInvoice()) {
1034 $lastprevid = $this->fk_prev_id;
1035 $cumulated_percent = 0.0;
1037 while (!$all_found) {
1038 $sql =
"SELECT situation_percent, fk_prev_id FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $lastprevid);
1039 $resql = $this->db->query($sql);
1041 if ($resql && $this->db->num_rows($resql) > 0) {
1042 $obj = $this->db->fetch_object($resql);
1043 $cumulated_percent += (float) $obj->situation_percent;
1045 if ($include_credit_note) {
1046 $sql_credit_note =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
1047 $sql_credit_note .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
1048 $sql_credit_note .=
" WHERE fd.fk_prev_id = ".((int) $lastprevid);
1049 $sql_credit_note .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
1050 $sql_credit_note .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
1052 $res_credit_note = $this->db->query($sql_credit_note);
1053 if ($res_credit_note) {
1054 while ($cn = $this->db->fetch_object($res_credit_note)) {
1055 $cumulated_percent += (float) $cn->situation_percent;
1063 if ($obj->fk_prev_id) {
1064 $lastprevid = $obj->fk_prev_id;
1069 $this->error = $this->db->error();
1070 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
1071 $this->db->rollback();
1075 return $cumulated_percent;