Refactor form schema in StickerMasterResource for improved clarity and organization

This commit is contained in:
dhanabalan
2025-09-05 17:27:24 +05:30
parent bb2b81d0f2
commit cac91722c3

View File

@@ -3,12 +3,12 @@
namespace App\Filament\Resources; namespace App\Filament\Resources;
use App\Filament\Exports\StickerMasterExporter; use App\Filament\Exports\StickerMasterExporter;
use App\Filament\Imports\ShiftImporter; //use App\Filament\Imports\ShiftImporter;
use App\Filament\Imports\StickerMasterImporter; use App\Filament\Imports\StickerMasterImporter;
use App\Filament\Resources\StickerMasterResource\Pages; use App\Filament\Resources\StickerMasterResource\Pages;
use App\Filament\Resources\StickerMasterResource\RelationManagers; //use App\Filament\Resources\StickerMasterResource\RelationManagers;
use App\Models\StickerMaster; use App\Models\StickerMaster;
use Closure; //use Closure;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Form; use Filament\Forms\Form;
@@ -36,397 +36,401 @@ class StickerMasterResource extends Resource
public static function form(Form $form): Form public static function form(Form $form): Form
{ {
return $form return $form
->schema([ ->schema([
Forms\Components\Select::make('plant_id') Forms\Components\Select::make('plant_id')
->relationship('plant', 'name') ->relationship('plant', 'name')
->reactive() ->reactive()
->nullable() ->nullable()
->default(function () { ->default(function () {
return optional(StickerMaster::latest()->first())->plant_id; return optional(StickerMaster::latest()->first())->plant_id;
}) })
->disabled(fn (Get $get) => !empty($get('id'))) //disable in edit if user try to change ->disabled(fn (Get $get) => !empty($get('id'))) //disable in edit if user try to change
->afterStateUpdated(fn (callable $set) => ->afterStateUpdated(fn (callable $set) =>
$set('item_id', null) & //when plant changed remove all the data which is in text input box $set('item_id', null) & //when plant changed remove all the data which is in text input box
$set('item_description', null) & $set('item_description', null) &
$set('item_error', null) & $set('item_error', null) &
$set('panel_box_code', null) & $set('panel_box_code', null) &
$set('load_rate', null) & $set('load_rate', null) &
$set('bundle_quantity', null) & $set('bundle_quantity', null) &
$set('material_type', null) & $set('material_type', null) &
$set('part_validation1', null) & $set('part_validation1', null) &
$set('part_validation2', null) & $set('part_validation2', null) &
$set('part_validation3', null) & $set('part_validation3', null) &
$set('part_validation4', null) & $set('part_validation4', null) &
$set('part_validation5', null) & $set('part_validation5', null) &
$set('laser_part_validation1', null) & $set('laser_part_validation1', null) &
$set('laser_part_validation2', null) & $set('laser_part_validation2', null) &
$set('serial_number_motor', false) & $set('serial_number_motor', false) &
$set('serial_number_pump', false) & $set('serial_number_pump', false) &
$set('serial_number_pumpset', false) & $set('serial_number_pumpset', false) &
$set('pack_slip_motor', false) & $set('pack_slip_motor', false) &
$set('pack_slip_pump', false) & $set('pack_slip_pump', false) &
$set('pack_slip_pumpset', false) & $set('pack_slip_pumpset', false) &
$set('name_plate_motor', false) & $set('name_plate_motor', false) &
$set('name_plate_pump', false) & $set('name_plate_pump', false) &
$set('name_plate_pumpset', false) & $set('name_plate_pumpset', false) &
$set('tube_sticker_motor', false) & $set('tube_sticker_motor', false) &
$set('tube_sticker_pump', false) & $set('tube_sticker_pump', false) &
$set('tube_sticker_pumpset', false) & $set('tube_sticker_pumpset', false) &
$set('warranty_card', false) $set('warranty_card', false)
) )
->required(), ->required(),
Forms\Components\Select::make('item_id') Forms\Components\Select::make('item_id')
->label('Item Code') ->label('Item Code')
->options(function (callable $get) { ->options(function (callable $get) {
if (!$get('plant_id')) { if (!$get('plant_id')) {
return []; return [];
} }
return \App\Models\Item::where('plant_id', $get('plant_id')) return \App\Models\Item::where('plant_id', $get('plant_id'))
->pluck('code', 'id') ->pluck('code', 'id')
->toArray(); ->toArray();
}) })
// ->rule(function (callable $get) { // ->rule(function (callable $get) {
// return Rule::unique('items', 'code') // return Rule::unique('items', 'code')
// ->where('plant_id', $get('plant_id')) // ->where('plant_id', $get('plant_id'))
// ->ignore($get('id')); // Ignore current record during updates // ->ignore($get('id')); // Ignore current record during updates
// }) // })
->required() ->required()
->nullable() ->nullable()
->searchable() ->searchable()
->reactive() ->reactive()
// ->disabled(fn (Get $get) => !empty($get('id'))) // ->disabled(fn (Get $get) => !empty($get('id')))
->live(debounce: 500) // Enable live updates ->live(debounce: 500) // Enable live updates
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
$itemId = $get('item_id');
$plantId = $get('plant_id'); //If plant_id is changed or empty, reset everything
$itemId = $get('item_id'); if (blank($plantId)) {
$set('item_id', null);
$set('item_error', null);
$set('item_description', null);
return;
}
//If plant_id is changed or empty, reset everything if (blank($itemId)) {
if (blank($plantId)) { $set('item_error', null);
$set('item_id', null); $set('item_description', null);
$set('item_error', null); return;
$set('item_description', null); }
return;
}
if (blank($itemId)) { $availableItems = \App\Models\Item::where('plant_id', $plantId)->exists();
$set('item_error', null); if (!$availableItems) {
$set('item_description', null); $set('item_error', null);
return; return;
} }
$availableItems = \App\Models\Item::where('plant_id', $plantId)->exists(); // Ensure `item_id` is not cleared
if (!$availableItems) { if (!$plantId || !$itemId) {
$set('item_error', null); $set('item_description', null);
return; return;
} }
// Ensure `item_id` is not cleared // Check if item exists for the selected plant
if (!$plantId || !$itemId) { $item = \App\Models\Item::where('plant_id', $plantId)
$set('item_description', null); ->where('id', $itemId)
return; ->first();
}
// Check if item exists for the selected plant if ($item) {
$item = \App\Models\Item::where('plant_id', $plantId) $set('item_description', $item->description);
->where('id', $itemId)
->first();
if ($item) {
$set('item_description', $item->description);
} else {
$set('item_description', null);
}
$duplicateSticker = StickerMaster::where('plant_id', $plantId)
->where('item_id', $itemId)
->exists();
if(!$get('id'))
{
$set('item_error', $duplicateSticker ? 'Item Code already exists for the selected plant.' : null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('item_error') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('item_error') ? $get('item_error') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('item_description')
->label('Description')
->required()
->afterStateHydrated(function ($component, $state, Get $get, Set $set) {
if ($get('id')) {
$itemId = StickerMaster::where('id', $get('id'))->first()?->item_id;
if ($itemId) {
$item = \App\Models\Item::where('id', $itemId)->first()?->description;
if ($item) {
$set('item_description', $item);
} else {
$set('item_description', null);
}
} else { } else {
$set('item_description', null); $set('item_description', null);
} }
}
})
->reactive()
->readOnly(true),
Forms\Components\TextInput::make('part_validation1') $duplicateSticker = StickerMaster::where('plant_id', $plantId)
->nullable(), ->where('item_id', $itemId)
->exists();
if(!$get('id'))
{
$set('item_error', $duplicateSticker ? 'Item Code already exists for the selected plant.' : null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('item_error') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('item_error') ? $get('item_error') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('part_validation2') Forms\Components\TextInput::make('item_description')
->nullable(), ->label('Description')
->required()
->afterStateHydrated(function ($component, $state, Get $get, Set $set) {
if ($get('id')) {
$itemId = StickerMaster::where('id', $get('id'))->first()?->item_id;
if ($itemId) {
$item = \App\Models\Item::where('id', $itemId)->first()?->description;
if ($item) {
$set('item_description', $item);
} else {
$set('item_description', null);
}
} else {
$set('item_description', null);
}
}
})
->reactive()
->readOnly(true),
Forms\Components\TextInput::make('part_validation3') Forms\Components\TextInput::make('part_validation1')
->nullable(), ->label('Part Validation 1')
->nullable(),
Forms\Components\TextInput::make('part_validation4') Forms\Components\TextInput::make('part_validation2')
->nullable(), ->label('Part Validation 2')
->nullable(),
Forms\Components\TextInput::make('part_validation5') Forms\Components\TextInput::make('part_validation3')
->nullable(), ->label('Part Validation 3')
->nullable(),
Forms\Components\TextInput::make('laser_part_validation1') Forms\Components\TextInput::make('part_validation4')
->nullable(), ->label('Part Validation 4')
->nullable(),
Forms\Components\TextInput::make('laser_part_validation2') Forms\Components\TextInput::make('part_validation5')
->nullable(), ->label('Part Validation 5 (Capacitor QR)')
->nullable(),
Forms\Components\TextInput::make('panel_box_code') Forms\Components\TextInput::make('laser_part_validation1')
->label('Panel Box Code') ->label('Laser Part Validation 1')
->readOnly(fn (callable $get) => $get('material_type')) ->nullable(),
->nullable(),
Forms\Components\TextInput::make('load_rate') Forms\Components\TextInput::make('laser_part_validation2')
->label('Load Rate') ->label('Laser Part Validation 1')
->default(0) ->nullable(),
->disabled(function ($get) {
return $get('material_type');
})
->integer()
->nullable(),
Forms\Components\TextInput::make('panel_box_code')
->label('Panel Box Code')
->readOnly(fn (callable $get) => $get('material_type'))
->nullable(),
Forms\Components\Select::make('material_type') Forms\Components\TextInput::make('load_rate')
->label('Material Type') ->label('Load Rate')
->options([ ->default(0)
'1' => 'Individual', ->disabled(function ($get) {
'2' => 'Bundle', return $get('material_type');
'3' => 'Quantity', })
]) ->integer()
->reactive() ->nullable(),
->afterStateUpdated(function ($state, callable $set, callable $get) {
if ($state) Forms\Components\Select::make('material_type')
{ ->label('Material Type')
$set('panel_box_code', null); ->options([
$set('load_rate', 0); '1' => 'Individual',
} '2' => 'Bundle',
'3' => 'Quantity',
])
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
if ($state !== "2") if ($state)
{ {
$set('bundle_quantity', null); $set('panel_box_code', null);
} $set('load_rate', 0);
else }
{
$set('bundle_quantity', 2);
}
//$plantId = $get('plant_id');
})
->nullable(),
Forms\Components\TextInput::make('bundle_quantity') if ($state !== "2")
->label('Bundle Quantity') {
->integer() $set('bundle_quantity', null);
->readOnly(fn (callable $get) => $get('material_type') !== "2") }
->nullable() else
->minValue(2) {
->reactive() $set('bundle_quantity', 2);
->afterStateUpdated(function ($state, callable $set,callable $get) { }
if($get('material_type') !== "2") //$plantId = $get('plant_id');
{ })
$set('bundle_quantity', null); ->nullable(),
}
else if ($get('bundle_quantity') < 2)
{
$set('bundle_quantity', 2);
}
}),
Forms\Components\Checkbox::make('serial_number_motor') Forms\Components\TextInput::make('bundle_quantity')
->reactive() ->label('Bundle Quantity')
->disabled(function ($get) { ->integer()
return $get('serial_number_pumpset'); ->readOnly(fn (callable $get) => $get('material_type') !== "2")
}) ->nullable()
->afterStateUpdated(function ($state, callable $set,callable $get) { ->minValue(2)
if ($get('serial_number_pumpset')) ->reactive()
{ ->afterStateUpdated(function ($state, callable $set,callable $get) {
$set('serial_number_motor', false); if($get('material_type') !== "2")
} {
}) $set('bundle_quantity', null);
->dehydrateStateUsing(fn ($state): mixed => $state ? $state : null), }
else if ($get('bundle_quantity') < 2)
{
$set('bundle_quantity', 2);
}
}),
Forms\Components\Checkbox::make('serial_number_pump') Forms\Components\Checkbox::make('serial_number_motor')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('serial_number_pumpset'); return $get('serial_number_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('serial_number_pumpset')) if ($get('serial_number_pumpset'))
{ {
$set('serial_number_pump', false); $set('serial_number_motor', false);
} }
}) })
->dehydrateStateUsing(fn ($state) => $state ? $state : null), ->dehydrateStateUsing(fn ($state): mixed => $state ? $state : null),
Forms\Components\Checkbox::make('serial_number_pumpset') Forms\Components\Checkbox::make('serial_number_pump')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('serial_number_motor') || $get('serial_number_pump'); return $get('serial_number_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('serial_number_pumpset')) if ($get('serial_number_pumpset'))
{ {
$set('serial_number_motor', false); $set('serial_number_pump', false);
$set('serial_number_pump', false); }
} })
}) ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('pack_slip_motor') Forms\Components\Checkbox::make('serial_number_pumpset')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('pack_slip_pumpset'); return $get('serial_number_motor') || $get('serial_number_pump');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('pack_slip_pumpset')) if ($get('serial_number_pumpset'))
{ {
$set('pack_slip_motor', false); $set('serial_number_motor', false);
} $set('serial_number_pump', false);
}) }
->dehydrateStateUsing(fn ($state) => $state ? $state : null), })
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('pack_slip_pump') Forms\Components\Checkbox::make('pack_slip_motor')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('pack_slip_pumpset'); return $get('pack_slip_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('pack_slip_pumpset')) if ($get('pack_slip_pumpset'))
{ {
$set('pack_slip_pump', false); $set('pack_slip_motor', false);
} }
}) })
->dehydrateStateUsing(fn ($state) => $state ? $state : null), ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('pack_slip_pumpset') Forms\Components\Checkbox::make('pack_slip_pump')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('pack_slip_motor') || $get('pack_slip_pump'); return $get('pack_slip_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('pack_slip_pumpset')) if ($get('pack_slip_pumpset'))
{ {
$set('pack_slip_motor', false); $set('pack_slip_pump', false);
$set('pack_slip_pump', false); }
} })
}) ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('name_plate_motor') Forms\Components\Checkbox::make('pack_slip_pumpset')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('name_plate_pumpset'); return $get('pack_slip_motor') || $get('pack_slip_pump');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('name_plate_pumpset')) if ($get('pack_slip_pumpset'))
{ {
$set('name_plate_motor', false); $set('pack_slip_motor', false);
} $set('pack_slip_pump', false);
}) }
->dehydrateStateUsing(fn ($state) => $state ? $state : null), })
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('name_plate_pump') Forms\Components\Checkbox::make('name_plate_motor')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('name_plate_pumpset'); return $get('name_plate_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('name_plate_pumpset')) if ($get('name_plate_pumpset'))
{ {
$set('name_plate_pump', false); $set('name_plate_motor', false);
} }
}) })
->dehydrateStateUsing(fn ($state) => $state ? $state : null), ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('name_plate_pumpset') Forms\Components\Checkbox::make('name_plate_pump')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('name_plate_motor') || $get('name_plate_pump'); return $get('name_plate_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('name_plate_pumpset')) if ($get('name_plate_pumpset'))
{ {
$set('name_plate_motor', false); $set('name_plate_pump', false);
$set('name_plate_pump', false); }
} })
}) ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('tube_sticker_motor') Forms\Components\Checkbox::make('name_plate_pumpset')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('tube_sticker_pumpset'); return $get('name_plate_motor') || $get('name_plate_pump');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('tube_sticker_pumpset')) if ($get('name_plate_pumpset'))
{ {
$set('tube_sticker_motor', false); $set('name_plate_motor', false);
} $set('name_plate_pump', false);
}) }
->dehydrateStateUsing(fn ($state) => $state ? $state : null), })
->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('tube_sticker_pump') Forms\Components\Checkbox::make('tube_sticker_motor')
->reactive() ->reactive()
->disabled(function ($get) { ->disabled(function ($get) {
return $get('tube_sticker_pumpset'); return $get('tube_sticker_pumpset');
}) })
->afterStateUpdated(function ($state, callable $set,callable $get) { ->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('tube_sticker_pumpset')) if ($get('tube_sticker_pumpset'))
{ {
$set('tube_sticker_pump', false); $set('tube_sticker_motor', false);
} }
}) })
->dehydrateStateUsing(fn ($state) => $state ? $state : null), ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
Forms\Components\Checkbox::make('tube_sticker_pumpset') Forms\Components\Checkbox::make('tube_sticker_pump')
->nullable() ->reactive()
->reactive() ->disabled(function ($get) {
->disabled(function ($get) { return $get('tube_sticker_pumpset');
return $get('tube_sticker_motor') || $get('tube_sticker_pump'); })
}) ->afterStateUpdated(function ($state, callable $set,callable $get) {
->afterStateUpdated(function ($state, callable $set,callable $get) { if ($get('tube_sticker_pumpset'))
if ($get('tube_sticker_pumpset')) {
{ $set('tube_sticker_pump', false);
$set('tube_sticker_motor', false); }
$set('tube_sticker_pump', false); })
} ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
})
->dehydrateStateUsing(fn ($state) => $state ? $state : null), //to pass null value
Forms\Components\Checkbox::make('warranty_card') Forms\Components\Checkbox::make('tube_sticker_pumpset')
->nullable() ->nullable()
->dehydrateStateUsing(fn ($state) => $state ? $state : null), ->reactive()
->disabled(function ($get) {
return $get('tube_sticker_motor') || $get('tube_sticker_pump');
})
->afterStateUpdated(function ($state, callable $set,callable $get) {
if ($get('tube_sticker_pumpset'))
{
$set('tube_sticker_motor', false);
$set('tube_sticker_pump', false);
}
})
->dehydrateStateUsing(fn ($state) => $state ? $state : null), //to pass null value
Forms\Components\TextInput::make('id') Forms\Components\Checkbox::make('warranty_card')
->hidden() ->nullable()
->readOnly(), ->dehydrateStateUsing(fn ($state) => $state ? $state : null),
]); Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
]);
} }
public static function table(Table $table): Table public static function table(Table $table): Table
@@ -439,13 +443,13 @@ class StickerMasterResource extends Resource
// ->numeric() // ->numeric()
// ->sortable(), // ->sortable(),
Tables\Columns\TextColumn::make('No.') Tables\Columns\TextColumn::make('No.')
->label('No.') ->label('No.')
->getStateUsing(function ($record, $livewire, $column, $rowLoop) { ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
Tables\Columns\TextColumn::make('plant.name') Tables\Columns\TextColumn::make('plant.name')
->label('Plant') ->label('Plant')
->alignCenter() ->alignCenter()