init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,352 @@
<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Enums\Geral\MedidaTempo;
use App\Enums\Parametrizacao\Exame\ComposicaoTipo;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameResultado;
use App\Models\Intranet\Cliente;
use App\Models\Parametrizacao\Exame\ExameComposicao;
use App\Models\Parametrizacao\Exame\ExameComposicaoLista;
use App\Models\Parametrizacao\Exame\ExameComposicaoVariavel;
use App\Services\Service;
use App\Utils\MaskUtil;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class ExameResultadoAgrupadoService extends Service
{
public static function index($id)
{
$currentLotacao = self::getCurrentLotacao();
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
$exame = self::getExame($id);
$exames = self::getExames($exame->exame->grupo_tipo_id, $exame->agendamento->id);
return [
'form' => [
'data_atual' => Carbon::now()->format('d/m/Y'),
'paciente' => [
'nome' => $exame->agendamento->paciente->cidadao->nome ?? null,
'nomeSocial' => $exame->agendamento->paciente->cidadao->nome_social,
'prefereNomeSocial' => $exame->agendamento->paciente->cidadao
->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
'data_nascimento' => $exame->agendamento->paciente->cidadao->data_nascimento
? $exame->agendamento->paciente->cidadao->data_nascimento->format('d/m/Y') : 'Não informada',
'cns' => $exame->agendamento->paciente->cidadao->cns,
'prontuario' => $exame->agendamento->paciente->prontuario,
'sexo' => $exame->agendamento->paciente->cidadao->sexo->descricao,
'telefone' => $exame->agendamento->paciente->cidadao->telefone_principal
? MaskUtil::getPhoneMasked($exame->agendamento->paciente->cidadao->telefone_principal->numero)
: null,
'idade' => [
'anos' => $exame->agendamento->paciente->cidadao->idade['anos'] ?? 'Não informada',
'meses' => $exame->agendamento->paciente->cidadao->idade['meses'] ?? 'Não informada',
'dias' => $exame->agendamento->paciente->cidadao->idade['dias'] ?? 'Não informada',
],
],
'agendamento' => [
'unidade' => $exame->agendamento->unidade->nome_fantasia ?? null,
'infraestrutura' => $exame->agendamento->infraestrutura->nome ?? null,
'agendamento_numero' => $exame->agendamento->agendamento_numero ?? null,
'medico' => $exame->agendamento->medico->nome ?? null,
'data' => $exame->agendamento->data->format('d/m/Y') ?? null,
'horario' => $exame->agendamento->horario ?? null,
'data_coleta' => (new Carbon($exame->data_coleta))->format('d/m/Y') ?? null,
'data_laudo' => (new Carbon($exame->data_laudo))->format('d/m/Y') ?? null,
],
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
'infraestrutura' => $currentLotacao->infraestrutura->nome,
'sistema' => [
'ramo_atividade' => [
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
],
'nome' => $cliente->sistema->nome ?? null,
'versao' => $cliente->sistema->versao ?? null,
],
'exame' => [
'exame_nome' => $exame->exame->nome,
'exame_grupo_tipo_id' => $exame->exame->grupo_tipo_id,
'exame_id' => $exame->exame_id,
'exame_status_id' => $exame->status_id,
'agendamento_id' => $exame->agendamento_id,
'agendamento_exame_id' => $exame->id,
'motivo_id' => $exame->motivo_id ?? null,
'observacao' => $exame->observacao ?? null,
'nao_realizado' => $exame->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
'nova_coleta' => $exame->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
'composicao' => ExameComposicao::where('exame_id', $exame->exame_id)
->where('valor_absoluto', null)
->where(function($query) use ($exame) {
$query->where('sexo_id', $exame->agendamento->paciente->cidadao->sexo_id)
->orWhereNull('sexo_id');
})
->where(function ($query) use ($exame) {
$query->whereNull('tipo_idade_id')
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', '!=', null)
->where(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::DIAS)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInDays(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInDays(Carbon::now()));
})
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::MESES)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInMonths(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInMonths(Carbon::now()));
})
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::ANOS)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInYears(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInYears(Carbon::now()));
});
});
})
->get()
->map(function($query) use ($id, $exame) {
return [
'id' => $query->id,
'exame_id' => $query->exame_id,
'descricao' => $query->descricao,
'tipo_resultado_id' => $query->tipo_resultado_id,
'limite_inferior' => number_format($query->limite_inferior, 2, ',', '.'),
'limite_superior' => number_format($query->limite_superior, 2, ',', '.'),
'unidade_id' => $query->unidade_id,
'unidade' => $query->unidade_id ? $query->unidade->descricao : '',
'referencia' => $query->referencia,
'situacao' => null,
'visivel' => $query->visivel,
'valor_absoluto' => $query->valor_absoluto,
'resultado' => $query->tipo_resultado_id == ComposicaoTipo::LISTA
? ExameComposicaoLista::where('id',
AgendamentoExameResultado::where('agendamento_exame_id', $id)
->where('composicao_id', $query->id)
->first()->valor)->first()->descricao ?? null
: AgendamentoExameResultado::where('agendamento_exame_id', $id)
->where('composicao_id', $query->id)
->first()->valor ?? null,
'referencia_especial' => $query->referencia_especial,
'variavel' => ExameComposicaoVariavel::where('composicao_id', $query->id)
->get()
->map(fn ($variavel) => [
'id' => $variavel->id,
'ordem' => $variavel->ordem,
'fator_id' => $variavel->fator_id ?? null,
'fator' => $variavel->fator->fator ?? null,
'valor_absoluto' => self::getValorAbsoluto($variavel->fator_id, $exame->id)
?? null,
]),
];
}),
'exames' => $exames->map(fn ($exameGrupo) => [
'exame_nome' => $exameGrupo->exame_nome,
'exame_id' => $exameGrupo->exame_id,
'exame_status_id' => $exameGrupo->status_id,
'agendamento_id' => $exameGrupo->agendamento_id,
'agendamento_exame_id' => $exameGrupo->agendamento_exame_id,
'motivo_id' => $exameGrupo->motivo_id ?? null,
'observacao' => $exameGrupo->observacao,
'nao_realizado' => $exameGrupo->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
'nova_coleta' => $exameGrupo->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
'composicao' => ExameComposicao::where('exame_id', $exameGrupo->exame_id)
->where('valor_absoluto', null)
->where(function($query) use ($exame) {
$query->where('sexo_id', $exame->agendamento->paciente->cidadao->sexo_id)
->orWhereNull('sexo_id');
})
->where(function ($query) use ($exame) {
$query->whereNull('tipo_idade_id')
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', '!=', null)
->where(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::DIAS)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInDays(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInDays(Carbon::now()));
})
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::MESES)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInMonths(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInMonths(Carbon::now()));
})
->orWhere(function ($idade) use ($exame) {
$idade
->where('tipo_idade_id', MedidaTempo::ANOS)
->where(
'idade_inicial',
'<=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInYears(Carbon::now()))
->where(
'idade_final',
'>=',
Carbon::create($exame->agendamento->paciente->cidadao->data_nascimento
->format('d-m-Y'))->diffInYears(Carbon::now()));
});
});
})
->get()
->map(function($query) use ($exameGrupo) {
return [
'id' => $query->id,
'exame_id' => $query->exame_id,
'descricao' => $query->descricao,
'tipo_resultado_id' => $query->tipo_resultado_id,
'limite_inferior' => number_format($query->limite_inferior, 2, ',', '.'),
'limite_superior' => number_format($query->limite_superior, 2, ',', '.'),
'unidade_id' => $query->unidade_id,
'unidade' => $query->unidade_id ? $query->unidade->descricao : '',
'referencia' => $query->referencia,
'situacao' => null,
'visivel' => $query->visivel,
'valor_absoluto' => $query->valor_absoluto,
'resultado' => $query->tipo_resultado_id == ComposicaoTipo::LISTA
? ExameComposicaoLista::where('id',
AgendamentoExameResultado::where('agendamento_exame_id', $exameGrupo->agendamento_exame_id)
->where('composicao_id', $query->id)
->first()->valor)->first()->descricao ?? null
: AgendamentoExameResultado::where('agendamento_exame_id', $exameGrupo->agendamento_exame_id)
->where('composicao_id', $query->id)
->first()->valor ?? null,
'referencia_especial' => $query->referencia_especial,
'variavel' => ExameComposicaoVariavel::where('composicao_id', $query->id)
->get()
->map(fn ($variavel) => [
'id' => $variavel->id,
'ordem' => $variavel->ordem,
'fator_id' => $variavel->fator_id ?? null,
'fator' => $variavel->fator->fator ?? null,
'valor_absoluto' => self::getValorAbsoluto($variavel->fator_id, $exameGrupo->agendamento_exame_id)
?? null,
]),
];
}),
]),
],
],
];
}
private static function getExame($id)
{
return AgendamentoExame::findOrFail($id);
}
public static function getExames($id, $agendamento_id)
{
$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_exame.observacao AS observacao',
'exame_laboratorial_agendamento_exame.motivo_id AS motivo_id',
'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',
])
->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')
->groupBy([
'exame_laboratorial_agendamento.id',
'parametrizacao_exame.id',
]);
$agendamentos->where('cadastro_grupo.id', $id);
$agendamentos->where('exame_laboratorial_agendamento.id', $agendamento_id);
$agendamentos->whereNotIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::NAO_REALIZADO, StatusAgendamento::NOVA_COLETA] );
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC');
$agendamentos = $agendamentos->get();
return $agendamentos;
}
public static function getValorAbsoluto($composicaoId, $agendamentoId)
{
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
->select(
'exame_laboratorial_agendamento_exame_resultado.valor AS resultado',
)
->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('parametrizacao_exame_composicao', 'parametrizacao_exame_composicao.exame_id', 'exame_laboratorial_agendamento_exame.exame_id')
->join('exame_laboratorial_agendamento_exame_resultado', 'exame_laboratorial_agendamento_exame_resultado.composicao_id', 'parametrizacao_exame_composicao.id')
->join('parametrizacao_exame_composicao_variavel', 'parametrizacao_exame_composicao_variavel.composicao_id', 'parametrizacao_exame_composicao.id')
->join('exame_laboratorial_formula_exame_fator', 'exame_laboratorial_formula_exame_fator.id', 'parametrizacao_exame_composicao_variavel.fator_id');
$agendamentos->where('exame_laboratorial_formula_exame_fator.id', $composicaoId);
$agendamentos->where('exame_laboratorial_agendamento_exame_resultado.agendamento_exame_id', $agendamentoId);
$agendamentos->where('parametrizacao_exame_composicao.valor_absoluto', true);
$agendamentos->where('parametrizacao_exame_composicao.tipo_resultado_id', ComposicaoTipo::FORMULA);
$agendamentos->where('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->limit(1);
$agendamentos = $agendamentos->get();
return $agendamentos;
}
}