form->fill([ 'plant_id' => $this->plantId, 'line_id' => $this->lineId, ]); } public function form(Form $form): Form { return $form ->statePath('filters') ->schema([ Section::make('') ->schema([ Select::make('plant_id') ->label('Plant Name') ->relationship('plant', 'name') ->reactive() ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->afterStateUpdated(function (callable $set, callable $get, $state) { $set('item_id', null); $set('line_id', null); $set('plant', $state); $pId = $get('plant_id'); if (! $pId) { $set('pqPlantError', 'Please select a plant first.'); } else { $set('pqPlantError', null); } $pId = $get('line_id'); if (! $pId) { $set('pqLineError', null); } }) ->required() ->default(fn ($get) => $get('plant') ?? session('last_selected_plant_id')) ->extraAttributes(fn ($get) => [ 'class' => $get('pqPlantError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null) ->hintColor('danger'), Select::make('line_id') ->label('Line Name') ->relationship('line', titleAttribute: 'name') ->reactive() ->required() ->options(function (callable $get) { $plantId = $get('plant_id'); if (! $plantId) { return []; } return Line::where('plant_id', $plantId) ->pluck('name', 'id') ->toArray(); }) ->afterStateUpdated(function (callable $set, callable $get, $state) { $set('item_id', null); $set('line', $state); $pId = $get('line_id'); if (! $pId) { $set('pqLineError', 'Please select a line.'); } else { $set('pqLineError', null); } }) ->default(fn ($get) => $get('line') ?? session('last_selected_line')) ->extraAttributes(fn ($get) => [ 'class' => $get('pqLineError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqLineError') ? $get('pqLineError') : null) ->hintColor('danger'), TextInput::make('item_id') ->label('Scan QR Code') ->reactive() ->extraAttributes([ 'wire:keydown.enter' => 'processScan($event.target.value)', ]), ]) ->columns(3), ]); } public function processScan($value) { $plantId = $this->filters['plant_id']; $plantId = trim($plantId) ?? null; $lineId = $this->filters['line_id']; $lineId = trim($lineId) ?? null; $itemId = $this->filters['item_id']; $itemId = trim($itemId) ?? null; if (! $plantId) { Notification::make() ->title('Unknown Plant') ->body('Please select a plant.') ->warning() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } else if (! $lineId) { Notification::make() ->title('Unknown Line') ->body('Please select a line.') ->warning() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } $plaCode = Plant::find($plantId)?->code; // Check if QR format contains '|' // if ($itemId && str_contains($itemId, '|')) // { // if (strpos($itemId, '|') == false) { // Notification::make() // ->title('Unknown Qr Code') // ->body('Scan valid QR code. (Ex: Item_Code|Serial_Number)') // ->warning() // ->send(); // return; // } // if (! preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $itemId)) { // $splits = explode('|', $itemId); // $iCode = trim($splits[0]); // $sNumber = isset($splits[1]) ? trim($splits[1]) : null; // if (! ctype_alnum($iCode)) { // Notification::make() // ->title('Invalid Item Code') // ->body('Item code must contain alpha-numeric values.') // ->warning() // ->send(); // return; // } elseif (strlen($iCode) < 6) { // Notification::make() // ->title('Invalid Item Code') // ->body('Item code must be at least 6 digits.') // ->warning() // ->send(); // return; // } elseif (! ctype_alnum($sNumber)) { // Notification::make() // ->title('Invalid Serial Number') // ->body('Serial number must contain alpha-numeric values.') // ->warning() // ->send(); // return; // } // $iId = Item::where('code', $iCode)->where('plant_id', $plantId)->first(); // if(!$iId){ // Notification::make() // ->title('Item not found') // ->body("Item code'{$iCode}' not found against plant code '{$plaCode}' in item master.") // ->danger() // ->send(); // return; // } // $sticker = StickerMaster::where('plant_id', $plantId)->where('item_id', $iId->id)->first(); // if(!$sticker){ // Notification::make() // ->title('Unknown Item Code') // ->body("Item code not found '{$iId->code}' in sticker master.") // ->danger() // ->send(); // return; // } // } // } // if (str_contains($itemId, '|')) { // $parts = array_map('trim', explode('|', $itemId)); // if (count($parts) != 2) { // Notification::make() // ->title('Invalid QR Format') // ->body('Expected format: ItemCode|SerialNumber') // ->danger() // ->send(); // return; // } // $itemCode = $parts[0]; // $serialNumber = $parts[1]; // if($serialNumber == '' || $serialNumber == null){ // Notification::make() // ->title('Unknown Serial number') // ->body("Serial number can't be empty!") // ->danger() // ->send(); // return; // } // elseif (! ctype_alnum($serialNumber)) { // Notification::make() // ->title('Unknown Serial number') // ->body("Serial Number should contain alpha-numeric values.") // ->danger() // ->send(); // return; // } // $iId = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); // if(!$iId){ // Notification::make() // ->title('Item not found') // ->body("Item code'{$itemCode}' not found against plant code '{$plaCode}' in item master.") // ->danger() // ->send(); // return; // } // } if (str_contains($itemId, '/')) { $parts = explode('/', $itemId); if (count($parts) < 3) { Notification::make() ->title('Invalid QR Format') ->body('Expected format: plantcode/itemcode/yearmm/serialnumber (Or) plantcode/panelboxcode/serialnumber') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } $supplier = $parts[0]; $panelBoxCode = $parts[1]; $panelBoxSerialNo = implode('/', array_slice($parts, 2)); $itemCode = $panelBoxCode; $serialNumber = $panelBoxSerialNo; $iId = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); if(!$iId){ Notification::make() ->title('Item not found') ->body("Item code'{$itemCode}' not found against plant code '{$plaCode}' in item master.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } $sticker = StickerMaster::where('plant_id', $plantId)->where('item_id', $iId->id)->first(); if(!$sticker){ Notification::make() ->title('Unknown Item Code') ->body("Item code not found '{$iId->code}' in sticker master.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } } else { Notification::make() ->title('Invalid QR Format') ->body('Expected format: plantcode/itemcode/yearmm/serialnumber (Or) plantcode/panelboxcode/serialnumber') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } if (strlen($itemCode) < 6) { Notification::make() ->title('Unknown Item Code') ->body('Item code must be at least 6 digits.') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } elseif (! ctype_alnum($itemCode)) { Notification::make() ->title('Unknown Item Code') ->body('Item Code should contain alpha-numeric values.') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } if (! $plantId) { Notification::make() ->title('Unknown Plant') ->body('Please select a plant first.') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } // Check if the item exists for the selected plant $stickerMaster = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($itemCode) { $query->where('code', $itemCode); })->first(); if (! $stickerMaster) { Notification::make() ->title('Unknown Item Code') ->body('Item code does not exist in master data.') ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } else { if (! $stickerMaster->item->uom) { Notification::make() ->title('Unknown Item Code') ->body("Item code does not have 'UOM' in master data.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } } $serialExists = PanelBoxValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first(); if ($serialExists) { Notification::make() ->title('Duplicate Serial Number') ->body("Serial number already exists $serialNumber.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); if (! $item) { Notification::make() ->title('Unknown Item Code') ->body("Item code '{$itemCode}' not found against plant code '{$plaCode}' in item master.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } // Find sticker master $sticker = StickerMaster::where('plant_id', $plantId)->where('item_id', $item->id)->first(); if (! $sticker) { Notification::make() ->title('Unknown Sticker') ->body("Sticker master not found for the selected item.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } // Create a new PanelBoxValidation record $userName = Filament::auth()->user()->name; $panelBoxValidation = PanelBoxValidation::create([ 'plant_id' => $plantId, 'line_id' => $lineId, 'sticker_master_id' => $sticker->id, 'serial_number' => $serialNumber, 'panel_box_code' => $itemCode ?? null, 'panel_box_supplier' => $supplier ?? null, 'panel_box_serial_number' => $serialNumber ?? null, 'status' => 'Ok', 'created_by' => $userName, 'updated_by' => $userName, ]); if($panelBoxValidation){ Notification::make() ->title('Success') ->body("Panel Box with Serial Number '{$serialNumber}' has been successfully inserted.") ->success() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } else { Notification::make() ->title('Error') ->body("Failed to insert Panel Box with Serial Number '{$serialNumber}'.") ->danger() ->send(); $this->form->fill([ 'plant_id' => $plantId, 'line_id' => $lineId, 'item_id' => null, ]); return; } } public static function canAccess(): bool { return Auth::check() && Auth::user()->can('view panel box inspection stock page'); } }