From aa7d1802c8c5d3de69a45c5a8b0ad784807cdb6b Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 24 Jan 2026 11:15:50 +0530 Subject: [PATCH] Added characteristics controller --- .../Controllers/CharacteristicsController.php | 1236 ++++++++++++++++- 1 file changed, 1228 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/CharacteristicsController.php b/app/Http/Controllers/CharacteristicsController.php index 1570cfb..81fadc3 100644 --- a/app/Http/Controllers/CharacteristicsController.php +++ b/app/Http/Controllers/CharacteristicsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\CharacteristicApproverMaster; use App\Models\CharacteristicValue; use App\Models\ClassCharacteristic; use App\Models\Item; @@ -9,9 +10,12 @@ use App\Models\Line; use App\Models\Machine; use App\Models\Plant; use App\Models\ProductCharacteristicsMaster; +use App\Models\RequestCharacteristic; use App\Models\User; use App\Models\WorkGroupMaster; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Response; +use Illuminate\Support\Facades\Storage; use Schema; use Str; @@ -287,7 +291,7 @@ class CharacteristicsController extends Controller 'status_code' => 'ERROR', 'status_description' => "User name '{$userName}' not found for the plant code '{$plantCode}'!", ], 403); - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', @@ -519,7 +523,7 @@ class CharacteristicsController extends Controller if (! $userPlant && ! $user->hasRole('Super Admin')) { $missingUsersPlant[$field] = $markUser; - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { $missingUsersRight[$field] = $markUser; } } @@ -829,7 +833,7 @@ class CharacteristicsController extends Controller 'status_code' => 'ERROR', 'status_description' => "User '{$userName}' not found for Plant '{$plantCode}'!", ], 403); - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', @@ -965,7 +969,7 @@ class CharacteristicsController extends Controller if (! $userPlant && ! $user->hasRole('Super Admin')) { $missingUsersPlant[$field] = $markUser; - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { $missingUsersRight[$field] = $markUser; } } @@ -1151,7 +1155,7 @@ class CharacteristicsController extends Controller if (! $userPlant && ! $user->hasRole('Super Admin')) { $missingUsersPlant[$field] = $markUser; - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { $missingUsersRight[$field] = $markUser; } } @@ -1400,7 +1404,7 @@ class CharacteristicsController extends Controller 'status_code' => 'ERROR', 'status_description' => 'User Name not found for the plant!', ], 403); - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', @@ -1789,7 +1793,7 @@ class CharacteristicsController extends Controller 'status_code' => 'ERROR', 'status_description' => 'User Name not found for the plant!', ], 403); - } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Design Manager') && ! $user->hasRole('Design Supervisor') && ! $user->hasRole('Design Employee')) { + } elseif (! $user->hasRole(['Super Admin', 'Design Manager', 'Design Supervisor', 'Design Employee'])) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', @@ -2514,8 +2518,1224 @@ class CharacteristicsController extends Controller } /** - * Display the specified resource. + * Display the specified resource from storage. */ + public function test(Request $request) + { + return response('Laser Routing Data Updated.', 200) + ->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 = strtolower($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' => strtolower($char['characteristic_name']), + // '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; + + 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'; + } + + $json = $request->input('data'); + $data = json_decode($json, true); + + $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 = strtolower($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}"; + + $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' => strtolower($char['characteristic_name']), + '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!', + ], 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); + } + + if (! $workFlowId) { + + $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); + } else { + $workflowId = $request->header('work-flow-id'); + + $filePath = "uploads/LaserDocs/{$workflowId}.pdf"; + $filename = "{$workflowId}.pdf"; + + $workFlowId = RequestCharacteristic::where('work_flow_id', $workflowId)->first(); + + if (! $workFlowId) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'work flow id number not found in database!', + ], 404); + } + + if (! str_ends_with(strtolower($filename), '.pdf')) { + $filename .= '.pdf'; + } + + 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!', + ], 404); + } + + $file = Storage::disk('local')->get($filePath); + $mimeType = Storage::disk('local')->mimeType($filePath); + + return Response::make($file, 200, [ + 'Content-Type' => $mimeType, + 'Content-Disposition' => 'inline; filename="'.$filename.'"', + ]); + } + } + public function show(string $id) { //