This commit is contained in:
295
api/app/Models/Cadastro/SujeitoAtencao/Paciente.php
Normal file
295
api/app/Models/Cadastro/SujeitoAtencao/Paciente.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Cadastro\SujeitoAtencao;
|
||||
|
||||
use App\Exceptions\Model\DeletingModelException;
|
||||
use App\Models\Ambulatorio\Agendamento as AmbulatorioAgendamento;
|
||||
use App\Models\Ambulatorio\DemandaEspontanea;
|
||||
use App\Models\AtencaoBasica\CadastroIndividual\CadastroIndividual;
|
||||
use App\Models\AtencaoBasica\Parametrizacao\Equipe;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\PacienteCondicaoSaude;
|
||||
use App\Models\Cadastro\SujeitoAtencao\PacienteInformacaoResidencial;
|
||||
use App\Models\Cadastro\SujeitoAtencao\PacienteInformacaoSocial;
|
||||
use App\Models\ExameImagem\Agendamento\Agendamento as ExameImagemAgendamento;
|
||||
use App\Models\ExameImagem\Agendamento\AgendamentoExame;
|
||||
use App\Models\ExameImagem\Jornada\Jornada;
|
||||
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento as ExameLaboratorialAgendamento;
|
||||
use App\Models\Farmacia\AltoCusto\Processo;
|
||||
use App\Models\Model;
|
||||
use App\Models\ProntoAtendimento\Atendimento\Faa;
|
||||
use Dyrynda\Database\Support\CascadeSoftDeletes;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||
|
||||
|
||||
class Paciente extends Authenticatable implements JWTSubject
|
||||
{
|
||||
use SoftDeletes, CascadeSoftDeletes;
|
||||
|
||||
protected $table = 'cadastro_paciente';
|
||||
|
||||
protected $casts = [
|
||||
'sequencial_desconhecido_em' => 'datetime:Y-m-d H:i:s',
|
||||
'sem_documentos_em' => 'datetime:Y-m-d H:i:s',
|
||||
'validade_inicio' => 'datetime:Y-m-d',
|
||||
'validade_fim' => 'datetime:Y-m-d',
|
||||
'data_ultima_menstruacao' => 'datetime:Y-m-d',
|
||||
'bloqueado' => 'boolean',
|
||||
];
|
||||
|
||||
protected $cascadeDeletes = [
|
||||
'prontuariosUnidade',
|
||||
'historicoClassificacoes'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'cidadao_id',
|
||||
'classificacao_id',
|
||||
'subclassificacao_id',
|
||||
'validade_inicio',
|
||||
'validade_fim',
|
||||
'unidade_id',
|
||||
'email',
|
||||
'sequencial_desconhecido',
|
||||
'sequencial_desconhecido_em',
|
||||
'sem_documentos_em',
|
||||
'prontuario',
|
||||
'observacao',
|
||||
'protocolo_cross',
|
||||
'bloqueado',
|
||||
'senha',
|
||||
];
|
||||
|
||||
|
||||
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',
|
||||
'cidadao_id',
|
||||
];
|
||||
|
||||
protected $with = [
|
||||
'cidadao',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'prontuario_unidade',
|
||||
'is_desconhecido',
|
||||
];
|
||||
|
||||
public function cidadao()
|
||||
{
|
||||
return $this->belongsTo(Cidadao::class);
|
||||
}
|
||||
|
||||
public function jornadasExameImagem()
|
||||
{
|
||||
return $this->hasMany(Jornada::class);
|
||||
}
|
||||
|
||||
public function classificacao()
|
||||
{
|
||||
return $this->belongsTo(PacienteClassificacao::class);
|
||||
}
|
||||
|
||||
public function subclassificacao()
|
||||
{
|
||||
return $this->belongsTo(PacienteSubclassificacao::class);
|
||||
}
|
||||
|
||||
public function unidade()
|
||||
{
|
||||
return $this->belongsTo(Unidade::class);
|
||||
}
|
||||
|
||||
public function condicaoSaude()
|
||||
{
|
||||
return $this->hasOne(PacienteCondicaoSaude::class);
|
||||
}
|
||||
|
||||
public function informacaoSocial()
|
||||
{
|
||||
return $this->hasOne(PacienteInformacaoSocial::class);
|
||||
}
|
||||
|
||||
public function informacaoResidencial()
|
||||
{
|
||||
return $this->hasOne(PacienteInformacaoResidencial::class);
|
||||
}
|
||||
|
||||
public function responsavel()
|
||||
{
|
||||
return $this->hasOne(PacienteResponsavel::class);
|
||||
}
|
||||
|
||||
public function dum()
|
||||
{
|
||||
return $this->hasOne(PacienteDum::class);
|
||||
}
|
||||
|
||||
public function prontuariosUnidade()
|
||||
{
|
||||
return $this->hasMany(ProntuarioUnidade::class);
|
||||
}
|
||||
|
||||
public function historicoClassificacoes()
|
||||
{
|
||||
return $this->hasMany(PacienteClassificacaoHistorico::class);
|
||||
}
|
||||
|
||||
public function gestacoes()
|
||||
{
|
||||
return $this->hasMany(PacienteGestacao::class);
|
||||
}
|
||||
|
||||
public function faas()
|
||||
{
|
||||
return $this->hasMany(Faa::class);
|
||||
}
|
||||
|
||||
public function cadastroIndividual()
|
||||
{
|
||||
return $this->hasOne(CadastroIndividual::class);
|
||||
}
|
||||
|
||||
public function agendamento()
|
||||
{
|
||||
return $this->hasMany(ExameLaboratorialAgendamento::class);
|
||||
}
|
||||
|
||||
public function agendamentosExameImagem()
|
||||
{
|
||||
return $this->hasMany(ExameImagemAgendamento::class);
|
||||
}
|
||||
|
||||
public function farmaciaAltoCustoProcessos()
|
||||
{
|
||||
return $this->hasMany(Processo::class);
|
||||
}
|
||||
|
||||
public function agendamentoExame()
|
||||
{
|
||||
return $this->belongsTo(AgendamentoExame::class);
|
||||
}
|
||||
|
||||
public function ambulatorioAgendamentos()
|
||||
{
|
||||
return $this->hasMany(AmbulatorioAgendamento::class);
|
||||
}
|
||||
|
||||
public function ambulatorioDemandasEspontaneas()
|
||||
{
|
||||
return $this->hasMany(DemandaEspontanea::class);
|
||||
}
|
||||
|
||||
public function getProntuarioAttribute($value)
|
||||
{
|
||||
return Str::padLeft($value, 6, '0');
|
||||
}
|
||||
|
||||
public function getProntuarioUnidadeAttribute()
|
||||
{
|
||||
$prontuarioUnidade = $this->prontuariosUnidade()->where('unidade_id', $this->unidade_id)->first();
|
||||
|
||||
return $prontuarioUnidade->prontuario ?? null;
|
||||
}
|
||||
|
||||
public function getIsDesconhecidoAttribute()
|
||||
{
|
||||
return $this->sequencial_desconhecido_em ? true : false;
|
||||
}
|
||||
|
||||
public function getIsAllowedToDeleteAttribute()
|
||||
{
|
||||
return $this->hasNoOne([
|
||||
'faas',
|
||||
'ambulatorioAgendamentos',
|
||||
'ambulatorioDemandasEspontaneas'
|
||||
]);
|
||||
}
|
||||
|
||||
private function hasNoOne(array $relations) : bool
|
||||
{
|
||||
foreach ($relations as $relation) {
|
||||
|
||||
if (!is_null($this->{$relation}) && $this->{$relation}()->get()->isNotEmpty()) {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
|
||||
if (auth()->check()) {
|
||||
|
||||
$model->created_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->created_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
|
||||
$model->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
}
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
|
||||
if (auth()->check()) {
|
||||
|
||||
$model->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
}
|
||||
});
|
||||
|
||||
static::deleting(function ($model) {
|
||||
|
||||
if (!$model->isAllowedToDelete) {
|
||||
|
||||
throw new DeletingModelException();
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
|
||||
if ($model->trashed() && auth()->check()) {
|
||||
|
||||
$model->deleted_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->deleted_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
|
||||
$model->save();
|
||||
}
|
||||
});
|
||||
|
||||
static::restored(function ($model) {
|
||||
|
||||
$model->deleted_lotacao_id = null;
|
||||
$model->deleted_centro_custo_id = null;
|
||||
|
||||
$model->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function getJWTIdentifier()
|
||||
{
|
||||
return $this->getKey();
|
||||
}
|
||||
|
||||
public function getJWTCustomClaims()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user