This commit is contained in:
400
api/app/Models/Cadastro/SujeitoAtencao/Cidadao.php
Normal file
400
api/app/Models/Cadastro/SujeitoAtencao/Cidadao.php
Normal file
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Cadastro\SujeitoAtencao;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\Nacionalidade as EnumNacionalidade;
|
||||
use App\Enums\Cadastro\SujeitoAtencao\RacaCor as EnumRacaCor;
|
||||
use App\Enums\Intranet\Pais as IntranetPais;
|
||||
use App\Models\Cadastro\Localizacao\Domicilio;
|
||||
use App\Models\Cadastro\Medicamento\Medicamento;
|
||||
use App\Models\Cadastro\SujeitoAtencao\DoencaCronica;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Nacionalidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\RacaCor;
|
||||
use App\Models\Intranet\Estado;
|
||||
use App\Models\Intranet\Municipio;
|
||||
use App\Models\Intranet\Pais;
|
||||
use App\Models\Model;
|
||||
use App\Services\Geral\AgeService;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Cidadao extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'cadastro_cidadao';
|
||||
|
||||
protected $casts = [
|
||||
'frequenta_escola' => 'boolean',
|
||||
'data_nascimento' => 'datetime:Y-m-d',
|
||||
'data_entrada_domicilio' => 'datetime:Y-m-d',
|
||||
'data_naturalizacao' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'cns',
|
||||
'cpf',
|
||||
'dnv',
|
||||
'motivo_duplicidade_cpf_id',
|
||||
'motivo_duplicidade_cpf_outro',
|
||||
'nome',
|
||||
'nome_social',
|
||||
'preferencia_nome_id',
|
||||
'data_nascimento',
|
||||
'sexo_id',
|
||||
'identidade_genero_id',
|
||||
'estado_civil_id',
|
||||
'conjuge',
|
||||
'situacao_familiar_conjugal_id',
|
||||
'raca_cor_id',
|
||||
'etnia_indigena_id',
|
||||
'tipo_sanguineo_id',
|
||||
'deficiencia_id',
|
||||
'deficiencia_outro',
|
||||
'mobilidade_reduzida_id',
|
||||
'nome_mae',
|
||||
'nome_pai',
|
||||
'local_nascimento_id',
|
||||
'pais_nascimento_id',
|
||||
'nacionalidade_id',
|
||||
'data_naturalizacao',
|
||||
'portaria_naturalizacao',
|
||||
'estado_nascimento_id',
|
||||
'municipio_nascimento_id',
|
||||
'frequenta_escola',
|
||||
'escolaridade_id',
|
||||
'data_entrada_domicilio',
|
||||
'domicilio_id',
|
||||
'caracteristicas_fisicas',
|
||||
'fator_risco_outro',
|
||||
'cd_usuario_sistema_v1',
|
||||
'cd_sus_complemento_v1',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'created_at',
|
||||
'created_lotacao_id',
|
||||
'created_centro_custo_id',
|
||||
'updated_at',
|
||||
'updated_lotacao_id',
|
||||
'updated_centro_custo_id',
|
||||
'deleted_at',
|
||||
'deleted_lotacao_id',
|
||||
'deleted_centro_custo_id',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'telefone_principal',
|
||||
'desconhece_nome_mae',
|
||||
'desconhece_nome_pai',
|
||||
'cpf_nao_pertence_ao_cidadao',
|
||||
'idade',
|
||||
];
|
||||
|
||||
protected $with = [
|
||||
'carteiraIdentidade',
|
||||
'carteiraHabilitacao',
|
||||
'carteiraTrabalho',
|
||||
'estrangeiroEntradaBrasil',
|
||||
'tituloEleitor',
|
||||
'certidao',
|
||||
'telefones',
|
||||
'conveniosMedico',
|
||||
'domicilio',
|
||||
'fatoresRisco',
|
||||
'alergias',
|
||||
'obito',
|
||||
'sexo',
|
||||
];
|
||||
|
||||
public function motivoDuplicidadeCpf()
|
||||
{
|
||||
return $this->belongsTo(MotivoDuplicidadeCpf::class);
|
||||
}
|
||||
|
||||
public function preferenciaNome()
|
||||
{
|
||||
return $this->belongsTo(PreferenciaNome::class);
|
||||
}
|
||||
|
||||
public function sexo()
|
||||
{
|
||||
return $this->belongsTo(Sexo::class);
|
||||
}
|
||||
|
||||
public function doencasCronicas()
|
||||
{
|
||||
return $this->hasMany(DoencaCronica::class);
|
||||
}
|
||||
|
||||
public function identidadeGenero()
|
||||
{
|
||||
return $this->belongsTo(IdentidadeGenero::class);
|
||||
}
|
||||
|
||||
public function estadoCivil()
|
||||
{
|
||||
return $this->belongsTo(EstadoCivil::class);
|
||||
}
|
||||
|
||||
public function situacaoFamiliarConjugal()
|
||||
{
|
||||
return $this->belongsTo(SituacaoFamiliarConjugal::class);
|
||||
}
|
||||
|
||||
public function racaCor()
|
||||
{
|
||||
return $this->belongsTo(RacaCor::class);
|
||||
}
|
||||
|
||||
public function etniaIndigena()
|
||||
{
|
||||
return $this->belongsTo(EtniaIndigena::class);
|
||||
}
|
||||
|
||||
public function tipoSanguineo()
|
||||
{
|
||||
return $this->belongsTo(TipoSanguineo::class);
|
||||
}
|
||||
|
||||
public function deficiencia()
|
||||
{
|
||||
return $this->belongsTo(Deficiencia::class);
|
||||
}
|
||||
|
||||
public function mobilidadeReduzida()
|
||||
{
|
||||
return $this->belongsTo(MobilidadeReduzida::class);
|
||||
}
|
||||
|
||||
public function localNascimento()
|
||||
{
|
||||
return $this->belongsTo(LocalNascimento::class);
|
||||
}
|
||||
|
||||
public function paisNascimento()
|
||||
{
|
||||
return $this->belongsTo(Pais::class);
|
||||
}
|
||||
|
||||
public function nacionalidade()
|
||||
{
|
||||
return $this->belongsTo(Nacionalidade::class);
|
||||
}
|
||||
|
||||
public function estadoNascimento()
|
||||
{
|
||||
return $this->belongsTo(Estado::class);
|
||||
}
|
||||
|
||||
public function municipioNascimento()
|
||||
{
|
||||
return $this->belongsTo(Municipio::class);
|
||||
}
|
||||
|
||||
public function escolaridade()
|
||||
{
|
||||
return $this->belongsTo(Escolaridade::class);
|
||||
}
|
||||
|
||||
public function domicilio()
|
||||
{
|
||||
return $this->belongsTo(Domicilio::class);
|
||||
}
|
||||
|
||||
public function conveniosMedico()
|
||||
{
|
||||
return $this->belongsToMany(ConvenioMedico::class, 'cadastro_cidadao_convenio_medico')
|
||||
->using(CidadaoConvenioMedico::class)
|
||||
->as('cidadaoConvenioMedico')
|
||||
->withTimestamps()
|
||||
->orderBy('nome');
|
||||
}
|
||||
|
||||
public function alergias()
|
||||
{
|
||||
return $this->belongsToMany(Alergia::class, 'cadastro_cidadao_alergia')
|
||||
->with('tipo')
|
||||
->using(CidadaoAlergia::class)
|
||||
->as('cidadaoAlergia')
|
||||
->withPivot('created_lotacao_id', 'created_centro_custo_id', 'criticidade_id', 'reacoes_observacoes', 'cid_id')
|
||||
->withTimestamps()
|
||||
->orderBy('agente_substancia_especifica');
|
||||
}
|
||||
|
||||
public function medicamentosUsoContinuo()
|
||||
{
|
||||
return $this->belongsToMany(Medicamento::class, 'cadastro_cidadao_medicamento_uso_continuo')
|
||||
->using(CidadaoMedicamentoUsoContinuo::class)
|
||||
->as('CidadaoMedicamentoUsoContinuo')
|
||||
->withPivot(
|
||||
'created_lotacao_id',
|
||||
'created_centro_custo_id',
|
||||
)
|
||||
->withTimestamps()
|
||||
->orderBy('cadastro_medicamento.nome');
|
||||
}
|
||||
|
||||
public function fatoresRisco()
|
||||
{
|
||||
return $this->belongsToMany(FatorRisco::class, 'cadastro_cidadao_fator_risco')
|
||||
->using(CidadaoFatorRisco::class)
|
||||
->as('cidadaoFatorRisco')
|
||||
->withPivot('created_lotacao_id', 'created_centro_custo_id')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function obito()
|
||||
{
|
||||
return $this->hasOne(Obito::class);
|
||||
}
|
||||
|
||||
public function telefones()
|
||||
{
|
||||
return $this->hasMany(CidadaoTelefone::class);
|
||||
}
|
||||
|
||||
public function carteiraIdentidade()
|
||||
{
|
||||
return $this->hasOne(CarteiraIdentidade::class);
|
||||
}
|
||||
|
||||
public function carteiraHabilitacao()
|
||||
{
|
||||
return $this->hasOne(CarteiraHabilitacao::class);
|
||||
}
|
||||
|
||||
public function carteiraTrabalho()
|
||||
{
|
||||
return $this->hasOne(CarteiraTrabalho::class);
|
||||
}
|
||||
|
||||
public function estrangeiroEntradaBrasil()
|
||||
{
|
||||
return $this->hasOne(EstrangeiroEntradaBrasil::class);
|
||||
}
|
||||
|
||||
public function tituloEleitor()
|
||||
{
|
||||
return $this->hasOne(TituloEleitor::class);
|
||||
}
|
||||
|
||||
public function certidao()
|
||||
{
|
||||
return $this->hasOne(Certidao::class);
|
||||
}
|
||||
|
||||
public function paciente()
|
||||
{
|
||||
return $this->hasOne(Paciente::class);
|
||||
}
|
||||
|
||||
public function usuario()
|
||||
{
|
||||
return $this->hasOne(Usuario::class);
|
||||
}
|
||||
|
||||
public function getTelefonePrincipalAttribute()
|
||||
{
|
||||
return $this->telefones()->where('principal', true)->first();
|
||||
}
|
||||
|
||||
public function getDesconheceNomeMaeAttribute()
|
||||
{
|
||||
return is_null($this->nome_mae);
|
||||
}
|
||||
|
||||
public function getDesconheceNomePaiAttribute()
|
||||
{
|
||||
return is_null($this->nome_pai);
|
||||
}
|
||||
|
||||
public function getCpfNaoPertenceAoCidadaoAttribute()
|
||||
{
|
||||
return !is_null($this->motivo_duplicidade_cpf_id);
|
||||
}
|
||||
|
||||
public function getIdadeAttribute()
|
||||
{
|
||||
if (!$this->data_nascimento) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$age = AgeService::getAge($this->data_nascimento);
|
||||
|
||||
return [
|
||||
'anos' => $age->years,
|
||||
'meses' => $age->months,
|
||||
'dias' => $age->days
|
||||
];
|
||||
}
|
||||
|
||||
public function getIdadeFormatadaAttribute()
|
||||
{
|
||||
if (is_null($this->idade)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$idade = $this->idade;
|
||||
|
||||
$anosText = $idade['anos'] > 1 ? 'anos' : 'ano';
|
||||
$mesesText = $idade['meses'] > 1 ? 'meses' : 'mês';
|
||||
$diasText = $idade['dias'] > 1 ? 'dias' : 'dia';
|
||||
|
||||
return "{$idade['anos']} {$anosText} e "
|
||||
. "{$idade['meses']} {$mesesText} e "
|
||||
. "{$idade['dias']} {$diasText}";
|
||||
}
|
||||
|
||||
public function getIsAllowedToDeleteAttribute()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function scopeDadosSemRecusaCadastro($query)
|
||||
{
|
||||
return $query
|
||||
->whereNotNull('nacionalidade_id')
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('pais_nascimento_id', IntranetPais::BRASIL)
|
||||
->whereNotNull('municipio_nascimento_id');
|
||||
})
|
||||
->orWhere('pais_nascimento_id', '!=', IntranetPais::BRASIL);
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->whereNotNull("{$this->table}.nome")
|
||||
->where("{$this->table}.nome", '!=', '');
|
||||
})
|
||||
->whereNotNull('raca_cor_id')
|
||||
->whereNotNull('sexo_id')
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where("{$this->table}.raca_cor_id", EnumRacaCor::INDIGENA)
|
||||
->whereNotNull("{$this->table}.etnia_indigena_id");
|
||||
})->orWhere("{$this->table}.raca_cor_id", '!=', EnumRacaCor::INDIGENA)
|
||||
->whereNull("{$this->table}.etnia_indigena_id");
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where("{$this->table}.nacionalidade_id", EnumNacionalidade::NATURALIZADO_BRASILEIRO)
|
||||
->where("{$this->table}.portaria_naturalizacao", '!=', '')
|
||||
->whereNotNull("{$this->table}.portaria_naturalizacao")
|
||||
->whereNotNull("{$this->table}.data_naturalizacao");
|
||||
})->orWhere("{$this->table}.nacionalidade_id", '!=', EnumNacionalidade::NATURALIZADO_BRASILEIRO)
|
||||
->whereNull("{$this->table}.data_naturalizacao");
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where("{$this->table}.cpf", '!=', '')
|
||||
->orWhere("{$this->table}.cns", '!=', '');
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where("{$this->table}.nacionalidade_id", '!=', EnumNacionalidade::ESTRANGEIRO)
|
||||
->whereHas('telefones');
|
||||
})->orWhere("{$this->table}.nacionalidade_id", EnumNacionalidade::ESTRANGEIRO);
|
||||
})
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user