201 lines
8.3 KiB
PHP
201 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Bpa;
|
|
|
|
use App\Enums\Sigtap\Registro;
|
|
use App\Models\Bpa\Inconsistencia;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\Sigtap\Competencia;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RelatorioInconsistenciaService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
switch (request()->query('registro_codigo')) {
|
|
case Registro::BPA_CONSOLIDADO:
|
|
return self::getProcedimentosConsolidados(request()->query());
|
|
break;
|
|
|
|
case Registro::BPA_INDIVIDUALIZADO:
|
|
return self::getProcedimentosIndividualizados(request()->query());
|
|
break;
|
|
|
|
default:
|
|
return self::get(Inconsistencia::class)->select(['codigo', 'descricao'])
|
|
->whereHas('procedimentos', function($query) {
|
|
$query->where([
|
|
'bpa_procedimento.origem' => request()->query('origem'),
|
|
'bpa_procedimento.unidade_id' => request()->query('unidade_id'),
|
|
'bpa_procedimento.competencia_id' => request()->query('competencia_id'),
|
|
'bpa_procedimento.paciente_id' => request()->query('paciente_id'),
|
|
'bpa_procedimento.procedimento_id' => request()->query('procedimento_id'),
|
|
'bpa_procedimento.ocupacao_id' => request()->query('ocupacao_id'),
|
|
]);
|
|
})
|
|
->get();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
return [
|
|
'form' => self::getForm(),
|
|
'model' => self::getModel(),
|
|
];
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
return self::get(Inconsistencia::class)->select(['codigo', 'descricao'])
|
|
->whereHas('procedimentos', function($query) use ($id) {
|
|
$query->where('id', $id);
|
|
})
|
|
->get();
|
|
}
|
|
|
|
private static function getUnidades()
|
|
{
|
|
return Unidade::orderBy('nome_fantasia')
|
|
->where('cnes', '<>', '')
|
|
->get()
|
|
->map(function($query) {
|
|
return [
|
|
'id' => $query->id,
|
|
'nome_fantasia' => $query->nome_fantasia,
|
|
];
|
|
});
|
|
}
|
|
|
|
private static function getCompetencias()
|
|
{
|
|
return Competencia::select('id', 'valor')
|
|
->orderBy('valor', 'desc')
|
|
->take(4)
|
|
->get();
|
|
}
|
|
|
|
private static function getForm()
|
|
{
|
|
return [
|
|
'unidades' => self::getUnidades(),
|
|
'competencias' => self::getCompetencias(),
|
|
'instrumentos_registro' => [
|
|
['codigo' => Registro::BPA_CONSOLIDADO, 'nome' => 'BPA - Consolidado'],
|
|
['codigo' => Registro::BPA_INDIVIDUALIZADO, 'nome' => 'BPA - Individualizado'],
|
|
],
|
|
];
|
|
}
|
|
|
|
private static function getModel()
|
|
{
|
|
return [
|
|
'unidade_id' => null,
|
|
'competencia_id' => null,
|
|
'registro_codigo' => null,
|
|
];
|
|
}
|
|
|
|
private static function getProcedimentosConsolidados($params)
|
|
{
|
|
return DB::table('bpa_procedimento')
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'bpa_procedimento.unidade_id')
|
|
->join('sigtap.competencia', 'competencia.id', 'bpa_procedimento.competencia_id')
|
|
->join('sigtap.procedimento', 'procedimento.id', 'bpa_procedimento.procedimento_id')
|
|
->join('sigtap.ocupacao', 'ocupacao.id', 'bpa_procedimento.ocupacao_id')
|
|
->join('sigtap.registro', 'registro.id', 'bpa_procedimento.registro_id')
|
|
->select([
|
|
'bpa_procedimento.id',
|
|
'bpa_procedimento.origem',
|
|
'cadastro_unidade.id AS unidade_id',
|
|
'cadastro_unidade.nome_fantasia AS unidade_nome',
|
|
'competencia.id AS competencia_id',
|
|
'competencia.valor AS competencia_valor',
|
|
'procedimento.id AS procedimento_id',
|
|
'procedimento.codigo AS procedimento_codigo',
|
|
'procedimento.nome AS procedimento_nome',
|
|
'registro.id AS registro_id',
|
|
'registro.codigo AS registro_codigo',
|
|
'ocupacao.id AS ocupacao_id',
|
|
'ocupacao.codigo AS ocupacao_codigo',
|
|
'ocupacao.nome AS ocupacao_nome',
|
|
'bpa_procedimento.idade',
|
|
DB::raw('SUM(bpa_procedimento.quantidade) AS quantidade'),
|
|
])
|
|
->whereNull('bpa_procedimento.deleted_at')
|
|
->where([
|
|
'bpa_procedimento.unidade_id' => $params['unidade_id'],
|
|
'bpa_procedimento.competencia_id' => $params['competencia_id'],
|
|
])
|
|
->where('registro.codigo', Registro::BPA_CONSOLIDADO)
|
|
->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('bpa_procedimento_inconsistencia')
|
|
->whereColumn('bpa_procedimento_inconsistencia.procedimento_id', 'bpa_procedimento.id');
|
|
})
|
|
->groupBy([
|
|
'bpa_procedimento.origem',
|
|
'cadastro_unidade.id',
|
|
'cadastro_unidade.nome_fantasia',
|
|
'competencia.valor',
|
|
'procedimento.id',
|
|
'procedimento.codigo',
|
|
'procedimento.nome',
|
|
'registro.id',
|
|
'registro.codigo',
|
|
'ocupacao.id',
|
|
'ocupacao.codigo',
|
|
'ocupacao.nome',
|
|
'bpa_procedimento.idade',
|
|
])
|
|
->get();
|
|
}
|
|
|
|
private static function getProcedimentosIndividualizados($params)
|
|
{
|
|
return DB::table('bpa_procedimento')
|
|
->select([
|
|
'bpa_procedimento.origem',
|
|
'cadastro_unidade.id AS unidade_id',
|
|
'cadastro_unidade.nome_fantasia AS unidade_nome',
|
|
'competencia.id AS competencia_id',
|
|
'competencia.valor AS competencia_valor',
|
|
'cadastro_cidadao_usuario.nome AS usuario_nome',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'cadastro_cidadao_paciente.nome AS paciente_nome',
|
|
'procedimento.id AS procedimento_id',
|
|
'procedimento.codigo AS procedimento_codigo',
|
|
'procedimento.nome AS procedimento_nome',
|
|
'ocupacao.id AS ocupacao_id',
|
|
'ocupacao.codigo AS ocupacao_codigo',
|
|
'ocupacao.nome AS ocupacao_nome',
|
|
'bpa_procedimento.quantidade',
|
|
'registro.codigo AS registro_codigo',
|
|
DB::raw("DATE_FORMAT(bpa_procedimento.data_atendimento, '%d/%m/%Y') AS data_atendimento"),
|
|
])
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'bpa_procedimento.unidade_id')
|
|
->join('sigtap.competencia', 'competencia.id', 'bpa_procedimento.competencia_id')
|
|
->join('cadastro_usuario', 'cadastro_usuario.id', 'bpa_procedimento.usuario_id')
|
|
->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'bpa_procedimento.paciente_id')
|
|
->join('cadastro_cidadao AS cadastro_cidadao_paciente', 'cadastro_cidadao_paciente.id', 'cadastro_paciente.cidadao_id')
|
|
->join('sigtap.procedimento', 'procedimento.id', 'bpa_procedimento.procedimento_id')
|
|
->join('sigtap.registro', 'registro.id', 'bpa_procedimento.registro_id')
|
|
->join('sigtap.ocupacao', 'ocupacao.id', 'bpa_procedimento.ocupacao_id')
|
|
->whereNull('bpa_procedimento.deleted_at')
|
|
->where([
|
|
'bpa_procedimento.unidade_id' => $params['unidade_id'],
|
|
'bpa_procedimento.competencia_id' => $params['competencia_id'],
|
|
])
|
|
->where('registro.codigo', Registro::BPA_INDIVIDUALIZADO)
|
|
->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('bpa_procedimento_inconsistencia')
|
|
->whereColumn('bpa_procedimento_inconsistencia.procedimento_id', 'bpa_procedimento.id');
|
|
})
|
|
->get();
|
|
}
|
|
}
|