From 19804f0574dd99e9e884300236f0c0a33e9c02b3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 13 May 2025 17:08:06 +1000 Subject: [PATCH] Adjustments for prepayment with underpayments --- app/Services/ClientPortal/InstantPayment.php | 1 - app/Services/Payment/UpdateInvoicePayment.php | 25 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Services/ClientPortal/InstantPayment.php b/app/Services/ClientPortal/InstantPayment.php index 02dfc5aec5..7d88a2e7d4 100644 --- a/app/Services/ClientPortal/InstantPayment.php +++ b/app/Services/ClientPortal/InstantPayment.php @@ -116,7 +116,6 @@ class InstantPayment $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), $client->currency()->precision); $invoice_balance = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), $client->currency()->precision); - /*If we don't allow under/over payments force the payable amount - prevents inspect element adjustments in JS*/ if ($settings->client_portal_allow_under_payment == false && $settings->client_portal_allow_over_payment == false) { diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 5b6a3ba2a6..5631727b9b 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -97,6 +97,26 @@ class UpdateInvoicePayment if ($invoice->is_proforma) { //keep proforma's hidden if (property_exists($this->payment_hash->data, 'pre_payment') && $this->payment_hash->data->pre_payment == "1") { + + if($invoice->balance != 0){ + $invoice->client->service()->updateBalance($invoice->balance*-1); + $invoice->balance = 0; + $invoice->status_id = \App\Models\Invoice::STATUS_PAID; + $invoice->saveQuietly(); + + $invoice + ->ledger() + ->updateInvoiceBalance(($invoice->balance + $paid_amount) * -1, "Prepayment Balance Adjustment"); + + } + else { + + $invoice + ->ledger() + ->updateInvoiceBalance($paid_amount * -1, "Prepayment Balance Adjustment"); + + } + $invoice->payments()->each(function ($p) { $p->pivot->forceDelete(); $p->invoices()->each(function ($i) { @@ -104,11 +124,6 @@ class UpdateInvoicePayment }); }); - - $invoice - ->ledger() - ->updateInvoiceBalance($paid_amount * -1, "Prepayment Balance Adjustment"); - $invoice->is_deleted = true; $invoice->deleted_at = now(); $invoice->saveQuietly();