From 16a1c65354aa4132fc94a82c6c63fa7c61663215 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 22 Aug 2022 15:05:02 +1000 Subject: [PATCH] valid credits rules --- app/Http/Requests/Payment/StorePaymentRequest.php | 5 +---- app/Http/Requests/Payment/UpdatePaymentRequest.php | 10 +++++++++- .../ValidationRules/ValidCreditsPresentRule.php | 14 ++++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 7fc64f69c1..94b873f461 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -76,9 +76,6 @@ class StorePaymentRequest extends Request } } - // if (array_key_exists('amount', $input)) - // $input['amount'] = 0; - if (isset($input['credits']) && is_array($input['credits']) === false) { $input['credits'] = null; } @@ -99,7 +96,7 @@ class StorePaymentRequest extends Request public function rules() { $rules = [ - 'amount' => ['numeric', 'bail', new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule()], + 'amount' => ['numeric', 'bail', new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule($this->all())], 'client_id' => 'bail|required|exists:clients,id', 'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id', 'invoices.*.amount' => 'bail|required', diff --git a/app/Http/Requests/Payment/UpdatePaymentRequest.php b/app/Http/Requests/Payment/UpdatePaymentRequest.php index 0071c2ec69..94cf3f859e 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], + 'invoices' => ['array', new PaymentAppliedValidAmount, new ValidCreditsPresentRule($this->all())], 'invoices.*.invoice_id' => 'distinct', 'documents' => 'mimes:png,ai,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx', ]; @@ -79,6 +79,14 @@ class UpdatePaymentRequest extends Request } } } + + if (isset($input['credits']) && is_array($input['credits']) !== false) { + foreach ($input['credits'] as $key => $value) { + if (array_key_exists('credits', $input['credits'][$key])) { + $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); + } + } + } $this->replace($input); } diff --git a/app/Http/ValidationRules/ValidCreditsPresentRule.php b/app/Http/ValidationRules/ValidCreditsPresentRule.php index 3cc5dab680..22bcdd7e0c 100644 --- a/app/Http/ValidationRules/ValidCreditsPresentRule.php +++ b/app/Http/ValidationRules/ValidCreditsPresentRule.php @@ -22,6 +22,13 @@ class ValidCreditsPresentRule implements Rule { use MakesHash; + private $input; + + public function __construct($input) + { + $this->input = $input; + } + /** * @param string $attribute * @param mixed $value @@ -44,11 +51,10 @@ class ValidCreditsPresentRule implements Rule { //todo need to ensure the clients credits are here not random ones! - if (request()->input('credits') && is_array(request()->input('credits')) && count(request()->input('credits')) > 0) { - $credit_collection = Credit::whereIn('id', $this->transformKeys(array_column(request()->input('credits'), 'credit_id'))) - ->count(); + if (array_key_exists('credits', $this->input) && is_array($this->input['credits']) && count($this->input['credits']) > 0) { + $credit_collection = Credit::whereIn('id', array_column($this->input['credits'], 'credit_id'))->count(); - return $credit_collection == count(request()->input('credits')); + return $credit_collection == count($this->input['credits']); } return true;