netgescon-master/docs/team/project/app/Models/LogModificaPreventivo.php
Pikappa2 480e7eafbd 🎯 NETGESCON - Setup iniziale repository completo
📋 Commit iniziale con:
-  Documentazione unificata in docs/
-  Codice Laravel in netgescon-laravel/
-  Script automazione in scripts/
-  Configurazione sync rsync
-  Struttura organizzata e pulita

🔄 Versione: 2025.07.19-1644
🎯 Sistema pronto per Git distribuito
2025-07-19 16:44:47 +02:00

83 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LogModificaPreventivo extends Model
{
use HasFactory;
protected $table = 'log_modifiche_preventivo';
protected $fillable = [
'entita',
'entita_id',
'versione_precedente',
'versione_nuova',
'utente_id',
'tipo_operazione',
'motivo',
'dati_precedenti',
'dati_nuovi',
'diff',
];
protected $casts = [
'versione_precedente' => 'integer',
'versione_nuova' => 'integer',
'dati_precedenti' => 'array',
'dati_nuovi' => 'array',
'diff' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Relazione con User
*/
public function utente()
{
return $this->belongsTo(User::class, 'utente_id');
}
/**
* Relazione polimorfica con entità
*/
public function entita()
{
return $this->morphTo();
}
/**
* Scope per entità
*/
public function scopePerEntita($query, $entita, $entitaId)
{
return $query->where('entita', $entita)->where('entita_id', $entitaId);
}
/**
* Genera diff stile GIT
*/
public static function generaDiff($datiPrecedenti, $datiNuovi)
{
$diff = [];
foreach ($datiNuovi as $campo => $valoreNuovo) {
$valorePrecedente = $datiPrecedenti[$campo] ?? null;
if ($valorePrecedente != $valoreNuovo) {
$diff[] = [
'campo' => $campo,
'da' => $valorePrecedente,
'a' => $valoreNuovo,
'tipo' => $valorePrecedente === null ? 'aggiunto' : 'modificato',
];
}
}
return $diff;
}
}