2974 lines
117 KiB
PHP
2974 lines
117 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ProntoAtendimento;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\FatorRisco as EnumFatorRisco;
|
|
use App\Enums\ProntoAtendimento\Atendimento\Desfecho;
|
|
use App\Services\Service;
|
|
use App\Utils\DataUtil;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RelatorioAtendimentoService extends Service
|
|
{
|
|
public static function autocompletePaciente()
|
|
{
|
|
$params = request()->all();
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
$pacientes = DB::table('cadastro_paciente')
|
|
->select(
|
|
'cadastro_paciente.id',
|
|
'cadastro_cidadao.nome',
|
|
DB::raw("LPAD(cadastro_paciente.prontuario, 7, 0) as prontuario")
|
|
)
|
|
->join(
|
|
'cadastro_cidadao',
|
|
'cadastro_cidadao.id',
|
|
'cadastro_paciente.cidadao_id'
|
|
)
|
|
->whereExists(function ($query) use ($params) {
|
|
|
|
$idadeDe = $params['idade_de'] ?? null;
|
|
$idadeAte = $params['idade_ate'] ?? null;
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_faa')
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.created_lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->whereColumn('cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id')
|
|
->where(function ($query) use ($params) {
|
|
|
|
$query->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params) {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn('lotacao_faa.id', 'pronto_atendimento_faa.lotacao_id');
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->whereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
});
|
|
})
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $query);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$query->join(
|
|
'pronto_atendimento_atendimento_classificacao_risco',
|
|
self::getJoinLastClassificacaoRiscoClosure($query)
|
|
);
|
|
|
|
$query->where(
|
|
'pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id',
|
|
$params['classificacao_risco_id']
|
|
);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$query->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] == 'excluido') {
|
|
|
|
$query->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
|
|
} else {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', $params['status']);
|
|
$query->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
|
|
if (self::hasParamId($params, 'cid_id')) {
|
|
|
|
$query->joinSub(
|
|
self::getCidJoinSub(),
|
|
'pronto_atendimento_atendimento_cid',
|
|
function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_cid.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
}
|
|
)
|
|
->where('pronto_atendimento_atendimento_cid.cid_id', $params['cid_id']);
|
|
}
|
|
|
|
if (!is_null($idadeDe)) {
|
|
|
|
$query->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) >= $idadeDe");
|
|
}
|
|
|
|
if (!is_null($idadeAte)) {
|
|
|
|
$query->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) <= $idadeAte");
|
|
}
|
|
});
|
|
|
|
return $pacientes;
|
|
}
|
|
|
|
public static function index()
|
|
{
|
|
$params = request()->all();
|
|
|
|
$idadeDe = $params['idade_de'] ?? null;
|
|
$idadeAte = $params['idade_ate'] ?? null;
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
$atendimentos = DB::table('pronto_atendimento_faa')
|
|
->select(
|
|
'cadastro_cidadao.nome AS nome_paciente',
|
|
'cadastro_cidadao.id AS cidadao_id',
|
|
'pronto_atendimento_faa.numero AS numero_faa',
|
|
'pronto_atendimento_faa.desfecho_id AS desfecho_id',
|
|
'cadastro_unidade.nome_fantasia AS nome_unidade',
|
|
'pronto_atendimento_atendimento_recepcao.atendido_em',
|
|
'pronto_atendimento_atendimento_recepcao.motivo_atendimento',
|
|
DB::raw("
|
|
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
|
THEN cadastro_infraestrutura.nome
|
|
ELSE cadastro_unidade.nome_fantasia
|
|
END AS local_atendimento
|
|
"),
|
|
'cadastro_paciente_classificacao.descricao AS classificacao_paciente',
|
|
|
|
DB::raw("
|
|
(
|
|
SELECT GROUP_CONCAT(cadastro_cidadao_telefone.numero SEPARATOR '#')
|
|
FROM cadastro_cidadao_telefone
|
|
WHERE cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
|
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
|
) AS telefone
|
|
"),
|
|
|
|
//procedimentos
|
|
DB::raw("COALESCE((
|
|
SELECT GROUP_CONCAT(sp.nome SEPARATOR '#')
|
|
FROM sigtap.procedimento sp
|
|
WHERE EXISTS(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.procedimento_id = sp.id
|
|
AND ap.atendimento_id = pronto_atendimento_atendimento.id
|
|
AND ap.deleted_at IS NULL
|
|
)
|
|
), '') AS procedimentos"),
|
|
|
|
//cids
|
|
DB::raw("COALESCE((
|
|
SELECT GROUP_CONCAT(sc.nome SEPARATOR '#')
|
|
FROM sigtap.cid sc
|
|
JOIN (
|
|
SELECT ac.cid_id,
|
|
ac.atendimento_id
|
|
FROM pronto_atendimento_atendimento_cid ac
|
|
|
|
UNION
|
|
|
|
SELECT ap.cid_principal_id as cid_id,
|
|
ap.atendimento_id
|
|
FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.is_parametrizado = 1
|
|
) x
|
|
WHERE x.cid_id = sc.id
|
|
AND x.atendimento_id = pronto_atendimento_atendimento.id
|
|
), '') AS cids"),
|
|
|
|
DB::raw("COALESCE((
|
|
SELECT GROUP_CONCAT(sc.codigo SEPARATOR '#')
|
|
FROM sigtap.cid sc
|
|
JOIN (
|
|
SELECT ac.cid_id,
|
|
ac.atendimento_id
|
|
FROM pronto_atendimento_atendimento_cid ac
|
|
|
|
UNION
|
|
|
|
SELECT ap.cid_principal_id as cid_id,
|
|
ap.atendimento_id
|
|
FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.is_parametrizado = 1
|
|
) x
|
|
WHERE x.cid_id = sc.id
|
|
AND x.atendimento_id = pronto_atendimento_atendimento.id
|
|
), '') AS cids_codigo"),
|
|
|
|
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as prontuario'),
|
|
'pronto_atendimento_faa_desfecho.nome AS nome_desfecho',
|
|
DB::raw("
|
|
(SELECT class_risco.nome
|
|
FROM pronto_atendimento_atendimento_classificacao_risco AS class_risco_atend
|
|
INNER JOIN pronto_atendimento_classificacao_risco AS class_risco
|
|
ON class_risco.id = class_risco_atend.classificacao_risco_id
|
|
WHERE class_risco_atend.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY class_risco_atend.created_at desc
|
|
LIMIT 1) AS classificacao_risco
|
|
"),
|
|
'pronto_atendimento_atendimento.id AS atendimento_id',
|
|
DB::raw("
|
|
CASE WHEN pronto_atendimento_faa.deleted_at IS NOT NULL THEN
|
|
'Excluído'
|
|
ELSE
|
|
DECODE_ORACLE(pronto_atendimento_faa.finalizado, 0, 'Aberto', 1, 'Finalizado', '')
|
|
END as status"
|
|
),
|
|
|
|
//nome_usuario
|
|
DB::raw(
|
|
"(
|
|
IF (
|
|
(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT cidadao.nome
|
|
FROM cadastro_usuario usuario
|
|
JOIN cadastro_cidadao cidadao ON cidadao.id = usuario.cidadao_id
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.usuario_id = usuario.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at
|
|
LIMIT 1
|
|
),
|
|
(
|
|
SELECT cidadao.nome
|
|
FROM cadastro_usuario usuario
|
|
JOIN cadastro_cidadao cidadao ON cidadao.id = usuario.cidadao_id
|
|
JOIN cadastro_lotacao lotacao ON lotacao.usuario_id = usuario.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
) AS nome_usuario"
|
|
),
|
|
//ocupacao
|
|
DB::raw(
|
|
"(
|
|
IF (
|
|
(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT sigtap_ocup.nome
|
|
FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.ocupacao_id = sigtap_ocup.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at
|
|
LIMIT 1
|
|
),
|
|
(
|
|
SELECT sigtap_ocup.nome
|
|
FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao lotacao ON lotacao.ocupacao_id = sigtap_ocup.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
) AS ocupacao"
|
|
),
|
|
//ocupacao código
|
|
DB::raw(
|
|
"(
|
|
IF (
|
|
(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT sigtap_ocup.codigo
|
|
FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.ocupacao_id = sigtap_ocup.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at
|
|
LIMIT 1
|
|
),
|
|
(
|
|
SELECT sigtap_ocup.codigo
|
|
FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao lotacao ON lotacao.ocupacao_id = sigtap_ocup.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
) AS ocupacao_codigo"
|
|
),
|
|
'cadastro_cidadao.data_nascimento',
|
|
DB::raw('TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE) AS idade'),
|
|
'cadastro_logradouro_tipo.descricao AS logradouro_tipo',
|
|
'cadastro_logradouro.nome AS logradouro_nome',
|
|
'cadastro_endereco.numero AS endereco_numero',
|
|
'cadastro_bairro.nome AS bairro_nome',
|
|
'cadastro_municipio.nome AS municipio_nome',
|
|
'cadastro_estado.uf',
|
|
'paciente_unidade.nome_fantasia AS unidade_referencia',
|
|
'cadastro_sexo.descricao AS sexo_descricao'
|
|
)
|
|
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.faa_id', 'pronto_atendimento_faa.id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->join('cadastro_sexo', 'cadastro_sexo.id', 'cadastro_cidadao.sexo_id')
|
|
->join('cadastro_lotacao AS lotacao_faa', 'lotacao_faa.id', 'pronto_atendimento_faa.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'lotacao_faa.infraestrutura_id')
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
|
->join(
|
|
'pronto_atendimento_atendimento_recepcao',
|
|
'pronto_atendimento_atendimento_recepcao.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
)
|
|
->leftJoin('cadastro_unidade AS paciente_unidade', 'paciente_unidade.id', 'cadastro_paciente.unidade_id')
|
|
->leftJoin('cadastro_paciente_classificacao', 'cadastro_paciente_classificacao.id', 'cadastro_paciente.classificacao_id')
|
|
->leftJoin('pronto_atendimento_faa_desfecho', 'pronto_atendimento_faa_desfecho.id', 'pronto_atendimento_faa.desfecho_id')
|
|
->leftJoin('cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id')
|
|
->leftJoin('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id')
|
|
->leftJoin('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
|
->leftJoin('intranet.cadastro_municipio AS cadastro_municipio', 'cadastro_municipio.id', 'cadastro_logradouro.municipio_id')
|
|
->leftJoin('cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id')
|
|
->leftJoin('cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id')
|
|
->leftJoin('intranet.cadastro_estado AS cadastro_estado', 'cadastro_estado.id', 'cadastro_municipio.estado_id')
|
|
->where(function ($query) {
|
|
|
|
$query->whereExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'paam.created_lotacao_id',
|
|
)
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
})
|
|
->orWhere(function ($query) {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereNotNull('pronto_atendimento_faa.lotacao_id')
|
|
->whereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhere(function ($query) {
|
|
|
|
$query->whereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam2')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'paam2.created_lotacao_id',
|
|
)
|
|
->whereColumn('paam2.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
});
|
|
});
|
|
|
|
if (isset($params['faa']) && (int)$params['faa'] > 0 ) {
|
|
|
|
$atendimentos->where('pronto_atendimento_faa.numero', $params['faa']);
|
|
|
|
} else {
|
|
|
|
$atendimentos->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_atendimento_recepcao.atendido_em, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $atendimentos);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$atendimentos->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$atendimentos->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$atendimentos->join(
|
|
'pronto_atendimento_atendimento_classificacao_risco',
|
|
self::getJoinLastClassificacaoRiscoClosure($atendimentos)
|
|
);
|
|
|
|
$atendimentos->whereIn(
|
|
'pronto_atendimento_atendimento_classificacao_risco.id',
|
|
function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw('MAX(id)'))
|
|
->from('pronto_atendimento_atendimento_classificacao_risco')
|
|
->groupBy('atendimento_id')
|
|
->havingRaw('MAX(classificacao_risco_id) = ?', [$params['classificacao_risco_id']]);
|
|
}
|
|
);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$atendimentos->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$atendimentos->whereRaw(
|
|
"{$params['ocupacao_id']} = (
|
|
IF (
|
|
(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT sigtap_ocup.id
|
|
FROM sigtap.ocupacao sigtap_ocup
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.ocupacao_id = sigtap_ocup.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at
|
|
LIMIT 1
|
|
),
|
|
(
|
|
SELECT sigtap_ocup.id
|
|
FROM sigtap.ocupacao sigtap_ocup
|
|
JOIN cadastro_lotacao lotacao ON lotacao.ocupacao_id = sigtap_ocup.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
)"
|
|
);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$atendimentos->whereRaw(
|
|
"{$params['profissional_id']} = (
|
|
IF (
|
|
(
|
|
SELECT 1
|
|
FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT usuario.id
|
|
FROM cadastro_usuario usuario
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.usuario_id = usuario.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at
|
|
LIMIT 1
|
|
),
|
|
(
|
|
SELECT usuario.id
|
|
FROM cadastro_usuario usuario
|
|
JOIN cadastro_lotacao lotacao ON lotacao.usuario_id = usuario.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
)"
|
|
);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] === 'excluido') {
|
|
|
|
$atendimentos->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
|
|
} else {
|
|
|
|
$atendimentos->where('pronto_atendimento_faa.finalizado', $params['status']);
|
|
$atendimentos->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
|
|
if (self::hasParamId($params, 'sexo_id')) {
|
|
|
|
$atendimentos->where('cadastro_cidadao.sexo_id', $params['sexo_id']);
|
|
}
|
|
|
|
$fatoresRiscoIn = [];
|
|
$fatoresRiscoNotIn = [];
|
|
|
|
if (isset($params['fator_risco_cancer']) && !is_null($params['fator_risco_cancer'])) {
|
|
|
|
$fator = (int)$params['fator_risco_cancer'];
|
|
$eFator = EnumFatorRisco::CANCERO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_diabetes']) && !is_null($params['fator_risco_diabetes'])) {
|
|
|
|
$fator = (int)$params['fator_risco_diabetes'];
|
|
$eFator = EnumFatorRisco::DIABETES;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_etilismo']) && !is_null($params['fator_risco_etilismo'])) {
|
|
|
|
$fator = (int)$params['fator_risco_etilismo'];
|
|
$eFator = EnumFatorRisco::ETILISMO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_gestante']) && !is_null($params['fator_risco_gestante'])) {
|
|
|
|
$fator = (int)$params['fator_risco_gestante'];
|
|
$eFator = EnumFatorRisco::GESTANTE;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
} else if ($fator === 0) {
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_hipertensao']) && !is_null($params['fator_risco_hipertensao'])) {
|
|
|
|
$fator = (int)$params['fator_risco_hipertensao'];
|
|
$eFator = EnumFatorRisco::HIPERTENSAO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_obesidade']) && !is_null($params['fator_risco_obesidade'])) {
|
|
|
|
$fator = (int)$params['fator_risco_obesidade'];
|
|
$eFator = EnumFatorRisco::OBESIDADE;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_perfil_sanguineo_alterado']) && !is_null($params['fator_risco_perfil_sanguineo_alterado'])) {
|
|
|
|
$fator = (int)$params['fator_risco_perfil_sanguineo_alterado'];
|
|
$eFator = EnumFatorRisco::PERFIL_SANGUINEO_ALTERADO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_radioterapia']) && !is_null($params['fator_risco_radioterapia'])) {
|
|
|
|
$fator = (int)$params['fator_risco_radioterapia'];
|
|
$eFator = EnumFatorRisco::RADIOTERAPIA;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_tabagismo']) && !is_null($params['fator_risco_tabagismo'])) {
|
|
|
|
$fator = (int)$params['fator_risco_tabagismo'];
|
|
$eFator = EnumFatorRisco::TABAGISMO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_uso_medicamentos_antineoplasicos_imunossupressores']) && !is_null($params['fator_risco_uso_medicamentos_antineoplasicos_imunossupressores'])) {
|
|
|
|
$fator = (int)$params['fator_risco_uso_medicamentos_antineoplasicos_imunossupressores'];
|
|
$eFator = EnumFatorRisco::USO_DE_MEDICAMENTOS_ANTINEOPLASICOS_OU_IMUNOSSUPRESSORES;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (isset($params['fator_risco_outro']) && !is_null($params['fator_risco_outro'])) {
|
|
|
|
$fator = (int)$params['fator_risco_outro'];
|
|
$eFator = EnumFatorRisco::OUTRO;
|
|
|
|
if ($fator === 1) {
|
|
|
|
array_push($fatoresRiscoIn, $eFator);
|
|
|
|
} else if ($fator === 0){
|
|
array_push($fatoresRiscoNotIn, $eFator);
|
|
}
|
|
}
|
|
|
|
if (count($fatoresRiscoIn) > 0) {
|
|
|
|
$fatoresRiscoInString = implode(',', $fatoresRiscoIn);
|
|
|
|
$atendimentos->whereExists(function ($query) use ($fatoresRiscoInString, $fatoresRiscoIn) {
|
|
$query->selectRaw(1)
|
|
->from('cadastro_cidadao_fator_risco')
|
|
->whereColumn('cadastro_cidadao_fator_risco.cidadao_id', 'cadastro_cidadao.id')
|
|
->whereRaw("cadastro_cidadao_fator_risco.fator_risco_id IN ($fatoresRiscoInString)")
|
|
->groupBy('cadastro_cidadao_fator_risco.cidadao_id')
|
|
->havingRaw("COUNT(DISTINCT cadastro_cidadao_fator_risco.fator_risco_id) = ?", [count($fatoresRiscoIn)]);
|
|
});
|
|
}
|
|
|
|
if (count($fatoresRiscoNotIn) > 0) {
|
|
|
|
$fatoresRiscoNotInString = implode(',', $fatoresRiscoNotIn);
|
|
|
|
$atendimentos->whereNotExists(function ($query) use ($fatoresRiscoNotInString, $fatoresRiscoNotIn) {
|
|
|
|
$query->selectRaw(1)
|
|
->from('cadastro_cidadao_fator_risco')
|
|
->whereColumn('cadastro_cidadao_fator_risco.cidadao_id', 'cadastro_cidadao.id')
|
|
->whereRaw("cadastro_cidadao_fator_risco.fator_risco_id IN ($fatoresRiscoNotInString)")
|
|
->groupBy('cadastro_cidadao_fator_risco.cidadao_id')
|
|
->havingRaw("COUNT(DISTINCT cadastro_cidadao_fator_risco.fator_risco_id) = ?", [count($fatoresRiscoNotIn)]);
|
|
});
|
|
}
|
|
|
|
|
|
if (self::hasParamId($params, 'cid_id')) {
|
|
|
|
$existsOnAtendimentoCid = function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_cid')
|
|
->whereColumn('pronto_atendimento_atendimento_cid.atendimento_id', 'pronto_atendimento_atendimento.id')
|
|
->where('pronto_atendimento_atendimento_cid.cid_id', $params['cid_id']);
|
|
};
|
|
|
|
$atendimentos->whereExists($existsOnAtendimentoCid)
|
|
->orWhereExists(function ($query) use ($params, $existsOnAtendimentoCid) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_procedimento')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_procedimento.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
)
|
|
->where('pronto_atendimento_atendimento_procedimento.cid_principal_id', $params['cid_id'])
|
|
->where('pronto_atendimento_atendimento_procedimento.is_parametrizado', true)
|
|
->whereNotExists($existsOnAtendimentoCid);
|
|
});
|
|
|
|
$atendimentos->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_atendimento_recepcao.atendido_em, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
}
|
|
|
|
if (!is_null($idadeDe)) {
|
|
|
|
$atendimentos->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) >= $idadeDe");
|
|
}
|
|
|
|
if (!is_null($idadeAte)) {
|
|
|
|
$atendimentos->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) <= $idadeAte");
|
|
}
|
|
|
|
if (self::hasParamId($params, 'paciente_id')) {
|
|
|
|
$atendimentos->where('cadastro_paciente.id', $params['paciente_id']);
|
|
}
|
|
|
|
if (isset($params['prontuario'])) {
|
|
|
|
$atendimentos->where('cadastro_paciente.prontuario', (int)$params['prontuario']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_paciente_id')) {
|
|
|
|
$atendimentos->where('cadastro_paciente.classificacao_id', $params['classificacao_paciente_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_referencia_id')) {
|
|
|
|
$atendimentos->where('cadastro_paciente.unidade_id', $params['unidade_referencia_id']);
|
|
}
|
|
}
|
|
|
|
$total_atendimentos = 0;
|
|
|
|
self::setOrderBy($atendimentos);
|
|
|
|
$atendimentos = $atendimentos->get()
|
|
->map(function ($atendimento) use ($total_atendimentos) {
|
|
|
|
$dtAtend = Carbon::create($atendimento->atendido_em);
|
|
|
|
$telefones = '';
|
|
|
|
foreach(explode('#', $atendimento->telefone) as $value){
|
|
|
|
$telefones .= MaskUtil::getPhoneMasked($value) . ', ';
|
|
}
|
|
|
|
$telefones = rtrim($telefones, ', ');
|
|
|
|
return array_merge([
|
|
'atendido_em' => $atendimento->atendido_em,
|
|
'motivo_atendimento' => $atendimento->motivo_atendimento,
|
|
'prontuario' => $atendimento->prontuario,
|
|
'nome_paciente' => $atendimento->nome_paciente,
|
|
'cidadao_id' => $atendimento->cidadao_id,
|
|
'sexo_descricao' => $atendimento->sexo_descricao,
|
|
'numero_faa' => $atendimento->numero_faa,
|
|
'nome_desfecho' => $atendimento->nome_desfecho,
|
|
'desfecho_id' => $atendimento->desfecho_id,
|
|
'nome_unidade' => $atendimento->nome_unidade,
|
|
'unidade_referencia' => $atendimento->unidade_referencia,
|
|
'local_atendimento' => $atendimento->local_atendimento,
|
|
'classificacao_risco' => $atendimento->classificacao_risco,
|
|
'classificacao_paciente' => $atendimento->classificacao_paciente,
|
|
'procedimentos' => $atendimento->procedimentos === ''
|
|
? [] : explode('#', $atendimento->procedimentos),
|
|
'cids' => $atendimento->cids === ''
|
|
? [] : explode('#', $atendimento->cids),
|
|
'cids_codigo' => $atendimento->cids_codigo === ''
|
|
? [] : explode('#', $atendimento->cids_codigo),
|
|
'profissional' => $atendimento->nome_usuario ?? '-',
|
|
'ocupacao' => $atendimento->ocupacao,
|
|
'ocupacao_codigo' => $atendimento->ocupacao_codigo,
|
|
'total_atendimentos' => $total_atendimentos,
|
|
'idade' => $atendimento->idade,
|
|
'data_nascimento' => $atendimento->data_nascimento,
|
|
'status' => $atendimento->status,
|
|
'dia_semana' => Str::ucfirst($dtAtend->translatedFormat('l')),
|
|
'endereco' => self::getFormatedEndereco($atendimento),
|
|
'telefones' => $telefones,
|
|
], self::getCidadaoFatoresRisco($atendimento->cidadao_id));
|
|
});
|
|
|
|
return [
|
|
'atendimentos' => $atendimentos,
|
|
'total_atendimentos' => $atendimentos->whereNotIn('desfecho_id', [Desfecho::EVASAO])->count(),
|
|
'total_pacientes' => $atendimentos->groupBy('cidadao_id')->count(),
|
|
'total_evasao' => $atendimentos->where('desfecho_id', Desfecho::EVASAO)->count(),
|
|
'total_faa' => $atendimentos->count()
|
|
];
|
|
}
|
|
|
|
private static function setOrderBy($builder)
|
|
{
|
|
$columns = [
|
|
'atendido_em' => 'pronto_atendimento_atendimento_recepcao.atendido_em',
|
|
'motivo_atendimento' => 'pronto_atendimento_atendimento_recepcao.motivo_atendimento',
|
|
'prontuario' => DB::raw('LPAD(cadastro_paciente.prontuario,7,0)'),
|
|
'nome_paciente' => 'cadastro_cidadao.nome',
|
|
'cidadao_id' => 'cadastro_cidadao.id',
|
|
'numero_faa' => 'pronto_atendimento_faa.numero',
|
|
'nome_desfecho' => 'pronto_atendimento_faa_desfecho.nome',
|
|
'desfecho_id' => 'pronto_atendimento_faa.desfecho_id',
|
|
'nome_unidade' => 'cadastro_unidade.nome_fantasia',
|
|
'unidade_referencia' => 'paciente_unidade.nome_fantasia',
|
|
'local_atendimento' => DB::raw("
|
|
CASE
|
|
WHEN cadastro_infraestrutura.nome IS NOT NULL
|
|
THEN cadastro_infraestrutura.nome
|
|
ELSE cadastro_unidade.nome_fantasia
|
|
END
|
|
"),
|
|
'classificacao_risco' => DB::raw("
|
|
(SELECT class_risco.nome
|
|
FROM pronto_atendimento_atendimento_classificacao_risco AS class_risco_atend
|
|
INNER JOIN pronto_atendimento_classificacao_risco AS class_risco
|
|
ON class_risco.id = class_risco_atend.classificacao_risco_id
|
|
WHERE class_risco_atend.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY class_risco_atend.created_at DESC
|
|
LIMIT 1)
|
|
"),
|
|
'classificacao_paciente' => 'cadastro_paciente_classificacao.descricao',
|
|
'procedimentos' => DB::raw("
|
|
COALESCE((
|
|
SELECT GROUP_CONCAT(sp.nome SEPARATOR '#')
|
|
FROM sigtap.procedimento sp
|
|
WHERE EXISTS(
|
|
SELECT 1 FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.procedimento_id = sp.id
|
|
AND ap.atendimento_id = pronto_atendimento_atendimento.id
|
|
AND ap.deleted_at IS NULL
|
|
)
|
|
), '')
|
|
"),
|
|
'cids' => DB::raw("
|
|
COALESCE((
|
|
SELECT GROUP_CONCAT(sc.nome SEPARATOR '#')
|
|
FROM sigtap.cid sc
|
|
JOIN (
|
|
SELECT ac.cid_id, ac.atendimento_id FROM pronto_atendimento_atendimento_cid ac
|
|
UNION
|
|
SELECT ap.cid_principal_id as cid_id, ap.atendimento_id
|
|
FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.is_parametrizado = 1
|
|
) x
|
|
WHERE x.cid_id = sc.id AND x.atendimento_id = pronto_atendimento_atendimento.id
|
|
), '')
|
|
"),
|
|
'cids_codigo' => DB::raw("
|
|
COALESCE((
|
|
SELECT GROUP_CONCAT(sc.codigo SEPARATOR '#')
|
|
FROM sigtap.cid sc
|
|
JOIN (
|
|
SELECT ac.cid_id, ac.atendimento_id FROM pronto_atendimento_atendimento_cid ac
|
|
UNION
|
|
SELECT ap.cid_principal_id as cid_id, ap.atendimento_id
|
|
FROM pronto_atendimento_atendimento_procedimento ap
|
|
WHERE ap.is_parametrizado = 1
|
|
) x
|
|
WHERE x.cid_id = sc.id AND x.atendimento_id = pronto_atendimento_atendimento.id
|
|
), '')
|
|
"),
|
|
'profissional' => DB::raw("
|
|
(
|
|
IF (
|
|
(
|
|
SELECT 1 FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT cidadao.nome FROM cadastro_usuario usuario
|
|
JOIN cadastro_cidadao cidadao ON cidadao.id = usuario.cidadao_id
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.usuario_id = usuario.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at LIMIT 1
|
|
),
|
|
(
|
|
SELECT cidadao.nome FROM cadastro_usuario usuario
|
|
JOIN cadastro_cidadao cidadao ON cidadao.id = usuario.cidadao_id
|
|
JOIN cadastro_lotacao lotacao ON lotacao.usuario_id = usuario.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
)
|
|
"),
|
|
'ocupacao' => DB::raw("
|
|
(
|
|
IF (
|
|
(
|
|
SELECT 1 FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT sigtap_ocup.nome FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.ocupacao_id = sigtap_ocup.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at LIMIT 1
|
|
),
|
|
(
|
|
SELECT sigtap_ocup.nome FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao lotacao ON lotacao.ocupacao_id = sigtap_ocup.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
)
|
|
"),
|
|
'ocupacao_codigo' => DB::raw("
|
|
(
|
|
IF (
|
|
(
|
|
SELECT 1 FROM pronto_atendimento_atendimento_medico atend_medico
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
LIMIT 1
|
|
) IS NOT NULL,
|
|
(
|
|
SELECT sigtap_ocup.codigo FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao atend_medico_lotacao ON atend_medico_lotacao.ocupacao_id = sigtap_ocup.id
|
|
JOIN pronto_atendimento_atendimento_medico atend_medico ON atend_medico.created_lotacao_id = atend_medico_lotacao.id
|
|
WHERE atend_medico.atendimento_id = pronto_atendimento_atendimento.id
|
|
ORDER BY atend_medico.created_at LIMIT 1
|
|
),
|
|
(
|
|
SELECT sigtap_ocup.codigo FROM sigtap.ocupacao AS sigtap_ocup
|
|
JOIN cadastro_lotacao lotacao ON lotacao.ocupacao_id = sigtap_ocup.id
|
|
WHERE lotacao.id = pronto_atendimento_faa.lotacao_id
|
|
)
|
|
)
|
|
)
|
|
"),
|
|
'data_nascimento' => DB::raw("
|
|
IF (cadastro_cidadao.data_nascimento IS NULL, 'Não informada',
|
|
DATE_FORMAT(cadastro_cidadao.data_nascimento, '%d/%m/%Y'))
|
|
"),
|
|
'status' => DB::raw("
|
|
CASE
|
|
WHEN pronto_atendimento_faa.deleted_at IS NOT NULL THEN 'Excluído'
|
|
ELSE DECODE_ORACLE(pronto_atendimento_faa.finalizado, 0, 'Aberto', 1, 'Finalizado', '')
|
|
END
|
|
")
|
|
];
|
|
|
|
if (is_null(request()->sort_by)) {
|
|
|
|
$builder->orderByRaw(join(', ', $columns));
|
|
|
|
return;
|
|
}
|
|
|
|
$column = $columns[request()->sort_by];
|
|
$direction = DataUtil::getBooleanValueFromStringBoolean(request()->sort_desc) ? 'DESC' : 'ASC';
|
|
|
|
unset($columns[request()->sort_by]);
|
|
|
|
$builder->orderByRaw(
|
|
"{$column} {$direction}, " . join(', ', $columns)
|
|
);
|
|
}
|
|
|
|
private static function getFormatedEndereco($atendimento)
|
|
{
|
|
return "{$atendimento->logradouro_tipo} {$atendimento->logradouro_nome},
|
|
{$atendimento->endereco_numero} - {$atendimento->bairro_nome}, {$atendimento->municipio_nome} - {$atendimento->uf}";
|
|
}
|
|
|
|
private static function getCidadaoFatoresRisco($cidadaoId)
|
|
{
|
|
$fatoresRiscoCadastrados = self::getFatoresRiscoCadastrados();
|
|
|
|
$cidadaoFatoresRisco = DB::table('cadastro_cidadao')
|
|
->select(
|
|
'cadastro_cidadao_fator_risco.fator_risco_id',
|
|
'cadastro_cidadao.fator_risco_outro'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_cidadao_fator_risco',
|
|
'cadastro_cidadao_fator_risco.cidadao_id',
|
|
'cadastro_cidadao.id'
|
|
)
|
|
->where('cadastro_cidadao.id', $cidadaoId)
|
|
->get();
|
|
|
|
$cidadaoFatoresRiscoIds = $cidadaoFatoresRisco->pluck('fator_risco_id')->map(function ($fator) {
|
|
|
|
return "_{$fator}";
|
|
});
|
|
|
|
$columns = [];
|
|
foreach ($fatoresRiscoCadastrados as $fatorRisco) {
|
|
|
|
$columns[$fatorRisco['value']] = 'Não';
|
|
|
|
if ($cidadaoFatoresRiscoIds->contains($fatorRisco['value'])) {
|
|
|
|
$columns[$fatorRisco['value']] = 'Sim';
|
|
|
|
if ($fatorRisco['value'] == "_".EnumFatorRisco::OUTRO) {
|
|
|
|
$columns[$fatorRisco['value']] = $cidadaoFatoresRisco[0]->fator_risco_outro;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $columns;
|
|
}
|
|
|
|
private static function getFatoresRiscoCadastrados()
|
|
{
|
|
return DB::table('cadastro_fator_risco')
|
|
->select('id','nome')
|
|
->orderBy('nome')
|
|
->get()
|
|
->map(function ($fatorRisco) {
|
|
|
|
return [
|
|
'value' => "_{$fatorRisco->id}",
|
|
'text' => $fatorRisco->nome
|
|
];
|
|
});
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$params = collect(request()->all());
|
|
$camposParaCarregar = collect();
|
|
|
|
if($params->isNotEmpty()){
|
|
$camposParaCarregar->push(
|
|
'dias_semana',
|
|
'unidades',
|
|
'locais_atendimento',
|
|
'classificacoes_risco',
|
|
'desfechos',
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
);
|
|
}
|
|
|
|
return [
|
|
'form' => self::camposFiltrados($camposParaCarregar),
|
|
'fatores' => self::getFatoresRiscoCadastrados(),
|
|
'model' => self::getModel(),
|
|
'additionals' => [
|
|
'data_atual' => Carbon::today()->format('d/m/Y'),
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function filtragemPorDiaSemana(){
|
|
return self::camposFiltrados(collect([
|
|
'unidades',
|
|
'locais_atendimento',
|
|
'classificacoes_risco',
|
|
'desfechos',
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorUnidade(){
|
|
return self::camposFiltrados(collect([
|
|
'locais_atendimento',
|
|
'classificacoes_risco',
|
|
'desfechos',
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorLocalAtendimento(){
|
|
return self::camposFiltrados(collect([
|
|
'classificacoes_risco',
|
|
'desfechos',
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorClassificacaoRisco(){
|
|
return self::camposFiltrados(collect([
|
|
'desfechos',
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorDesfecho(){
|
|
return self::camposFiltrados(collect([
|
|
'ocupacoes',
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorOcupacao(){
|
|
return self::camposFiltrados(collect([
|
|
'profissionais',
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorProfissional(){
|
|
return self::camposFiltrados(collect([
|
|
'status',
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorStatus(){
|
|
return self::camposFiltrados(collect([
|
|
'cids',
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorCid(){
|
|
return self::camposFiltrados(collect([
|
|
'sexos',
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorSexo(){
|
|
return self::camposFiltrados(collect([
|
|
'classificacoes_paciente',
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
public static function filtragemPorClassificacaoPaciente(){
|
|
return self::camposFiltrados(collect([
|
|
'unidades_referencia',
|
|
]));
|
|
}
|
|
|
|
private static function camposFiltrados(Collection $campos){
|
|
$params = request()->all();
|
|
$dados = collect();
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
if ($campos->contains('dias_semana')) {
|
|
$dados->put('dias_semana', self::getDiasSemana($params));
|
|
}
|
|
|
|
if ($campos->contains('unidades')) {
|
|
$dados->put('unidades', self::getUnidades($params));
|
|
}
|
|
|
|
if ($campos->contains('locais_atendimento')) {
|
|
$dados->put('locais_atendimento', self::getLocaisAtendimento($params));
|
|
}
|
|
|
|
if ($campos->contains('classificacoes_risco')) {
|
|
$dados->put('classificacoes_risco', self::getClassificacoesRisco($params));
|
|
}
|
|
|
|
if ($campos->contains('desfechos')) {
|
|
$dados->put('desfechos', self::getDesfechos($params));
|
|
}
|
|
|
|
if ($campos->contains('ocupacoes')) {
|
|
$dados->put('ocupacoes', self::getOcupacoes($params));
|
|
}
|
|
|
|
if ($campos->contains('profissionais')) {
|
|
$dados->put('profissionais', self::getProfissionais($params));
|
|
}
|
|
|
|
if ($campos->contains('status')) {
|
|
$dados->put('status', self::getStatus($params));
|
|
}
|
|
|
|
if ($campos->contains('cids')) {
|
|
$dados->put('cids', self::getCids($params));
|
|
}
|
|
|
|
if ($campos->contains('sexos')) {
|
|
$dados->put('sexos', self::getSexos($params));
|
|
}
|
|
|
|
if ($campos->contains('classificacoes_paciente')) {
|
|
$dados->put('classificacoes_paciente', self::getClassificacoesPaciente($params));
|
|
}
|
|
|
|
if ($campos->contains('unidades_referencia')) {
|
|
$dados->put('unidades_referencia', self::getUnidadesReferencia($params));
|
|
}
|
|
|
|
if ($campos->contains('fatores_risco_columns')) {
|
|
$dados->put('fatores_risco_columns', self::getFatoresRiscoCadastrados());
|
|
}
|
|
|
|
return $dados->all();
|
|
}
|
|
|
|
private static function adjustPeriod(&$params)
|
|
{
|
|
if (is_null($params['periodo']['date']['start'] ?? null) || is_null($params['periodo']['date']['end'] ?? null)) {
|
|
|
|
return;
|
|
}
|
|
|
|
$dateStart = $params['periodo']['date']['start'];
|
|
$dateEnd = $params['periodo']['date']['end'];
|
|
|
|
$timeStart = $params['periodo']['time']['start'];
|
|
$timeEnd = $params['periodo']['time']['end'];
|
|
|
|
$params['periodo']['date']['start'] = (Carbon::createFromFormat('d/m/Y', "{$dateStart}"))->startOfDay();
|
|
$params['periodo']['date']['end'] = (Carbon::createFromFormat('d/m/Y', "{$dateEnd}"))->endOfDay();
|
|
|
|
if (!is_null($timeStart ?? null)) {
|
|
|
|
$params['periodo']['date']['start'] = Carbon::createFromFormat('d/m/Y H:i:s', "{$dateStart} {$timeStart}");
|
|
}
|
|
|
|
if (!is_null($timeEnd)) {
|
|
|
|
$params['periodo']['date']['end'] = Carbon::createFromFormat('d/m/Y H:i:s', "{$dateEnd} {$timeEnd}");
|
|
}
|
|
|
|
$params['periodo']['date']['start'] = $params['periodo']['date']['start']->format('Y-m-d H:i');
|
|
$params['periodo']['date']['end'] = $params['periodo']['date']['end']->format('Y-m-d H:i');
|
|
}
|
|
|
|
private static function getDiasSemana($params)
|
|
{
|
|
$atendimentos = DB::table('pronto_atendimento_faa AS paf')
|
|
->select(
|
|
'paf.created_at'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento AS paa',
|
|
'paa.faa_id',
|
|
'paf.id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->where(function ($query) {
|
|
$query->whereNotNull(
|
|
'paam.id'
|
|
)
|
|
->orWhere(function ($query) {
|
|
$query->where(
|
|
'paf.finalizado',
|
|
true
|
|
)
|
|
->whereNotNull(
|
|
'paf.lotacao_id'
|
|
)
|
|
->whereNull(
|
|
'paam.id'
|
|
);
|
|
});
|
|
})
|
|
->whereBetween(
|
|
'paf.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
)
|
|
->get();
|
|
|
|
return $atendimentos
|
|
->map(function ($atendimento) {
|
|
$date = new Carbon($atendimento->created_at);
|
|
return [
|
|
'valor' => $date->format('w'),
|
|
'descricao' => Str::ucfirst($date->translatedFormat('l'))
|
|
];
|
|
})
|
|
->unique('valor')
|
|
->sortBy('valor')
|
|
->values();
|
|
}
|
|
|
|
private static function getUnidades($params)
|
|
{
|
|
$unidades = DB::table('cadastro_unidade AS cu')
|
|
->select(
|
|
'cu.id',
|
|
'cu.nome_fantasia'
|
|
)
|
|
->distinct()
|
|
->join(
|
|
'cadastro_infraestrutura AS ci',
|
|
'ci.unidade_id',
|
|
'cu.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao AS cl',
|
|
'cl.infraestrutura_id',
|
|
'ci.id'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_faa AS paf',
|
|
'paf.created_lotacao_id',
|
|
'cl.id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento AS paa',
|
|
'paa.faa_id',
|
|
'paf.id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->whereBetween(
|
|
'paf.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
)
|
|
->where(function ($query) {
|
|
|
|
$query->whereNotNull(
|
|
'paam.id'
|
|
)
|
|
->orWhere(function ($query) {
|
|
|
|
$query->where(
|
|
'paf.finalizado',
|
|
true
|
|
)
|
|
->whereNotNull('paf.lotacao_id')
|
|
->whereNull('paam.id');
|
|
})
|
|
->orWhereNull('paa.id');
|
|
});
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $unidades);
|
|
}
|
|
|
|
return $unidades
|
|
->orderBy('cu.nome_fantasia')
|
|
->get();
|
|
}
|
|
|
|
private static function getLocaisAtendimento($params)
|
|
{
|
|
$infraestruturas = DB::table('cadastro_infraestrutura AS ci')
|
|
->select(
|
|
'ci.id',
|
|
DB::raw("
|
|
CASE WHEN ci.nome IS NOT NULL
|
|
THEN ci.nome
|
|
ELSE cu.nome_fantasia
|
|
END AS nome
|
|
")
|
|
)
|
|
->distinct()
|
|
->leftJoin(
|
|
'cadastro_unidade AS cu',
|
|
'cu.id',
|
|
'ci.unidade_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_lotacao AS cl',
|
|
'cl.infraestrutura_id',
|
|
'ci.id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_faa AS paf',
|
|
'paf.created_lotacao_id',
|
|
'cl.id'
|
|
)
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(paf.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
)
|
|
->where(function ($query) {
|
|
|
|
$query->whereExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento AS paa')
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->whereColumn('paa.faa_id', 'paf.id');
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento AS paa')
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->whereColumn('paa.faa_id', 'paf.id');
|
|
})
|
|
->orWhere(function ($query) {
|
|
$query->where('paf.finalizado', true)
|
|
->whereNotNull('paf.lotacao_id')
|
|
->whereNotExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento AS paa')
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->whereColumn('paa.faa_id', 'paf.id');
|
|
});
|
|
});
|
|
});
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $infraestruturas);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
$infraestruturas->where('cu.id', $params['unidade_id']);
|
|
}
|
|
|
|
return $infraestruturas
|
|
->orderBy(DB::raw("
|
|
CASE WHEN ci.nome IS NOT NULL
|
|
THEN ci.nome
|
|
ELSE cu.nome_fantasia
|
|
END
|
|
"))
|
|
->get();
|
|
}
|
|
|
|
|
|
private static function getClassificacoesRisco($params)
|
|
{
|
|
return DB::table('pronto_atendimento_classificacao_risco AS pacr')
|
|
->select(
|
|
'pacr.id',
|
|
'pacr.nome'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_atendimento_classificacao_risco AS paacr',
|
|
'paacr.classificacao_risco_id',
|
|
'pacr.id'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_atendimento AS paa',
|
|
'paa.id',
|
|
'paacr.atendimento_id'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_faa AS paf',
|
|
'paf.id',
|
|
'paa.faa_id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao AS cl',
|
|
'cl.id',
|
|
'paf.created_lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura AS ci',
|
|
'ci.id',
|
|
'cl.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_atendimento_medico AS paam',
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
)
|
|
->whereBetween(
|
|
'paf.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
)
|
|
->where(function ($query) {
|
|
|
|
$query->whereNotNull(
|
|
'paf.lotacao_id'
|
|
)
|
|
->where(
|
|
'paf.finalizado',
|
|
true
|
|
)
|
|
->whereExists(function ($query) {
|
|
$query->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn(
|
|
'paam.atendimento_id',
|
|
'paa.id'
|
|
);
|
|
});
|
|
})
|
|
->where(function ($query) use ($params) {
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $query);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$query->where('ci.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$query->where('ci.id', $params['local_atendimento_id']);
|
|
}
|
|
})
|
|
->orderBy('pacr.nome')
|
|
->distinct()
|
|
->get();
|
|
}
|
|
|
|
private static function getDesfechos($params)
|
|
{
|
|
$query = DB::table('pronto_atendimento_faa_desfecho')
|
|
->select(
|
|
'pronto_atendimento_faa_desfecho.id',
|
|
'pronto_atendimento_faa_desfecho.nome'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_faa',
|
|
'pronto_atendimento_faa.desfecho_id',
|
|
'pronto_atendimento_faa_desfecho.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->where(
|
|
'pronto_atendimento_faa.finalizado',
|
|
true
|
|
)
|
|
->whereBetween(
|
|
'pronto_atendimento_faa.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $query);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'infraestrutura_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.id', $params['infraestrutura_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$query->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) use ($params) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', 'pronto_atendimento_faa.id')
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
});
|
|
}
|
|
|
|
$query->orderBy('pronto_atendimento_faa_desfecho.nome');
|
|
|
|
return $query
|
|
->distinct()
|
|
->get();
|
|
}
|
|
|
|
private static function getOcupacoes($params)
|
|
{
|
|
$ocupacoes = DB::table('pronto_atendimento_faa')
|
|
->selectRaw("
|
|
COALESCE(
|
|
ocupacao_atend_medico.id,
|
|
ocupacao_faa.id
|
|
) as id,
|
|
COALESCE(
|
|
ocupacao_atend_medico.nome,
|
|
ocupacao_faa.nome
|
|
) as nome
|
|
")
|
|
->distinct()
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico',
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_lotacao AS lotacao_atend_medico',
|
|
'lotacao_atend_medico.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->leftJoin(
|
|
'sigtap.ocupacao AS ocupacao_atend_medico',
|
|
'ocupacao_atend_medico.id',
|
|
'lotacao_atend_medico.ocupacao_id'
|
|
)
|
|
->leftJoin(
|
|
'sigtap.ocupacao AS ocupacao_faa',
|
|
'ocupacao_faa.id',
|
|
'cadastro_lotacao.ocupacao_id'
|
|
)
|
|
->where(
|
|
'pronto_atendimento_faa.finalizado',
|
|
true
|
|
)
|
|
->whereBetween(
|
|
'pronto_atendimento_faa.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $ocupacoes);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$ocupacoes->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'infraestrutura_id')) {
|
|
|
|
$ocupacoes->where('cadastro_infraestrutura.id', $params['infraestrutura_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$ocupacoes->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) use ($params) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', 'pronto_atendimento_atendimento.id')
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
});
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$ocupacoes->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
return $ocupacoes
|
|
->orderBy('nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getProfissionais($params)
|
|
{
|
|
$query = DB::table('pronto_atendimento_faa')
|
|
->select(
|
|
DB::raw("
|
|
COALESCE(
|
|
usuario_atend_medico.id,
|
|
usuario_faa.id
|
|
) as id,
|
|
COALESCE(
|
|
cidadao_atend_medico.nome,
|
|
cidadao_faa.nome
|
|
) as nome
|
|
")
|
|
)
|
|
->distinct()
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_centro_custo',
|
|
'cadastro_centro_custo.id',
|
|
'pronto_atendimento_faa.created_centro_custo_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_centro_custo.infraestrutura_id'
|
|
)
|
|
->leftJoin(
|
|
'pronto_atendimento_atendimento_medico',
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_lotacao AS atend_medico_lotacao',
|
|
'atend_medico_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_usuario AS usuario_atend_medico',
|
|
'usuario_atend_medico.id',
|
|
'atend_medico_lotacao.usuario_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_cidadao AS cidadao_atend_medico',
|
|
'cidadao_atend_medico.id',
|
|
'usuario_atend_medico.cidadao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_lotacao AS lotacao_faa',
|
|
'lotacao_faa.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_usuario AS usuario_faa',
|
|
'usuario_faa.id',
|
|
'lotacao_faa.usuario_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_cidadao AS cidadao_faa',
|
|
'cidadao_faa.id',
|
|
'usuario_faa.cidadao_id'
|
|
)
|
|
->where(function ($query) use ($params) {
|
|
$query->whereExists(function ($existsQuery) use ($params) {
|
|
$existsQuery->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$existsQuery->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($orWhereQuery) use ($params) {
|
|
$orWhereQuery->where(
|
|
'pronto_atendimento_faa.finalizado',
|
|
true
|
|
)
|
|
->whereExists(function ($existsQuery) use ($params) {
|
|
$existsQuery->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn(
|
|
'lotacao_faa.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
);
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$existsQuery->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
})
|
|
->doesntExist(function ($notExistsQuery) {
|
|
$notExistsQuery->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
});
|
|
});
|
|
})
|
|
->whereBetween(
|
|
'pronto_atendimento_faa.created_at',
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $query);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'infraestrutura_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.id', $params['infraestrutura_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$query->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) use ($params) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', 'pronto_atendimento_faa.id')
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
});
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$query->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
return $query->orderBy('nome')->get();
|
|
}
|
|
|
|
private static function getStatus($params)
|
|
{
|
|
$hasParamId = function($params, $key) {
|
|
return isset($params[$key]) && !is_null($params[$key]);
|
|
};
|
|
|
|
$faas = DB::table('pronto_atendimento_faa')
|
|
->select("pronto_atendimento_faa.finalizado")
|
|
->distinct()
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.created_lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->whereNull('pronto_atendimento_faa.deleted_at')
|
|
->where(function ($query) use ($params, $hasParamId) {
|
|
$query->whereExists(function ($query) use ($params, $hasParamId) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if ($hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params, $hasParamId) {
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params, $hasParamId) {
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn(
|
|
'lotacao_faa.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
);
|
|
|
|
if ($hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->whereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
});
|
|
})
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $faas);
|
|
}
|
|
|
|
if ($hasParamId($params, 'unidade_id')) {
|
|
|
|
$faas->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'infraestrutura_id')) {
|
|
|
|
$faas->where('cadastro_infraestrutura.id', $params['infraestrutura_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'classificacao_risco_id')) {
|
|
$faas->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', '=', 'pronto_atendimento_atendimento.id');
|
|
})
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'desfecho_id')) {
|
|
|
|
$faas->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
$faas = $faas->get()->map(function ($faa) {
|
|
return [
|
|
'valor' => $faa->finalizado,
|
|
'descricao' => $faa->finalizado == 1 ? 'Finalizado' : 'Aberto'
|
|
];
|
|
})->sortBy('descricao')->values();
|
|
|
|
$faasExcluida = DB::table('pronto_atendimento_faa')
|
|
->select("pronto_atendimento_faa.finalizado")
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.created_lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->whereNotNull('pronto_atendimento_faa.deleted_at')
|
|
->where(function ($query) use ($params, $hasParamId) {
|
|
$query->whereExists(function ($query) use ($params, $hasParamId) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if ($hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params, $hasParamId) {
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params, $hasParamId) {
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn(
|
|
'lotacao_faa.id',
|
|
'pronto_atendimento_faa.lotacao_id'
|
|
);
|
|
|
|
if ($hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->whereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn('pronto_atendimento_atendimento_medico.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn('pronto_atendimento_atendimento_medico.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $faasExcluida);
|
|
}
|
|
|
|
if ($hasParamId($params, 'unidade_id')) {
|
|
|
|
$faasExcluida->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'infraestrutura_id')) {
|
|
|
|
$faasExcluida->where('cadastro_infraestrutura.id', $params['infraestrutura_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$faasExcluida->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', '=', 'pronto_atendimento_atendimento.id');
|
|
})
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
}
|
|
|
|
if ($hasParamId($params, 'desfecho_id')) {
|
|
|
|
$faasExcluida->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
$firstExcluida = $faasExcluida->first();
|
|
|
|
if ($firstExcluida) {
|
|
$faas[] = [
|
|
'valor' => 'excluido',
|
|
'descricao' => 'Excluído'
|
|
];
|
|
}
|
|
|
|
return $faas;
|
|
}
|
|
|
|
private static function getCids($params)
|
|
{
|
|
$cids = DB::table('sigtap.cid AS scid')
|
|
->select(
|
|
'scid.id',
|
|
'scid.nome'
|
|
)
|
|
->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_faa')
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->joinSub(
|
|
self::getCidJoinSub(),
|
|
'pronto_atendimento_atendimento_cid',
|
|
function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_cid.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
}
|
|
)
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_faa.created_lotacao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_lotacao.infraestrutura_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_cid.cid_id',
|
|
'scid.id'
|
|
)
|
|
->where(function ($query) use ($params) {
|
|
|
|
$query->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'pronto_atendimento_atendimento_medico.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params) {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn('lotacao_faa.id', 'pronto_atendimento_faa.lotacao_id');
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->doesntExist(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_medico.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
});
|
|
})
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $query);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$query->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$query->join(
|
|
'pronto_atendimento_atendimento_classificacao_risco',
|
|
self::getJoinLastClassificacaoRiscoClosure($query)
|
|
);
|
|
|
|
$query->where(
|
|
'pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id',
|
|
$params['classificacao_risco_id']
|
|
);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$query->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] == 'excluido') {
|
|
|
|
$query->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
|
|
} else {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', $params['status']);
|
|
$query->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
});
|
|
|
|
return $cids
|
|
->orderBy('scid.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getSexos($params)
|
|
{
|
|
$params = request()->all();
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
$sexos = DB::table('cadastro_sexo')
|
|
->select(
|
|
'cadastro_sexo.id',
|
|
'cadastro_sexo.descricao'
|
|
)
|
|
->distinct()
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.sexo_id', 'cadastro_sexo.id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.cidadao_id', 'cadastro_cidadao.id')
|
|
->join('pronto_atendimento_faa', 'pronto_atendimento_faa.paciente_id','cadastro_paciente.id')
|
|
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.faa_id', 'pronto_atendimento_faa.id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'pronto_atendimento_faa.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->whereBetween(
|
|
DB::raw("DATE_FORMAT(pronto_atendimento_faa.created_at, '%Y-%m-%d %H:%i')"),
|
|
[
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]
|
|
);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $sexos);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$sexos->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$sexos->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$sexos->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', '=', 'pronto_atendimento_atendimento.id');
|
|
})
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$sexos->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] == 'excluido') {
|
|
|
|
$sexos->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
} else {
|
|
|
|
$sexos->where('pronto_atendimento_faa.finalizado', $params['status'])
|
|
->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
|
|
if (self::hasParamId($params, 'cid_id')) {
|
|
|
|
$sexos->join('pronto_atendimento_atendimento_cid', 'pronto_atendimento_atendimento_cid.atendimento_id', '=', 'pronto_atendimento_atendimento.id')
|
|
->where('pronto_atendimento_atendimento_cid.cid_id', $params['cid_id']);
|
|
}
|
|
|
|
$idadeDe = $params['idade_de'] ?? null;
|
|
$idadeAte = $params['idade_ate'] ?? null;
|
|
|
|
if (!is_null($idadeDe)) {
|
|
|
|
$sexos->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) >= ?", [$idadeDe]);
|
|
}
|
|
|
|
if (!is_null($idadeAte)) {
|
|
|
|
$sexos->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) <= ?", [$idadeAte]);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'paciente_id')) {
|
|
|
|
$sexos->where('cadastro_paciente.id', $params['paciente_id']);
|
|
}
|
|
|
|
if (isset($params['prontuario'])) {
|
|
|
|
$sexos->where('cadastro_paciente.prontuario', (int)$params['prontuario']);
|
|
}
|
|
|
|
return $sexos->orderBy('cadastro_sexo.descricao')->get();
|
|
}
|
|
|
|
private static function getClassificacoesPaciente($params)
|
|
{
|
|
$params = request()->all();
|
|
|
|
$idadeDe = $params['idade_de'] ?? null;
|
|
$idadeAte = $params['idade_ate'] ?? null;
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
$classificacoesPaciente = DB::table('pronto_atendimento_faa')
|
|
->select(
|
|
'cadastro_paciente_classificacao.id',
|
|
'cadastro_paciente_classificacao.descricao'
|
|
)
|
|
->distinct()
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'pronto_atendimento_faa.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao',
|
|
'cadastro_cidadao.id',
|
|
'cadastro_paciente.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente_classificacao',
|
|
'cadastro_paciente_classificacao.id',
|
|
'cadastro_paciente.classificacao_id'
|
|
)
|
|
->join(
|
|
'pronto_atendimento_atendimento',
|
|
'pronto_atendimento_atendimento.faa_id',
|
|
'pronto_atendimento_faa.id'
|
|
)
|
|
->join(
|
|
'cadastro_centro_custo',
|
|
'cadastro_centro_custo.id',
|
|
'pronto_atendimento_faa.created_centro_custo_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'cadastro_centro_custo.infraestrutura_id'
|
|
);
|
|
|
|
if (isset($params['periodo']['date']['start']) && isset($params['periodo']['date']['end'])) {
|
|
|
|
$classificacoesPaciente->whereBetween('pronto_atendimento_faa.created_at', [
|
|
$params['periodo']['date']['start'],
|
|
$params['periodo']['date']['end']
|
|
]);
|
|
}
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $classificacoesPaciente);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$classificacoesPaciente->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$classificacoesPaciente->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$classificacoesPaciente->join(
|
|
'pronto_atendimento_atendimento_classificacao_risco',
|
|
self::getJoinLastClassificacaoRiscoClosure($classificacoesPaciente)
|
|
)->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$classificacoesPaciente->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] == 'excluido') {
|
|
|
|
$classificacoesPaciente->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
} else {
|
|
|
|
$classificacoesPaciente->where('pronto_atendimento_faa.finalizado', $params['status'])
|
|
->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
|
|
if (self::hasParamId($params, 'cid_id')) {
|
|
|
|
$classificacoesPaciente->joinSub(
|
|
self::getCidJoinSub(),
|
|
'pronto_atendimento_atendimento_cid',
|
|
function ($join) {
|
|
|
|
$join->on('pronto_atendimento_atendimento_cid.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
}
|
|
)->where('pronto_atendimento_atendimento_cid.cid_id', $params['cid_id']);
|
|
}
|
|
|
|
if (!is_null($idadeDe)) {
|
|
|
|
$classificacoesPaciente->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) >= ?", [$idadeDe]);
|
|
}
|
|
|
|
if (!is_null($idadeAte)) {
|
|
|
|
$classificacoesPaciente->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) <= ?", [$idadeAte]);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'paciente_id')) {
|
|
|
|
$classificacoesPaciente->where('cadastro_paciente.id', $params['paciente_id']);
|
|
}
|
|
|
|
if (isset($params['prontuario'])) {
|
|
|
|
$classificacoesPaciente->where('cadastro_paciente.prontuario', (int)$params['prontuario']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'sexo_id')) {
|
|
|
|
$classificacoesPaciente->where('cadastro_cidadao.sexo_id', $params['sexo_id']);
|
|
}
|
|
|
|
$classificacoesPaciente->where(function ($query) use ($params) {
|
|
$query->whereExists(function ($query) use ($params) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->join(
|
|
'cadastro_lotacao',
|
|
'cadastro_lotacao.id',
|
|
'paam.created_lotacao_id'
|
|
)
|
|
->whereColumn(
|
|
'paam.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
);
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params) {
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params) {
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn('lotacao_faa.id', 'pronto_atendimento_faa.lotacao_id');
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->whereNotExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico')
|
|
->whereColumn('pronto_atendimento_atendimento_medico.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
});
|
|
|
|
return $classificacoesPaciente
|
|
->orderBy('cadastro_paciente_classificacao.descricao')
|
|
->get();
|
|
}
|
|
|
|
private static function getUnidadesReferencia($params)
|
|
{
|
|
$params = request()->all();
|
|
|
|
$idadeDe = $params['idade_de'] ?? null;
|
|
$idadeAte = $params['idade_ate'] ?? null;
|
|
|
|
self::adjustPeriod($params);
|
|
|
|
$unidades = DB::table('pronto_atendimento_faa')
|
|
->select(
|
|
'cadastro_unidade.id',
|
|
'cadastro_unidade.nome_fantasia'
|
|
)
|
|
->distinct()
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->join('cadastro_paciente_classificacao', 'cadastro_paciente_classificacao.id','cadastro_paciente.classificacao_id')
|
|
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.faa_id', 'pronto_atendimento_faa.id')
|
|
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'pronto_atendimento_faa.created_centro_custo_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_centro_custo.infraestrutura_id')
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_paciente.unidade_id')
|
|
->where(function ($query) use ($params) {
|
|
|
|
$query->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'paam.created_lotacao_id')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('cadastro_lotacao.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('cadastro_lotacao.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->orWhere(function ($query) use ($params) {
|
|
|
|
$query->where('pronto_atendimento_faa.finalizado', true)
|
|
->whereExists(function ($query) use ($params) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('cadastro_lotacao AS lotacao_faa')
|
|
->whereColumn('lotacao_faa.id', 'pronto_atendimento_faa.lotacao_id');
|
|
|
|
if (self::hasParamId($params, 'ocupacao_id')) {
|
|
|
|
$query->where('lotacao_faa.ocupacao_id', $params['ocupacao_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'profissional_id')) {
|
|
|
|
$query->where('lotacao_faa.usuario_id', $params['profissional_id']);
|
|
}
|
|
})
|
|
->whereNotExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->orWhereNotExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico AS paam')
|
|
->whereColumn('paam.atendimento_id', 'pronto_atendimento_atendimento.id');
|
|
});
|
|
})
|
|
->whereBetween('pronto_atendimento_faa.created_at', [
|
|
$params['periodo']['date']['start'] ?? null,
|
|
$params['periodo']['date']['end'] ?? null
|
|
]);
|
|
|
|
if (isset($params['dia_semana'])) {
|
|
|
|
self::addWhereRawDayOfWeek($params['dia_semana'], $unidades);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'unidade_id')) {
|
|
|
|
$unidades->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'local_atendimento_id')) {
|
|
|
|
$unidades->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_risco_id')) {
|
|
|
|
$unidades->join('pronto_atendimento_atendimento_classificacao_risco', function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_classificacao_risco.atendimento_id', '=', 'pronto_atendimento_atendimento.id');
|
|
})
|
|
->where('pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id', $params['classificacao_risco_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'desfecho_id')) {
|
|
|
|
$unidades->where('pronto_atendimento_faa.desfecho_id', $params['desfecho_id']);
|
|
}
|
|
|
|
if (isset($params['status'])) {
|
|
|
|
if ($params['status'] == 'excluido') {
|
|
|
|
$unidades->whereNotNull('pronto_atendimento_faa.deleted_at');
|
|
} else {
|
|
|
|
$unidades->where('pronto_atendimento_faa.finalizado', $params['status'])
|
|
->whereNull('pronto_atendimento_faa.deleted_at');
|
|
}
|
|
}
|
|
|
|
if (self::hasParamId($params, 'cid_id')) {
|
|
|
|
$unidades->joinSub(
|
|
self::getCidJoinSub(),
|
|
'pronto_atendimento_atendimento_cid',
|
|
function ($join) {
|
|
$join->on('pronto_atendimento_atendimento_cid.atendimento_id', '=', 'pronto_atendimento_atendimento.id');
|
|
}
|
|
)
|
|
->where('pronto_atendimento_atendimento_cid.cid_id', $params['cid_id']);
|
|
}
|
|
|
|
if (!is_null($idadeDe)) {
|
|
|
|
$unidades->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) >= ?", [$idadeDe]);
|
|
}
|
|
|
|
if (!is_null($idadeAte)) {
|
|
|
|
$unidades->whereRaw("(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE)) <= ?", [$idadeAte]);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'paciente_id')) {
|
|
|
|
$unidades->where('cadastro_paciente.id', $params['paciente_id']);
|
|
}
|
|
|
|
if (isset($params['prontuario'])) {
|
|
|
|
$unidades->where('cadastro_paciente.prontuario', (int)$params['prontuario']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'sexo_id')) {
|
|
|
|
$unidades->where('cadastro_cidadao.sexo_id', $params['sexo_id']);
|
|
}
|
|
|
|
if (self::hasParamId($params, 'classificacao_paciente_id')) {
|
|
|
|
$unidades->where('cadastro_paciente.classificacao_id', $params['classificacao_paciente_id']);
|
|
}
|
|
|
|
return $unidades->orderBy('cadastro_unidade.nome_fantasia')->get();
|
|
}
|
|
|
|
private static function getJoinLastClassificacaoRiscoClosure($join)
|
|
{
|
|
return function ($join) {
|
|
|
|
$join->on(
|
|
'pronto_atendimento_atendimento.id',
|
|
'=',
|
|
'pronto_atendimento_atendimento_classificacao_risco.atendimento_id'
|
|
)
|
|
->where(
|
|
'pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id',
|
|
self::getLastClassificacaoRiscoIdClosure($join)
|
|
);
|
|
};
|
|
}
|
|
|
|
private static function getLastClassificacaoRiscoIdClosure($query)
|
|
{
|
|
return function ($query) {
|
|
|
|
$query->select(
|
|
'pronto_atendimento_atendimento_classificacao_risco.classificacao_risco_id'
|
|
)
|
|
->from('pronto_atendimento_atendimento_classificacao_risco')
|
|
->whereColumn(
|
|
'pronto_atendimento_atendimento_classificacao_risco.atendimento_id',
|
|
'pronto_atendimento_atendimento.id'
|
|
)
|
|
->orderByDesc('pronto_atendimento_atendimento_classificacao_risco.created_at')
|
|
->limit(1);
|
|
};
|
|
}
|
|
|
|
private static function hasParamId($params, $param)
|
|
{
|
|
return isset($params[$param]) && $params[$param] > 0;
|
|
}
|
|
|
|
private static function getCidJoinSub() {
|
|
return DB::table('pronto_atendimento_atendimento_cid')
|
|
->select(
|
|
'pronto_atendimento_atendimento_cid.cid_id',
|
|
'pronto_atendimento_atendimento_cid.atendimento_id',
|
|
)
|
|
->union(
|
|
DB::table('pronto_atendimento_atendimento_procedimento')
|
|
->select(
|
|
'pronto_atendimento_atendimento_procedimento.cid_principal_id AS cid_id',
|
|
'pronto_atendimento_atendimento_procedimento.atendimento_id'
|
|
)
|
|
->where('pronto_atendimento_atendimento_procedimento.is_parametrizado', true)
|
|
);
|
|
}
|
|
|
|
private static function addWhereRawDayOfWeek($diaSemana, $query)
|
|
{
|
|
$diaSemana += 1;
|
|
|
|
$query->whereRaw("
|
|
DAYOFWEEK(pronto_atendimento_faa.created_at) = {$diaSemana}
|
|
");
|
|
}
|
|
|
|
private static function getModel()
|
|
{
|
|
return [
|
|
'periodo' => [
|
|
'date' => [
|
|
'start' => null,
|
|
'end' => null,
|
|
],
|
|
'time' => [
|
|
'start' => null,
|
|
'end' => null,
|
|
]
|
|
],
|
|
'dia_semana' => null,
|
|
'unidade_id' => null,
|
|
'local_atendimento_id' => null,
|
|
'classificacao_risco_id' => null,
|
|
'desfecho_id' => null,
|
|
'especialidade_id' => null,
|
|
'profissional_id' => null,
|
|
'status' => null,
|
|
'cid_id' => null,
|
|
'paciente_id' => null,
|
|
'paciente' => null,
|
|
'prontuario' => null,
|
|
'sexo_id' => null,
|
|
'classificacao_paciente_id' => null,
|
|
'unidade_referencia_id' => null,
|
|
'idade_de' => null,
|
|
'idade_ate' => null,
|
|
'fator_risco_cancer' => null,
|
|
'fator_risco_diabetes' => null,
|
|
'fator_risco_etilismo' => null,
|
|
'fator_risco_gestante' => null,
|
|
'fator_risco_hipertensao' => null,
|
|
'fator_risco_obesidade' => null,
|
|
'fator_risco_perfil_sanguineo_alterado' => null,
|
|
'fator_risco_radioterapia' => null,
|
|
'fator_risco_tabagismo' => null,
|
|
'fator_risco_uso_medicamentos_antineoplasicos_imunossupressores' => null,
|
|
'fator_risco_outro' => null,
|
|
'faa' => null,
|
|
];
|
|
}
|
|
}
|