format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia', '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', 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("SUM(parametrizacao_exame.quantidade_reagente) AS quantidade_reagente"), 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([ 'parametrizacao_exame.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, ] ); }); $agendamentos = $agendamentos->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) => [ 'coleta_nome_fantasia' => $agendamento->coleta_nome_fantasia, 'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome, 'laboratorio_nome_fantasia' => $agendamento->laboratorio_nome_fantasia, 'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome, 'exame_nome' => $agendamento->exame_nome, 'procedimento_codigo' => !empty($agendamento->procedimento_codigo) ? $agendamento->procedimento_codigo : '-', 'quantidade_reagente' => $agendamento->quantidade_reagente ?? '0', 'quantidade' => $agendamento->quantidade, ]), ], ]; } public static function loadFields(Request $request) { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $exames = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'), DB::raw('upper(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); } $exames->where(function($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, ] ); }); $exames = $exames->orderBy('exame_nome')->get(); $unidades = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS unidade_coleta_id', 'cadastro_unidade_coleta.nome_fantasia AS unidade_coleta_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'), ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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' ) ->groupBy([ 'exame_laboratorial_agendamento.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); } $unidades->where(function($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, ] ); }); $unidades = $unidades->get(); $laboratorios = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'cadastro_unidade_laboratorio.nome_fantasia AS unidade_laboratorio_nome', 'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_unidade_id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id', DB::raw(' CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END AS laboratorio_infraestrutura_nome'), ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_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' ) ->groupBy([ 'exame_laboratorial_agendamento.id', ]); if ($request->periodo) { $laboratorios->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']); $laboratorios->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']); } else { $laboratorios->where('exame_laboratorial_agendamento.data', $currentDate); } if ($request->exame_id) { $laboratorios->where('exame_laboratorial_agendamento_exame.exame_id', $request->exame_id); } if ($request->coleta_unidade_id) { $laboratorios->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id); } if ($request->coleta_infraestrutura_id) { $laboratorios->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id); } $laboratorios->where(function($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, ] ); }); $laboratorios = $laboratorios->get(); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), ], 'model' => [ 'unidades' => $unidades->map(fn ($unidade) => [ 'coleta_unidade_id' => $unidade->unidade_coleta_id, 'coleta_unidade_nome' => $unidade->unidade_coleta_nome, 'coleta_infraestrutura_id' => $unidade->coleta_infraestrutura_id, 'coleta_infraestrutura_nome' => $unidade->coleta_infraestrutura_nome, ]), 'laboratorios' => $laboratorios->map(fn ($laboratorio) => [ 'laboratorio_id' => $laboratorio->laboratorio_unidade_id, 'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome, 'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id, 'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome, ]), 'exames' => $exames->map(fn ($exame) => [ 'id' => $exame->exame_id, 'nome' => mb_strtoupper($exame->exame_nome), ]), ], ]; } }