Files
rkm-conectasus-ci-sandbox/api/app/Services/ExameLaboratorial/EntregaExameService.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

466 lines
21 KiB
PHP

<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class EntregaExameService 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'),
'entregue' => [
['value' => 1, 'text' => 'SIM'],
['value' => 0, 'text' => 'NÃO']
],
],
];
}
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('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
'cadastro_cidadao_medico.nome AS nome_medico',
'cadastro_grupo.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
DB::raw('true AS pertence_grupo'),
DB::raw('true AS mapa_agrupado'),
DB::raw('false AS selecionado'),
'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'),
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
'exame_laboratorial_agendamento_exame.entregue AS entregue',
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
])
->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')
->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', 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'),
'cadastro_cidadao_medico.nome AS nome_medico',
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
DB::raw('true AS pertence_grupo'),
DB::raw('false AS mapa_agrupado'),
DB::raw('false AS selecionado'),
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_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'),
'exame_laboratorial_agendamento_exame.entregue AS entregue',
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
])
->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')
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'exame_laboratorial_agendamento.id',
'parametrizacao_exame.id',
]);
$agendamentosSemMapaAgrupado->where(function ($query) {
$query->where('parametrizacao_exame.mapa_agrupado', false)
->orWhereNull('parametrizacao_exame.mapa_agrupado');
});
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', 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'),
'cadastro_cidadao_medico.nome AS nome_medico',
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
'parametrizacao_exame.id AS exame_grupo_id',
'parametrizacao_exame.id AS exame_id',
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
DB::raw('false AS pertence_grupo'),
DB::raw('false AS selecionado'),
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_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'),
'exame_laboratorial_agendamento_exame.entregue AS entregue',
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
])
->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')
->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->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO);
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
return [
'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'),
])
->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')
->whereNotNull('parametrizacao_exame.grupo_tipo_id');
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', 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')
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
->groupBy([
'exame_laboratorial_agendamento.id',
'parametrizacao_exame.id',
]);
$agendamentosSemMapaAgrupado->where(function ($query) {
$query->where('parametrizacao_exame.mapa_agrupado', false)
->orWhereNull('parametrizacao_exame.mapa_agrupado');
});
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', 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->where('exame_laboratorial_agendamento_exame.status_id', 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 store(Request $request)
{
DB::transaction(function () use ($request) {
self::saveEntregue($request);
});
}
private static function saveEntregue($request)
{
$usuario_id = self::getCurrentLotacao()->usuario_id;
foreach ($request->agendamentos as $params) {
$exame = AgendamentoExame::where('agendamento_id', $params['agendamento_id'])
->where('exame_id', $params['exame_id'])
->first();
$exame->entregue = $params['entregue'];
if ($params['entregue']) {
$exame->data_entregue = Carbon::now();
$exame->entregue_usuario_id = $usuario_id;
}
$exame->save();
}
}
}