From 27b4377e9e1c28001a2e1813a28c15a9b4435e67 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 27 Nov 2025 08:33:31 +0530 Subject: [PATCH] Updated error response codes in updateGR and related methods to return 404 for various validation errors and Added sfg_number duplicate warning --- app/Http/Controllers/PdfController.php | 81 +++++++++++++++----------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index 200e8cb..99787d1 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -35,7 +35,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } $data = $request->all(); @@ -46,12 +46,12 @@ class PdfController 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(); @@ -59,7 +59,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Plant not found!', - ], 400); + ], 404); } $plantId = $plant->id; @@ -67,7 +67,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "GR Number can't be empty!", - ], 400); + ], 404); } $grExists = GRMaster::where('plant_id', $plantId) @@ -87,7 +87,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Scanned by can't be empty!", - ], 400); + ], 404); } $user = User::where('name', $data['scanned_by']) @@ -109,7 +109,7 @@ class PdfController extends Controller 'status_code' => 'ERROR', 'status_description' => 'Duplicate item codes found in request!', 'duplicate_item_codes' => array_values($duplicateItemCodes), - ], 400); + ], 404); } $allSerials = []; @@ -129,7 +129,7 @@ class PdfController extends Controller 'status_code' => 'ERROR', 'status_description' => 'Duplicate serial numbers found in request!', 'duplicate_serial_numbers' => array_values($duplicateSerials), - ], 400); + ], 404); } $invalidLengthItemCodes = []; @@ -152,7 +152,7 @@ class PdfController extends Controller 'status_code' => 'ERROR', 'status_description' => 'Some item codes are invalid: must be at least 6 digits!', 'invalid_item_codes' => array_values($invalidLengthItemCodes), - ], 400); + ], 404); } $invalidItemCodes = []; @@ -187,7 +187,7 @@ class PdfController extends Controller 'status_description' => 'Some item codes are invalid!', 'not_found_items' => array_values($invalidItemCodes), 'not_in_plant' => array_values($invalidPlantItems), - ], 400); + ], 404); } $missingSerialsByItem = []; @@ -226,7 +226,7 @@ class PdfController extends Controller 'status_code' => 'ERROR', 'status_description' => 'Some serial numbers not found in DB for given plant, GR number, and item!', 'missing_serials' => $missingSerialsByItem, - ], 400); + ], 404); } $alreadyCompleted = []; @@ -259,7 +259,7 @@ class PdfController extends Controller 'status_code' => 'ERROR', 'status_description' => 'Below serial numbers are already completed', 'serial_numbers' => $alreadyCompleted, - ], 400); + ], 404); } // { // $itemCode = $item['item_code']; @@ -270,7 +270,7 @@ class PdfController extends Controller // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Item code: $itemCode not found" - // ], 400); + // ], 404); // } // $itemPlant = Item::where('plant_id', $plantId) @@ -279,7 +279,7 @@ class PdfController extends Controller // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Item code: $itemCode not found for the plant: $data[plant_code]" - // ], 400); + // ], 404); // } // $itemId = $itemObj->id; @@ -330,13 +330,13 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } $filename = $request->header('process-order'); if (! $filename) { - return response()->json(['error' => 'Missing file-name header'], 400); + return response()->json(['error' => 'Missing file-name header'], 404); } $filename = basename($filename); @@ -377,7 +377,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } $filename = $request->header('gr-number'); @@ -386,8 +386,8 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Gr Number cannot be empty!', - ], 403); - // return response()->json(['error' => 'Missing file-name header'], 400); + ], 404); + // return response()->json(['error' => 'Missing file-name header'], 404); } $filename = basename($filename); @@ -404,7 +404,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Pdf File not found for the provided GrNumber!', - ], 403); + ], 404); } $file = Storage::disk('local')->get($filePath); @@ -428,7 +428,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } $plantCode = $request->header('plant-code'); @@ -444,7 +444,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'GR Number cannot be empty!', - ], 403); + ], 404); } $plant = Plant::where('code', $plantCode)->first(); @@ -526,7 +526,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } $plantCode = $request->header('plant-code'); @@ -542,7 +542,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process Order cannot be empty!', - ], 403); + ], 404); } $plant = Plant::where('code', $plantCode)->first(); @@ -596,14 +596,14 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' has already reached its order quantity.", - ], 400); + ], 404); } // if ($totalReceivedQty > $proOrdAgPlant->order_quantity) { // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' received quantity is more than its order quantity." - // ], 400); + // ], 404); // } return response()->json([ @@ -628,7 +628,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 404); } Log::info('Process Order POST API called >>', ['request_data' => $request->all()]); @@ -640,19 +640,19 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); + ], 404); } elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code']) return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid plant code found!', - ], 400); + ], 404); } if (! $processOrder) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process-order are required!', - ], 400); + ], 404); } $plant = Plant::where('code', $plantCode)->first(); @@ -745,17 +745,17 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$createdBy}' not found!", - ], 403); + ], 404); } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$createdBy}' not found for the plant code '{$plantCode}'!", - ], 403); + ], 404); } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Process Employee') && ! $user->hasRole('Process Supervisor')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', - ], 403); + ], 404); } $item = Item::where('code', $itemCode)->first(); @@ -778,6 +778,17 @@ class PdfController extends Controller ], 404); } + $existing = ProcessOrder::where('plant_id', $plantId) + ->where('sfg_number', $sfgNo) + ->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "SFG number '{$sfgNo}' already exist for the plant code '{$plantCode}'!", + ], 404); + } + $itemId = $itemPlant->id; $existing = ProcessOrder::where('plant_id', $plantId) @@ -789,7 +800,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' already has item_code '{$existing->item->code}' for the plant code '{$plantCode}'!", - ], 409); + ], 404); } $existing = ProcessOrder::where('plant_id', $plantId) @@ -801,7 +812,7 @@ class PdfController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!", - ], 409); + ], 404); } $alreadyReceived = ProcessOrder::where('plant_id', $plantId)