netgescon-day0/app/Models/VoceSpesaMillesimale.php

70 lines
1.6 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 VoceSpesaMillesimale extends Model
{
use HasFactory;
protected $table = 'voci_spesa_millesimali';
protected $fillable = [
'tabella_millesimale_id',
'codice_voce',
'descrizione',
'importo_preventivo',
'importo_consuntivo',
'attiva'
];
protected $casts = [
'importo_preventivo' => 'decimal:2',
'importo_consuntivo' => 'decimal:2',
'attiva' => 'boolean'
];
/**
* Relazione con la tabella millesimale
*/
public function tabellaMillesimale(): BelongsTo
{
return $this->belongsTo(TabellaMillesimaleConti::class, 'tabella_millesimale_id');
}
/**
* Codice completo voce (TABELLA + VOCE)
*/
public function getCodiceCompletoAttribute(): string
{
return $this->tabellaMillesimale->codice . ' ' . $this->codice_voce;
}
/**
* Importo effettivo (consuntivo se presente, altrimenti preventivo)
*/
public function getImportoEffettivoAttribute(): ?float
{
return $this->importo_consuntivo ?? $this->importo_preventivo;
}
/**
* Scope attive
*/
public function scopeAttive($query)
{
return $query->where('attiva', true);
}
/**
* Scope per tabella
*/
public function scopeByTabella($query, $tabellaId)
{
return $query->where('tabella_millesimale_id', $tabellaId);
}
}