From a0d1635a58f703fd9bedfa183e75741bbcc73fc9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 6 Sep 2022 19:18:05 +1000 Subject: [PATCH] Fixes for handling partial payments with credits --- app/Http/Controllers/BaseController.php | 3 +- app/Services/Invoice/AutoBillInvoice.php | 42 +++++++++++-------- app/Services/Payment/UpdateInvoicePayment.php | 14 ++++--- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index b85683a4a8..4728fdd5a3 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -901,9 +901,10 @@ class BaseController extends Controller return redirect('/')->with(['signup' => 'true']); } + // 06-09-2022 - parse the path if loaded in a subdirectory for canvaskit resolution $canvas_path_array = parse_url(config('ninja.app_url')); - $canvas_path = (array_key_exists('path', $canvas_path_array)) ? $canvas_path_array['path'] : ''; + $canvas_path = rtrim(str_replace("index.php", "", $canvas_path),'/'); $data = []; diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 7c8a2da6f7..7b66d1535b 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -35,6 +35,9 @@ class AutoBillInvoice extends AbstractService protected $db; + /*Specific variable for partial payments */ + private bool $is_partial_amount = false; + public function __construct(Invoice $invoice, $db) { $this->invoice = $invoice; @@ -46,7 +49,8 @@ class AutoBillInvoice extends AbstractService { MultiDB::setDb($this->db); - $this->client = $this->invoice->client->fresh(); + /* Harvest Client*/ + $this->client = $this->invoice->client; $is_partial = false; @@ -68,6 +72,10 @@ class AutoBillInvoice extends AbstractService $this->applyCreditPayment(); } + //If this returns true, it means a partial invoice amount was paid as a credit and there is no further balance payable + if($this->is_partial_amount && $this->invoice->partial == 0) + return; + $amount = 0; /* Determine $amount */ @@ -169,9 +177,9 @@ class AutoBillInvoice extends AbstractService $payment->invoices()->attach($this->invoice->id, ['amount' => $amount]); $this->invoice - ->service() - ->setStatus(Invoice::STATUS_PAID) - ->save(); + ->service() + ->setCalculatedStatus() + ->save(); foreach ($this->used_credit as $credit) { $current_credit = Credit::find($credit['credit_id']); @@ -191,18 +199,18 @@ class AutoBillInvoice extends AbstractService ->updatePaymentBalance($amount * -1) ->save(); - $client = $this->invoice->client->fresh(); - - $client->service() - ->updateBalance($amount * -1) - ->updatePaidToDate($amount) - ->adjustCreditBalance($amount * -1) - ->save(); + $this->invoice + ->client + ->service() + ->updateBalanceAndPaidToDate($amount * -1, $amount) + // ->updateBalance($amount * -1) + // ->updatePaidToDate($amount) + ->adjustCreditBalance($amount * -1) + ->save(); $this->invoice->ledger() //09-03-2022 - // ->updateInvoiceBalance($amount * -1, "Invoice {$this->invoice->number} payment using Credit {$current_credit->number}") - ->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}") - ->save(); + ->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}") + ->save(); event('eloquent.created: App\Models\Payment', $payment); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); @@ -235,16 +243,14 @@ class AutoBillInvoice extends AbstractService return; } - $is_partial_amount = false; - if ($this->invoice->partial > 0) { - $is_partial_amount = true; + $this->is_partial_amount = true; } $this->used_credit = []; foreach ($available_credits as $key => $credit) { - if ($is_partial_amount) { + if ($this->is_partial_amount) { //more credit than needed if ($credit->balance > $this->invoice->partial) { diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index d073087301..5c170e93f5 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -62,15 +62,17 @@ class UpdateInvoicePayment $paid_amount = $paid_invoice->amount; } - \DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){ + $client->service()->updateBalanceAndPaidToDate($paid_amount*-1, $paid_amount); + + // \DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){ - $update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first(); + // $update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first(); - $update_client->paid_to_date += $paid_amount; - $update_client->balance -= $paid_amount; - $update_client->save(); + // $update_client->paid_to_date += $paid_amount; + // $update_client->balance -= $paid_amount; + // $update_client->save(); - }, 1); + // }, 1); /* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */ if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)