From 26ef7c7c0de41785499f6edd917edc8d1773f619 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 9 Mar 2022 15:45:13 +1100 Subject: [PATCH] Refactor to use increments --- app/Services/Credit/CreditService.php | 11 +++++++---- app/Services/Invoice/UpdateBalance.php | 13 ++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index 321399d25dcf..600684c4b403 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -167,21 +167,24 @@ class CreditService public function adjustBalance($adjustment) { - $this->credit->balance += $adjustment; + // $this->credit->balance += $adjustment; + $this->credit->increment('balance', $adjustment); return $this; } public function updatePaidToDate($adjustment) { - $this->credit->paid_to_date += $adjustment; - + // $this->credit->paid_to_date += $adjustment; + $this->credit->increment('paid_to_date', $adjustment); + return $this; } public function updateBalance($adjustment) { - $this->credit->balance -= $adjustment; + // $this->credit->balance -= $adjustment; + $this->credit->decrement('balance', $adjustment); return $this; } diff --git a/app/Services/Invoice/UpdateBalance.php b/app/Services/Invoice/UpdateBalance.php index 87864fd61342..51d023659569 100644 --- a/app/Services/Invoice/UpdateBalance.php +++ b/app/Services/Invoice/UpdateBalance.php @@ -34,17 +34,20 @@ class UpdateBalance extends AbstractService if ($this->invoice->is_deleted) { return $this->invoice; } -nlog("invoice id = {$this->invoice->id}"); -nlog("invoice balance = {$this->invoice->balance}"); -nlog("invoice adjustment = {$this->balance_adjustment}"); + + nlog("invoice id = {$this->invoice->id}"); + nlog("invoice balance = {$this->invoice->balance}"); + nlog("invoice adjustment = {$this->balance_adjustment}"); - $this->invoice->balance += floatval($this->balance_adjustment); + // $this->invoice->balance += floatval($this->balance_adjustment); + $this->invoice->increment('balance', floatval($this->balance_adjustment)); + if ($this->invoice->balance == 0 && !$this->is_draft) { $this->invoice->status_id = Invoice::STATUS_PAID; } -nlog("final balance = {$this->invoice->balance}"); + nlog("final balance = {$this->invoice->balance}"); return $this->invoice; }