header('Authorization'); $expectedToken = $expectedUser.':'.$expectedPw; if ('Bearer '.$expectedToken != $header_auth) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', ], 404); } $data = $request->all(); if ($data['plant_code'] == null || $data['plant_code'] == '') { // return response("ERROR: Please provide a valid plant code.", 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", ], 404); } elseif (Str::length($data['plant_code']) < 4 || ! is_numeric($data['plant_code']) || ! preg_match('/^[1-9]\d{3,}$/', $data['plant_code'])) {// !ctype_digit($data['plant_code']) return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid plant code found!', ], 404); } $plant = Plant::where('code', $data['plant_code'])->first(); if (! $plant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Plant not found!', ], 404); } $plantId = $plant->id; if ($data['gr_number'] == null || $data['gr_number'] == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "GR Number can't be empty!", ], 404); } $grExists = GRMaster::where('plant_id', $plantId) ->where('gr_number', $data['gr_number']) ->first(); if (! $grExists) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "GR Number {$data['gr_number']} not found for plant {$data['plant_code']}!", ], 404); } $scannedBy = $data['scanned_by'] ?? null; if ($scannedBy == '' || $scannedBy == null) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Scanned by can't be empty!", ], 404); } $user = User::where('name', $data['scanned_by']) ->first(); if (! $user) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "'{$data['scanned_by']}' user not found!", ], 404); } $itemCodes = array_column($data['item_codes'], 'item_code'); $duplicateItemCodes = array_unique(array_diff_assoc($itemCodes, array_unique($itemCodes))); if (count($duplicateItemCodes) > 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Duplicate item codes found in request!', 'duplicate_item_codes' => array_values($duplicateItemCodes), ], 404); } $allSerials = []; foreach ($data['item_codes'] as $item) { if (! isset($item['serial_numbers']) || ! is_array($item['serial_numbers'])) { continue; } foreach ($item['serial_numbers'] as $serial) { $allSerials[] = $serial; } } $duplicateSerials = array_unique(array_diff_assoc($allSerials, array_unique($allSerials))); if (count($duplicateSerials) > 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Duplicate serial numbers found in request!', 'duplicate_serial_numbers' => array_values($duplicateSerials), ], 404); } $invalidLengthItemCodes = []; foreach ($data['item_codes'] as $item) { $itemCode = $item['item_code'] ?? null; // Skip if item code is missing if (! $itemCode) { continue; } // Check if item code is less than 6 digits or not numeric if (strlen($itemCode) < 6 || ! ctype_digit($itemCode)) { $invalidLengthItemCodes[] = $itemCode; } } if (count($invalidLengthItemCodes) > 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Some item codes are invalid: must be at least 6 digits!', 'invalid_item_codes' => array_values($invalidLengthItemCodes), ], 404); } $invalidItemCodes = []; $invalidPlantItems = []; foreach ($data['item_codes'] as $item) { $itemCode = $item['item_code'] ?? null; if (! $itemCode) { $invalidItemCodes[] = '(missing)'; continue; } $itemObj = Item::where('code', $itemCode)->first(); if (! $itemObj) { $invalidItemCodes[] = $itemCode; continue; } $itemPlant = Item::where('plant_id', $plantId) ->where('code', $itemCode)->first(); if (! $itemPlant) { $invalidPlantItems[] = $itemCode; } } if (count($invalidItemCodes) > 0 || count($invalidPlantItems) > 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Some item codes are invalid!', 'not_found_items' => array_values($invalidItemCodes), 'not_in_plant' => array_values($invalidPlantItems), ], 404); } $missingSerialsByItem = []; foreach ($data['item_codes'] as $item) { $itemCode = $item['item_code']; $serialNumbers = $item['serial_numbers']; // Get the item id $itemObj = Item::where('plant_id', $plantId) ->where('code', $itemCode) ->first(); if (! $itemObj) { continue; } $itemId = $itemObj->id; $foundSerials = GRMaster::where('plant_id', $plantId) ->where('gr_number', $data['gr_number']) ->where('item_id', $itemId) ->whereIn('serial_number', $serialNumbers) ->pluck('serial_number') ->toArray(); $missingSerials = array_diff($serialNumbers, $foundSerials); if (count($missingSerials) > 0) { $missingSerialsByItem[$itemCode] = array_values($missingSerials); } } if (count($missingSerialsByItem) > 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Some serial numbers not found in DB for given plant, GR number, and item!', 'missing_serials' => $missingSerialsByItem, ], 404); } $alreadyCompleted = []; foreach ($data['item_codes'] as $item) { $itemCode = $item['item_code']; $serialNumbers = $item['serial_numbers']; $itemId = Item::where('code', $itemCode)->value('id'); foreach ($serialNumbers as $serial) { $gr = GRMaster::where('plant_id', $plantId) ->where('item_id', $itemId) ->where('gr_number', $data['gr_number']) ->where('serial_number', $serial) ->first(); if (! $gr) { continue; } if ($gr->status == 'Completed') { $alreadyCompleted[] = $serial; } } } if (! empty($alreadyCompleted)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Below serial numbers are already completed', 'serial_numbers' => $alreadyCompleted, ], 404); } // { // $itemCode = $item['item_code']; // $serialNumbers = $item['serial_numbers']; // $itemObj = Item::where('code', $itemCode)->first(); // if (!$itemObj) { // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Item code: $itemCode not found" // ], 404); // } // $itemPlant = Item::where('plant_id', $plantId) // ->where('code', $itemCode)->first(); // if (!$itemPlant) { // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Item code: $itemCode not found for the plant: $data[plant_code]" // ], 404); // } // $itemId = $itemObj->id; // // Update all serial numbers for this item // GRMaster::where('plant_id', $plantId) // ->where('item_id', $itemId) // ->where('gr_number', $data['gr_number']) // ->whereIn('serial_number', $serialNumbers) // ->update(['created_by' => $scannedBy]); // } foreach ($data['item_codes'] as $item) { $itemCode = $item['item_code']; $serialNumbers = $item['serial_numbers']; $itemId = Item::where('code', $itemCode)->value('id'); GRMaster::where('plant_id', $plantId) ->where('item_id', $itemId) ->where('gr_number', $data['gr_number']) ->whereIn('serial_number', $serialNumbers) ->update(['created_by' => $scannedBy, 'status' => 'Completed']); } return response()->json([ 'status_code' => 'SUCCESS', 'status_description' => 'Serial numbers updated successfully!', ], 200); } /** * Store a newly created resource in storage. */ public function getPdf(Request $request) { // Validate input // $request->validate([ // 'filename' => 'required|string', // ]); $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()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', ], 404); } $filename = $request->header('process-order'); if (! $filename) { return response()->json(['error' => 'Missing file-name header'], 404); } $filename = basename($filename); // Ensure the file has .pdf extension if (! str_ends_with(strtolower($filename), '.pdf')) { $filename .= '.pdf'; } $filePath = 'uploads/ProcessOrder/'.$filename; if (! Storage::disk('local')->exists($filePath)) { return response()->json(['error' => 'File not found'], 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 getGRPdf(Request $request) { // Validate input // $request->validate([ // 'filename' => 'required|string', // ]); $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()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', ], 404); } $filename = $request->header('gr-number'); if (! $filename) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Gr Number cannot be empty!', ], 404); // return response()->json(['error' => 'Missing file-name header'], 404); } $filename = basename($filename); // Ensure the file has .pdf extension if (! str_ends_with(strtolower($filename), '.pdf')) { $filename .= '.pdf'; } $filePath = 'uploads/GRNumber/'.$filename; if (! Storage::disk('local')->exists($filePath)) { // return response()->json(['error' => 'File not found'], 404); return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Pdf File not found for the provided GrNumber!', ], 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 getGRSerial(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()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', ], 404); } $plantCode = $request->header('plant-code'); $grNumber = $request->header('gr-number'); if (! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant Code value can't be empty", ], 404); } elseif (! $grNumber) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'GR Number cannot be empty!', ], 404); } $plant = Plant::where('code', $plantCode)->first(); $plantId = $plant->id; if (! $plant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant Code '{$plantCode}' not found!", ], 404); } $grExist = GRMaster::where('gr_number', $grNumber)->first(); if (! $grExist) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'GR Number not found', ], 404); } $grExists = GRMaster::where('plant_id', $plantId) ->where('gr_number', $grNumber) ->first(); if (! $grExists) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'GR Number not found for this plant!', ], 404); } $grRecords = GrMaster::where('plant_id', $plantId) ->where('gr_number', $grNumber) ->get(['serial_number', 'item_id']); if (empty($grRecords)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'No serial numbers found for the given GR number!', ], 404); } // return response()->json([ // 'serial_numbers' => $serialNumbers // ], 200); // $itemId = $grRecords->first()->item_id; // $itemCode = $itemId ? optional(Item::find($itemId))->code : null; // $serialNumbers = $grRecords->pluck('serial_number')->toArray(); // return response()->json([ // 'item_code' => $itemCode, // 'serial_numbers' => $serialNumbers // ], 200); $itemIds = $grRecords->pluck('item_id')->unique()->filter(); $items = Item::whereIn('id', $itemIds)->pluck('code', 'id'); $result = $grRecords->groupBy('item_id')->map(function ($group, $itemId) use ($items) { return [ 'item_code' => $items[$itemId] ?? null, 'serial_numbers' => $group->pluck('serial_number')->toArray(), ]; })->values(); // remove keys return response()->json($result, 200); } public function getProcessOrderData(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()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!', ], 404); } $plantCode = $request->header('plant-code'); $processOrder = $request->header('process-order'); $machineName = $request->header('machine-name'); if (! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant Code value can't be empty", ], 404); } elseif (! $processOrder) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process Order cannot be empty!', ], 404); } $plant = Plant::where('code', $plantCode)->first(); $plantId = $plant->id; if (! $plant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant Code '{$plantCode}' not found!", ], 404); } $processOrderExist = ProcessOrder::where('process_order', $processOrder)->first(); if (! $processOrderExist) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process order not found', ], 404); } $proOrdAgPlant = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->latest() ->first(); if (! $proOrdAgPlant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process order not found for this plant!', ], 404); } $coil = $proOrdAgPlant->coil_number; if($coil != 0){ $proOrdAgPlantMachine = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('machine_name', $machineName) ->first(); if (!$proOrdAgPlantMachine) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Machine name not found in process order!', ], 404); } $item = $proOrdAgPlant->item; $processOrderRecords = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->where('machine_name', $machineName) ->get(); $lastRecord = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->where('machine_name', $machineName) ->orderBy('id', 'desc') ->first(); $lastRecord1 = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->where('machine_name', $machineName) ->where('rework_status', 0) ->orderBy('id', 'desc') ->first(); $totalReceivedQty = $processOrderRecords->sum('received_quantity'); // $lastRecord = $processOrderRecords->first(); $item = $lastRecord->item; if ($totalReceivedQty == $proOrdAgPlant->order_quantity) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' has already reached its order quantity.", ], 404); } $itemId = $lastRecord->item_id; $line = ProcessOrder::where('plant_id', $plantId)->where('process_order', $processOrder)->where('item_id',$itemId)->first(); $lineId = $line->line_id; $lineName = Line::where('plant_id', $plantId)->where('id', $lineId)->value('name'); // $workCenter = Machine::where('plant_id', $plantId)->where('work_center', $machineName)->first(); $currentCoil = ($lastRecord->rework_status == 1) ? (string) $lastRecord1->coil_number : (string) $lastRecord->coil_number; $machine = Machine::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_center', $machineName)->first(); $machineId = $machine->id; if(!$machine){ return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Machine not found in machine table.", ], 404); } // if ($totalReceivedQty > $proOrdAgPlant->order_quantity) { // return responses()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' received quantity is more than its order quantity." // ], 404); // } $okQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('machine_id', $machineId)->where('status', 'Ok')->get()->count(); $notOkQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('machine_id', $machineId)->where('status', 'NotOk')->get()->count(); $currentCoil = ($lastRecord->rework_status == 1) ? (string) $lastRecord1->coil_number : (string) $lastRecord->coil_number; return response()->json([ 'item_code' => $item?->code ?? '', 'description' => $item?->description ?? '', 'line_name' => $lineName ?? '', // 'coil_number' => $proOrdAgPlant->coil_number ?? "", // 'order_quantity' => (string)$proOrdAgPlant->order_quantity ?? "", 'coil_number' => $currentCoil, 'order_quantity' => (string) $lastRecord->updated_order_quantity ?? '0.000', 'ok_quantity' => (string) $okQty ?? '0', 'not_ok_quantity' => (string) $notOkQty ?? '0', 'received_quantity' => (string) (($totalReceivedQty == 0) ? '0.000' : $totalReceivedQty) ?? '0.000', ]); } else{ $proOrdAgPlant = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->first(); if (!$proOrdAgPlant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'process order not found!', ], 404); } $item = $proOrdAgPlant->item; $processOrderRecords = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->get(); $lastRecord = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->orderBy('id', 'desc') ->first(); $lastRecord1 = ProcessOrder::with('item') ->where('plant_id', $plant->id) ->where('process_order', $processOrder) ->where('rework_status', 0) ->orderBy('id', 'desc') ->first(); $totalReceivedQty = $processOrderRecords->sum('received_quantity'); // $lastRecord = $processOrderRecords->first(); $item = $lastRecord->item; if ($totalReceivedQty == $proOrdAgPlant->order_quantity) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' has already reached its order quantity.", ], 404); } $itemId = $lastRecord->item_id; $line = ProcessOrder::where('plant_id', $plantId)->where('process_order', $processOrder)->where('item_id',$itemId)->first(); $lineId = $line->line_id; $lineName = Line::where('plant_id', $plantId)->where('id', $lineId)->value('name'); $currentCoil = ($lastRecord->rework_status == 1) ? (string) $lastRecord1->coil_number : (string) $lastRecord->coil_number; // if ($totalReceivedQty > $proOrdAgPlant->order_quantity) { // return responses()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' received quantity is more than its order quantity." // ], 404); // } $okQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('status', 'Ok')->get()->count(); $notOkQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('status', 'NotOk')->get()->count(); $currentCoil = ($lastRecord->rework_status == 1) ? (string) $lastRecord1->coil_number : (string) $lastRecord->coil_number; return response()->json([ 'item_code' => $item?->code ?? '', 'description' => $item?->description ?? '', 'line_name' => $lineName ?? '', // 'coil_number' => $proOrdAgPlant->coil_number ?? "", // 'order_quantity' => (string)$proOrdAgPlant->order_quantity ?? "", 'coil_number' => $currentCoil, 'order_quantity' => (string) $lastRecord->updated_order_quantity ?? '0.000', 'ok_quantity' => (string) $okQty ?? '0', 'not_ok_quantity' => (string) $notOkQty ?? '0', 'received_quantity' => (string) (($totalReceivedQty == 0) ? '0.000' : $totalReceivedQty) ?? '0.000', ]); } } public function storeProcessOrderData(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!', ], 404); } $plantCode = $request->header('plant-code'); $processOrder = $request->header('process-order'); $data = $request->all(); Log::info('Process Order POST API >>', ['plant-code' => $plantCode, 'process-order' => $processOrder, 'post-data' => $data]); 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); } if (! $processOrder) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Process-order are required!', ], 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; $itemCode = $data['item_code'] ?? ''; // $lineName = $data['line_name'] ?? ''; $coilNo = $data['coil_number'] ?? ''; $updatedOrderQty = $data['order_quantity'] ?? 0; $receivedQty = $data['received_quantity'] ?? 0; $scrapQty = $data['scrap_quantity'] ?? 0; $sfgNo = $data['sfg_number'] ?? ''; $machineId = $data['machine_id'] ?? ''; $rework = $data['rework'] ?? ''; $operationNo = $data['operation_number'] ?? ''; $createdBy = $data['created_by'] ?? ''; if ($itemCode == null || $itemCode == '' || ! $itemCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item code can't be empty!", ], 404); } elseif (Str::length($itemCode) < 6) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Item code should contain minimum 6 digits!', ], 404); } elseif (! ctype_alnum($itemCode)) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Item code should contain only alpha-numeric values!', ], 404); } $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); if(!$item){ return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item Code not found!", ], 404); } if($item){ $lineIdExist = $item->line_id ?? ''; if ($lineIdExist) { $line = Line::where('id', $lineIdExist)->where('plant_id', $plantId)->first(); if ($line) { $lineId = $line->id; } else { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Line not found!", ], 404); } } else { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Line not mapped for item code '$itemCode'!", ], 404); } } if ($scrapQty == null || $scrapQty == '') { $scrapQty = 0; } if ($rework == null || $rework == '' || ! $rework) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework value can't be empty!", ], 404); } if ($coilNo == null || $coilNo == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Coil number can't be empty!", ], 404); } elseif (! is_numeric($coilNo) || Str::length($coilNo) <= 0 || ! preg_match('/^\d+(\.\d+)?$/', $coilNo) ) { // !ctype_digit($data['plant_code']) return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid coil number found!', ], 404); } if ($operationNo == null || $operationNo == '' || ! $operationNo) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Operation Number can't be empty!", ], 404); } if ($createdBy == null || $createdBy == '' || ! $createdBy) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name can't be empty!", ], 404); } elseif ($createdBy == 'jothi') { $createdBy = 'Admin'; } $user = User::where('name', $createdBy)->first(); $userPlant = User::where('name', $createdBy)->where('plant_id', $plantId)->first(); if (! $user) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$createdBy}' not found!", ], 404); } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "User name '{$createdBy}' not found for the plant code '{$plantCode}'!", ], 404); } elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Process Employee') && ! $user->hasRole('Process Supervisor')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'User does not have rights!', ], 404); } $item = Item::where('code', $itemCode)->first(); if (! $item) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item code '{$itemCode}' not found!", ], 404); } $itemPlant = Item::where('code', $itemCode) ->where('plant_id', $plantId) ->first(); if (! $itemPlant) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!", ], 404); } $itemId = $itemPlant->id; if ($sfgNo != null && $sfgNo != '' && Str::length($sfgNo) > 0 && $sfgNo) { $existing = ProcessOrder::where('plant_id', $plantId) ->where('sfg_number', $sfgNo) ->first(); if ($existing) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "SFG number '{$sfgNo}' already exist for the plant code '{$plantCode}'!", ], 404); } } else { $sfgNo = null; } $existing = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('item_id', '!=', $itemId) ->first(); if ($existing) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Process order '{$processOrder}' already has item_code '{$existing->item->code}' for the plant code '{$plantCode}'!", ], 404); } $alreadyReceived = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('machine_name', $machineId) ->where('item_id', $itemId) ->sum('received_quantity'); $orderQty = 0; if ($updatedOrderQty == 0) { $orderExist = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('item_id', $itemId)->latest()->first(); // ->value('order_quantity') ?? 0; if ($orderExist) { $orderQty = $orderExist->order_quantity ?? 0; $updatedOrderQty = $orderExist->updated_order_quantity ?? 0; } else { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Order quantity can't be '0'", ], 404); } } else { $orderExist = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('item_id', $itemId)->latest()->first(); // ->value('order_quantity') ?? 0; if ($orderExist) { // $existOrderQty = $orderExist->updated_order_quantity ?? 0; $orderQty = $orderExist->order_quantity ?? 0; $existUpdatedOrdQuan = $orderExist->updated_order_quantity ?? 0; if ($existUpdatedOrdQuan != $updatedOrderQty) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Order quantity doesn't equal to exist process order '{$processOrder}'", ], 404); } } } $total = $alreadyReceived + $receivedQty; if ($rework != 'Yes' && $rework != 'No') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework value should be either 'Yes' or 'No'!", ], 404); } elseif ($rework == 'No') { if ($scrapQty != 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Scrap Quanity value should be '0'!", ], 404); } } $coilNo = (string) $coilNo; try { if ($rework == 'Yes') { if ($coilNo && Str::contains($coilNo, '.')) { // Contains dot $coilMain = explode('.', $coilNo)[0]; // 333 $existingCoil = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('line_id', $lineId) ->where('coil_number', $coilMain) ->first(); if (! $existingCoil) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Coil number '{$coilMain}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", ], 404); } elseif ($existingCoil->rework_status == 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework coil number '{$coilMain}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", ], 404); } if ($total > $updatedOrderQty) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework received quantity should not exceed exist order quantity! Exist Order Qty = '{$updatedOrderQty}', Already Exist Total Received Qty = '{$alreadyReceived}', Trying to Insert Rework Received Qty = '{$receivedQty}'", ], 404); } $existingCoil = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('line_id', $lineId) ->where('coil_number', $coilNo) ->first(); if (! $existingCoil) { ProcessOrder::Create( [ 'plant_id' => $plantId, 'line_id' => $lineId, 'process_order' => $processOrder, 'item_id' => $itemId, 'coil_number' => $coilNo, 'order_quantity' => $orderQty, 'updated_order_quantity' => $updatedOrderQty, 'received_quantity' => $receivedQty, 'scrap_quantity' => $scrapQty, 'sfg_number' => $sfgNo, 'machine_name' => $machineId, 'operation_number' => $operationNo, 'rework_status' => 1, 'created_by' => $createdBy, ] ); return response()->json([ 'status_code' => 'SUCCESS', 'status_description' => 'Record Updated Successfully (Rework).', ]); } else { $updated = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('line_id', $lineId) ->where('coil_number', $coilNo) ->update([ // 'order_quantity' => $orderQty, 'received_quantity' => $receivedQty, 'scrap_quantity' => $scrapQty, 'operation_number' => $operationNo, // 'sfg_number' => $sfgNo, // 'machine_name' => $machineId, 'rework_status' => 1, 'updated_by' => $createdBy, 'updated_at' => now(), ]); } } else { // if ($coilNo > 1) { // $prevCoilNo = (string) ($coilNo - 1); // $existPrevCoil = ProcessOrder::where('plant_id', $plantId) // ->where('process_order', $processOrder) // ->where('coil_number', $prevCoilNo) // ->first(); // if (! $existPrevCoil) { // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Previous coil number '{$prevCoilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", // ], 404); // } // } $existingCoil = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('line_id', $lineId) ->where('coil_number', $coilNo) ->first(); if (! $existingCoil) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Coil number '{$coilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", ], 404); } $existPrevCoil = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('item_id', $itemId) ->where('coil_number', $coilNo) ->first(); if ($existPrevCoil->received_quantity < $receivedQty) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework received quantity should not exceed exist received quantity! Exist Received Qty = '{$existPrevCoil->received_quantity}', Trying to Insert Rework Received Qty = '{$receivedQty}'", ], 404); } $updated = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('line_id', $lineId) ->where('coil_number', $coilNo) ->update([ // 'order_quantity' => $orderQty, 'received_quantity' => $receivedQty, 'scrap_quantity' => $scrapQty, 'operation_number' => $operationNo, // 'sfg_number' => $sfgNo, // 'machine_name' => $machineId, 'rework_status' => 1, 'updated_by' => $createdBy, 'updated_at' => now(), ]); } if ($updated == 0) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "No matching record found for rework coil number '{$coilNo}'!", ], 404); } else { return response()->json([ 'status_code' => 'SUCCESS', 'status_description' => 'Record Updated Successfully (Rework).', ]); } } else { if ($coilNo && Str::contains($coilNo, '.')) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Rework value should be 'Yes' for rework coil number '{$coilNo}'!", ], 404); } $existing = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->where('coil_number', $coilNo) ->first(); if ($existing) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Coil number '{$coilNo}' already exist against plant code '{$plantCode}' and process order '{$processOrder}'!", ], 404); } // if ($coilNo > 1) { // $prevCoilNo = (string) ($coilNo - 1); // $existPrevCoil = ProcessOrder::where('plant_id', $plantId) // ->where('process_order', $processOrder) // ->where('coil_number', $prevCoilNo) // ->first(); // if (! $existPrevCoil) { // return response()->json([ // 'status_code' => 'ERROR', // 'status_description' => "Previous coil number '{$prevCoilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", // ], 404); // } // } if ($total > $updatedOrderQty) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Received quantity should not exceed exist order quantity! Exist Order Qty = '{$updatedOrderQty}', Already Exist Total Received Qty = '{$alreadyReceived}', Trying to Insert Received Qty = '{$receivedQty}'", ], 404); } ProcessOrder::Create( [ 'plant_id' => $plantId, 'line_id' => $lineId, 'process_order' => $processOrder, 'item_id' => $itemId, 'coil_number' => $coilNo, 'order_quantity' => $orderQty, 'updated_order_quantity' => $updatedOrderQty, 'received_quantity' => $receivedQty, 'scrap_quantity' => $scrapQty, 'sfg_number' => $sfgNo, 'machine_name' => $machineId, 'operation_number' => $operationNo, 'rework_status' => 0, 'created_by' => $createdBy, ] ); return response()->json([ 'status_code' => 'SUCCESS', 'status_description' => 'Record Inserted Successfully.', ]); } } catch (\Exception $e) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => $e->getMessage(), ], 500); } } /** * Display the specified resource. */ public function show(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } // public function generate(string $stickerId) // { // $stickerId = trim($stickerId); // $elements = StickerStructureDetail::where('sticker_id', $stickerId) // ->first(); // $pdfService = new StickerPdfService(); // return $pdfService->generate($stickerId, $elements->toArray()); // } public function generate(Request $request, $stickerId) { $plantId = $request->query('plant_id'); $itemCharacteristicId = $request->query('item_characteristic_id'); $stickerId = trim($stickerId); //dd($plantId,$itemCharacteristicId,$stickerId); // Normalize "empty" or "" to null $plantId = ($plantId && $plantId != 'empty') ? $plantId : null; $itemCharacteristicId = ($itemCharacteristicId && $itemCharacteristicId != 'empty') ? $itemCharacteristicId : null; if ($plantId && $itemCharacteristicId) { $plantId = $request->query('plant_id'); $itemCharacteristicId = $request->query('item_characteristic_id'); $sticId = StickerStructureDetail::where('sticker_id', $stickerId)->first(); $sId = $sticId->id; $dynamicElements = StickerDetail::where('sticker_structure_detail_id', $sId) ->where('element_type', 'Dynamic') ->get(); $itemCharacteristic = null; if ($plantId && $itemCharacteristicId) { $itemCharacteristic = ItemCharacteristic::where('plant_id', $plantId) ->where('id', $itemCharacteristicId) ->first(); } $pdfService = new StickerPdfService(); return $pdfService->generate1( $stickerId, $dynamicElements, $itemCharacteristic ); } else { $elements = StickerStructureDetail::where('sticker_id', $stickerId) ->first(); $pdfService = new StickerPdfService(); return $pdfService->generate($stickerId, $elements->toArray()); } } // public function generatePdf(Request $request, $stickerId) // { // $plantId = $request->query('plant_id'); // $itemCharacteristicId = $request->query('item_characteristic_id'); // $stickerId = trim($stickerId); // //dd($plantId,$itemCharacteristicId,$stickerId); // // Normalize "empty" or "" to null // $plantId = ($plantId && $plantId != 'empty') ? $plantId : null; // $itemCharacteristicId = ($itemCharacteristicId && $itemCharacteristicId != 'empty') ? $itemCharacteristicId : null; // if ($plantId && $itemCharacteristicId) // { // $plantId = $request->query('plant_id'); // $itemCharacteristicId = $request->query('item_characteristic_id'); // $sticId = StickerStructureDetail::where('id', $stickerId)->first(); // $sId = $sticId->id; // $stickerId = $sticId->sticker_id; // $dynamicElements = StickerDetail::where('sticker_structure_detail_id', $sId) // ->where('element_type', 'Dynamic') // ->get(); // $itemCharacteristic = null; // if ($plantId && $itemCharacteristicId) { // $itemCharacteristic = ItemCharacteristic::where('plant_id', $plantId) // ->where('id', $itemCharacteristicId) // ->first(); // } // $pdfService = new StickerPdfService(); // return $pdfService->generatePdf1( // $stickerId, // $dynamicElements, // $itemCharacteristic // ); // } // else // { // $elements = StickerStructureDetail::where('sticker_id', $stickerId) // ->first(); // $pdfService = new StickerPdfService(); // return $pdfService->generate($stickerId, $elements->toArray()); // } // } }