format('Y-m-d'); $lotacao = Lotacao::findOrFail(auth()->payload()->get('lotacao_id')); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), 'profissional' => [ 'id' => $lotacao->usuario->id, 'cidadao_nome' => $lotacao->usuario->cidadao->nome, ], 'unidade_id' => $lotacao->infraestrutura->unidade->id, 'infraestrutura_id' => $lotacao->infraestrutura->id, 'data_atual' => Carbon::now()->format('d/m/Y'), 'status' => [ ['value' => 'laudo_importado', 'text' => 'LAUDO IMPORTADO'], ['value' => 'laudo_nao_importado', 'text' => 'LAUDO NÃO IMPORTADO'] ], ], ]; } public static function list(Request $request) { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento_exame.id AS agendamento_exame_id', 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento_laudo.arquivo AS laudo', 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data'), DB::raw('TIME_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%H:%i") AS hora_inicial'), 'parametrizacao_exame.id AS exame_id', 'parametrizacao_exame.nome AS exame_nome', 'cadastro_grupo.id AS grupo_id', 'cadastro_grupo.nome AS grupo_nome', 'exame_laboratorial_agendamento_exame.status_id AS status_id', 'cadastro_cidadao.nome AS paciente_nome', 'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id', 'cadastro_cidadao.nome_social AS paciente_nome_social', DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'), DB::raw(' CASE WHEN exame_laboratorial_agendamento_laudo.arquivo IS NOT NULL THEN "LAUDO IMPORTADO" ELSE "LAUDO NÃO IMPORTADO" END AS status' ), ]) ->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->leftJoin('exame_laboratorial_agendamento_laudo', 'exame_laboratorial_agendamento_laudo.agendamento_id', 'exame_laboratorial_agendamento.id') ->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id') ->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id') ->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id') ->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->groupBy([ 'exame_laboratorial_agendamento.id', ]); if ($request->periodo) { $agendamentos->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']); $agendamentos->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']); } else { $agendamentos->where('exame_laboratorial_agendamento.data', $currentDate); } if ($request->unidade_id && $request->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', $request->unidade_id); $agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $request->infraestrutura_id); } if ($request->status && $request->status == 'laudo_importado') { $agendamentos->whereNotNull('exame_laboratorial_agendamento_laudo.arquivo'); } if ($request->status && $request->status == 'laudo_nao_importado') { $agendamentos->whereNull('exame_laboratorial_agendamento_laudo.arquivo'); } if ($request->paciente_id) { $agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id); } if ($request->agendamento_numero) { $agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero); } if ($request->prontuario) { $agendamentos->where('cadastro_paciente.prontuario', $request->prontuario); } $agendamentos->whereExists(function($query) { $query->select(DB::raw(1)) ->from('exame_laboratorial_agendamento_exame') ->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id'); $query->where(function($status) { $status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::PENDENTE_ASSINATURA) ->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO); }); }); $agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC'); $agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC'); $agendamentos = $agendamentos->get()->toArray(); return [ 'model' => [ 'agendamentos' => $agendamentos, ], ]; } public static function load() { $unidade = DB::table('exame_laboratorial_parametrizacao_unidade') ->select([ 'cadastro_unidade.id AS unidade_id', 'cadastro_infraestrutura.id AS infraestrutura_id', 'cadastro_unidade.nome_fantasia as nome_fantasia', DB::raw(" CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN cadastro_infraestrutura.nome ELSE cadastro_unidade.nome_fantasia END AS nome_infraestrutura "), 'cadastro_infraestrutura.principal as principal' ]) ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_parametrizacao_unidade.infraestrutura_id') ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->whereExists(function ($query) { $query->select(DB::raw(1)) ->from('exame_laboratorial_agendamento') ->whereColumn( 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id', 'exame_laboratorial_parametrizacao_unidade.infraestrutura_id' ) ->where(function($query) { $query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); }); }) ->where('exame_laboratorial_parametrizacao_unidade.laboratorio', true) ->whereNull('cadastro_unidade.deleted_at') ->whereNull('cadastro_infraestrutura.deleted_at') ->distinct('infraestrutura_id'); $unidade = $unidade->get()->toArray(); $resultadoAgrupado = []; foreach ($unidade as $item) { $unidadeId = $item->unidade_id; $infraestruturaId = $item->infraestrutura_id; $nomeInfraestrutura = $item->nome_infraestrutura; if (!isset($resultadoAgrupado[$unidadeId])) { $resultadoAgrupado[$unidadeId] = [ 'id' => $unidadeId, 'nome_fantasia' => $item->nome_fantasia, 'infraestruturas' => [] ]; } $resultadoAgrupado[$unidadeId]['infraestruturas'][] = [ 'id' => $infraestruturaId, 'nome' => $nomeInfraestrutura, 'principal' => $item->principal ]; } usort($resultadoAgrupado, function ($a, $b) { return strcmp($a['nome_fantasia'], $b['nome_fantasia']); }); return $resultadoAgrupado; } public static function store(Request $request) { DB::transaction(function () use ($request) { if ($request['multiple'] == 'true') { self::saveMultiple($request); } else { self::saveLaudo($request); } }); if ($request['multiple'] == 'false') { return self::show($request); } } public static function saveLaudo($request) { if($request->files->get('laudo') != ''){ foreach ($request->files->get('laudo') as $file) { $agendamentoId = $request['agendamento_id']; $ordem = self::getNameArchive($agendamentoId, $request['quantidade']); if ($ordem == 1) { $arquivo = $agendamentoId . '.PDF'; } else { $arquivo = $agendamentoId . '_' . $ordem . '.PDF'; } try{ $laudo = new AgendamentoLaudo(); $laudo->agendamento_id = $agendamentoId; $laudo->ordem = $ordem; $laudo->arquivo = $arquivo; $laudo->save(); $pasta = storage_path('app' . DIRECTORY_SEPARATOR . 'exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default')); $file->move($pasta, $arquivo); }catch(\Exception $e){ throw new CriacaoArquivoException; } } } } public static function saveMultiple($request) { if($request->files->get('laudo') != ''){ foreach ($request->files->get('laudo') as $file) { if (strpos($file->getClientOriginalName(), '_')) { $agendamentoId = current(explode('_', $file->getClientOriginalName())); } else { $agendamentoId = current(explode('.', $file->getClientOriginalName())); } $agendamentoId = $agendamentoId; $ordem = self::getNameArchive($agendamentoId, $request['quantidade']); if ($ordem == 1) { $arquivo = $agendamentoId . '.PDF'; } else { $arquivo = $agendamentoId . '_' . $ordem . '.PDF'; } try{ $laudo = new AgendamentoLaudo(); $laudo->agendamento_id = $agendamentoId; $laudo->ordem = $ordem; $laudo->arquivo = $arquivo; $laudo->save(); $pasta = storage_path('app' . DIRECTORY_SEPARATOR . 'exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default')); $file->move($pasta, $arquivo); }catch(\Exception $e){ throw new CriacaoArquivoException; } } } } public static function getNameArchive($id, $quantidade) { for($i = 1; $i < $quantidade; $i++) { $arquivo = AgendamentoLaudo::where('agendamento_id', $id) ->where('ordem', $i)->first(); if (is_null($arquivo)) { break; } } return $i; } public static function getExames($id) { $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'parametrizacao_exame.nome AS exame_nome', 'exame_laboratorial_agendamento_exame_status.descricao AS status', ]) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->join('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id') ->groupBy([ 'exame_laboratorial_agendamento.id', 'parametrizacao_exame.id', ]); $agendamentos->where('exame_laboratorial_agendamento.id', $id); $agendamentos = $agendamentos->get(); return [ 'form' => [ 'exames' => $agendamentos ], ]; } public static function show($request) { return AgendamentoLaudo::where('agendamento_id', $request['agendamento_id']) ->orderBy('ordem') ->get() ->map(function($query) { return [ 'id' => $query->id ?? null, 'usuario' => Lotacao::find($query->created_lotacao_id)->usuario->cidadao->nome ?? (isset($query->externo_created_usuario_id) ? (WebServiceUsuario::find($query->externo_created_usuario_id)->nome . ' (Web service)') : null ), 'data' => (new Carbon($query->created_at))->format('d/m/Y') ?? null, 'arquivo' => $query->arquivo ?? null, 'url' => $query->url ?? null, ]; }); } public static function destroy($id) { $laudo = AgendamentoLaudo::findOrFail($id); if(Storage::exists('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo)){ Storage::delete('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo); } AgendamentoLaudo::findOrFail($id)->delete(); } public static function download($id) { $laudo = AgendamentoLaudo::findOrFail($id); return [ 'nome' => $laudo->arquivo, 'arquivo' => Storage::path('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo), ]; } }