From 9a6e364f5236f1b6c64534f367bbc07b465ef005 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 15 Sep 2025 17:40:52 +0530 Subject: [PATCH] Refactored store method in TestingPanelController to improve validation for line and machine names, replacing Line model with WorkGroupMaster for accurate data handling. --- .../Controllers/TestingPanelController.php | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index 5f87493..382d347 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -8,6 +8,7 @@ use App\Models\Machine; use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\TestingPanelReading; +use App\Models\WorkGroupMaster; use DB; use Filament\Notifications\Notification; use Illuminate\Http\Request; @@ -47,7 +48,6 @@ class TestingPanelController extends Controller ], 403); } - $data = $request->all(); if ($data['plant_code'] == null || $data['plant_code'] == '') @@ -78,19 +78,32 @@ class TestingPanelController extends Controller $plantId = $plant->id; - $line = Line::where('group_work_center', $data['line_name'])->first(); - if (!$line) + if ($data['line_name'] == null || $data['line_name'] == '') + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Group work center can't be empty!" + ], 400); + } + else if (Str::length($data['line_name']) < 0) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid group work center found!" + ], 400); + } + + $gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->first(); + if (!$gWorkCenter) { - //return response("Line not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Group work center not found!' ], 400); } - $line = Line::where('group_work_center', $data['line_name'])->where('plant_id', $plantId)->first(); - - if (!$line) + $gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->where('plant_id', $plantId)->first(); + if (!$gWorkCenter) { //return response( "Line not found for the specified plant : {$data['plant_code']}",400)->header('Content-Type', 'text/plain'); return response()->json([ @@ -99,13 +112,26 @@ class TestingPanelController extends Controller ], 400); } - $lineId = $line->id; + $gWorkCenterId = $gWorkCenter->id; + + if ($data['machine_name'] == null || $data['machine_name'] == '') + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center can't be empty!" + ], 400); + } + else if (Str::length($data['machine_name']) < 0) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid work center found!" + ], 400); + } $machine = Machine::where('work_center', $data['machine_name'])->first(); - if (!$machine) { - // return response('Machine not found', 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Work center not found!' @@ -113,18 +139,15 @@ class TestingPanelController extends Controller } $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first(); - if (!$machine) { - // return response("Machine not found for the specified plant : {$data['plant_code']}", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!" ], 400); } - $machine = Machine::where('work_center', $data['machine_name'])->where('line_id', $lineId)->first(); - + $machine = Machine::where('work_center', $data['machine_name'])->where('work_group_master_id', $gWorkCenterId)->first(); if (!$machine) { // return response("Machine not found for the specified line : {$data['line_name']}", 400)->header('Content-Type', 'text/plain'); @@ -135,17 +158,15 @@ class TestingPanelController extends Controller } - $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('line_id', $lineId)->first(); - + $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('work_group_master_id', $gWorkCenterId)->first(); if (!$machine) { - // return response('Machine not found for the specified plant and line', 400) - // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Work center not found for the specified plant and group work center!' + 'status_description' => "Work center not found for the specified Plant : '{$data['plant_code']}' and Group work center : '{$data['line_name']}'!" ], 400); } + $lineId = $machine->line_id; $machineId = $machine->id; try @@ -262,8 +283,7 @@ class TestingPanelController extends Controller $updateCountString = (string)$newUpdateCount; - $row = - [ + $row = [ 'plant_id' => $plantId, 'line_id' => $lineId, 'machine_id' => $machineId,