netgescon-day0/app/Models/PianoDeiConti.php

68 lines
1.4 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class PianoDeiConti extends Model
{
use HasFactory;
protected $table = 'piano_conti';
protected $fillable = [
'mastro',
'conto',
'sottoconto',
'descrizione',
'tipo',
'attivo'
];
protected $casts = [
'attivo' => 'boolean'
];
/**
* Ottieni il codice completo (MASTRO-CONTO-SOTTOCONTO)
*/
public function getCodiceCompletoAttribute(): string
{
return "{$this->mastro}-{$this->conto}-{$this->sottoconto}";
}
/**
* Scope per filtro per mastro
*/
public function scopePerMastro($query, string $mastro)
{
return $query->where('mastro', $mastro);
}
/**
* Scope per filtro per conto
*/
public function scopePerConto($query, string $mastro, string $conto)
{
return $query->where('mastro', $mastro)->where('conto', $conto);
}
/**
* Scope solo attivi
*/
public function scopeAttivi($query)
{
return $query->where('attivo', true);
}
/**
* Relazione con le voci spesa
*/
public function vociSpesa(): HasMany
{
return $this->hasMany(VoceSpesaMillesimale::class, 'piano_conti_id');
}
}