249 lines
8.1 KiB
PHP
249 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\ProntoAtendimento\PedidoExame;
|
|
|
|
use App\Enums\Cadastro\Estabelecimento\SetorTipo;
|
|
use App\Enums\Parametrizacao\Exame\Status as ExameStatus;
|
|
use App\Enums\ProntoAtendimento\Atendimento\Desfecho;
|
|
use App\Enums\Sigtap\Ocupacao as OcupacaoEnum;
|
|
use App\Enums\Sigtap\Ocupacao;
|
|
use App\Http\Requests\FormRequest;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Parametrizacao\Exame\Exame;
|
|
use App\Models\ProntoAtendimento\Atendimento\Atendimento;
|
|
use App\Models\Sigtap\Procedimento;
|
|
use App\Rules\NomeCompleto;
|
|
use App\Rules\Sigtap\Procedimento\ValidCompetencia;
|
|
use App\Rules\Sigtap\Procedimento\Idade;
|
|
use App\Rules\Sigtap\Procedimento\Ocupacao as OcupacaoRule;
|
|
use App\Rules\Sigtap\Procedimento\Quantidade;
|
|
use App\Rules\Sigtap\Procedimento\Sexo as SexoRule;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PedidoExameRequest extends FormRequest
|
|
{
|
|
public function authorize()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
switch($this->method()) {
|
|
case 'POST':
|
|
return $this->getPostRules();
|
|
break;
|
|
|
|
default:
|
|
return [];
|
|
}
|
|
|
|
}
|
|
|
|
private function getPostRules() {
|
|
|
|
$atendimento = is_numeric($this->atendimento_id ?? null)
|
|
? Atendimento::find($this->atendimento_id)
|
|
: null;
|
|
|
|
return $this->getPostPedidoExamesRules($atendimento);
|
|
}
|
|
|
|
private function getPostPedidoExamesRules($atendimento)
|
|
{
|
|
$isEncaminhamento = $atendimento->recepcao->is_encaminhamento;
|
|
$isPacienteEmObservacao = $this->isPacienteEmObservacao($atendimento);
|
|
|
|
$rules = [
|
|
'plantao_id' => [
|
|
'required',
|
|
'integer'
|
|
],
|
|
'pedidos_exames' => [
|
|
'nullable',
|
|
'array',
|
|
],
|
|
'pedidos_exames.*.data_solicitacao' => [
|
|
'nullable',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'pedidos_exames.*.unidade_encaminhamento_id' => [
|
|
'nullable',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\Estabelecimento\Unidade,id',
|
|
],
|
|
'pedidos_exames.*.unidade_externa_encaminhamento_id' => [
|
|
'nullable',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\UnidadeExterna,id'
|
|
],
|
|
'pedidos_exames.*.medico_solicitante_encaminhamento' => [
|
|
'nullable',
|
|
'string',
|
|
'between:6,150',
|
|
new NomeCompleto,
|
|
],
|
|
'observacoes' => [
|
|
'nullable',
|
|
'string',
|
|
'max:3000',
|
|
],
|
|
'centro_custo_id_para' => [
|
|
Rule::requiredIf(fn () =>
|
|
!$this->hasFinalizacao() && !$isEncaminhamento && !$isPacienteEmObservacao
|
|
),
|
|
'nullable',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\Estabelecimento\CentroCusto,id',
|
|
],
|
|
];
|
|
|
|
foreach (($this->pedidos_exames ?? []) as $i => $pedidoExames) {
|
|
|
|
$rules = array_merge($rules, [
|
|
"pedidos_exames.{$i}.id" => [
|
|
'nullable',
|
|
'integer',
|
|
],
|
|
"pedidos_exames.{$i}.urgente" => [
|
|
'nullable',
|
|
"required_without:pedidos_exames.{$i}.id",
|
|
'boolean',
|
|
],
|
|
"pedidos_exames.{$i}.justificativa" => [
|
|
'nullable',
|
|
'string',
|
|
'max:100',
|
|
],
|
|
"pedidos_exames.{$i}.exames" => [
|
|
'required',
|
|
'array',
|
|
],
|
|
]);
|
|
|
|
foreach ($pedidoExames['exames'] as $j => $item) {
|
|
|
|
$rules = array_merge($rules, [
|
|
"pedidos_exames.{$i}.exames.{$j}.id" => [
|
|
'nullable',
|
|
"required_with:pedidos_exames.{$i}.id",
|
|
'integer',
|
|
'exists:\App\Models\ProntoAtendimento\PedidoExame\Item,id'
|
|
],
|
|
"pedidos_exames.{$i}.exames.{$j}.status_id" => [
|
|
'nullable',
|
|
'integer',
|
|
'exists:\App\Models\Parametrizacao\Exame\ExameStatus,id'
|
|
],
|
|
"pedidos_exames.{$i}.exames.{$j}.observacao" => [
|
|
'nullable',
|
|
'string',
|
|
'min:1',
|
|
'max:1000'
|
|
],
|
|
"pedidos_exames.{$i}.exames.{$j}.exame_id" => [
|
|
'required',
|
|
$this->getDistinctExameValidator($pedidoExames, $item, $j),
|
|
'integer',
|
|
'exists:\App\Models\Parametrizacao\Exame\Exame,id',
|
|
],
|
|
]);
|
|
|
|
if (!$this->isRealizado($item['status_id'] ?? null))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$item = is_numeric($item['exame_id'])
|
|
? Exame::find($item['exame_id'])
|
|
: null;
|
|
|
|
if (is_null($item) || $this->isNullIdade($atendimento)) {
|
|
|
|
continue;
|
|
}
|
|
|
|
$procedimento = Procedimento::where('codigo', $item->codigo)
|
|
->orderByDesc('competencia_id')
|
|
->first();
|
|
|
|
$rules["pedidos_exames.{$i}.exames.{$j}.exame_id"] =
|
|
array_merge($rules["pedidos_exames.{$i}.exames.{$j}.exame_id"], [
|
|
new ValidCompetencia($procedimento),
|
|
new Idade($procedimento, $atendimento->faa->paciente->cidadao->idade['anos']),
|
|
new Quantidade($procedimento, 1),
|
|
new SexoRule($procedimento, $atendimento->faa->paciente->cidadao->sexo),
|
|
]);
|
|
|
|
if (!$atendimento->faa->paciente->isDesconhecido) {
|
|
|
|
$rules["pedidos_exames.{$i}.exames.{$j}.exame_id"] =
|
|
array_merge($rules["pedidos_exames.{$i}.exames.{$j}.exame_id"], [
|
|
new Idade($procedimento, $atendimento->faa->paciente->cidadao->idade['anos']),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
private function getOcupacaoProfissionalLogado()
|
|
{
|
|
return Lotacao::find(auth()->payload()->get('lotacao_id'))->ocupacao;
|
|
}
|
|
|
|
private function getDistinctExameValidator($pedidoExames, $currentItem, $currentIndex)
|
|
{
|
|
return function ($value, $attribute, $fail) use ($pedidoExames, $currentIndex, $currentItem) {
|
|
|
|
foreach ($pedidoExames['exames'] as $i => $item) {
|
|
|
|
if ($currentIndex != $i && $currentItem['exame_id'] == $item['exame_id']) {
|
|
|
|
return $fail(":attribute possui valor duplicado.");
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
private function isRealizado(?int $statusId)
|
|
{
|
|
return $statusId === ExameStatus::REALIZADO;
|
|
}
|
|
|
|
private function isNullIdade(Atendimento $atendimento)
|
|
{
|
|
return is_null($atendimento->faa->paciente->cidadao->idade ?? null);
|
|
}
|
|
|
|
private function getPostFinalizacaoDesfechoRules($atendimento)
|
|
{
|
|
$rules = [
|
|
'finalizacao.desfecho_id' => [
|
|
Rule::requiredIf(fn () => $this->hasFinalizacao()),
|
|
'integer',
|
|
Rule::in(Desfecho::PACIENTE_LIBERADO),
|
|
$this->getDesfechoIdValidator($atendimento),
|
|
'exists:\App\Models\ProntoAtendimento\Atendimento\Desfecho,id',
|
|
]
|
|
];
|
|
|
|
return $rules;
|
|
}
|
|
|
|
private function hasFinalizacao()
|
|
{
|
|
return isset($this->finalizacao);
|
|
}
|
|
|
|
private function isPacienteEmObservacao($atendimento)
|
|
{
|
|
return !is_null($atendimento->fila) && $atendimento->fila->whereHas('centroCusto.setor', function ($query) {
|
|
|
|
$query->where('tipo_id', SetorTipo::OBSERVACAO);
|
|
})
|
|
->exists();
|
|
}
|
|
}
|