Files
rkm-conectasus-ci-sandbox/api/app/Services/ExameLaboratorial/RelatorioMapaDiaService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

383 lines
21 KiB
PHP

<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Enums\Geral\MedidaTempo;
use App\Enums\Parametrizacao\Exame\ComposicaoTipo;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
use App\Models\Intranet\Cliente;
use App\Models\Parametrizacao\Exame\ExameComposicao;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class RelatorioMapaDiaService extends Service
{
public static function index()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
if (!request()->exame_id || request()->exame_id && request()->pertence_grupo && request()->mapa_agrupado) {
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
'exame_laboratorial_agendamento_exame.status_id AS status_id',
'cadastro_grupo.id AS exame_grupo_id',
'cadastro_grupo.nome AS exame_grupo_nome',
DB::raw('true AS pertence_grupo'),
DB::raw('true AS mapa_agrupado'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
])
->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')
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->groupBy([
'cadastro_grupo.id',
'exame_laboratorial_agendamento.id',
]);
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
if (request()->periodo) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->unidade_id) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->exame_id) {
$agendamentosComGrupo->where('cadastro_grupo.id', request()->exame_id);
}
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('cadastro_grupo.nome', 'ASC');
$agendamentosComGrupo = $agendamentosComGrupo->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('parametrizacao_exame.nome', 'ASC');
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
} else {
$agendamentosComGrupo = [];
}
if (!request()->exame_id || request()->exame_id && request()->pertence_grupo && !request()->mapa_agrupado) {
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
'exame_laboratorial_agendamento_exame.status_id AS status_id',
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.nome AS exame_grupo_nome',
DB::raw('true AS pertence_grupo'),
DB::raw('false AS mapa_agrupado'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
])
->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')
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->groupBy([
'parametrizacao_exame.id',
'exame_laboratorial_agendamento.id',
]);
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.mapa_agrupado', false);
if (request()->periodo) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->unidade_id) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->exame_id) {
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', request()->exame_id);
}
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('cadastro_grupo.nome', 'ASC');
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('parametrizacao_exame.nome', 'ASC');
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
} else {
$agendamentosSemMapaAgrupado = [];
}
if (!request()->exame_id || request()->exame_id && !request()->pertence_grupo) {
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
'exame_laboratorial_agendamento_exame.status_id AS status_id',
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.nome AS exame_grupo_nome',
DB::raw('true AS pertence_grupo'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
])
->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')
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->whereNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'parametrizacao_exame.id',
'exame_laboratorial_agendamento.id',
]);
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
if (request()->periodo) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->unidade_id) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->exame_id) {
$agendamentosSemGrupo->where('parametrizacao_exame.id', request()->exame_id);
}
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy('parametrizacao_exame.nome', 'ASC');
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
} else {
$agendamentosSemGrupo = [];
}
$agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo))
->sortBy([
['agendamento_id', 'asc'],
['exame_grupo_nome', 'asc'],
])
->values();
if (request()->periodo) {
$perido_start = Carbon::create(request()->periodo['start'])->format('d/m/Y');
$perido_end = Carbon::create(request()->periodo['end'])->format('d/m/Y');
} else {
$perido_start = $currentDate;
$perido_end = $currentDate;
}
return [
'form' => [
'data_atual' => Carbon::now()->format('d/m/Y'),
'paciente_nome' => request()->paciente_nome ?? '-',
'agendamento_numero' => request()->agendamento_numero ?? '-',
'prontuario' => request()->prontuario ?? '-',
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
'infraestrutura_nome' => request()->infraestrutura_nome ?? '',
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
'periodo' => [
'start' => $perido_start,
'end' => $perido_end,
],
'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,
],
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
'agendamento_id' => $agendamento->agendamento_id,
'agendamento_numero' => $agendamento->agendamento_numero,
'exame_grupo_id' => $agendamento->exame_grupo_id,
'exame_grupo_nome' => $agendamento->exame_grupo_nome,
'paciente_idade' => $agendamento->paciente_idade,
'paciente_nome' => $agendamento->paciente_nome,
'paciente_nome_social' => $agendamento->paciente_nome_social,
'exames' => AgendamentoExame::where('agendamento_id', $agendamento->agendamento_id)
->whereHas('exame', function($query) use ($agendamento) {
if (request()->pertence_grupo && request()->mapa_agrupado) {
$query->where('grupo_tipo_id', $agendamento->exame_grupo_id);
} else if (request()->pertence_grupo && !request()->mapa_agrupado) {
$query->where('id', $agendamento->exame_grupo_id);
} else {
$query->where('id', $agendamento->exame_grupo_id);
}
})
->get()
->map(function ($query) use ($agendamento) {
return [
'id' => $query->id,
'exame_id' => $query->exame_id,
'nome' => mb_strtoupper($query->exame->nome),
'composicao' => self::getComposicao($agendamento, $query->exame_id)
];
})
->sortBy('nome')
->values(),
]),
],
];
}
private static function getComposicao($agendamento, $exameId)
{
return ExameComposicao::where('exame_id', $exameId)
->where('visivel', true)
->whereIn('tipo_resultado_id', [ComposicaoTipo::NUMERICO, ComposicaoTipo::TEXTO, ComposicaoTipo::TEXTO_NUMERO, ComposicaoTipo::CAIXA_TEXTO])
->where(function($query) use ($agendamento) {
$query->where('sexo_id', $agendamento->paciente_sexo_id)
->orWhereNull('sexo_id');
})
->where(function ($query) use ($agendamento) {
$query->whereNull('tipo_idade_id')
->orWhere(function ($idade) use ($agendamento) {
$idade
->where(function ($idade) use ($agendamento) {
$idade
->where('tipo_idade_id', MedidaTempo::DIAS)
->where(
'idade_inicial',
'<=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInDays(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInDays(Carbon::now()));
})
->orWhere(function ($idade) use ($agendamento) {
$idade
->where('tipo_idade_id', MedidaTempo::MESES)
->where(
'idade_inicial',
'<=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInMonths(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInMonths(Carbon::now()));
})
->orWhere(function ($idade) use ($agendamento) {
$idade
->where('tipo_idade_id', MedidaTempo::ANOS)
->where(
'idade_inicial',
'<=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInYears(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($agendamento->paciente_data_nascimento)->diffInYears(Carbon::now()));
});
});
})
->orderBy('descricao')
->get()
->map(function($query) {
return [
'descricao' => $query->descricao,
'tipo_resultado_id' => $query->tipo_resultado_id,
'resultado' => $query->resultado,
];
});
}
}