netgescon-master/app/Models/RubricaUniversale.php

142 lines
3.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class RubricaUniversale extends Model
{
use SoftDeletes;
protected $table = 'rubrica_universale';
protected $fillable = [
'nome',
'cognome',
'ragione_sociale',
'tipo_contatto', // 'persona_fisica', 'persona_giuridica'
'codice_fiscale',
'partita_iva',
'indirizzo',
'civico',
'cap',
'citta',
'provincia',
'nazione',
'telefono_ufficio',
'telefono_cellulare',
'telefono_casa',
'fax',
'email',
'pec',
'sito_web',
'note',
'categoria', // 'banca', 'fornitore', 'assicurazione', 'cliente', 'condomino', 'altro'
'stato', // 'attivo', 'sospeso', 'inattivo'
'data_inserimento',
'data_ultima_modifica',
'creato_da',
'modificato_da'
];
protected $casts = [
'data_inserimento' => '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}%");
});
}
}