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;
|
$filePath = $path . '/' . $fileName;
|
||||||
|
|
||||||
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
$headerLine = array_shift($lines); // removes first line
|
||||||
|
|
||||||
if (!$lines) {
|
if (!$lines) {
|
||||||
return response()->json(['status' => 'ERROR', 'message' => 'File is empty'], 400);
|
return response()->json(['status' => 'ERROR', 'message' => 'File is empty'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug: return first 5 lines to check reading
|
// Debug: return first 5 lines to check reading
|
||||||
return response()->json([
|
// return response()->json([
|
||||||
'status' => 'DEBUG_LINES',
|
// 'status' => 'DEBUG_LINES',
|
||||||
'file' => $fileName,
|
// 'file' => $fileName,
|
||||||
'lines_sample' => array_slice($lines, 0, 5) // first 5 lines
|
// 'lines_sample' => array_slice($lines, 0, 5) // first 5 lines
|
||||||
]);
|
// ]);
|
||||||
|
|
||||||
if (!file_exists($filePath) || !is_readable($filePath)) {
|
if (!file_exists($filePath) || !is_readable($filePath)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -143,31 +145,62 @@ class SapFileController extends Controller
|
|||||||
], 500);
|
], 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = 'class_characteristics';
|
// $table = 'class_characteristics';
|
||||||
$columns = Schema::getColumnListing($table); // returns lowercase column names
|
// $columns = Schema::getColumnListing($table); // returns lowercase column names
|
||||||
|
|
||||||
$rows = [];
|
// $rows = [];
|
||||||
foreach ($lines as $line) {
|
// foreach ($lines as $line) {
|
||||||
$values = str_getcsv($line); // split by comma
|
// $values = str_getcsv($line); // split by comma
|
||||||
|
|
||||||
$row = [];
|
// $row = [];
|
||||||
foreach ($columns as $index => $column) {
|
// foreach ($columns as $index => $column) {
|
||||||
$value = $values[$index] ?? null; // if missing, set null
|
// $value = $values[$index] ?? null; // if missing, set null
|
||||||
if ($value === '') $value = null; // empty string -> null
|
// if ($value === '') $value = null; // empty string -> null
|
||||||
$row[$column] = $value;
|
// $row[$column] = $value;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// $rows[] = $row;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return response()->json([
|
||||||
|
// 'status' => 'DEBUG_ROWS',
|
||||||
|
// 'rows_sample' => array_slice($rows, 0, 5),
|
||||||
|
// ]);
|
||||||
|
|
||||||
$rows[] = $row;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert into database
|
// Insert into database
|
||||||
foreach ($rows as $row) {
|
// foreach ($rows as $row) {
|
||||||
\App\Models\ClassCharacteristic::create($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([
|
return response()->json([
|
||||||
'status' => 'SUCCESS',
|
'status' => 'SUCCESS',
|
||||||
'rows_imported' => count($rows),
|
'serial_numbers' => $serialNumbers,
|
||||||
|
'remaining_rows' => $remainingRows,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user