323 lines
12 KiB
PHP
323 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ProntoAtendimento;
|
|
|
|
use App\Enums\ProntoAtendimento\Atendimento\Desfecho;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\ProntoAtendimento\Atendimento\Atendimento;
|
|
use App\Models\ProntoAtendimento\Atendimento\EncaminhamentoHospitalar;
|
|
use App\Services\Cadastro\SujeitoAtencao\ObitoService;
|
|
use App\Services\ProntoAtendimento\Fila\FilaService;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class DesfechoService extends Service
|
|
{
|
|
public static function store($faa)
|
|
{
|
|
self::validations($faa);
|
|
|
|
$atendimento = self::getAtendimentoFaa($faa);
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
$lastAtendimentoMedico = $atendimento->atendimentosMedico->last() ?? null;
|
|
$dataHoraDesfechoMedico = is_null($lastAtendimentoMedico->desfecho_id ?? null)
|
|
? Carbon::now()
|
|
: $lastAtendimentoMedico->created_at;
|
|
|
|
switch (request()->desfecho_id) {
|
|
|
|
case Desfecho::FAA_EXTRAVIADA: {
|
|
|
|
$faa->usuario_id = $currentLotacao->usuario_id;
|
|
$faa->lotacao_id = $currentLotacao->id;
|
|
$faa->data_hora_desfecho = Carbon::now();
|
|
|
|
break;
|
|
}
|
|
case Desfecho::PACIENTE_ATENDIDO: {
|
|
|
|
if (!is_null($faa->reaberto_em) && !is_null($faa->usuario_id)) {
|
|
|
|
break;
|
|
}
|
|
|
|
$faa->usuario_id = Lotacao::findOrFail(request()->paciente_liberado['lotacao_id'])->usuario_id;
|
|
$faa->lotacao_id = request()->paciente_liberado['lotacao_id'];
|
|
$faa->data_hora_desfecho = $dataHoraDesfechoMedico;
|
|
|
|
break;
|
|
}
|
|
case Desfecho::ALTA_MEDICA: {
|
|
|
|
if (!is_null($faa->reaberto_em) && !is_null($faa->usuario_id)) {
|
|
|
|
break;
|
|
}
|
|
|
|
$faa->usuario_id = Lotacao::findOrFail(request()->alta_medica['lotacao_id'])->usuario_id;
|
|
$faa->lotacao_id = request()->alta_medica['lotacao_id'];
|
|
$faa->data_hora_desfecho = $dataHoraDesfechoMedico;
|
|
|
|
break;
|
|
}
|
|
case Desfecho::EVASAO: {
|
|
|
|
if (!is_null($faa->reaberto_em) && !is_null($faa->usuario_id)) {
|
|
|
|
break;
|
|
}
|
|
|
|
$faa->usuario_id = $currentLotacao->usuario_id;
|
|
$faa->lotacao_id = $currentLotacao->id;
|
|
$faa->data_hora_desfecho = $dataHoraDesfechoMedico;
|
|
|
|
if (!is_null($atendimento->fila)) {
|
|
|
|
FilaService::evade([
|
|
'motivo_evasao' => request()->evasao['motivo']
|
|
], $atendimento->fila_id);
|
|
}
|
|
|
|
break;
|
|
}
|
|
case Desfecho::OBITO: {
|
|
|
|
$faa->usuario_id = Lotacao::findOrFail(request()->obito['lotacao_id'])->usuario_id;
|
|
$faa->lotacao_id = request()->obito['lotacao_id'];
|
|
$faa->data_hora_desfecho = $dataHoraDesfechoMedico;
|
|
|
|
$obitoService = new ObitoService($faa->paciente->cidadao);
|
|
|
|
if (!is_null($obitoService->get())) {
|
|
|
|
break;
|
|
}
|
|
|
|
$obitoService->save(
|
|
Carbon::createFromFormat('d/m/Y H:i', request()->obito['obito_em']),
|
|
request()->obito['declaracao'],
|
|
request()->obito['causa'],
|
|
request()->obito['cid_id'],
|
|
$faa->usuario_id,
|
|
);
|
|
|
|
break;
|
|
}
|
|
case Desfecho::ENCAMINHAMENTO_HOSPITALAR: {
|
|
|
|
if ($atendimento->encaminhamentosHospitalar()->exists() && !is_null($faa->reaberto_em)) {
|
|
|
|
break;
|
|
}
|
|
|
|
$faa->usuario_id = Lotacao::findOrFail(request()->encaminhamento_hospitalar['lotacao_id'])->usuario_id;
|
|
$faa->lotacao_id = request()->encaminhamento_hospitalar['lotacao_id'];
|
|
$faa->data_hora_desfecho = $dataHoraDesfechoMedico;
|
|
|
|
EncaminhamentoHospitalar::create([
|
|
'atendimento_id' => $atendimento->id,
|
|
'usuario_id' => $faa->usuario_id,
|
|
'unidade_id' => request()->encaminhamento_hospitalar['unidade_id'] ?? null,
|
|
'unidade_nome' => request()->encaminhamento_hospitalar['unidade_nome'] ?? null,
|
|
'quarto' => request()->encaminhamento_hospitalar['quarto'] ?? null,
|
|
'leito' => request()->encaminhamento_hospitalar['leito'] ?? null,
|
|
'cid_id' => request()->encaminhamento_hospitalar['cid_id'] ?? null,
|
|
'medico_hospitalar' => request()->encaminhamento_hospitalar['medico_hospitalar'] ?? null,
|
|
'observacoes' => request()->encaminhamento_hospitalar['observacoes'] ?? null,
|
|
]);
|
|
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
private static function validations($faa)
|
|
{
|
|
$rules = [
|
|
'desfecho_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::in(
|
|
Desfecho::ALTA_MEDICA,
|
|
Desfecho::EVASAO,
|
|
Desfecho::ENCAMINHAMENTO_HOSPITALAR,
|
|
Desfecho::OBITO,
|
|
Desfecho::FAA_EXTRAVIADA,
|
|
Desfecho::PACIENTE_ATENDIDO,
|
|
),
|
|
'exists:\App\Models\ProntoAtendimento\Atendimento\Desfecho,id',
|
|
],
|
|
'alta_medica' => [
|
|
'nullable',
|
|
'required_if:desfecho_id,' . Desfecho::ALTA_MEDICA,
|
|
'array',
|
|
],
|
|
'evasao' => [
|
|
'nullable',
|
|
'required_if:desfecho_id,' . Desfecho::EVASAO,
|
|
'array',
|
|
],
|
|
'evasao.motivo' => [
|
|
'nullable',
|
|
'required_if:desfecho_id,' . Desfecho::EVASAO,
|
|
'string',
|
|
'max:200'
|
|
],
|
|
'encaminhamento_hospitalar' => [
|
|
'nullable',
|
|
'required_if:desfecho_id,' . Desfecho::ENCAMINHAMENTO_HOSPITALAR,
|
|
'array',
|
|
],
|
|
'obito' => [
|
|
'required_if:desfecho_id,' . Desfecho::OBITO,
|
|
'array',
|
|
],
|
|
];
|
|
|
|
if (!is_null(request()->alta_medica ?? null)) {
|
|
|
|
$rules = array_merge($rules, [
|
|
'alta_medica.lotacao_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\SujeitoAtencao\Lotacao,id',
|
|
],
|
|
]);
|
|
}
|
|
|
|
if (!is_null(request()->encaminhamento_hospitalar ?? null)) {
|
|
|
|
$rules = array_merge($rules,
|
|
[
|
|
'encaminhamento_hospitalar.unidade_id' => [
|
|
'nullable',
|
|
Rule::requiredIf(fn () =>
|
|
request()->desfecho_id === Desfecho::ENCAMINHAMENTO_HOSPITALAR &&
|
|
(request()->encaminhamento_hospitalar['unidade_nome'] ?? null) === null
|
|
),
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\Estabelecimento\Unidade,id',
|
|
],
|
|
'encaminhamento_hospitalar.unidade_nome' => [
|
|
'nullable',
|
|
Rule::requiredIf(fn () =>
|
|
request()->desfecho_id === Desfecho::ENCAMINHAMENTO_HOSPITALAR &&
|
|
(request()->encaminhamento_hospitalar['unidade_id'] ?? null) === null
|
|
),
|
|
'string',
|
|
'max:100',
|
|
],
|
|
'encaminhamento_hospitalar.lotacao_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\SujeitoAtencao\Lotacao,id',
|
|
],
|
|
'encaminhamento_hospitalar.quarto' => [
|
|
'nullable',
|
|
'string',
|
|
'max:3',
|
|
],
|
|
'encaminhamento_hospitalar.leito' => [
|
|
'nullable',
|
|
'string',
|
|
'max:3',
|
|
],
|
|
'encaminhamento_hospitalar.cid_id' => [
|
|
'nullable',
|
|
'integer',
|
|
'exists:\App\Models\Sigtap\Cid,id',
|
|
],
|
|
'encaminhamento_hospitalar.medico_hospitalar' => [
|
|
'nullable',
|
|
'string',
|
|
'max:150',
|
|
],
|
|
'encaminhamento_hospitalar.observacoes' => [
|
|
'nullable',
|
|
'string',
|
|
'max:3000',
|
|
],
|
|
]
|
|
);
|
|
}
|
|
|
|
if (is_array(request()->obito ?? null)) {
|
|
|
|
$rules = array_merge($rules, [
|
|
'obito.obito_em' => [
|
|
'bail',
|
|
'required',
|
|
'date_format:d/m/Y H:i',
|
|
self::getObitoEmValidator($faa),
|
|
],
|
|
'obito.declaracao' => [
|
|
'required',
|
|
'string',
|
|
'min:8',
|
|
'max:13'
|
|
],
|
|
'obito.causa' => [
|
|
'required',
|
|
'string',
|
|
'max:255'
|
|
],
|
|
'obito.cid_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:\App\Models\Sigtap\Cid,id'
|
|
],
|
|
'obito.lotacao_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:\App\Models\Cadastro\SujeitoAtencao\Lotacao,id',
|
|
],
|
|
]);
|
|
}
|
|
|
|
request()->validate($rules, [
|
|
'encaminhamento_hospitalar.unidade_id.required' =>
|
|
"O campo encaminhamento hospitalar.unidade id é obrigatório quando encaminhamento hospitalar.unidade nome não está presente.",
|
|
'encaminhamento_hospitalar.unidade_nome.required' =>
|
|
'O campo encaminhamento hospitalar.unidade nome é obrigatório quando encaminhamento hospitalar.unidade id não está presente.',
|
|
]);
|
|
}
|
|
|
|
private static function getObitoEmValidator($faa)
|
|
{
|
|
return function ($attribute, $value, $fail) use ($faa) {
|
|
|
|
$dataAtual = Carbon::now();
|
|
$dataObito = Carbon::createFromFormat('d/m/Y H:i', $value);
|
|
$diffObitoDataAtual = $dataAtual->diffInMinutes($dataObito, false);
|
|
|
|
if (!is_null($faa->paciente->cidadao->data_nascimento ?? null)) {
|
|
|
|
$dataNascimento = $faa->paciente->cidadao->data_nascimento;
|
|
$diffObitoDataNascimento = $dataNascimento->diffInMinutes($dataObito, false);
|
|
|
|
if ($diffObitoDataNascimento < 0 || $diffObitoDataAtual > 0) {
|
|
|
|
$msg = "A data e hora inserida deve ser entre a data de nascimento ";
|
|
$msg .= "{$dataNascimento->format('d/m/Y')} e a data e hora atual.";
|
|
|
|
$fail($msg);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if ($diffObitoDataAtual > 0) {
|
|
|
|
$fail("A data e hora inserida deve ser menor ou igual a data e hora atual.");
|
|
}
|
|
};
|
|
}
|
|
|
|
private static function getAtendimentoFaa($faa)
|
|
{
|
|
return Atendimento::where('faa_id', $faa->id)->get()->last();
|
|
}
|
|
}
|