From 828bfce192f363df0ce214780995521226cad484 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 12 Nov 2026 15:27:34 +0530 Subject: [PATCH 1/2] Added test certificate page --- app/Filament/Pages/TestCertificate.php | 1775 +++++++++++++++++ .../filament/pages/test-certificate.blade.php | 103 + 2 files changed, 1878 insertions(+) create mode 100644 app/Filament/Pages/TestCertificate.php create mode 100644 resources/views/filament/pages/test-certificate.blade.php diff --git a/app/Filament/Pages/TestCertificate.php b/app/Filament/Pages/TestCertificate.php new file mode 100644 index 00000000..e23b5c6c --- /dev/null +++ b/app/Filament/Pages/TestCertificate.php @@ -0,0 +1,1775 @@ +form->fill(); + } + + public function form(Form $form): Form + { + return $form + ->statePath('filters') + ->schema([ + Section::make('') + ->schema([ + Select::make('plant_id') + ->label('Plant') + ->reactive() + ->options(function(){ + + $plantId = Filament::auth()->user()->plant_id; + + return $plantId + ? Plant::where('id',$plantId) + ->pluck('name','id') + : Plant::pluck('name','id'); + + }) + ->required(), + Select::make('machine_id') + ->label('Work Center') + ->reactive() + ->options(function (callable $get) { + + $plantId = $get('plant_id'); + + if (empty($plantId)) { + return []; + } + + return Machine::where('plant_id', $plantId) + ->pluck('work_center', 'id') + ->toArray(); + }) + ->required(), + TextInput::make('production_order') + ->label('Production Order') + ->reactive() + ->afterStateUpdated(function ($state, callable $set, $get) { + + for ($i = 1; $i <= 8; $i++) { + $set("sticker_structure{$i}_id", null); + } + + if (blank($state)) { + return; + } + + $plantid = $get('plant_id'); + $machineId = $get('machine_id'); + + $plantName = Plant::where('id', $plantid)->value('name'); + + $workCenter = Machine::where('id', $machineId)->value('work_center'); + + if (empty($machineId)) { + Notification::make() + ->title('Work Center not selected') + ->body('Please select a Work Center first.') + ->warning() + ->send(); + + return; + } + + $productionOrder = ClassCharacteristic::where('plant_id', $plantid)->where('machine_id', $machineId)->where('aufnr', $state)->first(); + + if (! $productionOrder) { + Notification::make() + ->title('Production Order not found') + ->body("Production Order '{$state}' was not found against machine '$workCenter' and plant '$plantName'.") + ->danger() + ->send(); + + $this->dispatch('clear-serial-numbers'); + return; + } + + $plantId = $productionOrder->plant_id; + $itemId = $productionOrder->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(); + + 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('clear-serial-numbers'); + 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(); + $this->dispatch('clear-serial-numbers'); + return; + } + + $itemCharacteristicId = $itemCharacteristic->id; + + $stickerMappings = StickerMappingMaster::where('plant_id', $plantId) + ->where('item_id', $item->id) + ->get(); + + if ($stickerMappings->isEmpty()) { + Notification::make() + ->title('Sticker mapping not found') + ->body("No sticker mapping found for item '$itemCode' and plant '$plantName'.") + ->danger() + ->send(); + $this->dispatch('clear-serial-numbers'); + return; + } + + $stickerOptions = []; + + // foreach ($stickerMappings as $stickerMapping) { + + // for ($i = 1; $i <= 8; $i++) { + + // $stickerStructureId = $stickerMapping->{"sticker_structure{$i}_id"}; + + // if ($stickerStructureId) { + + // $detail = StickerStructureDetail::where('id', $stickerStructureId) + // ->first(); + + // if ($detail && $detail->sticker_id) { + // $stickerOptions[$detail->sticker_id] = $detail->sticker_id; + // } + // } + // } + // } + + 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; + } + + $set("sticker_structure{$i}_id", $stickerStructureId); + + } + } + + $set('sticker_options', $stickerOptions); + + $serialNumbers = ClassCharacteristic::query() + ->where('plant_id', $plantId) + ->where('aufnr', $state) + ->pluck('gernr') + ->filter() + ->values() + ->all(); + + $this->dispatch('load-serial-numbers',serialNumbers: $serialNumbers); + + }), + + Select::make('sticker_id') + ->label('Sticker ID') + ->options(fn (callable $get) => $get('sticker_options') ?? []) + ->reactive() + ->afterStateUpdated(function ($state, callable $set) { + + if (blank($state)) { + return; + } + + $sticker = StickerStructureDetail::where('sticker_id', $state)->first(); + + if (! $sticker) { + Notification::make() + ->title('Sticker not found') + ->body("Sticker ID '{$state}' was not found.") + ->danger() + ->send(); + + return; + } + + $set('sticker_code', $state); + + }), + Hidden::make('sticker_options') + ->default([]), + TextInput::make('from_serial_number') + ->label('From Serial Number') + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + + $plantId = $get('plant_id'); + $machineId = $get('machine_id'); + + // $record = ClassCharacteristic::where('plant_id', $plantId)->where('machine_id', $machineId)->where('gernr', $state)->first(); + + $record = ClassCharacteristic::where('plant_id', $plantId)->where('gernr', $state)->first(); + + if (! $record) { + $plantName = Plant::find($plantId)?->name; + // $machineName = Machine::find($machineId)?->work_center; + Notification::make() + ->title('Serial Number Not Found') + ->body("Serial number '{$state}' was not found against plant '$plantName'.") + ->danger() + ->send(); + + return; + } + + $plantName = Plant::where('id', $plantId)->value('name'); + + // $workCenter = Machine::where('id', $machineId)->value('work_center'); + + // if (empty($machineId)) { + // Notification::make() + // ->title('Work Center not selected') + // ->body('Please select a Work Center first.') + // ->warning() + // ->send(); + + // return; + // } + + // $productionOrder = ClassCharacteristic::where('plant_id', $plantId)->where('machine_id', $machineId)->where('gernr', $state)->first(); + + $productionOrder = ClassCharacteristic::where('plant_id', $plantId)->where('gernr', $state)->first(); + + if (! $productionOrder) { + Notification::make() + ->title('Production Order not found') + ->body("Production Order '{$state}' was not found against Serial Number '$state' and plant '$plantName'.") + ->danger() + ->send(); + + return; + } + + $plantId = $productionOrder->plant_id; + $itemId = $productionOrder->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(); + + 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(); + + 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 class characteristic.") + ->danger() + ->send(); + + return; + } + + $stickerOptions = []; + + $machineOptions = []; + + $stickerMachineId = null; + + foreach ($stickerMappings as $stickerMapping) { + + for ($i = 1; $i <= 8; $i++) { + + $machineColumn = "sticker{$i}_machine_id"; + + $stickerMachineId = $stickerMapping->$machineColumn; + + // if ((string) $stickerMachineId != (string) $machineId) { + // continue; + // } + + $machineOptions[] = $stickerMachineId; + + $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; + } + + $set("sticker_structure{$i}_id", $stickerStructureId); + + } + } + + if (! empty($machineOptions)) { + $set('machine_id', (string) $machineOptions[0]); + } + $set('sticker_options', $stickerOptions); + $set('production_order', $record->aufnr); + }), + TextInput::make('to_serial_number') + ->label('To Serial Number') + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + + if (blank($state)) { + return; + } + + $productionOrder = $get('production_order'); + $plantId = $get('plant_id'); + $machineId = $get('machine_id'); + + $toSerialRecord = ClassCharacteristic::where('plant_id', $plantId)->where('gernr', $state)->first(); + + if (! $toSerialRecord) { + Notification::make() + ->title('Serial Number Not Found') + ->body("To Serial Number '{$state}' was not found.") + ->danger() + ->send(); + + $set('to_serial_number', null); + + return; + } + + if ((string) $toSerialRecord->aufnr != (string) $productionOrder) { + Notification::make() + ->title('Production Order Mismatch') + ->body("To Serial Number '{$state}' does not belong to Production Order '{$productionOrder}'.") + ->danger() + ->send(); + + $set('to_serial_number', null); + + return; + } + + }), + ]) + ->columns(6), + ]); + } + + public function processSnoNo($qrcode) + { + + $parts = explode('|', $qrcode, 2); + + $itemCode = $parts[0] ?? null; + $serialNumber = $parts[1] ?? null; + + $plantId = $this->form->getState()['plant_id']; + + $plantId = trim($plantId) ?? null; + + $workCenter = $this->form->getState()['machine_id']; + + $prodOrderNo= $this->form->getState()['production_order']; + + $prodOrderNo = trim($prodOrderNo) ?? null; + + + if (!$qrcode || !preg_match('/^\d+\|\d+$/', $qrcode)) { + Notification::make() + ->title('Invalid QR Code') + ->body('QR code format should be like: 123456|12456456464') + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + elseif (!preg_match('/^[A-Za-z0-9]+$/', $itemCode)) { + Notification::make() + ->title('Invalid Item Code') + ->body('Item code should contain only alpha-numeric values.') + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + elseif (!preg_match('/^[A-Za-z0-9]+$/', $serialNumber)) { + Notification::make() + ->title('Invalid Serial Number') + ->body('Serial number should contain only alpha-numeric values.') + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + + $plant = Plant::find($plantId); + + $plantName = $plant ? $plant->name : null; + + $pOrderExist = ProductionOrder::where('plant_id', $plantId) + ->where('production_order', $prodOrderNo) + ->first(); + + if(!$pOrderExist){ + Notification::make() + ->title('Unknown Production Order') + ->body("Production Order not found against plant '$plantName'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + + $itemExist = Item::where('code', $itemCode)->first(); + + $itemAgaPlant = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); + + if(!$itemExist){ + Notification::make() + ->title('Unknown Item Code') + ->body("Item code '$itemCode' not found.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + elseif(!$itemAgaPlant){ + Notification::make() + ->title('Unknown Item Code') + ->body("Item code '$itemCode' not found against the the plant '$plantName'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + elseif ($itemAgaPlant->id != $pOrderExist->item_id) { + Notification::make() + ->title('Item Code Mismatch') + ->body("Item code '$itemCode' does not match the item associated with production order '$prodOrderNo'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + + $productionOrders = ProductionOrder::where('plant_id', $plantId) + ->where('production_order', $prodOrderNo) + ->where('item_id', $itemAgaPlant->id) + ->get(); + + $serialExist = null; + + foreach ($productionOrders as $productionOrder) { + + for ( + $serial = (int) $productionOrder->from_serial_number; + $serial <= (int) $productionOrder->to_serial_number; + $serial++ + ) { + if ($serial == (int) $serialNumber) { + $serialExist = $productionOrder; + break 2; + } + } + } + + if(!$serialExist){ + Notification::make() + ->title('Serial Number Not Found') + ->body("Serial number '$serialNumber' not found for production order '$prodOrderNo', item '$itemCode' and plant '$plantName'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'serial_number' => null, + ]); + + return; + } + + + //..Generate Sticker PDF and download it + + $this->ref_number = $this->form->getState()['production_order']; + + $this->serNo = $serialNumber; + + $user = Filament::auth()->user(); + + $operatorName = $user->name; + + $duplicate = StickerValidation::where('plant_id', $plantId) + ->where('production_order', $this->ref_number) + ->where('serial_number', $serialNumber) + ->first(); + + if ($duplicate) { + Notification::make() + ->danger() + ->title('Duplicate Serial Number') + ->body("Serial number $serialNumber already exists for this plant and production order!") + ->seconds(3) + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $this->ref_number, + 'serial_number' => null, + ]); + return; + } + + $itemC = Item::where('code', $itemCode) + ->where('plant_id',$plantId) + ->first(); + + $itemId = $itemC->id; + + $item = ClassCharacteristic::where('item_id', $itemId) + ->where('plant_id',$plantId) + ->first(); + + $itemI = $item->id; + + $mapping = StickerMappingMaster::where('plant_id', $plantId) + ->where('sticker1_machine_id', $workCenter) + ->where('item_characteristic_id', $itemI) + ->first(); + + if (!$mapping) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No sticker mapping found for this item and plant.") + ->send(); + return; + } + + $stickers = []; + + for ($i = 1; $i <= 8; $i++) { + $machineColumn = "sticker{$i}_machine_id"; + $ipColumn = "sticker{$i}_print_ip"; + $stickerColumn = "sticker_structure{$i}_id"; + $itemColumn = "item_characteristic_id"; + + if ( + !empty($mapping->$machineColumn) && + !empty($mapping->$stickerColumn) + ) { + $stickers[] = [ + 'machine_id' => $mapping->$machineColumn, + 'sticker_id' => $mapping->$stickerColumn, + 'item_characteristic' => $mapping->$itemColumn, + 'print_ip' => $mapping->$ipColumn, + ]; + } + } + + if (empty($stickers)) { + Notification::make() + ->danger() + ->title('No Sticker Configuration Found') + ->body('No sticker and machine mappings configured for this item and plant.') + ->send(); + return; + } + + + StickerValidation::create([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $this->ref_number ?? null, + 'serial_number' => $serialNumber, + 'status' => 'Printed', + // 'sticker_id' => $matchedSticker, + 'created_by' => $operatorName, + 'created_at' => now(), + 'updated_at' => now(), + ]); + + Notification::make() + ->success() + ->title('Sticker Recorded') + ->body("Item: $itemCode, Serial: $serialNumber recorded successfully!") + ->seconds(3) + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $this->ref_number, + 'serial_number' => null, + ]); + + $pdfUrls = []; + + foreach ($stickers as $sticker) + { + + $structure = StickerStructureDetail::findOrFail($sticker['sticker_id']); + + $itemCharacteristic = ClassCharacteristic::where('plant_id', $plantId) + ->where('id', $sticker['item_characteristic']) + ->firstOrFail(); + + $dynamicElements = StickerDetail::where( + 'sticker_structure_detail_id', + $structure->id + )->where('element_type', 'Dynamic')->get(); + + $pdfContent = (new StickerPdfService())->generatePdf1( + $structure->sticker_id, + $dynamicElements, + $itemCharacteristic, + $serialNumber, + $itemCode, + $plantId + ); + + $tempPdfPath = storage_path('app/temp_sticker_' . uniqid() . '.pdf'); + + file_put_contents($tempPdfPath, $pdfContent); + + // $pdfUrl = route('sticker.preview', [ + // 'path' => basename($tempPdfPath), + // ]); + $pdfUrls[] = route('sticker.preview', ['path' => basename($tempPdfPath)]); + } + + $this->dispatch('open-sticker-pdf', urls: $pdfUrls); + + Notification::make() + ->success() + ->title('Sticker Printed') + ->body("Sticker for Serial Number: $serialNumber printed successfully!") + ->seconds(3) + ->send(); + } + + public function generateSticker() + { + $plantId = $this->form->getState()['plant_id']; + + $plantId = trim($plantId) ?? null; + + $workCenter = $this->form->getState()['machine_id']; + + $prodOrderNo= $this->form->getState()['production_order']; + + $prodOrderNo = trim($prodOrderNo) ?? null; + + $fromSno= $this->form->getState()['from_serial_number']; + + $fromSno = trim($fromSno) ?? null; + + $toSno= $this->form->getState()['to_serial_number']; + + $toSno = trim($toSno) ?? null; + + $serialExist = ProductionOrder::where('plant_id', $plantId) + ->where('production_order', $prodOrderNo) + ->where('from_serial_number', '=', $fromSno) + ->where('to_serial_number', '=', $toSno) + ->first(); + + if(!$serialExist){ + Notification::make() + ->title('Serial Number Range Not Found') + ->body("Serial number range '$fromSno' to '$toSno' not found for production order '$prodOrderNo'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'machine_id' => $workCenter, + 'production_order' => $prodOrderNo, + 'from_serial_number' => null, + 'to_serial_number' => null, + ]); + + return; + } + else + { + + if($serialExist){ + $itemId = $serialExist->item_id; + $itemCode = Item::where('id', $itemId) + ->where('plant_id', $serialExist->plant_id) + ->value('code'); + } + + $this->ref_number = $this->form->getState()['production_order']; + + $fromSerNo = $this->form->getState()['from_serial_number']; + + $toSerNo = $this->form->getState()['to_serial_number']; + + $user = Filament::auth()->user(); + + $operatorName = $user->name; + + $itemC = Item::where('code', $itemCode) + ->where('plant_id',$plantId) + ->first(); + + $itemId = $itemC->id; + + $item = ItemCharacteristic::where('item_id', $itemId) + ->where('plant_id',$plantId) + ->first(); + + $itemI = $item->id; + + $mapping = StickerMappingMaster::where('plant_id', $plantId) + ->where('sticker1_machine_id', $workCenter) + ->where('item_characteristic_id', $itemI) + ->first(); + + if (!$mapping) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No sticker mapping found for this item and plant.") + ->send(); + return; + } + + $stickers = []; + + for ($i = 1; $i <= 8; $i++) { + $machineColumn = "sticker{$i}_machine_id"; + $ipColumn = "sticker{$i}_print_ip"; + $stickerColumn = "sticker_structure{$i}_id"; + $itemColumn = "item_characteristic_id"; + + if ( + !empty($mapping->$machineColumn) && + !empty($mapping->$stickerColumn) + ) { + $stickers[] = [ + 'machine_id' => $mapping->$machineColumn, + 'sticker_id' => $mapping->$stickerColumn, + 'item_characteristic' => $mapping->$itemColumn, + 'print_ip' => $mapping->$ipColumn, + ]; + } + } + + if (empty($stickers)) { + Notification::make() + ->danger() + ->title('No Sticker Configuration Found') + ->body('No sticker and machine mappings configured for this item and plant.') + ->send(); + return; + } + + $fromSerNo = (int) $this->form->getState()['from_serial_number']; + $toSerNo = (int) $this->form->getState()['to_serial_number']; + + if ($fromSerNo > $toSerNo) { + Notification::make() + ->danger() + ->title('Invalid Serial Number Range') + ->body("From Serial Number must be less than or equal to To Serial Number.") + ->send(); + + return; + } + + $pdfUrls = []; + + $stickerService = new StickerPdfService(); + + foreach ($stickers as $sticker) { + + $structure = StickerStructureDetail::findOrFail($sticker['sticker_id']); + + $itemCharacteristic = ItemCharacteristic::where('plant_id',$plantId)->where('id',$sticker['item_characteristic'])->firstOrFail(); + + $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); + + $template = $stickerService->generateTemplate( + $structure->sticker_id, + $elements, + $itemCharacteristic, + $itemCode, + $plantId + ); + + $pdfContent = $stickerService->generateBulkFromTemplate( + $template, + $fromSerNo, + $toSerNo, + $itemCode + ); + + $fileName ='bulk_sticker_' .$fromSerNo .'_' .$toSerNo .'_' .uniqid() .'.pdf'; + + $tempPdfPath = storage_path('app/' . $fileName); + + file_put_contents($tempPdfPath,$pdfContent); + + $pdfUrls[] = route('sticker.preview',['path' => $fileName,]); + } + + $this->dispatch('open-sticker-pdf',urls: $pdfUrls); + + Notification::make() + ->success() + ->title('Sticker Printed') + ->body("Sticker printed successfully!") + ->seconds(3) + ->send(); + } + } + + + public function generateStickerId() + { + $state = $this->form->getState(); + + $plantId = trim($state['plant_id'] ?? ''); + + $machineId = trim($state['machine_id'] ?? ''); + + $stickerId = $state['sticker_id'] ?? null; + + $pOrder = $state['production_order'] ?? null; + + $fromSno = trim($state['from_serial_number'] ?? ''); + + $toSno = trim($state['to_serial_number'] ?? ''); + + $plantName = Plant::where('id', $plantId)->value('name'); + + if (!$stickerId) { + + Notification::make() + ->danger() + ->title('Sticker ID Not Selected') + ->body('Please Choose a Sticker ID.') + ->send(); + + return; + } + + if ($fromSno == '' || $toSno == '') { + + Notification::make() + ->danger() + ->title('Serial Number Required') + ->body('Please enter both From Serial Number and To Serial Number.') + ->send(); + + return; + } + + if (strlen($fromSno) < 9) { + Notification::make() + ->danger() + ->title('Invalid From Serial Number') + ->body("From Serial Number '$fromSno' should contain minimum 9 digits.") + ->send(); + + return; + } + + if (strlen($toSno) < 9) { + Notification::make() + ->danger() + ->title('Invalid To Serial Number') + ->body("To Serial Number '$toSno' should contain minimum 9 digits.") + ->send(); + + return; + } + + $fromSerNo = (int) $fromSno; + + $toSerNo = (int) $toSno; + + if ($fromSerNo > $toSerNo) { + + Notification::make() + ->danger() + ->title('Invalid Serial Number Range') + ->body('From Serial Number must be less than or equal to To Serial Number.') + ->send(); + + return; + } + + $pOrderExist = ClassCharacteristic::where('plant_id',$plantId)->where('aufnr', $pOrder)->first(); + + if(!$pOrderExist){ + Notification::make() + ->danger() + ->title('Unknown Production Order') + ->body("Production Order '$pOrder' not found.") + ->send(); + return; + } + + if($pOrderExist){ + + $pOrderExistData = ClassCharacteristic::where('plant_id', $plantId) + ->where('aufnr', $pOrder) + ->pluck('gernr') + ->map(fn ($value) => (int) $value) + ->toArray(); + + if (!in_array((int) $fromSerNo, $pOrderExistData, true) || !in_array((int) $toSerNo, $pOrderExistData, true)) + { + Notification::make() + ->danger() + ->title('Unknown Serial Number') + ->body("The entered serial number range does not match Production Order '$pOrder' for plant '$plantName' in class characteristic.") + ->send(); + return; + } + } + + $existingSticker = StickerOrder::where('plant_id', $plantId) + ->where('production_order', $pOrder) + ->where('sticker_id', $stickerId) + ->where('print_status', 'Printed') + ->whereIn('serial_number', [$fromSerNo, $toSerNo]) + ->first(); + + if ($existingSticker) { + Notification::make() + ->danger() + ->title('Duplicate Sticker') + ->body("Serial Number '$fromSerNo' or '$toSerNo' already exists in a generated sticker '$stickerId' for Production Order '$pOrder' and plant '$plantName'.") + ->send(); + + return; + } + + $structure = StickerStructureDetail::where('sticker_id',$stickerId)->first(); + + if (!$structure) { + + Notification::make() + ->danger() + ->title('Sticker Structure Not Found') + ->body("No sticker structure found for sticker ID '{$stickerId}'.") + ->send(); + return; + } + + //.New Logic + + $stickerMappingMasterItems = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->get(); + + if ($stickerMappingMasterItems->isEmpty()) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No sticker mapping found for plant ID '{$plantId}'.") + ->send(); + + return; + } + + $structureIds = $stickerMappingMasterItems + ->flatMap(function ($mapping) { + $ids = []; + + for ($i = 1; $i <= 8; $i++) { + $column = "sticker_structure{$i}_id"; + + if (!empty($mapping->{$column})) { + $ids[] = $mapping->{$column}; + } + } + + return $ids; + }) + ->unique() + ->values(); + + $detailsById = StickerStructureDetail::whereIn('id', $structureIds) + ->get() + ->keyBy('id'); + + $structure = null; + $stickerMappingMasterItem = null; + + foreach ($stickerMappingMasterItems as $mapping) { + for ($i = 1; $i <= 8; $i++) { + $column = "sticker_structure{$i}_id"; + $structureId = $mapping->{$column}; + + if (empty($structureId)) { + continue; + } + + $detail = $detailsById->get($structureId); + + if (!$detail) { + continue; + } + + if ((string) $detail->sticker_id === (string) $stickerId) { + $structure = $detail; + $stickerMappingMasterItem = $mapping; + break 2; + } + } + } + + if (!$structure) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No mapping found for sticker ID '{$stickerId}' and plant ID '{$plantId}'.") + ->send(); + + return; + } + + + // $checkItem = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->where('sticker_structure1_id', $structure->id)->first(); + + $itemCode = Item::where('id', $pOrderExist->item_id)->value('code'); + + $plantCode = Plant::where('id', $plantId)->value('code'); + + // if (!$checkItem) { + + // Notification::make() + // ->danger() + // ->title('Item Not Found') + // ->body("Item code '$itemCode' not found in sticker mapping master for selected sticker ID '{$stickerId}'.") + // ->send(); + // return; + // } + + $itemCharacteristic1 = ClassCharacteristic::where('plant_id', $plantId)->where('aufnr', $pOrder)->where('item_id', $pOrderExist->item_id)->first(); + + if (!$itemCharacteristic1) { + + Notification::make() + ->danger() + ->title('Item Not Found') + ->body("Item code '{$itemCode}' was not found against job number '$pOrder' in class characteristic.") + ->send(); + return; + } + + //.. + + $matchedMachineId = null; + + for ($i = 1; $i <= 8; $i++) { + + $structureColumn = "sticker_structure{$i}_id"; + $machineColumn = "sticker{$i}_machine_id"; + + $mappedStructureId = $stickerMappingMasterItem->{$structureColumn} ?? null; + + if ( + $mappedStructureId !== null && + (string) $mappedStructureId === (string) $structure->id + ) { + $matchedMachineId = $stickerMappingMasterItem->{$machineColumn} ?? null; + break; + } + } + + $selectedMachineId = trim((string) $machineId); + $mappedMachineId = trim((string) $matchedMachineId); + + if ($mappedMachineId === '' || $mappedMachineId !== $selectedMachineId) { + + $Mname1 = Machine::where('id', $selectedMachineId)->first(); + $workC1 = $Mname1->work_center ?? null; + $Mname2 = Machine::where('id', $mappedMachineId)->first(); + $workC2 = $Mname2->work_center ?? null; + Notification::make() + ->danger() + ->title('Machine Mapping Mismatch') + ->body("Selected machine '{$workC1}' does not match the mapped machine '{$workC2}'.") + ->send(); + + return; + } + + // if ((string) $matchedMachineId != (string) $machineId) { + // Notification::make() + // ->danger() + // ->title('Machine Not Found') + // ->body("Machine '{$machineId}' is not mapped for Item Code '{$itemCode}' against sticker '$stickerId' in sticker mapping master.") + // ->send(); + + // return; + // } + + $itemCharacteristic = ClassCharacteristic::where('plant_id', $plantId) + ->where('item_id', $pOrderExist->item_id) + ->where('machine_id', $machineId) + ->where('aufnr', $pOrder) + ->first(); + + if(!$itemCharacteristic){ + Notification::make() + ->danger() + ->title('Record Not Found') + ->body("Record not found in class characteristic against job '$pOrder', item '$itemCode' and plant '$plantCode'.") + ->send(); + + return; + } + + $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); + + if ($elements->isEmpty()) { + + Notification::make() + ->danger() + ->title('Sticker Details Not Found') + ->body('No sticker details configured for the selected sticker.') + ->send(); + + return; + } + + $stickerService = new StickerPdfService(); + + $template = $stickerService->generateTemplate( + $structure->sticker_id, + $elements, + $itemCharacteristic, + $itemCode, + $plantId, + $machineId + ); + + $pdfContent = $stickerService->generateBulkFromTemplate( + $template, + $fromSerNo, + $toSerNo, + $itemCode, + ); + + $fileName = 'bulk_sticker_'. $fromSerNo. '_'. $toSerNo. '_'. uniqid(). '.pdf'; + + $tempPdfPath = storage_path('app/' . $fileName); + + file_put_contents( + $tempPdfPath, + $pdfContent + ); + + $pdfUrl = route( + 'sticker.preview', + [ + 'path' => $fileName, + ] + ); + + $this->dispatch('open-sticker-pdf',urls: [$pdfUrl]); + + $totalStickers = ($toSerNo - $fromSerNo) + 1; + + $item = Item::where('id', $pOrderExist->item_id)->where('plant_id', $plantId)->first(); + + $from = (int) $fromSerNo; + $to = (int) $toSerNo; + + for ($serial = $from; $serial <= $to; $serial++) { + + $serialNumber = (string) $serial; + + $exists = StickerOrder::where('plant_id', $plantId) + ->where('production_order', $pOrder) + ->where('sticker_id', $stickerId) + ->where('serial_number', $serialNumber) + ->first(); + + if (!$exists) { + StickerOrder::create([ + 'plant_id' => $plantId, + 'item_id' => $item->id, + 'production_order' => $pOrder, + 'serial_number' => $serialNumber, + 'sticker_id' => $stickerId, + 'print_status' => 'Printed', + ]); + } + + } + + Notification::make() + ->success() + ->title('Sticker Generated') + ->body("{$totalStickers} sticker(s) generated successfully.") + ->seconds(3) + ->send(); + + return; + + } + + + #[On('generate-sticker-id')] + public function generateStickerId2(string $serialNumbers) + { + $state = $this->form->getState(); + + $plantId = trim($state['plant_id'] ?? ''); + + $machineId = trim($state['machine_id'] ?? ''); + + $stickerId = $state['sticker_id'] ?? null; + + $pOrder = $state['production_order'] ?? null; + + $fromSno = trim($state['from_serial_number'] ?? ''); + + $toSno = trim($state['to_serial_number'] ?? ''); + + $serialNumbersArray = array_filter(array_map('trim', explode(',', $serialNumbers))); + + if (count($serialNumbersArray) > 50) { + Notification::make() + ->title('Too Many Serial Numbers') + ->body('You can enter a maximum of 50 serial numbers.') + ->danger() + ->send(); + + return; + } + + $plantName = Plant::where('id', $plantId)->value('name'); + + if (!$stickerId) { + + Notification::make() + ->danger() + ->title('Sticker ID Not Selected') + ->body('Please Choose a Sticker ID.') + ->send(); + + return; + } + + $pOrderExist = ClassCharacteristic::where('plant_id',$plantId)->where('aufnr', $pOrder)->first(); + + if(!$pOrderExist){ + Notification::make() + ->danger() + ->title('Unknown Production Order') + ->body("Production Order '$pOrder' not found.") + ->send(); + return; + } + + if($pOrderExist){ + + $existingSerialNumbers = ClassCharacteristic::where('plant_id', $plantId) + ->where('aufnr', $pOrder) + ->whereIn('gernr', $serialNumbersArray) + ->pluck('gernr') + ->map(fn ($value) => trim((string) $value)) + ->unique() + ->toArray(); + + $invalidSerialNumbers = array_values( + array_diff($serialNumbersArray, $existingSerialNumbers) + ); + + if (!empty($invalidSerialNumbers)) { + Notification::make() + ->danger() + ->title('Unknown Serial Numbers') + ->body( + "The following serial numbers do not belong to Production Order '$pOrder' " + . "for plant '$plantName': " + . implode(', ', $invalidSerialNumbers) + ) + ->send(); + + return; + } + } + + $existingStickers = StickerOrder::where('plant_id', $plantId) + ->where('production_order', $pOrder) + ->where('sticker_id', $stickerId) + ->where('print_status', 'Printed') + ->whereIn('serial_number', $serialNumbersArray) + ->pluck('serial_number') + ->unique() + ->values() + ->toArray(); + + if (!empty($existingStickers)) { + Notification::make() + ->danger() + ->title('Duplicate Sticker') + ->body("The following serial numbers already exist in generated stickers for ". "Sticker '$stickerId', Production Order '$pOrder', and plant '$plantName': ". implode(', ', $existingStickers)) + ->send(); + + return; + } + + $structure = StickerStructureDetail::where('sticker_id',$stickerId)->first(); + + if (!$structure) { + + Notification::make() + ->danger() + ->title('Sticker Structure Not Found') + ->body("No sticker structure found for sticker ID '{$stickerId}'.") + ->send(); + return; + } + + //.New Logic + + $stickerMappingMasterItems = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->get(); + + if ($stickerMappingMasterItems->isEmpty()) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No sticker mapping found for plant ID '{$plantId}'.") + ->send(); + + return; + } + + $structureIds = $stickerMappingMasterItems + ->flatMap(function ($mapping) { + $ids = []; + + for ($i = 1; $i <= 8; $i++) { + $column = "sticker_structure{$i}_id"; + + if (!empty($mapping->{$column})) { + $ids[] = $mapping->{$column}; + } + } + + return $ids; + }) + ->unique() + ->values(); + + $detailsById = StickerStructureDetail::whereIn('id', $structureIds) + ->get() + ->keyBy('id'); + + $structure = null; + $stickerMappingMasterItem = null; + + foreach ($stickerMappingMasterItems as $mapping) { + for ($i = 1; $i <= 8; $i++) { + $column = "sticker_structure{$i}_id"; + $structureId = $mapping->{$column}; + + if (empty($structureId)) { + continue; + } + + $detail = $detailsById->get($structureId); + + if (!$detail) { + continue; + } + + if ((string) $detail->sticker_id === (string) $stickerId) { + $structure = $detail; + $stickerMappingMasterItem = $mapping; + break 2; + } + } + } + + if (!$structure) { + Notification::make() + ->danger() + ->title('Sticker Mapping Not Found') + ->body("No mapping found for sticker ID '{$stickerId}' and plant ID '{$plantId}'.") + ->send(); + + return; + } + + + // $checkItem = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->where('sticker_structure1_id', $structure->id)->first(); + + $itemCode = Item::where('id', $pOrderExist->item_id)->value('code'); + + $plantCode = Plant::where('id', $plantId)->value('code'); + + // if (!$checkItem) { + + // Notification::make() + // ->danger() + // ->title('Item Not Found') + // ->body("Item code '$itemCode' not found in sticker mapping master for selected sticker ID '{$stickerId}'.") + // ->send(); + // return; + // } + + $itemCharacteristic1 = ClassCharacteristic::where('plant_id', $plantId)->where('aufnr', $pOrder)->where('item_id', $pOrderExist->item_id)->first(); + + if (!$itemCharacteristic1) { + + Notification::make() + ->danger() + ->title('Item Not Found') + ->body("Item code '{$itemCode}' was not found against job number '$pOrder' in class characteristic.") + ->send(); + return; + } + + $matchedMachineId = null; + + for ($i = 1; $i <= 8; $i++) { + + $structureColumn = "sticker_structure{$i}_id"; + $machineColumn = "sticker{$i}_machine_id"; + + $mappedStructureId = $stickerMappingMasterItem->{$structureColumn} ?? null; + + if ( + $mappedStructureId !== null && + (string) $mappedStructureId === (string) $structure->id + ) { + $matchedMachineId = $stickerMappingMasterItem->{$machineColumn} ?? null; + break; + } + } + + $selectedMachineId = trim((string) $machineId); + $mappedMachineId = trim((string) $matchedMachineId); + + if ($mappedMachineId === '' || $mappedMachineId !== $selectedMachineId) { + Notification::make() + ->danger() + ->title('Machine Mapping Mismatch') + ->body("Selected machine '{$selectedMachineId}' does not match the mapped machine '{$mappedMachineId}'.") + ->send(); + + return; + } + + $itemCharacteristic = ClassCharacteristic::where('plant_id', $plantId) + ->where('item_id', $pOrderExist->item_id) + ->where('machine_id', $machineId) + ->where('aufnr', $pOrder) + ->first(); + + if(!$itemCharacteristic){ + Notification::make() + ->danger() + ->title('Record Not Found') + ->body("Record not found in class characteristic against job '$pOrder', item '$itemCode' and plant '$plantCode'.") + ->send(); + + return; + } + + $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); + + if ($elements->isEmpty()) { + + Notification::make() + ->danger() + ->title('Sticker Details Not Found') + ->body('No sticker details configured for the selected sticker.') + ->send(); + + return; + } + + $stickerService = new StickerPdfService(); + + $template = $stickerService->generateTemplate( + $structure->sticker_id, + $elements, + $itemCharacteristic, + $itemCode, + $plantId, + $machineId + ); + + $pdfContent = $stickerService->generateMultipleBulkFromTemplate( + $template, + $serialNumbersArray, + $itemCode, + ); + + $fileName = 'bulk_sticker_'. '_'. uniqid(). '.pdf'; + + $tempPdfPath = storage_path('app/' . $fileName); + + file_put_contents( + $tempPdfPath, + $pdfContent + ); + + $pdfUrl = route( + 'sticker.preview', + [ + 'path' => $fileName, + ] + ); + + $this->dispatch('open-sticker-pdf',urls: [$pdfUrl]); + + $totalStickers = count($serialNumbersArray); + + $item = Item::where('id', $pOrderExist->item_id)->where('plant_id', $plantId)->first(); + + foreach ($serialNumbersArray as $serialNumber) { + + $serialNumber = trim($serialNumber); + + $exists = StickerOrder::where('plant_id', $plantId) + ->where('production_order', $pOrder) + ->where('sticker_id', $stickerId) + ->where('serial_number', $serialNumber) + ->first(); + + if (!$exists) { + StickerOrder::create([ + 'plant_id' => $plantId, + 'item_id' => $item->id, + 'production_order' => $pOrder, + 'serial_number' => $serialNumber, + 'sticker_id' => $stickerId, + 'print_status' => 'Printed', + ]); + } + } + + Notification::make() + ->success() + ->title('Sticker Generated') + ->body("{$totalStickers} sticker(s) generated successfully.") + ->seconds(3) + ->send(); + + return; + + } + + + #[On('production-order-updated')] + public function updateProductionOrder(string $productionOrder): void + { + $this->filters['production_order'] = $productionOrder; + } + + #[On('sticker-options')] + public function stickerStructure(array $stickerOptions): void + { + $this->filters['sticker_options'] = $stickerOptions; + } + + #[On('production-order-clear')] + public function updateProductionOrderClear(): void + { + $this->filters['production_order'] = null; + } + + #[On('sticker-options-clear')] + public function stickerStructureClear(): void + { + $this->filters['sticker_options'] = null; + } + + public static function canAccess(): bool + { + return Auth::check() && Auth::user()->can('view test certificate page'); + } +} diff --git a/resources/views/filament/pages/test-certificate.blade.php b/resources/views/filament/pages/test-certificate.blade.php new file mode 100644 index 00000000..6f242721 --- /dev/null +++ b/resources/views/filament/pages/test-certificate.blade.php @@ -0,0 +1,103 @@ + + + +{{ $this->form }} + +
+ {{-- --}} + +
+ {{-- --}} + {{--
+ +
--}} + +
+ +
+ +{{-- --}} + + + +
-- 2.49.1 From ab3a58f13c7e32b02086b543758e2f858d164f3a Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 12 Nov 2026 15:32:21 +0530 Subject: [PATCH 2/2] defined item varible in production plan importer --- app/Filament/Imports/ProductionPlanImporter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Filament/Imports/ProductionPlanImporter.php b/app/Filament/Imports/ProductionPlanImporter.php index e56fe6b4..c2f622c1 100644 --- a/app/Filament/Imports/ProductionPlanImporter.php +++ b/app/Filament/Imports/ProductionPlanImporter.php @@ -124,6 +124,7 @@ class ProductionPlanImporter extends Importer $plantCod = $this->data['plant']; $itemCod = $this->data['item']; $plant = null; + $item = null; $line = null; $block = null; -- 2.49.1