141 lines
3.4 KiB
PHP
Executable File
141 lines
3.4 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 PersonaUnitaRelazione extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'persone_unita_relazioni';
|
|
|
|
protected $fillable = [
|
|
'persona_id',
|
|
'unita_id',
|
|
'tipo_relazione',
|
|
'ruolo_rate',
|
|
'quota_relazione',
|
|
'data_inizio',
|
|
'data_fine',
|
|
'attivo',
|
|
'riceve_comunicazioni',
|
|
'riceve_convocazioni',
|
|
'vota_assemblea',
|
|
'note_relazione',
|
|
];
|
|
|
|
protected $casts = [
|
|
'data_inizio' => 'date',
|
|
'data_fine' => 'date',
|
|
'quota_relazione' => 'decimal:2',
|
|
'ruolo_rate' => 'string',
|
|
'attivo' => 'boolean',
|
|
'riceve_comunicazioni' => 'boolean',
|
|
'riceve_convocazioni' => 'boolean',
|
|
'vota_assemblea' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Relazione con persona
|
|
*/
|
|
public function persona(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Persona::class);
|
|
}
|
|
|
|
/**
|
|
* Relazione con unità immobiliare
|
|
*/
|
|
public function unitaImmobiliare(): BelongsTo
|
|
{
|
|
return $this->belongsTo(UnitaImmobiliare::class, 'unita_id');
|
|
}
|
|
|
|
/**
|
|
* Tipo di relazione configurabile
|
|
*/
|
|
public function tipoRelazioneDettagli(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TipoRelazionePersonaUnita::class, 'tipo_relazione', 'codice');
|
|
}
|
|
|
|
/**
|
|
* SCOPE: Relazioni attive
|
|
*/
|
|
public function scopeAttive($query)
|
|
{
|
|
return $query->where('attivo', true)
|
|
->where(function ($q) {
|
|
$q->whereNull('data_fine')
|
|
->orWhere('data_fine', '>=', now());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* SCOPE: Proprietari (includendo comproprietari)
|
|
*/
|
|
public function scopeProprietari($query)
|
|
{
|
|
return $query->whereIn('tipo_relazione', ['proprietario', 'comproprietario']);
|
|
}
|
|
|
|
/**
|
|
* SCOPE: Votanti in assemblea
|
|
*/
|
|
public function scopeVotanti($query)
|
|
{
|
|
return $query->where('vota_assemblea', true);
|
|
}
|
|
|
|
/**
|
|
* ACCESSOR: Descrizione relazione
|
|
*/
|
|
public function getDescrizioneRelazioneAttribute()
|
|
{
|
|
$descrizione = ucfirst(str_replace('_', ' ', $this->tipo_relazione));
|
|
|
|
if ($this->quota_relazione) {
|
|
$descrizione .= " ({$this->quota_relazione}%)";
|
|
}
|
|
|
|
return $descrizione;
|
|
}
|
|
|
|
/**
|
|
* ACCESSOR: Stato relazione
|
|
*/
|
|
public function getStatoRelazioneAttribute()
|
|
{
|
|
if (! $this->attivo) {
|
|
return 'Non Attiva';
|
|
}
|
|
|
|
if ($this->data_fine && $this->data_fine < now()) {
|
|
return 'Scaduta';
|
|
}
|
|
|
|
return 'Attiva';
|
|
}
|
|
|
|
/**
|
|
* Verifica se la relazione è attualmente valida
|
|
*/
|
|
public function isAttiva()
|
|
{
|
|
return $this->attivo &&
|
|
($this->data_fine === null || $this->data_fine >= now()->toDateString());
|
|
}
|
|
|
|
public static function deriveRuoloRate(?string $tipoRelazione): ?string
|
|
{
|
|
$tipoRelazione = strtolower(trim((string) ($tipoRelazione ?? '')));
|
|
if ($tipoRelazione === '') {
|
|
return null;
|
|
}
|
|
|
|
return in_array($tipoRelazione, ['inquilino', 'locatario', 'conduttore'], true) ? 'I' : 'C';
|
|
}
|
|
}
|