diff --git a/app/Http/Requests/Payment/UpdatePaymentRequest.php b/app/Http/Requests/Payment/UpdatePaymentRequest.php index 8f24eb3f6518..1d87ef164542 100644 --- a/app/Http/Requests/Payment/UpdatePaymentRequest.php +++ b/app/Http/Requests/Payment/UpdatePaymentRequest.php @@ -36,7 +36,7 @@ class UpdatePaymentRequest extends Request public function rules() { $rules = [ - 'invoices' => ['array', new PaymentAppliedValidAmount, new ValidCreditsPresentRule($this->all())], + 'invoices' => ['array', new PaymentAppliedValidAmount($this->all()), new ValidCreditsPresentRule($this->all())], 'invoices.*.invoice_id' => 'distinct', ]; @@ -87,6 +87,7 @@ class UpdatePaymentRequest extends Request } } } + $this->replace($input); } diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index dcb0c6d11b26..24c5dbe09e47 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -25,6 +25,10 @@ class PaymentAppliedValidAmount implements Rule private $message; + public function __construct(private array $input) + { + $this->input = $input; + } /** * @param string $attribute * @param mixed $value @@ -48,8 +52,8 @@ class PaymentAppliedValidAmount implements Rule private function calculateAmounts() :bool { $payment = Payment::withTrashed()->whereId($this->decodePrimaryKey(request()->segment(4)))->company()->first(); - $inv_collection = Invoice::withTrashed()->whereIn('id', array_column(request()->input('invoices'), 'invoice_id'))->get(); - + $inv_collection = Invoice::withTrashed()->whereIn('id', array_column($this->input['invoices'], 'invoice_id'))->get(); + if (! $payment) { return false; } @@ -74,15 +78,15 @@ class PaymentAppliedValidAmount implements Rule } } - if (request()->input('invoices') && is_array(request()->input('invoices'))) { - foreach (request()->input('invoices') as $invoice) { + if (isset($this->input['invoices']) && is_array($this->input['invoices'])) { + foreach ($this->input['invoices'] as $invoice) { $invoice_amounts += $invoice['amount']; $inv = $inv_collection->firstWhere('id', $invoice['invoice_id']); + nlog($inv); if($inv->balance < $invoice['amount']) { - // $this->message = ctrans('texts.insufficient_applied_amount_remaining'); $this->message = 'Amount cannot be greater than invoice balance'; return false;