63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Rules\Sigtap;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProcedimentoIsValidForCid implements Rule
|
|
{
|
|
private $cidId;
|
|
|
|
public function __construct($cidId)
|
|
{
|
|
$this->cidId = $cidId;
|
|
}
|
|
|
|
public function passes($attribute, $value)
|
|
{
|
|
return self::isValid($this->cidId, $value);
|
|
}
|
|
|
|
public static function isValid($cidId, $value)
|
|
{
|
|
$procedimentoRequerCid = DB::table('sigtap.procedimento')->where('id', $value)
|
|
->whereExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_cid')
|
|
->whereColumn('sigtap.procedimento_cid.procedimento_id', 'sigtap.procedimento.id')
|
|
->where('principal', 'S');
|
|
})->exists();
|
|
|
|
if($procedimentoRequerCid){
|
|
|
|
$cidValidoParaProceidmento = DB::table('sigtap.procedimento')->where('id', $value)
|
|
->whereExists(function ($query) use ($cidId) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_cid')
|
|
->whereColumn('sigtap.procedimento_cid.procedimento_id', 'sigtap.procedimento.id')
|
|
->where('sigtap.procedimento_cid.cid_id', $cidId);
|
|
})
|
|
->exists();
|
|
|
|
if($cidValidoParaProceidmento){
|
|
|
|
return true;
|
|
}else{
|
|
|
|
return false;
|
|
}
|
|
}else{
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public function message()
|
|
{
|
|
return 'Procedimento inválido para o CID informado.';
|
|
}
|
|
}
|