73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class AffittoCanoneDovuto extends Model
|
|
{
|
|
protected $table = 'affitti_canoni_dovuti';
|
|
|
|
protected $fillable = [
|
|
'affitto_id',
|
|
'anno',
|
|
'mese',
|
|
'mese_descrizione',
|
|
'fitto',
|
|
'istat_percentuale',
|
|
'istat_importo',
|
|
'descrizione_1',
|
|
'descrizione_2',
|
|
'descrizione_3',
|
|
'descrizione_4',
|
|
'importo_1',
|
|
'importo_2',
|
|
'importo_3',
|
|
'importo_4',
|
|
'bollo',
|
|
'totale',
|
|
'n_ricevuta',
|
|
'data_emissione',
|
|
'data_scadenza',
|
|
'saldo_progressivo',
|
|
];
|
|
|
|
protected $casts = [
|
|
'fitto' => 'decimal:2',
|
|
'istat_percentuale' => 'decimal:3',
|
|
'istat_importo' => 'decimal:2',
|
|
'importo_1' => 'decimal:2',
|
|
'importo_2' => 'decimal:2',
|
|
'importo_3' => 'decimal:2',
|
|
'importo_4' => 'decimal:2',
|
|
'bollo' => 'decimal:2',
|
|
'totale' => 'decimal:2',
|
|
'data_emissione' => 'date',
|
|
'data_scadenza' => 'date',
|
|
'saldo_progressivo' => 'decimal:2',
|
|
];
|
|
|
|
public function affitto()
|
|
{
|
|
return $this->belongsTo(AffittoImmobile::class, 'affitto_id');
|
|
}
|
|
|
|
public function generateNumeroRicevuta(): string
|
|
{
|
|
$affitto = $this->affitto;
|
|
$stabileCode = $affitto?->stabile?->codice_univoco
|
|
?: ($affitto?->stabile?->codice_stabile ?: $affitto?->codice_stabile_legacy);
|
|
$anno = $this->anno ?: date('Y');
|
|
$mese = str_pad((string) ($this->mese ?: 0), 2, '0', STR_PAD_LEFT);
|
|
$base = $stabileCode
|
|
? "AFF-{$stabileCode}-{$this->affitto_id}-{$anno}{$mese}"
|
|
: "AFF-{$this->affitto_id}-{$anno}{$mese}";
|
|
|
|
$code = $base;
|
|
$exists = self::where('n_ricevuta', $code)->whereKeyNot($this->id)->exists();
|
|
if ($exists) {
|
|
$code = $base . '-' . ($this->id ?? uniqid());
|
|
}
|
|
return $code;
|
|
}
|
|
}
|