'date', 'data_ultima_modifica' => 'date', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; /** * Relazione con utente che ha creato il contatto */ public function createBy() { return $this->belongsTo(User::class, 'creato_da'); } /** * Relazione con utente che ha modificato il contatto */ public function updatedBy() { return $this->belongsTo(User::class, 'modificato_da'); } /** * Relazione con dati bancari */ public function datiBancari() { return $this->hasMany(DatiBancari::class, 'contatto_id'); } /** * Relazione con documenti collegati */ public function documentiCollegati() { return $this->hasMany(DocumentoCollegato::class, 'contatto_id'); } /** * Accessor per il nome completo */ public function getNomeCompletoAttribute() { if ($this->tipo_contatto === 'persona_giuridica') { return $this->ragione_sociale; } return trim($this->nome . ' ' . $this->cognome); } /** * Accessor per l'indirizzo completo */ public function getIndirizzoCompletoAttribute() { $parts = array_filter([ $this->indirizzo, $this->civico, $this->cap, $this->citta, $this->provincia ? "({$this->provincia})" : null ]); return implode(' ', $parts); } /** * Scope per categoria */ public function scopeCategoria($query, $categoria) { return $query->where('categoria', $categoria); } /** * Scope per contatti attivi */ public function scopeAttivi($query) { return $query->where('stato', 'attivo'); } /** * Scope per ricerca */ public function scopeRicerca($query, $termine) { return $query->where(function ($q) use ($termine) { $q->where('nome', 'like', "%{$termine}%") ->orWhere('cognome', 'like', "%{$termine}%") ->orWhere('ragione_sociale', 'like', "%{$termine}%") ->orWhere('email', 'like', "%{$termine}%") ->orWhere('telefono_ufficio', 'like', "%{$termine}%") ->orWhere('telefono_cellulare', 'like', "%{$termine}%"); }); } }