236 lines
9.0 KiB
PHP
236 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Impressao\ProntoAtendimento\AtendimentoMedico;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
use App\Enums\Cadastro\SujeitoAtencao\Sexo;
|
|
use App\Enums\ProntoAtendimento\Atendimento\Medico\DeclaracaoComparecimentoTipo;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Models\Geral\DocumentoTipo;
|
|
use App\Models\ProntoAtendimento\Atendimento\Medico\DeclaracaoComparecimento;
|
|
use App\Utils\Exports\Pdf;
|
|
use App\Utils\Exports\Template;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
|
|
class DeclaracaoComparecimentoPacienteController extends Controller
|
|
{
|
|
private const TITLE = 'Declaração de comparecimento do paciente';
|
|
private const TEMPLATE = 'pronto-atendimento.atendimento-medico.declaracao-comparecimento';
|
|
|
|
public static function getTemplate($atendimentoId = null)
|
|
{
|
|
$declaracoes = $atendimentoId
|
|
? DeclaracaoComparecimento::orderByDesc('created_at')
|
|
->where(function($query) use ($atendimentoId) {
|
|
$query
|
|
->whereHas('atendimentoMedico', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->orWhereHas('atendimentoEnfermagem', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->orWhereHas('pacienteObservacao', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->orWhereHas('atendimentoTriagem', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->orWhereHas('atendimentoExameImagem', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
})
|
|
->orWhereHas('recepcao', function($query) use ($atendimentoId) {
|
|
$query->where('atendimento_id', $atendimentoId);
|
|
});
|
|
})
|
|
->where('tipo_id', DeclaracaoComparecimentoTipo::PACIENTE)
|
|
->get()
|
|
: request()->declaracoes;
|
|
|
|
$now = new \DateTime;
|
|
|
|
$data['declaracoes'] = [];
|
|
|
|
foreach ($declaracoes as $declaracao) {
|
|
|
|
if ((is_array($declaracao) && is_numeric($declaracao['id'] ?? null))
|
|
|| $declaracao instanceof DeclaracaoComparecimento
|
|
) {
|
|
|
|
if (!($declaracao instanceof DeclaracaoComparecimento)) {
|
|
|
|
$declaracao = DeclaracaoComparecimento::findOrFail($declaracao['id']);
|
|
}
|
|
|
|
$paciente = self::getPaciente($declaracao);
|
|
$profissioanal = self::getProfissional($declaracao);
|
|
|
|
$data['declaracoes'][] = [
|
|
'acompanhante' => [
|
|
'nome' => $declaracao->nome,
|
|
],
|
|
'paciente' => [
|
|
'nome' => $paciente->cidadao->nome,
|
|
'nomeSocial' => $paciente->cidadao->nome_social,
|
|
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'sexoMasculino' => $paciente->cidadao->sexo->id === Sexo::MASCULINO
|
|
|| $paciente->cidadao->sexo->base_id === Sexo::MASCULINO,
|
|
],
|
|
'documento' => [
|
|
'tipo' => $declaracao->documentoTipo->nome,
|
|
'numero' => MaskUtil::getDocumentMasked($declaracao->documento_tipo_id, $declaracao->documento_numero),
|
|
],
|
|
'unidade' => [
|
|
'nome' => $declaracao->updatedLotacao->infraestrutura->unidade->nome_fantasia,
|
|
],
|
|
'de' => $declaracao->de->format('H:i (d/m/Y)'),
|
|
'ate' => $declaracao->ate->format('H:i (d/m/Y)'),
|
|
'motivo' => $declaracao->motivo,
|
|
'profissional' => [
|
|
'nome' => $profissioanal->updatedLotacao->usuario->cidadao->nome,
|
|
'cbo' => $profissioanal->updatedLotacao->ocupacao->nome,
|
|
],
|
|
];
|
|
}
|
|
else {
|
|
|
|
$paciente = Paciente::findOrFail(request()->paciente_id);
|
|
$lotacao = Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
|
|
$documentoTipo = DocumentoTipo::findOrFail($declaracao['documento_tipo_id']);
|
|
|
|
$data['declaracoes'][] = [
|
|
'acompanhante' => [
|
|
'nome' => $declaracao['nome'],
|
|
],
|
|
'paciente' => [
|
|
'nome' => $paciente->cidadao->nome,
|
|
'nomeSocial' => $paciente->cidadao->nome_social,
|
|
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'sexoMasculino' => $paciente->cidadao->sexo->id === Sexo::MASCULINO
|
|
|| $paciente->cidadao->sexo->base_id === Sexo::MASCULINO,
|
|
],
|
|
'documento' => [
|
|
'tipo' => $documentoTipo->nome,
|
|
'numero' => MaskUtil::getDocumentMasked($documentoTipo->id, $declaracao['documento_numero']),
|
|
],
|
|
'unidade' => [
|
|
'nome' => $lotacao->infraestrutura->unidade->nome_fantasia,
|
|
],
|
|
'de' => Carbon::createFromFormat('Y-m-d H:i', $declaracao['de'])->format('H:i (d/m/Y)'),
|
|
'ate' => Carbon::createFromFormat('Y-m-d H:i', $declaracao['ate'])->format('H:i (d/m/Y)'),
|
|
'motivo' => $declaracao['motivo'],
|
|
'profissional' => [
|
|
'nome' => $lotacao->usuario->cidadao->nome,
|
|
'cbo' => $lotacao->ocupacao->nome,
|
|
],
|
|
];
|
|
}
|
|
}
|
|
|
|
return Template::make(self::TITLE, self::TEMPLATE)->withData($data)->getHtml();
|
|
}
|
|
|
|
|
|
public static function getPaciente($declaracao) {
|
|
|
|
$paciente = $declaracao->atendimentoMedico
|
|
? $declaracao->atendimentoMedico->atendimento->faa->paciente
|
|
: null;
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->atendimentoEnfermagem
|
|
? $declaracao->atendimentoEnfermagem->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->atendimentoTriagem
|
|
? $declaracao->atendimentoTriagem->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->pacienteObservacao
|
|
? $declaracao->pacienteObservacao->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->pacienteObservacao
|
|
? $declaracao->pacienteObservacao->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->atendimentoExameImagem
|
|
? $declaracao->atendimentoExameImagem->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
if (!$paciente) {
|
|
|
|
$paciente = $declaracao->recepcao
|
|
? $declaracao->recepcao->atendimento->faa->paciente
|
|
: null;
|
|
}
|
|
|
|
return $paciente;
|
|
}
|
|
|
|
public static function getProfissional($declaracao) {
|
|
|
|
$profissional = $declaracao->atendimentoMedico
|
|
? $declaracao->atendimentoMedico
|
|
: null;
|
|
|
|
if (!$profissional) {
|
|
|
|
$profissional = $declaracao->atendimentoEnfermagem
|
|
? $declaracao->atendimentoEnfermagem
|
|
: null;
|
|
}
|
|
|
|
if (!$profissional) {
|
|
|
|
$profissional = $declaracao->atendimentoTriagem
|
|
? $declaracao->atendimentoTriagem
|
|
: null;
|
|
}
|
|
|
|
if (!$profissional) {
|
|
|
|
$profissional = $declaracao->pacienteObservacao
|
|
? $declaracao->pacienteObservacao
|
|
: null;
|
|
}
|
|
|
|
if (!$profissional) {
|
|
|
|
$profissional = $declaracao->atendimentoExameImagem
|
|
? $declaracao->atendimentoExameImagem
|
|
: null;
|
|
}
|
|
|
|
if (!$profissional) {
|
|
|
|
$profissional = $declaracao->recepcao
|
|
? $declaracao->recepcao
|
|
: null;
|
|
}
|
|
|
|
return $profissional;
|
|
}
|
|
|
|
public function __invoke($atendimentoId = null)
|
|
{
|
|
return (new Pdf)->stream(self::getTemplate($atendimentoId), 'declaracao-comparecimento-paciente');
|
|
}
|
|
}
|