Files
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

206 lines
13 KiB
PHP

<?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 ExameResultadoService extends Service
{
public static function index($id)
{
$currentLotacao = self::getCurrentLotacao();
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
$exame = self::getExame($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,
'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) => [
'fator' => $variavel->fator->fator ?? null,
'valor_absoluto' => self::getValorAbsoluto($variavel->fator_id, $exame->id) ?? null,
]),
];
}),
],
],
];
}
private static function getExame($id)
{
return AgendamentoExame::findOrFail($id);
}
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;
}
}