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

1257 lines
53 KiB
PHP

<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Models\Cadastro\Estabelecimento\Unidade;
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class ExameSolicitadoStatusService extends Service
{
public static function list()
{
if(request()->all() == []){
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$periodoStart = $currentDate;
$periodoEnd = $currentDate;
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
'unidades' => Unidade::orderBy('nome_fantasia')
->get()
->map(fn ($query) => [
'id' => $query->id,
'nome_fantasia' => $query->nome_fantasia,
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
'id' => $infraestrutura->id,
'nome' => $infraestrutura->nome,
'principal' => $infraestrutura->principal,
])->values()
]),
'unidades_coleta' => ExameLaboratorialUnidade::where('unidade_coleta', true)
->get()
->map(function($query) {
return [
'id' => $query->id ?? null,
'unidade_id' => $query->unidade_id ?? null,
'infraestrutura_id' => $query->infraestrutura_id ?? null,
'infraestrutura_nome' => $query->infraestrutura->nome,
'infraestrutura_principal' => $query->infraestrutura->principal,
'nome_fantasia' => $query->unidade->nome_fantasia ?? null,
'unidade_coleta' => $query->unidade_coleta ?? null,
];
}),
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
'infraestrutura' => $currentLotacao->infraestrutura->nome,
],
'model' => [
'unidade_coleta' => [],
],
];
}else{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
->select([
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
DB::raw('CASE WHEN cadastro_infraestrutura_coleta.principal THEN cadastro_unidade_coleta.nome_fantasia ELSE cadastro_infraestrutura_coleta.nome END
AS coleta_infraestrutura_nome'),
])
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
->join('cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id')
->join('cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id')
->groupBy([
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
]);
if (request()->prontuario) {
$agendamentos->where('cadastro_paciente.prontuario', request()->prontuario);
}
if (request()->agendamento_id) {
$agendamentos->where('exame_laboratorial_agendamento_exame.agendamento_id', request()->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()->exame_id) {
$agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
}
if (request()->unidade_id) {
$agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$agendamentos->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$agendamentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
if (request()->status_id) {
$agendamentos->where(function($query) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$query->orWhereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
} else if ($status == StatusAgendamento::AGENDADO) {
$query
->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$query->orWhere('status_id', $status);
}
}
});
}
$agendamentos = $agendamentos->get();
if (request()->periodo) {
$periodoStart = Carbon::create(request()->periodo['start'])->format('Y-m-d');
$periodoEnd = Carbon::create(request()->periodo['end'])->format('Y-m-d');
} else {
$periodoStart = $currentDate;
$periodoEnd = $currentDate;
}
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
'unidades' => Unidade::orderBy('nome_fantasia')
->get()
->map(fn ($query) => [
'id' => $query->id,
'nome_fantasia' => $query->nome_fantasia,
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
'id' => $infraestrutura->id,
'nome' => $infraestrutura->nome,
'principal' => $infraestrutura->principal,
])->values()
]),
'unidades_coleta' => ExameLaboratorialUnidade::where('unidade_coleta', true)
->get()
->map(function($query) {
return [
'id' => $query->id ?? null,
'unidade_id' => $query->unidade_id ?? null,
'infraestrutura_id' => $query->infraestrutura_id ?? null,
'infraestrutura_nome' => $query->infraestrutura->nome,
'infraestrutura_principal' => $query->infraestrutura->principal,
'nome_fantasia' => $query->unidade->nome_fantasia ?? null,
'unidade_coleta' => $query->unidade_coleta ?? null,
];
}),
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
'infraestrutura' => $currentLotacao->infraestrutura->nome,
'agendado' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where(function($query) {
$query
->where('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
})
->count(),
'nao_coletado' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where(function($query) {
$query->whereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
})
->count(),
'coleta_realizada' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
};
})
->where('status_id', StatusAgendamento::REALIZADO)
->count(),
'pendente_assinatura' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where('status_id', StatusAgendamento::PENDENTE_ASSINATURA)
->count(),
'pendente_impressao' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where('status_id', StatusAgendamento::ASSINADO)
->where('entregue', false)
->where('impressao', false)
->count(),
'pendente_entrega' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where('status_id', StatusAgendamento::ASSINADO)
->where('entregue', false)
->where('impressao', true)
->count(),
'entregue' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
$query
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
})
->where('entregue', true)
->count(),
],
'model' => [
'unidade_coleta' => $agendamentos->map(fn ($coleta) => [
'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia,
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
'total_exames' => AgendamentoExame::whereHas('agendamento', function($query) use ($coleta, $periodoStart, $periodoEnd) {
$query
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id)
->where('data', '>=', $periodoStart)
->where('data', '<=', $periodoEnd)
->whereHas('paciente', function($query) {
if (request()->prontuario) {
$query->where('prontuario', request()->prontuario);
}
});
if (request()->agendamento_id) {
$query->where('agendamento_id', request()->agendamento_id);
}
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
})
->where(function($query) {
if (request()->exame_id) {
$query->where('exame_id', request()->exame_id);
}
if (request()->status_id) {
$query->where(function($query) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$query->orWhereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
} else if ($status == StatusAgendamento::AGENDADO) {
$query
->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$query->orWhere('status_id', $status);
}
}
});
}
})
->count(),
'agendamentos' => DB::table('exame_laboratorial_agendamento as agendamento')
->select([
'agendamento.id AS agendamento_id',
'agendamento.agendamento_numero',
'cidadao.nome AS paciente_nome',
'cidadao.nome_social AS paciente_nome_social',
'cidadao.preferencia_nome_id AS prefereNomeSocial',
'paciente.prontuario AS paciente_prontuario',
'agendamento.data',
'unidade.nome_fantasia AS unidade',
'unidade_infraestrutura.nome_fantasia AS infraestrutura_nome',
'coleta_unidade.nome_fantasia AS unidade_coleta',
'coleta_infraestrutura_unidade.nome_fantasia AS coleta_infraestrutura_nome',
'laboratorio_unidade.nome_fantasia AS laboratorio',
'unidade_laboratorio_infraestrutura.nome_fantasia AS laboratorio_infraestrutura_nome',
])
->join('cadastro_paciente as paciente', 'paciente.id', '=', 'agendamento.paciente_id')
->join('cadastro_cidadao as cidadao', 'cidadao.id', '=', 'paciente.cidadao_id')
->leftJoin('cadastro_unidade as unidade', 'unidade.id', '=', 'agendamento.unidade_id')
->leftJoin('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', '=', 'agendamento.infraestrutura_id')
->leftJoin('cadastro_unidade as unidade_infraestrutura', 'unidade_infraestrutura.id', '=', 'infraestrutura.unidade_id')
->leftJoin('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', '=', 'agendamento.coleta_unidade_id')
->leftJoin('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', '=', 'agendamento.coleta_infraestrutura_id')
->leftJoin('cadastro_unidade as coleta_infraestrutura_unidade', 'coleta_infraestrutura_unidade.id', '=', 'coleta_infraestrutura.unidade_id')
->leftJoin('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', '=', 'agendamento.laboratorio_unidade_id')
->leftJoin('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', '=', 'agendamento.laboratorio_infraestrutura_id')
->leftJoin('cadastro_unidade as unidade_laboratorio_infraestrutura', 'unidade_laboratorio_infraestrutura.id', '=', 'laboratorio_infraestrutura.unidade_id')
->whereBetween('agendamento.data', [$periodoStart, $periodoEnd])
->when(request()->agendamento_id, fn($q) => $q->where('agendamento.agendamento_id', request()->agendamento_id))
->when(request()->infraestrutura_id, fn($q) => $q->where('agendamento.infraestrutura_id', request()->infraestrutura_id))
->when(request()->unidade_id, fn($q) => $q->where('agendamento.unidade_id', request()->unidade_id))
->when(request()->coleta_unidade_id, fn($q) => $q->where('agendamento.coleta_unidade_id', request()->coleta_unidade_id))
->when(request()->laboratorio_id, fn($q) => $q->where('agendamento.laboratorio_unidade_id', request()->laboratorio_id))
->when(request()->laboratorio_infraestrutura_id, fn($q) => $q->where('agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id))
->when(request()->prontuario, fn($q) => $q->where('paciente.prontuario', request()->prontuario))
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('exame_laboratorial_agendamento_exame as agendamento_exame')
->whereRaw('agendamento_exame.agendamento_id = agendamento.id')
->when(request()->exame_id, fn($q) => $q->where('agendamento_exame.exame_id', request()->exame_id))
->when(request()->status_id, function ($query) {
$query->where(function ($q) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$q->orWhereIn('status_id', [
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]);
} elseif ($status == StatusAgendamento::AGENDADO) {
$q->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$q->orWhere('status_id', $status);
}
}
});
});
})
->orderBy('agendamento_id')
->orderBy('data')
->get()
->map(function ($agendamento) {
return [
'agendamento_id' => $agendamento->agendamento_id,
'agendamento_numero' => $agendamento->agendamento_numero,
'paciente_nome' => $agendamento->paciente_nome ?? null,
'paciente_nome_social' => $agendamento->paciente_nome_social,
'prefereNomeSocial' => $agendamento->prefereNomeSocial === PreferenciaNome::NOME_SOCIAL,
'paciente_prontuario' => $agendamento->paciente_prontuario,
'data' => $agendamento->data ? Carbon::parse($agendamento->data)->format('Y-m-d\TH:i:s.u\Z') : null,
'exames' => self::getAgendamentoExames($agendamento),
];
})
->values()
]),
],
];
}
}
private static function getAgendamentoExames($agendamento)
{
return DB::table('exame_laboratorial_agendamento_exame as agendamento_exame')
->where('agendamento_exame.agendamento_id', $agendamento->agendamento_id)
->select([
'agendamento_exame.id AS id',
'agendamento_exame.data_coleta',
'agendamento_exame.status_id',
'agendamento_exame.entregue',
'agendamento_exame.impressao',
'status.id AS status',
'status.descricao AS descricao',
'exame.id AS exame_id',
'exame.nome AS exameNome',
'exame.codigo AS procedimento',
])
->join('parametrizacao_exame as exame', 'exame.id', '=', 'agendamento_exame.exame_id')
->leftJoin('exame_laboratorial_agendamento_exame_status as status', 'status.id', '=', 'agendamento_exame.status_id')
->when(request()->exame_id, fn($q) => $q->where('agendamento_exame.exame_id', request()->exame_id))
->when(request()->status_id, function ($query) {
$query->where(function ($q) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$q->orWhereIn('status_id', [
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]);
} elseif ($status == StatusAgendamento::AGENDADO) {
$q->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$q->orWhere('status_id', $status);
}
}
});
})
->get()
->map(function ($exames) USE ($agendamento) {
return [
'id' => $exames->id,
'exame_id' => $exames->exame_id,
'exameNome' => $exames->exameNome ? mb_strtoupper($exames->exameNome) : 'NÃO DISPONÍVEL',
'procedimento' => $exames->procedimento ? $exames->exameNome : "SEM PROCEDIMENTO",
'agendamento_numero' => $agendamento->agendamento_numero,
'data_agendamento' => Carbon::parse($agendamento->data)->format('d/m/Y'),
'status' => $exames->status_id ? self::getStatus($exames) : 'AGENDADO',
'data_coleta' => $exames->data_coleta ? (new Carbon($exames->data_coleta))->format('d/m/Y') : '-',
'unidade' => $agendamento->unidade,
'infraestrutura_nome' => $agendamento->infraestrutura_nome,
'unidade_coleta' => $agendamento->unidade_coleta,
'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome,
'laboratorio' => $agendamento->laboratorio,
'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome,
];
});
}
public static function loadFields()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
],
'model' => [
'unidades' => self::getUnidades()->map(fn ($unidade) => [
'unidade_id' => $unidade->unidade_id,
'unidade_nome' => $unidade->unidade,
'infraestrutura_id' => $unidade->infraestrutura_id,
'infraestrutura_nome' => $unidade->infraestrutura_nome,
]),
'laboratorios' => self::getLaboratorios()->map(fn ($unidade) => [
'laboratorio_id' => $unidade->laboratorio_unidade_id,
'unidade_laboratorio_nome' => $unidade->unidade_laboratorio_nome,
'laboratorio_infraestrutura_id' => $unidade->laboratorio_infraestrutura_id,
'laboratorio_infraestrutura_nome' => $unidade->laboratorio_infraestrutura_nome,
]),
'coletas' => self::getColetas()->map(fn ($unidade) => [
'coleta_unidade_id' => $unidade->unidade_coleta_id,
'coleta_unidade_nome' => $unidade->unidade_coleta_nome,
'coleta_infraestrutura_id' => $unidade->coleta_infraestrutura_id,
'coleta_infraestrutura_nome' => $unidade->coleta_infraestrutura_nome,
]),
'exames' => self::getExames(),
],
];
}
public static function getExames() {
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$query = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.id AS agendamento_id',
'parametrizacao_exame.id AS exame_id',
'parametrizacao_exame.nome AS nome',
'exame_laboratorial_agendamento.data AS data'
])
->join(
'exame_laboratorial_agendamento',
'exame_laboratorial_agendamento.id',
'exame_laboratorial_agendamento_exame.agendamento_id'
)
->join(
'cadastro_paciente',
'cadastro_paciente.id',
'exame_laboratorial_agendamento.paciente_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'exame_laboratorial_agendamento_exame.exame_id'
);
if (request()->has('periodo')) {
$query->whereBetween('exame_laboratorial_agendamento.data', [
request()->periodo['start'],
request()->periodo['end']
]);
} else {
$query->whereDate('exame_laboratorial_agendamento.data', $currentDate);
}
return $query->orderBy('nome')->get()
->groupBy('agendamento_id')
->map(fn ($exames) => [
'agendamento_id' => $exames->first()->agendamento_id,
'exames' => $exames->map(fn ($exame) => [
'id' => $exame->exame_id,
'nome' => $exame->nome ? mb_strtoupper($exame->nome) : 'NÃO DISPONÍVEL'
])->values()
])
->values();
}
public static function getUnidades() {
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$unidades = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
'cadastro_unidade.nome_fantasia AS unidade',
DB::raw('
CASE WHEN cadastro_infraestrutura.principal THEN
cadastro_unidade.nome_fantasia
ELSE
cadastro_infraestrutura.nome
END AS infraestrutura_nome'),
])
->join(
'exame_laboratorial_agendamento',
'exame_laboratorial_agendamento.id',
'exame_laboratorial_agendamento_exame.agendamento_id'
)
->join(
'cadastro_infraestrutura',
'cadastro_infraestrutura.id',
'exame_laboratorial_agendamento.infraestrutura_id'
)
->join(
'cadastro_unidade',
'cadastro_unidade.id',
'cadastro_infraestrutura.unidade_id'
)
->groupBy([
'exame_laboratorial_agendamento.unidade_id',
]);
if (request()->periodo) {
$unidades->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$unidades->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$unidades->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->exame_id) {
$unidades->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
}
if (request()->status_id) {
$unidades->where(function($query) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$query->orWhereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
} else if ($status == StatusAgendamento::AGENDADO) {
$query
->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$query->orWhere('status_id', $status);
}
}
});
}
return $unidades->orderBy('unidade')->orderBy('infraestrutura_nome')->get();
}
public static function getLaboratorios() {
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$laboratorios = DB::table('exame_laboratorial_agendamento_exame')
->select([
'cadastro_unidade_laboratorio.nome_fantasia AS unidade_laboratorio_nome',
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_unidade_id',
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
DB::raw('
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
cadastro_unidade_laboratorio.nome_fantasia
ELSE
cadastro_infraestrutura_laboratorio.nome
END AS laboratorio_infraestrutura_nome'),
])
->join(
'exame_laboratorial_agendamento',
'exame_laboratorial_agendamento.id',
'exame_laboratorial_agendamento_exame.agendamento_id'
)
->join(
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
'cadastro_infraestrutura_laboratorio.id',
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id'
)
->join(
'cadastro_unidade as cadastro_unidade_laboratorio',
'cadastro_unidade_laboratorio.id',
'cadastro_infraestrutura_laboratorio.unidade_id'
)
->groupBy([
'exame_laboratorial_agendamento.laboratorio_unidade_id',
]);
if (request()->periodo) {
$laboratorios->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$laboratorios->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$laboratorios->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->exame_id) {
$laboratorios->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
}
if (request()->coleta_unidade_id) {
$laboratorios->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$laboratorios->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->status_id) {
$laboratorios->where(function($query) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$query->orWhereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
} else if ($status == StatusAgendamento::AGENDADO) {
$query
->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$query->orWhere('status_id', $status);
}
}
});
}
return $laboratorios->orderBy('unidade_laboratorio_nome')->orderBy('laboratorio_infraestrutura_nome')->get();
}
public static function getColetas() {
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$coletas = DB::table('exame_laboratorial_agendamento_exame')
->select([
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
'exame_laboratorial_agendamento.coleta_unidade_id AS unidade_coleta_id',
'cadastro_unidade_coleta.nome_fantasia AS unidade_coleta_nome',
DB::raw('
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
cadastro_unidade_coleta.nome_fantasia
ELSE
cadastro_infraestrutura_coleta.nome
END AS coleta_infraestrutura_nome'),
])
->join(
'exame_laboratorial_agendamento',
'exame_laboratorial_agendamento.id',
'exame_laboratorial_agendamento_exame.agendamento_id'
)
->join(
'cadastro_infraestrutura as cadastro_infraestrutura_coleta',
'cadastro_infraestrutura_coleta.id',
'exame_laboratorial_agendamento.coleta_infraestrutura_id'
)
->join(
'cadastro_unidade as cadastro_unidade_coleta',
'cadastro_unidade_coleta.id',
'cadastro_infraestrutura_coleta.unidade_id'
)
->groupBy([
'exame_laboratorial_agendamento.coleta_unidade_id',
]);
if (request()->periodo) {
$coletas->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$coletas->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$coletas->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->exame_id) {
$coletas->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
}
if (request()->unidade_id) {
$coletas->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$coletas->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->status_id) {
$coletas->where(function($query) {
foreach (request()->status_id as $status) {
if ($status == StatusAgendamento::NAO_REALIZADO) {
$query->orWhereIn(
'status_id',
[
StatusAgendamento::NAO_REALIZADO,
StatusAgendamento::FALTOU,
StatusAgendamento::NOVA_COLETA,
StatusAgendamento::OUTROS,
]
);
} else if ($status == StatusAgendamento::AGENDADO) {
$query
->orWhere('status_id', StatusAgendamento::AGENDADO)
->orWhereNull('status_id');
} else {
$query->orWhere('status_id', $status);
}
}
});
}
return $coletas->orderBy('unidade_coleta_nome')->orderBy('coleta_infraestrutura_nome')->get();
}
public static function getStatus($query) {
if ($query->entregue) {
return 'ENTREGUE';
} else if ($query->impressao) {
return 'PENDENTE DE ENTREGA';
} else if ($query->status_id == StatusAgendamento::ASSINADO) {
return 'PENDENTE DE IMPRESSÃO';
} else if ($query->status_id == StatusAgendamento::REALIZADO) {
return 'COLETA REALIZADA';
} else if ($query->status_id == StatusAgendamento::NAO_REALIZADO || $query->status_id == StatusAgendamento::FALTOU
|| $query->status_id == StatusAgendamento::NOVA_COLETA || $query->status_id == StatusAgendamento::OUTROS) {
return 'NÃO COLETADO';
}
return $query->descricao;
}
public static function getStatusFiltro($status) {
if ($status->id == StatusAgendamento::ASSINADO) {
return 'PENDENTE DE IMPRESSÃO';
} else if ($status->id == StatusAgendamento::REALIZADO) {
return 'COLETA REALIZADA';
}
return $status->descricao;
}
}