71 lines
1.7 KiB
PHP
Executable File
71 lines
1.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class PersonaAudit extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'persone_audit';
|
|
|
|
public $timestamps = false; // Usa data_modifica personalizzata
|
|
|
|
protected $fillable = [
|
|
'persona_id',
|
|
'campo_modificato',
|
|
'valore_precedente',
|
|
'valore_nuovo',
|
|
'utente_modifica',
|
|
'ip_modifica',
|
|
'data_modifica'
|
|
];
|
|
|
|
protected $casts = [
|
|
'data_modifica' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* Relazione con persona
|
|
*/
|
|
public function persona(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Persona::class);
|
|
}
|
|
|
|
/**
|
|
* SCOPE: Modifiche recenti
|
|
*/
|
|
public function scopeRecenti($query, $giorni = 30)
|
|
{
|
|
return $query->where('data_modifica', '>=', now()->subDays($giorni));
|
|
}
|
|
|
|
/**
|
|
* SCOPE: Per utente
|
|
*/
|
|
public function scopeUtente($query, $utente)
|
|
{
|
|
return $query->where('utente_modifica', $utente);
|
|
}
|
|
|
|
/**
|
|
* Crea record audit per modifica
|
|
*/
|
|
public static function registraModifica($personaId, $campo, $valorePrecedente, $valoreNuovo, $utente = null, $ip = null)
|
|
{
|
|
return static::create([
|
|
'persona_id' => $personaId,
|
|
'campo_modificato' => $campo,
|
|
'valore_precedente' => $valorePrecedente,
|
|
'valore_nuovo' => $valoreNuovo,
|
|
'utente_modifica' => $utente ?: (auth()->user()->name ?? 'Sistema'),
|
|
'ip_modifica' => $ip ?: request()->ip(),
|
|
'data_modifica' => now()
|
|
]);
|
|
}
|
|
}
|