52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Preventivo extends Model
|
|
{
|
|
protected $table = 'preventivi';
|
|
|
|
protected $fillable = [
|
|
'cliente_id', 'token', 'numero', 'stato', 'totale', 'pdf_path', 'confirmed_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'totale' => 'decimal:2',
|
|
'confirmed_at' => 'datetime',
|
|
];
|
|
|
|
public const STATI = [
|
|
'bozza' => 'Bozza',
|
|
'inviato' => 'Inviato',
|
|
'in_lavorazione' => 'In lavorazione',
|
|
'chiuso' => 'Chiuso',
|
|
];
|
|
|
|
public function cliente(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Cliente::class);
|
|
}
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(PreventivoItem::class);
|
|
}
|
|
|
|
/** Ricalcola e salva il totale dagli item. */
|
|
public function ricalcolaTotale(): void
|
|
{
|
|
$this->totale = $this->items()->sum('prezzo_totale');
|
|
$this->save();
|
|
}
|
|
|
|
/** Numero progressivo assegnato alla conferma (max+1). */
|
|
public static function prossimoNumero(): int
|
|
{
|
|
return (int) static::max('numero') + 1;
|
|
}
|
|
}
|