serialNumbers = collect($this->serialNumbers) ->filter(fn ($serial) => trim($serial) !== '') ->values() ->toArray(); // Clear all values whenever the checkbox changes $this->serialNumbers = ['']; } public function splitSerialNumbers(string $text): void { $serials = preg_split('/[\r\n\t ,;]+/', trim($text)); $this->serialNumbers = collect($serials) ->filter(fn ($serial) => $serial !== '') ->values() ->toArray(); if (empty($this->serialNumbers)) { $this->serialNumbers = ['']; } $plantId = $this->plantId; $machineId = $this->machineId; if (!$plantId) { Notification::make() ->danger() ->title('Plant Not Selected') ->body('Please select a plant before pasting serial numbers.') ->send(); $this->dispatch('sticker-options-clear', stickerOptions: ''); $this->dispatch('production-order-clear', productionOrder: ''); return; } $serialNumbersArray = array_values( array_filter( array_map('trim', $this->serialNumbers), fn ($serial) => $serial !== '' ) ); if (count($serialNumbersArray) > 50) { Notification::make() ->title('Too Many Serial Numbers') ->body('You can enter a maximum of 50 serial numbers.') ->danger() ->send(); $this->dispatch('sticker-options-clear', stickerOptions: ''); $this->dispatch('production-order-clear', productionOrder: ''); $this->serialNumbers = ['']; return; } $records = ClassCharacteristic::where('plant_id', $plantId)->whereIn('gernr', $serialNumbersArray)->get(); $foundSerialNumbers = $records->pluck('gernr') ->map(fn ($serial) => trim((string) $serial)) ->unique() ->toArray(); $missingSerialNumbers = array_values( array_diff($serialNumbersArray, $foundSerialNumbers) ); if (!empty($missingSerialNumbers)) { Notification::make() ->danger() ->title('Serial Numbers Not Found') ->body('The following serial numbers do not exist for the selected plant: '. implode(', ', $missingSerialNumbers)) ->send(); return; } $productionOrder = $records->first()->aufnr; $invalidSerialNumbers = $productionOrder ? $records->filter( fn ($record) => $record->aufnr != $productionOrder )->pluck('gernr')->unique()->values()->toArray() : []; if (!empty($invalidSerialNumbers)) { Notification::make() ->danger() ->title('Invalid Serial Numbers') ->body('The following serial numbers belong to a different production order: '. implode(', ', $invalidSerialNumbers)) ->send(); return; } $record = $records->first(); $itemId = $record->item_id; $item = Item::find($itemId); if (! $item) { Notification::make() ->title('Item not found') ->body("Item code '{$itemId}' was not found in item master.") ->danger() ->send(); $this->dispatch('sticker-options-clear', stickerOptions: ''); $this->dispatch('production-order-clear', productionOrder: ''); return; } $itemCode = $item->code; $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); if (! $item) { Notification::make() ->title('Item not found') ->body("Item '{$itemCode}' was not found for the selected plant.") ->danger() ->send(); $this->dispatch('sticker-options-clear', stickerOptions: ''); $this->dispatch('production-order-clear', productionOrder: ''); return; } $itemCharacteristic = ClassCharacteristic::where('plant_id', $plantId)->where('item_id', $item->id)->first(); if (! $itemCharacteristic) { Notification::make() ->title('Class characteristic not found') ->body("No class characteristic found for item '{$itemCode}'.") ->danger() ->send(); return; } // $itemCharacteristicId = $itemCharacteristic->id; $stickerMappings = StickerMappingMaster::where('plant_id', $plantId) ->where('item_id', $item->id) ->get(); if (! $stickerMappings) { Notification::make() ->title('Sticker mapping not found') ->body("No sticker mapping found for this plant and item '$itemCode'.") ->danger() ->send(); return; } $stickerOptions = []; foreach ($stickerMappings as $stickerMapping) { for ($i = 1; $i <= 8; $i++) { $machineColumn = "sticker{$i}_machine_id"; $stickerMachineId = $stickerMapping->$machineColumn; if ((string) $stickerMachineId != (string) $machineId) { continue; } $structureColumn = "sticker_structure{$i}_id"; $stickerStructureId = $stickerMapping->$structureColumn; if (empty($stickerStructureId)) { continue; } $detail = StickerStructureDetail::find($stickerStructureId); if ($detail && $detail->sticker_id) { $stickerOptions[$detail->sticker_id] = $detail->sticker_id; } $this->dispatch('sticker-structure-updated', stickerStructureId: $stickerStructureId); } } $this->dispatch('sticker-options', stickerOptions: $stickerOptions); $this->dispatch('production-order-updated', productionOrder: $productionOrder); } public function generate(): void { $serialNumbersString = implode(',', array_filter( $this->serialNumbers, fn ($serial) => trim($serial) !== '' )); $this->dispatch('generate-sticker-id', serialNumbers: $serialNumbersString); } public function clear(){ $this->serialNumbers = ['']; $this->dispatch('sticker-options-clear', stickerOptions: ''); $this->dispatch('production-order-clear', productionOrder: ''); } public function render() { return view('livewire.random-serial-number-list'); } }