762 lines
32 KiB
PHP
762 lines
32 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameImagem;
|
|
|
|
use App\Enums\ExameImagem\Fechamento\Status;
|
|
use App\Enums\Parametrizacao\Exame\Tipo;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\ExameImagem\Agendamento\AgendamentoExame;
|
|
use App\Models\ExameImagem\Fechamento\StatusFechamento;
|
|
use App\Models\Parametrizacao\Exame\Exame;
|
|
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 RelatorioExameStatusService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$params = request()->all();
|
|
|
|
$agendamentosBuilder = self::getAgendamentosBuilder();
|
|
|
|
if (!$agendamentosBuilder) {
|
|
|
|
return [];
|
|
}
|
|
|
|
$agendamentosBuilder = $agendamentosBuilder
|
|
->select(
|
|
'exame_imagem_agendamento_exame.id AS atendimento_id',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'exame_imagem_agendamento_exame.id AS id_paciente',
|
|
'cidadao.nome AS paciente_nome',
|
|
'cidadao.cpf as paciente_cpf',
|
|
'cidadao.cns as paciente_cns',
|
|
'cadastro_raca_cor.descricao AS paciente_raca_cor',
|
|
'sexo.descricao as paciente_sexo',
|
|
'medico_cidadao.nome AS medico_solicitante',
|
|
'parametrizacao_exame.id AS exame_id',
|
|
'parametrizacao_exame.nome AS exame_nome',
|
|
'parametrizacao_exame.codigo AS exame_codigo',
|
|
'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',
|
|
'sigtap.procedimento.nome AS nome_procedimento',
|
|
'exame_imagem_agendamento.agendador_id as agendador_id',
|
|
'agendador_cidadao.nome as nome_agendador',
|
|
'profissional.nome AS profissional_executante',
|
|
'parametrizacao_exame.valor_sigtap',
|
|
'sigtap.procedimento.valor_servico_ambilatorial',
|
|
DB::raw("
|
|
CASE
|
|
WHEN parametrizacao_exame.valor_sigtap = true
|
|
THEN FORMAT(sigtap.procedimento.valor_servico_ambilatorial, 2, 'pt_BR')
|
|
ELSE
|
|
FORMAT(parametrizacao_exame.valor, 2, 'pt_BR')
|
|
END AS exame_valor
|
|
"),
|
|
DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y %H:%i') AS data_hora_solicitacao"),
|
|
DB::raw('sigtap.procedimento.nome AS nome_procedimento'),
|
|
DB::raw('parametrizacao_exame.codigo AS codigo_procedimento'),
|
|
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS prontuario'),
|
|
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('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y")) as paciente_data_nascimento'),
|
|
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'),
|
|
);
|
|
|
|
self::setOrderBy($agendamentosBuilder);
|
|
|
|
return $agendamentosBuilder
|
|
->get()
|
|
->map(function ($exameStatus) {
|
|
|
|
return [
|
|
'data' => $exameStatus->data,
|
|
'prontuario' => $exameStatus->prontuario ?? '-',
|
|
'paciente_nome' => $exameStatus->paciente_nome,
|
|
'paciente_id' => $exameStatus->paciente_id,
|
|
'grupo' => $exameStatus->grupo,
|
|
'exame_nome' => $exameStatus->exame_nome,
|
|
'unidade_solicitante_nome' => $exameStatus->unidade_solicitante_nome,
|
|
'nome_fantasia' => $exameStatus->nome_fantasia,
|
|
'status_nome' => $exameStatus->status_nome,
|
|
'nome_procedimento' => $exameStatus->nome_procedimento,
|
|
'nome_agendador' => $exameStatus->nome_agendador,
|
|
'data_hora_solicitacao' => $exameStatus->data_hora_solicitacao,
|
|
'profissional_executante' => $exameStatus->profissional_executante,
|
|
'paciente_cpf' => $exameStatus->paciente_cpf ? MaskUtil::getCpfMasked($exameStatus->paciente_cpf) : '-',
|
|
'paciente_cns' => $exameStatus->paciente_cns ? MaskUtil::getCnsMasked( $exameStatus->paciente_cns) : '-',
|
|
'paciente_raca_cor' => $exameStatus->paciente_raca_cor,
|
|
'paciente_sexo' => $exameStatus->paciente_sexo,
|
|
'codigo_procedimento' => $exameStatus->codigo_procedimento,
|
|
'exame_valor' => $exameStatus->exame_valor,
|
|
'paciente_data_nascimento' => $exameStatus->paciente_data_nascimento,
|
|
];
|
|
});
|
|
}
|
|
|
|
private static function setOrderBy($builder)
|
|
{
|
|
$columns = [
|
|
'atendimento_id' => 'exame_imagem_agendamento_exame.id',
|
|
'paciente_id' => 'cadastro_paciente.id',
|
|
'id_paciente' => 'exame_imagem_agendamento_exame.id',
|
|
'paciente_nome' => 'cidadao.nome',
|
|
'paciente_cpf' => 'cidadao.cpf',
|
|
'paciente_cns' => 'cidadao.cns',
|
|
'paciente_raca_cor' => 'cadastro_raca_cor.descricao',
|
|
'paciente_sexo' => 'sexo.descricao',
|
|
'medico_solicitante' => 'medico_cidadao.nome',
|
|
'exame_id' => 'parametrizacao_exame.id',
|
|
'exame_nome' => 'parametrizacao_exame.nome',
|
|
'exame_codigo' => 'parametrizacao_exame.codigo',
|
|
'grupo' => 'cadastro_grupo.nome',
|
|
'grupo_exame_id' => 'cadastro_grupo.id',
|
|
'nome_fantasia' => 'cadastro_unidade.nome_fantasia',
|
|
'unidade_solicitante_nome' => 'cadastro_unidade_solicitante.nome_fantasia',
|
|
'nome_procedimento' => 'sigtap.procedimento.nome',
|
|
'agendador_id' => 'exame_imagem_agendamento.agendador_id',
|
|
'nome_agendador' => 'agendador_cidadao.nome',
|
|
'profissional_executante' => 'profissional.nome',
|
|
'exame_valor' => DB::raw('FORMAT(parametrizacao_exame.valor, 2, "pt_BR")'),
|
|
'data_hora_solicitacao' => DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y %H:%i')"),
|
|
'codigo_procedimento' => DB::raw('parametrizacao_exame.codigo'),
|
|
'prontuario' => DB::raw('LPAD(cadastro_paciente.prontuario,7,0)'),
|
|
'data' => DB::raw("DATE_FORMAT(exame_imagem_agendamento.data, '%d/%m/%Y')"),
|
|
'data_pedido' => DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y')"),
|
|
'paciente_data_nascimento' => DB::raw('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y"))'),
|
|
'nome_mae' => DB::raw('CASE WHEN cidadao.nome_mae IS NULL THEN "Não informado" ELSE cidadao.nome_mae END'),
|
|
'status_nome' => DB::raw('CASE WHEN exame_imagem_agendamento_exame.status_id IS NULL THEN "AGENDADO" ELSE exame_imagem_fechamento_status.nome END'),
|
|
];
|
|
|
|
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)
|
|
);
|
|
}
|
|
|
|
private static function getAgendamentosBuilder()
|
|
{
|
|
|
|
if (!request()->get('periodo')) {
|
|
|
|
return null;
|
|
}
|
|
|
|
$params = request()->all();
|
|
|
|
$agendamentos = DB::table('exame_imagem_agendamento_exame')
|
|
->join(
|
|
'exame_imagem_agendamento',
|
|
'exame_imagem_agendamento.id',
|
|
'exame_imagem_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_imagem_agendamento_exame.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao AS cidadao',
|
|
'cidadao.id',
|
|
'cadastro_paciente.cidadao_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'
|
|
)
|
|
->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',
|
|
'parametrizacao_exame.grupo_tipo_id',
|
|
'cadastro_grupo.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'
|
|
)
|
|
->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'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_usuario as dados_agendador',
|
|
'dados_agendador.id',
|
|
'exame_imagem_agendamento.agendador_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_cidadao AS agendador_cidadao',
|
|
'agendador_cidadao.id',
|
|
'dados_agendador.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'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_raca_cor',
|
|
'cadastro_raca_cor.id',
|
|
'cidadao.raca_cor_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_sexo as sexo',
|
|
'sexo.id',
|
|
'cidadao.sexo_id'
|
|
)
|
|
->whereBetween('exame_imagem_agendamento.data', [
|
|
Carbon::createFromFormat('d/m/Y', $params['periodo']['start'])->setTime(0, 0, 0),
|
|
Carbon::createFromFormat('d/m/Y', $params['periodo']['end'])->setTime(23, 59, 59)
|
|
])
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['unidade_id'])) {
|
|
|
|
$query->where('exame_imagem_agendamento.unidade_id', $params['unidade_id']);
|
|
}
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['unidade_solicitante_id'])) {
|
|
|
|
$query->where('cadastro_unidade_solicitante.id', $params['unidade_solicitante_id']);
|
|
}
|
|
})
|
|
->when(request()->paciente_id, function ($query) {
|
|
|
|
$query->where('exame_imagem_agendamento.paciente_id', request()->paciente_id);
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['exame_id'])) {
|
|
|
|
$query->where('exame_imagem_agendamento_exame.exame_id', $params['exame_id']);
|
|
}
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['prontuario'])) {
|
|
|
|
$query->where('cadastro_paciente.prontuario', $params['prontuario']);
|
|
}
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['procedimento'])) {
|
|
|
|
$query->where('parametrizacao_exame.codigo', $params['procedimento']);
|
|
}
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['status_id'])) {
|
|
|
|
$query->where('exame_imagem_agendamento_exame.status_id', $params['status_id']);
|
|
}
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (!is_null($params['grupo_id'])) {
|
|
|
|
$query->where('parametrizacao_exame.grupo_tipo_id', $params['grupo_id']);
|
|
}
|
|
})
|
|
->when($params['profissional_id'], function($query, $value) {
|
|
$query->where('exame_imagem_agendamento.profissional_id', $value);
|
|
});
|
|
|
|
return $agendamentos;
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('d/m/Y');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
return [
|
|
'form' => [],
|
|
'model' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'atendimento' => null,
|
|
'prontuario' => null,
|
|
'lancamentos' => [],
|
|
'paciente' => null,
|
|
'exame' => null,
|
|
'relatorioExameStatus' => self::getExameStatus(),
|
|
],
|
|
'additionals' => [
|
|
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
|
'current_date' => $now->format('d/m/Y')
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function datatable(Request $request)
|
|
{
|
|
$agendamentos = DB::table('exame_imagem_agendamento_exame')
|
|
->select(
|
|
'exame_imagem_agendamento_exame.agendamento_id AS agendamento_id',
|
|
'exame_imagem_agendamento_exame.id AS atendimento_id',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'exame_imagem_agendamento_exame.id AS id_paciente',
|
|
'exame_imagem_agendamento.unidade_id',
|
|
'cidadao.nome AS paciente_nome',
|
|
'cidadao.cpf as paciente_cpf',
|
|
'cidadao.cns as paciente_cns',
|
|
'cadastro_raca_cor.descricao AS paciente_raca_cor',
|
|
'sexo.descricao as paciente_sexo',
|
|
'medico_cidadao.nome AS medico_solicitante',
|
|
'exame_imagem_agendamento.profissional_id',
|
|
'parametrizacao_exame.id AS exame_id',
|
|
'parametrizacao_exame.nome AS exame_nome',
|
|
'parametrizacao_exame.codigo AS exame_codigo',
|
|
'parametrizacao_exame.valor_sigtap',
|
|
'sigtap.procedimento.valor_servico_ambilatorial',
|
|
DB::raw("
|
|
CASE
|
|
WHEN parametrizacao_exame.valor_sigtap = true
|
|
THEN FORMAT(sigtap.procedimento.valor_servico_ambilatorial, 2, 'pt_BR')
|
|
ELSE
|
|
FORMAT(parametrizacao_exame.valor, 2, 'pt_BR')
|
|
END AS exame_valor
|
|
"),
|
|
'sigtap.procedimento.nome AS nome_procedimento',
|
|
'profissional.nome AS profissional_executante',
|
|
'cadastro_unidade.nome_fantasia',
|
|
'cadastro_unidade_solicitante.nome_fantasia AS unidade_solicitante_nome',
|
|
'cadastro_unidade_solicitante.id AS unidade_solicitante_id',
|
|
'exame_imagem_agendamento.agendador_id as agendador_id',
|
|
'agendador_cidadao.nome as nome_agendador',
|
|
DB::raw("DATE_FORMAT(exame_imagem_agendamento.created_at, '%d/%m/%Y %H:%i') AS data_hora_solicitacao"),
|
|
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS prontuario'),
|
|
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('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y")) as paciente_data_nascimento'),
|
|
DB::raw('sigtap.procedimento.nome AS nome_procedimento'),
|
|
DB::raw('parametrizacao_exame.codigo AS codigo_procedimento'),
|
|
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'),
|
|
'cadastro_grupo.nome AS grupo'
|
|
)
|
|
->join(
|
|
'exame_imagem_agendamento',
|
|
'exame_imagem_agendamento.id',
|
|
'exame_imagem_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_imagem_agendamento_exame.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao AS cidadao',
|
|
'cidadao.id',
|
|
'cadastro_paciente.cidadao_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'
|
|
)
|
|
->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_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_grupo',
|
|
'parametrizacao_exame.grupo_tipo_id',
|
|
'cadastro_grupo.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'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_usuario as dados_agendador',
|
|
'dados_agendador.id',
|
|
'exame_imagem_agendamento.agendador_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_cidadao AS agendador_cidadao',
|
|
'agendador_cidadao.id',
|
|
'dados_agendador.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'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_raca_cor',
|
|
'cadastro_raca_cor.id',
|
|
'cidadao.raca_cor_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_sexo as sexo',
|
|
'sexo.id',
|
|
'cidadao.sexo_id'
|
|
)
|
|
->orderBy('cidadao.nome')
|
|
->where(function ($query) {
|
|
|
|
$query->where('exame_imagem_agendamento_exame.status_id', Status::AGENDADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::FALTOU)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::REALIZADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::FALTA_MEDICA)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::CANCELADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', NULL);
|
|
})
|
|
->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)
|
|
]);
|
|
|
|
if ($request->unidade_solicitante_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento.unidade_solicitante_id', $request->unidade_solicitante_id);
|
|
}
|
|
|
|
if ($request->unidade_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento.unidade_id', $request->unidade_id);
|
|
}
|
|
|
|
if ($request->profissional_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento.profissional_id', $request->profissional_id);
|
|
}
|
|
|
|
if ($request->grupo_id) {
|
|
|
|
$agendamentos->where('parametrizacao_exame.grupo_tipo_id', $request->grupo_id);
|
|
}
|
|
|
|
if ($request->exame_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento_exame.exame_id', $request->exame_id);
|
|
}
|
|
|
|
if ($request->status_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento_exame.status_id', $request->status_id);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
|
}
|
|
|
|
if ($request->procedimento) {
|
|
|
|
$agendamentos->where('parametrizacao_exame.codigo', $request->procedimento);
|
|
}
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentos->where('exame_imagem_agendamento.paciente_id', $request->paciente_id);
|
|
}
|
|
|
|
$agendamentos = $agendamentos->get();
|
|
|
|
return $agendamentos;
|
|
}
|
|
|
|
private static function getExameStatus()
|
|
{
|
|
$agendamentos = 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',
|
|
'parametrizacao_exame.id AS exame_id',
|
|
'parametrizacao_exame.nome AS exame_nome',
|
|
|
|
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'
|
|
)
|
|
->where(function ($query) {
|
|
|
|
$query->where('exame_imagem_agendamento_exame.status_id', Status::AGENDADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::FALTOU)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::REALIZADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::FALTA_MEDICA)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', Status::CANCELADO)
|
|
->orWhere('exame_imagem_agendamento_exame.status_id', NULL);
|
|
});
|
|
|
|
$agendamentos = $agendamentos->get();
|
|
|
|
return $agendamentos;
|
|
}
|
|
}
|