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([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', 'exame_laboratorial_agendamento.observacao AS observacao', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', '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', 'parametrizacao_exame.nome AS exame_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', 'cadastro_paciente.prontuario AS paciente_prontuario', DB::raw('CASE WHEN cadastro_cidadao.sexo_id = '. Sexo::MASCULINO .' THEN "M" ELSE "F" END AS paciente_sexo_descricao'), 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('CONCAT( cadastro_unidade.nome_fantasia, " - ", 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_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', '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') ->groupBy([ 'exame_laboratorial_agendamento.id', 'cadastro_grupo.id', 'parametrizacao_exame.id' ]); $agendamentos->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO); $agendamentos->where('cadastro_grupo.impressao_mapa_producao', true); $agendamentos->where('parametrizacao_exame.mapa_agrupado', true); 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->unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id); } if ($request->agendamento_id) { $agendamentos->where('exame_laboratorial_agendamento.id', $request->agendamento_id); } if ($request->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id); } if ($request->exame_id) { $agendamentos->where('cadastro_grupo.id', $request->exame_id); } if ($request->boolean('impressao_pendente')) { $agendamentos->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false); } $agendamentos = $agendamentos->orderBy('cadastro_grupo.nome', 'ASC'); $agendamentos = $agendamentos->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC'); $agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC'); $agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC'); $agendamentos = $agendamentos->get()->toArray(); $agendamentosSiglas = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', DB::raw('group_concat(parametrizacao_exame.sigla_exame ORDER BY parametrizacao_exame.sigla_exame ASC separator ", ") as sigla_exame'), ]) ->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.id', ]); $agendamentosSiglas->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO); if ($request->periodo) { $agendamentosSiglas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']); $agendamentosSiglas->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']); } else { $agendamentosSiglas->where('exame_laboratorial_agendamento.data', $currentDate); } if ($request->unidade_id) { $agendamentosSiglas->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id); } if ($request->infraestrutura_id) { $agendamentosSiglas->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id); } if ($request->boolean('impressao_pendente')) { $agendamentosSiglas->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false); } $agendamentosSiglas = $agendamentosSiglas->get()->toArray(); 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; } 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 ?? '', '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, 'sistema' => [ 'ramo_atividade' => [ 'nome' => $cliente->sistema->ramoAtividade->nome ?? null, ], 'nome' => $cliente->sistema->nome ?? null, 'versao' => $cliente->sistema->versao ?? null, ], 'agendamentos' => $agendamentos, 'agendamentos_siglas' => $agendamentosSiglas, ], ]; } }