From 75fd3d98413a0739f68e03427c642d249427c85d Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 27 Jan 2026 15:52:05 +0530 Subject: [PATCH] Enhanced validation and error handling in storeLaserRequestChar method --- .../Controllers/CharacteristicsController.php | 129 +++++++++++++----- 1 file changed, 98 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/CharacteristicsController.php b/app/Http/Controllers/CharacteristicsController.php index 81fadc3..0e69378 100644 --- a/app/Http/Controllers/CharacteristicsController.php +++ b/app/Http/Controllers/CharacteristicsController.php @@ -3011,6 +3011,8 @@ class CharacteristicsController extends Controller $userName = 'Admin'; } + $charField = $request->header('characteristic-field'); + $json = $request->input('data'); $data = json_decode($json, true); @@ -3034,6 +3036,10 @@ class CharacteristicsController extends Controller ], 404); } + if ($charField == null || $charField == '') { + $charField = 'nil'; + } + $plant = Plant::where('code', $plantCode)->first(); if (! $plant) { return response()->json([ @@ -3072,24 +3078,6 @@ class CharacteristicsController extends Controller ], 404); } - $machine = Machine::where('work_center', $workCenter)->first(); - if (! $machine) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found!", - ], 404); - } - - $machineAgaPlant = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); - if (! $machineAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}'!", - ], 404); - } - - $MachineId = $machineAgaPlant->id; - if ($machineName == null || $machineName == '' || ! $machineName) { return response()->json([ 'status_code' => 'ERROR', @@ -3097,11 +3085,82 @@ class CharacteristicsController extends Controller ], 404); } + if ($itemCode == null || $itemCode == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item Code can't be empty!", + ], 400); + } elseif (Str::length($itemCode) < 6 || ! ctype_alnum($itemCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid item code found!', + ], 404); + } + + if (! $jobNo) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number can't be empty", + ], 404); + } elseif (Str::length($jobNo) < 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' should contain minimum 7 digits!", + ], 404); + } elseif (! is_numeric($jobNo)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' should contain only numeric values!", + ], 404); + } + + $machine = Machine::where('work_center', $workCenter)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in master!", + ], 404); + } + + $machineAgaPlant = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); + if (! $machineAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in master!", + ], 404); + } + + $MachineId = $machineAgaPlant->id; + + $pCode = CharacteristicApproverMaster::where('plant_id', $plantId)->first(); + if (! $pCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in characteristic approver master!", + ], 404); + } + + $wCenter = CharacteristicApproverMaster::where('machine_id', $MachineId)->first(); + if (! $wCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in characteristic approver master!", + ], 404); + } + + $wCenterAgaPlant = CharacteristicApproverMaster::where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $wCenterAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in characteristic approver master!", + ], 404); + } + $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); if (! $mName) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' not found in master!", + 'status_description' => "Machine name '{$machineName}' not found in characteristic approver master!", ], 404); } @@ -3109,19 +3168,28 @@ class CharacteristicsController extends Controller if (! $mNameAgaPlant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' not found in master against plant code '{$plantCode}'!", + 'status_description' => "Machine name '{$machineName}' not found for the plant code '{$plantCode}' in characteristic approver master!", ], 404); } - $charId = $mNameAgaPlant->id; - - if ($itemCode == null || $itemCode == '' || ! $itemCode) { + $mNameAgaWorkCenter = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->first(); + if (! $mNameAgaWorkCenter) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item code can't be empty!", + 'status_description' => "Machine name '{$machineName}' not found for the work center '{$workCenter}' in characteristic approver master!", ], 404); } + $mNameAgaWorkCenterForPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $mNameAgaWorkCenterForPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in characteristic approver master!", + ], 404); + } + + $charId = $mNameAgaWorkCenterForPlant->id; + $iCode = Item::where('code', $itemCode)->first(); if (! $iCode) { return response()->json([ @@ -3144,10 +3212,7 @@ class CharacteristicsController extends Controller foreach ($characteristics as $char) { $charName = strtolower($char['characteristic_name']) ?? null; - $pendingExists = RequestCharacteristic::where('aufnr', $jobNo) - ->where('characteristic_name', $charName) - ->latest() - ->first(); + $pendingExists = RequestCharacteristic::where('plant_id', $plantId)->where('aufnr', $jobNo)->where('characteristic_name', $charName)->latest()->first(); if ($pendingExists) { @@ -3176,7 +3241,8 @@ class CharacteristicsController extends Controller $year = now()->format('y'); $month = now()->format('m'); - $prefix = "WF-{$year}{$month}-"; + $date = now()->format('d'); + $prefix = "WF-{$year}{$month}{$date}-"; // $existingWorkflowId = RequestCharacteristic::where('plant_id', $plantId) // ->where('machine_id', $MachineId) @@ -3188,8 +3254,9 @@ class CharacteristicsController extends Controller // } - $lastWorkflow = RequestCharacteristic::where('plant_id', $plantId) - ->where('machine_id', $MachineId) + $lastWorkflow = RequestCharacteristic::where('work_flow_id', 'like', "{$prefix}%") + // ->where('plant_id', $plantId) + // ->where('machine_id', $MachineId) ->where('work_flow_id', 'like', "{$prefix}%") ->orderByDesc('work_flow_id') ->first();