From 95ff0fd1a2a469be5ea53adc30c747c5a4c30dfa Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 30 May 2026 20:58:44 +0530 Subject: [PATCH] Added GET API method with logic to receive LeakTestStatus --- .../Controllers/TestingPanelController.php | 292 +++++++++--------- 1 file changed, 151 insertions(+), 141 deletions(-) diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index 48e4a7e..be11711 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -48,12 +48,12 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); + ], 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!', - ], 400); + ], 404); } $plant = Plant::where('code', $data['plant_code'])->first(); @@ -62,7 +62,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Plant not found!', - ], 400); + ], 404); } $plantId = $plant->id; @@ -71,12 +71,12 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Group work center can't be empty!", - ], 400); + ], 404); } elseif (Str::length($data['line_name']) < 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid group work center found!', - ], 400); + ], 404); } $gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->first(); @@ -84,7 +84,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Group work center not found!', - ], 400); + ], 404); } $gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->where('plant_id', $plantId)->first(); @@ -93,7 +93,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Group work center not found for the specified plant : '{$data['plant_code']}'!", - ], 400); + ], 404); } $gWorkCenterId = $gWorkCenter->id; @@ -102,12 +102,12 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center can't be empty!", - ], 400); + ], 404); } elseif (Str::length($data['machine_name']) < 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid work center found!', - ], 400); + ], 404); } $machine = Machine::where('work_center', $data['machine_name'])->first(); @@ -115,7 +115,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Work center not found!', - ], 400); + ], 404); } $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first(); @@ -123,7 +123,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!", - ], 400); + ], 404); } $machine = Machine::where('work_center', $data['machine_name'])->where('work_group_master_id', $gWorkCenterId)->first(); @@ -132,7 +132,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center not found for the specified Group work center : '{$data['line_name']}'!", - ], 400); + ], 404); } @@ -141,7 +141,7 @@ class TestingPanelController extends Controller 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']}'!", - ], 400); + ], 404); } $lineId = $machine->line_id; @@ -162,7 +162,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item code can't be empty!", - ], 400); + ], 404); } // Collect duplicates @@ -196,7 +196,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Duplicate item codes found in request: '.implode(', ', $duplicateItemCodes), - ], 400); + ], 404); } $uniqueInvalidCodes = array_unique($missedItemCodes); @@ -209,7 +209,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plant->name}'!", - ], 400); + ], 404); } $insertedSnoCount = []; @@ -326,7 +326,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Missed serial numbers are: '.implode(', ', $missingSno), - ], 400); + ], 404); } } } catch (\Exception $e) { @@ -367,22 +367,22 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); + ], 404); } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", - ], 400); + ], 404); } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", - ], 400); + ], 404); } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Invalid plant code '{$plantCode}' found!", - ], 400); + ], 404); } if ($itemCode == null || $itemCode == '' || ! $itemCode) { @@ -431,7 +431,6 @@ class TestingPanelController extends Controller if ($testStatus == null || $testStatus == '') { $testStatus = 'F'; - } elseif (Str::contains($testStatus, 'P', ignoreCase: true)) { $testStatus = 'P'; } elseif (Str::contains($testStatus, 'F', ignoreCase: true)) { @@ -449,7 +448,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code '{$plantCode}' not found!", - ], 400); + ], 404); } $plantId = $plant->id; @@ -502,133 +501,144 @@ class TestingPanelController extends Controller ], 403); } - // $plantCode = $request->header('plant-code'); - // $itemCode = $request->header('item-code'); - // // $description = $item ? $item->description : ''; - - // if ($plantCode == null || $plantCode == '') { - // // return response("ERROR: Plant Name can't be empty", 400) - // // ->header('Content-Type', 'text/plain'); - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code can't be empty!", - // ], 400); - // } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Invalid plant code found!', - // ], 400); - // } elseif ($itemCode == null || $itemCode == '' || ! $itemCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code can't be empty!", - // ], 404); - // } elseif (Str::length($itemCode) < 6) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' should contain minimum 6 digits!", - // ], 404); - // } elseif (! ctype_alnum($itemCode)) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' should contain only alpha-numeric values!", - // ], 404); - // } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $itemCode)) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' should not begin with '0'!", - // ], 404); - // } - - // $plant = Plant::where('code', $plantCode)->first(); - - // if (! $plant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Plant not found!', - // ], 400); - // } - - // $plantId = $plant->id; - - // $item = Item::where('code', $itemCode)->first(); - - // if (! $item) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Item code not found in item table!', - // ], 404); - // } - - // $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); - - // if (! $item) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code not found in item table for the plant : '$plant->name'!", - // ], 404); - // } - - // $itemId = $item->id; - + $plantCode = $request->header('plant-code') ?? null; + $serialNumbers = $request->header('serial-numbers') ?? null; // $description = $item ? $item->description : ''; - // $category = $item ? $item->category : ''; + if ($plantCode == null || $plantCode == '' || ! $plantCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! is_numeric($plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); + } - // $motorTestingMaster = MotorTestingMaster::where('item_id', $itemId)->first(); + if ($serialNumbers == null || $serialNumbers == '' || ! $serialNumbers) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Serial numbers can't be empty!", + ], 404); + } - // if (! $motorTestingMaster) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Item code not found in motor testing master table!', - // ], 404); + $plant = Plant::where('code', $plantCode)->first(); + if (! $plant) { + // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found!", + ], 400); + } + + $plantId = $plant->id; + + $serialArray = array_map('trim', explode(',', $serialNumbers)); + // $serialNumber = 'SNO-'.Str::random(5).'-'.time(); // Default random serial number if not provided + + // Remove null, empty string, or falsy values from $serialArray + $serialArray = array_filter($serialArray, function ($serial) { + return $serial != null && $serial != '' && $serial; + }); + + $serialStatus = []; + $validSerials = []; + $hasFail = false; + foreach ($serialArray as $serialNumber) { + if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) { + $serialStatus[$serialNumber] = "Serial number can't be empty!"; + $hasFail = true; + // $serialStatus[$serialNumber] = 'I'; + } elseif (Str::length($serialNumber) < 9) { + $serialStatus[$serialNumber] = 'Should contain minimum 9 digits!'; + $hasFail = true; + // $serialStatus[$serialNumber] = 'I'; + } elseif (! ctype_alnum($serialNumber)) { + $serialStatus[$serialNumber] = 'Should contain only alpha-numeric values!'; + $hasFail = true; + // $serialStatus[$serialNumber] = 'I'; + } elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) { + $serialStatus[$serialNumber] = "Should not begin with '0' or letter!"; + $hasFail = true; + // $serialStatus[$serialNumber] = 'I'; + } else { + $validSerials[$serialNumber] = 'Not Found'; + } + } + + // $key = array_search('26021040180982', $serialArray); + // if ($key !== false) { + // unset($serialArray[$key]); // } - // $motorTestingMaster = MotorTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first(); + if ($validSerials == null || empty($validSerials)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_descriptions' => $serialStatus, + ], 404); + } else { + $serialArray = $validSerials; // nonExistingSerials + } - // if (! $motorTestingMaster) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code not found in motor testing master table for the plant : '$plant->name'!", - // ], 404); - // } + // Check which serial numbers exist in leak_test_readings table + $existingSerials = []; - // $output = [ - // 'mot_subassembly_code' => $motorTestingMaster->subassembly_code ?? '', - // 'mot_model_name' => ($itemCode == '123456') ? 'SAMPLE TYPE' : $description, - // 'mot_non_isi_model' => $motorTestingMaster->isi_model ? '0' : '1', - // 'mot_phase' => $motorTestingMaster->phase ?? '', - // 'mot_hp' => $motorTestingMaster->hp ?? '', - // 'mot_kw' => $motorTestingMaster->kw ?? '', - // 'mot_volt' => $motorTestingMaster->volt ?? '', - // 'mot_cur' => $motorTestingMaster->current ?? '', - // 'mot_rpm' => $motorTestingMaster->rpm ?? '', - // 'mot_rate_torque_kg' => $motorTestingMaster->torque ?? '', - // 'mot_freq' => $motorTestingMaster->frequency ?? '', - // 'mot_conn' => $motorTestingMaster->connection ?? '', - // 'mot_ins_res_limit' => $motorTestingMaster->ins_res_limit ?? '', - // 'mot_ins_res_type' => $motorTestingMaster->ins_res_type ?? '', - // 'mot_category' => $category, - // 'mot_routine_test_time' => $motorTestingMaster->routine_test_time ?? '', - // 'mot_res_ry_ll' => $motorTestingMaster->res_ry_ll ?? '', - // 'mot_res_ry_ul' => $motorTestingMaster->res_ry_ul ?? '', - // 'mot_res_yb_ll' => $motorTestingMaster->res_yb_ll ?? '', - // 'mot_res_yb_ul' => $motorTestingMaster->res_yb_ul ?? '', - // 'mot_res_br_ll' => $motorTestingMaster->res_br_ll ?? '', - // 'mot_res_br_ul' => $motorTestingMaster->res_br_ul ?? '', - // 'mot_lock_volt_limit' => $motorTestingMaster->lock_volt_limit ?? '', - // 'mot_leak_cur_limit' => $motorTestingMaster->leak_cur_limit ?? '', - // 'mot_lock_cur_ll' => $motorTestingMaster->lock_cur_ll ?? '', - // 'mot_lock_cur_ul' => $motorTestingMaster->lock_cur_ul ?? '', - // 'mot_noload_cur_ll' => $motorTestingMaster->noload_cur_ll ?? '', - // 'mot_noload_cur_ul' => $motorTestingMaster->noload_cur_ul ?? '', - // 'mot_noload_pow_ll' => $motorTestingMaster->noload_pow_ll ?? '', - // 'mot_noload_pow_ul' => $motorTestingMaster->noload_pow_ul ?? '', - // 'mot_noload_spd_ll' => $motorTestingMaster->noload_spd_ll ?? '', - // 'mot_noload_spd_ul' => $motorTestingMaster->noload_spd_ul ?? '', - // ]; + $existingSerials = LeakTestReading::where('plant_id', $plantId)->whereIn('serial_number', array_keys($serialArray)) + ->pluck('test_status', 'serial_number') + ->toArray(); - // return response()->json($output, 200); + if ($existingSerials == null || empty($existingSerials)) { + $mergedResult = $serialStatus + $serialArray; // array_merge($serialStatus, $serialArray); + + return response()->json([ + 'status_code' => 'ERROR', + 'status_descriptions' => $mergedResult, + ], 404); + } + + $hasFail = (! $hasFail) ? in_array('F', $existingSerials) : $hasFail; + // Map 'F' to 'Fail' and 'P' to 'Pass' + $existingSerials = array_map(function ($status) { + return $status === 'F' ? 'Fail' : ($status === 'P' ? 'Pass' : $status); + }, $existingSerials); + + // Sort: Fail comes first, then Pass + uasort($existingSerials, function ($a, $b) { + // 'Fail' < 'Pass' so Fail comes first + return $a <=> $b; + }); + + // Get non-existing serial numbers (serials not in the database) + // $serialArray = array_diff($serialArray, array_keys($existingSerials)); + $nonExistingSerials = array_diff_key($serialArray, $existingSerials); + + if ($nonExistingSerials == null || empty($nonExistingSerials)) { + $mergedResult = $serialStatus + $existingSerials; + + return response()->json([ + 'status_code' => $hasFail ? 'ERROR' : 'SUCCESS', + 'status_descriptions' => $mergedResult, + ], $hasFail ? 404 : 200); + } else { + $mergedResult = $serialStatus + $nonExistingSerials + $existingSerials; // nonExistingSerials + + return response()->json([ + 'status_code' => 'ERROR', + 'status_descriptions' => $mergedResult, + ], 404); + } } /**