self::getAdditionals(), 'form' => self::getForm(), 'model' => self::getModel(), ]; } public static function store($request) { self::validate($request); DB::transaction(function() use ($request) { $centroCusto = CentroCusto::find($request->centro_custo_id); if ($centroCusto->setor->tipo_id === SetorTipo::ENFERMAGEM) { self::criaAtendimentosEnfermagem($request); self::insereProcedimentosAdministracaoMedicamento($request); self::finalizaFaasEnfermagemAbertas($request); } else if ($centroCusto->setor->tipo_id === SetorTipo::OBSERVACAO) { self::finalizaFaasObservacaoAbertas($request); } }); } public static function datatable() { return DB::table('pronto_atendimento_atendimento') ->select([ 'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"), 'cadastro_setor.nome AS setor_nome', 'pronto_atendimento_faa.id AS faa_id', 'pronto_atendimento_faa.numero AS faa_numero', DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%d/%m/%Y') AS data_atendimento"), 'cadastro_cidadao.nome AS paciente_nome', ]) ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_fila.centro_custo_id') ->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_centro_custo.infraestrutura_id') ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereIn('cadastro_setor.tipo_id', [SetorTipo::ENFERMAGEM, SetorTipo::OBSERVACAO]); } public static function list() { switch (request()->get('listar')) { case 'unidades': return self::getUnidades(request()->get('start_date'), request()->get('end_date')); break; case 'infraestruturas': return self::getInfraestruturas(request()->get('start_date'), request()->get('end_date'), request()->get('unidade_id')); break; case 'centros-custo': return self::getCentrosCusto(request()->get('start_date'), request()->get('end_date'), request()->get('infraestrutura_id')); break; default: return []; break; } } private static function validate($request) { return $request->validate([ 'start_date' => [ 'required', 'date', ], 'end_date' => [ 'required', 'date', ], 'centro_custo_id' => [ 'required', 'integer', 'exists:\App\Models\Cadastro\Estabelecimento\CentroCusto,id', ], ]); } private static function getAdditionals() { return [ 'data_atual' => Carbon::now()->format('d/m/Y'), ]; } private static function getForm() { return [ 'unidades' => [], 'infraestruturas' => [], 'centros_custo' => [], ]; } private static function getModel() { return [ 'periodo' => [ 'start' => null, 'end' => null, ], 'unidade_id' => null, 'infraestrutura_id' => null, 'centro_custo_id' => null, ]; } private static function getUnidades($startDate, $endDate) { return DB::table('cadastro_unidade') ->select([ 'cadastro_unidade.id', 'cadastro_unidade.nome_fantasia' ]) ->whereExists(function ($query) use ($startDate, $endDate) { $query->select(DB::raw(1)) ->from('pronto_atendimento_atendimento') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_fila.centro_custo_id') ->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_centro_custo.infraestrutura_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $startDate . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $endDate . "'") ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereIn('cadastro_setor.tipo_id', [SetorTipo::ENFERMAGEM, SetorTipo::OBSERVACAO]) ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id'); }) ->orderBy('cadastro_unidade.nome_fantasia') ->get(); } private static function getInfraestruturas($startDate, $endDate, $unidadeId) { 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_unidade.id', $unidadeId) ->whereExists(function ($query) use ($startDate, $endDate) { $query->select(DB::raw(1)) ->from('pronto_atendimento_atendimento') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_fila.centro_custo_id') ->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $startDate . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $endDate . "'") ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereIn('cadastro_setor.tipo_id', [SetorTipo::ENFERMAGEM, SetorTipo::OBSERVACAO]) ->whereColumn('cadastro_centro_custo.infraestrutura_id', 'cadastro_infraestrutura.id'); }) ->orderByRaw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)') ->get(); } private static function getCentrosCusto($startDate, $endDate, $infraestruturaId) { return DB::table('cadastro_centro_custo') ->select([ 'cadastro_centro_custo.id', 'cadastro_setor.nome', ]) ->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id') ->where('cadastro_centro_custo.infraestrutura_id', $infraestruturaId) ->whereIn('cadastro_setor.tipo_id', [SetorTipo::ENFERMAGEM, SetorTipo::OBSERVACAO]) ->whereExists(function ($query) use ($startDate, $endDate) { $query->select(DB::raw(1)) ->from('pronto_atendimento_atendimento') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $startDate . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $endDate . "'") ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereColumn('painel_senha_fila.centro_custo_id', 'cadastro_centro_custo.id'); }) ->orderBy('cadastro_setor.nome') ->get(); } private static function criaAtendimentosEnfermagem($request) { DB::table('pronto_atendimento_atendimento') ->select([ 'pronto_atendimento_atendimento.id AS atendimento_id', DB::raw(" ( SELECT pronto_atendimento_plantao.id FROM pronto_atendimento_plantao WHERE pronto_atendimento_plantao.inicio <= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.fim >= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.servico_id = true LIMIT 1 ) AS plantao_id "), DB::raw(" ( SELECT pronto_atendimento_plantao.lotacao_id FROM pronto_atendimento_plantao WHERE pronto_atendimento_plantao.inicio <= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.fim >= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.servico_id = true LIMIT 1 ) AS lotacao_id "), DB::raw(" ( SELECT pronto_atendimento_plantao.created_centro_custo_id FROM pronto_atendimento_plantao WHERE pronto_atendimento_plantao.inicio <= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.fim >= pronto_atendimento_faa.created_at AND pronto_atendimento_plantao.servico_id = true LIMIT 1 ) AS created_centro_custo_id "), 'pronto_atendimento_atendimento_prescricao_interna.created_at', 'pronto_atendimento_atendimento_prescricao_interna.updated_at', ]) ->join('pronto_atendimento_atendimento_medico', 'pronto_atendimento_atendimento_medico.atendimento_id', 'pronto_atendimento_atendimento.id') ->join('pronto_atendimento_atendimento_prescricao_interna', 'pronto_atendimento_atendimento_prescricao_interna.atendimento_medico_id', 'pronto_atendimento_atendimento_medico.id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $request->start_date . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $request->end_date . "'") ->where('painel_senha_fila.centro_custo_id', $request->centro_custo_id) ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereNotExists(function ($query) { $query->select(DB::raw(1)) ->from('pronto_atendimento_atendimento_enfermagem') ->whereColumn('pronto_atendimento_atendimento_enfermagem.atendimento_id', 'pronto_atendimento_atendimento.id'); }) ->get() ->each(function ($query) { DB::table('pronto_atendimento_atendimento_enfermagem') ->insert([ 'atendimento_id' => $query->atendimento_id, 'plantao_id' => $query->plantao_id, 'created_at' => $query->created_at, 'updated_at' => $query->updated_at, 'created_lotacao_id' => $query->lotacao_id, 'created_centro_custo_id' => $query->created_centro_custo_id, 'automatico' => true ]); }); } private static function insereProcedimentosAdministracaoMedicamento($request) { DB::table('pronto_atendimento_atendimento_prescricao_interna_medicamento') ->select([ 'pronto_atendimento_atendimento.id AS atendimento_id', DB::raw(" ( SELECT pronto_atendimento_atendimento_enfermagem.id FROM pronto_atendimento_atendimento_enfermagem WHERE pronto_atendimento_atendimento_enfermagem.atendimento_id = pronto_atendimento_atendimento.id ORDER BY pronto_atendimento_atendimento_enfermagem.id DESC LIMIT 1 ) AS atendimento_enfermagem_id "), DB::raw(" ( SELECT pronto_atendimento_atendimento_enfermagem.plantao_id FROM pronto_atendimento_atendimento_enfermagem WHERE pronto_atendimento_atendimento_enfermagem.atendimento_id = pronto_atendimento_atendimento.id ORDER BY pronto_atendimento_atendimento_enfermagem.id DESC LIMIT 1 ) AS plantao_id "), DB::raw(" ( SELECT pronto_atendimento_atendimento_enfermagem.created_lotacao_id FROM pronto_atendimento_atendimento_enfermagem WHERE pronto_atendimento_atendimento_enfermagem.atendimento_id = pronto_atendimento_atendimento.id ORDER BY pronto_atendimento_atendimento_enfermagem.id DESC LIMIT 1 ) AS created_lotacao_id "), DB::raw(" ( SELECT pronto_atendimento_atendimento_enfermagem.created_centro_custo_id FROM pronto_atendimento_atendimento_enfermagem WHERE pronto_atendimento_atendimento_enfermagem.atendimento_id = pronto_atendimento_atendimento.id ORDER BY pronto_atendimento_atendimento_enfermagem.id DESC LIMIT 1 ) AS created_centro_custo_id "), DB::raw(" ( SELECT procedimento.id FROM sigtap.procedimento WHERE procedimento.codigo = '" . Procedimento::ADMINISTRACAO_DE_MEDICAMENTOS_NA_ATENCAO_ESPECIALIZADA . "' ORDER BY procedimento.id DESC LIMIT 1 ) AS procedimento_id "), DB::raw("CAST(pronto_atendimento_atendimento_prescricao_interna.created_at AS DATE) AS realizado_em"), 'pronto_atendimento_atendimento_prescricao_interna.created_at', 'pronto_atendimento_atendimento_prescricao_interna.updated_at', ]) ->join('pronto_atendimento_atendimento_prescricao_interna', 'pronto_atendimento_atendimento_prescricao_interna.id', 'pronto_atendimento_atendimento_prescricao_interna_medicamento.prescricao_interna_id') ->join('pronto_atendimento_atendimento_medico', 'pronto_atendimento_atendimento_medico.id', 'pronto_atendimento_atendimento_prescricao_interna.atendimento_medico_id') ->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_medico.atendimento_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $request->start_date . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $request->end_date . "'") ->where('painel_senha_fila.centro_custo_id', $request->centro_custo_id) ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false) ->whereNotExists(function ($query) { $query->select(DB::raw(1)) ->from('pronto_atendimento_atendimento_procedimento') ->join('sigtap.procedimento', 'procedimento.id', 'pronto_atendimento_atendimento_procedimento.procedimento_id') ->where('procedimento.codigo', Procedimento::ADMINISTRACAO_DE_MEDICAMENTOS_NA_ATENCAO_ESPECIALIZADA) ->whereColumn('pronto_atendimento_atendimento_procedimento.atendimento_id', 'pronto_atendimento_atendimento.id'); }) ->groupBy('pronto_atendimento_atendimento.id') ->get() ->each(function ($query) { DB::table('pronto_atendimento_atendimento_procedimento') ->insert([ 'atendimento_id' => $query->atendimento_id, 'atendimento_enfermagem_id' => $query->atendimento_enfermagem_id, 'procedimento_id' => $query->procedimento_id, 'plantao_id' => $query->plantao_id, 'lotacao_id' => $query->created_lotacao_id, 'quantidade' => 1, 'realizado_em' => $query->realizado_em, 'created_at' => $query->created_at, 'updated_at' => $query->updated_at, 'created_lotacao_id' => $query->created_lotacao_id, 'created_centro_custo_id' => $query->created_centro_custo_id, ]); }); } private static function finalizaFaasEnfermagemAbertas($request) { DB::table('pronto_atendimento_faa') ->whereIn('id', function ($query) use ($request) { $query->from('pronto_atendimento_atendimento') ->select('pronto_atendimento_faa.id') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $request->start_date . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $request->end_date . "'") ->where('painel_senha_fila.centro_custo_id', $request->centro_custo_id) ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false); }) ->get() ->each(function ($faa) { $atendimento = Atendimento::where('faa_id', $faa->id)->first()->atendimentosMedico->first(); if($atendimento && $atendimento->desfecho_id == Desfecho::ALTA_MEDICA) { Faa::find($faa->id)->update([ 'finalizado' => true, 'desfecho_id' => Desfecho::ALTA_MEDICA, 'data_hora_desfecho' => Carbon::now(), 'usuario_id' => $atendimento->plantao->lotacao->usuario_id, 'lotacao_id' => $atendimento->plantao->lotacao_id ]); } else { $currentLotacao = self::getCurrentLotacao(); Faa::find($faa->id)->update([ 'finalizado' => true, 'desfecho_id' => Desfecho::ALTA_MEDICA, 'data_hora_desfecho' => Carbon::now(), 'usuario_id' => $currentLotacao->usuario_id, 'lotacao_id' => $currentLotacao->id ]); } }); } private static function finalizaFaasObservacaoAbertas($request) { DB::table('pronto_atendimento_faa') ->whereIn('pronto_atendimento_faa.id', function ($query) use ($request) { $query->from('pronto_atendimento_atendimento') ->select('pronto_atendimento_faa.id') ->join('painel_senha_fila', 'painel_senha_fila.id', 'pronto_atendimento_atendimento.fila_id') ->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id') ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) >= '" . $request->start_date . "'") ->whereRaw("CAST(pronto_atendimento_faa.created_at AS DATE) <= '" . $request->end_date . "'") ->where('painel_senha_fila.centro_custo_id', $request->centro_custo_id) ->whereNull('painel_senha_fila.deleted_at') ->where('pronto_atendimento_faa.finalizado', false); }) ->get() ->each(function ($faa) { $atendimento = Atendimento::where('faa_id', $faa->id)->first()->atendimentosMedico->first(); if($atendimento && $atendimento->desfecho_id == Desfecho::ALTA_MEDICA) { Faa::find($faa->id)->update([ 'finalizado' => true, 'desfecho_id' => Desfecho::ALTA_MEDICA, 'data_hora_desfecho' => Carbon::now(), 'usuario_id' => $atendimento->plantao->lotacao->usuario_id, 'lotacao_id' => $atendimento->plantao->lotacao_id ]); } else{ $currentLotacao = self::getCurrentLotacao(); Faa::find($faa->id)->update([ 'finalizado' => true, 'desfecho_id' => Desfecho::ALTA_MEDICA, 'data_hora_desfecho' => Carbon::now(), 'usuario_id' => $currentLotacao->usuario_id, 'lotacao_id' => $currentLotacao->id ]); } }); } }