Save result of some methods into cached properties.

This commit is contained in:
Laurent Destailleur 2022-04-07 19:16:50 +02:00
parent baa4608812
commit 0e36097eba
2 changed files with 33 additions and 10 deletions

View File

@ -93,6 +93,14 @@ abstract class CommonInvoice extends CommonObject
const STATUS_ABANDONED = 3; const STATUS_ABANDONED = 3;
public $sumpayed;
public $sumpayed_multicurrency;
public $sumdeposit;
public $sumdeposit_multicurrency;
public $sumcreditnote;
public $sumcreditnote_multicurrency;
/** /**
* Return remain amount to pay. Property ->id and ->total_ttc must be set. * Return remain amount to pay. Property ->id and ->total_ttc must be set.
* This does not include open direct debit requests. * This does not include open direct debit requests.
@ -119,7 +127,7 @@ abstract class CommonInvoice extends CommonObject
* Payments dones using discounts, credit notes, etc are not included. * Payments dones using discounts, credit notes, etc are not included.
* *
* @param int $multicurrency Return multicurrency_amount instead of amount * @param int $multicurrency Return multicurrency_amount instead of amount
* @return float Amount of payment already done, <0 if KO * @return float Amount of payment already done, <0 and set ->error if KO
*/ */
public function getSommePaiement($multicurrency = 0) public function getSommePaiement($multicurrency = 0)
{ {
@ -138,10 +146,13 @@ abstract class CommonInvoice extends CommonObject
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$this->db->free($resql); $this->db->free($resql);
if ($multicurrency) { if ($multicurrency) {
$this->sumpayed_multicurrency = $obj->multicurrency_amount;
return $obj->multicurrency_amount; return $obj->multicurrency_amount;
} else { } else {
$this->sumpayed = $obj->amount;
return $obj->amount; return $obj->amount;
} }
} else { } else {
@ -155,12 +166,12 @@ abstract class CommonInvoice extends CommonObject
* Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended). * Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended).
* *
* @param int $multicurrency Return multicurrency_amount instead of amount * @param int $multicurrency Return multicurrency_amount instead of amount
* @return float <0 if KO, Sum of deposits amount otherwise * @return float <0 and set ->error if KO, Sum of deposits amount otherwise
*/ */
public function getSumDepositsUsed($multicurrency = 0) public function getSumDepositsUsed($multicurrency = 0)
{ {
if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') { if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') {
// TODO // FACTURE_DEPOSITS_ARE_JUST_PAYMENTS was never supported for purchase invoice, so we can return 0 with no need of SQL for this case.
return 0.0; return 0.0;
} }
@ -170,6 +181,12 @@ abstract class CommonInvoice extends CommonObject
$result = $discountstatic->getSumDepositsUsed($this, $multicurrency); $result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
if ($result >= 0) { if ($result >= 0) {
if ($multicurrency) {
$this->sumdeposit_multicurrency = $result;
} else {
$this->sumdeposit = $result;
}
return $result; return $result;
} else { } else {
$this->error = $discountstatic->error; $this->error = $discountstatic->error;
@ -181,7 +198,7 @@ abstract class CommonInvoice extends CommonObject
* Return amount (with tax) of all credit notes invoices + excess received used by invoice * Return amount (with tax) of all credit notes invoices + excess received used by invoice
* *
* @param int $multicurrency Return multicurrency_amount instead of amount * @param int $multicurrency Return multicurrency_amount instead of amount
* @return float <0 if KO, Sum of credit notes and deposits amount otherwise * @return float <0 and set ->error if KO, Sum of credit notes and deposits amount otherwise
*/ */
public function getSumCreditNotesUsed($multicurrency = 0) public function getSumCreditNotesUsed($multicurrency = 0)
{ {
@ -190,6 +207,12 @@ abstract class CommonInvoice extends CommonObject
$discountstatic = new DiscountAbsolute($this->db); $discountstatic = new DiscountAbsolute($this->db);
$result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency); $result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency);
if ($result >= 0) { if ($result >= 0) {
if ($multicurrency) {
$this->sumcreditnote_multicurrency = $result;
} else {
$this->sumcreditnote = $result;
}
return $result; return $result;
} else { } else {
$this->error = $discountstatic->error; $this->error = $discountstatic->error;

View File

@ -562,7 +562,7 @@ class DiscountAbsolute
* Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended). * Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended).
* *
* @param CommonInvoice $invoice Object invoice (customer of supplier) * @param CommonInvoice $invoice Object invoice (customer of supplier)
* @param int $multicurrency 1=Return multicurrency_amount instead of amount * @param int $multicurrency 1=Return multicurrency_amount instead of amount. TODO Add a mode multicurrency = -1 to return array with amount + multicurrency amount
* @return int <0 if KO, Sum of credit notes and deposits amount otherwise * @return int <0 if KO, Sum of credit notes and deposits amount otherwise
*/ */
public function getSumDepositsUsed($invoice, $multicurrency = 0) public function getSumDepositsUsed($invoice, $multicurrency = 0)
@ -603,7 +603,7 @@ class DiscountAbsolute
* Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment * Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment
* *
* @param CommonInvoice $invoice Object invoice * @param CommonInvoice $invoice Object invoice
* @param int $multicurrency 1=Return multicurrency_amount instead of amount * @param int $multicurrency 1=Return multicurrency_amount instead of amount. TODO Add a mode multicurrency = -1 to return array with amount + multicurrency amount
* @return int <0 if KO, Sum of credit notes and excess received amount otherwise * @return int <0 if KO, Sum of credit notes and excess received amount otherwise
*/ */
public function getSumCreditNotesUsed($invoice, $multicurrency = 0) public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
@ -643,7 +643,7 @@ class DiscountAbsolute
* Return amount (with tax) of all converted amount for this credit note * Return amount (with tax) of all converted amount for this credit note
* *
* @param CommonInvoice $invoice Object invoice * @param CommonInvoice $invoice Object invoice
* @param int $multicurrency Return multicurrency_amount instead of amount * @param int $multicurrency Return multicurrency_amount instead of amount. TODO Add a mode multicurrency = -1 to return array with amount + multicurrency amount
* @return int <0 if KO, Sum of credit notes and deposits amount otherwise * @return int <0 if KO, Sum of credit notes and deposits amount otherwise
*/ */
public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0) public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)