From 36e51ad6cb5ad73ebd7dc01d7f98689114a03a7a Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 26 Nov 2025 19:48:52 +0530 Subject: [PATCH] Enhanced storeProcessOrderData method with improved validation and error handling for plant code, item code, coil number, order quantity, received quantity, SFG number, machine ID, and created by --- app/Http/Controllers/PdfController.php | 182 ++++++++++++++++++++----- 1 file changed, 145 insertions(+), 37 deletions(-) diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index 1768358..200e8cb 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -631,15 +631,27 @@ class PdfController extends Controller ], 403); } - Log::info('POST : Process Orders API called', ['request_data' => $request->all()]); + Log::info('Process Order POST API called >>', ['request_data' => $request->all()]); $plantCode = $request->header('plant-code'); $processOrder = $request->header('process-order'); - if (! $plantCode || ! $processOrder) { + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'plant-code and process-order are required', + 'status_description' => "Plant code can't be empty!", + ], 400); + } 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); + } + + if (! $processOrder) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Process-order are required!', ], 400); } @@ -647,88 +659,184 @@ class PdfController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code', + 'status_description' => "Plant code '{$plantCode}' not found!", ], 404); } $plantId = $plant->id; - $validated = $request->validate([ - 'item_code' => 'nullable|integer', - 'coil_number' => 'nullable|string', - 'order_quantity' => 'nullable|integer', - 'received_quantity' => 'nullable|numeric', - 'created_by' => 'nullable|string', - ]); + $data = $request->all(); - $item = Item::where('code', $validated['item_code']) - ->where('plant_id', $plantId) - ->first(); + $itemCode = $data['item_code'] ?? ''; + $coilNo = $data['coil_number'] ?? ''; + $orderQty = $data['order_quantity'] ?? 0; + $receivedQty = $data['received_quantity'] ?? 0; + $sfgNo = $data['sfg_number'] ?? ''; + $machineId = $data['machine_id'] ?? ''; + $createdBy = $data['created_by'] ?? ''; - if (! $item) { + // $validated = $request->validate([ + // 'item_code' => 'nullable|integer', + // 'coil_number' => 'nullable|string', + // 'order_quantity' => 'nullable|integer', + // 'received_quantity' => 'nullable|numeric', + // 'sfg_number' => 'nullable|string', + // 'machine_id' => 'nullable|string', + // 'created_by' => 'nullable|string', + // ]); + + if ($itemCode == null || $itemCode == '' || ! $itemCode) { return response()->json([ - 'message' => 'Invalid item_code for this plant', + '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 should contain minimum 6 digits!', + ], 404); + } elseif (! ctype_alnum($itemCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Item code should contain only alpha-numeric values!', ], 404); } - $createdBy = $validated['created_by'] ?? null; + if ($coilNo == null || $coilNo == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number can't be empty!", + ], 404); + } elseif (! is_numeric($coilNo) || Str::length($coilNo) <= 0 || ! preg_match('/^\d{1,}$/', $coilNo)) { // !ctype_digit($data['plant_code']) + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid coil number found!', + ], 404); + } + + if ($sfgNo == null || $sfgNo == '' || ! $sfgNo) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "SFG number can't be empty!", + ], 404); + } + + if ($machineId == null || $machineId == '' || ! $machineId) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine ID can't be empty!", + ], 404); + } + + if ($createdBy == null || $createdBy == '' || ! $createdBy) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($createdBy == 'jothi') { + $createdBy = 'Admin'; + } $user = User::where('name', $createdBy)->first(); + $userPlant = User::where('name', $createdBy)->where('plant_id', $plantId)->first(); + if (! $user) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "User '{$createdBy}' not found", + 'status_description' => "User name '{$createdBy}' not found!", + ], 403); + } 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); + } 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); + } + + $item = Item::where('code', $itemCode)->first(); + + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found!", ], 404); } - $existing = ProcessOrder::where('plant_id', $plant->id) + $itemPlant = Item::where('code', $itemCode) + ->where('plant_id', $plantId) + ->first(); + + if (! $itemPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", + ], 404); + } + + $itemId = $itemPlant->id; + + $existing = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) - ->where('item_id', '!=', $item->id) + ->where('item_id', '!=', $itemId) ->first(); if ($existing) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' already has item_code '{$existing->item->code}'", + 'status_description' => "Process order '{$processOrder}' already has item_code '{$existing->item->code}' for the plant code '{$plantCode}'!", ], 409); } - $alreadyReceived = ProcessOrder::where('plant_id', $plant->id) + $existing = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) - ->where('item_id', $item->id) + ->where('coil_number', $coilNo) + ->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!", + ], 409); + } + + $alreadyReceived = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('item_id', $itemId) ->sum('received_quantity'); - $newReceived = $validated['received_quantity'] ?? 0; - $orderQty = $validated['order_quantity'] ?? 0; - if ($orderQty == 0) { - $orderQty = ProcessOrder::where('plant_id', $plant->id) + $orderQty = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) - ->where('item_id', $item->id) + ->where('item_id', $itemId) ->value('order_quantity') ?? 0; } - $total = $alreadyReceived + $newReceived; + $total = $alreadyReceived + $receivedQty; if ($total > $orderQty) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Received quantity cannot exceed order quantity. - Order Qty = {$orderQty}, Already Received = {$alreadyReceived},Trying to Insert = {$newReceived}", + 'status_description' => "Received quantity should not exceed order quantity! Order Qty = {$orderQty}, Already Received Qty = {$alreadyReceived}, Trying to Insert Qty = {$receivedQty}", ], 404); } try { ProcessOrder::Create( [ - 'plant_id' => $plant->id, + 'plant_id' => $plantId, 'process_order' => $processOrder, - 'item_id' => $item->id, - 'coil_number' => $validated['coil_number'] ?? '', - 'order_quantity' => $validated['order_quantity'] ?? 0, - 'received_quantity' => $validated['received_quantity'] ?? 0, - 'created_by' => $validated['created_by'] ?? '', + 'item_id' => $itemId, + 'coil_number' => $coilNo, + 'order_quantity' => $orderQty, + 'received_quantity' => $receivedQty, + 'sfg_number' => $sfgNo, + 'machine_name' => $machineId, + 'created_by' => $createdBy, ] ); -- 2.49.1