46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Rules\Sigtap;
|
|
|
|
use App\Models\Sigtap\Procedimento;
|
|
use App\Utils\MaskUtil;
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class QuantidadeIsValidForProcedimento implements Rule
|
|
{
|
|
private $procedimentoId;
|
|
|
|
public function __construct($procedimentoId)
|
|
{
|
|
$this->procedimentoId = $procedimentoId;
|
|
}
|
|
|
|
public function passes($attribute, $value)
|
|
{
|
|
return self::isValid($this->procedimentoId, $value);
|
|
}
|
|
|
|
public static function isValid($procedimentoId, $value)
|
|
{
|
|
$procedimento = DB::table('sigtap.procedimento')
|
|
->where('id', $procedimentoId)
|
|
->select('quantidade_maxima_execucao')
|
|
->first();
|
|
|
|
return $value >= 0
|
|
&& $value <= $procedimento->quantidade_maxima_execucao;
|
|
}
|
|
|
|
public function message()
|
|
{
|
|
$procedimento = Procedimento::where('id', $this->procedimentoId)
|
|
->first(['codigo', 'nome', 'quantidade_maxima_execucao']);
|
|
|
|
$descricao = MaskUtil::getProcedimentoMasked($procedimento->codigo) . " - {$procedimento->nome}";
|
|
|
|
return "A quantidade máxima permitida para o procedimento {$descricao} é "
|
|
. $procedimento->quantidade_maxima_execucao;
|
|
}
|
|
}
|