format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); $cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail(); if ($request->grupos) { $agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'cadastro_grupo.id AS exame_grupo_id', 'cadastro_grupo.nome AS exame_grupo_nome', DB::raw('true AS pertence_grupo'), DB::raw('true AS mapa_agrupado'), 'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_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') ->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->groupBy([ 'exame_laboratorial_agendamento.id', 'cadastro_grupo.id', 'exame_laboratorial_agendamento_exame.assinatura_profissional_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); } if ($request->grupos) { $agendamentosComGrupo->where(function($query) use ($request) { foreach ($request->grupos as $grupo) { $query ->orWhere(function ($query) use ($grupo) { $query ->where('exame_laboratorial_agendamento.id', $grupo['agendamento_id']) ->where('cadastro_grupo.id', $grupo['id']) ->where('exame_laboratorial_agendamento_exame.assinatura_profissional_id', $grupo['assinatura_profissional_id']); }); } }); } $agendamentosComGrupo = $agendamentosComGrupo->get()->toArray(); } else { $agendamentosComGrupo = []; } $agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'parametrizacao_exame.id AS exame_grupo_id', 'parametrizacao_exame.nome AS exame_grupo_nome', DB::raw('true AS pertence_grupo'), DB::raw('false AS mapa_agrupado'), 'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_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') ->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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); } if ($request->exames) { $agendamentosSemMapaAgrupado->where(function($query) use ($request) { foreach ($request->exames as $exame) { $query ->orWhere(function ($query) use ($exame) { $query ->where('exame_laboratorial_agendamento.id', $exame['agendamento_id']) ->where('parametrizacao_exame.id', $exame['id']); }); } }); } $agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray(); $agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'parametrizacao_exame.id AS exame_grupo_id', 'parametrizacao_exame.nome AS exame_grupo_nome', DB::raw('false AS pertence_grupo'), 'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_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_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id') ->join('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id') ->join('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); if ($request->exames) { $agendamentosSemGrupo->where(function($query) use ($request) { foreach ($request->exames as $exame) { $query ->orWhere(function ($query) use ($exame) { $query ->where('exame_laboratorial_agendamento.id', $exame['agendamento_id']) ->where('parametrizacao_exame.id', $exame['id']); }); } }); } $agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray(); $agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado); return [ 'form' => [ 'data_atual' => Carbon::now()->format('d/m/Y'), '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, ], 'agendamentos' => self::getAgendamentos($agendamentos) ], ]; } 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 getAgendamentos($agendamentos) { $result = []; foreach($agendamentos as $agendamento) { $exame = self::getExame($agendamento); $exames = self::getExames($agendamento); $cliente = app('config')->get('database.default'); self::saveImpressao($exames); $result[] = array_merge([ '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, ], '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, '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, ]), ]; }), ]), ], ]); } return $result; } private static function getExame($agendamento) { return AgendamentoExame::where('agendamento_id', $agendamento->agendamento_id) ->whereHas('exame', function($query) use ($agendamento) { if ($agendamento->pertence_grupo == true) { if ($agendamento->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($agendamento) { $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 ($agendamento->pertence_grupo == true) { if ($agendamento->mapa_agrupado == true) { $agendamentos->where('cadastro_grupo.id', $agendamento->exame_grupo_id); $agendamentos->where('parametrizacao_exame.mapa_agrupado', true); } else { $agendamentos->where('parametrizacao_exame.id', $agendamento->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', $agendamento->exame_grupo_id); } $agendamentos->where('exame_laboratorial_agendamento.id', $agendamento->agendamento_id); $agendamentos->where('exame_laboratorial_agendamento_exame.assinatura_profissional_id', $agendamento->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(); }); } }