This commit is contained in:
@@ -0,0 +1,443 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameImagem;
|
||||
|
||||
use App\Enums\ExameImagem\Fechamento\Status;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Models\ExameImagem\Agendamento\Agendamento;
|
||||
use App\Models\ExameImagem\Agendamento\AgendamentoExame;
|
||||
use App\Models\ExameImagem\Agendamento\AgendamentoHistorico;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class AlteracaoResultadoLancamentoService extends Service
|
||||
{
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('d/m/Y');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => Carbon::now()->format('d/m/Y'),
|
||||
'end' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'lancamentos' => self::getLancamentos(),
|
||||
'usuario_sistema' => $currentLotacao->usuario->cidadao->nome,
|
||||
'usuario_id' => $currentLotacao->usuario->id,
|
||||
],
|
||||
'additionals' => [
|
||||
'data_atual' => Carbon::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 id_paciente',
|
||||
'exame_imagem_agendamento_exame.agendamento_id AS agendamento_id',
|
||||
'exame_imagem_agendamento_exame.status_troca AS troca',
|
||||
'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.urgente',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
'medico_cidadao.nome AS medico_solicitante',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cidadao.nome AS paciente_nome',
|
||||
'cidadao.nome_social AS nome_social',
|
||||
'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'
|
||||
),
|
||||
)
|
||||
->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_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_imagem_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.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(
|
||||
'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_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_imagem_agendamento_exame.paciente_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_cidadao AS cidadao',
|
||||
'cidadao.id',
|
||||
'cadastro_paciente.cidadao_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));
|
||||
});
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->paciente) {
|
||||
|
||||
$agendamentos->where('exame_imagem_agendamento.paciente_id', $request->paciente);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentos->where('exame_imagem_agendamento_exame.exame_id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
return $agendamentos;
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$lancamento = AgendamentoExame::findOrFail($id);
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'paciente' => Paciente::where('id', $lancamento->paciente_id)
|
||||
->with('unidade')
|
||||
->first(),
|
||||
'usuario_sistema' => $currentLotacao->usuario->cidadao->nome,
|
||||
'usuario_id' => $currentLotacao->usuario->id,
|
||||
'agendamento' => Agendamento::where('id', $lancamento->agendamento_id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'agendamento_numero' => $query->agendamento_numero,
|
||||
'data' => (new Carbon($query->data))->format('Y-m-d') ?? null,
|
||||
'id' => $query->id,
|
||||
'grupo_exame' => $query->grupo->nome,
|
||||
'grupo_exame_id' => $query->exame_id,
|
||||
'medico_id' => $query->medico_id,
|
||||
'medico_nome' => $query->medico->nome,
|
||||
'nome_responsavel' => $query->nome_responsavel,
|
||||
'profissional_id' => $query->profissional_id,
|
||||
'profissional_nome' => $query->jornada->usuario->nome,
|
||||
'unidade_id' => $query->unidade_id,
|
||||
'unidade_nome' => $query->unidade->nome_fantasia,
|
||||
'unidade_solicitante_id' => $query->unidade_solicitante_id,
|
||||
'unidade_solicitante_nome' => $query->unidadeSolicitante->nome_fantasia,
|
||||
'urgente' => $query->urgente ?? false,
|
||||
];
|
||||
})
|
||||
->sortBy('data')
|
||||
->values()
|
||||
->first(),
|
||||
],
|
||||
'model' => [
|
||||
'exame_id' => $lancamento->exame_id,
|
||||
'exame_nome' => $lancamento->exame->nome,
|
||||
'data_atual' => Carbon::now()->format('Y-m-d'),
|
||||
'id_paciente' => $lancamento->id,
|
||||
'laudos' => AgendamentoHistorico::whereNotNull('arquivo')
|
||||
->where('agendamento_exame_id', $lancamento->id)
|
||||
->groupBy('laudo_nome')
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(1)
|
||||
->get()
|
||||
->map(function ($agendamento) {
|
||||
return [
|
||||
'arquivo' => $agendamento->arquivo,
|
||||
'id' => $agendamento->id,
|
||||
'laudo_nome' => $agendamento->laudo_nome ?? null,
|
||||
'laudo_data' => (new Carbon($agendamento->created_at))->format('d/m/Y') ?? null,
|
||||
'laudo_hora' => (new Carbon($agendamento->created_at))->format('H:i') ?? null,
|
||||
'salvo' => true,
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'resultado' => $lancamento->resultado,
|
||||
'resultado_parametrizacao' => $lancamento->exame->resultado,
|
||||
'status_id' => $lancamento->status_id ?? null,
|
||||
'status_troca' => $lancamento->status_troca,
|
||||
'principal' => $lancamento->principal,
|
||||
'historico' => AgendamentoHistorico::whereNotNull('resultado')
|
||||
->where('agendamento_exame_id', $lancamento->id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'data' => (new Carbon($query->created_at))->format('d/m/Y') ?? null,
|
||||
'horario' => (new Carbon($query->created_at))->format('H:i') ?? null,
|
||||
'resultado' => $query->resultado,
|
||||
'arquivo_pdf' => $query->laudo_nome,
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'usuario_id' => $lancamento->usuario_id,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
|
||||
self::validate($request);
|
||||
|
||||
try {
|
||||
|
||||
$lancamento = AgendamentoExame::findOrFail($request['id']);
|
||||
|
||||
DB::transaction(function () use ($request, $lancamento) {
|
||||
|
||||
$lancamento->status_troca = true;
|
||||
|
||||
$lancamento->principal = true;
|
||||
|
||||
if ($lancamento['status_id'] == Status::RESULTADO) {
|
||||
|
||||
$lancamento->status_troca = true;
|
||||
}
|
||||
|
||||
$lancamento->update($request->all());
|
||||
|
||||
if($request->files->get('laudos') != ''){
|
||||
|
||||
foreach ($request->files->get('laudos') as $laudo) {
|
||||
|
||||
$arquivo = strtoupper($laudo->getClientOriginalName());
|
||||
$pasta = storage_path('app' . DIRECTORY_SEPARATOR . 'exame-imagem' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default'));
|
||||
$laudo->move($pasta, $arquivo);
|
||||
}
|
||||
}
|
||||
|
||||
$historico = new AgendamentoHistorico();
|
||||
|
||||
$historico->agendamento_exame_id = $lancamento['id'];
|
||||
$historico->agendamento_id = $lancamento['agendamento_id'];
|
||||
$historico->paciente_id = $lancamento['paciente_id'];
|
||||
$historico->resultado = $lancamento['resultado'];
|
||||
$historico->usuario_id = $lancamento['usuario_id'];
|
||||
|
||||
if ($request->files->get('laudos') != '') {
|
||||
|
||||
$historico->arquivo = $lancamento['arquivo'];
|
||||
$historico->laudo_nome = $lancamento['laudo_nome'];
|
||||
}
|
||||
|
||||
$historico->save();
|
||||
$idsHistorico[] = $historico->id;
|
||||
});
|
||||
|
||||
} catch (Exception $error) {
|
||||
return response()->json(['error' => $error->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return $lancamento;
|
||||
}
|
||||
|
||||
public static function download($id)
|
||||
{
|
||||
$laudo = AgendamentoHistorico::findOrFail($id);
|
||||
|
||||
return [
|
||||
'nome' => $laudo->arquivo,
|
||||
'arquivo' => Storage::path('exame-imagem' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo),
|
||||
];
|
||||
}
|
||||
|
||||
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_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',
|
||||
'cidadao.nome_social AS nome_social',
|
||||
'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'
|
||||
),
|
||||
)
|
||||
->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_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_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_imagem_agendamento_exame.paciente_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_cidadao AS cidadao',
|
||||
'cidadao.id',
|
||||
'cadastro_paciente.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_usuario_registro_tipo',
|
||||
'cadastro_usuario_registro_tipo.id',
|
||||
'profissional_cidadao.registro_tipo_id'
|
||||
);
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
return $agendamentos;
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$validations = [
|
||||
'resultado' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:3500',
|
||||
],
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'resultado.max' => 'A quantidade máxima de caracteres foi excedida.'
|
||||
];
|
||||
|
||||
return $request->validate($validations, $messages);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user