From 9774cda2ad16e48b027ca89f6907063302585f1d Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 26 Sep 2025 11:49:31 +0530 Subject: [PATCH] Refactor readFiles method to enhance CSV row validation and ensure unique remaining rows are collected --- app/Http/Controllers/SapFileController.php | 53 ++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/SapFileController.php b/app/Http/Controllers/SapFileController.php index b6113b48c..36c447e73 100644 --- a/app/Http/Controllers/SapFileController.php +++ b/app/Http/Controllers/SapFileController.php @@ -183,31 +183,38 @@ class SapFileController extends Controller $serialNumbers = []; $remainingRows = []; - foreach ($lines as $line) { - $values = str_getcsv($line); + foreach ($lines as $line) + { + $values = str_getcsv($line); + + // Debug: make sure CSV split is correct + if (count($values) < 6) { + return response()->json([ + 'status' => 'ERROR', + 'message' => 'In text file has fewer than 6 columns', + 'line' => $line + ], 400); + } + + $serialNumbers[] = $values[5] ?? null; + + $remainingRow = $values; + unset($remainingRow[5]); + // $remainingRow = array_values($remainingRow); // reindex + // $remainingRows[] = $remainingRow; + $remainingRow = array_values($remainingRow); // reindex + + // Only add unique remaining rows + if (!in_array($remainingRow, $remainingRows, true)) { + $remainingRows[] = $remainingRow; + } + } - // Debug: make sure CSV split is correct - if (count($values) < 6) { return response()->json([ - 'status' => 'ERROR', - 'message' => 'CSV row has fewer than 6 columns', - 'line' => $line - ], 400); - } - - $serialNumbers[] = $values[5] ?? null; - - $remainingRow = $values; - unset($remainingRow[5]); - $remainingRow = array_values($remainingRow); // reindex - $remainingRows[] = $remainingRow; -} - -return response()->json([ - 'status' => 'SUCCESS', - 'serial_numbers' => $serialNumbers, - 'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug -]); + 'status' => 'SUCCESS', + 'serial_numbers' => $serialNumbers, + 'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug + ]); }