From 54d2f7d055e42a44f1f018d63a700799551fc399 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 21 Aug 2026 09:32:09 +0530 Subject: [PATCH] Added store before test readings post api function with validations --- .../Controllers/TestingPanelController.php | 262 +++++++++++++++++- 1 file changed, 258 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index 68ed095..61b45a7 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\BeforeTestReading; use App\Models\Item; use App\Models\LeakTestReading; use App\Models\Machine; @@ -66,6 +67,7 @@ class TestingPanelController extends Controller } $plantId = $plant->id; + $plantName = $plant->name; if ($data['line_name'] == null || $data['line_name'] == '') { return response()->json([ @@ -203,12 +205,12 @@ class TestingPanelController extends Controller if (! empty($uniqueInvalidCodes)) { - // return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plant->name}", 400) + // return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plantName}", 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plant->name}'!", + 'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plantName}'!", ], 404); } @@ -660,6 +662,257 @@ class TestingPanelController extends Controller } } + /** + * Store a newly created before test status in storage. + */ + public function storeBeforeTestStatus(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser.':'.$expectedPw; + + if ('Bearer '.$expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!', + ], 403); + } + + $data = $request->all(); + + if ($data['plant_code'] == null || $data['plant_code'] == '') { + // return response("ERROR: Please provide a valid plant code.", 400) + // ->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (Str::length($data['plant_code']) < 4 || ! is_numeric($data['plant_code']) || ! preg_match('/^[1-9]\d{3,}$/', $data['plant_code'])) {// !ctype_digit($data['plant_code']) + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid plant code found!', + ], 404); + } + + $plant = Plant::where('code', $data['plant_code'])->first(); + if (! $plant) { + // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Plant not found!', + ], 404); + } + + $plantId = $plant->id; + $plantName = $plant->name; + + if ($data['line_name'] == null || $data['line_name'] == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Group work center can't be empty!", + ], 404); + } elseif (Str::length($data['line_name']) < 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid group work center found!', + ], 404); + } + + $gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->first(); + if (! $gWorkCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Group work center not found!', + ], 404); + } + + $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([ + 'status_code' => 'ERROR', + 'status_description' => "Group work center not found for the specified plant : '{$data['plant_code']}'!", + ], 404); + } + + $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!", + ], 404); + } elseif (Str::length($data['machine_name']) < 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid work center found!', + ], 404); + } + + $machine = Machine::where('work_center', $data['machine_name'])->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Work center not found!', + ], 404); + } + + $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!", + ], 404); + } + + $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'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center not found for the specified Group work center : '{$data['line_name']}'!", + ], 404); + + } + + $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('work_group_master_id', $gWorkCenterId)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center not found for the specified Plant : '{$data['plant_code']}' and Group work center : '{$data['line_name']}'!", + ], 404); + } + + $lineId = $machine->line_id; + $machineId = $machine->id; + + try { + $insertedSerials = []; + $missedItemCodes = []; + $duplicateItemCodes = []; + $existSnoCount = []; + + if (! empty($data['item_codes']) && is_array($data['item_codes'])) { + foreach ($data['item_codes'] as $item) { + $code = $item['item_code'] ?? null; + + // Check if item_code is present + if ($code == '' || $code == null) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code can't be empty!", + ], 404); + } + + // Collect duplicates + if (isset($itemCodeCounts[$code])) { + $itemCodeCounts[$code]++; + // Only add to duplicates array once + if ($itemCodeCounts[$code] == 2) { + $duplicateItemCodes[] = $code; + } + } else { + $itemCodeCounts[$code] = 1; + } + + $motorTestingMaster = MotorTestingMaster::whereHas('item', function ($query) use ($item) { + $query->where('code', $item['item_code']); + })->where('plant_id', $plantId)->first(); + + if (! $motorTestingMaster) { + $missedItemCodes[] = $item['item_code']; + } + + if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) { + foreach ($item['serial_numbers'] as $serial) { + $existSnoCount[] = $serial['serial_number']; + } + } + } + + // If any duplicates found, return error + if (! empty($duplicateItemCodes)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Duplicate item codes found in request: '.implode(', ', $duplicateItemCodes), + ], 404); + } + + $uniqueInvalidCodes = array_unique($missedItemCodes); + + if (! empty($uniqueInvalidCodes)) { + + // return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plantName}", 400) + // ->header('Content-Type', 'text/plain'); + + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plantName}'!", + ], 404); + } + + $insertedSnoCount = []; + + foreach ($data['item_codes'] as $item) { + + $motorTestingMaster = MotorTestingMaster::whereHas('item', callback: function ($query) use ($item) { + $query->where('code', $item['item_code']); + })->where('plant_id', $plantId)->first(); + + $motorTestingMasterId = $motorTestingMaster->id; + + if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) { + foreach ($item['serial_numbers'] as $serial) { + + $row = [ + 'plant_id' => $plantId, + 'line_id' => $lineId, + 'machine_id' => $machineId, + 'motor_testing_master_id' => $motorTestingMasterId, + 'serial_number' => $serial['serial_number'] ?? null, + 'before_fr_res_ry' => $serial['before_fr_res_ry'] ?? null, + 'before_fr_res_yb' => $serial['before_fr_res_yb'] ?? null, + 'before_fr_res_br' => $serial['before_fr_res_br'] ?? null, + 'before_fr_ir' => $serial['before_fr_ir'] ?? null, + 'tested_by' => (($serial['tested_by'] == 'jothi') ? 'Admin' : $serial['tested_by']) ?? null, + 'scanned_at' => $serial['scanned_at'] ?? now(), + ]; + + // Insert the new record + BeforeTestReading::create($row); + $insertedSerials[] = $serial['serial_number'] ?? '[unknown]'; + $insertedSnoCount[] = $serial['serial_number']; + } + } + } + } + + if (! empty($insertedSerials)) { + if (count($existSnoCount) == count($insertedSnoCount)) { + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Inserted before test serial numbers are: '.implode(', ', $insertedSerials), + ], 200); + } else { + $missingSno = array_diff($existSnoCount, $insertedSnoCount); + + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Missed before test serial numbers are: '.implode(', ', $missingSno), + ], 404); + } + } + } catch (\Exception $e) { + // return response($e->getMessage(), 500)->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Store testing panel before test readings internal server error : '.$e?->getCode(), + ], 500); + } + } + /** * Display the specified resource. */ @@ -726,6 +979,7 @@ class TestingPanelController extends Controller } $plantId = $plant->id; + $plantName = $plant->name; $item = Item::where('code', $itemCode)->first(); @@ -741,7 +995,7 @@ class TestingPanelController extends Controller if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item code not found in item table for the plant : '$plant->name'!", + 'status_description' => "Item code not found in item table for the plant : '$plantName'!", ], 404); } @@ -765,7 +1019,7 @@ class TestingPanelController extends Controller if (! $motorTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item code not found in motor testing master table for the plant : '$plant->name'!", + 'status_description' => "Item code not found in motor testing master table for the plant : '$plantName'!", ], 404); }