172 lines
9.6 KiB
PHP
172 lines
9.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
|
use App\Models\Intranet\Cliente;
|
|
use App\Services\Service;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
|
|
class RelatorioExameAgendadoRealizadoService extends Service
|
|
{
|
|
public static function index($id)
|
|
{
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
|
|
|
$agendamento = Agendamento::where('paciente_id', $id)->firstOrFail();
|
|
|
|
return [
|
|
'form' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
|
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
'sistema' => [
|
|
'ramo_atividade' => [
|
|
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
|
|
],
|
|
'nome' => $cliente->sistema->nome ?? null,
|
|
'versao' => $cliente->sistema->versao ?? null,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'paciente' => [
|
|
'nome' => $agendamento->paciente->cidadao->nome ?? null,
|
|
'nome_social' => $agendamento->paciente->cidadao->nome_social,
|
|
'prefere_nome_social' => $agendamento->paciente->cidadao
|
|
->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'data_nascimento' => $agendamento->paciente->cidadao->data_nascimento
|
|
? $agendamento->paciente->cidadao->data_nascimento->format('d/m/Y')
|
|
: '',
|
|
'cpf' => $agendamento->paciente->cidadao->cpf
|
|
? MaskUtil::getCpfMasked($agendamento->paciente->cidadao->cpf) : null,
|
|
'rg' => $agendamento->paciente->cidadao->carteiraIdentidade->numero ?? null,
|
|
'convenio_medico_nome' => $agendamento->paciente->cidadao->conveniosMedico->first()->nome ?? null,
|
|
'situacao_familiar' => $agendamento->paciente->cidadao->situacaoFamiliarConjugal->descricao ?? null,
|
|
'prontuario' => $agendamento->paciente->prontuario,
|
|
'unidade' => $agendamento->paciente->unidade->nome_fantasia ?? null,
|
|
'sexo' => $agendamento->paciente->cidadao->sexo ?? '-',
|
|
'classificacao_id' => $agendamento->paciente->classificacao_id ?? '-',
|
|
'classificacao_nome' => $agendamento->paciente->classificacao->descricao ?? '-',
|
|
'cns' => $agendamento->paciente->cidadao->cns ? MaskUtil::getCnsMasked($agendamento->paciente->cidadao->cns) : '',
|
|
'telefone' => MaskUtil::getPhoneMasked($agendamento->paciente->cidadao->telefone_principal->numero ?? ''),
|
|
],
|
|
'endereco' => [
|
|
'estado_id' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id,
|
|
'municipio' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio,
|
|
'logradouro' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro,
|
|
'cep' => MaskUtil::getCepMasked($agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep),
|
|
'numero' => $agendamento->paciente->cidadao->domicilio->endereco->numero,
|
|
'sem_numero' => $agendamento->paciente->cidadao->domicilio->endereco->sem_numero,
|
|
'complemento' => $agendamento->paciente->cidadao->domicilio->endereco->complemento ?? '',
|
|
'zona_id' => $agendamento->paciente->cidadao->domicilio->endereco->zona_id,
|
|
],
|
|
'agendados' => Agendamento::where('paciente_id', $id)
|
|
->whereHas('agendamentoExame', function($query) {
|
|
$query->whereNull('status_id')
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO);
|
|
})
|
|
->get()
|
|
->map(fn ($agendamento) => [
|
|
'agendamento_id' => $agendamento->id,
|
|
'agendamento_numero' => $agendamento->agendamento_numero,
|
|
'data' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
|
'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null,
|
|
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
|
->get()
|
|
->map(function ($query) use ($agendamento) {
|
|
return [
|
|
'id' => $query->id,
|
|
'exame_id' => $query->exame_id,
|
|
'nome' => $query->exame->nome,
|
|
'codigo' => $query->exame->codigo ?? null,
|
|
'valor' => $query->valor,
|
|
'status' => $query->status->descricao ?? 'AGENDADO',
|
|
'status_id' => $query->status_id,
|
|
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
|
'data_entrega' => (new Carbon($agendamento->data_coleta))
|
|
->addDays($query->exame->prazo_entrega)->format('d/m/Y') ?? null,
|
|
'observacao' => $query->exame->observacao ?? null
|
|
];
|
|
})
|
|
->values(),
|
|
'medico_id' => $agendamento->medico_id,
|
|
'medico_nome' => $agendamento->medico->nome,
|
|
'observacao' => $agendamento->observacao,
|
|
'paciente_id' => $agendamento->paciente_id,
|
|
'suspeita' => $agendamento->suspeita_clinica,
|
|
'unidade' => $agendamento->unidade->nome_fantasia,
|
|
'infraestrutura' => $agendamento->infraestrutura->nome,
|
|
'medicamentos' => $agendamento->uso_medicamento,
|
|
])
|
|
->sortBy('data')
|
|
->values(),
|
|
'realizados' => Agendamento::where('paciente_id', $id)
|
|
->whereHas('agendamentoExame', function($query) {
|
|
$query->where('status_id', StatusAgendamento::REALIZADO);
|
|
})
|
|
->get()
|
|
->map(fn ($agendamento) => [
|
|
'agendamento_id' => $agendamento->id,
|
|
'agendamento_numero' => $agendamento->agendamento_numero,
|
|
'data' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
|
'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null,
|
|
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
|
->get()
|
|
->map(function ($query) use ($agendamento) {
|
|
return [
|
|
'id' => $query->id,
|
|
'exame_id' => $query->exame_id,
|
|
'nome' => $query->exame->nome,
|
|
'codigo' => $query->exame->codigo ?? null,
|
|
'valor' => $query->valor,
|
|
'status' => self::getStatus($query),
|
|
'status_id' => $query->status_id,
|
|
'data_entrega' => (new Carbon($agendamento->data_coleta))
|
|
->addDays($query->exame->prazo_entrega)->format('d/m/Y') ?? null,
|
|
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
|
'observacao' => $query->exame->observacao ?? null
|
|
];
|
|
})
|
|
->values(),
|
|
'medico_id' => $agendamento->medico_id,
|
|
'medico_nome' => $agendamento->medico->nome,
|
|
'observacao' => $agendamento->observacao,
|
|
'paciente_id' => $agendamento->paciente_id,
|
|
'suspeita' => $agendamento->suspeita_clinica,
|
|
'unidade' => $agendamento->unidade->nome_fantasia,
|
|
'infraestrutura' => $agendamento->infraestrutura->nome,
|
|
'medicamentos' => $agendamento->uso_medicamento,
|
|
])
|
|
->sortBy('data')
|
|
->values(),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getStatus($query) {
|
|
|
|
if ($query->entregue) {
|
|
|
|
return 'ENTREGUE';
|
|
} else if ($query->impressao) {
|
|
|
|
return 'PENDENTE DE ENTREGA';
|
|
} else if ($query->status_id == StatusAgendamento::ASSINADO) {
|
|
|
|
return 'PENDENTE DE IMPRESSÃO';
|
|
} else if ($query->status_id == StatusAgendamento::REALIZADO) {
|
|
|
|
return 'COLETA REALIZADA';
|
|
}
|
|
|
|
return $query->status->descricao;
|
|
}
|
|
}
|