125 lines
3.1 KiB
PHP
125 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class RataEmessa extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'rate_emesse';
|
|
|
|
protected $fillable = [
|
|
'amministratore_id',
|
|
'anno_gestione_id',
|
|
'stabile_id',
|
|
'condomino_id',
|
|
'numero_rata',
|
|
'data_emissione',
|
|
'data_scadenza',
|
|
'tipo_rata',
|
|
'periodo_riferimento',
|
|
'importo_ordinario',
|
|
'importo_straordinario',
|
|
'importo_riscaldamento',
|
|
'importo_totale',
|
|
'stato_pagamento',
|
|
'data_pagamento',
|
|
'importo_pagato',
|
|
'modalita_pagamento',
|
|
'numero_ricevuta',
|
|
'note'
|
|
];
|
|
|
|
protected $casts = [
|
|
'data_emissione' => 'date',
|
|
'data_scadenza' => 'date',
|
|
'data_pagamento' => 'date',
|
|
'importo_ordinario' => 'decimal:2',
|
|
'importo_straordinario' => 'decimal:2',
|
|
'importo_riscaldamento' => 'decimal:2',
|
|
'importo_totale' => 'decimal:2',
|
|
'importo_pagato' => 'decimal:2'
|
|
];
|
|
|
|
const STATO_EMESSA = 'emessa';
|
|
const STATO_PAGATA = 'pagata';
|
|
const STATO_PARZIALE = 'parziale';
|
|
const STATO_SCADUTA = 'scaduta';
|
|
|
|
const TIPO_ORDINARIA = 'ordinaria';
|
|
const TIPO_STRAORDINARIA = 'straordinaria';
|
|
const TIPO_RISCALDAMENTO = 'riscaldamento';
|
|
const TIPO_MISTA = 'mista';
|
|
|
|
public function amministratore(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Administrator::class);
|
|
}
|
|
|
|
public function annoGestione(): BelongsTo
|
|
{
|
|
return $this->belongsTo(AnnoGestione::class);
|
|
}
|
|
|
|
public function stabile(): BelongsTo
|
|
{
|
|
return $this->belongsTo(StabileGescon::class);
|
|
}
|
|
|
|
public function condomino(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CondominoStabile::class);
|
|
}
|
|
|
|
public function incassi()
|
|
{
|
|
return $this->hasMany(IncassoPagamento::class, 'rata_id');
|
|
}
|
|
|
|
public function scopePagate($query)
|
|
{
|
|
return $query->where('stato_pagamento', self::STATO_PAGATA);
|
|
}
|
|
|
|
public function scopeScadute($query)
|
|
{
|
|
return $query->where('data_scadenza', '<', now())
|
|
->whereIn('stato_pagamento', [self::STATO_EMESSA, self::STATO_PARZIALE]);
|
|
}
|
|
|
|
public function scopeByTipo($query, $tipo)
|
|
{
|
|
return $query->where('tipo_rata', $tipo);
|
|
}
|
|
|
|
public function scopeByAnno($query, $anno)
|
|
{
|
|
return $query->whereHas('annoGestione', function ($q) use ($anno) {
|
|
$q->where('anno_ordinario', $anno);
|
|
});
|
|
}
|
|
|
|
public function getImportoResiduoAttribute()
|
|
{
|
|
return $this->importo_totale - $this->importo_pagato;
|
|
}
|
|
|
|
public function getIsScadutaAttribute()
|
|
{
|
|
return $this->data_scadenza < now() &&
|
|
in_array($this->stato_pagamento, [self::STATO_EMESSA, self::STATO_PARZIALE]);
|
|
}
|
|
|
|
public function getGiorniRitardoAttribute()
|
|
{
|
|
if (!$this->is_scaduta) {
|
|
return 0;
|
|
}
|
|
return now()->diffInDays($this->data_scadenza);
|
|
}
|
|
}
|