select([ 'ambulatorio_atendimento.id AS atendimento_id', DB::raw("'Agendada' AS demanda_tipo"), DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%d/%m/%Y') AS data_atendimento"), 'cadastro_cidadao_usuario.nome AS usuario_nome', 'cadastro_especialidade.nome AS especialidade_nome', 'cadastro_paciente.id AS paciente_id', 'cadastro_cidadao.nome AS paciente_nome', 'cadastro_paciente.prontuario AS paciente_prontuario', DB::raw("DATE_FORMAT(ambulatorio_agendamento.horario, '%H:%i') AS horario_atendimento"), DB::raw(" ( SELECT GROUP_CONCAT(CONCAT(procedimento.codigo, ' - ', procedimento.nome) SEPARATOR ',') FROM ambulatorio_atendimento_procedimento JOIN sigtap.procedimento ON procedimento.id = ambulatorio_atendimento_procedimento.procedimento_id WHERE ambulatorio_atendimento_procedimento.atendimento_id = ambulatorio_atendimento.id AND ambulatorio_atendimento_procedimento.usuario_id = cadastro_usuario.id AND ( EXISTS ( SELECT 1 FROM ambulatorio_atendimento_soap AS s WHERE s.id = ambulatorio_atendimento_procedimento.soap_id AND s.is_rascunho = 0 ) OR NOT EXISTS ( SELECT 1 FROM ambulatorio_atendimento_soap AS s WHERE s.id = ambulatorio_atendimento_procedimento.soap_id ) ) ) AS procedimentos "), DB::raw(" ( SELECT GROUP_CONCAT(CONCAT( IFNULL(CONCAT(cid.codigo, ' - ', cid.nome), ''), CASE WHEN cid.id IS NOT NULL AND geral_ciap.id IS NOT NULL THEN ' / ' ELSE '' END, IFNULL(CONCAT(geral_ciap.codigo, ' - ', geral_ciap.descricao), '') ) SEPARATOR ', ') FROM ambulatorio_atendimento_soap_problema_condicao INNER JOIN ambulatorio_atendimento_soap ON ambulatorio_atendimento_soap.id = ambulatorio_atendimento_soap_problema_condicao.soap_id LEFT JOIN sigtap.cid ON cid.id = ambulatorio_atendimento_soap_problema_condicao.cid_id LEFT JOIN geral_ciap ON geral_ciap.id = ambulatorio_atendimento_soap_problema_condicao.ciap_id WHERE ambulatorio_atendimento_soap.atendimento_id = ambulatorio_atendimento.id ) AS cids_ciaps "), 'ambulatorio_agendamento_status.id AS agendamento_status_id', 'ambulatorio_agendamento_status.nome AS agendamento_status_nome', ]) ->leftJoin('ambulatorio_atendimento', 'ambulatorio_atendimento.agendamento_id','ambulatorio_agendamento.id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_jornada.especialidade_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_agendamento.paciente_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->join('ambulatorio_agendamento_status', 'ambulatorio_agendamento_status.id', 'ambulatorio_agendamento.status_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ->when($params->especialidade_id, fn ($q) => $q->where('cadastro_especialidade.id', $params->especialidade_id)) ->when($params->usuario_id, fn ($q) => $q->where('cadastro_usuario.id', $params->usuario_id)) ->when($params->status_id, fn ($q) => $q->where('ambulatorio_agendamento.status_id', $params->status_id), fn ($q) => $q->where('ambulatorio_agendamento.status_id', '<>', AgendamentoStatus::AGENDADO), ) ->when($params->status_id === AgendamentoStatus::REALIZADO, fn ($q) => $q ->unionAll( DB::table('ambulatorio_atendimento') ->select([ 'ambulatorio_atendimento.id AS atendimento_id', DB::raw("'Espontânea' AS demanda_tipo"), DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%d/%m/%Y') AS data_atendimento"), 'cadastro_cidadao_usuario.nome AS usuario_nome', 'cadastro_especialidade.nome AS especialidade_nome', 'cadastro_paciente.id AS paciente_id', 'cadastro_cidadao.nome AS paciente_nome', 'cadastro_paciente.prontuario AS paciente_prontuario', DB::raw("NULL AS horario_atendimento"), DB::raw(" ( SELECT GROUP_CONCAT(CONCAT(procedimento.codigo, ' - ', procedimento.nome) SEPARATOR ',') FROM ambulatorio_atendimento_procedimento JOIN sigtap.procedimento ON procedimento.id = ambulatorio_atendimento_procedimento.procedimento_id WHERE ambulatorio_atendimento_procedimento.atendimento_id = ambulatorio_atendimento.id AND ambulatorio_atendimento_procedimento.usuario_id = cadastro_usuario.id AND ( EXISTS ( SELECT 1 FROM ambulatorio_atendimento_soap AS s WHERE s.id = ambulatorio_atendimento_procedimento.soap_id AND s.is_rascunho = 0 ) OR NOT EXISTS ( SELECT 1 FROM ambulatorio_atendimento_soap AS s WHERE s.id = ambulatorio_atendimento_procedimento.soap_id ) ) ) AS procedimentos "), DB::raw(" ( SELECT GROUP_CONCAT(CONCAT( IFNULL(CONCAT(cid.codigo, ' - ', cid.nome), ''), CASE WHEN cid.id IS NOT NULL AND geral_ciap.id IS NOT NULL THEN ' / ' ELSE '' END, IFNULL(CONCAT(geral_ciap.codigo, ' - ', geral_ciap.descricao), '') ) SEPARATOR ', ') FROM ambulatorio_atendimento_soap_problema_condicao INNER JOIN ambulatorio_atendimento_soap ON ambulatorio_atendimento_soap.id = ambulatorio_atendimento_soap_problema_condicao.soap_id LEFT JOIN sigtap.cid ON cid.id = ambulatorio_atendimento_soap_problema_condicao.cid_id LEFT JOIN geral_ciap ON geral_ciap.id = ambulatorio_atendimento_soap_problema_condicao.ciap_id WHERE ambulatorio_atendimento_soap.atendimento_id = ambulatorio_atendimento.id ) AS cids_ciaps "), DB::raw(AgendamentoStatus::REALIZADO . " AS agendamento_status_id"), DB::raw(" ( SELECT ambulatorio_agendamento_status.nome FROM ambulatorio_agendamento_status WHERE ambulatorio_agendamento_status.id = " . AgendamentoStatus::REALIZADO . " ) AS agendamento_status_nome "), ]) ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'ambulatorio_demanda_espontanea.usuario_id') ->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_demanda_espontanea.especialidade_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_demanda_espontanea.paciente_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ->when($params->especialidade_id, fn ($q) => $q->where('cadastro_especialidade.id', $params->especialidade_id)) ->when($params->usuario_id, fn ($q) => $q->where('cadastro_usuario.id', $params->usuario_id)) ) ); self::setOrderBy($atendimentos); $atendimentos = $atendimentos->get(); return [ 'atendimentos' => $atendimentos->map(function($query) { $query->procedimentos = $query->procedimentos ? explode(',', $query->procedimentos) : []; $query->cids_ciaps = $query->cids_ciaps ? explode(',', $query->cids_ciaps) : []; return $query; }), 'totalizadores' => [ 'quantidade_pacientes' => $atendimentos->unique('paciente_id')->count(), 'quantidade_atendimentos_realizados' => $atendimentos->count(), 'quantidade_faltas' => $atendimentos->sum(fn ($item) => $item->agendamento_status_id == AgendamentoStatus::FALTA_DO_PACIENTE ? 1 : 0), 'quantidade_procedimentos_lancados' => $atendimentos->sum(fn ($item) => $item->procedimentos ? count($item->procedimentos) : 0), ], ]; } public static function create() { return [ 'form' => [ 'unidades' => [], 'infraestruturas' => [], 'especialidades' => [], 'usuarios' => [], 'status' => [], ], 'model' => [ 'periodo' => ['start' => null, 'end' => null], 'inicio' => null, 'fim' => null, 'unidade_id' => null, 'infraestrutura_id' => null, 'especialidade_id' => null, 'usuario_id' => null, 'status_id' => null, ], ]; } public static function list() { $params = self::getParams(); switch (request()->listar) { case 'unidades': { return ['unidades' => self::getUnidades($params)]; } case 'infraestruturas': { return ['infraestruturas' => self::getInfraestruturas($params)]; } case 'especialidades': { return [ 'especialidades' => self::getEspecialidades($params), 'usuarios' => self::getUsuarios($params), 'status' => self::getStatus($params) ]; } case 'usuarios': { return [ 'usuarios' => self::getUsuarios($params), 'status' => self::getStatus($params) ]; } case 'status': { return ['status' => self::getStatus($params)]; } } } private static function getParams() { return (object) [ 'periodo' => [ 'start' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['start'])->setTime(0, 0, 0), 'end' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['end'])->setTime(23, 59, 59) ], 'unidade_id' => request()->get('unidade_id'), 'infraestrutura_id' => request()->get('infraestrutura_id'), 'especialidade_id' => request()->get('especialidade_id'), 'usuario_id' => request()->get('usuario_id'), 'status_id' => request()->get('status_id'), ]; } private static function getUnidades($params) { return DB::table('cadastro_unidade') ->select([ 'cadastro_unidade.id', 'cadastro_unidade.nome_fantasia', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_infraestrutura') ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_lotacao') ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_especialidade') ->whereColumn('cadastro_especialidade.id', 'ambulatorio_jornada.especialidade_id') ) ->where('ambulatorio_agendamento.status_id', '<>', AgendamentoStatus::AGENDADO) ->whereBetween('ambulatorio_jornada.data', $params->periodo) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->whereColumn('ambulatorio_demanda_espontanea.created_lotacao_id', 'cadastro_lotacao.id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_especialidade') ->whereColumn('cadastro_especialidade.id', 'ambulatorio_demanda_espontanea.especialidade_id') ) ->whereNotNull('ambulatorio_atendimento.finalizado_em') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ) ) ) ) ->orderBy('cadastro_unidade.nome_fantasia') ->get(); } private static function getInfraestruturas($params) { return DB::table('cadastro_infraestrutura') ->select([ 'cadastro_infraestrutura.id', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS nome"), ]) ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_lotacao') ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_especialidade') ->whereColumn('cadastro_especialidade.id', 'ambulatorio_jornada.especialidade_id') ) ->where('ambulatorio_agendamento.status_id', '<>', AgendamentoStatus::AGENDADO) ->whereBetween('ambulatorio_jornada.data', $params->periodo) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->whereColumn('ambulatorio_demanda_espontanea.created_lotacao_id', 'cadastro_lotacao.id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_especialidade') ->whereColumn('cadastro_especialidade.id', 'ambulatorio_demanda_espontanea.especialidade_id') ) ->whereNotNull('ambulatorio_atendimento.finalizado_em') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ) ) ) ->orderByRaw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)') ->get(); } private static function getEspecialidades($params) { return DB::table('cadastro_especialidade') ->select([ 'cadastro_especialidade.id', 'cadastro_especialidade.nome', ]) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('ambulatorio_jornada.especialidade_id', 'cadastro_especialidade.id') ->where('ambulatorio_agendamento.status_id', '<>', AgendamentoStatus::AGENDADO) ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('ambulatorio_demanda_espontanea.especialidade_id', 'cadastro_especialidade.id') ->whereNotNull('ambulatorio_atendimento.finalizado_em') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ) ) ->orderBy('cadastro_especialidade.nome') ->get(); } private static function getUsuarios($params) { return DB::table('cadastro_usuario') ->select([ 'cadastro_usuario.id', 'cadastro_cidadao.nome', ]) ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('cadastro_lotacao') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id') ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id') ->where('ambulatorio_agendamento.status_id', '<>', AgendamentoStatus::AGENDADO) ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->especialidade_id, fn ($q) => $q->where('ambulatorio_jornada.especialidade_id', $params->especialidade_id)) ) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('ambulatorio_demanda_espontanea.usuario_id', 'cadastro_usuario.id') ->whereNotNull('ambulatorio_atendimento.finalizado_em') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ->when($params->especialidade_id, fn ($q) => $q->where('ambulatorio_demanda_espontanea.especialidade_id', $params->especialidade_id)) ) ->orderBy('cadastro_cidadao.nome') ->get(); } private static function getStatus($params) { return DB::table('ambulatorio_agendamento_status') ->select([ 'ambulatorio_agendamento_status.id', 'ambulatorio_agendamento_status.nome', ]) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_agendamento') ->leftJoin('ambulatorio_atendimento','ambulatorio_atendimento.agendamento_id','ambulatorio_agendamento.id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('ambulatorio_agendamento.status_id', 'ambulatorio_agendamento_status.id') ->whereIn('ambulatorio_agendamento.status_id',[AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE]) ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_lotacao.infraestrutura_id', $params->infraestrutura_id) ->when($params->especialidade_id, fn ($q) => $q->where('ambulatorio_jornada.especialidade_id', $params->especialidade_id)) ->when($params->usuario_id, fn ($q) => $q->where('cadastro_lotacao.usuario_id', $params->usuario_id)) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_agendamento_status.id', AgendamentoStatus::REALIZADO) ->whereNotNull('ambulatorio_atendimento.finalizado_em') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ->when($params->especialidade_id, fn ($q) => $q->where('ambulatorio_demanda_espontanea.especialidade_id', $params->especialidade_id)) ->when($params->usuario_id, fn ($q) => $q->where('ambulatorio_demanda_espontanea.usuario_id', $params->usuario_id)) ) ) ->orderBy('ambulatorio_agendamento_status.nome') ->get(); } private static function setOrderBy($builder) { $columns = [ 'demanda_tipo' => 'demanda_tipo', 'data_atendimento' => 'data_atendimento', 'usuario_nome' => 'usuario_nome', 'especialidade_nome' => 'especialidade_nome', 'paciente_nome' => 'paciente_nome', 'paciente_prontuario' => 'paciente_prontuario', 'horario_atendimento' => 'horario_atendimento', 'agendamento_status_nome' => 'agendamento_status_nome', ]; if (is_null(request()->sort_by)) { $builder->orderByRaw(join(', ', $columns)); return; } $column = $columns[request()->sort_by]; $direction = DataUtil::getBooleanValueFromStringBoolean(request()->sort_desc) ? 'DESC' : 'ASC'; unset($columns[request()->sort_by]); $builder->orderByRaw("{$column} {$direction}, " . join(', ', $columns)); } }