0, ]; $periodo = [ 'start' => Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->startOfDay(), 'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->endOfDay(), ]; $procedimentos = DB::query() ->fromSub(DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'agendamento.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->join('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('jornada.data', $periodo) ->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->when(request()->idade_de, fn ($q, $idadeDe) => $q ->whereRaw("TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) >= {$idadeDe}") ) ->when(request()->idade_ate, fn ($q, $idadeAte) => $q ->whereRaw("TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) <= {$idadeAte}") ) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ->selectRaw(" procedimento.codigo as procedimento_codigo, ocupacao.codigo as ocupacao_codigo, TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) AS idade, SUM(atendimento_procedimento.quantidade) as quantidade ") ->groupByRaw(" procedimento.codigo, ocupacao.codigo, TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) ") ->unionAll(DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'demanda_espontanea.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->join('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('demanda_espontanea.data', $periodo) ->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->when(request()->idade_de, fn ($q, $idadeDe) => $q ->whereRaw("TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) >= {$idadeDe}") ) ->when(request()->idade_ate, fn ($q, $idadeAte) => $q ->whereRaw("TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) <= {$idadeAte}") ) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ->selectRaw(" procedimento.codigo as procedimento_codigo, ocupacao.codigo as ocupacao_codigo, TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) AS idade, SUM(atendimento_procedimento.quantidade) as quantidade ") ->groupByRaw(" procedimento.codigo, ocupacao.codigo, TIMESTAMPDIFF(YEAR, paciente_cidadao.data_nascimento, NOW()) ") ) , 'sub') ->groupByRaw(" sub.procedimento_codigo, sub.ocupacao_codigo, sub.idade ") ->selectRaw(" sub.procedimento_codigo, sub.ocupacao_codigo, sub.idade, SUM(sub.quantidade) as quantidade ") ->get(); $procedimentos->each(function ($procedimento) use ($totalizadores) { $procedimento->procedimento_codigo = MaskUtil::getProcedimentoMasked($procedimento->procedimento_codigo); $totalizadores->quantidade += $procedimento->quantidade; }); return compact('procedimentos', 'totalizadores'); } public static function list() { $data = (object) [ 'additionals' => [], 'form' => [], 'model' => [], ]; $params = self::getParams(); $sets = [ 'periodo' => function () use ($data) { $data->model['periodo'] = ['start' => null, 'end' => null]; }, 'unidade' => function () use ($data, $params) { $unidadesData = self::getUnidadesData($params); $data->form['unidades'] = $unidadesData['form']; $data->model['unidade_id'] = $unidadesData['model']; }, 'infraestrutura' => function () use ($data, $params) { $infraestruturasData = self::getInfraestruturasData($params); $data->form['infraestruturas'] = $infraestruturasData['form']; $data->model['infraestrutura_id'] = $infraestruturasData['model']; }, 'ocupacao' => function () use ($data, $params) { $ocupacoesData = self::getOcupacoesData($params); $data->form['ocupacoes'] = $ocupacoesData['form']; $data->model['ocupacao_id'] = $ocupacoesData['model']; }, 'procedimento' => function () use ($data, $params) { $procedimentosData = self::getProcedimentosData($params); $data->form['procedimentos'] = $procedimentosData['form']; $data->model['procedimento_codigo'] = $procedimentosData['model']; }, ]; $load = is_null(request()->load_from); if ($load) { $data->additionals = [ 'data_atual' => Carbon::today()->format('d/m/Y') ]; $data->model['idade_de'] = null; $data->model['idade_ate'] = null; } foreach ($sets as $field => $set) { if ($load) { $set(); } else if ($field === request()->load_from) { $load = true; } } foreach ($data as $property => $itens) { if (empty($itens)) { unset($data->{$property}); } } return $data; } private static function getParams() { $params = request()->only([ 'periodo', 'unidade_id', 'infraestrutura_id', 'ocupacao_id', 'procedimento_codigo', ]); if (isset($params['periodo']) && !is_null($params['periodo']['start']) && !is_null($params['periodo']['end']) ) { $start = Carbon::createFromFormat('d/m/Y', $params['periodo']['start'])->startOfDay(); $end = Carbon::createFromFormat('d/m/Y', $params['periodo']['end'])->endOfDay(); $params['periodo']['start'] = $start; $params['periodo']['end'] = $end; } else { $params['periodo'] = null; } return $params; } private static function getUnidadesData($params) { $unidades = collect(); $id = null; if (!is_null($params['periodo'])) { $unidades = DB::table('cadastro_unidade as unidade') ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('cadastro_infraestrutura as infraestrutura') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ) ->whereColumn('infraestrutura.unidade_id', 'unidade.id') ->whereNull('infraestrutura.deleted_at') ) ->whereNull('unidade.deleted_at') ->orderBy('unidade.nome_fantasia') ->selectRaw(" unidade.id, unidade.nome_fantasia ") ->get(); } return ['form' => $unidades, 'model' => $id]; } private static function getInfraestruturasData($params) { $infraestruturas = collect(); $id = null; if (!is_null($params['periodo']) && !is_null($params['unidade_id'])) { $infraestruturas = DB::table('cadastro_infraestrutura as infraestrutura') ->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id') ->where('infraestrutura.unidade_id', $params['unidade_id']) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ) ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->orderByRaw("COALESCE(infraestrutura.nome, unidade.nome_fantasia)") ->selectRaw(" infraestrutura.id, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as nome ") ->get(); } return ['form' => $infraestruturas, 'model' => $id]; } private static function getOcupacoesData($params) { $ocupacoes = collect(); $id = null; if (!is_null($params['periodo'])) { $ocupacoes = DB::table('sigtap.ocupacao as ocupacao') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereColumn('atendimento_procedimento.ocupacao_id', 'ocupacao.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereColumn('atendimento_procedimento.ocupacao_id', 'ocupacao.id') ->whereNotNull('atendimento.finalizado_em') ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ) ->orderByRaw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome)") ->selectRaw(" ocupacao.id, CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) as descricao ") ->get(); } return ['form' => $ocupacoes, 'model' => $id]; } private static function getProcedimentosData($params) { $procedimentos = collect(); $codigo = null; if (!is_null($params['periodo'])) { $procedimentos = DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNotNull('atendimento.finalizado_em') ->orderByRaw("CONCAT(procedimento.codigo, ' - ', procedimento.nome)") ->selectRaw(" procedimento.codigo, procedimento.nome ") ->distinct() ->union( DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNotNull('atendimento.finalizado_em') ->orderByRaw("CONCAT(procedimento.codigo, ' - ', procedimento.nome)") ->selectRaw(" procedimento.codigo, procedimento.nome ") ->distinct() ) ->get(); $procedimentos->each(function ($procedimento) { $procedimento->descricao = MaskUtil::getProcedimentoMasked($procedimento->codigo) . " - {$procedimento->nome}"; unset($procedimento->nome); }); } return ['form' => $procedimentos, 'model' => $codigo]; } }