format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ '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', DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'), DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'), 'procedimento.codigo as procedimento_codigo', 'procedimento.nome AS procedimento_nome', 'parametrizacao_exame.nome AS exame_nome', 'exame_laboratorial_agendamento_exame.status_id AS status_id', 'exame_laboratorial_agendamento_exame_status.descricao AS status_nome', 'cadastro_unidade.nome_fantasia AS nome_fantasia', DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome'), ]) ->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') ->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); }); }) ->groupBy([ 'cadastro_paciente.id', 'exame_laboratorial_agendamento_exame.exame_id', 'exame_laboratorial_agendamento_exame.status_id', ]); if (request()->paciente_id) { $agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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()->unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id); } if (request()->status_id) { $agendamentos->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) { $query ->orWhereIn('status_id', [ StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE ] ); } else { $query->orWhere('status_id', $status); } } }); } $agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->get(); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], '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() ]), 'unidades_coleta' => ExameLaboratorialUnidade::where('unidade_coleta', true) ->get() ->map(function($query) { return [ 'id' => $query->id ?? null, 'unidade_id' => $query->unidade_id ?? null, 'infraestrutura_id' => $query->infraestrutura_id ?? null, 'infraestrutura_nome' => $query->infraestrutura->nome, 'infraestrutura_principal' => $query->infraestrutura->principal, 'nome_fantasia' => $query->unidade->nome_fantasia ?? null, 'unidade_coleta' => $query->unidade_coleta ?? null, ]; }), 'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia, 'unidade_id' => $currentLotacao->infraestrutura->unidade->id, 'infraestrutura_id' => $currentLotacao->infraestrutura->id, 'infraestrutura' => $currentLotacao->infraestrutura->nome, ], 'model' => [ 'agendamentos' => $agendamentos->map(fn ($agendamento) => [ 'paciente_nome' => $agendamento->paciente_nome ?? null, 'paciente_nome_social' => $agendamento->paciente_nome_social, 'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL, 'paciente_prontuario' => $agendamento->paciente_prontuario, 'paciente_nascimento' => $agendamento->paciente_nascimento, 'data_agendamento' => $agendamento->data, 'exame_nome' => mb_strtoupper($agendamento->exame_nome), 'procedimento' => $agendamento->procedimento_codigo ? $agendamento->procedimento_codigo . ' - ' . $agendamento->procedimento_nome : 'SEM PROCEDIMENTO', 'status' => $agendamento->status_id ? self::getStatus($agendamento) : 'AGENDADO', 'quantidade' => $agendamento->quantidade, 'unidade' => $agendamento->nome_fantasia, 'infraestrutura_nome' => $agendamento->infraestrutura_nome, ]), 'total' => self::getTotal()->quantidade, ], ]; } public static function loadFields() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), ], 'model' => [ 'unidades' => self::getUnidades()->map(fn ($unidade) => [ 'unidade_id' => $unidade->unidade_id, 'unidade_nome' => $unidade->unidade, 'infraestrutura_id' => $unidade->infraestrutura_id, 'infraestrutura_nome' => $unidade->infraestrutura_nome, ]), 'exames' => self::getExames()->map(fn ($exame) => [ 'id' => $exame->exame_id, 'nome' => mb_strtoupper($exame->exame_nome), ]), ], ]; } public static function getUnidades() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $unidades = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'exame_laboratorial_agendamento.unidade_id AS unidade_id', 'cadastro_unidade.nome_fantasia AS unidade', DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome'), ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->groupBy([ 'cadastro_infraestrutura.unidade_id', ]); if (request()->periodo) { $unidades->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $unidades->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $unidades->where('exame_laboratorial_agendamento.data', $currentDate); } if (request()->exame_id) { $unidades->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id); } if (request()->paciente_id) { $unidades->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id); } if (request()->status_id) { $unidades->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) { $query ->orWhereIn('status_id', [ StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE ] ); } else { $query->orWhere('status_id', $status); } } }); } return $unidades->get(); } public static function getExames() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $exames = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'parametrizacao_exame.nome AS exame_nome', 'parametrizacao_exame.id AS exame_id', ]) ->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' ) ->groupBy([ 'exame_laboratorial_agendamento_exame.exame_id', ]); if (request()->periodo) { $exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $exames->where('exame_laboratorial_agendamento.data', $currentDate); } return $exames->get(); } public static function getStatus($query) { if ($query->status_id == StatusAgendamento::REALIZADO) { return 'COLETA REALIZADA'; } else if ($query->status_id == StatusAgendamento::PENDENTE_ASSINATURA || $query->status_id == StatusAgendamento::ASSINADO || $query->status_id == StatusAgendamento::PENDENTE_ENTREGA || $query->status_id == StatusAgendamento::ENTREGUE) { return 'RESULTADO LANÇADO'; } return $query->status_nome; } public static function getTotal() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $total = DB::table('exame_laboratorial_agendamento_exame') ->select([ DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'), ]) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id'); if (request()->paciente_id) { $total->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id); } if (request()->periodo) { $total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $total->where('exame_laboratorial_agendamento.data', $currentDate); } if (request()->exame_id) { $total->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id); } if (request()->unidade_id) { $total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id); } if (request()->status_id) { $total->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) { $query ->orWhereIn('status_id', [ StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE ] ); } else { $query->orWhere('status_id', $status); } } }); } return $total->first(); } 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(); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ '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', DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'), DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'), 'procedimento.codigo as procedimento_codigo', 'procedimento.nome AS procedimento_nome', 'parametrizacao_exame.nome AS exame_nome', 'exame_laboratorial_agendamento_exame.status_id AS status_id', 'exame_laboratorial_agendamento_exame_status.descricao AS status_nome', 'cadastro_unidade.nome_fantasia AS nome_fantasia', DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome'), ]) ->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') ->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); }); }) ->groupBy([ 'cadastro_paciente.id', 'exame_laboratorial_agendamento_exame.exame_id', 'exame_laboratorial_agendamento_exame.status_id', ]); if (request()->paciente_id) { $agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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()->unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id); } if (request()->status_id) { $agendamentos->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) { $query ->orWhereIn('status_id', [ StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE ] ); } else { $query->orWhere('status_id', $status); } } }); } $agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->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 [ 'data_atual' => Carbon::now()->format('d/m/Y'), 'paciente_nome' => request()->paciente_nome ?? '-', 'agendamento_numero' => request()->agendamento_id ?? '-', 'prontuario' => request()->prontuario ?? '-', 'unidade_nome' => request()->unidade_nome ?? 'TODAS', 'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS', 'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS', '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, ], 'periodo' => [ 'start' => $periodoStartFilter, 'end' => $periodoEndFilter, ], 'status' => collect(request()->status_selecionado), 'colunas' => collect(request()->selectedCols), 'agendamentos' => $agendamentos->map(fn ($agendamento) => [ 'paciente_nome' => $agendamento->paciente_nome ?? null, 'paciente_nome_social' => $agendamento->paciente_nome_social, 'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL, 'paciente_prontuario' => $agendamento->paciente_prontuario, 'paciente_nascimento' => $agendamento->paciente_nascimento, 'data_agendamento' => $agendamento->data, 'exame_nome' => mb_strtoupper($agendamento->exame_nome), 'procedimento' => $agendamento->procedimento_codigo ? $agendamento->procedimento_codigo . ' - ' . $agendamento->procedimento_nome : 'SEM PROCEDIMENTO', 'status' => $agendamento->status_id ? self::getStatus($agendamento) : 'AGENDADO', 'quantidade' => $agendamento->quantidade, 'unidade' => $agendamento->nome_fantasia, 'infraestrutura_nome' => $agendamento->infraestrutura_nome, ]), 'total' => self::getTotal()->quantidade, ]; } }