netgescon-day0/app/Models/PianoConti.php

82 lines
1.7 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 PianoConti extends Model
{
use HasFactory;
protected $table = 'piano_conti';
protected $fillable = [
'mastro',
'conto',
'sottoconto',
'quarto_livello',
'descrizione',
'tipo',
'attivo'
];
protected $casts = [
'attivo' => 'boolean'
];
/**
* Codice completo del conto
*/
public function getCodiceCompletoAttribute(): string
{
$parts = [$this->mastro, $this->conto, $this->sottoconto];
if (!empty($this->quarto_livello)) {
$parts[] = $this->quarto_livello;
}
return implode('.', $parts);
}
/**
* Codice mastro-conto
*/
public function getCodiceMastroContoAttribute(): string
{
return $this->mastro . '.' . $this->conto;
}
/**
* Codice a 4 livelli (se presente quarto_livello)
*/
public function getCodiceQuattroLivelliAttribute(): string
{
$q = trim((string)($this->quarto_livello ?? ''));
return $this->mastro . '.' . $this->conto . '.' . $this->sottoconto . ($q !== '' ? ('.' . $q) : '');
}
/**
* Scope per mastro
*/
public function scopeByMastro($query, $mastro)
{
return $query->where('mastro', $mastro);
}
/**
* Scope per tipo
*/
public function scopeByTipo($query, $tipo)
{
return $query->where('tipo', $tipo);
}
/**
* Scope attivi
*/
public function scopeAttivi($query)
{
return $query->where('attivo', true);
}
}