72 lines
1.6 KiB
PHP
72 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DettaglioMillesimi extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'dettaglio_millesimi';
|
|
|
|
protected $fillable = [
|
|
'tabella_millesimale_id',
|
|
'unita_immobiliare_id',
|
|
'millesimi',
|
|
'partecipa',
|
|
'note',
|
|
'created_by'
|
|
];
|
|
|
|
protected $casts = [
|
|
'millesimi' => 'decimal:4',
|
|
'partecipa' => 'boolean'
|
|
];
|
|
|
|
/**
|
|
* Relazione con TabellaMillesimale
|
|
*/
|
|
public function tabellaMillesimale()
|
|
{
|
|
return $this->belongsTo(TabellaMillesimale::class, 'tabella_millesimale_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* Relazione con UnitaImmobiliare
|
|
*/
|
|
public function unitaImmobiliare()
|
|
{
|
|
return $this->belongsTo(UnitaImmobiliare::class, 'unita_immobiliare_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* Relazione con User creatore
|
|
*/
|
|
public function createdBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by', 'id');
|
|
}
|
|
|
|
/**
|
|
* Scope per unità che partecipano
|
|
*/
|
|
public function scopePartecipanti($query)
|
|
{
|
|
return $query->where('partecipa', true);
|
|
}
|
|
|
|
/**
|
|
* Calcola percentuale su totale tabella
|
|
*/
|
|
public function getPercentualeAttribute(): float
|
|
{
|
|
if (!$this->tabellaMillesimale || $this->tabellaMillesimale->totale_millesimi == 0) {
|
|
return 0;
|
|
}
|
|
|
|
return ($this->millesimi / $this->tabellaMillesimale->totale_millesimi) * 100;
|
|
}
|
|
}
|