format('d/m/Y'); $currentLotacao = self::getCurrentLotacao(); return [ 'form' => [ 'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia, 'infraestrutura' => $currentLotacao->infraestrutura->nome, ], 'model' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'atendimento' => null, 'prontuario' => null, 'data_nascimento' => null, 'paciente' => null, ], 'additionals' => [ 'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'), 'current_date' => $now->format('d/m/Y') ] ]; } public static function datatable(Request $request) { $agendamentos = DB::table('exame_imagem_agendamento') ->select( 'exame_imagem_agendamento.id AS atendimento_id', 'exame_imagem_agendamento.agendamento_numero AS agendamento_numero', 'exame_imagem_agendamento.medico_id', 'exame_imagem_agendamento.unidade_id', 'exame_imagem_agendamento.exame_id AS grupo_id', 'exame_imagem_agendamento.urgente', 'exame_imagem_agendamento.profissional_id', 'exame_imagem_agendamento.nome_responsavel', 'exame_imagem_agendamento.urgente', 'exame_imagem_agendamento.observacao', 'exame_imagem_agendamento.hora_exame', 'cadastro_unidade.nome_fantasia', 'cadastro_infraestrutura.telefone AS telefone', 'cadastro_endereco.id AS endereco_id', DB::raw("COALESCE(cadastro_endereco.numero, 'S/N') AS endereco_numero"), 'cadastro_logradouro.id AS logradouro_id', 'cadastro_logradouro.nome AS logradouro_nome', 'cadastro_bairro.id AS bairro_id', 'cadastro_bairro.nome AS bairro_nome', 'medico_cidadao.nome AS medico_solicitante', 'cadastro_paciente.id AS paciente_id', 'cidadao.nome AS paciente_nome', 'cadastro_grupo.nome AS grupo', 'cadastro_unidade_solicitante.nome_fantasia AS unidade_solicitante_nome', 'profissional.nome AS profissional_nome', 'parametrizacao_exame.id AS exame_id', 'parametrizacao_exame.nome AS exame_nome', DB::raw("DATE_FORMAT(exame_imagem_agendamento.data, '%d/%m/%Y') AS data"), DB::raw('DATE_FORMAT(exame_imagem_agendamento.hora_exame, "%H:%i") AS hora_exame'), DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y') AS data_pedido"), DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS prontuario'), DB::raw('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y")) as data_nascimento'), DB::raw( '( CASE WHEN cidadao.nome_mae IS NULL THEN "Não informado" ELSE cidadao.nome_mae END ) as nome_mae' ), ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_imagem_agendamento.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->join( 'cadastro_infraestrutura AS cadastro_infraestrutura_solicitante', 'cadastro_infraestrutura_solicitante.id', 'exame_imagem_agendamento.infraestrutura_solicitante_id' ) ->join( 'cadastro_unidade AS cadastro_unidade_solicitante', 'cadastro_unidade_solicitante.id', 'cadastro_infraestrutura_solicitante.unidade_id' ) ->join( 'cadastro_endereco', 'cadastro_endereco.id', 'cadastro_infraestrutura.endereco_id' ) ->join( 'cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id' ) ->join( 'cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.medico_id' ) ->join( 'cadastro_cidadao AS medico_cidadao', 'medico_cidadao.id', 'cadastro_usuario.cidadao_id' ) ->leftJoin( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_imagem_agendamento.paciente_id' ) ->leftJoin( 'cadastro_cidadao AS cidadao', 'cidadao.id', 'cadastro_paciente.cidadao_id' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_agendamento.exame_id' ) ->join( 'cadastro_grupo', 'cadastro_grupo.id', 'exame_imagem_agendamento.exame_id' ) ->join( 'cadastro_usuario AS profissional_cidadao', 'profissional_cidadao.id', 'exame_imagem_agendamento.profissional_id' ) ->join( 'cadastro_cidadao AS profissional', 'profissional.id', 'profissional_cidadao.cidadao_id' ) ->when($request->periodo, function ($query, $value) { $query->whereBetween('exame_imagem_agendamento.data', [ Carbon::createFromFormat('d/m/Y', $value['start'])->setTime(0, 0, 0), Carbon::createFromFormat('d/m/Y', $value['end'])->setTime(23, 59, 59) ]); }) ->when($request->data_nascimento, function ($query, $value) { $query->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $value)); }) ->when($request->paciente, function ($query, $value) { $query->where('cidadao.nome', 'like', "{$value}%"); }) ->when($request->atendimento, function ($query, $value) { $query->where('exame_imagem_agendamento.agendamento_numero', $value); }) ->when($request->prontuario, function ($query, $value) { $query->where('cadastro_paciente.prontuario', $value); }) ->when($request->id, function ($query, $value) { $query->where('exame_imagem_agendamento.id', $value); }) ->get() ->map(function ($atendimento) { $atendimento->impressoes = self::getImpressoes( $atendimento->atendimento_id, [ 'agendamento' => [ 'protocolo' => [ 'bairro_unidade_executante' => $atendimento->bairro_nome, 'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->atendimento_id) ->get() ->map(function ($query) { return [ 'id' => $query->id, 'exame_id' => $query->exame_id, 'nome' => $query->exame->nome, 'codigo' => $query->exame->codigo ?? null, 'observacao' => $query->exame->observacao, 'status_id' => $query->status_id ?? null, 'valor' => $query->exame->procedimento->valor_servico_ambilatorial ?? null, ]; }) ->values(), 'data_exame' => $atendimento->data, 'data_nascimento' => $atendimento->data_nascimento, 'grupo' => $atendimento->grupo, 'hora_exame' => (new Carbon($atendimento->hora_exame))->format('H:i') ?? null, 'logradouro_unidade_executante' => $atendimento->logradouro_nome, 'medico_solicitante' => $atendimento->medico_solicitante, 'mensagem_protocolo' => self::getMensagem(), 'numero_agendamento' => $atendimento->agendamento_numero, 'nome_responsavel' => $atendimento->nome_responsavel, 'numero_unidade_executante' => $atendimento->endereco_numero, 'observacao' => $atendimento->observacao, 'paciente' => $atendimento->paciente_nome, 'prontuario' => $atendimento->prontuario, 'profissional' => $atendimento->profissional_nome, 'sistema' => self::getSistema(), 'telefone_unidade_executante' => MaskUtil::getPhoneMasked($atendimento->telefone), 'unidade_executante' => $atendimento->nome_fantasia, 'unidade_solicitante' => $atendimento->unidade_solicitante_nome, 'urgente' => $atendimento->urgente, ], 'etiqueta' => [ 'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->atendimento_id) ->get() ->map(function ($query) { return [ 'id' => $query->id, 'exame_id' => $query->exame_id, 'nome' => $query->exame->nome, 'codigo' => $query->exame->codigo ?? null, 'observacao' => $query->exame->observacao, 'status_id' => $query->status_id ?? null, 'valor' => $query->exame->procedimento->valor_servico_ambilatorial ?? null, ]; }) ->values(), 'data_exame' => $atendimento->data, 'data_nascimento' => $atendimento->data_nascimento, 'medico_solicitante' => $atendimento->medico_solicitante, 'numero_agendamento' => $atendimento->agendamento_numero, 'paciente' => $atendimento->paciente_nome, 'unidade_executante' => $atendimento->nome_fantasia, ] ] ] ); return $atendimento; }); return $agendamentos; } private static function getSistema() { $cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail(); return [ 'ramo_atividade' => $cliente->sistema->ramoAtividade->nome ?? null, 'nome_sistema' => $cliente->sistema->nome ?? null, 'versao' => $cliente->sistema->versao ?? null, ]; } private static function getMensagem() { $mensagemProtocolo = MensagemProtocolo::select('mensagem')->first(); return [ 'mensagem' => $mensagemProtocolo->mensagem, ]; } public static function getImpressoes($atendimentoId, $data = [], $excluded = []) { $impressoes = [ 'agendamento' => self::getimpressoesAgendamentoExames($atendimentoId, $data['agendamento'] ?? []), ]; foreach ($excluded as $key => $value) { if (is_array($value)) { foreach ($value as $col => $vl) { $indice = array_search($vl, array_column($impressoes[$key], $col)); if ($indice >= 0) { unset($impressoes[$key][$indice]); } } } else { if (array_key_exists($value, $impressoes)) { unset($impressoes[$value]); } } } return $impressoes; } public static function getimpressoesAgendamentoExames($atendimentoId, $data = []) { $impressoesAgendamentoExames = []; if (isset($data['protocolo'])) { $impressoesAgendamentoExames[] = [ 'value' => 'protocolo', 'text' => 'Comprovante de agendamento', 'disabled' => false, 'data' => $data['protocolo'] ]; } if (isset($data['etiqueta'])) { $impressoesAgendamentoExames[] = [ 'value' => 'etiqueta', 'text' => 'Etiqueta', 'disabled' => false, 'data' => $data['etiqueta'] ]; } return $impressoesAgendamentoExames; } public static function getOnlyDigits(&$value) { return preg_replace('/[^\d]/', '', (string) $value); } public static function getPhoneMasked($number) { $number = self::getOnlyDigits($number); switch (strlen($number)) { case 10: { return preg_replace( '/(\d{2})(\d{4})(\d{4})/', '($1) $2-$3', $number ); } case 11: { return preg_replace( '/(\d{2})(\d{5})(\d{4})/', '($1) $2-$3', $number ); } default: { return $number; } } } }