79 lines
1.8 KiB
PHP
Executable File
79 lines
1.8 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 MillesimiUnita extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'millesimi_unita';
|
|
|
|
protected $fillable = [
|
|
'unita_immobiliare_id',
|
|
'tabella_millesimale_id',
|
|
'millesimi',
|
|
'consumo_individuale'
|
|
];
|
|
|
|
protected $casts = [
|
|
'millesimi' => 'decimal:3',
|
|
'consumo_individuale' => 'decimal:2'
|
|
];
|
|
|
|
/**
|
|
* Relazione con unità immobiliare
|
|
*/
|
|
public function unitaImmobiliare(): BelongsTo
|
|
{
|
|
return $this->belongsTo(UnitaImmobiliare::class, 'unita_immobiliare_id');
|
|
}
|
|
|
|
/**
|
|
* Relazione con tabella millesimale
|
|
*/
|
|
public function tabellaMillesimale(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TabellaMillesimaleConti::class, 'tabella_millesimale_id');
|
|
}
|
|
|
|
/**
|
|
* Millesimi formattati
|
|
*/
|
|
public function getMillesimiFormattedAttribute(): string
|
|
{
|
|
return number_format($this->millesimi, 3, ',', '.');
|
|
}
|
|
|
|
/**
|
|
* Calcola quota spettante da un importo
|
|
*/
|
|
public function calcolaQuotaSpettante(float $importoTotale): float
|
|
{
|
|
if (!$this->tabellaMillesimale->hasMillesimi()) {
|
|
return 0;
|
|
}
|
|
|
|
return ($importoTotale * $this->millesimi) / $this->tabellaMillesimale->totale_millesimi;
|
|
}
|
|
|
|
/**
|
|
* Scope per unità immobiliare
|
|
*/
|
|
public function scopeByUnita($query, $unitaId)
|
|
{
|
|
return $query->where('unita_immobiliare_id', $unitaId);
|
|
}
|
|
|
|
/**
|
|
* Scope per tabella
|
|
*/
|
|
public function scopeByTabella($query, $tabellaId)
|
|
{
|
|
return $query->where('tabella_millesimale_id', $tabellaId);
|
|
}
|
|
}
|