From 48f2f469d30157dfc3c36e5ce2385212adcf0db1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 14:11:24 +1100 Subject: [PATCH] refactor for invoice deletion --- app/Services/Invoice/MarkInvoiceDeleted.php | 52 ++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index e7b55b669928..489c0f2566fd 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -23,6 +23,8 @@ class MarkInvoiceDeleted extends AbstractService private $adjustment_amount = 0; + private $total_payments = 0; + public function __construct(Invoice $invoice) { $this->invoice = $invoice; @@ -38,9 +40,54 @@ class MarkInvoiceDeleted extends AbstractService $this->cleanup() ->setAdjustmentAmount() - ->deletePaymentables(); + ->deletePaymentables() + ->adjustPayments() + ->adjustPaidToDate() + ->adjustLedger(); } + private function adjustLedger() + { + $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1); + + return $this; + } + + private function adjustPaidToDate() + { + $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); + + return $this; + } + + private function adjustPayments() + { + //if total payments = adjustment amount - that means we need to delete the payments as well. + + if($this->adjustment_amount == $this->total_payments) { + + $this->invoice->payments()->update(['deleted_at'=> now(), 'is_deleted' => true]); + + } + else { + //adjust payments down by the amount applied to the invoice payment. + + $this->invoice->payments->each(function ($payment){ + + $payment_adjustment = $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('amount')); + + $payment->amount -= $payment_adjustment; + $payment->applied -= $payment_adjustment; + $payment->save(); + + }); + } + + return $this; + } private function setAdjustmentAmount() { @@ -52,6 +99,9 @@ class MarkInvoiceDeleted extends AbstractService ->sum(DB::raw('amount')); } + + $this->total_payments = $this->invoice->payments->sum('amount'); + return $this; }