This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameHistorico;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class AgendamentoExameHistoricoService extends Service {
|
||||
|
||||
public static function store($exame, $agendamento, $acao, $justificativa = null) {
|
||||
$historico = new AgendamentoExameHistorico;
|
||||
|
||||
$historico->exame_id = $exame->exame_id;
|
||||
$historico->agendamento_id = $agendamento->id;
|
||||
$historico->acao_id = $acao;
|
||||
$historico->data = (new Carbon($agendamento->data))->format('Y-m-d');
|
||||
$historico->horario = $agendamento->horario;
|
||||
$historico->status_id = $exame->status_id;
|
||||
$historico->justificativa = $justificativa;
|
||||
|
||||
$historico->save();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
3400
api/app/Services/ExameLaboratorial/AgendamentoExameService.php
Normal file
3400
api/app/Services/ExameLaboratorial/AgendamentoExameService.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,775 @@
|
||||
<?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\Enums\Parametrizacao\Exame\Tipo;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameResultado;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExame;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExameFator;
|
||||
use App\Models\ExameLaboratorial\LancamentoColeta\Status;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Motivo;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\ProfissionalAssinatura;
|
||||
use App\Models\Parametrizacao\Exame\ExameComposicao;
|
||||
use App\Models\Parametrizacao\Exame\ExameComposicaoLista;
|
||||
use App\Models\Parametrizacao\Exame\ExameComposicaoVariavel;
|
||||
use App\Models\Sigtap\Competencia;
|
||||
use App\Models\Sigtap\Procedimento;
|
||||
use App\Exceptions\ExameLaboratorial\LotacaoException;
|
||||
use App\Services\Service;
|
||||
use App\Services\Sigtap\ProcedimentoValidationService;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AssinaturaEletronicaService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$lotacao = Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'profissional' => [
|
||||
'id' => $lotacao->usuario->id,
|
||||
'nome' => $lotacao->usuario->cidadao->nome,
|
||||
'cbo' => $lotacao->ocupacao->nome,
|
||||
'ocupacao_id' => $lotacao->ocupacao->id ?? null,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'status' => Status::where(function($query) {
|
||||
$query->where('id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('id', StatusAgendamento::PENDENTE_ASSINATURA)
|
||||
->orWhere('id', StatusAgendamento::ASSINADO)
|
||||
->orWhere('id', StatusAgendamento::NAO_REALIZADO)
|
||||
->orWhere('id', StatusAgendamento::NOVA_COLETA);
|
||||
})
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->map(fn ($status) => [
|
||||
'id' => $status->id,
|
||||
'descricao' => $status->descricao
|
||||
]),
|
||||
'profissionais_assinatura' => ProfissionalAssinatura::all()
|
||||
->map(fn ($profissional) => [
|
||||
'id' => $profissional->usuario_id,
|
||||
'nome' => $profissional->usuario->cidadao->nome,
|
||||
'lotacoes' => $profissional->usuario->lotacoes,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$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.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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('cadastro_unidade.nome_fantasia AS unidade_nome'),
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
"),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.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->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentos->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
if ($request->status_id) {
|
||||
|
||||
$agendamentos->where(function($query) use ($request) {
|
||||
|
||||
foreach ($request->status_id as $status) {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$agendamentos->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id');
|
||||
|
||||
$query->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::PENDENTE_ASSINATURA);
|
||||
});
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
|
||||
$agendamentos = $agendamentos->get()->toArray();
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getAtendimento()
|
||||
{
|
||||
$id = request()->id;
|
||||
|
||||
if(is_numeric($id)){
|
||||
|
||||
$exame = self::getExame($id);
|
||||
$exames = $exame->exame->mapa_agrupado ? self::getExames($exame->exame->grupo_tipo_id, $exame->agendamento->id) : collect();
|
||||
}
|
||||
|
||||
$lotacao = self::getLotacaoLogado();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'profissional' => [
|
||||
'id' => $lotacao->usuario->id,
|
||||
'nome' => $lotacao->usuario->cidadao->nome,
|
||||
'cbo' => $lotacao->ocupacao->nome,
|
||||
'ocupacao_id' => $lotacao->ocupacao->id ?? null,
|
||||
],
|
||||
'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')
|
||||
: '',
|
||||
'idade' => $exame->agendamento->paciente->cidadao->data_nascimento
|
||||
? "{$exame->agendamento->paciente->cidadao->idade['anos']} ano(s) "
|
||||
. "{$exame->agendamento->paciente->cidadao->idade['meses']} mês(s) "
|
||||
. "{$exame->agendamento->paciente->cidadao->idade['dias']} dia(s) "
|
||||
: '',
|
||||
'sexo' => $exame->agendamento->paciente->cidadao->sexo ?? '-',
|
||||
'telefone' => MaskUtil::getPhoneMasked($exame->agendamento->paciente->cidadao->telefone_principal->numero ?? ''),
|
||||
],
|
||||
'data_agendamento' => (new Carbon($exame->agendamento->data))->format('d/m/Y'),
|
||||
'data_coleta' => (new Carbon($exame->data_coleta))->format('d/m/Y'),
|
||||
'medico' => $exame->agendamento->medico()->withTrashed()->first()->nome,
|
||||
'uso_medicamento' => $exame->agendamento->uso_medicamento ?? 'Não informado',
|
||||
'suspeita_clinica' => $exame->agendamento->suspeita_clinica ?? 'Não informada',
|
||||
'observacao' => $exame->agendamento->observacao ?? 'Não informada',
|
||||
'motivo' => Motivo::get()
|
||||
->map(fn($query) => [
|
||||
'id' => $query->id ?? null,
|
||||
'descricao' => $query->descricao ?? null,
|
||||
]),
|
||||
'competencia' => Competencia::orderByDesc('valor')->take(4)->get(),
|
||||
],
|
||||
'model' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'agendamentosNotSave' => null,
|
||||
'exame_nome' => $exame->exame->nome,
|
||||
'exame_grupo_nome' => $exame->exame->grupo->nome,
|
||||
'exame_grupo_tipo_id' => $exame->exame->grupo_tipo_id,
|
||||
'procedimento_id' => $exame->exame->getProcedimentoAttribute()->id ?? null,
|
||||
'procedimento_competencia' => $exame->exame->getProcedimentoAttribute()->competencia ?? null,
|
||||
'competencia' => $exame->exame->procedimento
|
||||
? self::getCompetencia($exame->agendamento->data->format('Ym'), $exame->exame->procedimento->codigo,
|
||||
$exame->agendamento->laboratorio_infraestrutura_id) : null,
|
||||
'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,
|
||||
'assinatura_profissional_id' => $exame->assinatura_profissional_id ?? null,
|
||||
'nao_realizado' => $exame->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
|
||||
'nova_coleta' => $exame->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
|
||||
'observacao' => $exame->observacao,
|
||||
'composicao' => self::getComposicao($exame, $exame->exame_id, $exame->id),
|
||||
'historico' => self::getHistorico($exame->exame_id, $exame->agendamento_id, $exame->agendamento->paciente->id)
|
||||
->map(fn ($historico) => [
|
||||
'data' => $historico->data,
|
||||
'exame_nome' => $historico->exame_nome,
|
||||
'exame_id' => $historico->exame_id,
|
||||
'observacao' => $historico->observacao,
|
||||
'composicao' => self::getComposicaoHistorico($exame, $historico->agendamento_exame_id)
|
||||
]),
|
||||
'exames' => $exames->map(fn ($exameGrupo) => [
|
||||
'exame_nome' => $exameGrupo->exame_nome,
|
||||
'exame_id' => $exameGrupo->exame_id,
|
||||
'exame_status_id' => $exameGrupo->status_id,
|
||||
'procedimento_id' => $exameGrupo->procedimento_id ?? null,
|
||||
'agendamento_id' => $exameGrupo->agendamento_id,
|
||||
'agendamento_exame_id' => $exameGrupo->agendamento_exame_id,
|
||||
'mapa_agrupado' => $exameGrupo->mapa_agrupado,
|
||||
'motivo_id' => $exameGrupo->motivo_id ?? null,
|
||||
'competencia' => $exameGrupo->procedimento_codigo
|
||||
? self::getCompetencia($exameGrupo->data, $exameGrupo->procedimento_codigo,
|
||||
$exame->agendamento->laboratorio_infraestrutura_id)
|
||||
: null,
|
||||
'assinatura_profissional_id' => $exameGrupo->assinatura_profissional_id ?? null,
|
||||
'nao_realizado' => $exameGrupo->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
|
||||
'nova_coleta' => $exameGrupo->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
|
||||
'observacao' => $exameGrupo->observacao,
|
||||
'historico' => self::getHistorico($exameGrupo->exame_id, $exameGrupo->agendamento_id, $exame->agendamento->paciente->id)
|
||||
->map(fn ($historico) => [
|
||||
'data' => $historico->data,
|
||||
'exame_nome' => $historico->exame_nome,
|
||||
'exame_id' => $historico->exame_id,
|
||||
'observacao' => $historico->observacao,
|
||||
'composicao' => self::getComposicaoHistorico($exame, $historico->agendamento_exame_id)
|
||||
]),
|
||||
'composicao' => self::getComposicao($exame, $exameGrupo->exame_id, $exameGrupo->agendamento_exame_id)
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getComposicao($exame, $exameGrupo, $agendamentoExameId)
|
||||
{
|
||||
|
||||
return ExameComposicao::where('exame_id', $exameGrupo)
|
||||
->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 ($agendamentoExameId) {
|
||||
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,
|
||||
'tipo_idade_id' => $query->tipo_idade_id,
|
||||
'tipo_idade' => $query->tipo_idade_id ? $query->tipoIdade->descricao : '-',
|
||||
'idade_inicial' => $query->idade_inicial,
|
||||
'idade_final' => $query->idade_final,
|
||||
'sexo_id' => $query->sexo_id,
|
||||
'sexo' => $query->sexo->descricao ?? 'AMBOS',
|
||||
'tipo_resultado_id' => $query->tipo_resultado_id,
|
||||
'tipo_resultado' => $query->tipoResultado->descricao,
|
||||
'casa_decimal_id' => $query->casa_decimal_id,
|
||||
'formula_id' => $query->formula_id,
|
||||
'valor' => ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->where('padrao', true)
|
||||
->first()->id ?? null,
|
||||
'situacao' => null,
|
||||
'visivel' => $query->visivel,
|
||||
'valor_absoluto' => $query->valor_absoluto,
|
||||
'resultado' => AgendamentoExameResultado::where('agendamento_exame_id', $agendamentoExameId)
|
||||
->where('composicao_id', $query->id)->first()->valor
|
||||
?? ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->where('padrao', true)->first()->id
|
||||
?? null,
|
||||
'referencia_especial' => $query->referencia_especial,
|
||||
'lista' => ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->orderBy('ordem')
|
||||
->get()
|
||||
->map(fn ($composicao) => [
|
||||
'id' => $composicao->id,
|
||||
'descricao' => $composicao->descricao,
|
||||
'ordem' => $composicao->ordem,
|
||||
'situacao_id' => $composicao->situacao_id,
|
||||
'situacao' => ucfirst(strtolower($composicao->situacao->descricao)),
|
||||
'padrao' => $composicao->padrao,
|
||||
]),
|
||||
'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,
|
||||
]),
|
||||
'formula' => FormulaExame::where('id', $query->formula_id)
|
||||
->get()
|
||||
->map(fn ($formula) => [
|
||||
'id' => $formula->id,
|
||||
'nome' => $formula->nome,
|
||||
'fatores' => FormulaExameFator::where('formula_exame_id', $formula->id)
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'formula_exame_id' => $query->formula_exame_id,
|
||||
'fator' => $query->fator,
|
||||
'operacao_id' => $query->operacao_id,
|
||||
'operacao_descricao' => $query->operacao->descricao,
|
||||
'tipo' => $query->tipo,
|
||||
'valor' => $query->valor ? number_format($query->valor, 6, ',', '.') : null,
|
||||
'exponenciacao' => $query->exponenciacao ? true : false,
|
||||
'exponenciacao_tipo' => $query->exponenciacao_tipo,
|
||||
'exponenciacao_valor' => $query->exponenciacao_valor
|
||||
? number_format($query->exponenciacao_valor, 6, ',', '.')
|
||||
: null,
|
||||
'ordem' => $query->ordem,
|
||||
];
|
||||
})->values(),
|
||||
]),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function getComposicaoHistorico($exame, $exameId)
|
||||
{
|
||||
|
||||
return AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->whereHas('composicao', function($query) use ($exame) {
|
||||
|
||||
$query->where('sexo_id', $exame->agendamento->paciente->cidadao->sexo->id)
|
||||
->orWhereNull('sexo_id');
|
||||
|
||||
$query->where(function ($idade) use ($exame) {
|
||||
|
||||
$idade
|
||||
->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()))
|
||||
->orWhereNull('tipo_idade_id');
|
||||
})
|
||||
->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()))
|
||||
->orWhereNull('tipo_idade_id');
|
||||
})
|
||||
->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()))
|
||||
->orWhereNull('tipo_idade_id');
|
||||
});
|
||||
});
|
||||
})
|
||||
->get()
|
||||
->map(function($query) use ($exameId) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'descricao' => $query->composicao->descricao,
|
||||
'tipo_resultado_id' => $query->composicao->tipo_resultado_id,
|
||||
'resultado' =>
|
||||
$query->composicao->tipo_resultado_id == ComposicaoTipo::LISTA
|
||||
? ExameComposicaoLista::where('id',
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->where('composicao_id', $query->composicao->id)->first()->valor
|
||||
)->first()->descricao ?? null
|
||||
: AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->where('composicao_id', $query->composicao->id)
|
||||
->first()->valor ?? null,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function getExame($id)
|
||||
{
|
||||
return AgendamentoExame::findOrFail($id);
|
||||
}
|
||||
|
||||
private static function getCompetencia($data, $codigo, $infraestruturaId)
|
||||
{
|
||||
|
||||
$competencias = Competencia::orderByDesc('valor')->take(4)->get();
|
||||
$competencia = $competencias->firstWhere('valor', $data);
|
||||
|
||||
if (is_null($competencia)) {
|
||||
|
||||
$competencia = $competencias->first();
|
||||
};
|
||||
|
||||
$procedimentoServiceValidation = new ProcedimentoValidationService(
|
||||
Procedimento::where('codigo', $codigo)
|
||||
->where('competencia_id', $competencia->id)
|
||||
->first()
|
||||
);
|
||||
|
||||
$lotacao = Lotacao::where('usuario_id', request()->profissional_id)
|
||||
->where('infraestrutura_id', $infraestruturaId)
|
||||
->first();
|
||||
|
||||
if(is_null($lotacao)){
|
||||
throw new LotacaoException;
|
||||
}
|
||||
|
||||
return $procedimentoServiceValidation->isOcupacaoValid($lotacao->ocupacao);
|
||||
}
|
||||
|
||||
private static function getLotacaoLogado()
|
||||
{
|
||||
return Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
|
||||
}
|
||||
|
||||
public static function getExames($id, $agendamentoId)
|
||||
{
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento_exame.id AS agendamento_exame_id',
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y%m") AS data'),
|
||||
'exame_laboratorial_agendamento_exame.motivo_id AS motivo_id',
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.observacao AS observacao',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'parametrizacao_exame.mapa_agrupado AS mapa_agrupado',
|
||||
'cadastro_grupo.id AS grupo_id',
|
||||
'cadastro_grupo.nome AS grupo_nome',
|
||||
'sigtap.procedimento.id as procedimento_id',
|
||||
'sigtap.procedimento.nome as procedimento_nome',
|
||||
'sigtap.procedimento.codigo as procedimento_codigo',
|
||||
'sigtap.procedimento.quantidade_maxima_execucao',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->leftJoin(
|
||||
'parametrizacao_procedimento',
|
||||
'parametrizacao_procedimento.procedimento_codigo',
|
||||
'parametrizacao_exame.codigo'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('cadastro_grupo.id', $id);
|
||||
$agendamentos->where('exame_laboratorial_agendamento.id', $agendamentoId);
|
||||
$agendamentos->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
$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 getHistorico($id, $agendamentoId, $pacienteId)
|
||||
{
|
||||
$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',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_laudo, "%d/%m/%Y") AS data'),
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('parametrizacao_exame.id', $id);
|
||||
$agendamentos->where('exame_laboratorial_agendamento.id', '!=', $agendamentoId);
|
||||
$agendamentos->where('cadastro_paciente.id', $pacienteId);
|
||||
$agendamentos->whereNotNull('exame_laboratorial_agendamento_exame.data_laudo');
|
||||
|
||||
$agendamentos->whereNotIn('exame_laboratorial_agendamento_exame.status_id',
|
||||
[StatusAgendamento::NAO_REALIZADO, StatusAgendamento::NOVA_COLETA]);
|
||||
|
||||
$agendamentos->where(function ($q) {
|
||||
$q->whereNotIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::ASSINADO])
|
||||
->orWhere(function ($sub) {
|
||||
$sub->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO)
|
||||
->where('entregue', true);
|
||||
});
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
return $agendamentos;
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$grupos = DB::table('parametrizacao_exame')
|
||||
->select([
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
])
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereExists(function ($query) use ($request, $currentDate) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->where(function($query) use ($request, $currentDate) {
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
});
|
||||
})
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$grupos = $grupos->get()->toArray();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'grupos' => $grupos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::saveStatusExame($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function saveStatusExame($request)
|
||||
{
|
||||
|
||||
foreach ($request->agendamentos as $params) {
|
||||
|
||||
$exame = AgendamentoExame::where('agendamento_id', $params['agendamento_id'])
|
||||
->where('exame_id', $params['exame_id'])
|
||||
->first();
|
||||
|
||||
if ($params['nao_realizado']) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::NAO_REALIZADO;
|
||||
$exame->motivo_id = $params['motivo_id'];
|
||||
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])->delete();
|
||||
}
|
||||
|
||||
if ($params['nova_coleta']) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::NOVA_COLETA;
|
||||
$exame->motivo_id = $params['motivo_id'];
|
||||
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])->delete();
|
||||
}
|
||||
|
||||
if (!$params['nao_realizado'] && !$params['nova_coleta']) {
|
||||
|
||||
if ($params['exame_status_id'] == StatusAgendamento::ASSINADO) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::ASSINADO;
|
||||
$exame->assinatura_profissional_id = $params['assinatura_profissional_id'];
|
||||
$exame->data_assinatura = Carbon::now();
|
||||
} else {
|
||||
|
||||
$exame->status_id = StatusAgendamento::PENDENTE_ASSINATURA;
|
||||
$exame->data_laudo = Carbon::now();
|
||||
}
|
||||
}
|
||||
|
||||
$exame->observacao = $params['observacao'];
|
||||
$exame->save();
|
||||
|
||||
if (!$params['nao_realizado'] && !$params['nova_coleta']) {
|
||||
|
||||
foreach ($params['composicao'] as $paramsComposicao) {
|
||||
|
||||
if ($paramsComposicao['resultado'] !== null) {
|
||||
|
||||
$exameComposicao = AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])
|
||||
->where('composicao_id', $paramsComposicao['id'])
|
||||
->first() ?? new AgendamentoExameResultado;
|
||||
|
||||
$exameComposicao->agendamento_exame_id = $params['agendamento_exame_id'];
|
||||
$exameComposicao->composicao_id = $paramsComposicao['id'];
|
||||
$exameComposicao->valor = $paramsComposicao['resultado'];
|
||||
|
||||
$exameComposicao->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameResultado;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CancelamentoResultadoService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id');
|
||||
|
||||
$agendamentosComGrupo->whereNotNull('parametrizacao_exame.grupo_tipo_id');
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
$agendamentosComGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, 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) {
|
||||
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->whereNotNull('parametrizacao_exame.grupo_tipo_id');
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function ($query) {
|
||||
|
||||
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
||||
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
||||
});
|
||||
|
||||
$agendamentosSemMapaAgrupado->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, 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->exame_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.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->exame_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'entregue' => [
|
||||
['value' => 1, 'text' => 'SIM'],
|
||||
['value' => 0, 'text' => 'NÃO']
|
||||
],
|
||||
],
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->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_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id');
|
||||
|
||||
$agendamentosComGrupo->whereNotNull('parametrizacao_exame.grupo_tipo_id');
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
$agendamentosComGrupo->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, 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);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->whereNotNull('parametrizacao_exame.grupo_tipo_id');
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function ($query) {
|
||||
|
||||
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
||||
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
||||
});
|
||||
|
||||
$agendamentosSemMapaAgrupado->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, 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);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('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->whereIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO]);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function destroy(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
$agendamento = AgendamentoExame::where('agendamento_id', $request->agendamento_id)
|
||||
->where('exame_id', $request->exame_id)
|
||||
->first();
|
||||
|
||||
$agendamento->entregue = false;
|
||||
$agendamento->impressao = false;
|
||||
$agendamento->data_entregue = null;
|
||||
$agendamento->data_laudo = null;
|
||||
$agendamento->data_assinatura = null;
|
||||
$agendamento->data_impressao = null;
|
||||
$agendamento->assinatura_profissional_id = null;
|
||||
$agendamento->impressao_usuario_id = null;
|
||||
$agendamento->entregue_usuario_id = null;
|
||||
$agendamento->status_id = StatusAgendamento::REALIZADO;
|
||||
|
||||
$agendamento->save();
|
||||
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $agendamento->id)->delete();
|
||||
});
|
||||
}
|
||||
}
|
||||
1439
api/app/Services/ExameLaboratorial/ColetaAgendadaService.php
Normal file
1439
api/app/Services/ExameLaboratorial/ColetaAgendadaService.php
Normal file
File diff suppressed because it is too large
Load Diff
465
api/app/Services/ExameLaboratorial/EntregaExameService.php
Normal file
465
api/app/Services/ExameLaboratorial/EntregaExameService.php
Normal file
@@ -0,0 +1,465 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EntregaExameService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'entregue' => [
|
||||
['value' => 1, 'text' => 'SIM'],
|
||||
['value' => 0, 'text' => 'NÃO']
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('true AS mapa_agrupado'),
|
||||
DB::raw('false AS selecionado'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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) {
|
||||
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('false AS mapa_agrupado'),
|
||||
DB::raw('false AS selecionado'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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->exame_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
DB::raw('false AS selecionado'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame.impressao THEN "SIM" ELSE "NAO" END AS impressao'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('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->exame_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('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);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::saveEntregue($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function saveEntregue($request)
|
||||
{
|
||||
|
||||
$usuario_id = self::getCurrentLotacao()->usuario_id;
|
||||
|
||||
foreach ($request->agendamentos as $params) {
|
||||
|
||||
$exame = AgendamentoExame::where('agendamento_id', $params['agendamento_id'])
|
||||
->where('exame_id', $params['exame_id'])
|
||||
->first();
|
||||
|
||||
$exame->entregue = $params['entregue'];
|
||||
|
||||
if ($params['entregue']) {
|
||||
|
||||
$exame->data_entregue = Carbon::now();
|
||||
$exame->entregue_usuario_id = $usuario_id;
|
||||
}
|
||||
|
||||
$exame->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ExameAgendadoRealizadoService extends Service
|
||||
{
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
DB::raw("
|
||||
(
|
||||
SELECT GROUP_CONCAT(cadastro_cidadao_telefone.numero SEPARATOR ', ')
|
||||
FROM cadastro_cidadao_telefone
|
||||
WHERE cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
||||
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
||||
) AS telefone
|
||||
"),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_municipio.nome,
|
||||
" - ",
|
||||
cadastro_estado.uf
|
||||
) AS municipio'),
|
||||
])
|
||||
->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_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id')
|
||||
->leftJoin('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id')
|
||||
->leftJoin('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
||||
->leftJoin('intranet.cadastro_municipio', 'cadastro_municipio.id', 'cadastro_logradouro.municipio_id')
|
||||
->leftJoin('intranet.cadastro_estado', 'cadastro_estado.id', 'cadastro_municipio.estado_id')
|
||||
->groupBy([
|
||||
'cadastro_paciente.id',
|
||||
])
|
||||
->limit(10);
|
||||
|
||||
if ($request->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
||||
'data_nascimento' => $agendamento->data_nascimento ?? null,
|
||||
'nome_mae' => $agendamento->nome_mae ?? null,
|
||||
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
||||
'paciente_id' => $agendamento->paciente_id ?? null,
|
||||
'paciente_preferencia_nome_id' => $agendamento->paciente_preferencia_nome_id ?? null,
|
||||
'paciente_nome_social' => $agendamento->paciente_nome_social ?? null,
|
||||
'paciente_prontuario' => $agendamento->paciente_prontuario ?? null,
|
||||
'assinatura_profissional_id' => $agendamento->assinatura_profissional_id ?? null,
|
||||
'municipio' => $agendamento->municipio ?? null,
|
||||
'telefones' => Paciente::findOrFail($agendamento->paciente_id)->cidadao->telefones
|
||||
->map(fn ($t) => MaskUtil::getPhoneMasked($t->numero))->all()
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
|
||||
$agendamento = Agendamento::where('paciente_id', $id)->firstOrFail();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente' => [
|
||||
'nome' => $agendamento->paciente->cidadao->nome ?? null,
|
||||
'nomeSocial' => $agendamento->paciente->cidadao->nome_social,
|
||||
'prefereNomeSocial' => $agendamento->paciente->cidadao
|
||||
->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'data_nascimento' => $agendamento->paciente->cidadao->data_nascimento
|
||||
? $agendamento->paciente->cidadao->data_nascimento->format('d/m/Y')
|
||||
: '',
|
||||
'cpf' => $agendamento->paciente->cidadao->cpf
|
||||
? MaskUtil::getCpfMasked($agendamento->paciente->cidadao->cpf) : null,
|
||||
'rg' => $agendamento->paciente->cidadao->carteiraIdentidade->numero ?? null,
|
||||
'convenio_medico_nome' => $agendamento->paciente->cidadao->conveniosMedico->first()->nome ?? null,
|
||||
'situacao_familiar' => $agendamento->paciente->cidadao->situacaoFamiliarConjugal->descricao ?? null,
|
||||
'prontuario' => $agendamento->paciente->prontuario,
|
||||
'unidade' => $agendamento->paciente->unidade->nome_fantasia ?? null,
|
||||
'sexo' => $agendamento->paciente->cidadao->sexo ?? '-',
|
||||
'classificacao_id' => $agendamento->paciente->classificacao_id ?? '-',
|
||||
'classificacao_nome' => $agendamento->paciente->classificacao->descricao ?? '-',
|
||||
'cns' => $agendamento->paciente->cidadao->cns ? MaskUtil::getCnsMasked($agendamento->paciente->cidadao->cns) : '',
|
||||
'telefones' => $agendamento->paciente->cidadao->telefones
|
||||
->map(fn ($t) => MaskUtil::getPhoneMasked($t->numero))->all()
|
||||
],
|
||||
'endereco' => [
|
||||
'estado_id' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id
|
||||
: null,
|
||||
'municipio' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio
|
||||
: null,
|
||||
'logradouro' => isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro
|
||||
: null,
|
||||
'cep' => MaskUtil::getCepMasked(
|
||||
isset($agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep
|
||||
: null
|
||||
),
|
||||
'numero' => isset($agendamento->paciente->cidadao->domicilio->endereco->numero)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->numero
|
||||
: null,
|
||||
'sem_numero' => isset($agendamento->paciente->cidadao->domicilio->endereco->sem_numero)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->sem_numero
|
||||
: null,
|
||||
'complemento' => isset($agendamento->paciente->cidadao->domicilio->endereco->complemento)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->complemento
|
||||
: '',
|
||||
'zona_id' => isset($agendamento->paciente->cidadao->domicilio->endereco->zona_id)
|
||||
? $agendamento->paciente->cidadao->domicilio->endereco->zona_id
|
||||
: null,
|
||||
],
|
||||
],
|
||||
'model' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'agendados' => Agendamento::where('paciente_id', $id)
|
||||
->whereHas('agendamentoExame', function($query) {
|
||||
$query->whereNull('status_id')
|
||||
->OrWhere('status_id', StatusAgendamento::AGENDADO);
|
||||
})
|
||||
->get()
|
||||
->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'data' => (new Carbon($agendamento->created_at))->format('d/m/Y') ?? null,
|
||||
'data_coleta' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
||||
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame->nome,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'valor' => $query->valor,
|
||||
'status' => $query->status->descricao ?? 'AGENDADO',
|
||||
'status_id' => $query->status_id,
|
||||
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'medico_id' => $agendamento->medico_id,
|
||||
'medico_nome' => $agendamento->medico->nome,
|
||||
'observacao' => $agendamento->observacao,
|
||||
'paciente_id' => $agendamento->paciente_id,
|
||||
'suspeita' => $agendamento->suspeita_clinica,
|
||||
'unidade' => $agendamento->unidade->nome_fantasia,
|
||||
'laboratorioUnidade' => $agendamento->laboratorioUnidade->nome_fantasia,
|
||||
'infraestrutura' => $agendamento->infraestrutura->nome,
|
||||
'laboratorioInfraestrutura' => $agendamento->laboratorioInfraestrutura->nome,
|
||||
'medicamentos' => $agendamento->uso_medicamento,
|
||||
])
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
'finalizados' => Agendamento::where('paciente_id', $id)
|
||||
->whereHas('agendamentoExame', function($query) {
|
||||
|
||||
$query
|
||||
->whereNotIn('status_id', [StatusAgendamento::AGENDADO])
|
||||
->whereNotNull('status_id');
|
||||
})
|
||||
->get()
|
||||
->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'data' => (new Carbon($agendamento->created_at))->format('d/m/Y') ?? null,
|
||||
'data_coleta' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
||||
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame->nome,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'valor' => $query->valor,
|
||||
'status' => self::getStatus($query),
|
||||
'status_id' => $query->status_id,
|
||||
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'medico_id' => $agendamento->medico_id,
|
||||
'medico_nome' => $agendamento->medico->nome,
|
||||
'observacao' => $agendamento->observacao,
|
||||
'paciente_id' => $agendamento->paciente_id,
|
||||
'suspeita' => $agendamento->suspeita_clinica,
|
||||
'unidade' => $agendamento->unidade->nome_fantasia,
|
||||
'laboratorioUnidade' => $agendamento->laboratorioUnidade->nome_fantasia,
|
||||
'infraestrutura' => $agendamento->infraestrutura->nome,
|
||||
'laboratorioInfraestrutura' => $agendamento->laboratorioInfraestrutura->nome,
|
||||
'medicamentos' => $agendamento->uso_medicamento,
|
||||
])
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
return $query->status->descricao ?? null;
|
||||
}
|
||||
}
|
||||
407
api/app/Services/ExameLaboratorial/ExameProducaoService.php
Normal file
407
api/app/Services/ExameLaboratorial/ExameProducaoService.php
Normal file
@@ -0,0 +1,407 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ExameProducaoService extends Service
|
||||
{
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$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',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("SUM(parametrizacao_exame.quantidade_reagente) AS quantidade_reagente"),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'parametrizacao_exame.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->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);
|
||||
}
|
||||
|
||||
$agendamentos->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
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' => [
|
||||
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
||||
'coleta_nome_fantasia' => $agendamento->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $agendamento->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome,
|
||||
'exame_nome' => $agendamento->exame_nome,
|
||||
'procedimento_codigo' => !empty($agendamento->procedimento_codigo)
|
||||
? $agendamento->procedimento_codigo : '-',
|
||||
'quantidade_reagente' => $agendamento->quantidade_reagente ?? '0',
|
||||
'quantidade' => $agendamento->quantidade,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw('upper(parametrizacao_exame.id) AS exame_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'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
]);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
$exames->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$exames = $exames->orderBy('exame_nome')->get();
|
||||
|
||||
$unidades = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'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',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_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.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);
|
||||
}
|
||||
|
||||
$unidades->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$unidades = $unidades->get();
|
||||
|
||||
$laboratorios = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'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.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);
|
||||
}
|
||||
|
||||
$laboratorios->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$laboratorios = $laboratorios->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'unidades' => $unidades->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,
|
||||
]),
|
||||
'laboratorios' => $laboratorios->map(fn ($laboratorio) => [
|
||||
'laboratorio_id' => $laboratorio->laboratorio_unidade_id,
|
||||
'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome,
|
||||
'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id,
|
||||
'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'id' => $exame->exame_id,
|
||||
'nome' => mb_strtoupper($exame->exame_nome),
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
205
api/app/Services/ExameLaboratorial/ExameResultadoService.php
Normal file
205
api/app/Services/ExameLaboratorial/ExameResultadoService.php
Normal file
@@ -0,0 +1,205 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
1256
api/app/Services/ExameLaboratorial/ExameSolicitadoStatusService.php
Normal file
1256
api/app/Services/ExameLaboratorial/ExameSolicitadoStatusService.php
Normal file
File diff suppressed because it is too large
Load Diff
210
api/app/Services/ExameLaboratorial/FormulaExameService.php
Normal file
210
api/app/Services/ExameLaboratorial/FormulaExameService.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExame;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExameFator;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\Operacao;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FormulaExameService extends Service
|
||||
{
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
$FormulaExame = DB::table('exame_laboratorial_formula_exame')
|
||||
->leftJoin(
|
||||
'exame_laboratorial_formula_exame_fator',
|
||||
'exame_laboratorial_formula_exame_fator.formula_exame_id',
|
||||
'exame_laboratorial_formula_exame.id'
|
||||
)
|
||||
->groupBy('exame_laboratorial_formula_exame.id')
|
||||
->select(
|
||||
'exame_laboratorial_formula_exame.id',
|
||||
'exame_laboratorial_formula_exame.nome',
|
||||
DB::raw('group_concat(exame_laboratorial_formula_exame_fator.fator separator ", ") as fatores'),
|
||||
)
|
||||
->whereNull('exame_laboratorial_formula_exame.deleted_at');
|
||||
|
||||
return $FormulaExame;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'operacoes' => Operacao::orderBy('id')
|
||||
->get()
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'descricao' => $query->descricao,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
$formula_exame = new FormulaExame;
|
||||
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
$formula_exame = FormulaExame::create($request->all());
|
||||
|
||||
$formula_exame_id = $formula_exame->id;
|
||||
|
||||
foreach ($request['fatores'] as $param) {
|
||||
|
||||
$fator = new FormulaExameFator();
|
||||
|
||||
$fator->fill($param);
|
||||
|
||||
$fator->formula_exame_id = $formula_exame_id;
|
||||
$fator->valor = $param['valor'];
|
||||
|
||||
if (!is_null($param['valor'])) {
|
||||
$fator->valor = str_replace('.', '', $fator->valor);
|
||||
$fator->valor = str_replace(',', '.', $fator->valor);
|
||||
}
|
||||
|
||||
$fator->exponenciacao_valor = $param['exponenciacao_valor'];
|
||||
|
||||
if (!is_null($param['exponenciacao_valor'])) {
|
||||
$fator->exponenciacao_valor = str_replace('.', '', $fator->exponenciacao_valor);
|
||||
$fator->exponenciacao_valor = str_replace(',', '.', $fator->exponenciacao_valor);
|
||||
}
|
||||
|
||||
$fator->save();
|
||||
}
|
||||
});
|
||||
|
||||
return $formula_exame;
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$FormulaExame = FormulaExame::findOrFail($id);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'operacoes' => Operacao::orderBy('id')
|
||||
->get()
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'descricao' => $query->descricao,
|
||||
]),
|
||||
],
|
||||
'model' => [
|
||||
'id' => $FormulaExame->id,
|
||||
'nome' => $FormulaExame->nome,
|
||||
'fatores' => FormulaExameFator::where('formula_exame_id', $FormulaExame->id)
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'formula_exame_id' => $query->formula_exame_id,
|
||||
'fator' => $query->fator,
|
||||
'operacao_id' => $query->operacao_id,
|
||||
'operacao_descricao' => $query->operacao->descricao,
|
||||
'tipo' => $query->tipo,
|
||||
'valor' => $query->valor ? number_format($query->valor, 6, ',', '.') : null,
|
||||
'exponenciacao' => $query->exponenciacao ? true : false,
|
||||
'exponenciacao_tipo' => $query->exponenciacao_tipo,
|
||||
'exponenciacao_valor' => $query->exponenciacao_valor
|
||||
? number_format($query->exponenciacao_valor, 6, ',', '.')
|
||||
: null,
|
||||
'ordem' => $query->ordem,
|
||||
];
|
||||
})->values(),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
$FormulaExame = FormulaExame::findOrFail($id);
|
||||
|
||||
DB::transaction(function () use ($request, $FormulaExame, $id) {
|
||||
$FormulaExame->update($request->all());
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($request['fatores'] as $param) {
|
||||
|
||||
$fator = FormulaExameFator::where('id', $param['id'])
|
||||
->first() ?? new FormulaExameFator();
|
||||
|
||||
$fator->fill($param);
|
||||
|
||||
$fator->formula_exame_id = $id;
|
||||
$fator->valor = $param['valor'];
|
||||
|
||||
if (!is_null($param['valor'])) {
|
||||
$fator->valor = str_replace('.', '', $fator->valor);
|
||||
$fator->valor = str_replace(',', '.', $fator->valor);
|
||||
}
|
||||
|
||||
$fator->exponenciacao_valor = $param['exponenciacao_valor'];
|
||||
|
||||
if (!is_null($param['exponenciacao_valor'])) {
|
||||
$fator->exponenciacao_valor = str_replace('.', '', $fator->exponenciacao_valor);
|
||||
$fator->exponenciacao_valor = str_replace(',', '.', $fator->exponenciacao_valor);
|
||||
}
|
||||
|
||||
$fator->save();
|
||||
|
||||
$ids[] = $fator->id;
|
||||
}
|
||||
|
||||
FormulaExameFator::where('formula_exame_id', $id)->whereNotIn('id', $ids)->delete();
|
||||
});
|
||||
|
||||
return $FormulaExame;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
FormulaExame::findOrFail($id)->delete();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$validations = [
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
],
|
||||
'fatores.*.fator' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,50',
|
||||
],
|
||||
'fatores.*.operacao_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'fatores.*.ordem' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'nome.required' => 'Nome é obrigatório.',
|
||||
'fatores.*.fator' => 'Fator é obrigatório.',
|
||||
'fatores.*.operacao_id' => 'Operação é obrigatória.',
|
||||
'fatores.*.ordem' => 'Ordem é obrigatória.',
|
||||
];
|
||||
|
||||
return $request->validate($validations, $messages);
|
||||
}
|
||||
}
|
||||
401
api/app/Services/ExameLaboratorial/ImportarLaudoService.php
Normal file
401
api/app/Services/ExameLaboratorial/ImportarLaudoService.php
Normal file
@@ -0,0 +1,401 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Exceptions\ExameLaboratorial\CriacaoArquivoException;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoLaudo;
|
||||
use App\Models\WebService\Usuario as WebServiceUsuario;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ImportarLaudoService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$lotacao = Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'profissional' => [
|
||||
'id' => $lotacao->usuario->id,
|
||||
'cidadao_nome' => $lotacao->usuario->cidadao->nome,
|
||||
],
|
||||
'unidade_id' => $lotacao->infraestrutura->unidade->id,
|
||||
'infraestrutura_id' => $lotacao->infraestrutura->id,
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'status' => [
|
||||
['value' => 'laudo_importado', 'text' => 'LAUDO IMPORTADO'],
|
||||
['value' => 'laudo_nao_importado', 'text' => 'LAUDO NÃO IMPORTADO']
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$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_laudo.arquivo AS laudo',
|
||||
'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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('
|
||||
CASE WHEN exame_laboratorial_agendamento_laudo.arquivo IS NOT NULL
|
||||
THEN "LAUDO IMPORTADO"
|
||||
ELSE "LAUDO NÃO IMPORTADO" END AS status'
|
||||
),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_laudo', 'exame_laboratorial_agendamento_laudo.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_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->unidade_id && $request->infraestrutura_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', $request->unidade_id);
|
||||
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->status && $request->status == 'laudo_importado') {
|
||||
|
||||
$agendamentos->whereNotNull('exame_laboratorial_agendamento_laudo.arquivo');
|
||||
}
|
||||
|
||||
if ($request->status && $request->status == 'laudo_nao_importado') {
|
||||
|
||||
$agendamentos->whereNull('exame_laboratorial_agendamento_laudo.arquivo');
|
||||
}
|
||||
|
||||
if ($request->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
$agendamentos->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id');
|
||||
|
||||
$query->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::PENDENTE_ASSINATURA)
|
||||
->orWhere('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()->toArray();
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function load()
|
||||
{
|
||||
|
||||
$unidade = DB::table('exame_laboratorial_parametrizacao_unidade')
|
||||
->select([
|
||||
'cadastro_unidade.id AS unidade_id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
])
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_parametrizacao_unidade.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento')
|
||||
->whereColumn(
|
||||
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id',
|
||||
'exame_laboratorial_parametrizacao_unidade.infraestrutura_id'
|
||||
)
|
||||
->where(function($query) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
});
|
||||
})
|
||||
->where('exame_laboratorial_parametrizacao_unidade.laboratorio', true)
|
||||
->whereNull('cadastro_unidade.deleted_at')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->distinct('infraestrutura_id');
|
||||
|
||||
$unidade = $unidade->get()->toArray();
|
||||
|
||||
$resultadoAgrupado = [];
|
||||
|
||||
foreach ($unidade as $item) {
|
||||
$unidadeId = $item->unidade_id;
|
||||
$infraestruturaId = $item->infraestrutura_id;
|
||||
$nomeInfraestrutura = $item->nome_infraestrutura;
|
||||
|
||||
if (!isset($resultadoAgrupado[$unidadeId])) {
|
||||
$resultadoAgrupado[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'nome_fantasia' => $item->nome_fantasia,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$resultadoAgrupado[$unidadeId]['infraestruturas'][] = [
|
||||
'id' => $infraestruturaId,
|
||||
'nome' => $nomeInfraestrutura,
|
||||
'principal' => $item->principal
|
||||
];
|
||||
}
|
||||
|
||||
usort($resultadoAgrupado, function ($a, $b) {
|
||||
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
|
||||
});
|
||||
|
||||
return $resultadoAgrupado;
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
if ($request['multiple'] == 'true') {
|
||||
|
||||
self::saveMultiple($request);
|
||||
} else {
|
||||
|
||||
self::saveLaudo($request);
|
||||
}
|
||||
});
|
||||
|
||||
if ($request['multiple'] == 'false') {
|
||||
|
||||
return self::show($request);
|
||||
}
|
||||
}
|
||||
|
||||
public static function saveLaudo($request)
|
||||
{
|
||||
if($request->files->get('laudo') != ''){
|
||||
|
||||
foreach ($request->files->get('laudo') as $file) {
|
||||
|
||||
$agendamentoId = $request['agendamento_id'];
|
||||
$ordem = self::getNameArchive($agendamentoId, $request['quantidade']);
|
||||
|
||||
if ($ordem == 1) {
|
||||
$arquivo = $agendamentoId . '.PDF';
|
||||
} else {
|
||||
$arquivo = $agendamentoId . '_' . $ordem . '.PDF';
|
||||
}
|
||||
|
||||
try{
|
||||
$laudo = new AgendamentoLaudo();
|
||||
$laudo->agendamento_id = $agendamentoId;
|
||||
$laudo->ordem = $ordem;
|
||||
$laudo->arquivo = $arquivo;
|
||||
|
||||
$laudo->save();
|
||||
|
||||
$pasta = storage_path('app' . DIRECTORY_SEPARATOR . 'exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default'));
|
||||
|
||||
$file->move($pasta, $arquivo);
|
||||
}catch(\Exception $e){
|
||||
throw new CriacaoArquivoException;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function saveMultiple($request)
|
||||
{
|
||||
if($request->files->get('laudo') != ''){
|
||||
|
||||
foreach ($request->files->get('laudo') as $file) {
|
||||
|
||||
if (strpos($file->getClientOriginalName(), '_')) {
|
||||
|
||||
$agendamentoId = current(explode('_', $file->getClientOriginalName()));
|
||||
} else {
|
||||
|
||||
$agendamentoId = current(explode('.', $file->getClientOriginalName()));
|
||||
}
|
||||
|
||||
$agendamentoId = $agendamentoId;
|
||||
$ordem = self::getNameArchive($agendamentoId, $request['quantidade']);
|
||||
|
||||
if ($ordem == 1) {
|
||||
$arquivo = $agendamentoId . '.PDF';
|
||||
} else {
|
||||
$arquivo = $agendamentoId . '_' . $ordem . '.PDF';
|
||||
}
|
||||
|
||||
try{
|
||||
$laudo = new AgendamentoLaudo();
|
||||
$laudo->agendamento_id = $agendamentoId;
|
||||
$laudo->ordem = $ordem;
|
||||
$laudo->arquivo = $arquivo;
|
||||
|
||||
$laudo->save();
|
||||
|
||||
$pasta = storage_path('app' . DIRECTORY_SEPARATOR . 'exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default'));
|
||||
|
||||
$file->move($pasta, $arquivo);
|
||||
}catch(\Exception $e){
|
||||
throw new CriacaoArquivoException;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getNameArchive($id, $quantidade)
|
||||
{
|
||||
|
||||
for($i = 1; $i < $quantidade; $i++) {
|
||||
|
||||
$arquivo = AgendamentoLaudo::where('agendamento_id', $id)
|
||||
->where('ordem', $i)->first();
|
||||
|
||||
if (is_null($arquivo)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $i;
|
||||
}
|
||||
|
||||
public static function getExames($id)
|
||||
{
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao AS status',
|
||||
])
|
||||
->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('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.id', $id);
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'exames' => $agendamentos
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($request)
|
||||
{
|
||||
|
||||
return AgendamentoLaudo::where('agendamento_id', $request['agendamento_id'])
|
||||
->orderBy('ordem')
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id ?? null,
|
||||
'usuario' => Lotacao::find($query->created_lotacao_id)->usuario->cidadao->nome
|
||||
?? (isset($query->externo_created_usuario_id)
|
||||
? (WebServiceUsuario::find($query->externo_created_usuario_id)->nome . ' (Web service)')
|
||||
: null
|
||||
),
|
||||
'data' => (new Carbon($query->created_at))->format('d/m/Y') ?? null,
|
||||
'arquivo' => $query->arquivo ?? null,
|
||||
'url' => $query->url ?? null,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
|
||||
$laudo = AgendamentoLaudo::findOrFail($id);
|
||||
|
||||
if(Storage::exists('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo)){
|
||||
|
||||
Storage::delete('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo);
|
||||
}
|
||||
|
||||
AgendamentoLaudo::findOrFail($id)->delete();
|
||||
}
|
||||
|
||||
public static function download($id)
|
||||
{
|
||||
$laudo = AgendamentoLaudo::findOrFail($id);
|
||||
|
||||
return [
|
||||
'nome' => $laudo->arquivo,
|
||||
'arquivo' => Storage::path('exame-laboratorial' . DIRECTORY_SEPARATOR . 'laudos' . DIRECTORY_SEPARATOR . app('config')->get('database.default') . DIRECTORY_SEPARATOR . $laudo->arquivo),
|
||||
];
|
||||
}
|
||||
}
|
||||
240
api/app/Services/ExameLaboratorial/ImpressaoExameService.php
Normal file
240
api/app/Services/ExameLaboratorial/ImpressaoExameService.php
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ImpressaoExameService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'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'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_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);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('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);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
344
api/app/Services/ExameLaboratorial/ImpressaoService.php
Normal file
344
api/app/Services/ExameLaboratorial/ImpressaoService.php
Normal file
@@ -0,0 +1,344 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\MensagemProtocolo;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ImpressaoService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'exame_laboratorial_agendamento.data AS data_entrega',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.ultima_menstruacao AS ultima_menstruacao',
|
||||
'exame_laboratorial_agendamento.gestante AS gestante',
|
||||
DB::raw('TIME_FORMAT(exame_laboratorial_agendamento.horario, "%h:%i") AS hora_inicial'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.jornada_id AS jornada_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
||||
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.observacao AS observacao',
|
||||
'exame_laboratorial_agendamento.responsavel AS responsavel',
|
||||
'exame_laboratorial_agendamento.suspeita_clinica AS suspeita',
|
||||
'exame_laboratorial_agendamento.ultima_menstruacao AS data_menstruacao',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento.urgente AS urgente',
|
||||
'exame_laboratorial_agendamento.uso_medicamento AS uso_medicamento',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
'parametrizacao_exame.sigla_exame AS sigla_exame',
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id',
|
||||
'exame_laboratorial_agendamento_exame.ocupacao_id AS ocupacao_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS paciente_prontuario'),
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_infraestrutura.telefone AS telefone',
|
||||
DB::raw("COALESCE(cadastro_endereco.numero, 'S/N') AS endereco_numero"),
|
||||
'cadastro_logradouro.nome AS logradouro_nome',
|
||||
'cadastro_logradouro.cep AS logradouro_cep',
|
||||
'cadastro_logradouro_tipo.descricao AS logradouro_tipo',
|
||||
'cadastro_bairro.nome AS bairro_nome',
|
||||
'cadastro_infraestrutura_coleta.telefone AS telefone_coleta',
|
||||
DB::raw("COALESCE(cadastro_endereco_coleta.numero, 'S/N') AS endereco_numero_coleta"),
|
||||
'cadastro_logradouro_coleta.nome AS logradouro_nome_coleta',
|
||||
'cadastro_logradouro_coleta.cep AS logradouro_cep_coleta',
|
||||
'cadastro_bairro_coleta.nome AS bairro_nome_coleta',
|
||||
'cadastro_logradouro_tipo_coleta.descricao AS logradouro_tipo_coleta',
|
||||
'cadastro_unidade.nome_fantasia AS infraestrutura_nome',
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_infraestrutura_nome',
|
||||
'cadastro_infraestrutura_laboratorio.telefone AS telefone_laboratorio',
|
||||
DB::raw("COALESCE(cadastro_endereco_laboratorio.numero, 'S/N') AS endereco_numero_laboratorio"),
|
||||
'cadastro_logradouro_laboratorio.nome AS logradouro_nome_laboratorio',
|
||||
'cadastro_logradouro_laboratorio.cep AS logradouro_cep_laboratorio',
|
||||
'cadastro_bairro_laboratorio.nome AS bairro_nome_laboratorio',
|
||||
'cadastro_logradouro_tipo_laboratorio.descricao AS logradouro_tipo_laboratorio',
|
||||
'cadastro_unidade.nome_fantasia AS infraestrutura_nome',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_infraestrutura_nome',
|
||||
'exame_laboratorial_agendamento.supervisor_id AS supervisor_id',
|
||||
DB::raw('CASE WHEN cadastro_cidadao.nome_mae IS NOT NULL THEN cadastro_cidadao.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
'regulacao_solicitacao.numero as regulacao_solicitacao_numero',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->leftJoin('cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id')
|
||||
->leftJoin('cadastro_infraestrutura as cadastro_infraestrutura_laboratorio', 'cadastro_infraestrutura_laboratorio.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade as cadastro_unidade_laboratorio', 'cadastro_unidade_laboratorio.id', 'cadastro_infraestrutura_laboratorio.unidade_id')
|
||||
->leftJoin('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_infraestrutura.endereco_id')
|
||||
->leftJoin('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
||||
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id')
|
||||
->leftJoin('cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id')
|
||||
->leftJoin('cadastro_endereco as cadastro_endereco_coleta', 'cadastro_endereco_coleta.id', 'cadastro_infraestrutura_coleta.endereco_id')
|
||||
->leftJoin('cadastro_logradouro as cadastro_logradouro_coleta', 'cadastro_logradouro_coleta.id', 'cadastro_endereco_coleta.logradouro_id')
|
||||
->leftJoin('cadastro_bairro as cadastro_bairro_coleta', 'cadastro_bairro_coleta.id', 'cadastro_logradouro_coleta.bairro_id')
|
||||
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo_coleta', 'cadastro_logradouro_tipo_coleta.id', 'cadastro_logradouro_coleta.tipo_id')
|
||||
->leftJoin('cadastro_endereco as cadastro_endereco_laboratorio', 'cadastro_endereco_laboratorio.id', 'cadastro_infraestrutura_laboratorio.endereco_id')
|
||||
->leftJoin('cadastro_logradouro as cadastro_logradouro_laboratorio', 'cadastro_logradouro_laboratorio.id', 'cadastro_endereco_laboratorio.logradouro_id')
|
||||
->leftJoin('cadastro_bairro as cadastro_bairro_laboratorio', 'cadastro_bairro_laboratorio.id', 'cadastro_logradouro_laboratorio.bairro_id')
|
||||
->leftJoin('regulacao_solicitacao', 'regulacao_solicitacao.id', 'exame_laboratorial_agendamento.regulacao_solicitacao_id')
|
||||
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo_laboratorio', 'cadastro_logradouro_tipo_laboratorio.id', 'cadastro_logradouro_laboratorio.tipo_id')
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', '!=', StatusAgendamento::CANCELADO)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
} else {
|
||||
|
||||
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->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get()
|
||||
->map(function ($atendimento) {
|
||||
|
||||
$atendimento->impressoes = self::getImpressoes(
|
||||
[
|
||||
'agendamento' => [
|
||||
'protocolo' => [
|
||||
'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->agendamento_id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame ? $query->exame->nome : null,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'valor' => $query->valor,
|
||||
'status_id' => $query->status_id,
|
||||
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'data' => $atendimento->data,
|
||||
'data_entrega' => $atendimento->data_entrega,
|
||||
'data_nascimento' => $atendimento->data_nascimento,
|
||||
'hora_exame' => $atendimento->hora_inicial ?? null,
|
||||
'mensagem_protocolo' => self::getMensagem(),
|
||||
'agendamento_numero' => $atendimento->agendamento_numero,
|
||||
'medico_nome' => $atendimento->medico_nome,
|
||||
'nome_responsavel' => $atendimento->responsavel,
|
||||
'observacao' => $atendimento->observacao,
|
||||
'paciente_nome' => $atendimento->paciente_nome,
|
||||
'paciente_nome_social' => $atendimento->paciente_nome_social,
|
||||
'prontuario' => $atendimento->paciente_prontuario,
|
||||
'local_coleta' => $atendimento->coleta_infraestrutura_nome,
|
||||
'laboratorio' => $atendimento->laboratorio_infraestrutura_nome,
|
||||
'sistema' => self::getSistema(),
|
||||
'urgente' => $atendimento->urgente,
|
||||
'unidade' => $atendimento->infraestrutura_nome,
|
||||
'telefone' => MaskUtil::getPhoneMasked($atendimento->telefone),
|
||||
'endereco_numero' => $atendimento->endereco_numero,
|
||||
'logradouro_nome' => $atendimento->logradouro_nome,
|
||||
'logradouro_cep' => MaskUtil::getCepMasked($atendimento->logradouro_cep),
|
||||
'logradouro_tipo' => $atendimento->logradouro_tipo,
|
||||
'bairro_nome' => $atendimento->bairro_nome,
|
||||
'telefone_coleta' => MaskUtil::getPhoneMasked($atendimento->telefone_coleta),
|
||||
'endereco_numero_coleta' => $atendimento->endereco_numero_coleta,
|
||||
'logradouro_nome_coleta' => $atendimento->logradouro_nome_coleta,
|
||||
'logradouro_cep_coleta' => MaskUtil::getCepMasked($atendimento->logradouro_cep_coleta),
|
||||
'logradouro_tipo_coleta' => $atendimento->logradouro_tipo_coleta,
|
||||
'bairro_nome_coleta' => $atendimento->bairro_nome_coleta,
|
||||
'telefone_laboratorio' => MaskUtil::getPhoneMasked($atendimento->telefone_laboratorio),
|
||||
'endereco_numero_laboratorio' => $atendimento->endereco_numero_laboratorio,
|
||||
'logradouro_nome_laboratorio' => $atendimento->logradouro_nome_laboratorio,
|
||||
'logradouro_cep_laboratorio' => MaskUtil::getCepMasked($atendimento->logradouro_cep_laboratorio),
|
||||
'logradouro_tipo_laboratorio' => $atendimento->logradouro_tipo_laboratorio,
|
||||
'bairro_nome_laboratorio' => $atendimento->bairro_nome_laboratorio,
|
||||
'regulacao_solicitacao_numero' => $atendimento->regulacao_solicitacao_numero,
|
||||
],
|
||||
'etiqueta' => [
|
||||
'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->agendamento_id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame ? $query->exame->nome : null,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null,
|
||||
'status_id' => $query->status_id ?? null,
|
||||
'valor' => $query->exame->procedimento->valor_servico_ambilatorial ?? null,
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'data_exame' => $atendimento->data,
|
||||
'data_nascimento' => $atendimento->data_nascimento,
|
||||
'medico_solicitante' => $atendimento->medico_nome,
|
||||
'unidade' => $atendimento->infraestrutura_nome,
|
||||
'laboratorio' => $atendimento->laboratorio_infraestrutura_nome,
|
||||
'agendamento_numero' => $atendimento->agendamento_numero,
|
||||
'paciente_nome' => $atendimento->paciente_nome,
|
||||
'prontuario' => $atendimento->paciente_prontuario,
|
||||
'idade' => $atendimento->paciente_idade,
|
||||
'paciente_nome_social' => $atendimento->paciente_nome_social,
|
||||
'sistema' => self::getSistema(),
|
||||
]
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
return $atendimento;
|
||||
});
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getSistema()
|
||||
{
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
return [
|
||||
'ramo_atividade' => $cliente->sistema->ramoAtividade->nome ?? null,
|
||||
'nome_sistema' => $cliente->sistema->nome ?? null,
|
||||
'versao' => $cliente->sistema->versao ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getMensagem()
|
||||
{
|
||||
$mensagemProtocolo = MensagemProtocolo::select('mensagem')->first();
|
||||
|
||||
return $mensagemProtocolo->mensagem;
|
||||
|
||||
}
|
||||
|
||||
public static function getImpressoes($data = [], $excluded = [])
|
||||
{
|
||||
$impressoes = [
|
||||
'agendamento' => self::getimpressoesAgendamentoExames($data['agendamento'] ?? []),
|
||||
];
|
||||
|
||||
foreach ($excluded as $key => $value) {
|
||||
|
||||
if (is_array($value)) {
|
||||
|
||||
foreach ($value as $col => $vl) {
|
||||
|
||||
$indice = array_search($vl, array_column($impressoes[$key], $col));
|
||||
if ($indice >= 0) {
|
||||
|
||||
unset($impressoes[$key][$indice]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
if (array_key_exists($value, $impressoes)) {
|
||||
|
||||
unset($impressoes[$value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $impressoes;
|
||||
}
|
||||
|
||||
public static function getimpressoesAgendamentoExames($data = [])
|
||||
{
|
||||
$impressoesAgendamentoExames = [];
|
||||
|
||||
if (isset($data['protocolo'])) {
|
||||
|
||||
$impressoesAgendamentoExames[] = [
|
||||
'value' => 'protocolo',
|
||||
'text' => 'Comprovante de agendamento',
|
||||
'disabled' => false,
|
||||
'data' => $data['protocolo']
|
||||
];
|
||||
}
|
||||
|
||||
if (isset($data['etiqueta'])) {
|
||||
|
||||
$impressoesAgendamentoExames[] = [
|
||||
'value' => 'etiqueta',
|
||||
'text' => 'Etiqueta',
|
||||
'disabled' => false,
|
||||
'data' => $data['etiqueta']
|
||||
];
|
||||
}
|
||||
|
||||
return $impressoesAgendamentoExames;
|
||||
}
|
||||
}
|
||||
237
api/app/Services/ExameLaboratorial/JornadaBloqueioService.php
Normal file
237
api/app/Services/ExameLaboratorial/JornadaBloqueioService.php
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Exceptions\ExameLaboratorial\ConflitoJornadaBloqueioException;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\JornadaBloqueio;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class JornadaBloqueioService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
$jornadas = DB::table('exame_laboratorial_jornada_bloqueio as jornada_bloqueio')
|
||||
->join(
|
||||
'cadastro_infraestrutura as infraestrutura',
|
||||
'infraestrutura.id',
|
||||
'jornada_bloqueio.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as unidade',
|
||||
'unidade.id',
|
||||
'infraestrutura.unidade_id'
|
||||
)
|
||||
->select(DB::raw("
|
||||
jornada_bloqueio.id,
|
||||
unidade.nome_fantasia as unidade_nome_fantasia,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome,
|
||||
DATE_FORMAT(jornada_bloqueio.inicio, '%d/%m/%Y') as inicio,
|
||||
DATE_FORMAT(jornada_bloqueio.fim, '%d/%m/%Y') as fim
|
||||
"));
|
||||
|
||||
return datatables()
|
||||
->of($jornadas)
|
||||
->filterColumn('jornada_bloqueio.inicio', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("DATE_FORMAT(jornada_bloqueio.inicio, '%d/%m/%Y') LIKE ?", ["%{$keyword}%"])
|
||||
->orWhereRaw(
|
||||
"CONCAT(
|
||||
SUBSTR(jornada_bloqueio.inicio,9,2),
|
||||
SUBSTR(jornada_bloqueio.inicio,6,2),
|
||||
SUBSTR(jornada_bloqueio.inicio,1,4)
|
||||
) like ?",
|
||||
["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('jornada_bloqueio.fim', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("DATE_FORMAT(jornada_bloqueio.fim, '%d/%m/%Y') LIKE ?", ["%{$keyword}%"])
|
||||
->orWhereRaw(
|
||||
"CONCAT(
|
||||
SUBSTR(jornada_bloqueio.fim,9,2),
|
||||
SUBSTR(jornada_bloqueio.fim,6,2),
|
||||
SUBSTR(jornada_bloqueio.fim,1,4)
|
||||
) like ?",
|
||||
["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->skipTotalRecords()
|
||||
->toJson();
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$params = (object) request()->all();
|
||||
|
||||
switch (request()->listar) {
|
||||
case 'infraestruturas': {
|
||||
return self::getInfraestruturas($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'unidades' => self::getUnidades(),
|
||||
'infraestruturas' => [],
|
||||
],
|
||||
'model' => [
|
||||
'id' => null,
|
||||
'unidade_id' => null,
|
||||
'infraestrutura_id' => null,
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$bloqueio = JornadaBloqueio::findOrFail($id);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'unidades' => self::getUnidades($bloqueio->infraestrutura->unidade_id),
|
||||
'infraestruturas' => self::getInfraestruturas((object) [
|
||||
'unidade_id' => $bloqueio->infraestrutura->unidade_id,
|
||||
], $bloqueio->infraestrutura_id),
|
||||
],
|
||||
'model' => [
|
||||
'id' => $bloqueio->id,
|
||||
'unidade_id' => $bloqueio->infraestrutura->unidade_id,
|
||||
'infraestrutura_id' => $bloqueio->infraestrutura_id,
|
||||
'periodo' => [
|
||||
'start' => $bloqueio->inicio->format('d/m/Y'),
|
||||
'end' => $bloqueio->fim->format('d/m/Y'),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getUnidades($unidadeId = null)
|
||||
{
|
||||
return Unidade::orderBy('nome_fantasia')
|
||||
->where('id', $unidadeId)
|
||||
->orWhereHas('infraestruturas')
|
||||
->get(['id', 'nome_fantasia']);
|
||||
}
|
||||
|
||||
private static function getInfraestruturas($params, $infraestruturaId = null)
|
||||
{
|
||||
return Infraestrutura::setEagerLoads([])
|
||||
->where('id', $infraestruturaId)
|
||||
->orWhere(fn ($q) => $q->where('unidade_id', $params->unidade_id))
|
||||
->get(['id', 'nome', 'principal', 'unidade_id'])
|
||||
->map(fn ($infraestrutura) => [
|
||||
'id' => $infraestrutura->id,
|
||||
'nome' => $infraestrutura->nome,
|
||||
])
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
$params = self::getParams();
|
||||
|
||||
self::validateConflito($params);
|
||||
|
||||
return JornadaBloqueio::create($params)->fresh();
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
$bloqueio = JornadaBloqueio::findOrFail($id);
|
||||
|
||||
self::validate();
|
||||
|
||||
$params = self::getParams();
|
||||
|
||||
self::validateConflito($params, $bloqueio->id);
|
||||
|
||||
$bloqueio->update($params);
|
||||
|
||||
return $bloqueio->fresh();
|
||||
}
|
||||
|
||||
private static function getParams()
|
||||
{
|
||||
$params = request()->all();
|
||||
|
||||
$params['inicio'] = Carbon::createFromFormat('d/m/Y', $params['inicio']);
|
||||
$params['fim'] = Carbon::createFromFormat('d/m/Y', $params['fim']);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public static function validate()
|
||||
{
|
||||
$rules = [
|
||||
'infraestrutura_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Infraestrutura::class, 'id'),
|
||||
],
|
||||
'inicio' => [
|
||||
'required',
|
||||
'date_format:d/m/Y',
|
||||
'before_or_equal:fim'
|
||||
],
|
||||
'fim' => [
|
||||
'required',
|
||||
'date_format:d/m/Y',
|
||||
'after_or_equal:inicio'
|
||||
],
|
||||
];
|
||||
|
||||
$messages = [];
|
||||
|
||||
request()->validate($rules, $messages);
|
||||
}
|
||||
|
||||
private static function validateConflito($params, $id = null)
|
||||
{
|
||||
$conflito = JornadaBloqueio::when($id, fn ($q) => $q->where('id', '<>', $id))
|
||||
->where('infraestrutura_id', $params['infraestrutura_id'])
|
||||
->where(fn ($q) => $q
|
||||
->where(fn ($q) => $q
|
||||
->whereDate('inicio', '<=', $params['inicio'])
|
||||
->whereDate('fim', '>=', $params['inicio'])
|
||||
)
|
||||
->orWhere(fn ($q) => $q
|
||||
->whereDate('inicio', '<=', $params['fim'])
|
||||
->whereDate('fim', '>=', $params['fim'])
|
||||
)
|
||||
->orWhere(fn ($q) => $q
|
||||
->whereDate('inicio', '>=', $params['inicio'])
|
||||
->whereDate('inicio', '<=', $params['fim'])
|
||||
)
|
||||
->orWhere(fn ($q) => $q
|
||||
->whereDate('fim', '>=', $params['inicio'])
|
||||
->whereDate('fim', '<=', $params['fim'])
|
||||
)
|
||||
)
|
||||
->exists();
|
||||
|
||||
if ($conflito) {
|
||||
|
||||
throw new ConflitoJornadaBloqueioException();
|
||||
}
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
JornadaBloqueio::findOrFail($id)->delete();
|
||||
}
|
||||
}
|
||||
210
api/app/Services/ExameLaboratorial/JornadaService.php
Normal file
210
api/app/Services/ExameLaboratorial/JornadaService.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Feriado\Feriado;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
||||
use App\Models\ExameLaboratorial\Jornada\Jornada;
|
||||
use App\Models\ExameLaboratorial\Jornada\SemanaJornada;
|
||||
use App\Models\ExameLaboratorial\Jornada\TipoJornada;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\IntegracaoAplicativo;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as UnidadeParametrizacao;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class JornadaService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('cadastro_unidade')
|
||||
->select([
|
||||
'cadastro_infraestrutura.id',
|
||||
DB::raw("
|
||||
CONCAT(
|
||||
cadastro_unidade.nome_fantasia,
|
||||
' - ',
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END
|
||||
) AS nome_fantasia
|
||||
"),
|
||||
])
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
||||
->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_parametrizacao_unidade')
|
||||
->whereColumn('exame_laboratorial_parametrizacao_unidade.unidade_id', 'cadastro_unidade.id')
|
||||
->whereColumn('exame_laboratorial_parametrizacao_unidade.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->where('exame_laboratorial_parametrizacao_unidade.unidade_coleta', true)
|
||||
->whereNull('exame_laboratorial_parametrizacao_unidade.deleted_at');
|
||||
});
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
|
||||
$unidades = UnidadeParametrizacao::where('infraestrutura_id', $id)->first();
|
||||
|
||||
$integracaoAplicativo = IntegracaoAplicativo::first();
|
||||
|
||||
$laboratorios = UnidadeParametrizacao::where('laboratorio', true)
|
||||
->whereNotNull('infraestrutura_id')
|
||||
->get()
|
||||
->map(fn ($param) => [
|
||||
'id' => $param->infraestrutura_id,
|
||||
'nome' => $param->infraestrutura->nome ?? null,
|
||||
])
|
||||
->unique('id')
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'min_date' => Carbon::now()->subDays(90)->format('d/m/Y'),
|
||||
'max_date' => Carbon::now()->addYear()->format('d/m/Y'),
|
||||
'unidade' => $unidades->unidade->nome_fantasia,
|
||||
'infraestrutura' => $unidades->infraestrutura->nome,
|
||||
'habilita_agendamento_app' => $integracaoAplicativo->habilita_agendamento_conectasus_cidadao ?? false,
|
||||
'laboratorios' => $laboratorios,
|
||||
'feriados' => Feriado::orderBy('id')
|
||||
->get()
|
||||
->map(fn ($feriado) => [
|
||||
'id' => $feriado->id,
|
||||
'data' => (new Carbon($feriado->data))->format('d/m/Y') ?? null,
|
||||
'tipo_id' => $feriado->tipo_id,
|
||||
'tipo_nome' => $feriado->tipo->descricao,
|
||||
'observacao' => $feriado->observacao,
|
||||
'modulos' => $feriado->modulos->pluck('id'),
|
||||
]),
|
||||
'dias_semana' => SemanaJornada::orderBy('id')
|
||||
->get()
|
||||
->map(fn ($semanaJornadas) => [
|
||||
'id' => $semanaJornadas->id,
|
||||
'nome' => $semanaJornadas->nome,
|
||||
'disabled' => false,
|
||||
]),
|
||||
'tipos' => TipoJornada::orderBy('id')
|
||||
->get()
|
||||
->map(fn ($tipo) => [
|
||||
'id' => $tipo->id,
|
||||
'nome' => $tipo->nome
|
||||
]),
|
||||
],
|
||||
'model' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'unidade_id' => $unidades->unidade_id,
|
||||
'infraestrutura_id' => $unidades->infraestrutura_id,
|
||||
'jornadas' => Jornada::where('unidade_id', $unidades->unidade_id)
|
||||
->where('infraestrutura_id', $unidades->infraestrutura_id)
|
||||
->get()
|
||||
->map(function ($jornada) use ($unidades) {
|
||||
|
||||
$statusPermitidos = [
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::NOVA_COLETA,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
];
|
||||
|
||||
$agendamentosQuery = Agendamento::where('jornada_id', $jornada->id)
|
||||
->where('coleta_infraestrutura_id', $unidades->infraestrutura_id) // agora funciona
|
||||
->whereHas('agendamentoExame', function ($query) use ($statusPermitidos) {
|
||||
$query->whereIn('status_id', $statusPermitidos);
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentosQuery->count();
|
||||
|
||||
$primeiroHorario = (clone $agendamentosQuery)
|
||||
->orderBy('horario', 'asc')
|
||||
->value('horario');
|
||||
|
||||
$ultimoHorario = (clone $agendamentosQuery)
|
||||
->orderBy('horario', 'desc')
|
||||
->value('horario');
|
||||
|
||||
return [
|
||||
'jornada_id' => $jornada->id,
|
||||
'unidade_id' => $jornada->unidade_id,
|
||||
'infraestrutura_id' => $jornada->infraestrutura_id,
|
||||
'semana_id' => $jornada->semana_id,
|
||||
'dias_semana' => $jornada->semana_id,
|
||||
'tipo_id' => $jornada->tipo_id,
|
||||
'tipo_nome' => $jornada->tipo->nome ?? null,
|
||||
'data' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
||||
'periodo' => [
|
||||
'start' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
||||
'end' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
||||
],
|
||||
'hora_inicial' => (new Carbon($jornada->hora_inicial))->format('H:i') ?? null,
|
||||
'hora_fim' => (new Carbon($jornada->hora_fim))->format('H:i') ?? null,
|
||||
'vagas' => $jornada->vagas ?? null,
|
||||
'vagas_app' => $jornada->vagas_app ?? null,
|
||||
'laboratorio_prestador_infraestrutura_id' => $jornada->laboratorio_prestador_infraestrutura_id ?? null,
|
||||
'laboratorio_prestador_nome' => $jornada->laboratorioPrestadorInfraestrutura->nome ?? null,
|
||||
'tempo_atendimento' => $jornada->tempo_atendimento ?? null,
|
||||
'selecionado' => false,
|
||||
'agendamentos' => $agendamentos,
|
||||
'primeiro_horario' => $primeiroHorario,
|
||||
'ultimo_horario' => $ultimoHorario,
|
||||
'vagas_disponiveis' => (($jornada->vagas + ($jornada->vagas_app ?? 0)) - $agendamentos) >= 0
|
||||
? (($jornada->vagas + ($jornada->vagas_app ?? 0)) - $agendamentos) : 0,
|
||||
];
|
||||
})
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
'jornadasNotSave' => null,
|
||||
'periodo' => [
|
||||
'start' => Carbon::now()->format('d/m/Y'),
|
||||
'end' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::saveJornada($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function saveJornada($request)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->jornadas as $params) {
|
||||
|
||||
$tipo = Jornada::where('data', Carbon::createFromFormat('d/m/Y', $params['data'])->format('Y-m-d'))
|
||||
->where('unidade_id', $params['unidade_id'])
|
||||
->where('infraestrutura_id', $params['infraestrutura_id'])
|
||||
->where('hora_inicial', $params['hora_inicial'])
|
||||
->first() ?? new Jornada();
|
||||
|
||||
$tipo->fill($params);
|
||||
|
||||
$tipo->data = Carbon::createFromFormat('d/m/Y', $params['data']);
|
||||
|
||||
$tipo->save();
|
||||
|
||||
$ids[] = $tipo->id;
|
||||
}
|
||||
|
||||
Jornada::where('unidade_id', $request->unidade_id)
|
||||
->where('infraestrutura_id', $request->infraestrutura_id)
|
||||
->whereNotIn('id', $ids)
|
||||
->get()
|
||||
->each(function ($jornada) {
|
||||
$jornada->delete();
|
||||
});
|
||||
}
|
||||
}
|
||||
1122
api/app/Services/ExameLaboratorial/LancamentoColetaService.php
Normal file
1122
api/app/Services/ExameLaboratorial/LancamentoColetaService.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,659 @@
|
||||
<?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\Enums\Parametrizacao\Exame\Tipo;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExameResultado;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExame;
|
||||
use App\Models\ExameLaboratorial\FormulaExame\FormulaExameFator;
|
||||
use App\Models\ExameLaboratorial\LancamentoColeta\Status;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Motivo;
|
||||
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 LancamentoResultadoService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'status' => Status::where(function($query) {
|
||||
$query->where('id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('id', StatusAgendamento::PENDENTE_ASSINATURA)
|
||||
->orWhere('id', StatusAgendamento::ASSINADO)
|
||||
->orWhere('id', StatusAgendamento::NAO_REALIZADO)
|
||||
->orWhere('id', StatusAgendamento::NOVA_COLETA);
|
||||
})
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->map(fn ($status) => [
|
||||
'id' => $status->id,
|
||||
'descricao' => $status->descricao
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$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.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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('cadastro_unidade.nome_fantasia AS unidade_nome'),
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
"),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL);
|
||||
|
||||
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->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentos->where('cadastro_grupo.id', $request->exame_id);
|
||||
} else {
|
||||
$agendamentos->whereNotNull('cadastro_grupo.id');
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
if ($request->status_id) {
|
||||
|
||||
$agendamentos->where(function($query) use ($request) {
|
||||
|
||||
foreach ($request->status_id as $status) {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$agendamentos->whereExists(function($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id');
|
||||
|
||||
$query->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::PENDENTE_ASSINATURA);
|
||||
});
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
|
||||
$agendamentos = $agendamentos->get()->toArray();
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
|
||||
if(is_numeric($id)){
|
||||
|
||||
$exame = self::getExame($id);
|
||||
$exames = $exame->exame->mapa_agrupado ? self::getExames($exame->exame->grupo_tipo_id, $exame->agendamento->id) : collect();
|
||||
}
|
||||
|
||||
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')
|
||||
: '',
|
||||
'idade' => $exame->agendamento->paciente->cidadao->data_nascimento
|
||||
? "{$exame->agendamento->paciente->cidadao->idade['anos']} ano(s) "
|
||||
. "{$exame->agendamento->paciente->cidadao->idade['meses']} mês(s) "
|
||||
. "{$exame->agendamento->paciente->cidadao->idade['dias']} dia(s) "
|
||||
: '',
|
||||
'sexo' => $exame->agendamento->paciente->cidadao->sexo ?? '-',
|
||||
'telefone' => MaskUtil::getPhoneMasked($exame->agendamento->paciente->cidadao->telefone_principal->numero ?? ''),
|
||||
],
|
||||
'data_agendamento' => (new Carbon($exame->agendamento->data))->format('d/m/Y'),
|
||||
'data_coleta' => (new Carbon($exame->data_coleta))->format('d/m/Y'),
|
||||
'medico' => $exame->agendamento->medico()->withTrashed()->first()->nome,
|
||||
'uso_medicamento' => $exame->agendamento->uso_medicamento ?? 'Não informado',
|
||||
'suspeita_clinica' => $exame->agendamento->suspeita_clinica ?? 'Não informada',
|
||||
'observacao' => $exame->agendamento->observacao ?? 'Não informada',
|
||||
'motivo' => Motivo::get()
|
||||
->map(fn($query) => [
|
||||
'id' => $query->id ?? null,
|
||||
'descricao' => $query->descricao ?? null,
|
||||
]),
|
||||
],
|
||||
'model' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'agendamentosNotSave' => null,
|
||||
'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,
|
||||
'nao_realizado' => $exame->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
|
||||
'nova_coleta' => $exame->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
|
||||
'observacao' => $exame->observacao,
|
||||
'composicao' => self::getComposicao($exame, $exame->exame_id, $exame->id),
|
||||
'historico' => self::getHistorico($exame->exame_id, $exame->agendamento_id, $exame->agendamento->paciente->id)
|
||||
->map(fn ($historico) => [
|
||||
'data' => $historico->data,
|
||||
'exame_nome' => $historico->exame_nome,
|
||||
'exame_id' => $historico->exame_id,
|
||||
'observacao' => $historico->observacao,
|
||||
'composicao' => self::getComposicaoHistorico($exame, $historico->agendamento_exame_id)
|
||||
]),
|
||||
'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,
|
||||
'mapa_agrupado' => $exameGrupo->mapa_agrupado,
|
||||
'motivo_id' => $exameGrupo->motivo_id ?? null,
|
||||
'nao_realizado' => $exameGrupo->status_id == StatusAgendamento::NAO_REALIZADO ? true : false,
|
||||
'nova_coleta' => $exameGrupo->status_id == StatusAgendamento::NOVA_COLETA ? true : false,
|
||||
'observacao' => $exameGrupo->observacao,
|
||||
'historico' => self::getHistorico($exameGrupo->exame_id, $exameGrupo->agendamento_id, $exame->agendamento->paciente->id)
|
||||
->map(fn ($historico) => [
|
||||
'data' => $historico->data,
|
||||
'exame_nome' => $historico->exame_nome,
|
||||
'exame_id' => $historico->exame_id,
|
||||
'observacao' => $historico->observacao,
|
||||
'composicao' => self::getComposicaoHistorico($exame, $historico->agendamento_exame_id)
|
||||
]),
|
||||
'composicao' => self::getComposicao($exame, $exameGrupo->exame_id, $exameGrupo->agendamento_exame_id)
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getComposicao($exame, $exameGrupo, $agendamentoExameId)
|
||||
{
|
||||
|
||||
return ExameComposicao::where('exame_id', $exameGrupo)
|
||||
->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 ($agendamentoExameId) {
|
||||
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,
|
||||
'tipo_idade_id' => $query->tipo_idade_id,
|
||||
'tipo_idade' => $query->tipo_idade_id ? $query->tipoIdade->descricao : '-',
|
||||
'idade_inicial' => $query->idade_inicial,
|
||||
'idade_final' => $query->idade_final,
|
||||
'sexo_id' => $query->sexo_id,
|
||||
'sexo' => $query->sexo->descricao ?? 'AMBOS',
|
||||
'tipo_resultado_id' => $query->tipo_resultado_id,
|
||||
'tipo_resultado' => $query->tipoResultado->descricao,
|
||||
'casa_decimal_id' => $query->casa_decimal_id,
|
||||
'formula_id' => $query->formula_id,
|
||||
'valor' => ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->where('padrao', true)
|
||||
->first()->id ?? null,
|
||||
'situacao' => null,
|
||||
'visivel' => $query->visivel,
|
||||
'valor_absoluto' => $query->valor_absoluto,
|
||||
'resultado' => AgendamentoExameResultado::where('agendamento_exame_id', $agendamentoExameId)
|
||||
->where('composicao_id', $query->id)->first()->valor
|
||||
?? ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->where('padrao', true)->first()->id
|
||||
?? null,
|
||||
'referencia_especial' => $query->referencia_especial,
|
||||
'lista' => ExameComposicaoLista::where('composicao_id', $query->id)
|
||||
->orderBy('ordem')
|
||||
->get()
|
||||
->map(fn ($composicao) => [
|
||||
'id' => $composicao->id,
|
||||
'descricao' => $composicao->descricao,
|
||||
'ordem' => $composicao->ordem,
|
||||
'situacao_id' => $composicao->situacao_id,
|
||||
'situacao' => ucfirst(strtolower($composicao->situacao->descricao)),
|
||||
'padrao' => $composicao->padrao,
|
||||
]),
|
||||
'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,
|
||||
]),
|
||||
'formula' => FormulaExame::where('id', $query->formula_id)
|
||||
->get()
|
||||
->map(fn ($formula) => [
|
||||
'id' => $formula->id,
|
||||
'nome' => $formula->nome,
|
||||
'fatores' => FormulaExameFator::where('formula_exame_id', $formula->id)
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'formula_exame_id' => $query->formula_exame_id,
|
||||
'fator' => $query->fator,
|
||||
'operacao_id' => $query->operacao_id,
|
||||
'operacao_descricao' => $query->operacao->descricao,
|
||||
'tipo' => $query->tipo,
|
||||
'valor' => $query->valor ? number_format($query->valor, 6, ',', '.') : null,
|
||||
'exponenciacao' => $query->exponenciacao ? true : false,
|
||||
'exponenciacao_tipo' => $query->exponenciacao_tipo,
|
||||
'exponenciacao_valor' => $query->exponenciacao_valor
|
||||
? number_format($query->exponenciacao_valor, 6, ',', '.')
|
||||
: null,
|
||||
'ordem' => $query->ordem,
|
||||
];
|
||||
})->values(),
|
||||
]),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function getComposicaoHistorico($exame, $exameId)
|
||||
{
|
||||
|
||||
return AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->whereHas('composicao', function($query) use ($exame) {
|
||||
|
||||
$query->where('sexo_id', $exame->agendamento->paciente->cidadao->sexo->id)
|
||||
->orWhereNull('sexo_id');
|
||||
|
||||
$query->whereNull('valor_absoluto');
|
||||
|
||||
$query->where(function ($idade) use ($exame) {
|
||||
|
||||
$idade
|
||||
->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 ($exameId) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'descricao' => $query->composicao->descricao,
|
||||
'tipo_resultado_id' => $query->composicao->tipo_resultado_id,
|
||||
'resultado' =>
|
||||
$query->composicao->tipo_resultado_id == ComposicaoTipo::LISTA
|
||||
? ExameComposicaoLista::where('id',
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->where('composicao_id', $query->composicao->id)->first()->valor
|
||||
)->first()->descricao ?? null
|
||||
: AgendamentoExameResultado::where('agendamento_exame_id', $exameId)
|
||||
->where('composicao_id', $query->composicao->id)
|
||||
->first()->valor ?? null,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function getExame($id)
|
||||
{
|
||||
return AgendamentoExame::findOrFail($id);
|
||||
}
|
||||
|
||||
public static function getExames($id, $agendamentoId)
|
||||
{
|
||||
$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.motivo_id AS motivo_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.observacao AS observacao',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'parametrizacao_exame.mapa_agrupado AS mapa_agrupado',
|
||||
'cadastro_grupo.id AS grupo_id',
|
||||
'cadastro_grupo.nome AS grupo_nome',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('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', $agendamentoId);
|
||||
$agendamentos->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
$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 getHistorico($id, $agendamentoId, $pacienteId)
|
||||
{
|
||||
$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',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_laudo, "%d/%m/%Y") AS data'),
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('parametrizacao_exame.id', $id);
|
||||
$agendamentos->where('exame_laboratorial_agendamento.id', '!=', $agendamentoId);
|
||||
$agendamentos->where('cadastro_paciente.id', $pacienteId);
|
||||
$agendamentos->whereNotNull('exame_laboratorial_agendamento_exame.data_laudo');
|
||||
|
||||
$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 loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$grupos = DB::table('parametrizacao_exame')
|
||||
->select([
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
])
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereExists(function ($query) use ($request, $currentDate) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->where(function($query) use ($request, $currentDate) {
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
});
|
||||
})
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$grupos = $grupos->get()->toArray();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'grupos' => $grupos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::saveStatusExame($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function saveStatusExame($request)
|
||||
{
|
||||
|
||||
foreach ($request->agendamentos as $params) {
|
||||
|
||||
$exame = AgendamentoExame::where('agendamento_id', $params['agendamento_id'])
|
||||
->where('exame_id', $params['exame_id'])
|
||||
->first();
|
||||
|
||||
if ($params['nao_realizado']) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::NAO_REALIZADO;
|
||||
$exame->motivo_id = $params['motivo_id'];
|
||||
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])->delete();
|
||||
}
|
||||
|
||||
if ($params['nova_coleta']) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::NOVA_COLETA;
|
||||
$exame->motivo_id = $params['motivo_id'];
|
||||
|
||||
AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])->delete();
|
||||
}
|
||||
|
||||
if (!$params['nao_realizado'] && !$params['nova_coleta']) {
|
||||
|
||||
$exame->status_id = StatusAgendamento::PENDENTE_ASSINATURA;
|
||||
$exame->data_laudo = Carbon::now();
|
||||
}
|
||||
|
||||
$exame->observacao = $params['observacao'];
|
||||
$exame->save();
|
||||
|
||||
if (!$params['nao_realizado'] && !$params['nova_coleta']) {
|
||||
|
||||
foreach ($params['composicao'] as $paramsComposicao) {
|
||||
|
||||
if ($paramsComposicao['resultado'] !== null) {
|
||||
|
||||
$exameComposicao = AgendamentoExameResultado::where('agendamento_exame_id', $params['agendamento_exame_id'])
|
||||
->where('composicao_id', $paramsComposicao['id'])
|
||||
->first() ?? new AgendamentoExameResultado;
|
||||
|
||||
$exameComposicao->agendamento_exame_id = $params['agendamento_exame_id'];
|
||||
$exameComposicao->composicao_id = $paramsComposicao['id'];
|
||||
$exameComposicao->valor = $paramsComposicao['resultado'];
|
||||
|
||||
$exameComposicao->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
368
api/app/Services/ExameLaboratorial/MapaDiaService.php
Normal file
368
api/app/Services/ExameLaboratorial/MapaDiaService.php
Normal file
@@ -0,0 +1,368 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Enums\Parametrizacao\Exame\Tipo;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MapaDiaService extends Service
|
||||
{
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
||||
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
||||
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
||||
'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()
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
DB::raw("COUNT(cadastro_grupo.id) AS quantidade"),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('true AS mapa_agrupado'),
|
||||
DB::raw("
|
||||
(
|
||||
SELECT parametrizacao_exame_composicao.id
|
||||
FROM parametrizacao_exame_composicao
|
||||
WHERE parametrizacao_exame_composicao.visivel = TRUE
|
||||
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
|
||||
LIMIT 1
|
||||
) AS composicao
|
||||
"),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
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->unidade_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('false AS mapa_agrupado'),
|
||||
DB::raw("
|
||||
(
|
||||
SELECT parametrizacao_exame_composicao.id
|
||||
FROM parametrizacao_exame_composicao
|
||||
WHERE parametrizacao_exame_composicao.visivel = TRUE
|
||||
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
|
||||
LIMIT 1
|
||||
) AS composicao
|
||||
"),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function ($query) {
|
||||
|
||||
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
||||
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
||||
});
|
||||
|
||||
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->unidade_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
DB::raw("
|
||||
(
|
||||
SELECT parametrizacao_exame_composicao.id
|
||||
FROM parametrizacao_exame_composicao
|
||||
WHERE parametrizacao_exame_composicao.visivel = TRUE
|
||||
AND parametrizacao_exame_composicao.exame_id = parametrizacao_exame.id
|
||||
LIMIT 1
|
||||
) AS composicao
|
||||
"),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->whereNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
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->unidade_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo))
|
||||
->sortBy([
|
||||
['agendamento_id', 'asc'],
|
||||
['exame_grupo_nome', 'asc'],
|
||||
])
|
||||
->values();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'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()
|
||||
]),
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'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,
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$grupos = DB::table('parametrizacao_exame')
|
||||
->select([
|
||||
'cadastro_grupo.id AS id',
|
||||
'cadastro_grupo.nome AS nome',
|
||||
])
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereExists(function ($query) use ($request, $currentDate) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->where(function($query) use ($request, $currentDate) {
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
});
|
||||
})
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$grupos = $grupos->get()->toArray();
|
||||
|
||||
$unidades = DB::table('cadastro_infraestrutura')
|
||||
->select([
|
||||
'cadastro_unidade.id AS unidade_id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
])
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento')
|
||||
->whereColumn(
|
||||
'exame_laboratorial_agendamento.infraestrutura_id',
|
||||
'cadastro_infraestrutura.id'
|
||||
)
|
||||
->leftJoin('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->where(function($query) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
});
|
||||
})
|
||||
->whereNull('cadastro_unidade.deleted_at')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->distinct('infraestrutura_id');
|
||||
|
||||
$unidades = $unidades->get()->toArray();
|
||||
|
||||
$unidadeInfraestrutura = [];
|
||||
|
||||
foreach ($unidades as $item) {
|
||||
$unidadeId = $item->unidade_id;
|
||||
$infraestruturaId = $item->infraestrutura_id;
|
||||
$nomeInfraestrutura = $item->nome_infraestrutura;
|
||||
|
||||
if (!isset($unidadeInfraestrutura[$unidadeId])) {
|
||||
$unidadeInfraestrutura[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'nome_fantasia' => $item->nome_fantasia,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$unidadeInfraestrutura[$unidadeId]['infraestruturas'][] = [
|
||||
'id' => $infraestruturaId,
|
||||
'nome' => $nomeInfraestrutura,
|
||||
'principal' => $item->principal
|
||||
];
|
||||
}
|
||||
|
||||
usort($unidadeInfraestrutura, function ($a, $b) {
|
||||
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
|
||||
});
|
||||
|
||||
return [
|
||||
'unidades' => $unidadeInfraestrutura,
|
||||
'grupos' => $grupos,
|
||||
];
|
||||
}
|
||||
}
|
||||
348
api/app/Services/ExameLaboratorial/MapaProducaoService.php
Normal file
348
api/app/Services/ExameLaboratorial/MapaProducaoService.php
Normal file
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\Sexo;
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Enums\Parametrizacao\Exame\Tipo;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamento;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MapaProducaoService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
||||
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.observacao AS observacao',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") AS sigla_exame'),
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao.sexo_id = '. Sexo::MASCULINO .' THEN "M" ELSE "F" END AS paciente_sexo_descricao'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
$agendamentos->where('cadastro_grupo.impressao_mapa_producao', true);
|
||||
$agendamentos->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
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->unidade_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->boolean('impressao_pendente')) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentos->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get()->toArray();
|
||||
|
||||
$agendamentosSiglas = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame ORDER BY parametrizacao_exame.sigla_exame ASC separator ", ") as sigla_exame'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
$agendamentosSiglas = $agendamentosSiglas->get()->toArray();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'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()
|
||||
]),
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'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,
|
||||
'unidades_faturamento' => self::getUnidadesFaturamento()
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'agendamentos' => $agendamentos,
|
||||
'agendamentos_siglas' => $agendamentosSiglas,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$grupos = DB::table('parametrizacao_exame')
|
||||
->select([
|
||||
'cadastro_grupo.id AS id',
|
||||
'cadastro_grupo.nome AS nome',
|
||||
])
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->whereExists(function ($query) use ($request, $currentDate) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento_exame')
|
||||
->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->where(function($query) use ($request, $currentDate) {
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
});
|
||||
})
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
||||
->where('cadastro_grupo.impressao_mapa_producao', true)
|
||||
->where('parametrizacao_exame.mapa_agrupado', true)
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$grupos = $grupos->get()->toArray();
|
||||
|
||||
$unidades = DB::table('cadastro_infraestrutura')
|
||||
->select([
|
||||
'cadastro_unidade.id AS unidade_id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
])
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('exame_laboratorial_agendamento')
|
||||
->whereColumn(
|
||||
'exame_laboratorial_agendamento.infraestrutura_id',
|
||||
'cadastro_infraestrutura.id'
|
||||
)
|
||||
->leftJoin('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->where('cadastro_grupo.impressao_mapa_producao', true)
|
||||
->where('parametrizacao_exame.mapa_agrupado', true)
|
||||
->where(function($query) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
});
|
||||
})
|
||||
->whereNull('cadastro_unidade.deleted_at')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->distinct('infraestrutura_id');
|
||||
|
||||
$unidades = $unidades->get()->toArray();
|
||||
|
||||
$unidadeInfraestrutura = [];
|
||||
|
||||
foreach ($unidades as $item) {
|
||||
$unidadeId = $item->unidade_id;
|
||||
$infraestruturaId = $item->infraestrutura_id;
|
||||
$nomeInfraestrutura = $item->nome_infraestrutura;
|
||||
|
||||
if (!isset($unidadeInfraestrutura[$unidadeId])) {
|
||||
$unidadeInfraestrutura[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'nome_fantasia' => $item->nome_fantasia,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$unidadeInfraestrutura[$unidadeId]['infraestruturas'][] = [
|
||||
'id' => $infraestruturaId,
|
||||
'nome' => $nomeInfraestrutura,
|
||||
'principal' => $item->principal
|
||||
];
|
||||
}
|
||||
|
||||
usort($unidadeInfraestrutura, function ($a, $b) {
|
||||
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
|
||||
});
|
||||
|
||||
return [
|
||||
'unidades' => $unidadeInfraestrutura,
|
||||
'grupos' => $grupos,
|
||||
];
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::saveSImpressao($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function saveSImpressao($request)
|
||||
{
|
||||
|
||||
foreach ($request->agendamentos as $params) {
|
||||
|
||||
AgendamentoExame::whereHas('exame', function($query) use ($params) {
|
||||
|
||||
$query->where('grupo_tipo_id', $params['exame_grupo_id']);
|
||||
})
|
||||
->where('agendamento_id', $params['agendamento_id'])
|
||||
->update(['impressao_mapa_producao' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getUnidadesFaturamento()
|
||||
{
|
||||
return UnidadeFaturamento::get()
|
||||
->map(fn($query) => [
|
||||
'id' => $query->id,
|
||||
'unidade_id' => $query->unidade_id,
|
||||
'nomeFantasia' => $query->unidade->nome_fantasia,
|
||||
'infraestrutura_id' => $query->infraestrutura_id,
|
||||
'infraestrutura_nome' => $query->infraestrutura->nome,
|
||||
'profissionais' => $query->unidadeFaturamentoUsuario
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'usuario' => [
|
||||
'id' => $query->usuario_id,
|
||||
'usuario_ocupacao' => $query->usuario->nome . ' - ' . $query->ocupacao->codigo,
|
||||
'ocupacao_id' => $query->ocupacao_id,
|
||||
],
|
||||
];
|
||||
}),
|
||||
'faturamento_relacionado' => $query->unidadeFaturamentoUnidade
|
||||
->map(function ($relacionada) {
|
||||
return [
|
||||
'id' => $relacionada->id,
|
||||
'relacionada_unidade_id' => $relacionada->unidade_id,
|
||||
'unidade_id' => $relacionada->unidade_id_principal,
|
||||
|
||||
'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id,
|
||||
'infraestrutura_id' => $relacionada->infraestrutura_id_principal,
|
||||
'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura_nome' => $relacionada->infraestrutura->nome,
|
||||
];
|
||||
})
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
4098
api/app/Services/ExameLaboratorial/ParametrizacaoService.php
Normal file
4098
api/app/Services/ExameLaboratorial/ParametrizacaoService.php
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,985 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\Sexo;
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\LancamentoColeta\Status;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamento;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioColetaRealizadaService extends Service
|
||||
{
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$coletasRealizadas = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
||||
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'cadastro_unidade.nome_fantasia as unidade_solicitante',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'parametrizacao_exame.nome as exame_nome',
|
||||
'parametrizacao_exame.codigo as codigo_procedimento',
|
||||
'sigtap.procedimento.nome as nome_procedimento',
|
||||
'sigtap.procedimento.id as procedimento_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.cns AS paciente_cns',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS paciente_prontuario'),
|
||||
DB::raw('IF (cadastro_cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y")) AS data_nascimento'),
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_carteira_identidade.numero AS rg',
|
||||
DB::raw('CONCAT(
|
||||
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
||||
" anos"
|
||||
) AS paciente_idade'
|
||||
),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_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'
|
||||
),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("
|
||||
CASE cadastro_cidadao.sexo_id WHEN " . Sexo::MASCULINO . " THEN
|
||||
'MASCULINO'
|
||||
WHEN " . Sexo::FEMININO . " THEN
|
||||
'FEMININO'
|
||||
ELSE
|
||||
'-'
|
||||
END AS paciente_sexo
|
||||
"),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_logradouro_tipo.descricao,
|
||||
" ",
|
||||
cadastro_logradouro.nome,
|
||||
", ",
|
||||
cadastro_endereco.numero,
|
||||
" - ",
|
||||
cadastro_bairro.nome
|
||||
) AS endereco_paciente'),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->leftJoin(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_grupo',
|
||||
'cadastro_grupo.id',
|
||||
'parametrizacao_exame.grupo_tipo_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_laboratorial_agendamento.paciente_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_paciente.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_carteira_identidade',
|
||||
function ($leftJoin) {
|
||||
|
||||
$leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id')
|
||||
->whereNull('cadastro_carteira_identidade.deleted_at');
|
||||
}
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_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'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_domicilio',
|
||||
'cadastro_domicilio.id',
|
||||
'cadastro_cidadao.domicilio_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_endereco',
|
||||
'cadastro_endereco.id',
|
||||
'cadastro_domicilio.endereco_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_logradouro',
|
||||
'cadastro_logradouro.id',
|
||||
'cadastro_endereco.logradouro_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_logradouro_tipo',
|
||||
'cadastro_logradouro_tipo.id',
|
||||
'cadastro_logradouro.tipo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_bairro',
|
||||
'cadastro_bairro.id',
|
||||
'cadastro_logradouro.bairro_id'
|
||||
)
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
|
||||
if ($request->periodo) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
|
||||
} else {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.infraestrutura_id',$request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_unidade_id) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_infraestrutura_id) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
$coletasRealizadas->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->procedimento_id) {
|
||||
|
||||
$coletasRealizadas->where(function ($query) use ($request) {
|
||||
foreach ($request->procedimento_id as $procedimento) {
|
||||
$query->orWhere('sigtap.procedimento.id', $procedimento);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->paciente_id) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
$coletasRealizadas->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
$coletasRealizadas = $coletasRealizadas->get()->toArray();
|
||||
|
||||
$agendamentos = $coletasRealizadas;
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'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,
|
||||
];
|
||||
}),
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'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,
|
||||
'status' => Status::where(function ($query) {
|
||||
$query->where('id', StatusAgendamento::REALIZADO) ;
|
||||
})
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->map(fn($status) => [
|
||||
'id' => $status->id,
|
||||
'descricao' => $status->descricao
|
||||
]),
|
||||
'sistema' => [
|
||||
'ramo_atividade' => [
|
||||
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
|
||||
],
|
||||
'nome' => $cliente->sistema->nome ?? null,
|
||||
'versao' => $cliente->sistema->versao ?? null,
|
||||
],
|
||||
'unidades_faturamento' => self::getUnidadesFaturamento(),
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getUnidadesFaturamento()
|
||||
{
|
||||
return UnidadeFaturamento::get()
|
||||
->map(fn($query) => [
|
||||
'id' => $query->id,
|
||||
'unidade_id' => $query->unidade_id,
|
||||
'nomeFantasia' => $query->unidade->nome_fantasia,
|
||||
'infraestrutura_id' => $query->infraestrutura_id,
|
||||
'infraestrutura_nome' => $query->infraestrutura->nome,
|
||||
'profissionais' => $query->unidadeFaturamentoUsuario
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'usuario' => [
|
||||
'id' => $query->usuario_id,
|
||||
'usuario_ocupacao' => $query->usuario->nome . ' - ' . $query->ocupacao->codigo,
|
||||
'ocupacao_id' => $query->ocupacao_id,
|
||||
],
|
||||
];
|
||||
}),
|
||||
'faturamento_relacionado' => $query->unidadeFaturamentoUnidade
|
||||
->map(function ($relacionada) {
|
||||
return [
|
||||
'id' => $relacionada->id,
|
||||
'relacionada_unidade_id' => $relacionada->unidade_id,
|
||||
'unidade_id' => $relacionada->unidade_id_principal,
|
||||
|
||||
'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id,
|
||||
'infraestrutura_id' => $relacionada->infraestrutura_id_principal,
|
||||
'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura_nome' => $relacionada->infraestrutura->nome,
|
||||
];
|
||||
})
|
||||
]);
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$coletasRealizadas = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
'exame_laboratorial_agendamento_exame.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'
|
||||
)
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
if ($request->periodo) {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data','<=', $request->periodo['end']);
|
||||
|
||||
} else {
|
||||
$coletasRealizadas->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
$agendamentos = $coletasRealizadas->get()->toArray();
|
||||
|
||||
return [
|
||||
'form' => [],
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
'procedimentos' => self::getProcedimentos()->map(fn($proc) =>[
|
||||
'procedimento_id' => $proc->procedimento_id,
|
||||
'procedimento_nome' => $proc->procedimento_nome
|
||||
]),
|
||||
'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,
|
||||
]),
|
||||
'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,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getProcedimentos()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$procedimentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'sigtap.procedimento.nome AS procedimento_nome',
|
||||
'sigtap.procedimento.id AS procedimento_id',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
])
|
||||
->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('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->leftJoin(
|
||||
'parametrizacao_procedimento',
|
||||
'parametrizacao_procedimento.procedimento_codigo',
|
||||
'parametrizacao_exame.codigo'
|
||||
)
|
||||
->where(function ($query) {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
})
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'sigtap.procedimento.id'
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$procedimentos->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$procedimentos->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
|
||||
} else {
|
||||
$procedimentos->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$procedimentos->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
$procedimentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
$procedimentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
$procedimentos->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_infraestrutura_id) {
|
||||
$procedimentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
return $procedimentos->get();
|
||||
}
|
||||
|
||||
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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->where('exame_laboratorial_agendamento_exame.status_id',StatusAgendamento::REALIZADO)
|
||||
->groupBy(['exame_laboratorial_agendamento.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('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->procedimento_id) {
|
||||
$unidades->where(function ($query) {
|
||||
foreach (request()->procedimento_id as $procedimento) {
|
||||
$query->orWhere('sigtap.procedimento.id', $procedimento);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$unidades->orderBy('cadastro_unidade.nome_fantasia');
|
||||
|
||||
return $unidades->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.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('parametrizacao_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()->procedimento_id) {
|
||||
$coletas->where(function ($query) {
|
||||
foreach (request()->procedimento_id as $procedimento) {
|
||||
$query->orWhere('sigtap.procedimento.id', $procedimento);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $coletas->get();
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome',
|
||||
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
||||
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'cadastro_unidade.nome_fantasia as unidade_solicitante',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'parametrizacao_exame.nome as exame_nome',
|
||||
'parametrizacao_exame.codigo as codigo_procedimento',
|
||||
'sigtap.procedimento.nome as nome_procedimento',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.cns AS paciente_cns',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS paciente_prontuario'),
|
||||
DB::raw('IF (cadastro_cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y")) AS data_nascimento'),
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_carteira_identidade.numero AS rg',
|
||||
DB::raw(
|
||||
'CONCAT(
|
||||
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
||||
" anos"
|
||||
) AS paciente_idade'
|
||||
),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_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'
|
||||
),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("
|
||||
CASE cadastro_cidadao.sexo_id WHEN " . Sexo::MASCULINO . " THEN
|
||||
'MASCULINO'
|
||||
WHEN " . Sexo::FEMININO . " THEN
|
||||
'FEMININO'
|
||||
ELSE
|
||||
'-'
|
||||
END AS paciente_sexo
|
||||
"),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_logradouro_tipo.descricao,
|
||||
" ",
|
||||
cadastro_logradouro.nome,
|
||||
", ",
|
||||
cadastro_endereco.numero,
|
||||
" - ",
|
||||
cadastro_bairro.nome
|
||||
) AS endereco_paciente'),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->leftJoin(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_grupo',
|
||||
'cadastro_grupo.id',
|
||||
'parametrizacao_exame.grupo_tipo_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_laboratorial_agendamento.paciente_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_paciente.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_carteira_identidade',
|
||||
function ($leftJoin) {
|
||||
|
||||
$leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id')
|
||||
->whereNull('cadastro_carteira_identidade.deleted_at');
|
||||
}
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_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'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_domicilio',
|
||||
'cadastro_domicilio.id',
|
||||
'cadastro_cidadao.domicilio_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_endereco',
|
||||
'cadastro_endereco.id',
|
||||
'cadastro_domicilio.endereco_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_logradouro',
|
||||
'cadastro_logradouro.id',
|
||||
'cadastro_endereco.logradouro_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_logradouro_tipo',
|
||||
'cadastro_logradouro_tipo.id',
|
||||
'cadastro_logradouro.tipo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_bairro',
|
||||
'cadastro_bairro.id',
|
||||
'cadastro_logradouro.bairro_id'
|
||||
)
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
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()->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()->prontuario) {
|
||||
$agendamentos->where('cadastro_paciente.prontuario', request()->prontuario);
|
||||
}
|
||||
|
||||
if (request()->paciente) {
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente);
|
||||
}
|
||||
|
||||
if (request()->procedimento_id) {
|
||||
$agendamentos->where(function ($query) {
|
||||
foreach (request()->procedimento_id as $procedimento) {
|
||||
$query->orWhere('parametrizacao_exame.codigo', $procedimento);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
self::setOrderBy($agendamentos);
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
|
||||
} else {
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'unidade_solicitante' => request()->unidade_solicitante ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'paciente' => request()->paciente ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'agendamentos' => $agendamentos->map(fn($agendamento) => [
|
||||
'paciente_nome' => $agendamento->paciente_nome ?? '-',
|
||||
'paciente_prontuario' => $agendamento->paciente_prontuario ?? '-',
|
||||
'data' => $agendamento->data ?? '-',
|
||||
'paciente_sexo' => $agendamento->paciente_sexo,
|
||||
'data_nascimento' => $agendamento->data_nascimento,
|
||||
'endereco_paciente' => $agendamento->endereco_paciente ?? '-',
|
||||
'paciente_cns' => MaskUtil::getCnsMasked($agendamento->paciente_cns),
|
||||
'coleta_unidade_nome' => $agendamento->coleta_unidade_nome,
|
||||
'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome,
|
||||
'unidade_solicitante' => $agendamento->unidade_solicitante,
|
||||
'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome,
|
||||
'exame_nome' => $agendamento->exame_nome,
|
||||
'codigo_procedimento' => !empty($agendamento->codigo_procedimento)
|
||||
? $agendamento->codigo_procedimento : '-',
|
||||
'nome_procedimento' => $agendamento->nome_procedimento,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private static function setOrderBy($builder)
|
||||
{
|
||||
|
||||
if (is_null(request()->sortBy)) {
|
||||
|
||||
$builder->orderBy('exame_laboratorial_agendamento_exame.id');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$columns = [
|
||||
'data' => 'exame_laboratorial_agendamento.data',
|
||||
'paciente_nome' => 'cadastro_cidadao.nome',
|
||||
'paciente_prontuario' => 'cadastro_paciente.prontuario',
|
||||
'paciente_cns' => 'cadastro_cidadao.cns',
|
||||
'codigo_procedimento' => 'parametrizacao_exame.codigo',
|
||||
'nome_procedimento' => 'sigtap.procedimento.nome',
|
||||
'exame_nome' => 'parametrizacao_exame.nome'
|
||||
];
|
||||
|
||||
$column = $columns[request()->sortBy];
|
||||
$direction = DataUtil::getBooleanValueFromStringBoolean(request()->sortDesc) ? 'DESC' : 'ASC';
|
||||
|
||||
$builder->orderByRaw("{$column} {$direction}, exame_laboratorial_agendamento_exame.id");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,751 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioCotaConsumoService extends Service
|
||||
{
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
||||
->select([
|
||||
'cadastro_unidade.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
|
||||
'parametrizacao_exame.id as exame_id',
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_utilizado'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_utilizada'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_definido'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_quantidade'),
|
||||
DB::raw('COUNT(parametrizacao_exame.id) AS quantidade'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS coleta_infraestrutura_nome
|
||||
'),
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_nome'),
|
||||
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_parametrizacao_cota_exame',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota',
|
||||
'exame_laboratorial_parametrizacao_cota.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
||||
'cadastro_infraestrutura_laboratorio.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as cadastro_unidade_laboratorio',
|
||||
'cadastro_unidade_laboratorio.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura_laboratorio.unidade_id'
|
||||
)
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
||||
->groupBy([
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
/**
|
||||
* FILTRO DE PERÍODO (CORRIGIDO: INTERSEÇÃO DE DATAS)
|
||||
*/
|
||||
if (request()->periodo) {
|
||||
|
||||
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
|
||||
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
|
||||
|
||||
$agendamentos->where(
|
||||
'exame_laboratorial_parametrizacao_cota.inicio',
|
||||
'<=',
|
||||
$fim->toDateString()
|
||||
);
|
||||
|
||||
$agendamentos->where(
|
||||
'exame_laboratorial_parametrizacao_cota.fim',
|
||||
'>=',
|
||||
$inicio->toDateString()
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentos->where(
|
||||
'exame_laboratorial_parametrizacao_cota.inicio',
|
||||
'<=',
|
||||
$currentDate
|
||||
);
|
||||
|
||||
$agendamentos->where(
|
||||
'exame_laboratorial_parametrizacao_cota.fim',
|
||||
'>=',
|
||||
$currentDate
|
||||
);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
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' => [
|
||||
'agendamentos' => $agendamentos
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id ?? null,
|
||||
'valor_definido' => $query->valor_definido ?? null,
|
||||
'quantidade_definida' => $query->cota_quantidade,
|
||||
'exame_nome' => $query->exame_nome,
|
||||
'exame_id' => $query->exame_id,
|
||||
'procedimento_codigo' => !empty($query->procedimento_codigo) ? $query->procedimento_codigo : '-',
|
||||
'coleta_nome_fantasia' => $query->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $query->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $query->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $query->laboratorio_infraestrutura_nome,
|
||||
'valor_utilizado' => self::getValorUtilizado($query->exame_id),
|
||||
'quantidade_utilizada' => self::getQuantidadeUtilizada($query->exame_id)
|
||||
];
|
||||
})
|
||||
->values()
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getValorUtilizado($exame_id) {
|
||||
|
||||
$currentDate = Carbon::now()->format('Y-m-d');
|
||||
$periodoStart = request()->periodo['start'] ?? $currentDate;
|
||||
$periodoEnd = request()->periodo['end'] ?? $currentDate;
|
||||
|
||||
return AgendamentoExame::where('exame_id', $exame_id)
|
||||
->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'status_id',
|
||||
[
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
)
|
||||
->orWhereNull('status_id');
|
||||
})
|
||||
->whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd) {
|
||||
|
||||
$query
|
||||
->where(
|
||||
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
||||
'>=',
|
||||
Carbon::createFromFormat('Y-m-d', $periodoStart)
|
||||
->format('Y-m-d')
|
||||
)
|
||||
->where(
|
||||
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
||||
'<=',
|
||||
Carbon::createFromFormat('Y-m-d', $periodoEnd)
|
||||
->format('Y-m-d')
|
||||
);
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$query->where('unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$query->where('infraestrutura_id', request()->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);
|
||||
}
|
||||
})
|
||||
->sum('valor');
|
||||
}
|
||||
|
||||
public static function getQuantidadeUtilizada($exame_id) {
|
||||
|
||||
$currentDate = Carbon::now()->format('Y-m-d');
|
||||
$periodoStart = request()->periodo['start'] ?? $currentDate;
|
||||
$periodoEnd = request()->periodo['end'] ?? $currentDate;
|
||||
|
||||
return AgendamentoExame::where('exame_id', $exame_id)
|
||||
->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'status_id',
|
||||
[
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
)
|
||||
->orWhereNull('status_id');
|
||||
})
|
||||
->whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd) {
|
||||
|
||||
$query
|
||||
->where(
|
||||
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
||||
'>=',
|
||||
Carbon::createFromFormat('Y-m-d', $periodoStart)
|
||||
->format('Y-m-d')
|
||||
)
|
||||
->where(
|
||||
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
||||
'<=',
|
||||
Carbon::createFromFormat('Y-m-d', $periodoEnd)
|
||||
->format('Y-m-d')
|
||||
);
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$query->where('unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$query->where('infraestrutura_id', request()->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);
|
||||
}
|
||||
})
|
||||
->count('id');
|
||||
}
|
||||
|
||||
public static function loadFields()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$inicio = null;
|
||||
$fim = null;
|
||||
|
||||
if (request()->periodo) {
|
||||
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
|
||||
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
|
||||
}
|
||||
|
||||
$exames = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
||||
->select([
|
||||
'parametrizacao_exame.id as id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS nome'),
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota_exame',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota',
|
||||
'exame_laboratorial_parametrizacao_cota.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
|
||||
)
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
||||
->groupBy(['parametrizacao_exame.id']);
|
||||
|
||||
if ($inicio && $fim) {
|
||||
|
||||
$exames->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
|
||||
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$exames->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$exames = $exames->get();
|
||||
|
||||
$unidades = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
||||
->select([
|
||||
'cadastro_unidade.nome_fantasia AS nome',
|
||||
'cadastro_unidade.id AS id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_infraestrutura.principal AS principal',
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
'),
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota_exame',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota',
|
||||
'exame_laboratorial_parametrizacao_cota.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
||||
->groupBy(['cadastro_infraestrutura.id']);
|
||||
|
||||
if ($inicio && $fim) {
|
||||
|
||||
$unidades->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
|
||||
|
||||
} else {
|
||||
|
||||
$unidades->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$unidades->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$unidades = $unidades->get();
|
||||
|
||||
$laboratorios = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
||||
->select([
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS nome',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_parametrizacao_cota.unidade_id AS id',
|
||||
'cadastro_infraestrutura_laboratorio.principal as principal',
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS infraestrutura_nome
|
||||
'),
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota_exame',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota',
|
||||
'exame_laboratorial_parametrizacao_cota.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
||||
'cadastro_infraestrutura_laboratorio.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as cadastro_unidade_laboratorio',
|
||||
'cadastro_unidade_laboratorio.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura_laboratorio.unidade_id'
|
||||
)
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
||||
->groupBy(['cadastro_infraestrutura_laboratorio.id']);
|
||||
|
||||
if ($inicio && $fim) {
|
||||
|
||||
$laboratorios->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
|
||||
|
||||
} else {
|
||||
|
||||
$laboratorios->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
$laboratorios = $laboratorios->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'unidades' => $unidades,
|
||||
'laboratorios' => $laboratorios,
|
||||
'exames' => $exames,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$inicio = null;
|
||||
$fim = null;
|
||||
|
||||
if (request()->periodo) {
|
||||
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
|
||||
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
|
||||
}
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
||||
->select([
|
||||
'cadastro_unidade.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
|
||||
'parametrizacao_exame.id as exame_id',
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_utilizado'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_utilizada'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_definido'),
|
||||
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_quantidade'),
|
||||
DB::raw('COUNT(parametrizacao_exame.id) AS quantidade'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS coleta_infraestrutura_nome
|
||||
'),
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_nome'),
|
||||
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_parametrizacao_cota_exame',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_parametrizacao_cota',
|
||||
'exame_laboratorial_parametrizacao_cota.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
||||
'cadastro_infraestrutura_laboratorio.id',
|
||||
'=',
|
||||
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as cadastro_unidade_laboratorio',
|
||||
'cadastro_unidade_laboratorio.id',
|
||||
'=',
|
||||
'cadastro_infraestrutura_laboratorio.unidade_id'
|
||||
)
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
|
||||
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
||||
->groupBy(['parametrizacao_exame.id']);
|
||||
|
||||
if ($inicio && $fim) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
|
||||
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
$agendamentos->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_numero ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'agendamentos' => $agendamentos
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->id ?? null,
|
||||
'valor_definido' => $query->valor_definido > 0
|
||||
? 'R$ ' . number_format($query->valor_definido, 2, ',', '.')
|
||||
: '-',
|
||||
'quantidade_definida' => $query->cota_quantidade,
|
||||
'exame_nome' => $query->exame_nome,
|
||||
'exame_id' => $query->exame_id,
|
||||
'procedimento_codigo' => !empty($query->procedimento_codigo) ? $query->procedimento_codigo : '-',
|
||||
'coleta_nome_fantasia' => $query->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $query->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $query->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $query->laboratorio_infraestrutura_nome,
|
||||
'valor_utilizado' => $query->valor_utilizado > 0
|
||||
? 'R$ ' . number_format(self::getValorUtilizado($query->exame_id), 2, ',', '.')
|
||||
: '-',
|
||||
'quantidade_utilizada' => $query->cota_quantidade
|
||||
? self::getQuantidadeUtilizada($query->exame_id)
|
||||
: '-'
|
||||
];
|
||||
})
|
||||
->values()
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioEntregaExameListService extends Service
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('true AS mapa_agrupado'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
])
|
||||
->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');
|
||||
|
||||
$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) {
|
||||
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
if ($request->exames) {
|
||||
|
||||
$agendamentosComGrupo->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']);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(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',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
])
|
||||
->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->exame_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
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',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'cadastro_cidadao_medico.nome AS nome_medico',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
DB::raw('UPPER(parametrizacao_exame.nome) AS exame_grupo_nome'),
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'exame_laboratorial_agendamento_exame.entregue AS entregue',
|
||||
])
|
||||
->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->exame_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
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' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,679 @@
|
||||
<?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 RelatorioEntregaExameSelecionadoService extends Service
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,430 @@
|
||||
<?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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class RelatorioExameAgendadoRealizadoService extends Service
|
||||
{
|
||||
public static function index($id)
|
||||
{
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamento = Agendamento::where('paciente_id', $id)->firstOrFail();
|
||||
|
||||
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,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente' => [
|
||||
'nome' => $agendamento->paciente->cidadao->nome ?? null,
|
||||
'nome_social' => $agendamento->paciente->cidadao->nome_social,
|
||||
'prefere_nome_social' => $agendamento->paciente->cidadao
|
||||
->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'data_nascimento' => $agendamento->paciente->cidadao->data_nascimento
|
||||
? $agendamento->paciente->cidadao->data_nascimento->format('d/m/Y')
|
||||
: '',
|
||||
'cpf' => $agendamento->paciente->cidadao->cpf
|
||||
? MaskUtil::getCpfMasked($agendamento->paciente->cidadao->cpf) : null,
|
||||
'rg' => $agendamento->paciente->cidadao->carteiraIdentidade->numero ?? null,
|
||||
'convenio_medico_nome' => $agendamento->paciente->cidadao->conveniosMedico->first()->nome ?? null,
|
||||
'situacao_familiar' => $agendamento->paciente->cidadao->situacaoFamiliarConjugal->descricao ?? null,
|
||||
'prontuario' => $agendamento->paciente->prontuario,
|
||||
'unidade' => $agendamento->paciente->unidade->nome_fantasia ?? null,
|
||||
'sexo' => $agendamento->paciente->cidadao->sexo ?? '-',
|
||||
'classificacao_id' => $agendamento->paciente->classificacao_id ?? '-',
|
||||
'classificacao_nome' => $agendamento->paciente->classificacao->descricao ?? '-',
|
||||
'cns' => $agendamento->paciente->cidadao->cns ? MaskUtil::getCnsMasked($agendamento->paciente->cidadao->cns) : '',
|
||||
'telefone' => MaskUtil::getPhoneMasked($agendamento->paciente->cidadao->telefone_principal->numero ?? ''),
|
||||
],
|
||||
'endereco' => [
|
||||
'estado_id' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio->estado->id,
|
||||
'municipio' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro->municipio,
|
||||
'logradouro' => $agendamento->paciente->cidadao->domicilio->endereco->logradouro,
|
||||
'cep' => MaskUtil::getCepMasked($agendamento->paciente->cidadao->domicilio->endereco->logradouro->cep),
|
||||
'numero' => $agendamento->paciente->cidadao->domicilio->endereco->numero,
|
||||
'sem_numero' => $agendamento->paciente->cidadao->domicilio->endereco->sem_numero,
|
||||
'complemento' => $agendamento->paciente->cidadao->domicilio->endereco->complemento ?? '',
|
||||
'zona_id' => $agendamento->paciente->cidadao->domicilio->endereco->zona_id,
|
||||
],
|
||||
'agendados' => Agendamento::where('paciente_id', $id)
|
||||
->whereHas('agendamentoExame', function($query) {
|
||||
$query->whereNull('status_id')
|
||||
->orWhere('status_id', StatusAgendamento::AGENDADO);
|
||||
})
|
||||
->get()
|
||||
->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'data' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
||||
'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null,
|
||||
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
||||
->get()
|
||||
->map(function ($query) use ($agendamento) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame->nome,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'valor' => $query->valor,
|
||||
'status' => $query->status->descricao ?? 'AGENDADO',
|
||||
'status_id' => $query->status_id,
|
||||
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
||||
'data_entrega' => (new Carbon($agendamento->data_coleta))
|
||||
->addDays($query->exame->prazo_entrega)->format('d/m/Y') ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'medico_id' => $agendamento->medico_id,
|
||||
'medico_nome' => $agendamento->medico->nome,
|
||||
'observacao' => $agendamento->observacao,
|
||||
'paciente_id' => $agendamento->paciente_id,
|
||||
'suspeita' => $agendamento->suspeita_clinica,
|
||||
'unidade' => $agendamento->unidade->nome_fantasia,
|
||||
'infraestrutura' => $agendamento->infraestrutura->nome,
|
||||
'medicamentos' => $agendamento->uso_medicamento,
|
||||
])
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
'realizados' => Agendamento::where('paciente_id', $id)
|
||||
->whereHas('agendamentoExame', function($query) {
|
||||
$query->where('status_id', StatusAgendamento::REALIZADO);
|
||||
})
|
||||
->get()
|
||||
->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'data' => (new Carbon($agendamento->data))->format('d/m/Y') ?? null,
|
||||
'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null,
|
||||
'incluido' => AgendamentoExame::where('agendamento_id', $agendamento->id)
|
||||
->get()
|
||||
->map(function ($query) use ($agendamento) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => $query->exame->nome,
|
||||
'codigo' => $query->exame->codigo ?? null,
|
||||
'valor' => $query->valor,
|
||||
'status' => self::getStatus($query),
|
||||
'status_id' => $query->status_id,
|
||||
'data_entrega' => (new Carbon($agendamento->data_coleta))
|
||||
->addDays($query->exame->prazo_entrega)->format('d/m/Y') ?? null,
|
||||
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
||||
'observacao' => $query->exame->observacao ?? null
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
'medico_id' => $agendamento->medico_id,
|
||||
'medico_nome' => $agendamento->medico->nome,
|
||||
'observacao' => $agendamento->observacao,
|
||||
'paciente_id' => $agendamento->paciente_id,
|
||||
'suspeita' => $agendamento->suspeita_clinica,
|
||||
'unidade' => $agendamento->unidade->nome_fantasia,
|
||||
'infraestrutura' => $agendamento->infraestrutura->nome,
|
||||
'medicamentos' => $agendamento->uso_medicamento,
|
||||
])
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
return $query->status->descricao;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,423 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Cidadao;
|
||||
use App\Models\Parametrizacao\Exame\Exame;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioExameBloqueadoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$totalizadores = (object) ['quantidade' => 0];
|
||||
|
||||
$consulta = DB::table('exame_laboratorial_agendamento_exame_bloqueado')
|
||||
->select([
|
||||
DB::raw("CONCAT(DATE_FORMAT(exame_laboratorial_agendamento_exame_bloqueado.created_at, '%d/%m/%Y'), ' ', DATE_FORMAT(exame_laboratorial_agendamento_exame_bloqueado.created_at, '%H:%i')) as data"),
|
||||
'cadastro_cidadao.nome',
|
||||
'cadastro_unidade.nome_fantasia as unidade',
|
||||
'cidadao_profissional.nome as profissional',
|
||||
'parametrizacao_exame.nome as exame',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.valor',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado_motivo.descricao as motivo',
|
||||
DB::raw("CASE WHEN cidadao_supervisor.id IS NOT NULL THEN 'LIBERADO' ELSE 'BLOQUEADO' END as status"),
|
||||
DB::raw("CASE WHEN cidadao_supervisor.id IS NOT NULL THEN cidadao_supervisor.nome ELSE 'NÃO INFORMADO' END as supervisor")
|
||||
])
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.exame_id',
|
||||
'parametrizacao_exame.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_paciente',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.paciente_id',
|
||||
'cadastro_paciente.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_paciente.cidadao_id',
|
||||
'cadastro_cidadao.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_agendamento_exame_bloqueado_motivo',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado_motivo.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.motivo_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario as cadastro_profissional',
|
||||
'cadastro_lotacao.usuario_id',
|
||||
'cadastro_profissional.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cidadao_profissional',
|
||||
'cadastro_profissional.cidadao_id',
|
||||
'cidadao_profissional.id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_usuario as usuario_supervisor',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id',
|
||||
'usuario_supervisor.id',
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_cidadao as cidadao_supervisor',
|
||||
'usuario_supervisor.cidadao_id',
|
||||
'cidadao_supervisor.id'
|
||||
)
|
||||
->when(request()->periodo['start'], function ($query) {
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data);
|
||||
})
|
||||
->when(request()->periodo['end'], function ($query) {
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data);
|
||||
})
|
||||
->when(request()->unidade_id, function ($query) {
|
||||
$query->where('cadastro_unidade.id', request()->unidade_id);
|
||||
})
|
||||
->when(request()->exame_id, function ($query) {
|
||||
$query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id);
|
||||
})
|
||||
->when(request()->profissional_id, function ($query) {
|
||||
$query->where('cidadao_profissional.id', request()->profissional_id);
|
||||
})
|
||||
->when(request()->supervisor_id, function ($query) {
|
||||
$query->where('cidadao_supervisor.id', request()->supervisor_id);
|
||||
})
|
||||
->when(request()->status, function ($query) {
|
||||
|
||||
if (request()->status == 'bloqueado') {
|
||||
|
||||
$query->whereNull('exame_laboratorial_agendamento_exame_bloqueado.supervisor_id');
|
||||
} else {
|
||||
$query->whereNotNull('exame_laboratorial_agendamento_exame_bloqueado.supervisor_id');
|
||||
}
|
||||
})
|
||||
->orderBy('exame_laboratorial_agendamento_exame_bloqueado.created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$totalizadores = [
|
||||
'quantidade_bloqueado' => $consulta->whereNull('supervisor_id')->count('id'),
|
||||
'quantidade_liberado' => $consulta->whereNotNull('supervisor_id')->count('id'),
|
||||
'quantidade_total' => $consulta->count('id'),
|
||||
'total_bloqueado' => number_format($consulta->whereNull('supervisor_id')->sum('valor'), 2, ',', '.'),
|
||||
'total_liberado' => number_format($consulta->whereNotNull('supervisor_id')->sum('valor'), 2, ',', '.'),
|
||||
'total' => number_format($consulta->sum('valor'), 2, ',', '.'),
|
||||
];
|
||||
|
||||
$consulta = $consulta->map(function ($item) {
|
||||
$item->valor = number_format($item->valor, 2, ',', '.');
|
||||
return $item;
|
||||
});
|
||||
|
||||
return ['consulta' => $consulta, 'totalizadores' => $totalizadores];
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
return [
|
||||
'additionals' => [],
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'unidades' => self::getUnidades(),
|
||||
'exames' => self::getExames(),
|
||||
'profissionais' => self::getProfissionais(),
|
||||
'supervisores' => self::getSupervisores(),
|
||||
'status' => [
|
||||
['value' => 'bloqueado', 'text' => 'BLOQUEADO'],
|
||||
['value' => 'liberado', 'text' => 'LIBERADO'],
|
||||
],
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'unidade_id' => null,
|
||||
'exame_id' => null,
|
||||
'supervisor_id' => null,
|
||||
'profissional_id' => null,
|
||||
'status' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getUnidades()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$unidades = DB::table('exame_laboratorial_agendamento_exame_bloqueado')
|
||||
->select([
|
||||
'cadastro_unidade.id',
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome'),
|
||||
])
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data);
|
||||
} else {
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy([
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
]);
|
||||
|
||||
return $unidades->get();
|
||||
}
|
||||
|
||||
public static function getPrintExportData()
|
||||
{
|
||||
$resultado = self::index();
|
||||
$itens = $resultado['consulta'];
|
||||
$unidade = Unidade::find(request()->unidade_id);
|
||||
$exame = Exame::find(request()->exame_id);
|
||||
$profissional = Cidadao::find(request()->profissional_id);
|
||||
$supervisor = Cidadao::find(request()->supervisor_id);
|
||||
|
||||
return [
|
||||
'inicio' => request()->periodo['start'],
|
||||
'fim' => request()->periodo['end'],
|
||||
'unidade' => $unidade->nome_fantasia ?? 'Todas',
|
||||
'exame' => $exame->nome ?? 'Todos',
|
||||
'profissional' => $profissional->nome ?? 'Todos',
|
||||
'supervisor' => $supervisor->nome ?? 'Todos',
|
||||
'status' => request()->status ? strtoupper(request()->status) : 'Todos',
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'itens' => $itens,
|
||||
'totalizadores' => $resultado['totalizadores'],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getExames()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return DB::table('exame_laboratorial_agendamento_exame_bloqueado')
|
||||
->select([
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
])
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.exame_id'
|
||||
)
|
||||
->when(request()->unidade_id, function ($query) {
|
||||
$query->where('cadastro_unidade.id', request()->unidade_id);
|
||||
})
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data);
|
||||
} else {
|
||||
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy('parametrizacao_exame.id')
|
||||
->orderBy('parametrizacao_exame.nome', 'ASC')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getProfissionais()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return DB::table('exame_laboratorial_agendamento_exame_bloqueado')
|
||||
->select([
|
||||
'cidadao_profissional.id AS profissional_id',
|
||||
'cidadao_profissional.nome AS profissional_nome',
|
||||
])
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario as cadastro_profissional',
|
||||
'cadastro_lotacao.usuario_id',
|
||||
'cadastro_profissional.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cidadao_profissional',
|
||||
'cadastro_profissional.cidadao_id',
|
||||
'cidadao_profissional.id'
|
||||
)
|
||||
->when(request()->unidade_id, function ($query) {
|
||||
$query->where('cadastro_unidade.id', request()->unidade_id);
|
||||
})
|
||||
->when(request()->exame_id, function ($query) {
|
||||
$query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id);
|
||||
})
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data);
|
||||
} else {
|
||||
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy('cidadao_profissional.id')
|
||||
->orderBy('cidadao_profissional.nome', 'ASC')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getSupervisores()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return DB::table('exame_laboratorial_agendamento_exame_bloqueado')
|
||||
->select([
|
||||
'cidadao_supervisor.id AS supervisor_id',
|
||||
'cidadao_supervisor.nome AS supervisor_nome',
|
||||
])
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario as cadastro_profissional',
|
||||
'cadastro_lotacao.usuario_id',
|
||||
'cadastro_profissional.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cidadao_profissional',
|
||||
'cadastro_profissional.cidadao_id',
|
||||
'cidadao_profissional.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario as usuario_supervisor',
|
||||
'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id',
|
||||
'usuario_supervisor.id',
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cidadao_supervisor',
|
||||
'usuario_supervisor.cidadao_id',
|
||||
'cidadao_supervisor.id'
|
||||
)
|
||||
->when(request()->unidade_id, function ($query) {
|
||||
$query->where('cadastro_unidade.id', request()->unidade_id);
|
||||
})
|
||||
->when(request()->exame_id, function ($query) {
|
||||
$query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id);
|
||||
})
|
||||
->when(request()->profissional_id, function ($query) {
|
||||
$query->where('cidadao_profissional.id', request()->profissional_id);
|
||||
})
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data);
|
||||
} else {
|
||||
|
||||
$query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy('cidadao_profissional.id')
|
||||
->orderBy('cidadao_profissional.nome', 'ASC')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,598 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioExameLaboratorioApoioService extends Service
|
||||
{
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS paciente_data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->grupo_id) {
|
||||
|
||||
$exames->where('parametrizacao_exame.grupo_tipo_id', request()->grupo_id);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$exames->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$exames->where(function($query) {
|
||||
|
||||
$query->whereNotIn('exame_laboratorial_agendamento_exame.status_id', [StatusAgendamento::AGENDADO])
|
||||
->whereNotNull('exame_laboratorial_agendamento_exame.status_id');
|
||||
|
||||
});
|
||||
|
||||
$exames = $exames->get();
|
||||
|
||||
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()
|
||||
]),
|
||||
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
||||
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
||||
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
||||
],
|
||||
'model' => [
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'exame_nome' => mb_strtoupper($exame->exame_nome),
|
||||
'exame_grupo_nome' => mb_strtoupper($exame->exame_grupo_nome),
|
||||
'paciente_nome' => $exame->paciente_nome,
|
||||
'paciente_preferencia_nome_id' => $exame->paciente_preferencia_nome_id,
|
||||
'paciente_nome_social' => $exame->paciente_nome_social,
|
||||
'paciente_idade' => Paciente::where('id', $exame->paciente_id)->first()->cidadao->idade,
|
||||
'paciente_data_nascimento' => $exame->paciente_data_nascimento,
|
||||
'data_coleta' => $exame->data_coleta,
|
||||
'unidade' => $exame->nome_fantasia,
|
||||
'infraestrutura_nome' => $exame->infraestrutura_nome,
|
||||
'medico_nome' => $exame->medico_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 ($laboratorio) => [
|
||||
'laboratorio_id' => $laboratorio->laboratorio_unidade_id,
|
||||
'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome,
|
||||
'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id,
|
||||
'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
'exames' => self::getExames()->map(fn ($exame) => [
|
||||
'id' => $exame->exame_id,
|
||||
'nome' => mb_strtoupper($exame->exame_nome),
|
||||
]),
|
||||
'grupos' => self::getGrupos()->map(fn ($grupo) => [
|
||||
'id' => $grupo->exame_grupo_id,
|
||||
'nome' => mb_strtoupper($grupo->exame_grupo_nome),
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getGrupos() {
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$grupos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
])
|
||||
->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([
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$grupos->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$grupos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
$grupos->whereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
|
||||
return $grupos->get();
|
||||
}
|
||||
|
||||
public static function getExames() {
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
]);
|
||||
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->grupo_id) {
|
||||
|
||||
$exames->where('parametrizacao_exame.grupo_tipo_id', request()->grupo_id);
|
||||
}
|
||||
|
||||
$exames->whereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
|
||||
return $exames->get();
|
||||
}
|
||||
|
||||
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([
|
||||
'cadastro_infraestrutura.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()->laboratorio_id) {
|
||||
|
||||
$unidades->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$unidades->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
$unidades->whereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
|
||||
return $unidades->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([
|
||||
'cadastro_infraestrutura_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);
|
||||
}
|
||||
|
||||
$laboratorios->whereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
|
||||
return $laboratorios->get();
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
DB::raw('CONCAT(
|
||||
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
||||
" anos"
|
||||
) AS paciente_idade'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS paciente_data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->grupo_id) {
|
||||
|
||||
$exames->where('parametrizacao_exame.grupo_tipo_id', request()->grupo_id);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$exames->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$exames->whereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
|
||||
$exames = $exames->orderBy('data_coleta', 'ASC');
|
||||
|
||||
$exames = $exames->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'grupo_nome' => request()->grupo_id && request()->grupo_nome ? request()->grupo_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'exame_nome' => mb_strtoupper($exame->exame_nome),
|
||||
'exame_grupo_nome' => mb_strtoupper($exame->exame_grupo_nome),
|
||||
'paciente_nome' => $exame->paciente_nome,
|
||||
'paciente_preferencia_nome_id' => $exame->paciente_preferencia_nome_id,
|
||||
'paciente_nome_social' => $exame->paciente_nome_social,
|
||||
'paciente_idade' => $exame->paciente_idade,
|
||||
'paciente_data_nascimento' => $exame->paciente_data_nascimento,
|
||||
'data_coleta' => $exame->data_coleta,
|
||||
'unidade' => $exame->nome_fantasia,
|
||||
'infraestrutura_nome' => $exame->infraestrutura_nome,
|
||||
'medico_nome' => $exame->medico_nome,
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
618
api/app/Services/ExameLaboratorial/RelatorioExameListService.php
Normal file
618
api/app/Services/ExameLaboratorial/RelatorioExameListService.php
Normal file
@@ -0,0 +1,618 @@
|
||||
<?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 RelatorioExameListService extends Service
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
$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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioExameProducaoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("SUM(parametrizacao_exame.quantidade_reagente) AS quantidade_reagente"),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'parametrizacao_exame.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()->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);
|
||||
}
|
||||
|
||||
$agendamentos->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_numero ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
||||
'coleta_nome_fantasia' => $agendamento->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $agendamento->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome,
|
||||
'exame_nome' => $agendamento->exame_nome,
|
||||
'procedimento_codigo' => !empty($agendamento->procedimento_codigo)
|
||||
? $agendamento->procedimento_codigo : '-',
|
||||
'quantidade_reagente' => $agendamento->quantidade_reagente ?? '0',
|
||||
'quantidade' => $agendamento->quantidade,
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
429
api/app/Services/ExameLaboratorial/RelatorioExameService.php
Normal file
429
api/app/Services/ExameLaboratorial/RelatorioExameService.php
Normal file
@@ -0,0 +1,429 @@
|
||||
<?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 RelatorioExameService 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);
|
||||
|
||||
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,
|
||||
'fator' => $variavel->fator->fator,
|
||||
'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 ?? null;
|
||||
}
|
||||
|
||||
} 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,858 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioExameSolicitadoStatusService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$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.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');
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStart = $currentDate;
|
||||
$periodoEnd = $currentDate;
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_numero ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS',
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'status' => collect(request()->status_selecionado),
|
||||
'total_status' => collect(request()->status_selecionado)
|
||||
->map(function ($query) {
|
||||
return $query["id"];
|
||||
})
|
||||
->toArray(),
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'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) {
|
||||
|
||||
$query
|
||||
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id)
|
||||
->where('data', '>=', request()->periodo['start'])
|
||||
->where('data', '<=', request()->periodo['end'])
|
||||
->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' => Agendamento::whereHas('agendamentoExame', 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
->whereHas('paciente', function($query) {
|
||||
|
||||
if (request()->prontuario) {
|
||||
$query->where('prontuario', request()->prontuario);
|
||||
}
|
||||
})
|
||||
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id)
|
||||
->where(function($query) {
|
||||
|
||||
|
||||
if (request()->agendamento_id) {
|
||||
|
||||
$query->where('agendamento_id', request()->agendamento_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$query->where('infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$query->where('unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
|
||||
$query->where('coleta_unidade_id', request()->coleta_unidade_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('data', '>=', request()->periodo['start'])
|
||||
->where('data', '<=', request()->periodo['end'])
|
||||
->get()
|
||||
->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'paciente_nome' => $agendamento->paciente->cidadao->nome ?? null,
|
||||
'paciente_nome_social' => $agendamento->paciente->cidadao->nome_social,
|
||||
'prefereNomeSocial' => $agendamento->paciente->cidadao
|
||||
->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'paciente_prontuario' => $agendamento->paciente->prontuario,
|
||||
'data' => $agendamento->data ?? null,
|
||||
'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null,
|
||||
'exames' => AgendamentoExame::where('agendamento_id', $agendamento->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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
->get()
|
||||
->map(function ($query) use ($agendamento) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'exame_nome' => mb_strtoupper($query->exame->nome),
|
||||
'procedimento' => $query->exame->procedimento
|
||||
? $query->exame->procedimento->nome : "SEM PROCEDIMENTO",
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'data_agendamento' => $agendamento->data->format('d/m/Y'),
|
||||
'status' => $query->status_id ? self::getStatus($query) : 'AGENDADO',
|
||||
'data_coleta' => $query->data_coleta ? $query->data_coleta->format('d/m/Y') : '-',
|
||||
'unidade' => $agendamento->unidade->nome_fantasia,
|
||||
'infraestrutura_nome' => $agendamento->infraestrutura->nome,
|
||||
'unidade_coleta' => $agendamento->coletaUnidade->nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $agendamento->coletaInfraestrutura->nome,
|
||||
'laboratorio' => $agendamento->laboratorioUnidade->nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $agendamento->laboratorioInfraestrutura->nome,
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
])
|
||||
->sortBy('data')
|
||||
->values(),
|
||||
]),
|
||||
'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(),
|
||||
];
|
||||
}
|
||||
|
||||
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->status->descricao;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,768 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioFinanceiroExameService extends Service
|
||||
{
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
||||
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
'exame_laboratorial_agendamento_exame.valor as valor_unitario',
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
||||
cadastro_unidade_coleta.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_coleta.nome
|
||||
END AS coleta_infraestrutura_nome'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome'),
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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')
|
||||
->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')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento_exame.valor',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->order) {
|
||||
|
||||
$exames->orderBy('exame_nome', request()->order);
|
||||
$exames->orderBy('valor_unitario', request()->order);
|
||||
}
|
||||
|
||||
$exames->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::DUPLICIDADE,
|
||||
StatusAgendamento::OUTROS,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$exames = $exames->get();
|
||||
|
||||
$total = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
||||
])
|
||||
->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');
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
$total->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::DUPLICIDADE,
|
||||
StatusAgendamento::OUTROS,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$total = $total->first();
|
||||
|
||||
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' => [
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'exame_nome' => mb_strtoupper($exame->exame_nome),
|
||||
'procedimento' => $exame->procedimento_codigo
|
||||
? $exame->procedimento_codigo . ' - ' . $exame->procedimento_nome
|
||||
: 'SEM PROCEDIMENTO',
|
||||
'valor_unitario' => number_format($exame->valor_unitario, 2, ',', '.'),
|
||||
'valor_total' => number_format($exame->valor_total, 2, ',', '.'),
|
||||
'quantidade' => number_format($exame->quantidade, 0),
|
||||
'unidade' => $exame->nome_fantasia,
|
||||
'infraestrutura_nome' => $exame->infraestrutura_nome,
|
||||
'unidade_coleta' => $exame->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $exame->coleta_infraestrutura_nome,
|
||||
'laboratorio' => $exame->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $exame->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
'total' => number_format($total->valor_total, 2, ',', '.'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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,
|
||||
]),
|
||||
'coletas' => self::getColetas()->map(fn ($coleta) => [
|
||||
'coleta_unidade_id' => $coleta->unidade_coleta_id,
|
||||
'coleta_unidade_nome' => $coleta->unidade_coleta_nome,
|
||||
'coleta_infraestrutura_id' => $coleta->coleta_infraestrutura_id,
|
||||
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
||||
]),
|
||||
'laboratorios' => self::getLaboratorios()->map(fn ($laboratorio) => [
|
||||
'laboratorio_id' => $laboratorio->laboratorio_unidade_id,
|
||||
'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome,
|
||||
'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id,
|
||||
'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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([
|
||||
'cadastro_infraestrutura.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);
|
||||
}
|
||||
|
||||
$unidades->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
return $unidades->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([
|
||||
'cadastro_infraestrutura_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()->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);
|
||||
}
|
||||
|
||||
$coletas->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
return $coletas->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([
|
||||
'cadastro_infraestrutura_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()->unidade_id) {
|
||||
|
||||
$laboratorios->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$laboratorios->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_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);
|
||||
}
|
||||
|
||||
$laboratorios->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
return $laboratorios->get();
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
||||
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
'exame_laboratorial_agendamento_exame.valor as valor_unitario',
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
||||
cadastro_unidade_coleta.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_coleta.nome
|
||||
END AS coleta_infraestrutura_nome'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome'),
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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')
|
||||
->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')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento_exame.valor',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->order) {
|
||||
|
||||
$exames->orderBy('exame_nome', request()->order);
|
||||
$exames->orderBy('valor_unitario', request()->order);
|
||||
}
|
||||
|
||||
$exames->where(function($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::DUPLICIDADE,
|
||||
StatusAgendamento::OUTROS
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$exames = $exames->get();
|
||||
$total = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
||||
])
|
||||
->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');
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_unidade_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if (request()->coleta_infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
||||
}
|
||||
|
||||
if (request()->laboratorio_infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
||||
}
|
||||
|
||||
$total->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::AGENDADO,
|
||||
StatusAgendamento::DUPLICIDADE,
|
||||
StatusAgendamento::OUTROS,
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$total = $total->first();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_id ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS',
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'status' => collect(request()->status_selecionado),
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'exame_nome' => mb_strtoupper($exame->exame_nome),
|
||||
'procedimento' => $exame->procedimento_codigo
|
||||
? $exame->procedimento_codigo . ' - ' . $exame->procedimento_nome
|
||||
: 'SEM PROCEDIMENTO',
|
||||
'valor_unitario' => number_format($exame->valor_unitario, 2, ',', '.'),
|
||||
'valor_total' => number_format($exame->valor_total, 2, ',', '.'),
|
||||
'quantidade' => number_format($exame->quantidade, 0),
|
||||
'unidade' => $exame->nome_fantasia,
|
||||
'infraestrutura_nome' => $exame->infraestrutura_nome,
|
||||
'unidade_coleta' => $exame->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $exame->coleta_infraestrutura_nome,
|
||||
'laboratorio' => $exame->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $exame->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
'total' => number_format($total->valor_total, 2, ',', '.'),
|
||||
];
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,608 @@
|
||||
<?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\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioHistoricoPacienteService extends Service
|
||||
{
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao AS status_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'cadastro_paciente.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
]);
|
||||
|
||||
if (request()->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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()->status_id) {
|
||||
|
||||
$agendamentos->where(function($query) {
|
||||
|
||||
foreach (request()->status_id as $status) {
|
||||
|
||||
if ($status == StatusAgendamento::AGENDADO) {
|
||||
|
||||
$query
|
||||
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhereNull('status_id');
|
||||
} else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) {
|
||||
|
||||
$query
|
||||
->orWhereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
} else {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->get();
|
||||
|
||||
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' => [
|
||||
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
||||
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
||||
'paciente_nome_social' => $agendamento->paciente_nome_social,
|
||||
'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'paciente_prontuario' => $agendamento->paciente_prontuario,
|
||||
'paciente_nascimento' => $agendamento->paciente_nascimento,
|
||||
'data_agendamento' => $agendamento->data,
|
||||
'exame_nome' => mb_strtoupper($agendamento->exame_nome),
|
||||
'procedimento' => $agendamento->procedimento_codigo
|
||||
? $agendamento->procedimento_codigo . ' - ' . $agendamento->procedimento_nome
|
||||
: 'SEM PROCEDIMENTO',
|
||||
'status' => $agendamento->status_id ? self::getStatus($agendamento) : 'AGENDADO',
|
||||
'quantidade' => $agendamento->quantidade,
|
||||
'unidade' => $agendamento->nome_fantasia,
|
||||
'infraestrutura_nome' => $agendamento->infraestrutura_nome,
|
||||
]),
|
||||
'total' => self::getTotal()->quantidade,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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,
|
||||
]),
|
||||
'exames' => self::getExames()->map(fn ($exame) => [
|
||||
'id' => $exame->exame_id,
|
||||
'nome' => mb_strtoupper($exame->exame_nome),
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
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_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_laboratorial_agendamento.paciente_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->groupBy([
|
||||
'cadastro_infraestrutura.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()->paciente_id) {
|
||||
|
||||
$unidades->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
||||
}
|
||||
|
||||
if (request()->status_id) {
|
||||
|
||||
$unidades->where(function($query) {
|
||||
|
||||
foreach (request()->status_id as $status) {
|
||||
|
||||
if ($status == StatusAgendamento::AGENDADO) {
|
||||
|
||||
$query
|
||||
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhereNull('status_id');
|
||||
} else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) {
|
||||
|
||||
$query
|
||||
->orWhereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
} else {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $unidades->get();
|
||||
}
|
||||
|
||||
public static function getExames() {
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'parametrizacao_exame.id AS exame_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'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
return $exames->get();
|
||||
}
|
||||
|
||||
public static function getStatus($query) {
|
||||
|
||||
if ($query->status_id == StatusAgendamento::REALIZADO) {
|
||||
|
||||
return 'COLETA REALIZADA';
|
||||
} else if ($query->status_id == StatusAgendamento::PENDENTE_ASSINATURA
|
||||
|| $query->status_id == StatusAgendamento::ASSINADO
|
||||
|| $query->status_id == StatusAgendamento::PENDENTE_ENTREGA
|
||||
|| $query->status_id == StatusAgendamento::ENTREGUE) {
|
||||
|
||||
return 'RESULTADO LANÇADO';
|
||||
}
|
||||
|
||||
return $query->status_nome;
|
||||
}
|
||||
|
||||
public static function getTotal() {
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$total = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
])
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id');
|
||||
|
||||
if (request()->paciente_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
||||
}
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->status_id) {
|
||||
|
||||
$total->where(function($query) {
|
||||
|
||||
foreach (request()->status_id as $status) {
|
||||
|
||||
if ($status == StatusAgendamento::AGENDADO) {
|
||||
|
||||
$query
|
||||
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhereNull('status_id');
|
||||
} else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) {
|
||||
|
||||
$query
|
||||
->orWhereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
} else {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $total->first();
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('COUNT(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao AS status_nome',
|
||||
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'cadastro_paciente.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
]);
|
||||
|
||||
if (request()->paciente_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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()->status_id) {
|
||||
|
||||
$agendamentos->where(function($query) {
|
||||
|
||||
foreach (request()->status_id as $status) {
|
||||
|
||||
if ($status == StatusAgendamento::AGENDADO) {
|
||||
|
||||
$query
|
||||
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhereNull('status_id');
|
||||
} else if ($status == StatusAgendamento::PENDENTE_ASSINATURA) {
|
||||
|
||||
$query
|
||||
->orWhereIn('status_id',
|
||||
[
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE
|
||||
]
|
||||
);
|
||||
} else {
|
||||
|
||||
$query->orWhere('status_id', $status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_id ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS',
|
||||
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'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,
|
||||
],
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'status' => collect(request()->status_selecionado),
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
||||
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
||||
'paciente_nome_social' => $agendamento->paciente_nome_social,
|
||||
'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'paciente_prontuario' => $agendamento->paciente_prontuario,
|
||||
'paciente_nascimento' => $agendamento->paciente_nascimento,
|
||||
'data_agendamento' => $agendamento->data,
|
||||
'exame_nome' => mb_strtoupper($agendamento->exame_nome),
|
||||
'procedimento' => $agendamento->procedimento_codigo
|
||||
? $agendamento->procedimento_codigo . ' - ' . $agendamento->procedimento_nome
|
||||
: 'SEM PROCEDIMENTO',
|
||||
'status' => $agendamento->status_id ? self::getStatus($agendamento) : 'AGENDADO',
|
||||
'quantidade' => $agendamento->quantidade,
|
||||
'unidade' => $agendamento->nome_fantasia,
|
||||
'infraestrutura_nome' => $agendamento->infraestrutura_nome,
|
||||
]),
|
||||
'total' => self::getTotal()->quantidade,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,457 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioLancamentoColetaService extends Service
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) {
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia AS unidade_nome',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame) as sigla_exame'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
'),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_unidade_coleta.nome_fantasia,
|
||||
" - ",
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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')
|
||||
->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.id',
|
||||
'cadastro_grupo.id',
|
||||
]);
|
||||
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
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->status_id){
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_unidade_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_infraestrutura_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id && $request->pertence_grupo) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU)
|
||||
->orWhereNull('exame_laboratorial_agendamento_exame.status_id');
|
||||
});
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentosComGrupo = [];
|
||||
}
|
||||
|
||||
if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) {
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia AS unidade_nome',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame) as sigla_exame'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
'),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_unidade_coleta.nome_fantasia,
|
||||
" - ",
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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')
|
||||
->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.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function ($query) {
|
||||
|
||||
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
||||
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
||||
});
|
||||
|
||||
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->status_id){
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_unidade_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_infraestrutura_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id && $request->pertence_grupo) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU)
|
||||
->orWhereNull('exame_laboratorial_agendamento_exame.status_id');
|
||||
});
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentosSemMapaAgrupado = [];
|
||||
}
|
||||
|
||||
if (!$request->exame_id || $request->exame_id && !$request->pertence_grupo) {
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia AS unidade_nome',
|
||||
'cadastro_cidadao_medico.nome AS medico_nome',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
'parametrizacao_exame.sigla_exame AS sigla_exame',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('
|
||||
CASE WHEN cadastro_infraestrutura.principal THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura.nome
|
||||
END AS infraestrutura_nome
|
||||
'),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_unidade_coleta.nome_fantasia,
|
||||
" - ",
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_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')
|
||||
->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')
|
||||
->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->status_id){
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', $request->status_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_unidade_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
||||
}
|
||||
|
||||
if ($request->coleta_infraestrutura_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id && !$request->pertence_grupo) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', $request->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo->where(function($status) {
|
||||
|
||||
$status->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::DUPLICIDADE)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::OUTROS)
|
||||
->orWhere('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::FALTOU)
|
||||
->orWhereNull('exame_laboratorial_agendamento_exame.status_id');
|
||||
});
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
} else {
|
||||
|
||||
$agendamentosSemGrupo = [];
|
||||
}
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$perido_start = Carbon::create($request->periodo['start'])->format('d/m/Y');
|
||||
$perido_end = Carbon::create($request->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$perido_start = $currentDate;
|
||||
$perido_end = $currentDate;
|
||||
}
|
||||
|
||||
$agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo))
|
||||
->sortBy([
|
||||
['agendamento_id', 'asc'],
|
||||
['exame_grupo_nome', 'asc'],
|
||||
])
|
||||
->values();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => $request->paciente_nome ?? '-',
|
||||
'agendamento_numero' => $request->agendamento_numero ?? '-',
|
||||
'prontuario' => $request->prontuario ?? '-',
|
||||
'unidade_nome' => $request->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => $request->infraestrutura_nome ?? 'TODOS',
|
||||
'coleta_unidade_nome' => $request->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => $request->coleta_infraestrutura_nome ?? '',
|
||||
'exame_grupo' => $request->exame_id && $request->exame_nome ? $request->exame_nome : 'TODOS',
|
||||
'periodo' => [
|
||||
'start' => $perido_start,
|
||||
'end' => $perido_end,
|
||||
],
|
||||
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
||||
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
||||
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
||||
'agendado' => self::getTotal($agendamentos, StatusAgendamento::AGENDADO),
|
||||
'duplicidade' => self::getTotal($agendamentos, StatusAgendamento::DUPLICIDADE),
|
||||
'realizado' => self::getTotal($agendamentos, StatusAgendamento::REALIZADO),
|
||||
'outros' => self::getTotal($agendamentos, StatusAgendamento::OUTROS),
|
||||
'faltou' => self::getTotal($agendamentos, StatusAgendamento::FALTOU),
|
||||
'sistema' => [
|
||||
'ramo_atividade' => [
|
||||
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
|
||||
],
|
||||
'nome' => $cliente->sistema->nome ?? null,
|
||||
'versao' => $cliente->sistema->versao ?? null,
|
||||
],
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getTotal($agendamentos, $status_id) {
|
||||
|
||||
$total = 0;
|
||||
|
||||
foreach ($agendamentos as $agendamento) {
|
||||
|
||||
if ($agendamento->status_id == $status_id) {
|
||||
|
||||
$total += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
382
api/app/Services/ExameLaboratorial/RelatorioMapaDiaService.php
Normal file
382
api/app/Services/ExameLaboratorial/RelatorioMapaDiaService.php
Normal file
@@ -0,0 +1,382 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
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\Intranet\Cliente;
|
||||
use App\Models\Parametrizacao\Exame\ExameComposicao;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioMapaDiaService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
if (!request()->exame_id || request()->exame_id && request()->pertence_grupo && request()->mapa_agrupado) {
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_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'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
|
||||
])
|
||||
->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('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->groupBy([
|
||||
'cadastro_grupo.id',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
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()->unidade_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_grupo.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('cadastro_grupo.nome', 'ASC');
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentosComGrupo = [];
|
||||
}
|
||||
|
||||
if (!request()->exame_id || request()->exame_id && request()->pertence_grupo && !request()->mapa_agrupado) {
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_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'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
|
||||
])
|
||||
->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('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->groupBy([
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.mapa_agrupado', false);
|
||||
|
||||
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()->unidade_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('cadastro_grupo.nome', 'ASC');
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentosSemMapaAgrupado = [];
|
||||
}
|
||||
|
||||
if (!request()->exame_id || request()->exame_id && !request()->pertence_grupo) {
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d-%m-%Y") AS paciente_data_nascimento'),
|
||||
])
|
||||
->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('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->whereNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
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()->unidade_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
}
|
||||
|
||||
if (request()->exame_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('parametrizacao_exame.id', request()->exame_id);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
} else {
|
||||
|
||||
$agendamentosSemGrupo = [];
|
||||
}
|
||||
|
||||
$agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo))
|
||||
->sortBy([
|
||||
['agendamento_id', 'asc'],
|
||||
['exame_grupo_nome', 'asc'],
|
||||
])
|
||||
->values();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$perido_start = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$perido_end = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$perido_start = $currentDate;
|
||||
$perido_end = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => request()->paciente_nome ?? '-',
|
||||
'agendamento_numero' => request()->agendamento_numero ?? '-',
|
||||
'prontuario' => request()->prontuario ?? '-',
|
||||
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => request()->infraestrutura_nome ?? '',
|
||||
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'periodo' => [
|
||||
'start' => $perido_start,
|
||||
'end' => $perido_end,
|
||||
],
|
||||
'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' => $agendamentos->map(fn ($agendamento) => [
|
||||
'agendamento_id' => $agendamento->agendamento_id,
|
||||
'agendamento_numero' => $agendamento->agendamento_numero,
|
||||
'exame_grupo_id' => $agendamento->exame_grupo_id,
|
||||
'exame_grupo_nome' => $agendamento->exame_grupo_nome,
|
||||
'paciente_idade' => $agendamento->paciente_idade,
|
||||
'paciente_nome' => $agendamento->paciente_nome,
|
||||
'paciente_nome_social' => $agendamento->paciente_nome_social,
|
||||
'exames' => AgendamentoExame::where('agendamento_id', $agendamento->agendamento_id)
|
||||
->whereHas('exame', function($query) use ($agendamento) {
|
||||
|
||||
if (request()->pertence_grupo && request()->mapa_agrupado) {
|
||||
|
||||
$query->where('grupo_tipo_id', $agendamento->exame_grupo_id);
|
||||
} else if (request()->pertence_grupo && !request()->mapa_agrupado) {
|
||||
|
||||
$query->where('id', $agendamento->exame_grupo_id);
|
||||
} else {
|
||||
|
||||
$query->where('id', $agendamento->exame_grupo_id);
|
||||
}
|
||||
})
|
||||
->get()
|
||||
->map(function ($query) use ($agendamento) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'exame_id' => $query->exame_id,
|
||||
'nome' => mb_strtoupper($query->exame->nome),
|
||||
'composicao' => self::getComposicao($agendamento, $query->exame_id)
|
||||
];
|
||||
})
|
||||
->sortBy('nome')
|
||||
->values(),
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getComposicao($agendamento, $exameId)
|
||||
{
|
||||
|
||||
return ExameComposicao::where('exame_id', $exameId)
|
||||
->where('visivel', true)
|
||||
->whereIn('tipo_resultado_id', [ComposicaoTipo::NUMERICO, ComposicaoTipo::TEXTO, ComposicaoTipo::TEXTO_NUMERO, ComposicaoTipo::CAIXA_TEXTO])
|
||||
->where(function($query) use ($agendamento) {
|
||||
$query->where('sexo_id', $agendamento->paciente_sexo_id)
|
||||
->orWhereNull('sexo_id');
|
||||
})
|
||||
->where(function ($query) use ($agendamento) {
|
||||
$query->whereNull('tipo_idade_id')
|
||||
->orWhere(function ($idade) use ($agendamento) {
|
||||
|
||||
$idade
|
||||
->where(function ($idade) use ($agendamento) {
|
||||
|
||||
$idade
|
||||
->where('tipo_idade_id', MedidaTempo::DIAS)
|
||||
->where(
|
||||
'idade_inicial',
|
||||
'<=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInDays(Carbon::now()))
|
||||
->where(
|
||||
'idade_final',
|
||||
'>=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInDays(Carbon::now()));
|
||||
})
|
||||
->orWhere(function ($idade) use ($agendamento) {
|
||||
|
||||
$idade
|
||||
->where('tipo_idade_id', MedidaTempo::MESES)
|
||||
->where(
|
||||
'idade_inicial',
|
||||
'<=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInMonths(Carbon::now()))
|
||||
->where(
|
||||
'idade_final',
|
||||
'>=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInMonths(Carbon::now()));
|
||||
})
|
||||
->orWhere(function ($idade) use ($agendamento) {
|
||||
|
||||
$idade
|
||||
->where('tipo_idade_id', MedidaTempo::ANOS)
|
||||
->where(
|
||||
'idade_inicial',
|
||||
'<=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInYears(Carbon::now()))
|
||||
->where(
|
||||
'idade_final',
|
||||
'>=',
|
||||
Carbon::create($agendamento->paciente_data_nascimento)->diffInYears(Carbon::now()));
|
||||
});
|
||||
});
|
||||
})
|
||||
->orderBy('descricao')
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'descricao' => $query->descricao,
|
||||
'tipo_resultado_id' => $query->tipo_resultado_id,
|
||||
'resultado' => $query->resultado,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\Sexo;
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioMapaProducaoService extends Service
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.observacao AS observacao',
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
||||
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
||||
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame) as sigla_exame'),
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
DB::raw('CASE WHEN cadastro_cidadao.sexo_id = '. Sexo::MASCULINO .' THEN "M" ELSE "F" END AS paciente_sexo_descricao'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
||||
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
||||
DB::raw('CASE WHEN exame_laboratorial_agendamento_exame_status.descricao IS NOT NULL THEN exame_laboratorial_agendamento_exame_status.descricao ELSE "-" END AS status_descricao'),
|
||||
DB::raw('CONCAT(
|
||||
cadastro_unidade.nome_fantasia,
|
||||
" - ",
|
||||
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('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_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', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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.id',
|
||||
'cadastro_grupo.id',
|
||||
'parametrizacao_exame.id'
|
||||
]);
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
$agendamentos->where('cadastro_grupo.impressao_mapa_producao', true);
|
||||
$agendamentos->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
|
||||
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->unidade_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.id', $request->agendamento_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->exame_id) {
|
||||
|
||||
$agendamentos->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->boolean('impressao_pendente')) {
|
||||
|
||||
$agendamentos->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false);
|
||||
}
|
||||
|
||||
$agendamentos = $agendamentos->orderBy('cadastro_grupo.nome', 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy(DB::raw('ISNULL(parametrizacao_exame.posicao_mapa), parametrizacao_exame.posicao_mapa'), 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.posicao_mapa', 'ASC');
|
||||
$agendamentos = $agendamentos->orderBy('parametrizacao_exame.nome', 'ASC');
|
||||
$agendamentos = $agendamentos->get()->toArray();
|
||||
|
||||
$agendamentosSiglas = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
DB::raw('group_concat(parametrizacao_exame.sigla_exame ORDER BY parametrizacao_exame.sigla_exame ASC separator ", ") as sigla_exame'),
|
||||
])
|
||||
->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')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->unidade_id) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id);
|
||||
}
|
||||
|
||||
if ($request->infraestrutura_id) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id);
|
||||
}
|
||||
|
||||
if ($request->boolean('impressao_pendente')) {
|
||||
|
||||
$agendamentosSiglas->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false);
|
||||
}
|
||||
|
||||
$agendamentosSiglas = $agendamentosSiglas->get()->toArray();
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$perido_start = Carbon::create($request->periodo['start'])->format('d/m/Y');
|
||||
$perido_end = Carbon::create($request->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$perido_start = $currentDate;
|
||||
$perido_end = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'paciente_nome' => $request->paciente_nome ?? '-',
|
||||
'agendamento_numero' => $request->agendamento_numero ?? '-',
|
||||
'prontuario' => $request->prontuario ?? '-',
|
||||
'unidade_nome' => $request->unidade_nome ?? 'TODAS',
|
||||
'infraestrutura_nome' => $request->infraestrutura_nome ?? '',
|
||||
'exame_grupo' => $request->exame_id && $request->exame_nome ? $request->exame_nome : 'TODOS',
|
||||
'periodo' => [
|
||||
'start' => $perido_start,
|
||||
'end' => $perido_end,
|
||||
],
|
||||
'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' => $agendamentos,
|
||||
'agendamentos_siglas' => $agendamentosSiglas,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioPacienteAtendidoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$totalizadores = (object) ['quantidade' => 0];
|
||||
|
||||
$date = request()->query('periodo');
|
||||
|
||||
if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $date['start'])) {
|
||||
$periodo['start'] = Carbon::createFromFormat('d/m/Y', $date['start'])->format('Y-m-d');
|
||||
$periodo['end'] = Carbon::createFromFormat('d/m/Y', $date['end'])->format('Y-m-d');
|
||||
} else {
|
||||
$periodo['start'] = $date['start'];
|
||||
$periodo['end'] = $date['end'];
|
||||
}
|
||||
|
||||
$consulta = DB::table('exame_laboratorial_agendamento')
|
||||
->join(
|
||||
'exame_laboratorial_agendamento_exame',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'exame_laboratorial_agendamento.unidade_id',
|
||||
'cadastro_unidade.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_paciente',
|
||||
'exame_laboratorial_agendamento_exame.paciente_id',
|
||||
'cadastro_paciente.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_paciente.cidadao_id',
|
||||
'cadastro_cidadao.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'exame_laboratorial_agendamento.medico_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cadastro_cidadao_1',
|
||||
'cadastro_cidadao_1.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'exame_laboratorial_agendamento_exame_status',
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
'exame_laboratorial_agendamento_exame_status.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
|
||||
'cadastro_infraestrutura.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as cadastro_unidade_1',
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
'cadastro_unidade_1.id'
|
||||
)
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
]
|
||||
)
|
||||
->when(request()->query('unidade_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.unidade_id', $id))
|
||||
->when(request()->query('infraestrutura_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->query('medico_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.medico_id', $id))
|
||||
->select(
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'exame_laboratorial_agendamento.data',
|
||||
DB::raw('cadastro_cidadao.nome AS paciente'),
|
||||
DB::raw('cadastro_cidadao_1.nome AS medico'),
|
||||
DB::raw('cadastro_unidade.nome_fantasia AS unidade'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta,"%d/%m/%Y") AS data_coleta'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data,"%d/%m/%Y") AS data_pedido'),
|
||||
DB::raw("IFNULL(cadastro_infraestrutura.nome, cadastro_unidade_1.nome_fantasia) AS local")
|
||||
);
|
||||
|
||||
if(isset($periodo)){
|
||||
$consulta = $consulta->whereBetween('exame_laboratorial_agendamento.data', [$periodo['start'], $periodo['end']]);
|
||||
}
|
||||
|
||||
$consulta = $consulta->groupBy('exame_laboratorial_agendamento.id')
|
||||
->orderBy('exame_laboratorial_agendamento.id')
|
||||
->get();
|
||||
|
||||
$totalizadores = [
|
||||
'total' => $consulta->count(),
|
||||
];
|
||||
|
||||
return ['consulta' => $consulta, 'totalizadores' => $totalizadores];
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
|
||||
$unidades = self::getUnidades();
|
||||
|
||||
return [
|
||||
'additionals' => [],
|
||||
'form' => [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
'periodo' => [
|
||||
'start' => Carbon::now()->startOfMonth()->format('Y-m-d'),
|
||||
'end' => Carbon::now()->endOfMonth()->format('Y-m-d'),
|
||||
],
|
||||
'unidades' => Unidade::orderBy('nome_fantasia')
|
||||
->whereIn('id', $unidades->pluck('unidade_id'))
|
||||
->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()
|
||||
]),
|
||||
'medicos' => self::getMedicos()->map(fn ($medicos) => [
|
||||
'medico_id' => $medicos->laboratorio_unidade_id,
|
||||
'medico_nome' => $medicos->usuario,
|
||||
])->values(),
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'unidade_id' => null,
|
||||
'infraestrutura_id' => null,
|
||||
'medico_id' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$medicos = self::getMedicos();
|
||||
|
||||
$consulta = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_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(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
]);
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$consulta->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
||||
$consulta->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
||||
} else {
|
||||
|
||||
$consulta->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$consulta->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
}
|
||||
|
||||
if (request()->medico_id) {
|
||||
|
||||
$consulta->where('exame_laboratorial_agendamento.medico_id', request()->medico_id);
|
||||
}
|
||||
|
||||
$consulta = $consulta->get();
|
||||
|
||||
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,
|
||||
])->values(),
|
||||
'medicos' => self::getMedicos()->map(fn ($medicos) => [
|
||||
'medico_id' => $medicos->laboratorio_unidade_id,
|
||||
'medico_nome' => $medicos->nome,
|
||||
])->values(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getUnidades()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$unidade = DB::table('exame_laboratorial_agendamento')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.unidade_id',
|
||||
'cadastro_unidade.nome_fantasia AS unidade',
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_agendamento_exame',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'exame_laboratorial_agendamento.unidade_id'
|
||||
)
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $data);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.unidade_id',
|
||||
]);
|
||||
|
||||
return $unidade->get();
|
||||
}
|
||||
|
||||
private static function getMedicos()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$medico = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.medico_id AS laboratorio_unidade_id',
|
||||
'cadastro_cidadao.nome AS usuario',
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_agendamento',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'exame_laboratorial_agendamento.medico_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->when(request()->unidade_id, function ($query) {
|
||||
$query->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
||||
})
|
||||
->when(request()->infraestrutura_id, function ($query) {
|
||||
$query->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
||||
})
|
||||
->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO)
|
||||
->where(function($query) use ($currentDate) {
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d');
|
||||
$query->where('exame_laboratorial_agendamento.data', '>=', $data);
|
||||
|
||||
$data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d');
|
||||
$query->where('exame_laboratorial_agendamento.data', '<=', $data);
|
||||
} else {
|
||||
|
||||
$query->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
})
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.medico_id',
|
||||
])
|
||||
->orderBy('cadastro_cidadao.nome', 'asc');
|
||||
|
||||
return $medico->get();
|
||||
}
|
||||
|
||||
public static function getPrintExportData()
|
||||
{
|
||||
$resultado = self::index();
|
||||
$itens = $resultado['consulta'];
|
||||
|
||||
$unidade = Unidade::find(request()->unidade_id);
|
||||
$medico = Usuario::find(request()->medico_id);
|
||||
$infraestrutura = Infraestrutura::select([
|
||||
DB::raw('IFNULL(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS local')
|
||||
])
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
'cadastro_unidade.id'
|
||||
)
|
||||
->where('cadastro_infraestrutura.id', request()->infraestrutura_id)
|
||||
->first();
|
||||
|
||||
return [
|
||||
'inicio' => request()->periodo['start'],
|
||||
'fim' => request()->periodo['end'],
|
||||
'unidade' => $unidade->nome_fantasia ?? 'Todas',
|
||||
'medico' => $medico->nome ?? 'Todos',
|
||||
'local' => $infraestrutura->local ?? 'Todos',
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'itens' => $itens,
|
||||
'totalizadores' => $resultado['totalizadores'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,721 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
||||
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioQuantitativoExameSolicitadoService extends Service
|
||||
{
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$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',
|
||||
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.unidade_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->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);
|
||||
}
|
||||
|
||||
$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,
|
||||
],
|
||||
'model' => [
|
||||
'unidade_coleta' => $agendamentos->map(
|
||||
fn ($coleta) => [
|
||||
'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $coleta->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $coleta->laboratorio_infraestrutura_nome,
|
||||
'exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd, $coleta) {
|
||||
|
||||
$query
|
||||
->where('data', '>=', $periodoStart)
|
||||
->where('data', '<=', $periodoEnd)
|
||||
->where('coleta_infraestrutura_id', $coleta->coleta_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);
|
||||
}
|
||||
})
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy('exame_id')
|
||||
->select([
|
||||
'parametrizacao_exame.id',
|
||||
'parametrizacao_exame.nome',
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
])
|
||||
->get(),
|
||||
'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);
|
||||
|
||||
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);
|
||||
}
|
||||
})
|
||||
->count(),
|
||||
]
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
])
|
||||
->join(
|
||||
'exame_laboratorial_agendamento',
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
]);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$exames->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
$exames = $exames->get();
|
||||
|
||||
$unidades = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'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',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_laboratorial_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_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.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);
|
||||
}
|
||||
|
||||
$unidades = $unidades->get();
|
||||
|
||||
$laboratorios = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'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.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);
|
||||
}
|
||||
|
||||
$laboratorios = $laboratorios->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'unidades' => $unidades->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,
|
||||
]),
|
||||
'laboratorios' => $laboratorios->map(fn ($laboratorio) => [
|
||||
'laboratorio_id' => $laboratorio->laboratorio_unidade_id,
|
||||
'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome,
|
||||
'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id,
|
||||
'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome,
|
||||
]),
|
||||
'exames' => $exames->map(fn ($exame) => [
|
||||
'exames' => AgendamentoExame::where('agendamento_id', $exame->agendamento_id)
|
||||
->get()
|
||||
->map(function ($query) {
|
||||
return [
|
||||
'id' => $query->exame_id,
|
||||
'nome' => mb_strtoupper($query->exame->nome),
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.unidade_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()->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);
|
||||
}
|
||||
|
||||
$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');
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStart = $currentDate;
|
||||
$periodoEnd = $currentDate;
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'unidade_coleta' => $agendamentos->map(
|
||||
fn ($coleta) => [
|
||||
'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $coleta->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $coleta->laboratorio_infraestrutura_nome,
|
||||
'exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd, $coleta) {
|
||||
|
||||
$query
|
||||
->where('data', '>=', $periodoStart)
|
||||
->where('data', '<=', $periodoEnd)
|
||||
->where('coleta_infraestrutura_id', $coleta->coleta_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);
|
||||
}
|
||||
})
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->groupBy('exame_id')
|
||||
->select([
|
||||
'parametrizacao_exame.id',
|
||||
'parametrizacao_exame.nome',
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
||||
])
|
||||
->get(),
|
||||
'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);
|
||||
|
||||
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);
|
||||
}
|
||||
})->count(),
|
||||
]
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,760 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioQuantitativoProducaoService extends Service
|
||||
{
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'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,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
$inicio = request()->periodo['start'] ?? $now->format('Y-m-d');
|
||||
$fim = request()->periodo['end'] ?? $now->format('Y-m-d');
|
||||
$periodo = [$inicio, $fim];
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'procedimento.codigo as procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao as status',
|
||||
'cadastro_cidadao.nome as profissional',
|
||||
'ocupacao.codigo as ocupacao_codigo',
|
||||
'ocupacao.nome as ocupacao_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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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')
|
||||
->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')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame_status.id', 'exame_laboratorial_agendamento_exame.status_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao as cidadao_paciente', 'cidadao_paciente.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id')
|
||||
->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('cadastro_unidade_coleta.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura_coleta.id', $id))
|
||||
->when(request()->laboratorio_id, fn ($q, $id) => $q->where('cadastro_unidade_laboratorio.id', $id))
|
||||
->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura_laboratorio.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id))
|
||||
->when(request()->status, fn ($q, $id) => $q->where('exame_laboratorial_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->when(request()->idade_de, fn ($q, $idadeDe) => $q
|
||||
->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cidadao_paciente.data_nascimento, CURRENT_DATE) >= " . $idadeDe))
|
||||
)
|
||||
->when(request()->idade_ate, fn ($q, $idadeAte) => $q
|
||||
->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cidadao_paciente.data_nascimento, CURRENT_DATE) <= " . $idadeAte))
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id',
|
||||
'cadastro_lotacao.usuario_id',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao',
|
||||
]);
|
||||
|
||||
self::setOrderBy($agendamentos);
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
$resultadoAgrupado = [];
|
||||
|
||||
foreach ($agendamentos as $item) {
|
||||
$unidadeId = $item->coleta_infraestrutura_id;
|
||||
|
||||
if (!isset($resultadoAgrupado[$unidadeId])) {
|
||||
$resultadoAgrupado[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'coleta_nome_fantasia' => $item->coleta_nome_fantasia,
|
||||
'coleta_infraestrutura_nome' => $item->coleta_infraestrutura_nome,
|
||||
'laboratorio_nome_fantasia' => $item->laboratorio_nome_fantasia,
|
||||
'laboratorio_infraestrutura_nome' => $item->laboratorio_infraestrutura_nome,
|
||||
'exames' => [],
|
||||
'total' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$resultadoAgrupado[$unidadeId]['total'] = $resultadoAgrupado[$unidadeId]['total'] + $item->quantidade;
|
||||
|
||||
$resultadoAgrupado[$unidadeId]['exames'][] = [
|
||||
'id' => $item->exame_id,
|
||||
'nome' => $item->exame_nome,
|
||||
'procedimento_codigo' => $item->procedimento_codigo ?? '-',
|
||||
'procedimento_nome' => $item->procedimento_nome ?? '-',
|
||||
'status' => $item->status,
|
||||
'profissional' => $item->profissional ?? '-',
|
||||
'quantidade' => $item->quantidade,
|
||||
'ocupacao_nome' => $item->ocupacao_nome ?? '-',
|
||||
'ocupacao_codigo' => $item->ocupacao_codigo ?? '-',
|
||||
];
|
||||
}
|
||||
|
||||
usort($resultadoAgrupado, function ($a, $b) {
|
||||
return strcmp($a['coleta_nome_fantasia'], $b['coleta_nome_fantasia']);
|
||||
});
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'unidade_coleta' => $resultadoAgrupado,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function loadFields()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$inicio = request()->periodo['start'] ?? $now->format('Y-m-d');
|
||||
$fim = request()->periodo['end'] ?? $now->format('Y-m-d');
|
||||
$periodo = [$inicio, $fim];
|
||||
|
||||
return [
|
||||
'local_coleta' => self::getLocalColeta($periodo),
|
||||
'laboratorios' => self::getLaboratorio($periodo),
|
||||
'exames' => self::getExames($periodo),
|
||||
'status' => self::getStatus($periodo),
|
||||
'ocupacoes' => self::getOcupacao($periodo),
|
||||
'profissionais' => self::getProfissional($periodo),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getStatus($periodo)
|
||||
{
|
||||
return DB::table('exame_laboratorial_agendamento_exame_status')
|
||||
->select(
|
||||
'exame_laboratorial_agendamento_exame_status.id',
|
||||
'exame_laboratorial_agendamento_exame_status.descricao',
|
||||
)
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.status_id', 'exame_laboratorial_agendamento_exame_status.id')
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
||||
->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id')
|
||||
->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id')
|
||||
->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
|
||||
->join('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id))
|
||||
->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id))
|
||||
->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('sigtap.procedimento.codigo', $id))
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('id','descricao')
|
||||
->orderBy('descricao')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function autocompleteProcedimento()
|
||||
{
|
||||
|
||||
$now = Carbon::now();
|
||||
|
||||
$inicio = request()->periodo['start'] ?? $now->format('Y-m-d');
|
||||
$fim = request()->periodo['end'] ?? $now->format('Y-m-d');
|
||||
$periodo = [$inicio, $fim];
|
||||
|
||||
return DB::table('parametrizacao_exame')
|
||||
->select(
|
||||
'sigtap.procedimento.codigo as codigo',
|
||||
'sigtap.procedimento.nome as nome',
|
||||
)
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
||||
->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id')
|
||||
->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id')
|
||||
->leftJoin('cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento.lotacao_id')
|
||||
->leftJoin('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->join('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id))
|
||||
->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id))
|
||||
->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->when(request()->profissional_id, fn ($q, $id) => $q->where('cadastro_usuario.id', $id))
|
||||
->distinct('codigo','nome')
|
||||
->orderBy('nome');
|
||||
}
|
||||
|
||||
private static function getOcupacao($periodo)
|
||||
{
|
||||
return DB::table('cadastro_lotacao')
|
||||
->select(
|
||||
'ocupacao.id',
|
||||
'ocupacao.nome',
|
||||
)
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.lotacao_id', 'cadastro_lotacao.id')
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
||||
->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id')
|
||||
->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id))
|
||||
->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id))
|
||||
->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id))
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getProfissional($periodo)
|
||||
{
|
||||
return DB::table('cadastro_usuario')
|
||||
->select(
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome',
|
||||
)
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.usuario_id', 'cadastro_usuario.id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->join('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('cadastro_infraestrutura as laboratorio_infraestrutura', 'laboratorio_infraestrutura.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
||||
->join('cadastro_unidade as laboratorio_unidade', 'laboratorio_unidade.id', 'laboratorio_infraestrutura.unidade_id')
|
||||
->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id))
|
||||
->when(request()->laboratorio_id, fn ($q, $id) => $q->where('laboratorio_unidade.id', $id))
|
||||
->when(request()->laboratorio_infraestrutura_id, fn ($q, $id) => $q->where('laboratorio_infraestrutura.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getExames($periodo)
|
||||
{
|
||||
return DB::table('parametrizacao_exame')
|
||||
->select(
|
||||
'parametrizacao_exame.id',
|
||||
'parametrizacao_exame.nome',
|
||||
)
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('id','nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getLaboratorio($periodo)
|
||||
{
|
||||
|
||||
$unidade = DB::table('cadastro_infraestrutura')
|
||||
->select([
|
||||
'cadastro_unidade.id AS unidade_id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
])
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->join('cadastro_infraestrutura as coleta_infraestrutura', 'coleta_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
||||
->join('cadastro_unidade as coleta_unidade', 'coleta_unidade.id', 'coleta_infraestrutura.unidade_id')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->coleta_unidade_id, fn ($q, $id) => $q->where('coleta_unidade.id', $id))
|
||||
->when(request()->coleta_infraestrutura_id, fn ($q, $id) => $q->where('coleta_infraestrutura.id', $id))
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('infraestrutura_id')
|
||||
->orderBy('nome_fantasia')
|
||||
->get();
|
||||
|
||||
$resultadoAgrupado = [];
|
||||
|
||||
foreach ($unidade as $item) {
|
||||
$unidadeId = $item->unidade_id;
|
||||
$infraestruturaId = $item->infraestrutura_id;
|
||||
$nomeInfraestrutura = $item->nome_infraestrutura;
|
||||
|
||||
if (!isset($resultadoAgrupado[$unidadeId])) {
|
||||
$resultadoAgrupado[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'nome_fantasia' => $item->nome_fantasia,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$resultadoAgrupado[$unidadeId]['infraestruturas'][] = [
|
||||
'id' => $infraestruturaId,
|
||||
'nome' => $nomeInfraestrutura,
|
||||
'principal' => $item->principal
|
||||
];
|
||||
}
|
||||
|
||||
usort($resultadoAgrupado, function ($a, $b) {
|
||||
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
|
||||
});
|
||||
|
||||
return $resultadoAgrupado;
|
||||
}
|
||||
|
||||
private static function getLocalColeta($periodo)
|
||||
{
|
||||
|
||||
$unidade = DB::table('cadastro_infraestrutura')
|
||||
->select([
|
||||
'cadastro_unidade.id AS unidade_id',
|
||||
'cadastro_infraestrutura.id AS infraestrutura_id',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
])
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->join('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->whereBetween('exame_laboratorial_agendamento.data', $periodo)
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
})
|
||||
->distinct('infraestrutura_id')
|
||||
->orderBy('nome_fantasia')
|
||||
->get();
|
||||
|
||||
$resultadoAgrupado = [];
|
||||
|
||||
foreach ($unidade as $item) {
|
||||
$unidadeId = $item->unidade_id;
|
||||
$infraestruturaId = $item->infraestrutura_id;
|
||||
$nomeInfraestrutura = $item->nome_infraestrutura;
|
||||
|
||||
if (!isset($resultadoAgrupado[$unidadeId])) {
|
||||
$resultadoAgrupado[$unidadeId] = [
|
||||
'id' => $unidadeId,
|
||||
'nome_fantasia' => $item->nome_fantasia,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$resultadoAgrupado[$unidadeId]['infraestruturas'][] = [
|
||||
'id' => $infraestruturaId,
|
||||
'nome' => $nomeInfraestrutura,
|
||||
'principal' => $item->principal
|
||||
];
|
||||
}
|
||||
|
||||
usort($resultadoAgrupado, function ($a, $b) {
|
||||
return strcmp($a['nome_fantasia'], $b['nome_fantasia']);
|
||||
});
|
||||
|
||||
return $resultadoAgrupado;
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
||||
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
||||
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
||||
'parametrizacao_exame.codigo AS procedimento_codigo',
|
||||
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
||||
|
||||
'exame_laboratorial_agendamento_exame.agendamento_id',
|
||||
'exame_laboratorial_agendamento_exame.exame_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'),
|
||||
DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'),
|
||||
DB::raw(
|
||||
'
|
||||
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
||||
cadastro_unidade_laboratorio.nome_fantasia
|
||||
ELSE
|
||||
cadastro_infraestrutura_laboratorio.nome
|
||||
END AS laboratorio_infraestrutura_nome'
|
||||
),
|
||||
DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"),
|
||||
])
|
||||
->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'
|
||||
)
|
||||
->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'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_laboratorial_agendamento_exame.exame_id'
|
||||
)
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.unidade_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()->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);
|
||||
}
|
||||
|
||||
$agendamentos->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereIn(
|
||||
'exame_laboratorial_agendamento_exame.status_id',
|
||||
[
|
||||
StatusAgendamento::REALIZADO,
|
||||
StatusAgendamento::PENDENTE_ASSINATURA,
|
||||
StatusAgendamento::ASSINADO,
|
||||
StatusAgendamento::PENDENTE_ENTREGA,
|
||||
StatusAgendamento::ENTREGUE,
|
||||
StatusAgendamento::NOVA_COLETA
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
$agendamentos = $agendamentos->get();
|
||||
|
||||
if (request()->periodo) {
|
||||
|
||||
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
||||
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
||||
} else {
|
||||
|
||||
$periodoStartFilter = $currentDate;
|
||||
$periodoEndFilter = $currentDate;
|
||||
}
|
||||
|
||||
return [
|
||||
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
||||
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
||||
'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS',
|
||||
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
||||
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
||||
'ocupacao' => request()->ocupacao_nome ?? 'TODAS',
|
||||
'profissional' => request()->profissional_nome ?? 'TODOS',
|
||||
'status' => request()->status_nome ?? 'TODOS',
|
||||
'procedimento' => request()->procedimento_codigo ?? 'TODOS',
|
||||
'idade_de' => request()->idade_de ?? '-',
|
||||
'idade_ate' => request()->idade_ate ?? '-',
|
||||
'periodo' => [
|
||||
'start' => $periodoStartFilter,
|
||||
'end' => $periodoEndFilter,
|
||||
],
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'unidade_coleta' => self::list()['model']['unidade_coleta']
|
||||
];
|
||||
}
|
||||
|
||||
private static function setOrderBy($builder)
|
||||
{
|
||||
$columns = [
|
||||
'nome' => 'exame_nome',
|
||||
'procedimento_codigo' => 'procedimento.codigo',
|
||||
'procedimento_nome' => 'procedimento.nome',
|
||||
'status' => 'exame_laboratorial_agendamento_exame_status.descricao',
|
||||
'profissional' => 'cadastro_cidadao.nome',
|
||||
'ocupacao_codigo' => 'ocupacao.codigo',
|
||||
'ocupacao_nome' => 'ocupacao.nome',
|
||||
'quantidade' => DB::raw("COUNT(parametrizacao_exame.id)"),
|
||||
];
|
||||
|
||||
if (is_null(request()->sortBy)) {
|
||||
|
||||
$builder->orderByRaw(join(', ', $columns));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$column = $columns[request()->sortBy];
|
||||
$direction = DataUtil::getBooleanValueFromStringBoolean(request()->sortDesc) ? 'DESC' : 'ASC';
|
||||
|
||||
unset($columns[request()->sortBy]);
|
||||
|
||||
$builder->orderByRaw(
|
||||
"{$column} {$direction}, " . join(', ', $columns)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user