Files
rkm-conectasus-ci-sandbox/api/app/Services/ExameLaboratorial/CancelamentoResultadoService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

453 lines
21 KiB
PHP

<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameResultado;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CancelamentoResultadoService extends Service
{
public static function create()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
],
];
}
public static function list(Request $request)
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
'cadastro_grupo.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
'cadastro_paciente.prontuario AS paciente_prontuario',
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.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_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id')
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id')
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id');
$agendamentosComGrupo->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
$agendamentosComGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
if ($request->periodo) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosComGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->exame_id) {
$agendamentosComGrupo->where('parametrizacao_exame.id', $request->exame_id);
}
if ($request->data_nascimento) {
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
'cadastro_paciente.prontuario AS paciente_prontuario',
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.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 as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
->groupBy([
'exame_laboratorial_agendamento.id',
'parametrizacao_exame.id',
]);
$agendamentosSemMapaAgrupado->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosSemMapaAgrupado->where(function ($query) {
$query->where('parametrizacao_exame.mapa_agrupado', false)
->orWhereNull('parametrizacao_exame.mapa_agrupado');
});
$agendamentosSemMapaAgrupado->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
if ($request->periodo) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosSemMapaAgrupado->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->exame_id) {
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', $request->exame_id);
}
if ($request->data_nascimento) {
$agendamentosSemMapaAgrupado->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
'cadastro_cidadao.nome AS paciente_nome',
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
'cadastro_cidadao.nome_social AS paciente_nome_social',
'cadastro_paciente.prontuario AS paciente_prontuario',
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
->whereNull('parametrizacao_exame.grupo_tipo_id');
if ($request->periodo) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosSemGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->exame_id) {
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
}
if ($request->data_nascimento) {
$agendamentosSemGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosSemGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
'entregue' => [
['value' => 1, 'text' => 'SIM'],
['value' => 0, 'text' => 'NÃO']
],
],
'model' => [
'agendamentos' => $agendamentos,
],
];
}
public static function loadFields(Request $request)
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'parametrizacao_exame.id AS exame_grupo_id',
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
])
->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('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id');
$agendamentosComGrupo->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
$agendamentosComGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
if ($request->periodo) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosComGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosComGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->exame_id && $request->pertence_grupo) {
$agendamentosComGrupo->where('cadastro_grupo.id', $request->exame_id);
}
if ($request->data_nascimento) {
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
->select([
'parametrizacao_exame.id AS exame_grupo_id',
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.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')
->groupBy([
'exame_laboratorial_agendamento.id',
'parametrizacao_exame.id',
]);
$agendamentosSemMapaAgrupado->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosSemMapaAgrupado->where(function ($query) {
$query->where('parametrizacao_exame.mapa_agrupado', false)
->orWhereNull('parametrizacao_exame.mapa_agrupado');
});
$agendamentosSemMapaAgrupado->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
if ($request->periodo) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosSemMapaAgrupado->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->data_nascimento) {
$agendamentosSemMapaAgrupado->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
->select([
'parametrizacao_exame.id AS exame_grupo_id',
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
])
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
->whereNull('parametrizacao_exame.grupo_tipo_id');
if ($request->periodo) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
} else {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
}
if ($request->paciente_id) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
}
if ($request->agendamento_numero) {
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
}
if ($request->prontuario) {
$agendamentosSemGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
}
if ($request->data_nascimento) {
$agendamentosSemGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
}
$agendamentosSemGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
],
'model' => [
'agendamentos' => $agendamentos,
],
];
}
public static function destroy(Request $request)
{
DB::transaction(function () use ($request) {
$agendamento = AgendamentoExame::where('agendamento_id', $request->agendamento_id)
->where('exame_id', $request->exame_id)
->first();
$agendamento->entregue = false;
$agendamento->impressao = false;
$agendamento->data_entregue = null;
$agendamento->data_laudo = null;
$agendamento->data_assinatura = null;
$agendamento->data_impressao = null;
$agendamento->assinatura_profissional_id = null;
$agendamento->impressao_usuario_id = null;
$agendamento->entregue_usuario_id = null;
$agendamento->status_id = StatusAgendamento::REALIZADO;
$agendamento->save();
AgendamentoExameResultado::where('agendamento_exame_id', $agendamento->id)->delete();
});
}
}