1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00

valid credits rules

This commit is contained in:
= 2022-08-22 15:05:02 +10:00
parent 2c8d25eeb3
commit 16a1c65354
3 changed files with 20 additions and 9 deletions

View File

@ -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',

View File

@ -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);
}

View File

@ -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;