self::APERTA]; // L'arrivo lo registra RichiesteServizio (con il numero del territorio): // qui solo i cambi di stato, con chi li ha fatti. protected static $recordEvents = ['updated']; protected $fillable = ['tipo', 'nome', 'numero_territorio', 'note', 'stato', 'assegnazione_id', 'gestita_da', 'gestita_at']; protected function casts(): array { return [ 'nome' => 'encrypted', 'note' => 'encrypted', 'gestita_at' => 'datetime', 'assegnazione_id' => 'integer', ]; } public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logOnly(['tipo', 'stato', 'assegnazione_id']) ->logOnlyDirty() ->dontSubmitEmptyLogs(); } public function assegnazione() { return $this->belongsTo(Assegnazione::class); } public function gestitaDa() { return $this->belongsTo(User::class, 'gestita_da'); } public function scopeAperte($query) { return $query->where('stato', self::APERTA); } public function isAperta(): bool { return $this->stato === self::APERTA; } public function getTipoEtichettaAttribute(): string { return $this->tipo === self::RICONSEGNA ? 'Riconsegna' : 'Nuovo territorio'; } /** Chiusa da un'assegnazione o da un rientro fatti a partire da lei. */ public function evadi(?Assegnazione $assegnazione, ?User $chi): void { if (! $this->isAperta()) { return; } $this->update([ 'stato' => self::EVASA, 'assegnazione_id' => $assegnazione?->id, 'gestita_da' => $chi?->id, 'gestita_at' => now(), ]); } public function scarta(?User $chi): void { if (! $this->isAperta()) { return; } $this->update([ 'stato' => self::SCARTATA, 'gestita_da' => $chi?->id, 'gestita_at' => now(), ]); } }