Enhance readFiles method to validate CSV row structure and return error response for insufficient columns
This commit is contained in:
@@ -177,31 +177,37 @@ class SapFileController extends Controller
|
|||||||
// 'status' => 'SUCCESS',
|
// 'status' => 'SUCCESS',
|
||||||
// 'rows_imported' => count($rows),
|
// 'rows_imported' => count($rows),
|
||||||
// ]);
|
// ]);
|
||||||
// Remove header row
|
|
||||||
$headerLine = array_shift($lines);
|
|
||||||
|
|
||||||
// Prepare arrays
|
// Prepare arrays
|
||||||
$serialNumbers = [];
|
$serialNumbers = [];
|
||||||
$remainingRows = [];
|
$remainingRows = [];
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
$values = str_getcsv($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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Debug: make sure CSV split is correct
|
||||||
|
if (count($values) < 6) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'SUCCESS',
|
'status' => 'ERROR',
|
||||||
'serial_numbers' => $serialNumbers,
|
'message' => 'CSV row has fewer than 6 columns',
|
||||||
'remaining_rows' => $remainingRows,
|
'line' => $line
|
||||||
]);
|
], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$serialNumbers[] = $values[5] ?? null;
|
||||||
|
|
||||||
|
$remainingRow = $values;
|
||||||
|
unset($remainingRow[5]);
|
||||||
|
$remainingRow = array_values($remainingRow); // reindex
|
||||||
|
$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