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) { $agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'exame_laboratorial_agendamento.data AS data', 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'cadastro_unidade.nome_fantasia AS unidade_nome', 'cadastro_cidadao_medico.nome AS medico_nome', '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('group_concat(parametrizacao_exame.sigla_exame) as sigla_exame'), 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('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'), DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'), DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'), DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'), DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome '), DB::raw('CONCAT( cadastro_unidade_coleta.nome_fantasia, " - ", 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('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_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_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') ->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') ->groupBy([ 'exame_laboratorial_agendamento.id', 'cadastro_grupo.id', ]); $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->paciente_id) { $agendamentosComGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id); } if($request->status_id){ $agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id); } if ($request->agendamento_numero) { $agendamentosComGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero); } 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->coleta_unidade_id) { $agendamentosComGrupo->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id); } if ($request->coleta_infraestrutura_id) { $agendamentosComGrupo->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id); } if ($request->exame_id && $request->pertence_grupo) { $agendamentosComGrupo->where('cadastro_grupo.id', $request->exame_id); } $agendamentosComGrupo->where(function($status) { $status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU) ->orWhereNull('exame_laboratorial_agendamento_exame.status_id'); }); $agendamentosComGrupo = $agendamentosComGrupo->get()->toArray(); } else { $agendamentosComGrupo = []; } if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) { $agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'exame_laboratorial_agendamento.data AS data', 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'cadastro_unidade.nome_fantasia AS unidade_nome', 'cadastro_cidadao_medico.nome AS medico_nome', '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('group_concat(parametrizacao_exame.sigla_exame) as sigla_exame'), 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('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'), DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'), DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'), DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'), DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome '), DB::raw('CONCAT( cadastro_unidade_coleta.nome_fantasia, " - ", 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('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_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_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') ->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') ->groupBy([ 'exame_laboratorial_agendamento.id', 'parametrizacao_exame.id', ]); $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->paciente_id) { $agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id); } if($request->status_id){ $agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id); } if ($request->agendamento_numero) { $agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero); } 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->coleta_unidade_id) { $agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id); } if ($request->coleta_infraestrutura_id) { $agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id); } if ($request->exame_id && $request->pertence_grupo) { $agendamentosSemMapaAgrupado->where('cadastro_grupo.id', $request->exame_id); } $agendamentosSemMapaAgrupado->where(function($status) { $status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU) ->orWhereNull('exame_laboratorial_agendamento_exame.status_id'); }); $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', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'exame_laboratorial_agendamento.data AS data', 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'cadastro_unidade.nome_fantasia AS unidade_nome', 'cadastro_cidadao_medico.nome AS medico_nome', 'exame_laboratorial_agendamento.unidade_id AS unidade_id', 'parametrizacao_exame.id AS exame_grupo_id', 'parametrizacao_exame.nome AS exame_grupo_nome', 'parametrizacao_exame.sigla_exame AS sigla_exame', 'exame_laboratorial_agendamento_exame.status_id AS status_id', DB::raw('false 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('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'), DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'), DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'), DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'), DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome '), DB::raw('CONCAT( cadastro_unidade_coleta.nome_fantasia, " - ", 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('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_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_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') ->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') ->whereNull('parametrizacao_exame.grupo_tipo_id'); 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->paciente_id) { $agendamentosSemGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id); } if($request->status_id){ $agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id); } if ($request->agendamento_numero) { $agendamentosSemGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero); } 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->coleta_unidade_id) { $agendamentosSemGrupo->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id); } if ($request->coleta_infraestrutura_id) { $agendamentosSemGrupo->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id); } if ($request->exame_id && !$request->pertence_grupo) { $agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id); } $agendamentosSemGrupo->where(function($status) { $status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU) ->orWhereNull('exame_laboratorial_agendamento_exame.status_id'); }); $agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray(); } else { $agendamentosSemGrupo = []; } 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; } $agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo)) ->sortBy([ ['agendamento_id', 'asc'], ['exame_grupo_nome', 'asc'], ]) ->values(); 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 ?? 'TODOS', 'coleta_unidade_nome' => $request->coleta_unidade_nome ?? 'TODAS', 'coleta_infraestrutura_nome' => $request->coleta_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, 'agendado' => self::getTotal($agendamentos, StatusAgendamento::AGENDADO), 'duplicidade' => self::getTotal($agendamentos, StatusAgendamento::DUPLICIDADE), 'realizado' => self::getTotal($agendamentos, StatusAgendamento::REALIZADO), 'outros' => self::getTotal($agendamentos, StatusAgendamento::OUTROS), 'faltou' => self::getTotal($agendamentos, StatusAgendamento::FALTOU), 'sistema' => [ 'ramo_atividade' => [ 'nome' => $cliente->sistema->ramoAtividade->nome ?? null, ], 'nome' => $cliente->sistema->nome ?? null, 'versao' => $cliente->sistema->versao ?? null, ], 'agendamentos' => $agendamentos, ], ]; } public static function getTotal($agendamentos, $status_id) { $total = 0; foreach ($agendamentos as $agendamento) { if ($agendamento->status_id == $status_id) { $total += 1; } } return $total; } }