1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/ValidationRules/ValidCreditsPresentRule.php
David Bomba 0878decf18
Implement payment rules at application edge (FormRequest) (#3202)
* Ensure payments, invoice and credit amount balance in the validator prior to saving

* additional payment validation rules and tests for processing payments

* Factories for credits

* Tests for payments

* Working on updating a payment

* Working on updating a payment

* fixes for updating a payment

* Working on Payment Tests

* More tests for payments, formrequests

* remove product_key as required from products
2020-01-10 07:15:10 +11:00

66 lines
1.4 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\ValidationRules;
use App\Libraries\MultiDB;
use App\Models\Credit;
use App\Models\User;
use Illuminate\Contracts\Validation\Rule;
/**
* Class ValidCreditsPresentRule
* @package App\Http\ValidationRules
*/
class ValidCreditsPresentRule implements Rule
{
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return $this->validCreditsPresent();
}
/**
* @return string
*/
public function message()
{
return 'Attempting to use one or more invalid credits';
}
private function validCreditsPresent() :bool
{
$data = [];
//todo need to ensure the clients credits are here not random ones!
if(request()->input('credits') && is_array(request()->input('credits')))
{
foreach(request()->input('credits') as $credit)
{
$cred = Credit::find($credit['id']);
if($cred->balance >= $credit['amount'])
return false;
}
}
return true;
}
}