274 lines
15 KiB
PHP
274 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
|
use App\Services\Service;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ExameAgendadoRealizadoService extends Service
|
|
{
|
|
|
|
public static function list(Request $request)
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
|
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
|
DB::raw("
|
|
(
|
|
SELECT GROUP_CONCAT(cadastro_cidadao_telefone.numero SEPARATOR ', ')
|
|
FROM cadastro_cidadao_telefone
|
|
WHERE cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
|
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
|
) AS telefone
|
|
"),
|
|
DB::raw('CONCAT(
|
|
cadastro_municipio.nome,
|
|
" - ",
|
|
cadastro_estado.uf
|
|
) AS municipio'),
|
|
])
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
|
->join('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
|
->join('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->leftJoin('cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id')
|
|
->leftJoin('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id')
|
|
->leftJoin('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
|
->leftJoin('intranet.cadastro_municipio', 'cadastro_municipio.id', 'cadastro_logradouro.municipio_id')
|
|
->leftJoin('intranet.cadastro_estado', 'cadastro_estado.id', 'cadastro_municipio.estado_id')
|
|
->groupBy([
|
|
'cadastro_paciente.id',
|
|
])
|
|
->limit(10);
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
|
}
|
|
|
|
if ($request->data_nascimento) {
|
|
|
|
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
|
}
|
|
|
|
$agendamentos = $agendamentos->get();
|
|
|
|
return [
|
|
'form' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
],
|
|
'model' => [
|
|
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
|
'data_nascimento' => $agendamento->data_nascimento ?? null,
|
|
'nome_mae' => $agendamento->nome_mae ?? null,
|
|
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
|
'paciente_id' => $agendamento->paciente_id ?? null,
|
|
'paciente_preferencia_nome_id' => $agendamento->paciente_preferencia_nome_id ?? null,
|
|
'paciente_nome_social' => $agendamento->paciente_nome_social ?? null,
|
|
'paciente_prontuario' => $agendamento->paciente_prontuario ?? null,
|
|
'assinatura_profissional_id' => $agendamento->assinatura_profissional_id ?? null,
|
|
'municipio' => $agendamento->municipio ?? null,
|
|
'telefones' => Paciente::findOrFail($agendamento->paciente_id)->cidadao->telefones
|
|
->map(fn ($t) => MaskUtil::getPhoneMasked($t->numero))->all()
|
|
]),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
|
|
$agendamento = Agendamento::where('paciente_id', $id)->firstOrFail();
|
|
|
|
return [
|
|
'form' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'paciente' => [
|
|
'nome' => $agendamento->paciente->cidadao->nome ?? null,
|
|
'nomeSocial' => $agendamento->paciente->cidadao->nome_social,
|
|
'prefereNomeSocial' => $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) : '',
|
|
'telefones' => $agendamento->paciente->cidadao->telefones
|
|
->map(fn ($t) => MaskUtil::getPhoneMasked($t->numero))->all()
|
|
],
|
|
'endereco' => [
|
|
'estado_id' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id
|
|
: null,
|
|
'municipio' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio
|
|
: null,
|
|
'logradouro' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro
|
|
: null,
|
|
'cep' => MaskUtil::getCepMasked(
|
|
isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep
|
|
: null
|
|
),
|
|
'numero' => isset($agendamento->paciente->cidadao->domicilio->endereco->numero)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->numero
|
|
: null,
|
|
'sem_numero' => isset($agendamento->paciente->cidadao->domicilio->endereco->sem_numero)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->sem_numero
|
|
: null,
|
|
'complemento' => isset($agendamento->paciente->cidadao->domicilio->endereco->complemento)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->complemento
|
|
: '',
|
|
'zona_id' => isset($agendamento->paciente->cidadao->domicilio->endereco->zona_id)
|
|
? $agendamento->paciente->cidadao->domicilio->endereco->zona_id
|
|
: null,
|
|
],
|
|
],
|
|
'model' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'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->created_at))->format('d/m/Y') ?? null,
|
|
'data_coleta' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
|
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
|
->get()
|
|
->map(function ($query) {
|
|
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,
|
|
'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,
|
|
'laboratorioUnidade' => $agendamento->laboratorioUnidade->nome_fantasia,
|
|
'infraestrutura' => $agendamento->infraestrutura->nome,
|
|
'laboratorioInfraestrutura' => $agendamento->laboratorioInfraestrutura->nome,
|
|
'medicamentos' => $agendamento->uso_medicamento,
|
|
])
|
|
->sortBy('data')
|
|
->values(),
|
|
'finalizados' => Agendamento::where('paciente_id', $id)
|
|
->whereHas('agendamentoExame', function($query) {
|
|
|
|
$query
|
|
->whereNotIn('status_id', [StatusAgendamento::AGENDADO])
|
|
->whereNotNull('status_id');
|
|
})
|
|
->get()
|
|
->map(fn ($agendamento) => [
|
|
'agendamento_id' => $agendamento->id,
|
|
'agendamento_numero' => $agendamento->agendamento_numero,
|
|
'data' => (new Carbon($agendamento->created_at))->format('d/m/Y') ?? null,
|
|
'data_coleta' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
|
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
|
->get()
|
|
->map(function ($query) {
|
|
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,
|
|
'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,
|
|
'laboratorioUnidade' => $agendamento->laboratorioUnidade->nome_fantasia,
|
|
'infraestrutura' => $agendamento->infraestrutura->nome,
|
|
'laboratorioInfraestrutura' => $agendamento->laboratorioInfraestrutura->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 ?? null;
|
|
}
|
|
}
|