From 4da20051f6a05fb1d66aa88a030956f2449eb2e1 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 10 Feb 2026 14:36:49 +0530 Subject: [PATCH] Added request-type parameter and some validation on GET and POST --- .../Controllers/CharacteristicsController.php | 2062 ++++++++--------- 1 file changed, 916 insertions(+), 1146 deletions(-) diff --git a/app/Http/Controllers/CharacteristicsController.php b/app/Http/Controllers/CharacteristicsController.php index 0ec7c4a..7e0c61a 100644 --- a/app/Http/Controllers/CharacteristicsController.php +++ b/app/Http/Controllers/CharacteristicsController.php @@ -14,6 +14,7 @@ use App\Models\RequestCharacteristic; use App\Models\User; use App\Models\WorkGroupMaster; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; use Schema; @@ -72,10 +73,6 @@ class CharacteristicsController extends Controller $item = Item::where('code', $itemCode)->first(); - $itemPlant = Item::where('code', $itemCode) - ->where('plant_id', $plantId) - ->first(); - if (! $item) { return response()->json([ 'status_code' => 'ERROR', @@ -83,15 +80,19 @@ class CharacteristicsController extends Controller ], 404); } - if (! $itemPlant) { + $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 code '{$plant->code}'!", + 'status_description' => "Item Code '{$itemCode}' not found for the plant code '{$plantCode}'!", ], 404); } - // $characteristics = ProductCharacteristicsMaster::where('plant_id', $plant->id) - // ->where('item_id', $item->id) + $ItemId = $item->id; + + // $characteristics = ProductCharacteristicsMaster::where('plant_id', $plantId) + // ->where('item_id', $ItemId) // ->first([ // 'line_id', // 'machine_id', @@ -103,8 +104,8 @@ class CharacteristicsController extends Controller // 'middle' // ]); - $characteristics = ProductCharacteristicsMaster::where('plant_id', $plant->id) - ->where('item_id', $item->id) + $characteristics = ProductCharacteristicsMaster::where('plant_id', $plantId) + ->where('item_id', $ItemId) ->get([ 'line_id', 'machine_id', @@ -119,7 +120,7 @@ class CharacteristicsController extends Controller if ($characteristics->count() == 0) { return response()->json([ 'status_code' => 'ERROR', - 'message' => "No data found for the plant_code: {$plant->code} and item_code: {$item->code}", + 'message' => "No data found for the plant code: {$plantCode} and item code: {$itemCode}", ], 404); } @@ -136,12 +137,12 @@ class CharacteristicsController extends Controller // unset($response['machine_id']); // $workCenter = Machine::where('id', $machineId) - // ->where('plant_id', $plant->id) + // ->where('plant_id', $plantId) // ->where('line_id', $characteristics->line_id) // ->value('work_center'); // $workGroupMasterId = Machine::where('line_id', $characteristics->line_id) - // ->where('plant_id', $plant->id) + // ->where('plant_id', $plantId) // ->value('work_group_master_id'); // $groupWorkCenter = null; @@ -172,12 +173,12 @@ class CharacteristicsController extends Controller $lineName = $line ? $line->name : null; $workCenter = Machine::where('id', $char->machine_id) - ->where('plant_id', $plant->id) + ->where('plant_id', $plantId) ->where('line_id', $char->line_id) ->value('work_center'); $workGroupMasterId = ProductCharacteristicsMaster::where('line_id', $char->line_id) - ->where('plant_id', $plant->id) + ->where('plant_id', $plantId) ->value('work_group_master_id'); $groupWorkCenter = null; @@ -307,9 +308,7 @@ class CharacteristicsController extends Controller ], 404); } - $machine = Machine::where('work_center', $workCenter) - ->where('plant_id', $plantId) - ->first(); + $machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); if (! $machine) { return response()->json([ @@ -1809,9 +1808,7 @@ class CharacteristicsController extends Controller ], 404); } - $machine = Machine::where('work_center', $workCenter) - ->where('plant_id', $plantId) - ->first(); + $machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); if (! $machine) { return response()->json([ @@ -2224,6 +2221,8 @@ class CharacteristicsController extends Controller ], 404); } + $ItemId = $item->id; + $line = Line::where('name', $lineName)->first(); if (! $line) { @@ -2266,7 +2265,7 @@ class CharacteristicsController extends Controller // $category = $item ? $item->category : ''; - $charMaster = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $item->id) + $charMaster = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $ItemId) ->where('line_id', $lineAgaPlant->id)->where('machine_id', $workAgaPlant->id) ->first(); @@ -2526,491 +2525,23 @@ class CharacteristicsController extends Controller ->header('Content-Type', 'text/plain'); } - // public function storeLaserRequestChar(Request $request) - // { - // $expectedUser = env('API_AUTH_USER'); - // $expectedPw = env('API_AUTH_PW'); - - // $header_auth = $request->header('Authorization'); - // $expectedToken = $expectedUser.':'.$expectedPw; - - // if ('Bearer '.$expectedToken != $header_auth) { - // return response('ERROR: Unauthorized', 403) - // ->header('Content-Type', 'text/plain'); - // } - - // $userName = $request->header('user-name'); - - // if (! $userName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name can't be empty!", - // ], 404); - // } elseif ($userName == 'jothi') { - // $userName = 'Admin'; - // } - - // $data = $request->all(); - - // $plantCode = $data['plant_code'] ?? ''; - // $workCenter = $data['work_center'] ?? ''; - // $machineName = $data['machine_name'] ?? ''; - // $jobNo = $data['aufnr'] ?? ''; - // $itemCode = $data['item_code'] ?? ''; - // $received = $data['received_characteristics'][0] ?? []; - // $requested = $data['requested_characteristics'][0] ?? []; - - // if ($plantCode == null || $plantCode == '' || ! $plantCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code can't be empty!", - // ], 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!', - // ], 404); - // } - - // $plant = Plant::where('code', $plantCode)->first(); - // if (! $plant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code '{$plantCode}' not found!", - // ], 404); - // } - - // $plantId = $plant->id; - - // if ($workCenter == null || $workCenter == '' || ! $workCenter) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center can't be empty!", - // ], 404); - // } - - // $machine = Machine::where('work_center', $workCenter)->first(); - // if (! $machine) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center '{$workCenter}' not found!", - // ], 404); - // } - - // $machineAgaPlant = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); - // if (! $machineAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}'!", - // ], 404); - // } - - // $MachineId = $machineAgaPlant->id; - - // if ($machineName == null || $machineName == '' || ! $machineName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name can't be empty!", - // ], 404); - // } - - // $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); - // if (! $mName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name '{$machineName}' not found in master!", - // ], 404); - // } - - // $mNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); - // if (! $mNameAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name '{$machineName}' not found in master against plant code '{$plantCode}'!", - // ], 404); - // } - - // $charId = $mNameAgaPlant->id; - - // if ($itemCode == null || $itemCode == '' || ! $itemCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code can't be empty!", - // ], 404); - // } - - // $iCode = Item::where('code', $itemCode)->first(); - // if (! $iCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' not found!", - // ], 404); - // } - - // $iCodeAgaPlant = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); - // if (! $iCodeAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", - // ], 404); - // } - - // $ItemId = $iCodeAgaPlant->id; - - // $pendingCharacteristics = []; - - // foreach ($received as $charKey => $receivedValue) { - // $pendingExists = RequestCharacteristic::where('aufnr', $jobNo) - // ->where('characteristic_name', $charKey) - // ->where(function ($q) { - // $q->whereNull('approver_status1') - // ->orWhereNull('approver_status2') - // ->orWhereNull('approver_status3'); - // }) - // ->exists(); - - // if ($pendingExists) { - // $pendingCharacteristics[] = $charKey; - // } - // } - - // if (!empty($pendingCharacteristics)) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Pending approvals exist for some characteristics', - // 'pending_characteristics' => $pendingCharacteristics - // ], 409); - // } - - // try - // { - // $workFlowIds = []; - - // foreach ($received as $charKey => $receivedValue) { - - // $record = RequestCharacteristic::create([ - // 'plant_id' => $plantId, - // 'machine_id' => $MachineId, - // 'item_id' => $ItemId, - // 'characteristic_approver_master_id' => $charId, - // 'aufnr' => $jobNo, - // 'characteristic_name' => $charKey, - // 'current_value' => $receivedValue, - // 'update_value' => $requested[$charKey], - // 'created_by' => $userName, - // 'created_at' => now(), - // 'updated_at' => now(), - // ]); - - // $workFlowIds[$charKey] = (string) $record->id; - // } - - // return response()->json([ - // 'work_flow_ids' => [$workFlowIds] - // ], 200); - // } - // catch (\Exception $e) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => $e->getMessage(), - // ], 500); - // } - // } - - /** - * Display the specified resource from storage. - */ - // public function storeLaserRequestChar(Request $request) - // { - // $expectedUser = env('API_AUTH_USER'); - // $expectedPw = env('API_AUTH_PW'); - - // $header_auth = $request->header('Authorization'); - // $expectedToken = $expectedUser.':'.$expectedPw; - - // if ('Bearer '.$expectedToken != $header_auth) { - // return response('ERROR: Unauthorized', 403) - // ->header('Content-Type', 'text/plain'); - // } - - // $userName = $request->header('user-name'); - - // if (! $userName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name can't be empty!", - // ], 404); - // } elseif ($userName == 'jothi') { - // $userName = 'Admin'; - // } - - // $data = $request->all(); - - // $plantCode = $data['plant_code'] ?? ''; - // $workCenter = $data['work_center'] ?? ''; - // $machineName = $data['machine_name'] ?? ''; - // $jobNo = $data['aufnr'] ?? ''; - // $itemCode = $data['item_code'] ?? ''; - // $characteristics = $data['requested_characteristics'] ?? []; - // // $requested = $data['requested_characteristics'][0] ?? []; - - // if ($plantCode == null || $plantCode == '' || ! $plantCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code can't be empty!", - // ], 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!', - // ], 404); - // } - - // $plant = Plant::where('code', $plantCode)->first(); - // if (! $plant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code '{$plantCode}' not found!", - // ], 404); - // } - - // $plantId = $plant->id; - - // $user = User::where('name', $userName)->first(); - - // $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); - - // if (! $user) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name '{$userName}' not found!", - // ], 403); - // } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name '{$userName}' not found for the plant code '{$plantCode}'!", - // ], 403); - // } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'User does not have rights!', - // ], 403); - // } - - // if ($workCenter == null || $workCenter == '' || ! $workCenter) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center can't be empty!", - // ], 404); - // } - - // $machine = Machine::where('work_center', $workCenter)->first(); - // if (! $machine) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center '{$workCenter}' not found!", - // ], 404); - // } - - // $machineAgaPlant = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); - // if (! $machineAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}'!", - // ], 404); - // } - - // $MachineId = $machineAgaPlant->id; - - // if ($machineName == null || $machineName == '' || ! $machineName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name can't be empty!", - // ], 404); - // } - - // $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); - // if (! $mName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name '{$machineName}' not found in master!", - // ], 404); - // } - - // $mNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); - // if (! $mNameAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name '{$machineName}' not found in master against plant code '{$plantCode}'!", - // ], 404); - // } - - // $charId = $mNameAgaPlant->id; - - // if ($itemCode == null || $itemCode == '' || ! $itemCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code can't be empty!", - // ], 404); - // } - - // $iCode = Item::where('code', $itemCode)->first(); - // if (! $iCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' not found!", - // ], 404); - // } - - // $iCodeAgaPlant = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); - // if (! $iCodeAgaPlant) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", - // ], 404); - // } - - // $ItemId = $iCodeAgaPlant->id; - - // $pendingCharacteristics = []; - - // foreach ($characteristics as $char) { - // $charName = strtoupper($char['characteristic_name']) ?? null; - // $pendingExists = RequestCharacteristic::where('aufnr', $jobNo) - // ->where('characteristic_name', $charName) - // ->latest() - // ->first(); - - // if ($pendingExists) { - - // $app1 = $pendingExists->approver_status1 ?? null; - // $app2 = $pendingExists->approver_status2 ?? null; - // $app3 = $pendingExists->approver_status3 ?? null; - - // if ($app1 != 'Rejected' && $app2 != 'Rejected' && $app3 != 'Rejected' && $app1 != 'Approved' && $app2 != 'Approved' && $app3 != 'Approved') { - // $pendingCharacteristics[] = strtoupper($charName); - // } - // } - // } - - // $uniquePendChars = array_unique($pendingCharacteristics); - - // if (! empty($uniquePendChars)) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Approval is already pending for the following characteristics : '.implode(', ', $uniquePendChars), - // ], 404); - // } - - // try { - - // $workFlowIds = []; - - // $year = now()->format('y'); - // $month = now()->format('m'); - // $prefix = "WF-{$year}{$month}-"; - - // // $existingWorkflowId = RequestCharacteristic::where('plant_id', $plantId) - // // ->where('machine_id', $MachineId) - // // ->where('aufnr', $jobNo) - // // ->value('work_flow_id'); - - // // if ($existingWorkflowId) { - // // $workflowId = $existingWorkflowId; - - // // } - - // $lastWorkflow = RequestCharacteristic::where('plant_id', $plantId) - // ->where('machine_id', $MachineId) - // ->where('work_flow_id', 'like', "{$prefix}%") - // ->orderByDesc('work_flow_id') - // ->first(); - - // if ($lastWorkflow) { - // $lastSerial = substr($lastWorkflow->work_flow_id, strlen($prefix)); - // $nextSerial = str_pad( - // intval($lastSerial) + 1, - // 3, - // '0', - // STR_PAD_LEFT - // ); - // } else { - // $nextSerial = '001'; - // } - - // $workflowId = "{$prefix}{$nextSerial}"; - - // foreach ($characteristics as $char) { - - // $record = RequestCharacteristic::create([ - // 'plant_id' => $plantId, - // 'machine_id' => $MachineId, - // 'item_id' => $ItemId, - // 'characteristic_approver_master_id' => $charId, - // 'aufnr' => $jobNo, - // 'characteristic_name' => strtoupper($char['characteristic_name']),//strtolower - // 'current_value' => $char['current_value'], - // 'update_value' => $char['update_value'], - // 'created_by' => $userName, - // 'work_flow_id' => $workflowId, - // ]); - - // $workFlowIds[strtoupper($char['characteristic_name'])] = (string) $record->work_flow_id; - // } - - // if (! $request->hasFile('pdf_file')) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'No PDF file provided!', - // ], 404); - // } - - // $file = $request->file('pdf_file'); - // $filename = $workflowId.'.pdf'; - - // $filePath = $file->storeAs( - // 'uploads/LaserDocs', - // $filename, - // 'local' - // ); - - // return response()->json([ - // 'work_flow_ids' => [$workFlowIds], - // ], 200); - - // } catch (\Exception $e) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => $e->getMessage(), - // ], 500); - // } - - // } - public function storeLaserRequestChar(Request $request) { $expectedUser = env('API_AUTH_USER'); $expectedPw = env('API_AUTH_PW'); - $header_auth = $request->header('Authorization'); - $expectedToken = $expectedUser.':'.$expectedPw; + $headerAuth = $request->header('Authorization'); + $expectedToken = 'Bearer '.$expectedUser.':'.$expectedPw; - if ('Bearer '.$expectedToken != $header_auth) { - return response('ERROR: Unauthorized', 403) - ->header('Content-Type', 'text/plain'); + if ($headerAuth !== $expectedToken) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!', + ], 403); } $userName = $request->header('user-name'); - if (! $userName) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "User name can't be empty!", - ], 404); - } elseif ($userName == 'jothi') { - $userName = 'Admin'; - } - $charField = $request->header('characteristic-field'); $json = $request->input('data'); @@ -3023,21 +2554,86 @@ class CharacteristicsController extends Controller $itemCode = $data['item_code'] ?? ''; $characteristics = $data['requested_characteristics'] ?? []; // $requested = $data['requested_characteristics'][0] ?? []; + $requestType = $request->header('request-type'); - if ($plantCode == null || $plantCode == '' || ! $plantCode) { + Log::info("Request {$requestType} POST API >>", ['user-name' => $userName, 'characteristic-field' => $charField, 'post-data' => $data]); + + if (! $userName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + + if (! $plantCode || $plantCode == null || $plantCode == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", ], 404); - } elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code']) + } elseif (! $itemCode || $itemCode == null || $itemCode == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code can't be empty!", + ], 404); + } elseif (! $workCenter || $workCenter == null || $workCenter == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center can't be empty!", + ], 404); + } elseif (! $jobNo || $jobNo == null || $jobNo == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number can't be empty!", + ], 404); + } elseif (! $machineName || $machineName == null || $machineName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name can't be empty!", + ], 404); + } elseif (! $requestType || $requestType == null || $requestType == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Request type can't be empty!", + ], 404); + } + + if ($charField == null || $charField == '' || $charField == 'nil') { + $charField = 'NIL'; + } + + if (! 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!', ], 404); } - if ($charField == null || $charField == '') { - $charField = 'NIL'; + if (Str::length($itemCode) < 6 || ! ctype_alnum($itemCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid item code found!', + ], 404); + } + + if (Str::length($jobNo) < 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' should contain minimum 7 digits!", + ], 404); + } elseif (! is_numeric($jobNo)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' should contain only numeric values!", + ], 404); + } + + if ($requestType != 'Characteristic' && $requestType != 'Quality') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Unknown request type found!', + ], 404); } $plant = Plant::where('code', $plantCode)->first(); @@ -3052,14 +2648,16 @@ class CharacteristicsController extends Controller $user = User::where('name', $userName)->first(); - $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); - if (! $user) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$userName}' not found!", ], 403); - } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + } + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $userPlant && ! $user->hasRole('Super Admin')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$userName}' not found for the plant code '{$plantCode}'!", @@ -3071,38 +2669,539 @@ class CharacteristicsController extends Controller ], 403); } - if ($workCenter == null || $workCenter == '' || ! $workCenter) { + $machine = Machine::where('work_center', $workCenter)->first(); + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in master!", + ], 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 code '{$plantCode}' in master!", + ], 404); + } + + $MachineId = $machine->id; + + $iCode = Item::where('code', $itemCode)->first(); + if (! $iCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found!", + ], 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 code '{$plantCode}'!", + ], 404); + } + + $ItemId = $item->id; + + $pCode = CharacteristicApproverMaster::where('plant_id', $plantId)->first(); + if (! $pCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in approver master!", + ], 404); + } + + $wCenter = CharacteristicApproverMaster::where('machine_id', $MachineId)->first(); + if (! $wCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in approver master!", + ], 404); + } + + $wCenterAgaPlant = CharacteristicApproverMaster::where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $wCenterAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); + if (! $mName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' not found in approver master!", + ], 404); + } + + $mNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); + if (! $mNameAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $cField = CharacteristicApproverMaster::where('characteristic_field', $charField)->first(); + if (! $cField) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}'' not found in approver master!", + ], 404); + } + + $cFieldAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('plant_id', $plantId)->first(); + if (! $cFieldAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $mNameAndWorkCenter = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->first(); + if (! $mNameAndWorkCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found in approver master!", + ], 404); + } + + $mNameAndWorkCenterAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $mNameAndWorkCenterAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $cFieldAndWorkCent = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->first(); + if (! $cFieldAndWorkCent) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the work center '{$workCenter}' in approver master!", + ], 404); + } + + $cFieldAndWorkCentAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $cFieldAndWorkCentAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $cFieldAndMach = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_name', $machineName)->first(); + if (! $cFieldAndMach) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the machine name '{$machineName}' in approver master!", + ], 404); + } + + $cFieldAndWorkCentAndMachAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->first(); + if (! $cFieldAndWorkCentAndMachAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', and machine name '{$machineName}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $pendingCharacteristics = []; + $unknownCharacteristics = []; + $appTypExist = null; + + if (! empty($characteristics)) { + $appTypExist = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->where('approver_type', 'Characteristic')->first(); + if (! $appTypExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', machine name '{$machineName}', and plant code '{$plantCode}' not found for the approver type 'Characteristic' in approver master!", + ], 404); + } + + $charId = $appTypExist->id; + + foreach ($characteristics as $char) { + $charNameUpp = strtoupper($char['characteristic_name']) ?? null; + $charNameLow = strtolower($char['characteristic_name']) ?? null; + $pendingExists = RequestCharacteristic::where('plant_id', $plantId)->where('aufnr', $jobNo)->where('characteristic_name', $charNameUpp)->latest()->first(); + + $columns = Schema::getColumnListing('class_characteristics'); + + if (! in_array($charNameLow, $columns, true)) { + $unknownCharacteristics[] = strtoupper($charNameUpp); + } + + if ($pendingExists) { + + $app1 = $pendingExists->approver_status1 ?? null; + $app2 = $pendingExists->approver_status2 ?? null; + $app3 = $pendingExists->approver_status3 ?? null; + + if ($app1 != 'Rejected' && $app2 != 'Rejected' && $app3 != 'Rejected' && $app1 != 'Approved' && $app2 != 'Approved' && $app3 != 'Approved') { + $pendingCharacteristics[] = strtoupper($charNameUpp); + } + } + } + + $uniquePendChars = array_unique($pendingCharacteristics); + + if (! empty($uniquePendChars)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Characteristic approval is already pending for the following request characteristics : '.implode(', ', $uniquePendChars), + ], 404); + } + + $uniqueUnkChars = array_unique($unknownCharacteristics); + + if (! empty($uniqueUnkChars)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Unknown characteristic fields found : '.implode(', ', $uniqueUnkChars), + ], 404); + } + + try { + + $workFlowIds = []; + + $year = now()->format('y'); + $month = now()->format('m'); + $date = now()->format('d'); + $prefix = "WF-{$year}{$month}{$date}-"; + + // $existingWorkflowId = RequestCharacteristic::where('plant_id', $plantId) + // ->where('machine_id', $MachineId) + // ->where('aufnr', $jobNo) + // ->value('work_flow_id'); + + // if ($existingWorkflowId) { + // $workflowId = $existingWorkflowId; + + // } + + $lastWorkflow = RequestCharacteristic::where('work_flow_id', 'like', "{$prefix}%") + // ->where('plant_id', $plantId) + // ->where('machine_id', $MachineId) + ->where('work_flow_id', 'like', "{$prefix}%") + ->orderByDesc('work_flow_id') + ->first(); + + if ($lastWorkflow) { + $lastSerial = substr($lastWorkflow->work_flow_id, strlen($prefix)); + $nextSerial = str_pad( + intval($lastSerial) + 1, + 4, + '0', + STR_PAD_LEFT + ); + } else { + $nextSerial = '0001'; + } + + $workflowId = "{$prefix}{$nextSerial}"; + + $file = $request->file('file'); + + // if (! $request->hasFile('file')) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => 'No PDF file provided!', + // ], 404); + // } + + foreach ($characteristics as $char) { + + $record = RequestCharacteristic::create([ + 'plant_id' => $plantId, + 'machine_id' => $MachineId, + 'item_id' => $ItemId, + 'characteristic_approver_master_id' => $charId, + 'aufnr' => $jobNo, + 'characteristic_name' => strtoupper($char['characteristic_name']), // strtolower + 'current_value' => $char['current_value'], + 'update_value' => $char['update_value'], + 'created_by' => $userName, + 'work_flow_id' => $workflowId, + ]); + + $workFlowIds[strtoupper($char['characteristic_name'])] = (string) $record->work_flow_id; + } + + if ($request->hasFile('file')) { + $extension = strtolower($file->getClientOriginalExtension()); + $filename = $workflowId.'.'.$extension; + + if ($extension != 'pdf' && $extension != 'PDF') { + return response()->json([ + 'status' => 'ERROR', + 'status_description' => 'Only .PDF files are allowed.', + ], 404); + } + + if ($file && $file->getSize() > 1024 * 1024) { // 1 MB in bytes + return response()->json([ + 'status' => 'ERROR', + 'status_description' => 'File size must not exceed 1 MB', + ], 404); + } + + $filePath = $file->storeAs( + 'uploads/LaserDocs', + $filename, + 'local' + ); + + $filePath = "uploads/LaserDocs/{$filename}"; + + if (! Storage::disk('local')->exists($filePath)) { + RequestCharacteristic::where('work_flow_id', $workflowId)->where('plant_id', $plantId)->forceDelete(); + + return response()->json([ + 'status' => 'ERROR', + 'status_description' => "Document upload failed for Characteristic Work Flow ID : '{$workflowId}'", + ], 404); + } + } + + return response()->json([ + 'work_flow_ids' => [$workFlowIds], + ], 200); + + } catch (\Exception $e) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => $e->getMessage(), + ], 500); + } + } else { + $appTypExist = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->where('approver_type', 'Quality')->first(); + if (! $appTypExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', machine name '{$machineName}', and plant code '{$plantCode}' not found for the approver type 'Quality' in approver master!", + ], 404); + } + + $charId = $appTypExist->id; + + $pendingExists = RequestCharacteristic::where('plant_id', $plantId)->where('aufnr', $jobNo)->where(function ($query) { + $query->whereNull('characteristic_name')->orWhere('characteristic_name', ''); + })->latest()->first(); + + if ($pendingExists) { + + $app1 = $pendingExists->approver_status1 ?? null; + $app2 = $pendingExists->approver_status2 ?? null; + $app3 = $pendingExists->approver_status3 ?? null; + + if ($app1 != 'Approved' && $app2 != 'Approved' && $app3 != 'Approved') {// $app1 != 'Rejected' && $app2 != 'Rejected' && $app3 != 'Rejected' && + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Quality Approval is already pending for the Job Number : '{$jobNo}'", + ], 404); + } + } + + try { + $year = now()->format('y'); + $month = now()->format('m'); + $date = now()->format('d'); + $prefix = "WF-{$year}{$month}{$date}-"; + + $lastWorkflow = RequestCharacteristic::where('work_flow_id', 'like', "{$prefix}%") + ->where('work_flow_id', 'like', "{$prefix}%") + ->orderByDesc('work_flow_id') + ->first(); + + if ($lastWorkflow) { + $lastSerial = substr($lastWorkflow->work_flow_id, strlen($prefix)); + $nextSerial = str_pad( + intval($lastSerial) + 1, + 4, + '0', + STR_PAD_LEFT + ); + } else { + $nextSerial = '0001'; + } + + $workflowId = "{$prefix}{$nextSerial}"; + + $file = $request->file('file'); + if (! $request->hasFile('file')) { + return response()->json([ + 'status' => 'ERROR', + 'status_description' => 'Quality approval image (.png) not found!', + ], 404); + } + + $extension = strtolower($file->getClientOriginalExtension()); + + $filename = $workflowId.'.'.$extension; + + if ($extension != 'png' && $extension != 'PNG') { + return response()->json([ + 'status' => 'ERROR', + 'status_description' => 'Only .PNG files are allowed.', + ], 404); + } + + if ($file && $file->getSize() > 50 * 1024) { + return response()->json([ + 'status' => 'ERROR', + 'status_description' => 'File size must not exceed 50 KB', + ], 404); + } + + $record = RequestCharacteristic::create([ + 'plant_id' => $plantId, + 'machine_id' => $MachineId, + 'item_id' => $ItemId, + 'characteristic_approver_master_id' => $charId, + 'aufnr' => $jobNo, + 'characteristic_name' => '', + 'current_value' => '', + 'update_value' => '', + 'created_by' => $userName, + 'work_flow_id' => $workflowId, + ]); + + $approvalId = []; + + if ($request->hasFile('file')) { + $filePath = $file->storeAs( + 'uploads/LaserDocs', + $filename, + 'local' + ); + + $approvalId['Approval_ID'] = $workflowId; + + $filePath = "uploads/LaserDocs/{$filename}"; + + if (! Storage::disk('local')->exists($filePath)) { + RequestCharacteristic::where('work_flow_id', $workflowId)->where('plant_id', $plantId)->forceDelete(); + + return response()->json([ + 'status' => 'ERROR', + 'status_description' => "Document upload failed for Quality Work Flow ID : '{$workflowId}'", + ], 404); + } + + return response()->json([ + 'work_flow_ids' => [$approvalId], + ], 200); + } else { + RequestCharacteristic::where('work_flow_id', $workflowId)->where('plant_id', $plantId)->forceDelete(); + + return response()->json([ + 'status' => 'ERROR', + 'status_description' => "Document upload failed for Quality Work Flow ID : '{$workflowId}'", + ], 404); + } + + } catch (\Exception $e) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => $e->getMessage(), + ], 500); + } + } + } + + public function getLaserRequestChar(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!', + ], 403); + } + + $userName = $request->header('user-name'); + $charField = $request->header('characteristic-field'); + $plantCode = $request->header('plant-code'); + $workCenter = $request->header('work-center'); + $machineName = $request->header('machine-name'); + $jobNo = $request->header('aufnr'); + $itemCode = $request->header('item-code'); + $workFlowId = $request->header('work-flow-id'); + $workFlowId = strtoupper($workFlowId); + $requestType = $request->header('request-type'); + + Log::info("Request {$requestType} POST API >>", ['user-name' => $userName, 'characteristic-field' => $charField, 'plant-code' => $plantCode, 'work-center' => $workCenter, 'machine-name' => $machineName, 'aufnr' => $jobNo, 'item-code' => $itemCode, 'work-flow-id' => $workFlowId]); + + if (! $userName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + + if (! $plantCode || $plantCode == null || $plantCode == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! $itemCode || $itemCode == null || $itemCode == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code can't be empty!", + ], 404); + } elseif (! $workCenter || $workCenter == null || $workCenter == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center can't be empty!", ], 404); - } - - if ($machineName == null || $machineName == '' || ! $machineName) { + } elseif (! $jobNo || $jobNo == null || $jobNo == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number can't be empty!", + ], 404); + } elseif (! $machineName || $machineName == null || $machineName == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Machine name can't be empty!", ], 404); - } - - if ($itemCode == null || $itemCode == '') { + } elseif (! $requestType || $requestType == null || $requestType == '') { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code can't be empty!", - ], 400); - } elseif (Str::length($itemCode) < 6 || ! ctype_alnum($itemCode)) { + 'status_description' => "Request type can't be empty!", + ], 404); + } + + if ($charField == null || $charField == '' || $charField == 'nil') { + $charField = 'NIL'; + } + + if (! 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!', + ], 404); + } + + if (Str::length($itemCode) < 6 || ! ctype_alnum($itemCode)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid item code found!', ], 404); } - if (! $jobNo) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Job number can't be empty", - ], 404); - } elseif (Str::length($jobNo) < 7) { + if (Str::length($jobNo) < 7) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Job number '{$jobNo}' should contain minimum 7 digits!", @@ -3114,555 +3213,18 @@ class CharacteristicsController extends Controller ], 404); } - $machine = Machine::where('work_center', $workCenter)->first(); - if (! $machine) { + if ($requestType != 'Characteristic' && $requestType != 'Quality') { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found in master!", - ], 404); - } - - $machineAgaPlant = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first(); - if (! $machineAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in master!", - ], 404); - } - - $MachineId = $machineAgaPlant->id; - - $iCode = Item::where('code', $itemCode)->first(); - if (! $iCode) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Item code '{$itemCode}' not found!", - ], 404); - } - - $iCodeAgaPlant = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); - if (! $iCodeAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", - ], 404); - } - - $ItemId = $iCodeAgaPlant->id; - - $pCode = CharacteristicApproverMaster::where('plant_id', $plantId)->first(); - if (! $pCode) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Plant code '{$plantCode}' not found in characteristic approver master!", - ], 404); - } - - $wCenter = CharacteristicApproverMaster::where('machine_id', $MachineId)->first(); - if (! $wCenter) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found in characteristic approver master!", - ], 404); - } - - $wCenterAgaPlant = CharacteristicApproverMaster::where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); - if (! $wCenterAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); - if (! $mName) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' not found in characteristic approver master!", - ], 404); - } - - $mNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); - if (! $mNameAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $cField = CharacteristicApproverMaster::where('characteristic_field', $charField)->first(); - if (! $cField) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}' not found in characteristic approver master!", - ], 404); - } - - $cFieldAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('plant_id', $plantId)->first(); - if (! $cFieldAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $mNameAndWorkCenter = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->first(); - if (! $mNameAndWorkCenter) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found in characteristic approver master!", - ], 404); - } - - $mNameAndWorkCenterAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); - if (! $mNameAndWorkCenterAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $cFieldAndWorkCent = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->first(); - if (! $cFieldAndWorkCent) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}' not found for the work center '{$workCenter}' in characteristic approver master!", - ], 404); - } - - $cFieldAndWorkCentAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); - if (! $cFieldAndWorkCentAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $cFieldAndMach = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_name', $machineName)->first(); - if (! $cFieldAndMach) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}' not found for the machine name '{$machineName}' in characteristic approver master!", - ], 404); - } - - $cFieldAndWorkCentAndMachAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->first(); - if (! $cFieldAndWorkCentAndMachAgaPlant) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', and machine name '{$machineName}' not found for the plant code '{$plantCode}' in characteristic approver master!", - ], 404); - } - - $charId = $cFieldAndWorkCentAndMachAgaPlant->id; - - $pendingCharacteristics = []; - - foreach ($characteristics as $char) { - $charName = strtoupper($char['characteristic_name']) ?? null; - $pendingExists = RequestCharacteristic::where('plant_id', $plantId)->where('aufnr', $jobNo)->where('characteristic_name', $charName)->latest()->first(); - - if ($pendingExists) { - - $app1 = $pendingExists->approver_status1 ?? null; - $app2 = $pendingExists->approver_status2 ?? null; - $app3 = $pendingExists->approver_status3 ?? null; - - if ($app1 != 'Rejected' && $app2 != 'Rejected' && $app3 != 'Rejected' && $app1 != 'Approved' && $app2 != 'Approved' && $app3 != 'Approved') { - $pendingCharacteristics[] = strtoupper($charName); - } - } - } - - $uniquePendChars = array_unique($pendingCharacteristics); - - if (! empty($uniquePendChars)) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => 'Approval is already pending for the following characteristics : '.implode(', ', $uniquePendChars), - ], 404); - } - - try { - - $workFlowIds = []; - - $year = now()->format('y'); - $month = now()->format('m'); - $date = now()->format('d'); - $prefix = "WF-{$year}{$month}{$date}-"; - - // $existingWorkflowId = RequestCharacteristic::where('plant_id', $plantId) - // ->where('machine_id', $MachineId) - // ->where('aufnr', $jobNo) - // ->value('work_flow_id'); - - // if ($existingWorkflowId) { - // $workflowId = $existingWorkflowId; - - // } - - $lastWorkflow = RequestCharacteristic::where('work_flow_id', 'like', "{$prefix}%") - // ->where('plant_id', $plantId) - // ->where('machine_id', $MachineId) - ->where('work_flow_id', 'like', "{$prefix}%") - ->orderByDesc('work_flow_id') - ->first(); - - if ($lastWorkflow) { - $lastSerial = substr($lastWorkflow->work_flow_id, strlen($prefix)); - $nextSerial = str_pad( - intval($lastSerial) + 1, - 3, - '0', - STR_PAD_LEFT - ); - } else { - $nextSerial = '001'; - } - - $workflowId = "{$prefix}{$nextSerial}"; - - $file = $request->file('file'); - - // if (! $request->hasFile('file')) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'No PDF file provided!', - // ], 404); - // } - - foreach ($characteristics as $char) { - - $record = RequestCharacteristic::create([ - 'plant_id' => $plantId, - 'machine_id' => $MachineId, - 'item_id' => $ItemId, - 'characteristic_approver_master_id' => $charId, - 'aufnr' => $jobNo, - 'characteristic_name' => strtoupper($char['characteristic_name']), // strtolower - 'current_value' => $char['current_value'], - 'update_value' => $char['update_value'], - 'created_by' => $userName, - 'work_flow_id' => $workflowId, - ]); - - $workFlowIds[strtoupper($char['characteristic_name'])] = (string) $record->work_flow_id; - } - - if ($request->hasFile('file')) { - $filename = $workflowId.'.pdf'; - $filePath = $file->storeAs( - 'uploads/LaserDocs', - $filename, - 'local' - ); - } - - return response()->json([ - 'work_flow_ids' => [$workFlowIds], - ], 200); - - } catch (\Exception $e) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => $e->getMessage(), - ], 500); - } - - } - - // public function getLaserRequestChar(Request $request) - // { - - // $expectedUser = env('API_AUTH_USER'); - // $expectedPw = env('API_AUTH_PW'); - - // if ($request->header('Authorization') != 'Bearer '.$expectedUser.':'.$expectedPw) { - // return response('ERROR: Unauthorized', 403); - // } - - // $userName = $request->header('user-name'); - // $plantCode = $request->header('plant-code'); - // $workCenter = $request->header('work-center'); - // $machineName = $request->header('machine-name'); - // $jobNo = $request->header('aufnr'); - // $itemCode = $request->header('item-code'); - - // if (! $userName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "username can't be empty!", - // ], 404); - // } elseif (! $plantCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "plant code can't be empty!", - // ], 404); - // } elseif (! $itemCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "item code can't be empty!", - // ], 404); - // } elseif (! $workCenter) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "work center can't be empty!", - // ], 404); - // } elseif (! $jobNo) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Job number can't be empty!", - // ], 404); - // } elseif (! $machineName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "machine name can't be empty!", - // ], 404); - // } - - // if ($userName == 'jothi') { - // $userName = 'Admin'; - // } - - // if ($plantCode == null || $plantCode == '' || ! $plantCode) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Plant code can't be empty!", - // ], 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!', - // ], 404); - // } - - // $plant = Plant::where('code', $plantCode)->first(); - // if (! $plant) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "plant code '$plantCode' not found", - // ], 404); - // } - - // $plantId = $plant->id; - - // $user = User::where('name', $userName)->first(); - - // $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); - - // if (! $user) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name '{$userName}' not found!", - // ], 404); - // } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "User name '{$userName}' not found for the plant code '{$plantCode}'!", - // ], 404); - // } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'User does not have rights!', - // ], 404); - // } - - // $machineWork = Machine::where('work_center', $workCenter)->first(); - - // if (! $machineWork) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'work center not found', - // ], 404); - // } - - // $machine = Machine::where('work_center', $workCenter) - // ->where('plant_id', $plantId) - // ->first(); - - // if (! $machine) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "work center not found against plant code '$plantCode'", - // ], 404); - // } - - // $iCode = Item::where('code', $itemCode)->first(); - - // if (! $iCode) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "Item code '$itemCode' not found", - // ], 404); - // } - - // $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); - - // if (! $item) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "Item code not found against plant code '$plantCode'", - // ], 404); - // } - - // $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); - - // if (! $mName) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Machine name '$machineName' not found", - // ], 404); - // } - - // $maNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); - - // if (! $maNameAgaPlant) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "Machine name not found against plant code '$plantCode'", - // ], 404); - // } - - // $job = RequestCharacteristic::where('aufnr', $jobNo)->first(); - - // if (! $job) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Job number '$jobNo' not found", - // ], 404); - // } - - // $jobAgaPlant = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId)->first(); - - // if (! $jobAgaPlant) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "Job number '$jobNo' not found against plant code '$plantCode'", - // ], 404); - // } - - // $jobAgaPlantMachine = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId) - // ->where('machine_id', $machine->id)->where('item_id', $item->id)->first(); - - // if (! $jobAgaPlantMachine) { - // return response()->json(['status_code' => 'ERROR', - // 'status_description' => "Records not found for the given plant code '$plantCode' and item code '$itemCode' and work center '$workCenter' and '$jobNo'", - // ], 404); - // } - - // $records = RequestCharacteristic::with('approver')->where([ - // 'plant_id' => $plantId, - // 'machine_id' => $machine->id, - // 'item_id' => $item->id, - // 'aufnr' => $jobNo, - // ]) - // ->orderBy('id') - // ->get(); - - // $response = []; - - // foreach ($records as $row) { - // $workFlowStatus = ( - // ($row->approver_status1 == 'Approved') || ($row->approver_status1 == 'Rejected') || - // ($row->approver_status2 == 'Approved') || ($row->approver_status2 == 'Rejected') || - // ($row->approver_status3 == 'Approved') || ($row->approver_status3 == 'Rejected') - // ) ? '0' : '1'; - - // $response[] = [ - // 'characteristic_name' => strtoupper($row->characteristic_name) ?? '', - // 'current_value' => $row->current_value ?? '', - // 'update_value' => $row->update_value ?? '', - // 'work_flow_id' => (string) $row->work_flow_id, - // 'work_flow_status' => $workFlowStatus, - // // 'approver_name_1' => $row->approver_name1 ?? '', - // 'approver_name_1' => $row->approver?->name1 ?? '', - // 'approver_status_1' => $row->approver_status1 ?? '', - // 'approver_remark_1' => $row->approver_remark1 ?? '', - // 'approved_at_1' => $row->approved1_at ?? '', // ? $row->approved1_at->format('d-m-Y H:i:s') : '', - // 'approver_name_2' => $row->approver?->name2 ?? '', - // 'approver_status_2' => $row->approver_status2 ?? '', - // 'approver_remark_2' => $row->approver_remark2 ?? '', - // 'approved_at_2' => $row->approved2_at ?? '', // ? $row->approved2_at->format('d-m-Y H:i:s') : '', - // 'approver_name_3' => $row->approver?->name3 ?? '', - // 'approver_status_3' => $row->approver_status3 ?? '', - // 'approver_remark_3' => $row->approver_remark3 ?? '', - // 'approved_at_3' => $row->approved3_at ?? '', // ? $row->approved3_at->format('d-m-Y H:i:s') : '', - // 'requested_by' => ($row->created_by == 'Admin') ? 'jothi' : $row->created_by ?? '', - // 'requested_at' => $row->created_at ? $row->created_at->format('Y-m-d H:i:s') : '', - // ]; - // } - - // return response()->json([ - // 'requested_characteristics' => $response, - // ], 200); - // } - - public function getLaserRequestChar(Request $request) - { - - $expectedUser = env('API_AUTH_USER'); - $expectedPw = env('API_AUTH_PW'); - - if ($request->header('Authorization') != 'Bearer '.$expectedUser.':'.$expectedPw) { - return response('ERROR: Unauthorized', 403); - } - - $userName = $request->header('user-name'); - $plantCode = $request->header('plant-code'); - $workCenter = $request->header('work-center'); - $machineName = $request->header('machine-name'); - $jobNo = $request->header('aufnr'); - $itemCode = $request->header('item-code'); - $workFlowId = $request->header('work-flow-id'); - - if (! $userName) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "username can't be empty!", - ], 404); - } elseif (! $plantCode) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "plant code can't be empty!", - ], 404); - } elseif (! $itemCode) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "item code can't be empty!", - ], 404); - } elseif (! $workCenter) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "work center can't be empty!", - ], 404); - } elseif (! $jobNo) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Job number can't be empty!", - ], 404); - } elseif (! $machineName) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "machine name can't be empty!", - ], 404); - } - - if ($userName == 'jothi') { - $userName = 'Admin'; - } - - if ($plantCode == null || $plantCode == '' || ! $plantCode) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Plant code can't be empty!", - ], 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!', + 'status_description' => 'Unknown request type found!', ], 404); } $plant = Plant::where('code', $plantCode)->first(); if (! $plant) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "plant code '$plantCode' not found", + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found!", ], 404); } @@ -3670,14 +3232,16 @@ class CharacteristicsController extends Controller $user = User::where('name', $userName)->first(); - $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); - if (! $user) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$userName}' not found!", ], 404); - } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + } + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $userPlant && ! $user->hasRole('Super Admin')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$userName}' not found for the plant code '{$plantCode}'!", @@ -3689,146 +3253,352 @@ class CharacteristicsController extends Controller ], 404); } - $machineWork = Machine::where('work_center', $workCenter)->first(); - - if (! $machineWork) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => 'work center not found', - ], 404); - } - - $machine = Machine::where('work_center', $workCenter) - ->where('plant_id', $plantId) - ->first(); - + $machine = Machine::where('work_center', $workCenter)->first(); if (! $machine) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "work center not found against plant code '$plantCode'", + 'status_description' => "Work center '{$workCenter}' not found in master!", ], 404); } - $iCode = Item::where('code', $itemCode)->first(); + $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 code '{$plantCode}' in master!", + ], 404); + } + $MachineId = $machine->id; + + $iCode = Item::where('code', $itemCode)->first(); if (! $iCode) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "Item code '$itemCode' not found", + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found!", ], 404); } $item = Item::where('code', $itemCode)->where('plant_id', $plantId)->first(); - if (! $item) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "Item code not found against plant code '$plantCode'", + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", + ], 404); + } + + $ItemId = $item->id; + + $pCode = CharacteristicApproverMaster::where('plant_id', $plantId)->first(); + if (! $pCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in approver master!", + ], 404); + } + + $wCenter = CharacteristicApproverMaster::where('machine_id', $MachineId)->first(); + if (! $wCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found in approver master!", + ], 404); + } + + $wCenterAgaPlant = CharacteristicApproverMaster::where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $wCenterAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", ], 404); } $mName = CharacteristicApproverMaster::where('machine_name', $machineName)->first(); - if (! $mName) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Machine name '$machineName' not found", + 'status_description' => "Machine name '{$machineName}' not found in approver master!", ], 404); } - $maNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); + $mNameAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('plant_id', $plantId)->first(); + if (! $mNameAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } - if (! $maNameAgaPlant) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "Machine name not found against plant code '$plantCode'", + $cField = CharacteristicApproverMaster::where('characteristic_field', $charField)->first(); + if (! $cField) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found in approver master!", + ], 404); + } + + $cFieldAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('plant_id', $plantId)->first(); + if (! $cFieldAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $mNameAndWorkCenter = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->first(); + if (! $mNameAndWorkCenter) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found in approver master!", + ], 404); + } + + $mNameAndWorkCenterAgaPlant = CharacteristicApproverMaster::where('machine_name', $machineName)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $mNameAndWorkCenterAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Machine name '{$machineName}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $cFieldAndWorkCent = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->first(); + if (! $cFieldAndWorkCent) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the work center '{$workCenter}' in approver master!", + ], 404); + } + + $cFieldAndWorkCentAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('plant_id', $plantId)->first(); + if (! $cFieldAndWorkCentAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' and work center '{$workCenter}' not found for the plant code '{$plantCode}' in approver master!", + ], 404); + } + + $cFieldAndMach = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_name', $machineName)->first(); + if (! $cFieldAndMach) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}' not found for the machine name '{$machineName}' in approver master!", + ], 404); + } + + $cFieldAndWorkCentAndMachAgaPlant = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->first(); + if (! $cFieldAndWorkCentAndMachAgaPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', and machine name '{$machineName}' not found for the plant code '{$plantCode}' in approver master!", ], 404); } $job = RequestCharacteristic::where('aufnr', $jobNo)->first(); - if (! $job) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Job number '$jobNo' not found", + 'status_description' => "Job number '{$jobNo}' not found!", ], 404); } $jobAgaPlant = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId)->first(); - if (! $jobAgaPlant) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "Job number '$jobNo' not found against plant code '$plantCode'", + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' not found for the plant code '{$plantCode}'!", ], 404); } - $jobAgaPlantMachine = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId) - ->where('machine_id', $machine->id)->where('item_id', $item->id)->first(); - + $jobAgaPlantMachine = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId)->where('item_id', $ItemId)->first(); if (! $jobAgaPlantMachine) { - return response()->json(['status_code' => 'ERROR', - 'status_description' => "Records not found for the given plant code '$plantCode' and item code '$itemCode' and work center '$workCenter' and '$jobNo'", + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '{$jobNo}' and item code '{$itemCode}' records not found for the plant code '{$plantCode}'!", ], 404); } - $workFlowId = strtoupper($workFlowId); + $jobAgaPlantMachine = RequestCharacteristic::where('aufnr', $jobNo)->where('plant_id', $plantId)->where('item_id', $ItemId)->where('machine_id', $MachineId)->first(); + if (! $jobAgaPlantMachine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Job number '$jobNo', item code '$itemCode' and work center '$workCenter' records not found for the plant code '{$plantCode}'", + ], 404); + } + + $reqCharExist = RequestCharacteristic::where('aufnr', $jobNo) + ->where('plant_id', $plantId) + ->where('item_id', $ItemId) + ->where('machine_id', $MachineId) + ->whereHas('approver', function ($query) use ($requestType) { + $query->where('approver_type', $requestType); + }) + ->with('approver') // Optional: eager load for later use + ->first(); + + if ($requestType == 'Characteristic') { + $appTypExist = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->where('approver_type', 'Characteristic')->first(); + if (! $appTypExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', machine name '{$machineName}', and plant code '{$plantCode}' not found for the approver type 'Characteristic' in approver master!", + ], 404); + } + + if (! $reqCharExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Request characteristic approvals not found for the Job number '{$jobNo}', item code '{$itemCode}', work center '{$workCenter}' and plant code '{$plantCode}'!", + ], 404); + } + + if ($workFlowId) { + $charWfIdExist = RequestCharacteristic::where('work_flow_id', $workFlowId)->first(); + if (! $charWfIdExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Characteristic approval 'Work Flow ID' not found in database!", + ], 404); + } + + $charWfIdExist = RequestCharacteristic::where('work_flow_id', $workFlowId) + ->whereHas('approver', function ($query) { + $query->where('approver_type', 'Characteristic'); + }) + ->first(); + if (! $charWfIdExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Quality approval Work Flow ID : '{$workFlowId}' found!", + ], 404); + } + } + } else { + $appTypExist = CharacteristicApproverMaster::where('characteristic_field', $charField)->where('machine_id', $MachineId)->where('machine_name', $machineName)->where('plant_id', $plantId)->where('approver_type', 'Quality')->first(); + if (! $appTypExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Master characteristic field value '{$charField}', work center '{$workCenter}', machine name '{$machineName}', and plant code '{$plantCode}' not found for the approver type 'Quality' in approver master!", + ], 404); + } + + if (! $reqCharExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Request quality approvals not found for the Job number '{$jobNo}', item code '{$itemCode}', work center '{$workCenter}' and plant code '{$plantCode}'!", + ], 404); + } + + if ($workFlowId) { + $qualWfIdExist = RequestCharacteristic::where('work_flow_id', $workFlowId)->first(); + if (! $qualWfIdExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Quality approval 'Work Flow ID' not found in database!", + ], 404); + } + + $qualWfIdExist = RequestCharacteristic::where('work_flow_id', $workFlowId) + ->whereHas('approver', function ($query) { + $query->where('approver_type', 'Quality'); + }) + ->first(); + if (! $qualWfIdExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Characteristic approval Work Flow ID : '{$workFlowId}' found!", + ], 404); + } + } + } if (! $workFlowId) { - - $records = RequestCharacteristic::with('approver')->where([ - 'plant_id' => $plantId, - 'machine_id' => $machine->id, - 'item_id' => $item->id, - 'aufnr' => $jobNo, - ]) - ->orderBy('id') - ->get(); + $records = RequestCharacteristic::with('approver')->where('aufnr', $jobNo)->where('plant_id', $plantId)->where('item_id', $ItemId)->where('machine_id', $MachineId)->whereHas('approver', function ($query) use ($requestType) { + $query->where('approver_type', $requestType); + })->orderBy('id')->get(); $response = []; foreach ($records as $row) { - $workFlowStatus = ( - ($row->approver_status1 == 'Approved') || ($row->approver_status1 == 'Rejected') || - ($row->approver_status2 == 'Approved') || ($row->approver_status2 == 'Rejected') || - ($row->approver_status3 == 'Approved') || ($row->approver_status3 == 'Rejected') - ) ? '0' : '1'; + if ($requestType == 'Characteristic') { + $workFlowStatus = ( + ($row->approver_status1 == 'Approved') || ($row->approver_status2 == 'Approved') || ($row->approver_status3 == 'Approved') || ($row->approver_status1 == 'Rejected') || ($row->approver_status2 == 'Rejected') || ($row->approver_status3 == 'Rejected') + ) ? '0' : '1'; - $response[] = [ - 'characteristic_name' => strtoupper($row->characteristic_name) ?? '', - 'current_value' => $row->current_value ?? '', - 'update_value' => $row->update_value ?? '', - 'work_flow_id' => (string) $row->work_flow_id, - 'work_flow_status' => $workFlowStatus, - // 'approver_name_1' => $row->approver_name1 ?? '', - 'approver_name_1' => $row->approver?->name1 ?? '', - 'approver_status_1' => $row->approver_status1 ?? '', - 'approver_remark_1' => $row->approver_remark1 ?? '', - 'approved_at_1' => $row->approved1_at ?? '', // ? $row->approved1_at->format('d-m-Y H:i:s') : '', - 'approver_name_2' => $row->approver?->name2 ?? '', - 'approver_status_2' => $row->approver_status2 ?? '', - 'approver_remark_2' => $row->approver_remark2 ?? '', - 'approved_at_2' => $row->approved2_at ?? '', // ? $row->approved2_at->format('d-m-Y H:i:s') : '', - 'approver_name_3' => $row->approver?->name3 ?? '', - 'approver_status_3' => $row->approver_status3 ?? '', - 'approver_remark_3' => $row->approver_remark3 ?? '', - 'approved_at_3' => $row->approved3_at ?? '', // ? $row->approved3_at->format('d-m-Y H:i:s') : '', - 'requested_by' => ($row->created_by == 'Admin') ? 'jothi' : $row->created_by ?? '', - 'requested_at' => $row->created_at ? $row->created_at->format('Y-m-d H:i:s') : '', - ]; + // if ($row->approver?->approver_type != 'Characteristic' || ! $row->characteristic_name) { + // continue; + // } + + $response[] = [ + 'work_flow_id' => (string) $row->work_flow_id, + 'work_flow_status' => $workFlowStatus, + 'characteristic_name' => strtoupper($row->characteristic_name) ?? '', + 'current_value' => $row->current_value ?? '', + 'update_value' => $row->update_value ?? '', + // 'approver_name_1' => $row->approver_name1 ?? '', + 'approver_name_1' => $row->approver?->name1 ?? '', + 'approver_status_1' => $row->approver_status1 ?? '', + 'approver_remark_1' => $row->approver_remark1 ?? '', + 'approved_at_1' => $row->approved1_at ?? '', // ? $row->approved1_at->format('d-m-Y H:i:s') : '', + 'approver_name_2' => $row->approver?->name2 ?? '', + 'approver_status_2' => $row->approver_status2 ?? '', + 'approver_remark_2' => $row->approver_remark2 ?? '', + 'approved_at_2' => $row->approved2_at ?? '', // ? $row->approved2_at->format('d-m-Y H:i:s') : '', + 'approver_name_3' => $row->approver?->name3 ?? '', + 'approver_status_3' => $row->approver_status3 ?? '', + 'approver_remark_3' => $row->approver_remark3 ?? '', + 'approved_at_3' => $row->approved3_at ?? '', // ? $row->approved3_at->format('d-m-Y H:i:s') : '', + 'requested_by' => ($row->created_by == 'Admin') ? 'jothi' : $row->created_by ?? '', + 'requested_at' => $row->created_at ? $row->created_at->format('Y-m-d H:i:s') : '', + ]; + } else { + $workFlowStatus = ($row->approver_status1 == 'Approved' || $row->approver_status2 == 'Approved' || $row->approver_status3 == 'Approved') ? '0' : '1'; + + // if ($row->approver?->approver_type != 'Quality' || $row->characteristic_name) { + // continue; + // } + + $response[] = [ + 'work_flow_id' => (string) $row->work_flow_id, + 'work_flow_status' => $workFlowStatus, + // 'approver_name_1' => $row->approver_name1 ?? '', + 'approver_name_1' => $row->approver?->name1 ?? '', + 'approver_status_1' => $row->approver_status1 ?? '', + 'approver_remark_1' => $row->approver_remark1 ?? '', + 'approved_at_1' => $row->approved1_at ?? '', // ? $row->approved1_at->format('d-m-Y H:i:s') : '', + 'approver_name_2' => $row->approver?->name2 ?? '', + 'approver_status_2' => $row->approver_status2 ?? '', + 'approver_remark_2' => $row->approver_remark2 ?? '', + 'approved_at_2' => $row->approved2_at ?? '', // ? $row->approved2_at->format('d-m-Y H:i:s') : '', + 'approver_name_3' => $row->approver?->name3 ?? '', + 'approver_status_3' => $row->approver_status3 ?? '', + 'approver_remark_3' => $row->approver_remark3 ?? '', + 'approved_at_3' => $row->approved3_at ?? '', // ? $row->approved3_at->format('d-m-Y H:i:s') : '', + 'requested_by' => ($row->created_by == 'Admin') ? 'jothi' : $row->created_by ?? '', + 'requested_at' => $row->created_at ? $row->created_at->format('Y-m-d H:i:s') : '', + ]; + } } return response()->json([ 'requested_characteristics' => $response, ], 200); } else { - $workflowId = $request->header('work-flow-id'); + $filePath = ''; + $filename = ''; + if ($requestType == 'Characteristic') { + $filePath = "uploads/LaserDocs/{$workFlowId}.pdf"; + $filename = "{$workFlowId}.pdf"; + } else { + $filePath = "uploads/LaserDocs/{$workFlowId}.png"; + $filename = "{$workFlowId}.png"; + } - $filePath = "uploads/LaserDocs/{$workflowId}.pdf"; - $filename = "{$workflowId}.pdf"; + $workFlowIdExist = RequestCharacteristic::where('work_flow_id', $workFlowId)->first(); - $workFlowId = RequestCharacteristic::where('work_flow_id', $workflowId)->first(); - - if (! $workFlowId) { + if (! $workFlowIdExist) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'work flow id number not found in database!', + 'status_description' => "Work Flow ID '{$workFlowId}' not found in database!", ], 404); } @@ -3839,7 +3609,7 @@ class CharacteristicsController extends Controller if (! Storage::disk('local')->exists($filePath)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pdf File not found for the provided 'Work Flow ID' number!", + 'status_description' => ($requestType == 'Characteristic') ? "Characteristic approval document (.PDF) not found for the Work Flow ID '{$workFlowId}'!" : "Quality approval document (.PNG) not found for the Work Flow ID '{$workFlowId}'!", ], 404); }