96 lines
2.1 KiB
PHP
Executable File
96 lines
2.1 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;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class GestioneCondominiale extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'gestioni_condominiali';
|
|
|
|
protected $fillable = [
|
|
'stabile_id',
|
|
'codice',
|
|
'descrizione',
|
|
'tipo',
|
|
'anno_inizio',
|
|
'anno_fine',
|
|
'data_inizio',
|
|
'data_fine',
|
|
'stato'
|
|
];
|
|
|
|
protected $casts = [
|
|
'data_inizio' => 'date',
|
|
'data_fine' => 'date'
|
|
];
|
|
|
|
/**
|
|
* Relazione con lo stabile
|
|
*/
|
|
public function stabile(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Stabile::class);
|
|
}
|
|
|
|
/**
|
|
* Tabelle millesimali di questa gestione
|
|
*/
|
|
public function tabelleMillesimali(): HasMany
|
|
{
|
|
return $this->hasMany(TabellaMillesimale::class, 'gestione_id');
|
|
}
|
|
|
|
/**
|
|
* Ripartizioni di questa gestione
|
|
*/
|
|
public function ripartizioni(): HasMany
|
|
{
|
|
return $this->hasMany(RipartizioneSpesa::class, 'gestione_id');
|
|
}
|
|
|
|
/**
|
|
* Scope per tipo di gestione
|
|
*/
|
|
public function scopeByTipo($query, $tipo)
|
|
{
|
|
return $query->where('tipo', $tipo);
|
|
}
|
|
|
|
/**
|
|
* Scope per anno
|
|
*/
|
|
public function scopeByAnno($query, $anno)
|
|
{
|
|
return $query->where('anno_inizio', '<=', $anno)
|
|
->where(function ($q) use ($anno) {
|
|
$q->whereNull('anno_fine')
|
|
->orWhere('anno_fine', '>=', $anno);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Scope attive
|
|
*/
|
|
public function scopeAttive($query)
|
|
{
|
|
return $query->where('stato', 'ATTIVA');
|
|
}
|
|
|
|
/**
|
|
* Periodo completo della gestione
|
|
*/
|
|
public function getPeriodoAttribute(): string
|
|
{
|
|
if ($this->anno_fine && $this->anno_fine != $this->anno_inizio) {
|
|
return $this->anno_inizio . '-' . $this->anno_fine;
|
|
}
|
|
return (string) $this->anno_inizio;
|
|
}
|
|
}
|