This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user