diff --git a/app/Http/ValidationRules/Credit/ValidCreditsRules.php b/app/Http/ValidationRules/Credit/ValidCreditsRules.php index 527b5a2f310b..507d0262fc20 100644 --- a/app/Http/ValidationRules/Credit/ValidCreditsRules.php +++ b/app/Http/ValidationRules/Credit/ValidCreditsRules.php @@ -83,6 +83,24 @@ class ValidCreditsRules implements Rule return false; } + if(count($this->input['credits']) >=1){ + + $total_payments = $this->input['amount'] + array_sum(array_column($this->input['credits'], 'amount')); + +info(print_r($this->input,1)); +info("total payments = {$total_payments}"); +info("total credits available = " . array_sum(array_column($this->input['credits'], 'amount'))); +info("total invoices payable = " . array_sum(array_column($this->input['invoices'], 'amount'))); + + if($total_payments > array_sum(array_column($this->input['invoices'], 'amount'))){ + + $this->error_msg = "Sum of total payments and credits is greater than the total of invoices"; + return false; + } + + } + + return true; } diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index e9d5c38aaa68..e15861e5e1d7 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -82,13 +82,15 @@ class PaymentRepository extends BaseRepository $data['amount'] = array_sum(array_column($data['invoices'], 'amount')); $client = Client::find($data['client_id']); - info("updating client balance from {$client->balance} by this much ".$data['amount']); + //info("updating client balance from {$client->balance} by this much ".$data['amount']); $client->service()->updatePaidToDate($data['amount'])->save(); } } + //info(print_r($data,1)); + /*Fill the payment*/ $payment->fill($data); $payment->status_id = Payment::STATUS_COMPLETED;