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, 'lancamentos' => self::getLancamentos(), 'paciente' => null, 'exame' => 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_exame') ->select( 'exame_imagem_agendamento_exame.id AS atendimento_id', 'exame_imagem_agendamento_exame.arquivo AS arquivo', 'exame_imagem_agendamento_exame.resultado AS resultado', 'cadastro_paciente.id AS paciente_id', 'exame_imagem_agendamento_exame.id AS id_paciente', 'cidadao.nome AS paciente_nome', 'medico_cidadao.nome AS medico_solicitante', 'parametrizacao_exame.id AS exame_id', 'parametrizacao_exame.nome AS exame_nome', DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS prontuario'), DB::raw("DATE_FORMAT(exame_imagem_agendamento.data, '%d/%m/%Y') AS data"), DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y') AS data_pedido"), 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' ), ) ->orWhere(function ($query) { $query->where('exame_imagem_agendamento_exame.status_id', Status::REALIZADO) ->where('exame_imagem_agendamento_exame.status_troca', true); }) ->join( 'exame_imagem_agendamento', 'exame_imagem_agendamento.id', 'exame_imagem_agendamento_exame.agendamento_id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_imagem_agendamento_exame.paciente_id' ) ->join( 'cadastro_cidadao AS cidadao', 'cidadao.id', 'cadastro_paciente.cidadao_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' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_agendamento_exame.exame_id' ) ->whereBetween('exame_imagem_agendamento.data', [ Carbon::createFromFormat('d/m/Y', $request->periodo['start'])->setTime(0, 0, 0), Carbon::createFromFormat('d/m/Y', $request->periodo['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->exame, function ($query, $value) { $query->where('parametrizacao_exame.id', $value); }) ->when($request->prontuario, function ($query, $value) { $query->where('cadastro_paciente.prontuario', $value); }); if ($request->paciente_id) { $agendamentos->where('exame_imagem_agendamento.paciente_id', $request->paciente_id); } if ($request->exame_id) { $agendamentos->where('exame_imagem_agendamento_exame.exame_id', $request->exame_id); } $agendamentos = $agendamentos->get(); return $agendamentos; } private static function getLancamentos() { $agendamentos = DB::table('exame_imagem_agendamento_exame') ->select( 'exame_imagem_agendamento_exame.id AS id_paciente', 'exame_imagem_agendamento_exame.agendamento_id AS agendamento_id', 'exame_imagem_agendamento_exame.arquivo AS arquivo', 'exame_imagem_agendamento_exame.resultado AS resultado', 'exame_imagem_fechamento_status.id AS status_id', 'exame_imagem_fechamento_status.nome AS status_nome', 'exame_imagem_agendamento.medico_id', 'exame_imagem_agendamento.unidade_id', 'exame_imagem_agendamento.profissional_id', 'exame_imagem_agendamento.urgente', 'cadastro_unidade.nome_fantasia', 'medico_cidadao.nome AS medico_solicitante', 'profissional.nome AS profissional_nome', 'cadastro_usuario_registro_tipo.id AS registro_id', 'cadastro_usuario_registro_tipo.descricao AS registro_nome', 'cadastro_usuario_registro_tipo.sigla AS registro_sigla', 'profissional_cidadao.registro_numero AS registro_numero', 'cadastro_unidade_solicitante.nome_fantasia AS unidade_solicitante_nome', 'cadastro_paciente.id AS paciente_id', 'cidadao.nome AS paciente_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.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( 'exame_imagem_agendamento', 'exame_imagem_agendamento.id', 'exame_imagem_agendamento_exame.agendamento_id' ) ->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_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.medico_id' ) ->join( 'cadastro_cidadao AS medico_cidadao', 'medico_cidadao.id', 'cadastro_usuario.cidadao_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' ) ->join( 'exame_imagem_fechamento_status', 'exame_imagem_fechamento_status.id', 'exame_imagem_agendamento_exame.status_id' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_agendamento_exame.exame_id' ) ->leftJoin( 'cadastro_usuario_registro_tipo', 'cadastro_usuario_registro_tipo.id', 'profissional_cidadao.registro_tipo_id' ) ->leftJoin( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_imagem_agendamento_exame.paciente_id' ) ->leftJoin( 'cadastro_cidadao AS cidadao', 'cidadao.id', 'cadastro_paciente.cidadao_id' ); $agendamentos = $agendamentos->get(); return $agendamentos; } }