diff --git a/app/Http/Controllers/SapFileController.php b/app/Http/Controllers/SapFileController.php index 36c447e..c120c94 100644 --- a/app/Http/Controllers/SapFileController.php +++ b/app/Http/Controllers/SapFileController.php @@ -180,40 +180,71 @@ class SapFileController extends Controller // Prepare arrays - $serialNumbers = []; - $remainingRows = []; + // $serialNumbers = []; + // $remainingRows = []; - foreach ($lines as $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; + // } + // } + + // return response()->json([ + // 'status' => 'SUCCESS', + // 'serial_numbers' => $serialNumbers, + // 'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug + // ]); + + $serialNumbers = []; + $remainingRow = null; + + 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', + 'message' => 'Text file row has fewer than 6 columns', 'line' => $line ], 400); } + // Collect serial number $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; + // Store remaining row only once + if ($remainingRow === null) { + $remainingRow = $values; + unset($remainingRow[5]); // remove serial number + $remainingRow = array_values($remainingRow); // reindex } } return response()->json([ 'status' => 'SUCCESS', 'serial_numbers' => $serialNumbers, - 'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug + 'remaining_row' => $remainingRow, // only one copy ]); }