This commit is contained in:
838
api/app/Services/ExameImagem/RelatorioProducaoService.php
Normal file
838
api/app/Services/ExameImagem/RelatorioProducaoService.php
Normal file
@@ -0,0 +1,838 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameImagem;
|
||||
|
||||
use App\Enums\ExameImagem\Fechamento\Status;
|
||||
use App\Enums\Parametrizacao\Exame\Tipo;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
||||
use App\Models\ExameImagem\Fechamento\StatusFechamento;
|
||||
use App\Models\Parametrizacao\Exame\Exame;
|
||||
use App\Models\Sigtap\Ocupacao;
|
||||
use App\Models\Sigtap\Procedimento;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioProducaoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
|
||||
$consulta = self::getAgendamentosBuilder();
|
||||
|
||||
if (!$consulta) {
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
$consulta = $consulta
|
||||
->select(
|
||||
'exame_imagem_agendamento_exame.id AS atendimento_id',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'exame_imagem_agendamento.unidade_id',
|
||||
'exame_imagem_agendamento.unidade_solicitante_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'parametrizacao_exame.codigo AS procedimento',
|
||||
'sigtap.procedimento.nome AS nome_procedimento',
|
||||
'sigtap.procedimento.id AS procedimento_id',
|
||||
'cadastro_grupo.nome AS grupo',
|
||||
'cadastro_grupo.id AS grupo_exame_id',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
'cadastro_unidade_solicitante.nome_fantasia AS unidade_solicitante_nome',
|
||||
'exame_imagem_jornada.id AS jornada_id',
|
||||
'exame_imagem_jornada.status_id AS jornada_status_id',
|
||||
'exame_imagem_jornada.prestador_id',
|
||||
'exame_imagem_parametrizacao_unidade.id AS parametrizacao',
|
||||
'exame_imagem_parametrizacao_unidade.unidade_id AS parametrizacao_unidade_id',
|
||||
'cadastro_unidade_parametrizacao.nome_fantasia AS nome_prestador',
|
||||
'ocupacao.nome AS ocupacao_nome',
|
||||
'ocupacao.codigo AS ocupacao_codigo',
|
||||
'cadastro_cidadao.nome as profissional',
|
||||
DB::raw('count(exame_imagem_agendamento_exame.id) AS quantidade'),
|
||||
DB::raw("DATE_FORMAT(exame_imagem_agendamento.data, '%d/%m/%Y') AS data"),
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura_solicitante.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura_solicitante.nome
|
||||
ELSE
|
||||
cadastro_unidade_solicitante.nome_fantasia
|
||||
END AS infraestrutura_solicitante_nome
|
||||
"),
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS infraestrutura_nome
|
||||
"),
|
||||
DB::raw('CASE WHEN exame_imagem_agendamento_exame.status_id IS NULL THEN
|
||||
"AGENDADO"
|
||||
ELSE
|
||||
exame_imagem_fechamento_status.nome
|
||||
END AS status_nome'),
|
||||
);
|
||||
|
||||
self::setOrderBy($consulta);
|
||||
|
||||
return $consulta
|
||||
->groupBy([
|
||||
'exame_imagem_agendamento.unidade_id',
|
||||
'exame_imagem_agendamento.unidade_solicitante_id',
|
||||
'exame_imagem_agendamento_exame.exame_id',
|
||||
])
|
||||
->get()
|
||||
->map(function ($relatorio) {
|
||||
|
||||
return [
|
||||
'data' => $relatorio->data,
|
||||
'exame_nome' => $relatorio->exame_nome,
|
||||
'exame_id' => $relatorio->exame_id,
|
||||
'procedimento' => $relatorio->procedimento,
|
||||
'nome_procedimento' => $relatorio->nome_procedimento,
|
||||
'status_nome' => $relatorio->status_nome,
|
||||
'unidade_solicitante_nome' => $relatorio->unidade_solicitante_nome,
|
||||
'nome_fantasia' => $relatorio->nome_fantasia,
|
||||
'infraestrutura_nome' => $relatorio->infraestrutura_nome,
|
||||
'unidade_id' => $relatorio->unidade_id,
|
||||
'unidade_solicitante_id' => $relatorio->unidade_solicitante_id,
|
||||
'infraestrutura_solicitante_nome' => $relatorio->infraestrutura_solicitante_nome,
|
||||
'quantidade' => $relatorio->quantidade,
|
||||
'nome_prestador' => $relatorio->nome_prestador,
|
||||
'ocupacao_nome' => $relatorio->ocupacao_nome,
|
||||
'ocupacao_codigo' => $relatorio->ocupacao_codigo,
|
||||
'profissional' => $relatorio->profissional,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function getAgendamentosBuilder()
|
||||
{
|
||||
|
||||
$inicio = request()->periodo['start'];
|
||||
$fim = request()->periodo['end'];
|
||||
$periodo = [$inicio, $fim];
|
||||
|
||||
$agendamentos = DB::table('exame_imagem_agendamento_exame')
|
||||
->join(
|
||||
'exame_imagem_agendamento',
|
||||
'exame_imagem_agendamento.id',
|
||||
'exame_imagem_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'exame_imagem_fechamento_status',
|
||||
'exame_imagem_fechamento_status.id',
|
||||
'exame_imagem_agendamento_exame.status_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_imagem_agendamento_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_grupo',
|
||||
'cadastro_grupo.id',
|
||||
'exame_imagem_agendamento.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_imagem_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura AS cadastro_infraestrutura_solicitante',
|
||||
'cadastro_infraestrutura_solicitante.id',
|
||||
'exame_imagem_agendamento.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade AS cadastro_unidade_solicitante',
|
||||
'cadastro_unidade_solicitante.id',
|
||||
'cadastro_infraestrutura_solicitante.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'exame_imagem_jornada',
|
||||
'exame_imagem_jornada.id',
|
||||
'exame_imagem_agendamento.jornada_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'exame_imagem_parametrizacao_unidade',
|
||||
'exame_imagem_parametrizacao_unidade.id',
|
||||
'exame_imagem_jornada.prestador_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade AS cadastro_unidade_parametrizacao',
|
||||
'cadastro_unidade_parametrizacao.id',
|
||||
'exame_imagem_parametrizacao_unidade.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);
|
||||
});
|
||||
})
|
||||
->leftJoin(
|
||||
'parametrizacao_procedimento',
|
||||
'parametrizacao_procedimento.procedimento_codigo',
|
||||
'parametrizacao_exame.codigo'
|
||||
)
|
||||
->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.profissional_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->join('sigtap.ocupacao AS ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('exame_imagem_parametrizacao_ocupacao', 'exame_imagem_parametrizacao_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_imagem_agendamento.paciente_id')
|
||||
->join('cadastro_cidadao as cidadao_paciente', 'cidadao_paciente.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->when(request()->profissional_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.profissional_id', $id))
|
||||
->when(request()->prestador_id, fn ($q, $id) => $q->where('cadastro_unidade_parametrizacao.id', $id));
|
||||
|
||||
return $agendamentos;
|
||||
}
|
||||
|
||||
public static function getPrintExportData()
|
||||
{
|
||||
$unidadeSolicitante = Unidade::find(request()->unidade_solicitante_id);
|
||||
$infraestruturaSolicitante = Infraestrutura::find(request()->infraestrutura_solicitante_id);
|
||||
$unidadeExecutante = Unidade::find(request()->unidade_id);
|
||||
$infraestruturaExecutante = Infraestrutura::find(request()->infraestrutura_id);
|
||||
$exame = Exame::find(request()->exame_id);
|
||||
$status = StatusFechamento::find(request()->status_id);
|
||||
$ocupacao = Ocupacao::find(request()->ocupacao_id);
|
||||
$profissional = Usuario::find(request()->profissional_id);
|
||||
$prestador = Unidade::find(request()->prestador_id);
|
||||
$procedimento = Procedimento::where('codigo', request()->procedimento_codigo)->first();
|
||||
|
||||
return [
|
||||
'inicio' => Carbon::createFromFormat('Y-m-d', request()->periodo['start'])->format('d/m/Y'),
|
||||
'fim' => Carbon::createFromFormat('Y-m-d', request()->periodo['end'])->format('d/m/Y'),
|
||||
'unidadeSolicitante' => $unidadeSolicitante ? $unidadeSolicitante->nome_fantasia : 'Todas',
|
||||
'unidadeExecutante' => $unidadeExecutante ? $unidadeExecutante->nome_fantasia : 'Todas',
|
||||
'infraestrutura_executante' => $infraestruturaExecutante ? $infraestruturaExecutante->nome : 'Todos',
|
||||
'infraestrutura_solicitante' => $infraestruturaSolicitante ? $infraestruturaSolicitante->nome : 'Todos',
|
||||
'profissional' => $profissional ? $profissional->cidadao->nome : 'Todos',
|
||||
'prestador' => $prestador ? $prestador->nome_fantasia : 'Todos',
|
||||
'exame' => $exame ? $exame->nome : 'Todos',
|
||||
'status' => $status ? $status->nome : 'Todos',
|
||||
'ocupacao' => $ocupacao ? $ocupacao->nome : 'Todas',
|
||||
'procedimento' => $procedimento ? $procedimento->codigo : 'Todos',
|
||||
'procedimento_nome' => $procedimento ? $procedimento->nome : 'Todos',
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
'totalExames' => collect(request()->totalExames),
|
||||
];
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
$exames = self::getExamesBuilder()
|
||||
->when(request()->query('periodo'), fn ($q, $periodo) => $q->whereBetween('exame_imagem_agendamento.data', [
|
||||
Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->setTime(0, 0, 0),
|
||||
Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->setTime(23, 59, 59)
|
||||
]))
|
||||
->when(request()->unidade_executante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->prestador_id, fn ($q, $id) => $q->where('exame_imagem_parametrizacao_unidade.prestador', $id))
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->procedimento, fn ($q, $codigo) => $q->where('parametrizacao_exame.codigo', $codigo));
|
||||
|
||||
return $exames;
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('d/m/Y');
|
||||
|
||||
return [
|
||||
'additionals' => [
|
||||
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
||||
'current_date' => $now->format('d/m/Y')
|
||||
],
|
||||
'form' => self::getForms(),
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => request()->periodo['start'] ?? $currentDate,
|
||||
'end' => request()->periodo['end'] ?? $currentDate,
|
||||
],
|
||||
'exame_id' => null,
|
||||
'procedimento' => null,
|
||||
'prestador_id' => null,
|
||||
'profissional_id' => null,
|
||||
'unidade_solicitante_id' => null,
|
||||
'infraestrutura_solicitante_id' => null,
|
||||
'unidade_id' => null,
|
||||
'infraestrutura_id' => null,
|
||||
'status_id' => null,
|
||||
'ocupacao_id' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function setOrderBy($builder)
|
||||
{
|
||||
|
||||
$columns = [
|
||||
'exame_nome' => 'parametrizacao_exame.nome',
|
||||
'exame_id' => 'parametrizacao_exame.id',
|
||||
'procedimento' => 'parametrizacao_exame.codigo',
|
||||
'nome_procedimento' => 'sigtap.procedimento.nome',
|
||||
'status_nome' => DB::raw('CASE WHEN exame_imagem_agendamento_exame.status_id IS NULL THEN "AGENDADO" ELSE exame_imagem_fechamento_status.nome END'),
|
||||
'unidade_solicitante_nome' => 'cadastro_unidade_solicitante.nome_fantasia',
|
||||
'nome_fantasia' => 'cadastro_unidade.nome_fantasia',
|
||||
'infraestrutura_nome' => DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END
|
||||
"),
|
||||
'unidade_id' => 'exame_imagem_agendamento.unidade_id',
|
||||
'unidade_solicitante_id' => 'exame_imagem_agendamento.unidade_solicitante_id',
|
||||
'infraestrutura_solicitante_nome' => DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura_solicitante.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura_solicitante.nome
|
||||
ELSE
|
||||
cadastro_unidade_solicitante.nome_fantasia
|
||||
END
|
||||
"),
|
||||
'quantidade' => DB::raw('count(exame_imagem_agendamento_exame.id)'),
|
||||
'nome_prestador' => 'cadastro_unidade_parametrizacao.nome_fantasia',
|
||||
'ocupacao_nome' => 'ocupacao.nome',
|
||||
'ocupacao_codigo' => 'ocupacao.codigo',
|
||||
'profissional' => 'cadastro_cidadao.nome',
|
||||
];
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getForms()
|
||||
{
|
||||
$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 [
|
||||
'unidadesSolicitantes' => self::getUnidadesSolicitantes($periodo),
|
||||
'unidadesExecutantes' => self::getUnidadesExecutantes($periodo),
|
||||
'exames' => self::getExames($periodo),
|
||||
'status' => self::getStatus($periodo),
|
||||
'ocupacoes' => self::getOcupacao($periodo),
|
||||
'profissionais' => self::getProfissional($periodo),
|
||||
'prestadores' => self::getPrestador($periodo),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private static function getUnidadesSolicitantes($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_imagem_agendamento', 'exame_imagem_agendamento.infraestrutura_solicitante_id', 'cadastro_infraestrutura.id')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->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 getUnidadesExecutantes($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_imagem_agendamento', 'exame_imagem_agendamento.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->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 getStatus($periodo)
|
||||
{
|
||||
return DB::table('exame_imagem_fechamento_status')
|
||||
->select(
|
||||
'exame_imagem_fechamento_status.id',
|
||||
'exame_imagem_fechamento_status.nome',
|
||||
)
|
||||
->join('exame_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.status_id', 'exame_imagem_fechamento_status.id')
|
||||
->join('exame_imagem_agendamento', 'exame_imagem_agendamento.id', 'exame_imagem_agendamento_exame.agendamento_id')
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->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_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->join('exame_imagem_agendamento', 'exame_imagem_agendamento.id', 'exame_imagem_agendamento_exame.agendamento_id')
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::IMAGEM)
|
||||
->whereBetween('exame_imagem_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);
|
||||
});
|
||||
})
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->distinct('codigo','nome')
|
||||
->orderBy('nome');
|
||||
}
|
||||
|
||||
private static function getExames($periodo)
|
||||
{
|
||||
return DB::table('parametrizacao_exame')
|
||||
->select(
|
||||
'parametrizacao_exame.id',
|
||||
'parametrizacao_exame.nome',
|
||||
)
|
||||
->join('exame_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.exame_id', 'parametrizacao_exame.id')
|
||||
->join('exame_imagem_agendamento', 'exame_imagem_agendamento.id', 'exame_imagem_agendamento_exame.agendamento_id')
|
||||
->where('parametrizacao_exame.tipo_id', Tipo::IMAGEM)
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->distinct('id','nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getOcupacao($periodo)
|
||||
{
|
||||
return DB::table('exame_imagem_agendamento')
|
||||
->select(
|
||||
'ocupacao.id',
|
||||
'ocupacao.nome',
|
||||
)
|
||||
->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.profissional_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('exame_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.agendamento_id', 'exame_imagem_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_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('exame_imagem_parametrizacao_ocupacao', 'exame_imagem_parametrizacao_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getProfissional($periodo)
|
||||
{
|
||||
return DB::table('exame_imagem_agendamento')
|
||||
->select(
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome',
|
||||
)
|
||||
->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.profissional_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('exame_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.agendamento_id', 'exame_imagem_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_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('exame_imagem_parametrizacao_ocupacao', 'exame_imagem_parametrizacao_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
private static function getPrestador($periodo)
|
||||
{
|
||||
return DB::table('exame_imagem_agendamento')
|
||||
->select(
|
||||
'cadastro_unidade_parametrizacao.id',
|
||||
'cadastro_unidade_parametrizacao.nome_fantasia AS nome',
|
||||
)
|
||||
->join('cadastro_usuario', 'cadastro_usuario.id', 'exame_imagem_agendamento.profissional_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('exame_imagem_agendamento_exame', 'exame_imagem_agendamento_exame.agendamento_id', 'exame_imagem_agendamento.id')
|
||||
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_imagem_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('exame_imagem_parametrizacao_ocupacao', 'exame_imagem_parametrizacao_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('exame_imagem_jornada', 'exame_imagem_jornada.id', 'exame_imagem_agendamento.jornada_id')
|
||||
->join('exame_imagem_parametrizacao_unidade', 'exame_imagem_parametrizacao_unidade.id', 'exame_imagem_jornada.prestador_id')
|
||||
->join('cadastro_unidade AS cadastro_unidade_parametrizacao', 'cadastro_unidade_parametrizacao.id', 'exame_imagem_parametrizacao_unidade.unidade_id')
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->unidade_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_solicitante_id', $id))
|
||||
->when(request()->infraestrutura_solicitante_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_solicitante_id', $id))
|
||||
->when(request()->unidade_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.unidade_id', $id))
|
||||
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.infraestrutura_id', $id))
|
||||
->when(request()->status_id, fn ($q, $id) => $q->where('exame_imagem_agendamento_exame.status_id', $id))
|
||||
->when(request()->procedimento_codigo, fn ($q, $id) => $q->where('parametrizacao_exame.codigo', $id))
|
||||
->when(request()->exame_id, fn ($q, $id) => $q->where('parametrizacao_exame.id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('ocupacao.id', $id))
|
||||
->when(request()->profissional_id, fn ($q, $id) => $q->where('exame_imagem_agendamento.profissional_id', $id))
|
||||
->whereBetween('exame_imagem_agendamento.data', $periodo)
|
||||
->distinct('nome')
|
||||
->orderBy('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getExamesBuilder()
|
||||
{
|
||||
$result = DB::table('exame_imagem_agendamento_exame')
|
||||
->select(
|
||||
'exame_imagem_agendamento_exame.id AS id_paciente',
|
||||
'exame_imagem_agendamento_exame.agendamento_id AS agendamento_id',
|
||||
'exame_imagem_agendamento_exame.status_id',
|
||||
'exame_imagem_agendamento.medico_id',
|
||||
'exame_imagem_agendamento.unidade_id',
|
||||
'exame_imagem_agendamento.profissional_id',
|
||||
'exame_imagem_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_imagem_agendamento.jornada_id AS jornada_agendamento',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
'medico_cidadao.nome AS medico_solicitante',
|
||||
'profissional.nome AS profissional_nome',
|
||||
'cadastro_unidade_solicitante.nome_fantasia AS unidade_solicitante_nome',
|
||||
'cadastro_unidade_solicitante.id AS unidade_solicitante_id',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_grupo.nome AS grupo',
|
||||
'cadastro_grupo.id AS grupo_exame_id',
|
||||
'cidadao.nome AS paciente_nome',
|
||||
'exame_imagem_jornada.id AS jornada_id',
|
||||
'exame_imagem_jornada.status_id AS jornada_status_id',
|
||||
'exame_imagem_jornada.prestador_id',
|
||||
'exame_imagem_parametrizacao_unidade.id AS parametrizacao',
|
||||
'exame_imagem_parametrizacao_unidade.unidade_id AS parametrizacao_unidade_id',
|
||||
'cadastro_unidade_parametrizacao.nome_fantasia AS parametrizacao_nome_prestador',
|
||||
'parametrizacao_exame.id AS exame_id',
|
||||
'parametrizacao_exame.nome AS exame_nome',
|
||||
'parametrizacao_exame.codigo AS exame_codigo',
|
||||
'sigtap.procedimento.nome AS nome_procedimento',
|
||||
'sigtap.procedimento.id AS procedimento_id',
|
||||
|
||||
DB::raw('DATE_FORMAT(exame_imagem_agendamento.hora_exame, "%H:%i") AS hora_exame'),
|
||||
DB::raw("DATE_FORMAT(exame_imagem_agendamento.data, '%d/%m/%Y') AS data"),
|
||||
DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y') AS data_pedido"),
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS prontuario'),
|
||||
DB::raw('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y")) as data_nascimento'),
|
||||
DB::raw("
|
||||
(
|
||||
SELECT cadastro_cidadao_telefone.numero
|
||||
FROM cadastro_cidadao_telefone
|
||||
WHERE cadastro_cidadao_telefone.principal = TRUE
|
||||
AND cadastro_cidadao_telefone.cidadao_id = cadastro_paciente.cidadao_id
|
||||
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
||||
LIMIT 1
|
||||
) AS paciente_telefone
|
||||
"),
|
||||
DB::raw(
|
||||
'(
|
||||
CASE WHEN cidadao.nome_mae IS NULL THEN
|
||||
"Não informado"
|
||||
ELSE
|
||||
cidadao.nome_mae
|
||||
END
|
||||
) as nome_mae'
|
||||
),
|
||||
DB::raw('CASE WHEN exame_imagem_agendamento_exame.status_id IS NULL THEN
|
||||
"AGENDADO"
|
||||
ELSE
|
||||
exame_imagem_fechamento_status.nome
|
||||
END AS status_nome'),
|
||||
)
|
||||
->join(
|
||||
'exame_imagem_agendamento',
|
||||
'exame_imagem_agendamento.id',
|
||||
'exame_imagem_agendamento_exame.agendamento_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'exame_imagem_agendamento.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura AS cadastro_infraestrutura_solicitante',
|
||||
'cadastro_infraestrutura_solicitante.id',
|
||||
'exame_imagem_agendamento.infraestrutura_solicitante_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade AS cadastro_unidade_solicitante',
|
||||
'cadastro_unidade_solicitante.id',
|
||||
'cadastro_infraestrutura_solicitante.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'exame_imagem_agendamento.medico_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao AS medico_cidadao',
|
||||
'medico_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario AS profissional_cidadao',
|
||||
'profissional_cidadao.id',
|
||||
'exame_imagem_agendamento.profissional_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao AS profissional',
|
||||
'profissional.id',
|
||||
'profissional_cidadao.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao AS cadastro_cidadao_usuario',
|
||||
'cadastro_cidadao_usuario.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'parametrizacao_exame',
|
||||
'parametrizacao_exame.id',
|
||||
'exame_imagem_agendamento_exame.exame_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_grupo',
|
||||
'cadastro_grupo.id',
|
||||
'exame_imagem_agendamento.exame_id'
|
||||
)
|
||||
->join(
|
||||
'exame_imagem_jornada',
|
||||
'exame_imagem_jornada.id',
|
||||
'exame_imagem_agendamento.jornada_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_usuario_registro_tipo',
|
||||
'cadastro_usuario_registro_tipo.id',
|
||||
'profissional_cidadao.registro_tipo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_paciente',
|
||||
'cadastro_paciente.id',
|
||||
'exame_imagem_agendamento_exame.paciente_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_cidadao AS cidadao',
|
||||
'cidadao.id',
|
||||
'cadastro_paciente.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'exame_imagem_fechamento_status',
|
||||
'exame_imagem_fechamento_status.id',
|
||||
'exame_imagem_agendamento_exame.status_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'exame_imagem_parametrizacao_unidade',
|
||||
'exame_imagem_parametrizacao_unidade.id',
|
||||
'exame_imagem_jornada.prestador_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade AS cadastro_unidade_parametrizacao',
|
||||
'cadastro_unidade_parametrizacao.id',
|
||||
'exame_imagem_parametrizacao_unidade.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);
|
||||
});
|
||||
})
|
||||
->leftJoin(
|
||||
'parametrizacao_procedimento',
|
||||
'parametrizacao_procedimento.procedimento_codigo',
|
||||
'parametrizacao_exame.codigo'
|
||||
)
|
||||
->where(function ($query) {
|
||||
|
||||
$query->where('exame_imagem_agendamento_exame.status_id', Status::REALIZADO);
|
||||
});
|
||||
|
||||
$result = $result->get();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user