Files
rkm-conectasus-ci-sandbox/api/app/Services/ExameLaboratorial/MapaDiaService.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

369 lines
16 KiB
PHP

<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Enums\Parametrizacao\Exame\Tipo;
use App\Models\Cadastro\Estabelecimento\Unidade;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MapaDiaService extends Service
{
public static function create()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
'infraestrutura' => $currentLotacao->infraestrutura->nome,
'data_atual' => Carbon::now()->format('d/m/Y'),
'unidades' => Unidade::orderBy('nome_fantasia')
->get()
->map(fn ($query) => [
'id' => $query->id,
'nome_fantasia' => $query->nome_fantasia,
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
'id' => $infraestrutura->id,
'nome' => $infraestrutura->nome,
'principal' => $infraestrutura->principal,
])->values()
]),
],
];
}
public static function list(Request $request)
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'cadastro_grupo.id AS exame_grupo_id',
'cadastro_grupo.nome AS exame_grupo_nome',
DB::raw("COUNT(cadastro_grupo.id) AS quantidade"),
DB::raw('true AS pertence_grupo'),
DB::raw('true AS mapa_agrupado'),
DB::raw("
(
SELECT parametrizacao_exame_composicao.id
FROM parametrizacao_exame_composicao
WHERE parametrizacao_exame_composicao.visivel = TRUE
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
LIMIT 1
) AS composicao
"),
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'cadastro_grupo.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->get()->toArray();
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.nome AS exame_grupo_nome',
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
DB::raw('true AS pertence_grupo'),
DB::raw('false AS mapa_agrupado'),
DB::raw("
(
SELECT parametrizacao_exame_composicao.id
FROM parametrizacao_exame_composicao
WHERE parametrizacao_exame_composicao.visivel = TRUE
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
LIMIT 1
) AS composicao
"),
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'parametrizacao_exame.id',
]);
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
$agendamentosSemMapaAgrupado->where(function ($query) {
$query->where('parametrizacao_exame.mapa_agrupado', false)
->orWhereNull('parametrizacao_exame.mapa_agrupado');
});
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('cadastro_grupo.id', $request->exame_id);
}
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.nome AS exame_grupo_nome',
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
DB::raw('false AS pertence_grupo'),
DB::raw("
(
SELECT parametrizacao_exame_composicao.id
FROM parametrizacao_exame_composicao
WHERE parametrizacao_exame_composicao.visivel = TRUE
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
LIMIT 1
) AS composicao
"),
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
->whereNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'parametrizacao_exame.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->get()->toArray();
$agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo))
->sortBy([
['agendamento_id', 'asc'],
['exame_grupo_nome', 'asc'],
])
->values();
return [
'form' => [
'unidades' => Unidade::orderBy('nome_fantasia')
->get()
->map(fn ($query) => [
'id' => $query->id,
'nome_fantasia' => $query->nome_fantasia,
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
'id' => $infraestrutura->id,
'nome' => $infraestrutura->nome,
'principal' => $infraestrutura->principal,
])->values()
]),
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'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,
],
'model' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'agendamentos' => $agendamentos,
],
];
}
public static function loadFields(Request $request)
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$grupos = DB::table('parametrizacao_exame')
->select([
'cadastro_grupo.id AS id',
'cadastro_grupo.nome AS nome',
])
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
->whereExists(function ($query) use ($request, $currentDate) {
$query->select(DB::raw(1))
->from('exame_laboratorial_agendamento_exame')
->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
->where(function($query) use ($request, $currentDate) {
if ($request->periodo) {
$query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$query->where('exame_laboratorial_agendamento.data', $currentDate);
}
});
})
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
->groupBy([
'cadastro_grupo.id',
]);
$grupos = $grupos->get()->toArray();
$unidades = DB::table('cadastro_infraestrutura')
->select([
'cadastro_unidade.id AS unidade_id',
'cadastro_infraestrutura.id AS infraestrutura_id',
'cadastro_unidade.nome_fantasia as nome_fantasia',
DB::raw("
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
cadastro_infraestrutura.nome
ELSE
cadastro_unidade.nome_fantasia
END AS nome_infraestrutura
"),
'cadastro_infraestrutura.principal as principal'
])
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('exame_laboratorial_agendamento')
->whereColumn(
'exame_laboratorial_agendamento.infraestrutura_id',
'cadastro_infraestrutura.id'
)
->leftJoin('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
->where(function($query) {
$query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
});
})
->whereNull('cadastro_unidade.deleted_at')
->whereNull('cadastro_infraestrutura.deleted_at')
->distinct('infraestrutura_id');
$unidades = $unidades->get()->toArray();
$unidadeInfraestrutura = [];
foreach ($unidades as $item) {
$unidadeId = $item->unidade_id;
$infraestruturaId = $item->infraestrutura_id;
$nomeInfraestrutura = $item->nome_infraestrutura;
if (!isset($unidadeInfraestrutura[$unidadeId])) {
$unidadeInfraestrutura[$unidadeId] = [
'id' => $unidadeId,
'nome_fantasia' => $item->nome_fantasia,
'infraestruturas' => []
];
}
$unidadeInfraestrutura[$unidadeId]['infraestruturas'][] = [
'id' => $infraestruturaId,
'nome' => $nomeInfraestrutura,
'principal' => $item->principal
];
}
usort($unidadeInfraestrutura, function ($a, $b) {
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
});
return [
'unidades' => $unidadeInfraestrutura,
'grupos' => $grupos,
];
}
}