From f353a699a2e9a0781f5e5a4fef80f06df9400334 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 26 Sep 2025 11:38:14 +0530 Subject: [PATCH] Refactor readFiles method to handle file content processing and return structured response with serial numbers and remaining rows --- app/Http/Controllers/SapFileController.php | 75 ++++++++++++++++------ 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/SapFileController.php b/app/Http/Controllers/SapFileController.php index 8da15ea..7da07a2 100644 --- a/app/Http/Controllers/SapFileController.php +++ b/app/Http/Controllers/SapFileController.php @@ -125,16 +125,18 @@ class SapFileController extends Controller $filePath = $path . '/' . $fileName; $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + $headerLine = array_shift($lines); // removes first line + if (!$lines) { return response()->json(['status' => 'ERROR', 'message' => 'File is empty'], 400); } // Debug: return first 5 lines to check reading - return response()->json([ - 'status' => 'DEBUG_LINES', - 'file' => $fileName, - 'lines_sample' => array_slice($lines, 0, 5) // first 5 lines - ]); + // return response()->json([ + // 'status' => 'DEBUG_LINES', + // 'file' => $fileName, + // 'lines_sample' => array_slice($lines, 0, 5) // first 5 lines + // ]); if (!file_exists($filePath) || !is_readable($filePath)) { return response()->json([ @@ -143,31 +145,62 @@ class SapFileController extends Controller ], 500); } - $table = 'class_characteristics'; - $columns = Schema::getColumnListing($table); // returns lowercase column names + // $table = 'class_characteristics'; + // $columns = Schema::getColumnListing($table); // returns lowercase column names - $rows = []; - foreach ($lines as $line) { - $values = str_getcsv($line); // split by comma + // $rows = []; + // foreach ($lines as $line) { + // $values = str_getcsv($line); // split by comma - $row = []; - foreach ($columns as $index => $column) { - $value = $values[$index] ?? null; // if missing, set null - if ($value === '') $value = null; // empty string -> null - $row[$column] = $value; - } + // $row = []; + // foreach ($columns as $index => $column) { + // $value = $values[$index] ?? null; // if missing, set null + // if ($value === '') $value = null; // empty string -> null + // $row[$column] = $value; + // } + + // $rows[] = $row; + // } + + // return response()->json([ + // 'status' => 'DEBUG_ROWS', + // 'rows_sample' => array_slice($rows, 0, 5), + // ]); - $rows[] = $row; - } // Insert into database - foreach ($rows as $row) { - \App\Models\ClassCharacteristic::create($row); + // foreach ($rows as $row) { + // \App\Models\ClassCharacteristic::create($row); + // } + + // return response()->json([ + // 'status' => 'SUCCESS', + // 'rows_imported' => count($rows), + // ]); + // Remove header row + $headerLine = array_shift($lines); + + // Prepare arrays + $serialNumbers = []; + $remainingRows = []; + + foreach ($lines as $line) { + $values = str_getcsv($line); + + // Serial number is 6th column (index 5) + $serialNumbers[] = $values[5] ?? null; + + // Remove the serial number column and keep remaining + $remainingRow = $values; + unset($remainingRow[5]); + $remainingRow = array_values($remainingRow); // reindex array + $remainingRows[] = $remainingRow; } return response()->json([ 'status' => 'SUCCESS', - 'rows_imported' => count($rows), + 'serial_numbers' => $serialNumbers, + 'remaining_rows' => $remainingRows, ]); }