From 66fd68cf9356f14a5d6ccfd5b063196a473b1690 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 17 Feb 2024 19:03:20 +1100 Subject: [PATCH] Refactor for payment processing --- app/Services/Invoice/AutoBillInvoice.php | 48 +++++++++--------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 1091d18748..dd0da025ab 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -22,6 +22,8 @@ use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; +use App\Repositories\CreditRepository; +use App\Repositories\PaymentRepository; use App\Services\AbstractService; use App\Utils\Ninja; use Illuminate\Support\Str; @@ -32,8 +34,6 @@ class AutoBillInvoice extends AbstractService private array $used_credit = []; - private array $used_unapplied = []; - /*Specific variable for partial payments */ private bool $is_partial_amount = false; @@ -168,11 +168,6 @@ class AutoBillInvoice extends AbstractService } } - private function finalizePaymentUsingUnapplied() - { - - } - /** * If the credits on file cover the invoice amount * the we create a matching payment using credits only @@ -228,8 +223,6 @@ class AutoBillInvoice extends AbstractService ->client ->service() ->updateBalanceAndPaidToDate($amount * -1, $amount) - // ->updateBalance($amount * -1) - // ->updatePaidToDate($amount) ->adjustCreditBalance($amount * -1) ->save(); @@ -286,45 +279,40 @@ class AutoBillInvoice extends AbstractService $this->is_partial_amount = true; } - $this->used_unapplied = []; + $payment_repo = new PaymentRepository(new CreditRepository()); + foreach ($unapplied_payments as $key => $payment) { $payment_balance = $payment->amount - $payment->applied; + if ($this->is_partial_amount) { //more than needed if ($payment_balance > $this->invoice->partial) { - $this->used_unapplied[$key]['payment_id'] = $payment->id; - $this->used_unapplied[$key]['amount'] = $this->invoice->partial; - $this->invoice->balance -= $this->invoice->partial; - $this->invoice->paid_to_date += $this->invoice->partial; - $this->invoice->partial = 0; + $payload = ['invoices' => [['invoice_id' => $this->invoice->id,'amount' => $this->invoice->partial]]]; + $payment_repo->save($payload, $payment); break; } else { - $this->used_unapplied[$key]['payment_id'] = $payment->id; - $this->used_unapplied[$key]['amount'] = $payment_balance; - $this->invoice->partial -= $payment_balance; - $this->invoice->balance -= $payment_balance; - $this->invoice->paid_to_date += $payment_balance; + $payload = ['invoices' => [['invoice_id' => $this->invoice->id,'amount' => $payment_balance]]]; + $payment_repo->save($payload, $payment); } } else { //more than needed if ($payment_balance > $this->invoice->balance) { - $this->used_unapplied[$key]['payment_id'] = $payment->id; - $this->used_unapplied[$key]['amount'] = $this->invoice->balance; - $this->invoice->paid_to_date += $this->invoice->balance; - $this->invoice->balance = 0; + + $payload = ['invoices' => [['invoice_id' => $this->invoice->id,'amount' => $this->invoice->balance]]]; + $payment_repo->save($payload, $payment); break; } else { - $this->used_unapplied[$key]['payment_id'] = $payment->id; - $this->used_unapplied[$key]['amount'] = $payment_balance; - $this->invoice->balance -= $payment_balance; - $this->invoice->paid_to_date += $payment_balance; + + $payload = ['invoices' => [['invoice_id' => $this->invoice->id,'amount' => $payment_balance]]]; + $payment_repo->save($payload, $payment); + } } - } - $this->finalizePaymentUsingUnapplied(); + $this->invoice = $this->invoice->fresh(); + } return $this; }