Files
carmine.savino 06617a985a feat: add previous configuration handling for user selections in quotes
- Implemented session storage management for the last configuration choices made by the user.
- Added functionality to prompt users when changing models to retain their previous selections.
- Introduced methods to apply previous choices to the newly loaded model, including finishes, handles, glass types, and options.
- Enhanced user experience by ensuring selections are automatically applied where applicable.
2026-10-19 19:49:44 +02:00

100 lines
3.3 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\HaImmaginiCatalogo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class Finitura extends Model
{
use HaImmaginiCatalogo;
protected $table = 'finiture';
public const TIPI = [
'profilo' => 'Finitura profilo',
'maniglia' => 'Maniglia e cerniere',
];
/** Lati del serramento su cui la finitura del profilo è disponibile. */
public const LATI = [
'entrambi' => 'Interno ed esterno',
'interno' => 'Solo interno',
'esterno' => 'Solo esterno',
];
/** Scelta per modello: oltre ai lati, "nessuno" la disattiva nel modello. */
public const LATI_MODELLO = self::LATI + ['nessuno' => 'Disattiva nel modello'];
protected static array $immagini = ['anteprima', 'texture', 'texture_bump'];
protected $fillable = [
'sistema_id', 'tipo', 'lato', 'codice', 'descrizione', 'codice_mazzetta', 'colore', 'colore_corpo_base',
'roughness', 'metalness', 'anteprima', 'texture', 'texture_bump', 'bump_scala',
'predefinita', 'ordine', 'attivo',
];
protected $casts = [
'roughness' => 'float',
'metalness' => 'float',
'bump_scala' => 'float',
'predefinita' => 'boolean',
'ordine' => 'integer',
'attivo' => 'boolean',
];
public function sistema(): BelongsTo
{
return $this->belongsTo(Sistema::class);
}
public function scopeAttive(Builder $query): Builder
{
return $query->where('attivo', true)->orderBy('ordine')->orderBy('id');
}
/**
* Finiture del profilo disponibili nel modello, ciascuna con `lato_modello`
* (entrambi | interno | esterno). La scelta del modello, se c'è, vince su attivazione
* e lato del sistema: può anche attivare una finitura disattiva nel sistema.
*
* @return Collection<int,Finitura>
*/
public static function delProfiloPerModello(Modello $modello): Collection
{
$scelte = DB::table('finiture_modelli')->where('modello_id', $modello->getKey())->pluck('lato', 'finitura_id');
return static::query()
->where('sistema_id', $modello->sistema_id)
->where('tipo', 'profilo')
->orderBy('ordine')
->orderBy('id')
->get()
->each(fn (Finitura $f) => $f->lato_modello = $scelte[$f->id] ?? ($f->attivo ? $f->lato : 'nessuno'))
->reject(fn (Finitura $f) => $f->lato_modello === 'nessuno')
->values();
}
public function disponibileSu(string $lato): bool
{
return in_array($this->lato_modello ?? $this->lato, ['entrambi', $lato], true);
}
protected static function booted(): void
{
// Un solo elemento predefinito per sistema e tipo.
static::saved(function (self $record): void {
if ($record->predefinita && ($record->wasRecentlyCreated || $record->wasChanged('predefinita'))) {
static::query()
->where('sistema_id', $record->sistema_id)->where('tipo', $record->tipo)
->whereKeyNot($record->getKey())
->update(['predefinita' => false]);
}
});
}
}