98 lines
2.1 KiB
PHP
98 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class DettaglioMillesimale extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'dettagli_millesimali';
|
|
|
|
protected $fillable = [
|
|
'tabella_id',
|
|
'unita_immobiliare_id',
|
|
'millesimi',
|
|
'percentuale',
|
|
'quota_fissa',
|
|
'note',
|
|
'creato_da',
|
|
'modificato_da'
|
|
];
|
|
|
|
protected $casts = [
|
|
'millesimi' => 'decimal:4',
|
|
'percentuale' => 'decimal:4',
|
|
'quota_fissa' => 'decimal:2',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Relazione con TabellaMillesimale
|
|
*/
|
|
public function tabella()
|
|
{
|
|
return $this->belongsTo(TabellaMillesimale::class, 'tabella_id');
|
|
}
|
|
|
|
/**
|
|
* Relazione con UnitaImmobiliare
|
|
*/
|
|
public function unitaImmobiliare()
|
|
{
|
|
return $this->belongsTo(UnitaImmobiliare::class, 'unita_immobiliare_id');
|
|
}
|
|
|
|
/**
|
|
* Relazione con utente che ha creato
|
|
*/
|
|
public function createBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'creato_da');
|
|
}
|
|
|
|
/**
|
|
* Relazione con utente che ha modificato
|
|
*/
|
|
public function updatedBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'modificato_da');
|
|
}
|
|
|
|
/**
|
|
* Accessor per percentuale calcolata dai millesimi
|
|
*/
|
|
public function getPercentualeCalcolataAttribute()
|
|
{
|
|
return $this->millesimi / 10; // 1000 millesimi = 100%
|
|
}
|
|
|
|
/**
|
|
* Scope per tabella
|
|
*/
|
|
public function scopePerTabella($query, $tabellaId)
|
|
{
|
|
return $query->where('tabella_id', $tabellaId);
|
|
}
|
|
|
|
/**
|
|
* Scope per unità immobiliare
|
|
*/
|
|
public function scopePerUnita($query, $unitaId)
|
|
{
|
|
return $query->where('unita_immobiliare_id', $unitaId);
|
|
}
|
|
|
|
/**
|
|
* Scope ordinato per millesimi decrescenti
|
|
*/
|
|
public function scopeOrdinatoPerMillesimi($query)
|
|
{
|
|
return $query->orderBy('millesimi', 'desc');
|
|
}
|
|
}
|