Add rules preventing overpayment on invoices

This commit is contained in:
David Bomba 2021-01-21 10:53:02 +11:00
parent 79723759eb
commit db9e8ff830
2 changed files with 19 additions and 5 deletions

View File

@ -53,21 +53,40 @@ class ValidInvoicesRules implements Rule
//todo optimize this into a single query //todo optimize this into a single query
foreach ($this->input['invoices'] as $invoice) { foreach ($this->input['invoices'] as $invoice) {
$unique_array[] = $invoice['invoice_id']; $unique_array[] = $invoice['invoice_id'];
$inv = Invoice::whereId($invoice['invoice_id'])->first(); $inv = Invoice::whereId($invoice['invoice_id'])->first();
if (! $inv) { if (! $inv) {
$this->error_msg = 'Invoice not found '; $this->error_msg = 'Invoice not found ';
return false; return false;
} }
if ($inv->client_id != $this->input['client_id']) { if ($inv->client_id != $this->input['client_id']) {
$this->error_msg = 'Selected invoices are not from a single client'; $this->error_msg = 'Selected invoices are not from a single client';
return false; 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)) { if (! (array_unique($unique_array) == $unique_array)) {

View File

@ -100,11 +100,6 @@ class ApplyPayment extends AbstractService
} }
} }
// $this->payment
// ->ledger()
// ->updatePaymentBalance($this->payment_amount * -1);
$this->payment $this->payment
->ledger() ->ledger()
->updatePaymentBalance($amount_paid); ->updatePaymentBalance($amount_paid);