Refactor readFiles method to handle file content processing and return structured response with serial numbers and remaining rows
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user