From db9e8ff830a606d51c6a55c94e3c2fc59cbfbb0f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 21 Jan 2021 10:53:02 +1100 Subject: [PATCH] Add rules preventing overpayment on invoices --- .../Payment/ValidInvoicesRules.php | 19 +++++++++++++++++++ app/Services/Invoice/ApplyPayment.php | 5 ----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Http/ValidationRules/Payment/ValidInvoicesRules.php b/app/Http/ValidationRules/Payment/ValidInvoicesRules.php index bcb231f94e..2880af0fa5 100644 --- a/app/Http/ValidationRules/Payment/ValidInvoicesRules.php +++ b/app/Http/ValidationRules/Payment/ValidInvoicesRules.php @@ -53,21 +53,40 @@ class ValidInvoicesRules implements Rule //todo optimize this into a single query foreach ($this->input['invoices'] as $invoice) { + $unique_array[] = $invoice['invoice_id']; $inv = Invoice::whereId($invoice['invoice_id'])->first(); if (! $inv) { + $this->error_msg = 'Invoice not found '; return false; } if ($inv->client_id != $this->input['client_id']) { + $this->error_msg = 'Selected invoices are not from a single client'; return false; } + + if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] == $inv->amount){ + //catch here nothing to do - we need this to prevent the last elseif triggering + } + else if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] > $inv->amount){ + + $this->error_msg = 'Amount cannot be greater than invoice balance'; + + return false; + } + else if($invoice['amount'] > $inv->balance) { + + $this->error_msg = 'Amount cannot be greater than invoice balance'; + + return false; + } } if (! (array_unique($unique_array) == $unique_array)) { diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index 952c5038f3..33d9e77b48 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -100,11 +100,6 @@ class ApplyPayment extends AbstractService } } - // $this->payment - // ->ledger() - // ->updatePaymentBalance($this->payment_amount * -1); - - $this->payment ->ledger() ->updatePaymentBalance($amount_paid);