From e014ae7035023aa09735a9aec27ba4dc8216b101 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 16 Oct 2026 15:58:59 +0530 Subject: [PATCH] Changed logic in sticker order reprint --- app/Filament/Pages/StickerOrderReprint.php | 129 ++++++++++++++++++--- 1 file changed, 112 insertions(+), 17 deletions(-) diff --git a/app/Filament/Pages/StickerOrderReprint.php b/app/Filament/Pages/StickerOrderReprint.php index e9ff55d5..a7617328 100644 --- a/app/Filament/Pages/StickerOrderReprint.php +++ b/app/Filament/Pages/StickerOrderReprint.php @@ -904,34 +904,90 @@ class StickerOrderReprint extends Page implements HasForms return; } - $stickerMappingMasterItem = StickerMappingMaster::where('plant_id', $plantId)->first(); - - if (!$stickerMappingMasterItem) { + $stickerMappingMasterItems = StickerMappingMaster::where('plant_id', $plantId)->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 = []; - $checkItem = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->where('sticker_structure1_id', $structure->id)->first(); + 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) { + // 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; - } + // 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(); @@ -963,21 +1019,60 @@ class StickerOrderReprint extends Page implements HasForms $matchedMachineId = null; - $matchedMachineId = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->where('sticker_structure1_id', $structure->id)->where('sticker1_machine_id', $machineId)->first(); + for ($i = 1; $i <= 8; $i++) { - if (!$matchedMachineId) { + $structureColumn = "sticker_structure{$i}_id"; + if ( + (string) $stickerMappingMasterItem->{$structureColumn} + == (string) $structure->id + ) { + $machineColumn = "sticker{$i}_machine_id"; + + $matchedMachineId = $stickerMappingMasterItem->{$machineColumn}; + + break; + } + } + + if ($matchedMachineId == null || (string) $matchedMachineId != (string) $machineId) + { Notification::make() ->danger() - ->title('Machine Not Found') - ->body("Machine '{$machineId}' is not mapped for Item Code '{$itemCode}' in sticker mapping master.") + ->title('Machine Mapping Mismatch') + ->body("Selected machine '{$machineId}' does not match the machine mapped to the selected sticker.") ->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; + } + + // $matchedMachineId = null; + + // $matchedMachineId = StickerMappingMaster::where('plant_id', $plantId)->where('item_id', $pOrderExist->item_id)->where('sticker_structure1_id', $structure->id)->where('sticker1_machine_id', $machineId)->first(); + + // if (!$matchedMachineId) { + + // Notification::make() + // ->danger() + // ->title('Machine Not Found') + // ->body("Machine '{$machineId}' is not mapped for Item Code '{$itemCode}' in sticker mapping master.") + // ->send(); + + // return; + // } + $itemCharacteristic = ClassCharacteristic::where('plant_id', $plantId) - ->where('item_id', $checkItem->item_id) + ->where('item_id', $pOrderExist->item_id) ->where('machine_id', $machineId) ->where('aufnr', $pOrder) ->first();