196 lines
5.2 KiB
PHP
196 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Log\Operacao;
|
|
use App\Exceptions\Model\DeletingModelException;
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Log\Acao;
|
|
use App\Models\Regulacao\Solicitacao;
|
|
use App\Models\WebService\Usuario AS ExternoUsuario;
|
|
use Illuminate\Database\Eloquent\Model as BaseModel;
|
|
|
|
abstract class Model extends BaseModel
|
|
{
|
|
public function createdLotacao()
|
|
{
|
|
return $this->belongsTo(Lotacao::class);
|
|
}
|
|
|
|
public function createdCentroCusto()
|
|
{
|
|
return $this->belongsTo(CentroCusto::class);
|
|
}
|
|
|
|
public function externoCreatedUsuario()
|
|
{
|
|
return $this->belongsTo(ExternoUsuario::class);
|
|
}
|
|
|
|
public function updatedLotacao()
|
|
{
|
|
return $this->belongsTo(Lotacao::class);
|
|
}
|
|
|
|
public function updatedCentroCusto()
|
|
{
|
|
return $this->belongsTo(CentroCusto::class);
|
|
}
|
|
|
|
public function externoUpdatedUsuario()
|
|
{
|
|
return $this->belongsTo(ExternoUsuario::class);
|
|
}
|
|
|
|
public function deletedLotacao()
|
|
{
|
|
return $this->belongsTo(Lotacao::class);
|
|
}
|
|
|
|
public function deletedCentroCusto()
|
|
{
|
|
return $this->belongsTo(CentroCusto::class);
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
$usesSoftDelete = self::hasGlobalScope('Illuminate\Database\Eloquent\SoftDeletingScope');
|
|
|
|
static::creating(function ($model) {
|
|
|
|
if (auth()->guard('api')->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');
|
|
}
|
|
else if (auth()->guard('external')->check()) {
|
|
|
|
$model->externo_created_usuario_id = auth()->user()->id;
|
|
$model->externo_updated_usuario_id = auth()->user()->id;
|
|
}
|
|
});
|
|
|
|
static::created(function ($model) {
|
|
$namespace = get_class($model);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
else if ($namespace === Solicitacao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $model->getTable();
|
|
$json = $model->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::INCLUSAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
});
|
|
|
|
static::updating(function ($model) {
|
|
|
|
if (auth()->guard('api')->check()) {
|
|
|
|
$model->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
|
$model->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
|
}
|
|
else if (auth()->guard('external')->check()) {
|
|
|
|
$model->externo_updated_usuario_id = auth()->user()->id;
|
|
}
|
|
});
|
|
|
|
static::updated(function ($model) {
|
|
$namespace = get_class($model);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
else if ($namespace === Solicitacao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $model->getTable();
|
|
$json = $model->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::ATUALIZACAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
});
|
|
|
|
static::deleting(function ($model) use ($usesSoftDelete) {
|
|
|
|
if ($usesSoftDelete && !$model->isAllowedToDelete) {
|
|
|
|
throw new DeletingModelException;
|
|
}
|
|
});
|
|
|
|
static::deleted(function ($model) use ($usesSoftDelete) {
|
|
$namespace = get_class($model);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
else if ($namespace === Solicitacao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $model->getTable();
|
|
$json = $model->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::EXCLUSAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
|
|
if ($usesSoftDelete && $model->trashed() && auth()->guard('api')->check()) {
|
|
|
|
$model->deleted_lotacao_id = auth()->payload()->get('lotacao_id');
|
|
$model->deleted_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
|
|
|
$model->save();
|
|
}
|
|
});
|
|
|
|
if ($usesSoftDelete) {
|
|
|
|
static::restored(function ($model) {
|
|
|
|
$model->deleted_lotacao_id = null;
|
|
$model->deleted_centro_custo_id = null;
|
|
|
|
$model->save();
|
|
});
|
|
}
|
|
}
|
|
|
|
protected function hasNoOne(array $relations) : bool
|
|
{
|
|
foreach ($relations as $relation) {
|
|
|
|
if (!is_null($this->{$relation}) && $this->{$relation}()->get()->isNotEmpty()) {
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|