From a41626e8135a1e53d191a7dfa0022254c6e4b35e Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 23 Nov 2026 11:53:43 +0530 Subject: [PATCH] Added post api logic of production characteristics in pdf controller --- app/Http/Controllers/PdfController.php | 316 +++++++++++++++++++++++++ 1 file changed, 316 insertions(+) diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index 268df09b..2eeea51c 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -6,8 +6,12 @@ use App\Models\CharacteristicValue; use App\Models\GrMaster; use App\Models\Item; use App\Models\Line; +use App\Models\Machine; use App\Models\Plant; use App\Models\ProcessOrder; +use App\Models\ProductionCharacteristic; +use App\Models\ProductionQuantity; +use App\Models\Shift; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; @@ -25,6 +29,318 @@ class PdfController extends Controller // } + public function storeProdCharData(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $headerAuth = $request->header('Authorization'); + $expectedToken = 'Bearer '.$expectedUser.':'.$expectedPw; + + if ($headerAuth != $expectedToken) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!', + ], 401); + } + + $plantCode = $request->header('plant-code'); + $itemCode = $request->header('item-code'); + $lineName = $request->header('line-name'); + $workCenter = $request->header('work-center'); + $pOrder = $request->header('production-order'); + $sNo = $request->header('serial-number'); + $employeeCode = $request->header('employee-code'); + + if ($plantCode == null || $plantCode == '' || ! $plantCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 400); + } elseif (! is_numeric($plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 400); + } 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); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 400); + } + + $plant = Plant::where('code', $plantCode)->first(); + if (! $plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in plants table!", + ], 404); + } + + $plantId = $plant->id; + $plantName = $plant->name; + + if ($lineName == null || $lineName == '' || ! $lineName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name can't be empty!", + ], 404); + } + + $line = Line::where('name', $lineName)->first(); + if (! $line) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line '{$lineName}' not found in lines table!", + ], 404); + } + + $line = Line::where('name', $lineName)->where('plant_id', $plantId)->first(); + if (! $line) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line '{$lineName}' not found for the plant name '{$plantName}' in lines table!", + ], 404); + } + + $lineId = $line->id; + + if ($itemCode == null || $itemCode == '' || ! $itemCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code can't be empty!", + ], 404); + } + + $item = Item::where('code', $itemCode)->first(); + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item Code '{$itemCode}' not found in items table!", + ], 404); + } + + $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found for the plant name '{$plantName}' in items table!", + ], 404); + } + + $itemId = $item->id; + + if ($workCenter == null || $workCenter == '' || ! $workCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center can't be empty!", + ], 404); + } elseif (Str::length($workCenter) < 6) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' should contain minimum 6 characters!", + ], 404); + } elseif (! ctype_alnum($workCenter)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' should contain only alpha-numeric values!", + ], 404); + } + + $machine = Machine::where('work_center', $workCenter)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in machines table!", + ], 404); + } + + $machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found for the plant name '{$plantName}' in machines table!", + ], 404); + } + + $machine = Machine::where('line_id', $lineId)->where('work_center', $workCenter)->where('plant_id', $plantId)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' is not mapped for the given line name '{$lineName}' in machines table!", + ], 404); + } + + $machineId = $machine->id; + + + if ($pOrder == null || $pOrder == '' || ! $pOrder) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Production Order can't be empty!", + ], 404); + } elseif (Str::length($pOrder) < 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Production Order '{$pOrder}' should contain minimum 7 digits!", + ], 404); + } + + if ($sNo == null || $sNo == '' || ! $sNo) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Serial Number can't be empty!", + ], 404); + } elseif (Str::length($sNo) < 9) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Serial Number '{$sNo}' should contain minimum 9 digits!", + ], 404); + } elseif (! ctype_alnum($sNo)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Serial Number '{$sNo}' should contain only alpha-numeric values!", + ], 404); + } + + if ($employeeCode == null || $employeeCode == '' || ! $employeeCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Employee Code can't be empty!", + ], 404); + } elseif (Str::length($employeeCode) < 6) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Employee Code '{$employeeCode}' should contain minimum 6 digits!", + ], 404); + } elseif (! ctype_alnum($employeeCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Employee Code '{$employeeCode}' should contain only alpha-numeric values!", + ], 404); + } + + $eCode = User::where('plant_id', $plantId)->where('name', $employeeCode)->first(); + + if(!$eCode){ + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Employee Code '{$employeeCode}' not found!", + ], 404); + } + + $userName = $eCode->name; + + $data = $request->all(); + + $errorMessage = ''; + + foreach ($data as $index => $characteristic) { + + $characteristicsName = $characteristic['characteristics_name'] ?? ''; + $obsValue = $characteristic['observed_value'] ?? ''; + $status = $characteristic['status'] ?? ''; + + if (empty($characteristicsName)) { + $errorMessage = "Row " . ($index + 1) . ": Characteristics name is missing."; + break; + } elseif ($obsValue === '') { + $errorMessage = "Row " . ($index + 1) . ": Observed value is missing."; + break; + } elseif (empty($status)) { + $errorMessage = "Row " . ($index + 1) . ": Status is missing."; + break; + } elseif (!in_array($status, ['Ok', 'NotOk'], true)) { + $errorMessage = "Row " . ($index + 1) . ": Status must be either Ok or NotOk."; + break; + } + } + + if ($errorMessage != '') { + $response = [ + 'status_code' => 'ERROR', + 'status_description' => $errorMessage, + ]; + + return response()->json($response, 404); + } + + $recordProdQuan = ProductionQuantity::where('plant_id', $plantId)->where('serial_number', $sNo)->first(); + + if($recordProdQuan){ + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Duplicate Serial Number $sNo already exist against plant $plantName.", + ], 404); + } + + $currentTime = now()->format('H:i'); + + if ($currentTime >= '08:00' && $currentTime < '19:30') { + $shift = 'Day'; + } else { + $shift = 'Night'; + } + + $shift = Shift::where('plant_id', $plantId)->where('name', $shift)->first(); + + if (!$shift) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "$shift shift not found for plant $plantName.", + ], 404); + } + + $recordIns = ProductionQuantity::insert([ + 'plant_id' => $plantId, + 'line_id' => $lineId, + 'item_id' => $itemId, + 'shift_id' => $shift->id, + 'machine_id' => $machineId, + 'serial_number' => $sNo, + 'production_order' => $pOrder, + 'operator_id' => $userName, + 'created_at' => now(), + ]); + + if($recordIns){ + // Insert into production_characteristics (multiple records) + foreach ($data as $characteristic) { + + $recordCharIns = ProductionCharacteristic::insert([ + 'plant_id' => $plantId, + 'line_id' => $lineId, + 'item_id' => $itemId, + 'machine_id' => $machineId, + 'serial_number' => $sNo, + 'production_order' => $pOrder, + 'characteristic_name' => $characteristic['characteristics_name'], + 'observed_value' => $characteristic['observed_value'], + 'status' => $characteristic['status'], + 'created_at' => now(), + ]); + } + + if($recordCharIns){ + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Production quantity and characteristic records have been created successfully.', + ], 200); + } + else{ + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Failed to insert production characteristics for Serial Number $sNo.Please Revoke this Serial Number from Production Quantities table", + ], 404); + } + + } + } + public function updateGR(Request $request) { $expectedUser = env('API_AUTH_USER');