This commit is contained in:
174
api/app/Models/Cadastro/SujeitoAtencao/Lotacao.php
Normal file
174
api/app/Models/Cadastro/SujeitoAtencao/Lotacao.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Cadastro\SujeitoAtencao;
|
||||
|
||||
use App\Models\Administracao\LotacaoPapel;
|
||||
use App\Models\Administracao\LotacaoPermissao;
|
||||
use App\Models\Administracao\Papel;
|
||||
use App\Models\Administracao\Permissao;
|
||||
use App\Models\Almoxarifado\Requisicao;
|
||||
use App\Models\Almoxarifado\SaidaItem;
|
||||
use App\Models\Ambulatorio\Jornada;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Model;
|
||||
use App\Models\ProntoAtendimento\Plantao\Plantao;
|
||||
use App\Models\Sigtap\Ocupacao;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Lotacao extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'cadastro_lotacao';
|
||||
|
||||
protected $casts = [
|
||||
'realiza_atendimento' => 'boolean',
|
||||
'principal' => 'boolean',
|
||||
'ativo' => 'boolean',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'infraestrutura_id',
|
||||
'ocupacao_id',
|
||||
'realiza_atendimento',
|
||||
'principal',
|
||||
'ativo',
|
||||
];
|
||||
|
||||
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',
|
||||
'pivot'
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'unidade_id'
|
||||
];
|
||||
|
||||
public function infraestrutura()
|
||||
{
|
||||
return $this->belongsTo(Infraestrutura::class);
|
||||
}
|
||||
|
||||
public function usuario()
|
||||
{
|
||||
return $this->belongsTo(Usuario::class);
|
||||
}
|
||||
|
||||
public function ocupacao()
|
||||
{
|
||||
return $this->belongsTo(Ocupacao::class);
|
||||
}
|
||||
|
||||
public function papeis()
|
||||
{
|
||||
return $this
|
||||
->belongsToMany(
|
||||
Papel::class,
|
||||
'administracao_lotacao_papel',
|
||||
'lotacao_id',
|
||||
'papel_id'
|
||||
)
|
||||
->using(LotacaoPapel::class)
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function permissoes()
|
||||
{
|
||||
return $this
|
||||
->belongsToMany(
|
||||
Permissao::class,
|
||||
'administracao_lotacao_permissao',
|
||||
'lotacao_id',
|
||||
'permissao_id'
|
||||
)
|
||||
->using(LotacaoPermissao::class)
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function plantoes()
|
||||
{
|
||||
return $this->hasMany(Plantao::class);
|
||||
}
|
||||
|
||||
public function almoxarifadoRequisicoesRequisitante()
|
||||
{
|
||||
return $this->hasMany(Requisicao::class, 'created_lotacao_id');
|
||||
}
|
||||
|
||||
public function almoxarifadoSaidaItens()
|
||||
{
|
||||
return $this->hasMany(SaidaItem::class, 'created_lotacao_id');
|
||||
}
|
||||
|
||||
public function ambulatorioJornadas()
|
||||
{
|
||||
return $this->hasMany(Jornada::class);
|
||||
}
|
||||
|
||||
public function centrosCusto()
|
||||
{
|
||||
return $this->belongsToMany(CentroCusto::class,'cadastro_lotacao_centro_custo', 'lotacao_id', 'centro_custo_id')
|
||||
->using(LotacaoCentroCusto::class)
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function getUnidadeIdAttribute()
|
||||
{
|
||||
return Infraestrutura::findOrFail($this->infraestrutura_id)->unidade_id;
|
||||
}
|
||||
|
||||
public function getIsAllowedToDeleteAttribute()
|
||||
{
|
||||
return $this->hasNoOne([
|
||||
'papeis',
|
||||
'permissoes',
|
||||
'plantoes',
|
||||
'almoxarifadoRequisicoesRequisitante',
|
||||
'almoxarifadoSaidaItens',
|
||||
'ambulatorioJornadas',
|
||||
]);
|
||||
}
|
||||
|
||||
public function hasPermissao(Permissao $permissao)
|
||||
{
|
||||
return $this->isAllowed($permissao) || $this->isParentAllowed($permissao);
|
||||
}
|
||||
|
||||
private function isParentAllowed(Permissao $permissao)
|
||||
{
|
||||
foreach (($permissao->funcionalidade->funcionalidadesPai ?? []) as $funcionalidadePai) {
|
||||
|
||||
foreach (($funcionalidadePai->permissoes ?? []) as $permissaoPai) {
|
||||
|
||||
if ($this->isAllowed($permissaoPai)) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isAllowed(Permissao $permissao)
|
||||
{
|
||||
foreach (($this->papeis ?? []) as $papel) {
|
||||
|
||||
if ($papel->permissoes->contains('id', $permissao->id)) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->permissoes->contains('id', $permissao->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user