Files
kreios/backoffice/app/Filament/Resources/RegolaVetroResource.php
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

156 lines
7.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\RegolaVetroResource\Pages;
use App\Filament\Support\CampiConfiguratore;
use App\Models\RegolaVetro;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\HtmlString;
use Illuminate\Validation\Rules\Unique;
class RegolaVetroResource extends Resource
{
protected static ?string $model = RegolaVetro::class;
protected static ?string $navigationIcon = 'heroicon-o-calculator';
protected static ?string $slug = 'regole-vetro';
protected static ?string $navigationGroup = 'Configuratore';
protected static ?string $navigationLabel = 'Regole vetro';
protected static ?string $modelLabel = 'regola vetro';
protected static ?string $pluralModelLabel = 'regole vetro';
protected static ?int $navigationSort = 22;
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Section::make()
->columns(2)
->schema([
CampiConfiguratore::sistema(),
Forms\Components\TextInput::make('codice')
->label('Codice')
->helperText('Es. F2, TR_A_F.')
->required()
->maxLength(50)
->unique(
ignoreRecord: true,
modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('sistema_id', $get('sistema_id')),
),
Forms\Components\TextInput::make('descrizione')->label('Tipologia')->required()->maxLength(255)->columnSpanFull(),
]),
Forms\Components\Section::make('Lastre')
->description('Per ogni riga: larghezza vetro LV = (L − detrazione) ÷ divisore, altezza vetro HV = H − detrazione, ripetuta per la quantità. Es. L/2 − 181 si scrive detrazione 362, divisore 2.')
->schema([
Forms\Components\Repeater::make('lastre')
->hiddenLabel()
->columns(5)
->minItems(1)
->defaultItems(1)
->reorderable(false)
->live(onBlur: true)
->schema([
Forms\Components\TextInput::make('descrizione')->label('Descrizione')->placeholder('es. Vetro anta')->maxLength(100),
Forms\Components\TextInput::make('quantita')->label('Quantità')->integer()->minValue(1)->default(1)->required(),
Forms\Components\TextInput::make('divisore')->label('Divisore L')->integer()->minValue(1)->default(1)->required(),
Forms\Components\TextInput::make('detrazione_larghezza')->label('Detrazione L (mm)')->numeric()->default(0)->required(),
Forms\Components\TextInput::make('detrazione_altezza')->label('Detrazione H (mm)')->numeric()->default(0)->required(),
]),
]),
Forms\Components\Section::make('Prova')
->description('Verifica la regola su una misura: non viene salvata.')
->columns(3)
->collapsible()
->schema([
Forms\Components\TextInput::make('prova_larghezza')->label('Larghezza L (mm)')->integer()->default(1200)->dehydrated(false)->live(onBlur: true),
Forms\Components\TextInput::make('prova_altezza')->label('Altezza H (mm)')->integer()->default(1400)->dehydrated(false)->live(onBlur: true),
Forms\Components\Placeholder::make('prova_risultato')
->label('Lastre risultanti')
->content(fn (Get $get) => self::anteprima($get('lastre') ?? [], (float) $get('prova_larghezza'), (float) $get('prova_altezza'))),
]),
Forms\Components\Textarea::make('note')->label('Note')->rows(2)->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->defaultSort('codice')
->columns([
Tables\Columns\TextColumn::make('codice')->label('Codice')->searchable()->sortable(),
Tables\Columns\TextColumn::make('descrizione')->label('Tipologia')->searchable()->wrap(),
Tables\Columns\TextColumn::make('lastre')
->label('Formula')
->state(fn (RegolaVetro $record) => self::formula($record->lastre ?? []))
->html(),
Tables\Columns\TextColumn::make('modelli_count')->label('Modelli')->counts('modelli'),
])
->filters([
Tables\Filters\SelectFilter::make('sistema')->label('Sistema')->relationship('sistema', 'nome'),
])
->actions([
Tables\Actions\EditAction::make()->iconButton(),
Tables\Actions\DeleteAction::make()
->iconButton()
->modalDescription('I modelli che usano questa regola non avranno più variazione di prezzo per il vetro.'),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ManageRegoleVetro::route('/'),
];
}
/** Formula leggibile, una riga per lastra (es. "2 × (L − 312) ÷ 2 × (H − 189)"). */
private static function formula(array $lastre): HtmlString
{
$righe = array_map(function (array $l) {
$larghezza = 'L'.self::meno($l['detrazione_larghezza'] ?? 0);
if ((int) ($l['divisore'] ?? 1) > 1) {
$larghezza = "({$larghezza}) ÷ {$l['divisore']}";
}
$descrizione = filled($l['descrizione'] ?? null) ? e($l['descrizione']).': ' : '';
return $descrizione.((int) ($l['quantita'] ?? 1)).' × '.$larghezza.' × (H'.self::meno($l['detrazione_altezza'] ?? 0).')';
}, $lastre);
return new HtmlString(implode('<br>', $righe));
}
private static function meno(mixed $detrazione): string
{
return (float) $detrazione ? ' − '.(float) $detrazione : '';
}
private static function anteprima(array $lastre, float $larghezza, float $altezza): HtmlString
{
if ($larghezza <= 0 || $altezza <= 0 || ! $lastre) {
return new HtmlString('—');
}
$calcolate = (new RegolaVetro(['lastre' => array_values($lastre)]))->calcolaLastre($larghezza, $altezza);
return new HtmlString(implode('<br>', array_map(fn (array $l) => sprintf(
'%s%d × %s × %s mm = %s m² cad.',
filled($l['descrizione']) ? e($l['descrizione']).': ' : '',
$l['quantita'],
number_format($l['larghezza'], 1, ',', '.'),
number_format($l['altezza'], 1, ',', '.'),
number_format(max(0, $l['larghezza']) * max(0, $l['altezza']) / 1_000_000, 3, ',', '.'),
), $calcolate)));
}
}