144 lines
7.0 KiB
PHP
144 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Impressao\ProntoAtendimento\AtendimentoMedico;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Models\ProntoAtendimento\Atendimento\Medico\Encaminhamento;
|
|
use App\Models\Sigtap\Cid;
|
|
use App\Models\Sigtap\Ocupacao;
|
|
use App\Utils\DataUtil;
|
|
use App\Utils\Exports\Pdf;
|
|
use App\Utils\Exports\Template;
|
|
use App\Utils\MaskUtil;
|
|
|
|
class GuiaEncaminhamentoController extends Controller
|
|
{
|
|
private const TITLE = 'Guia de encaminhamento';
|
|
private const SUBTITLE = 'Referência';
|
|
private const TEMPLATE = 'pronto-atendimento.atendimento-medico.guia-encaminhamento';
|
|
|
|
public static function getTemplate($atendimentoId = null)
|
|
{
|
|
$encaminhamentos = $atendimentoId
|
|
? Encaminhamento::orderByDesc('created_at')
|
|
->whereHas('atendimentoMedico', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->get()
|
|
: request()->encaminhamentos;
|
|
|
|
$data['encaminhamentos'] = [];
|
|
|
|
foreach ($encaminhamentos as $encaminhamento) {
|
|
|
|
if ((is_array($encaminhamento) && is_numeric($encaminhamento['id'] ?? null))
|
|
|| $encaminhamento instanceof Encaminhamento
|
|
) {
|
|
|
|
if (!($encaminhamento instanceof Encaminhamento)) {
|
|
|
|
$encaminhamento = Encaminhamento::findOrFail($encaminhamento['id']);
|
|
}
|
|
|
|
$paciente = $encaminhamento->atendimentoMedico->atendimento->faa->paciente;
|
|
|
|
$cid = "{$encaminhamento->atendimentoMedico->cid->cid->codigo} - "
|
|
. $encaminhamento->atendimentoMedico->cid->cid->nome;
|
|
|
|
$data['encaminhamentos'][] = [
|
|
'paciente' => [
|
|
'nome' => $paciente->cidadao->nome,
|
|
'nomeSocial' => $paciente->cidadao->nome_social,
|
|
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'cns' => $paciente->cidadao->cns,
|
|
'sexo' => $paciente->cidadao->sexo->descricao,
|
|
'idadeCompleta' => $paciente->cidadao->idade_formatada,
|
|
'dataNascimento' => $paciente->cidadao->data_nascimento
|
|
? (new \DateTime($paciente->cidadao->data_nascimento))->format('d/m/Y')
|
|
: null,
|
|
'telefone' => $paciente->cidadao->telefone_principal
|
|
? MaskUtil::getPhoneMasked($paciente->cidadao->telefone_principal->numero)
|
|
: null,
|
|
'prontuarioUnidade' => $paciente->prontuario_unidade,
|
|
'nomeMae' => $paciente->cidadao->nome_mae,
|
|
'municipioNascimento' => $paciente->cidadao->municipioNascimento
|
|
? "{$paciente->cidadao->municipioNascimento->nome} / "
|
|
. $paciente->cidadao->municipioNascimento->estado->uf
|
|
: '',
|
|
],
|
|
'unidade' => [
|
|
'nome' => $encaminhamento->updatedLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'cnes' => $encaminhamento->updatedLotacao->infraestrutura->unidade->cnes,
|
|
],
|
|
'profissional' => [
|
|
'nome' => $encaminhamento->updatedLotacao->usuario->cidadao->nome,
|
|
'cbo' => $encaminhamento->updatedLotacao->ocupacao->nome,
|
|
'cns' => $encaminhamento->updatedLotacao->usuario->cidadao->cns,
|
|
],
|
|
'cbo' => $encaminhamento->ocupacao->nome,
|
|
'cid' => $cid,
|
|
'urgente' => $encaminhamento->urgente,
|
|
'motivo' => $encaminhamento->motivo,
|
|
'observacoes' => $encaminhamento->observacoes ?? ''
|
|
];
|
|
}
|
|
else {
|
|
|
|
$paciente = Paciente::findOrFail(request()->paciente_id);
|
|
$cid = request()->cid_id ? Cid::findOrFail(request()->cid_id) : null;
|
|
$lotacao = Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
|
|
|
|
$data['encaminhamentos'][] = [
|
|
'paciente' => [
|
|
'nome' => $paciente->cidadao->nome,
|
|
'nomeSocial' => $paciente->cidadao->nome_social,
|
|
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'cns' => $paciente->cidadao->cns,
|
|
'sexo' => $paciente->cidadao->sexo->descricao,
|
|
'idadeCompleta' => $paciente->cidadao->idade_formatada,
|
|
'dataNascimento' => $paciente->cidadao->data_nascimento
|
|
? (new \DateTime($paciente->cidadao->data_nascimento))->format('d/m/Y')
|
|
: null,
|
|
'telefone' => $paciente->cidadao->telefone_principal
|
|
? MaskUtil::getPhoneMasked($paciente->cidadao->telefone_principal->numero)
|
|
: null,
|
|
'prontuarioUnidade' => $paciente->prontuario_unidade,
|
|
'nomeMae' => $paciente->cidadao->nome_mae,
|
|
'municipioNascimento' => $paciente->cidadao->municipioNascimento
|
|
? "{$paciente->cidadao->municipioNascimento->nome} / "
|
|
. $paciente->cidadao->municipioNascimento->estado->uf
|
|
: '',
|
|
],
|
|
'unidade' => [
|
|
'nome' => $lotacao->infraestrutura->unidade->nome_fantasia,
|
|
'cnes' => $lotacao->infraestrutura->unidade->cnes,
|
|
],
|
|
'profissional' => [
|
|
'nome' => $lotacao->usuario->cidadao->nome,
|
|
'cbo' => $lotacao->ocupacao->nome,
|
|
'cns' => $lotacao->usuario->cidadao->cns,
|
|
],
|
|
'cbo' => Ocupacao::findOrFail($encaminhamento['ocupacao_id'])->nome,
|
|
'cid' => $cid ? "{$cid->codigo} - {$cid->nome}" : '',
|
|
'urgente' => DataUtil::getBooleanValueFromStringBoolean($encaminhamento['urgente']),
|
|
'motivo' => $encaminhamento['motivo'],
|
|
'observacoes' => $encaminhamento['observacoes'] ?? ''
|
|
];
|
|
}
|
|
}
|
|
|
|
return Template::make(self::TITLE, self::TEMPLATE)
|
|
->withSubtitle(self::SUBTITLE)
|
|
->withData($data)
|
|
->getHtml();
|
|
}
|
|
|
|
public function __invoke($atendimentoId = null)
|
|
{
|
|
return (new Pdf)->stream(self::getTemplate($atendimentoId), 'guia-encaminhamento');
|
|
}
|
|
}
|