diff --git a/app/Http/ValidationRules/Payment/ValidInvoicesRules.php b/app/Http/ValidationRules/Payment/ValidInvoicesRules.php index bcb231f94e1f..2880af0fa525 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 952c5038f31c..33d9e77b48f7 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);