118 lines
5.0 KiB
PHP
118 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\ProntoAtendimento\RegularizacaoCadastro;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\Classificacao;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Models\Cadastro\Parametrizacao\ParametrizacaoPermissaoUsuario;
|
|
use App\Models\Cadastro\SujeitoAtencao\PacienteClassificacao;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RegularizacaoCadastroController extends Controller
|
|
{
|
|
protected $modulo = 'pronto-atendimento';
|
|
protected $funcionalidade = 'regularizacao-cadastro';
|
|
|
|
public function datatable()
|
|
{
|
|
$atendimentos = DB::table('cadastro_paciente as paciente')
|
|
->select([
|
|
'cidadao.caracteristicas_fisicas',
|
|
'cidadao.nome',
|
|
'faa.id as faa_id',
|
|
'faa.numero as faa_numero',
|
|
'paciente.id as id',
|
|
'paciente.prontuario',
|
|
'paciente.classificacao_id',
|
|
'paciente.sequencial_desconhecido',
|
|
'sexo.descricao as sexo',
|
|
DB::raw('DATE_FORMAT(COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em), "%d/%m/%Y") as data'),
|
|
DB::raw('IF (cidadao.data_nascimento IS NULL, "Não informada", DATE_FORMAT(cidadao.data_nascimento, "%d/%m/%Y")) as data_nascimento'),
|
|
DB::raw('IF (cidadao.nome_mae IS NULL, "Não informado", cidadao.nome_mae) as nome_mae'),
|
|
])
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->join('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'paciente.created_centro_custo_id')
|
|
->leftJoin('pronto_atendimento_faa as faa', 'faa.paciente_id', 'paciente.id')
|
|
->where('centro_custo.infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id)
|
|
->where(function ($query) {
|
|
$query->whereNotNull('paciente.sem_documentos_em')
|
|
->orWhereNotNull('paciente.sequencial_desconhecido_em');
|
|
})
|
|
->whereNull('paciente.deleted_at');
|
|
|
|
if (!request()->get('order')) {
|
|
$atendimentos->orderByRaw('COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em) DESC');
|
|
}
|
|
|
|
return datatables()
|
|
->of($atendimentos)
|
|
->filterColumn('data', function ($query, $keyword) {
|
|
$query->whereRaw("DATE_FORMAT(COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em), '%d/%m/%Y') like ?", ["%{$keyword}%"])
|
|
->orWhereRaw(
|
|
"CONCAT(
|
|
SUBSTR(COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em),9,2),
|
|
SUBSTR(COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em),6,2),
|
|
SUBSTR(COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em),1,4)
|
|
) like ?",
|
|
["%{$keyword}%"]
|
|
);
|
|
})
|
|
->orderColumn('data', function ($query, $order) {
|
|
$query->orderByRaw('COALESCE(paciente.sem_documentos_em, paciente.sequencial_desconhecido_em) ' . $order);
|
|
})
|
|
->skipTotalRecords()
|
|
->make(true);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$lotacao = $this->getCurrentLotacao();
|
|
|
|
$permissaoUsuario = ParametrizacaoPermissaoUsuario::where('usuario_id', $lotacao->usuario_id)
|
|
->where('infraestrutura_id', $lotacao->infraestrutura_id)
|
|
->first();
|
|
|
|
$countUsuario = ParametrizacaoPermissaoUsuario::count();
|
|
|
|
$classificacoes = $permissaoUsuario
|
|
? PacienteClassificacao::orderBy('descricao')
|
|
->get()
|
|
->filter(function ($classificacao) use ($permissaoUsuario) {
|
|
|
|
$exibe = false;
|
|
|
|
switch ($classificacao->id) {
|
|
case Classificacao::MUNICIPE:
|
|
$exibe = $permissaoUsuario->municipe;
|
|
break;
|
|
case Classificacao::PROVISORIO:
|
|
$exibe = $permissaoUsuario->provisorio;
|
|
break;
|
|
case Classificacao::OUTRO_MUNICIPIO:
|
|
$exibe = $permissaoUsuario->outro_municipio;
|
|
break;
|
|
}
|
|
|
|
return $exibe ? [
|
|
'id' => $classificacao->id,
|
|
'descricao' => $classificacao->descricao
|
|
] : null;
|
|
})
|
|
->values()
|
|
: [];
|
|
|
|
return [
|
|
'classificacoes' => $classificacoes,
|
|
'is_permissao_cadastro' => $countUsuario === 0 ? true : false,
|
|
];
|
|
}
|
|
|
|
private function getCentroCustoLogado()
|
|
{
|
|
return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id'));
|
|
}
|
|
}
|