133 lines
3.5 KiB
PHP
133 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Log\Operacao;
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Log\Acao;
|
|
use App\Models\WebService\Usuario AS ExternoUsuario;
|
|
use Illuminate\Database\Eloquent\Relations\Pivot as BasePivot;
|
|
|
|
abstract class Pivot extends BasePivot
|
|
{
|
|
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);
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($pivot) {
|
|
|
|
if (auth()->guard('api')->check()) {
|
|
|
|
$pivot->created_lotacao_id = auth()->payload()->get('lotacao_id');
|
|
$pivot->created_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
|
|
|
$pivot->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
|
$pivot->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
|
}
|
|
else if (auth()->guard('external')->check()) {
|
|
|
|
$pivot->externo_created_usuario_id = auth()->user()->id;
|
|
$pivot->externo_updated_usuario_id = auth()->user()->id;
|
|
}
|
|
});
|
|
|
|
static::created(function ($pivot) {
|
|
$namespace = get_class($pivot);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $pivot->getTable();
|
|
$json = $pivot->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::INCLUSAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
});
|
|
|
|
static::updating(function ($pivot) {
|
|
|
|
if (auth()->guard('api')->check()) {
|
|
|
|
$pivot->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
|
$pivot->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
|
}
|
|
else if (auth()->guard('external')->check()) {
|
|
|
|
$pivot->externo_updated_usuario_id = auth()->user()->id;
|
|
}
|
|
});
|
|
|
|
static::updated(function ($pivot) {
|
|
$namespace = get_class($pivot);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $pivot->getTable();
|
|
$json = $pivot->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::ATUALIZACAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
});
|
|
|
|
static::deleted(function ($pivot) {
|
|
$namespace = get_class($pivot);
|
|
|
|
if ($namespace === Acao::class) {
|
|
return;
|
|
}
|
|
|
|
$table = $pivot->getTable();
|
|
$json = $pivot->toJson();
|
|
|
|
Acao::create([
|
|
'operacao_id' => Operacao::EXCLUSAO,
|
|
'namespace' => $namespace,
|
|
'table' => $table,
|
|
'data' => $json,
|
|
]);
|
|
});
|
|
}
|
|
}
|