format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); return [ 'form' => [ '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, ], ]; } public static function list() { $now = Carbon::now(); $inicio = request()->periodo['start'] ?? $now->format('Y-m-d'); $fim = request()->periodo['end'] ?? $now->format('Y-m-d'); $periodo = [$inicio, $fim]; $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia', 'cadastro_unidade_coleta.id AS unidade_id_coleta', 'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia', 'procedimento.codigo as procedimento_codigo', 'procedimento.nome AS procedimento_nome', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento_exame.exame_id', 'exame_laboratorial_agendamento_exame_status.descricao as status', 'cadastro_cidadao.nome as profissional', 'ocupacao.codigo as ocupacao_codigo', 'ocupacao.nome as ocupacao_nome', DB::raw(' CASE WHEN cadastro_infraestrutura_coleta.principal THEN cadastro_unidade_coleta.nome_fantasia ELSE cadastro_infraestrutura_coleta.nome END AS coleta_infraestrutura_nome'), DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'), DB::raw( ' CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END AS laboratorio_infraestrutura_nome' ), DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"), ]) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->join('cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id') ->join('cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id') ->join('cadastro_infraestrutura as cadastro_infraestrutura_laboratorio', 'cadastro_infraestrutura_laboratorio.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id') ->join('cadastro_unidade as cadastro_unidade_laboratorio', 'cadastro_unidade_laboratorio.id', 'cadastro_infraestrutura_laboratorio.unidade_id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->join('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 as cidadao_paciente', 'cidadao_paciente.id', 'cadastro_paciente.cidadao_id') ->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id') ->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id') ->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id') ->leftJoin('sigtap.procedimento', function ($join) { $join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo') ->where('sigtap.procedimento.competencia_id', '=', function ($query) { $query->select('sigtap.competencia.id') ->from('sigtap.competencia') ->orderByDesc('sigtap.competencia.valor') ->limit(1); }); }) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('cadastro_unidade_coleta.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura_coleta.id', $id)) ->when(request()->laboratorio_id, fn ($q, $id) => $q->where('cadastro_unidade_laboratorio.id', $id)) ->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura_laboratorio.id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id)) ->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id)) ->when(request()->status, fn ($q, $id) => $q->where('exame_laboratorial_agendamento_exame.status_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id)) ->when(request()->idade_de, fn ($q, $idadeDe) => $q ->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cidadao_paciente.data_nascimento, CURRENT_DATE) >= " . $idadeDe)) ) ->when(request()->idade_ate, fn ($q, $idadeAte) => $q ->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cidadao_paciente.data_nascimento, CURRENT_DATE) <= " . $idadeAte)) ) ->groupBy([ 'exame_laboratorial_agendamento.coleta_infraestrutura_id', 'exame_laboratorial_agendamento_exame.exame_id', 'cadastro_lotacao.usuario_id', 'exame_laboratorial_agendamento_exame_status.descricao', ]); self::setOrderBy($agendamentos); $agendamentos = $agendamentos->get(); $resultadoAgrupado = []; foreach ($agendamentos as $item) { $unidadeId = $item->coleta_infraestrutura_id; if (!isset($resultadoAgrupado[$unidadeId])) { $resultadoAgrupado[$unidadeId] = [ 'id' => $unidadeId, 'coleta_nome_fantasia' => $item->coleta_nome_fantasia, 'coleta_infraestrutura_nome' => $item->coleta_infraestrutura_nome, 'laboratorio_nome_fantasia' => $item->laboratorio_nome_fantasia, 'laboratorio_infraestrutura_nome' => $item->laboratorio_infraestrutura_nome, 'exames' => [], 'total' => null, ]; } $resultadoAgrupado[$unidadeId]['total'] = $resultadoAgrupado[$unidadeId]['total'] + $item->quantidade; $resultadoAgrupado[$unidadeId]['exames'][] = [ 'id' => $item->exame_id, 'nome' => $item->exame_nome, 'procedimento_codigo' => $item->procedimento_codigo ?? '-', 'procedimento_nome' => $item->procedimento_nome ?? '-', 'status' => $item->status, 'profissional' => $item->profissional ?? '-', 'quantidade' => $item->quantidade, 'ocupacao_nome' => $item->ocupacao_nome ?? '-', 'ocupacao_codigo' => $item->ocupacao_codigo ?? '-', ]; } usort($resultadoAgrupado, function ($a, $b) { return strcmp($a['coleta_nome_fantasia'], $b['coleta_nome_fantasia']); }); return [ 'model' => [ 'unidade_coleta' => $resultadoAgrupado, ], ]; } public static function loadFields() { $now = Carbon::now(); $inicio = request()->periodo['start'] ?? $now->format('Y-m-d'); $fim = request()->periodo['end'] ?? $now->format('Y-m-d'); $periodo = [$inicio, $fim]; return [ 'local_coleta' => self::getLocalColeta($periodo), 'laboratorios' => self::getLaboratorio($periodo), 'exames' => self::getExames($periodo), 'status' => self::getStatus($periodo), 'ocupacoes' => self::getOcupacao($periodo), 'profissionais' => self::getProfissional($periodo), ]; } private static function getStatus($periodo) { return DB::table('exame_laboratorial_agendamento_exame_status') ->select( 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame_status.descricao', ) ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.status_id', 'exame_laboratorial_agendamento_exame_status.id') ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id') ->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id') ->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id') ->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id') ->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->join('sigtap.procedimento', function ($join) { $join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo') ->where('sigtap.procedimento.competencia_id', '=', function ($query) { $query->select('sigtap.competencia.id') ->from('sigtap.competencia') ->orderByDesc('sigtap.competencia.valor') ->limit(1); }); }) ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id)) ->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id)) ->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id)) ->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id)) ->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('sigtap.procedimento.codigo', $id)) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('id','descricao') ->orderBy('descricao') ->get(); } public static function autocompleteProcedimento() { $now = Carbon::now(); $inicio = request()->periodo['start'] ?? $now->format('Y-m-d'); $fim = request()->periodo['end'] ?? $now->format('Y-m-d'); $periodo = [$inicio, $fim]; return DB::table('parametrizacao_exame') ->select( 'sigtap.procedimento.codigo as codigo', 'sigtap.procedimento.nome as nome', ) ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id') ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id') ->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id') ->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id') ->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id') ->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id') ->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->join('sigtap.procedimento', function ($join) { $join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo') ->where('sigtap.procedimento.competencia_id', '=', function ($query) { $query->select('sigtap.competencia.id') ->from('sigtap.competencia') ->orderByDesc('sigtap.competencia.valor') ->limit(1); }); }) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id)) ->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id)) ->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id)) ->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id)) ->distinct('codigo','nome') ->orderBy('nome'); } private static function getOcupacao($periodo) { return DB::table('cadastro_lotacao') ->select( 'ocupacao.id', 'ocupacao.nome', ) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.lotacao_id', 'cadastro_lotacao.id') ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->join('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id') ->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id') ->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id') ->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id)) ->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id)) ->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id)) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('nome') ->orderBy('nome') ->get(); } private static function getProfissional($periodo) { return DB::table('cadastro_usuario') ->select( 'cadastro_usuario.id', 'cadastro_cidadao.nome', ) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.usuario_id', 'cadastro_usuario.id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id') ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id') ->join('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id') ->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id') ->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id') ->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id)) ->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id)) ->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id)) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('nome') ->orderBy('nome') ->get(); } private static function getExames($periodo) { return DB::table('parametrizacao_exame') ->select( 'parametrizacao_exame.id', 'parametrizacao_exame.nome', ) ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id') ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('id','nome') ->orderBy('nome') ->get(); } private static function getLaboratorio($periodo) { $unidade = 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') ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id', 'cadastro_infraestrutura.id') ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id') ->whereNull('cadastro_infraestrutura.deleted_at') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id)) ->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id)) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('infraestrutura_id') ->orderBy('nome_fantasia') ->get(); $resultadoAgrupado = []; foreach ($unidade as $item) { $unidadeId = $item->unidade_id; $infraestruturaId = $item->infraestrutura_id; $nomeInfraestrutura = $item->nome_infraestrutura; if (!isset($resultadoAgrupado[$unidadeId])) { $resultadoAgrupado[$unidadeId] = [ 'id' => $unidadeId, 'nome_fantasia' => $item->nome_fantasia, 'infraestruturas' => [] ]; } $resultadoAgrupado[$unidadeId]['infraestruturas'][] = [ 'id' => $infraestruturaId, 'nome' => $nomeInfraestrutura, 'principal' => $item->principal ]; } usort($resultadoAgrupado, function ($a, $b) { return strcmp($a['nome_fantasia'], $b['nome_fantasia']); }); return $resultadoAgrupado; } private static function getLocalColeta($periodo) { $unidade = 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') ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.infraestrutura_id', 'cadastro_infraestrutura.id') ->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->whereNull('cadastro_infraestrutura.deleted_at') ->whereBetween('exame_laboratorial_agendamento.data', $periodo) ->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id)) ->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }) ->distinct('infraestrutura_id') ->orderBy('nome_fantasia') ->get(); $resultadoAgrupado = []; foreach ($unidade as $item) { $unidadeId = $item->unidade_id; $infraestruturaId = $item->infraestrutura_id; $nomeInfraestrutura = $item->nome_infraestrutura; if (!isset($resultadoAgrupado[$unidadeId])) { $resultadoAgrupado[$unidadeId] = [ 'id' => $unidadeId, 'nome_fantasia' => $item->nome_fantasia, 'infraestruturas' => [] ]; } $resultadoAgrupado[$unidadeId]['infraestruturas'][] = [ 'id' => $infraestruturaId, 'nome' => $nomeInfraestrutura, 'principal' => $item->principal ]; } usort($resultadoAgrupado, function ($a, $b) { return strcmp($a['nome_fantasia'], $b['nome_fantasia']); }); return $resultadoAgrupado; } public static function index() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia', 'cadastro_unidade_coleta.id AS unidade_id_coleta', 'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia', 'parametrizacao_exame.codigo AS procedimento_codigo', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento_exame.exame_id', DB::raw(' CASE WHEN cadastro_infraestrutura_coleta.principal THEN cadastro_unidade_coleta.nome_fantasia ELSE cadastro_infraestrutura_coleta.nome END AS coleta_infraestrutura_nome'), DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'), DB::raw( ' CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END AS laboratorio_infraestrutura_nome' ), DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"), ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id' ) ->join( 'cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id' ) ->join( 'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio', 'cadastro_infraestrutura_laboratorio.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id' ) ->join( 'cadastro_unidade as cadastro_unidade_laboratorio', 'cadastro_unidade_laboratorio.id', 'cadastro_infraestrutura_laboratorio.unidade_id' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id' ) ->groupBy([ 'exame_laboratorial_agendamento.unidade_id', ]); if (request()->periodo) { $agendamentos->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $agendamentos->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $agendamentos->where('exame_laboratorial_agendamento.data', $currentDate); } if (request()->exame_id) { $agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id); } if (request()->coleta_unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } $agendamentos->where(function ($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, StatusAgendamento::NOVA_COLETA ] ); }); $agendamentos = $agendamentos->get(); if (request()->periodo) { $periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y'); $periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y'); } else { $periodoStartFilter = $currentDate; $periodoEndFilter = $currentDate; } return [ 'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS', 'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS', 'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS', 'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS', 'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS', 'ocupacao' => request()->ocupacao_nome ?? 'TODAS', 'profissional' => request()->profissional_nome ?? 'TODOS', 'status' => request()->status_nome ?? 'TODOS', 'procedimento' => request()->procedimento_codigo ?? 'TODOS', 'idade_de' => request()->idade_de ?? '-', 'idade_ate' => request()->idade_ate ?? '-', 'periodo' => [ 'start' => $periodoStartFilter, 'end' => $periodoEndFilter, ], 'colunas' => collect(request()->selectedCols), 'unidade_coleta' => self::list()['model']['unidade_coleta'] ]; } private static function setOrderBy($builder) { $columns = [ 'nome' => 'exame_nome', 'procedimento_codigo' => 'procedimento.codigo', 'procedimento_nome' => 'procedimento.nome', 'status' => 'exame_laboratorial_agendamento_exame_status.descricao', 'profissional' => 'cadastro_cidadao.nome', 'ocupacao_codigo' => 'ocupacao.codigo', 'ocupacao_nome' => 'ocupacao.nome', 'quantidade' => DB::raw("COUNT(parametrizacao_exame.id)"), ]; if (is_null(request()->sortBy)) { $builder->orderByRaw(join(', ', $columns)); return; } $column = $columns[request()->sortBy]; $direction = DataUtil::getBooleanValueFromStringBoolean(request()->sortDesc) ? 'DESC' : 'ASC'; unset($columns[request()->sortBy]); $builder->orderByRaw( "{$column} {$direction}, " . join(', ', $columns) ); } }