2020-01-09 21:15:10 +01:00
|
|
|
<?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;
|
2020-02-01 21:45:23 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-01-09 21:15:10 +01:00
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ValidCreditsPresentRule
|
|
|
|
* @package App\Http\ValidationRules
|
|
|
|
*/
|
|
|
|
class ValidCreditsPresentRule implements Rule
|
|
|
|
{
|
2020-02-01 21:45:23 +01:00
|
|
|
use MakesHash;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this->validCreditsPresent();
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
2020-02-01 21:45:23 +01:00
|
|
|
return 'Insufficient balance on credit.';
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function validCreditsPresent() :bool
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
//todo need to ensure the clients credits are here not random ones!
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->input('credits') && is_array(request()->input('credits'))) {
|
|
|
|
foreach (request()->input('credits') as $credit) {
|
2020-02-01 21:45:23 +01:00
|
|
|
$cred = Credit::find($this->decodePrimaryKey($credit['credit_id']));
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
if (!$cred || $cred->balance == 0) {
|
2020-03-21 06:37:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|