Refactor readFiles method to enhance CSV row validation and ensure unique remaining rows are collected
This commit is contained in:
@@ -183,14 +183,15 @@ class SapFileController extends Controller
|
||||
$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' => 'CSV row has fewer than 6 columns',
|
||||
'message' => 'In text file has fewer than 6 columns',
|
||||
'line' => $line
|
||||
], 400);
|
||||
}
|
||||
@@ -199,15 +200,21 @@ class SapFileController extends Controller
|
||||
|
||||
$remainingRow = $values;
|
||||
unset($remainingRow[5]);
|
||||
// $remainingRow = array_values($remainingRow); // reindex
|
||||
// $remainingRows[] = $remainingRow;
|
||||
$remainingRow = array_values($remainingRow); // reindex
|
||||
$remainingRows[] = $remainingRow;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
// 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
|
||||
]);
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user