203 lines
8.5 KiB
PHP
203 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Impressao\ProntoAtendimento;
|
|
|
|
use App\Enums\ProntoAtendimento\Raat\CausaAcidente as RaatCausaAcidente;
|
|
use App\Enums\ProntoAtendimento\Raat\Diagnostico as RaatDiagnostico;
|
|
use App\Enums\ProntoAtendimento\Raat\ParteCorpoAtingida as RaatParteCorpoAtingida;
|
|
use App\Enums\ProntoAtendimento\Raat\RegimeTrabalho as RaatRegimeTrabalho;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ProntoAtendimento\Atendimento\Atendimento;
|
|
use App\Models\ProntoAtendimento\Atendimento\Cat;
|
|
use App\Models\ProntoAtendimento\Raat\Agente;
|
|
use App\Models\ProntoAtendimento\Raat\CausaAcidente;
|
|
use App\Models\ProntoAtendimento\Raat\ClassificacaoInicial;
|
|
use App\Models\ProntoAtendimento\Raat\Diagnostico;
|
|
use App\Models\ProntoAtendimento\Raat\ParteCorpoAtingida;
|
|
use App\Models\ProntoAtendimento\Raat\RegimeTrabalho;
|
|
use App\Models\ProntoAtendimento\Raat\TipoAcidente;
|
|
use App\Utils\Exports\Pdf;
|
|
use App\Utils\Exports\Template;
|
|
use App\Utils\MaskUtil;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class CatController extends Controller
|
|
{
|
|
private const TITLE = 'SOLICITAÇÃO PARA EMISSÃO DE COMUNICAÇÃO DE ACIDENTE DE TRABALHO - CAT';
|
|
private const TEMPLATE = 'pronto-atendimento.cat';
|
|
|
|
public static function getTemplate($atendimentoId)
|
|
{
|
|
$faa = Atendimento::findOrFail($atendimentoId)->faa;
|
|
$paciente = $faa->paciente;
|
|
$raat = $faa->raat;
|
|
$parecerMedico = $faa->raat ? $faa->raat->pareceresMedico()->orderByDesc('id')->first() : null;
|
|
$cat = Cat::where('raat_id', $raat->id)->first();
|
|
|
|
$tiposAcidentes = TipoAcidente::orderBy('nome')
|
|
->get()
|
|
->map(fn ($tipo) => [
|
|
$tipo->nome,
|
|
$tipo->id === ($raat->acidente_tipo_id ?? null)
|
|
]);
|
|
|
|
$regimeTrabalho = RegimeTrabalho::orderBy('nome')
|
|
->get()
|
|
->first(fn ($regime) => $regime->id === ($raat->regime_trabalho_id ?? null));
|
|
|
|
$regimeTrabalho = $regimeTrabalho->id === RaatRegimeTrabalho::OUTRO ? $raat->regime_trabalho_outro : $regimeTrabalho->nome;
|
|
|
|
$agentes = Agente::all()
|
|
->map(fn ($agente) => [
|
|
$agente->nome,
|
|
$agente->id === ($parecerMedico->agente_id ?? null)
|
|
]);
|
|
|
|
$partesCorpoAtingidasChecked = $parecerMedico ? $parecerMedico->partesCorpoAtingidas->pluck('id') : collect();
|
|
|
|
$partesCorpoAtingidas = ParteCorpoAtingida::orderBy('id')
|
|
->get()
|
|
->reject(fn ($parte) => $parte->id === RaatParteCorpoAtingida::OUTROS)
|
|
->map(fn ($parte) => [
|
|
$parte->nome,
|
|
$partesCorpoAtingidasChecked->contains($parte->id)
|
|
]);
|
|
|
|
$causasAcidenteChecked = $parecerMedico ? $parecerMedico->causasAcidente->pluck('id') : collect();
|
|
|
|
$causasAcidente = CausaAcidente::all()
|
|
->reject(fn ($causa) => $causa->id === RaatCausaAcidente::OUTROS)
|
|
->map(fn ($causa) => [
|
|
$causa->nome,
|
|
$causasAcidenteChecked->contains($causa->id)
|
|
]);
|
|
|
|
$diagnosticosChecked = $parecerMedico ? $parecerMedico->diagnosticos->pluck('id') : collect();
|
|
|
|
$diagnosticos = Diagnostico::all()
|
|
->reject(fn ($diagnostico) => $diagnostico->id === RaatDiagnostico::OUTROS)
|
|
->map(fn ($diagnostico) => [
|
|
$diagnostico->nome,
|
|
$diagnosticosChecked->contains($diagnostico->id)
|
|
]);
|
|
|
|
$classificacoesIniciais = ClassificacaoInicial::all()
|
|
->map(fn ($classificacao) => [
|
|
$classificacao->nome,
|
|
$classificacao->id === ($parecerMedico->classificacao_inicial_id ?? null)
|
|
]);
|
|
|
|
$data = [
|
|
'paciente' => [
|
|
'nome' => $paciente->cidadao->nome,
|
|
'sexo' => $paciente->cidadao->sexo->descricao,
|
|
'dataNascimento' => $paciente->cidadao->data_nascimento
|
|
? $paciente->cidadao->data_nascimento->format('d/m/Y') : '',
|
|
],
|
|
'empresa' => [
|
|
'nome' => $raat->empresa_nome ?? '',
|
|
'ramoAtividade' => $raat->empresa_ramo_atividade ?? '',
|
|
'endereco' => $raat->empresa_endereco ?? '',
|
|
'telefone' => $raat && $raat->empresa_telefone ? MaskUtil::getPhoneMasked($raat->empresa_telefone) : '',
|
|
'ocupacaoPaciente' => $cat->empresa_ocupacao_paciente,
|
|
],
|
|
'identificacaoAcidente' => [
|
|
'data' => $raat && $raat->acidente_data ? $raat->acidente_data->format('d/m/Y') : null,
|
|
'hora' => $raat && $raat->acidente_hora ? $raat->acidente_hora->format('H:i') : null,
|
|
'tipoAcidente' => self::retonarPrimeiroVerdadeiro($tiposAcidentes),
|
|
'local' => $raat->acidente_local ?? '',
|
|
'descricao' => $raat->acidente_descricao ?? '',
|
|
'regimeTrabalho' => $regimeTrabalho,
|
|
'regimeTrabalhoOutro' => $raat->regime_trabalho_outro ?? '',
|
|
],
|
|
'agente' => self::retonarPrimeiroVerdadeiro($agentes),
|
|
'partesCorpoAtingidas' => $parecerMedico ? self::retornaElementosInline($partesCorpoAtingidas, $parecerMedico->parte_corpo_atingida_outras) : '',
|
|
'causasAcidente' => $parecerMedico ? self::retornaElementosInline($causasAcidente, $parecerMedico->causa_acidente_outras) : '',
|
|
'diagnosticos' => $parecerMedico ? self::retornaElementosInline($diagnosticos, $parecerMedico->diagnostico_outros) : '',
|
|
'cid' => ($parecerMedico->atendimentoMedico->cid->cid ?? null)
|
|
? $parecerMedico->atendimentoMedico->cid->cid->codigo
|
|
. ' - ' . $parecerMedico->atendimentoMedico->cid->cid->nome
|
|
: '',
|
|
'observacao' => $parecerMedico->observacao ?? '',
|
|
'comAfastamento' => $parecerMedico ? ($parecerMedico->afastado === true ? 'SIM' : ($parecerMedico->afastado === false ? 'NÃO' : '')) : '',
|
|
'classificacoesIniciais' => $classificacoesIniciais,
|
|
'hasParecerMedico' => !!$parecerMedico,
|
|
'profissional' => [
|
|
'nome' => $parecerMedico->updatedLotacao->usuario->cidadao->nome ?? null,
|
|
'cbo' => $parecerMedico->updatedLotacao->ocupacao->nome ?? null,
|
|
],
|
|
'sofreuAcidenteTrabalho' => self::converteBoleanoEmRadio($cat->paciente_sofreu_acidente_trabalho ?? null),
|
|
'portadorDoencaOcupacional' => self::converteBoleanoEmRadio($cat->paciente_portador_doenca_ocupacional ?? null),
|
|
'precisaInternacao' => $cat->paciente_precisa_internacao === true ? 'SIM' : ($cat->paciente_precisa_internacao === false ? 'NÃO' : ''),
|
|
'diasAfastamento' => $cat->dias_afastamento ? $cat->dias_afastamento . ' dias' : '',
|
|
'confirmacoesMedicas' => self::parecerMedicoOpcoes($cat),
|
|
|
|
];
|
|
|
|
return Template::make(self::TITLE, self::TEMPLATE)->withData($data)->getHtml();
|
|
}
|
|
|
|
private static function retornaElementosInline(Collection $arr, ?string $value)
|
|
{
|
|
$filteredItems = collect($arr)
|
|
->filter(fn ($item) => $item[1] === true)
|
|
->map(fn ($item) => $item[0]);
|
|
|
|
if ($value) {
|
|
$filteredItems->add($value);
|
|
}
|
|
|
|
$count = $filteredItems->count();
|
|
$result = '';
|
|
|
|
if ($count > 1) {
|
|
$lastItem = $filteredItems->pop();
|
|
$result = $filteredItems->implode(', ') . ' e ' . $lastItem;
|
|
}
|
|
|
|
if ($count === 1) {
|
|
$result = $filteredItems->first();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private static function parecerMedicoOpcoes($cat)
|
|
{
|
|
return new Collection([
|
|
[
|
|
'Ter sofrido Acidente de Trabalho (Art.19 da Lei nº 8.213/1991).',
|
|
$cat->paciente_sofreu_acidente_trabalho
|
|
],
|
|
[
|
|
'Ser portador de Doença Ocupacional (Art.20 da Lei nº 8.213/1991).',
|
|
$cat->paciente_portador_doenca_ocupacional,
|
|
]
|
|
]);
|
|
}
|
|
|
|
private static function retonarPrimeiroVerdadeiro(Collection $arr)
|
|
{
|
|
return $arr->first(fn ($value) => $value[1] === true)[0] ?? null;
|
|
}
|
|
|
|
private static function converteBoleanoEmRadio(?bool $value)
|
|
{
|
|
return new Collection([
|
|
[
|
|
'Sim',
|
|
$value
|
|
],
|
|
[
|
|
'Não',
|
|
$value === false
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function __invoke($atendimentoId)
|
|
{
|
|
return (new Pdf)->stream(self::getTemplate($atendimentoId), self::TITLE);
|
|
}
|
|
}
|