From 60d5b9ca6b08295386832f4a2d2eb0c941b7aac4 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 14:56:19 +0530 Subject: [PATCH] Added all the process order related logic in characteristics controller --- .../Controllers/CharacteristicsController.php | 397 ++++++++++++++++-- 1 file changed, 352 insertions(+), 45 deletions(-) diff --git a/app/Http/Controllers/CharacteristicsController.php b/app/Http/Controllers/CharacteristicsController.php index 2cc1139..fad0657 100644 --- a/app/Http/Controllers/CharacteristicsController.php +++ b/app/Http/Controllers/CharacteristicsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\CharacteristicValue; use App\Models\ClassCharacteristic; use App\Models\Item; use App\Models\Line; @@ -1989,7 +1990,6 @@ class CharacteristicsController extends Controller public function getCharMaster(Request $request) { - $expectedUser = env('API_AUTH_USER'); $expectedPw = env('API_AUTH_PW'); $header_auth = $request->header('Authorization'); @@ -1999,7 +1999,7 @@ class CharacteristicsController extends Controller return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', - ], 403); + ], 401); } $plantCode = $request->header('plant-code'); @@ -2007,36 +2007,66 @@ class CharacteristicsController extends Controller $lineName = $request->header('line-name'); $workCenter = $request->header('work-center'); - if ($plantCode == null || $plantCode == '') { + if ($plantCode == null || $plantCode == '' || ! $plantCode) { 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)) { + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", ], 400); - } elseif ($itemCode == null || $itemCode == '') { + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code can't be empty!", + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", ], 400); - } elseif (Str::length($itemCode) < 6 || ! ctype_alnum($itemCode)) { + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid item code found!', + 'status_description' => "Invalid plant code '{$plantCode}' found!", ], 400); - } elseif ($lineName == null || $lineName == '') { + } 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); + } elseif ($lineName == null || $lineName == '' || ! $lineName) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Line name can't be empty!", - ], 400); - } elseif ($workCenter == null || $workCenter == '') { + ], 404); + } elseif ($workCenter == null || $workCenter == '' || ! $workCenter) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center can't be empty!", - ], 400); + ], 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); } $plant = Plant::where('code', $plantCode)->first(); @@ -2044,97 +2074,374 @@ class CharacteristicsController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plants table!", + ], 404); } $plantId = $plant->id; + $plantName = $plant->name; $item = Item::where('code', $itemCode)->first(); if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Item Code not found in item table!', + 'status_description' => "Item Code '{$itemCode}' not found in items table!", ], 404); } - $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); + $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); 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 '{$itemCode}' not found for the plant name '{$plantName}' in items table!", ], 404); } + $itemId = $item->id; + $line = Line::where('name', $lineName)->first(); if (! $line) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Line not found in lines table!', + 'status_description' => "Line name '{$lineName}' not found in lines table!", ], 404); } - $lineAgaPlant = Line::where('plant_id', $plantId)->where('name', $lineName)->first(); + $line = Line::where('name', $lineName)->where('plant_id', $plantId)->first(); - if (! $lineAgaPlant) { + if (! $line) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Line Name not found in lines table for the plant : '$plant->name'!", + 'status_description' => "Line name '{$lineName}' not found for the plant name '{$plantName}' in lines table!", ], 404); } - $work = Machine::where('work_center', $workCenter)->first(); + $lineId = $line->id; - if (! $work) { + $machine = Machine::where('work_center', $workCenter)->first(); + + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Work Center not found in machines table!', + 'status_description' => "Work center '{$workCenter}' not found in machines table!", ], 404); } - $workAgaPlant = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first(); + $machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); - if (! $workAgaPlant) { + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Work center not found in machines table for the plant : '$plant->name'!", + '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; + // $description = $item ? $item->description : ''; // $uom = $item ? $item->uom : ''; // $category = $item ? $item->category : ''; - $charMaster = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $item->id) - ->where('line_id', $lineAgaPlant->id)->where('machine_id', $workAgaPlant->id) - ->first(); + $charMasters = ProductCharacteristicsMaster::with('workGroupMaster')->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->where('plant_id', $plantId)->get(); // ->select(['name', 'characteristics_type', 'inspection_type', 'lower', 'middle', 'upper', 'work_group_master_id']) - if (! $charMaster) { + if ($charMasters->count() <= 0) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Characteristics not found in product master table for the plant : '$plant->name'!", + 'status_description' => "Characteristics not found for the plant name '{$plantName}' in product master table!", + ], 404); + } else { + if ($charMasters->count() == 1) { + $charMasters = ProductCharacteristicsMaster::with('workGroupMaster')->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->where('plant_id', $plantId)->first(); + + // $workGroup = WorkGroupMaster::find($charMasters->work_group_master_id); + // $workGroupName = $workGroup?->name ?? ''; + + $output = [ + 'work_group_master' => $charMasters?->workGroupMaster->name ?? '', // $workGroupName ?? '', + 'name' => $charMasters?->name ?? '', + 'characteristics_type' => $charMasters?->characteristics_type ?? '', + 'inspection_type' => $charMasters?->inspection_type ?? '', + 'lower' => (string) $charMasters?->lower ?? '', + 'middle' => (string) $charMasters?->middle ?? '', + 'upper' => (string) $charMasters?->upper ?? '', + ]; + } else { + $output = $charMasters->map(function ($charMast) { + $charMaster = [ + 'work_group_master' => $charMast?->workGroupMaster->name ?? '', // optional($charMast->workGroupMaster)->name ?? '', + 'name' => $charMast?->name ?? '', + 'characteristics_type' => $charMast?->characteristics_type ?? '', + 'inspection_type' => $charMast?->inspection_type ?? '', + 'lower' => (string) $charMast?->lower ?? '', + 'middle' => (string) $charMast?->middle ?? '', + 'upper' => (string) $charMast?->upper ?? '', + ]; + + return $charMaster; + }); + } + } + + return response()->json($output, 200); + } + + + public function storeCharValues(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'); + + 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); } - $workGroup = WorkGroupMaster::find($charMaster->work_group_master_id); - $workGroupName = $workGroup?->name ?? ''; + $plantId = $plant->id; + $plantName = $plant->name; - $output = [ - 'work_group_master' => $workGroupName ?? '', - 'name' => $charMaster?->name ?? '', - 'inspection_type' => $charMaster?->inspection_type ?? '', - 'characteristics_type' => $charMaster?->characteristics_type ?? '', - 'upper' => (string) $charMaster?->upper ?? '', - 'lower' => (string) $charMaster?->lower ?? '', - 'middle' => (string) $charMaster?->middle ?? '', - ]; + if ($lineName == null || $lineName == '' || ! $lineName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name can't be empty!", + ], 404); + } - return response()->json($output, 200); + $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; + + $data = $request->all(); + + $processOrder = $data['process_order'] ?? ''; + $coilNo = $data['coil_number'] ?? ''; + $status = $data['status'] ?? ''; + $obsValue = $data['observed_value'] ?? ''; + $createdBy = $data['created_by'] ?? ''; + + if ($processOrder == null || $processOrder == '' || ! $processOrder) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order can't be empty!", + ], 404); + } elseif (Str::length($processOrder) < 9) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order '{$processOrder}' should contain minimum 9 digits!", + ], 404); + } + + if ($coilNo == null || $coilNo == '' || ! $coilNo) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number can't be empty!", + ], 404); + } elseif (! is_numeric($coilNo)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number '{$coilNo}' should contain only numeric values!", + ], 404); + } elseif (Str::length($coilNo) > 1 && ! Str::contains($coilNo, '.') && ! preg_match('/^[1-9][0-9]*$/', $coilNo)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number '{$coilNo}' should not begin with '0'!", + ], 404); + } + + if ($status == null || $status == '' || ! $status) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Status can't be empty!", + ], 404); + } elseif (! in_array($status, ['Ok', 'NotOk'], true)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Status must be either 'Ok' or 'NotOk'!", + ], 404); + } + + if ($obsValue == null || $obsValue == '' || ! $obsValue) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Observed value can't be empty!", + ], 404); + } + + if ($createdBy == null || $createdBy == '' || ! $createdBy) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Created by can't be empty!", + ], 404); + } + + $existing = CharacteristicValue::where('process_order', $processOrder)->where('item_id', '!=', $itemId)->where('plant_id', $plantId)->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order '{$processOrder}' already has item_code '{$existing->item->code}' for the plant name '{$plantName}'!", + ], 404); + } + + $existing = CharacteristicValue::where('process_order', $processOrder)->where('coil_number', $coilNo)->where('plant_id', $plantId)->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant name '{$plantName}'!", + ], 404); + } + + CharacteristicValue::create([ + 'plant_id' => $plantId, + 'line_id' => $lineId, + 'item_id' => $itemId, + 'machine_id' => $machineId, + 'process_order' => $processOrder, + 'coil_number' => $coilNo, + 'status' => $status, + 'observed_value' => $obsValue, + 'created_by' => $createdBy, + ]); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Characteristics values inserted successfully.', + ], 200); } /**