431 lines
25 KiB
PHP
431 lines
25 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\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RelatorioEntregaExameService extends Service
|
|
{
|
|
public static function index(Request $request)
|
|
{
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
|
|
|
$exame = self::getExame($request);
|
|
$exames = self::getExames($request);
|
|
|
|
$cliente = app('config')->get('database.default');
|
|
|
|
self::saveImpressao($exames);
|
|
|
|
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,
|
|
'observacao' => $exame->observacao ?? null,
|
|
'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 ($exame) {
|
|
return [
|
|
'id' => $query->id,
|
|
'exame_id' => $query->exame_id,
|
|
'descricao' => $query->descricao,
|
|
'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' => self::getResultado($query->tipo_resultado_id, $exame->id, $query->id),
|
|
'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,
|
|
'data_assinatura' => $exameGrupo->data_assinatura,
|
|
'arquivo_assinatura' => $exameGrupo->arquivo_assinatura ?
|
|
base64_encode(file_get_contents(storage_path('app/exame-laboratorial/assinaturas/' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $exameGrupo->arquivo_assinatura))) : null,
|
|
|
|
'observacao' => $exameGrupo->observacao,
|
|
'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,
|
|
'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' => self::getResultado($query->tipo_resultado_id, $exameGrupo->agendamento_exame_id, $query->id),
|
|
'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 getResultado($resultadoId, $exameId, $composicaoId)
|
|
{
|
|
|
|
$valor = null;
|
|
|
|
if ($resultadoId == ComposicaoTipo::LISTA) {
|
|
|
|
$valor = AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
|
->where('composicao_id', $composicaoId)
|
|
->first();
|
|
|
|
if ($valor) {
|
|
|
|
$valor = ExameComposicaoLista::where('id', $valor->valor)->first()->descricao;
|
|
}
|
|
|
|
} else {
|
|
|
|
$valor = AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
|
->where('composicao_id', $composicaoId)
|
|
->first();
|
|
|
|
if ($valor) {
|
|
$valor = $valor->valor;
|
|
}
|
|
}
|
|
|
|
return $valor;
|
|
}
|
|
|
|
private static function getExame($request)
|
|
{
|
|
return AgendamentoExame::where('agendamento_id', $request->agendamento_id)
|
|
->whereHas('exame', function($query) use ($request) {
|
|
|
|
if ($request->pertence_grupo == true) {
|
|
|
|
if ($request->mapa_agrupado == true) {
|
|
|
|
$query->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
} else {
|
|
|
|
$query->where(function ($mapa) {
|
|
|
|
$mapa->where('parametrizacao_exame.mapa_agrupado', false)
|
|
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
|
});
|
|
}
|
|
}
|
|
})
|
|
->first();
|
|
}
|
|
|
|
public static function getExames($request)
|
|
{
|
|
$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',
|
|
'cadastro_usuario_assinatura.arquivo AS arquivo_assinatura',
|
|
'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('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_assinatura, "%d/%m/%Y") AS data_assinatura'),
|
|
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')
|
|
->leftJoin('cadastro_usuario_assinatura', 'cadastro_usuario_assinatura.usuario_id', 'exame_laboratorial_agendamento_exame.assinatura_profissional_id')
|
|
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
'parametrizacao_exame.id',
|
|
]);
|
|
|
|
if ($request->pertence_grupo == true) {
|
|
|
|
if ($request->mapa_agrupado == true) {
|
|
|
|
$agendamentos->where('cadastro_grupo.id', $request->exame_grupo_id);
|
|
|
|
$agendamentos->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
} else {
|
|
|
|
$agendamentos->where('parametrizacao_exame.id', $request->exame_grupo_id);
|
|
|
|
$agendamentos->where(function ($query) {
|
|
|
|
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
|
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
|
});
|
|
}
|
|
} else {
|
|
|
|
$agendamentos->where('parametrizacao_exame.id', $request->exame_grupo_id);
|
|
}
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.id', $request->agendamento_id);
|
|
$agendamentos->where('exame_laboratorial_agendamento_exame.assinatura_profissional_id', $request->assinatura_profissional_id);
|
|
$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->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;
|
|
}
|
|
|
|
private static function saveImpressao($exames)
|
|
{
|
|
|
|
$usuario_id = self::getCurrentLotacao()->usuario_id;
|
|
|
|
$exames->map(function ($exame) use ($usuario_id) {
|
|
|
|
$exame = AgendamentoExame::where('agendamento_id', $exame->agendamento_id)
|
|
->where('exame_id', $exame->exame_id)
|
|
->first();
|
|
|
|
$exame->impressao = true;
|
|
$exame->data_impressao = Carbon::now();
|
|
$exame->impressao_usuario_id = $usuario_id;
|
|
|
|
$exame->save();
|
|
});
|
|
}
|
|
}
|