Add row number to output. Add checking for correct number of fields. #660

This commit is contained in:
Korina Cordero 2022-05-12 08:06:36 +00:00
parent 9318b37077
commit 4afa21f51e
2 changed files with 36 additions and 15 deletions

View file

@ -462,6 +462,7 @@ class WarrantyController extends Controller
// csv output // csv output
$csv_handle = fopen('php://output', 'w+'); $csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, [ fputcsv($csv_handle, [
'Row Num',
'Encoded By', 'Encoded By',
'Date of Encoded', 'Date of Encoded',
'Owner First Name', 'Owner First Name',

View file

@ -37,6 +37,8 @@ class WarrantyBulkUploader
const F_BATT_ID = 18; // sap code in system const F_BATT_ID = 18; // sap code in system
const F_OWNER_TYPE = 19; const F_OWNER_TYPE = 19;
const F_TOTAL = 20;
protected $em; protected $em;
protected $batt_hash; protected $batt_hash;
@ -77,7 +79,7 @@ class WarrantyBulkUploader
} }
// process row // process row
$output_info[] = $this->processRow($fields, $sql_values); $output_info[] = $this->processRow($fields, $sql_values, $row_num);
$row_num++; $row_num++;
} }
@ -100,7 +102,7 @@ class WarrantyBulkUploader
return $output_info; return $output_info;
} }
protected function processRow($fields, &$sql_values) protected function processRow($fields, &$sql_values, $row_num)
{ {
// error_log('Processing warranty with serial ' . trim($fields[self::F_SERIAL])); // error_log('Processing warranty with serial ' . trim($fields[self::F_SERIAL]));
@ -116,7 +118,7 @@ class WarrantyBulkUploader
$plate_number = $this->cleanPlateNumber($fields[self::F_PLATE_NUMBER]); $plate_number = $this->cleanPlateNumber($fields[self::F_PLATE_NUMBER]);
// validate the necessary fields // validate the necessary fields
$output_info = $this->validateFields($fields); $output_info = $this->validateFields($fields, $row_num);
if (!empty($output_info)) if (!empty($output_info))
return $output_info; return $output_info;
@ -192,15 +194,24 @@ class WarrantyBulkUploader
// if we reach this point, warranty is going to be uploaded // if we reach this point, warranty is going to be uploaded
// so we also output to file the warranty that will be uploaded // so we also output to file the warranty that will be uploaded
$output_info = $this->setOutputInfo($fields, 'UPLOADED', ''); $output_info = $this->setOutputInfo($fields, 'UPLOADED', '', $row_num);
return $output_info; return $output_info;
} }
protected function validateFields($fields) protected function validateFields($fields, $row_num)
{ {
$errors = []; $errors = [];
// check number of fields.
// (1) should match what is in F_TOTAL
if (count($fields) != self::F_TOTAL)
{
$message = 'Invalid row.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors;
}
$serial = trim($fields[self::F_SERIAL]); $serial = trim($fields[self::F_SERIAL]);
$date_purchase = trim($fields[self::F_DATE_PURCHASE]); $date_purchase = trim($fields[self::F_DATE_PURCHASE]);
$sap_code = trim($fields[self::F_BATT_ID]); $sap_code = trim($fields[self::F_BATT_ID]);
@ -214,16 +225,16 @@ class WarrantyBulkUploader
if (empty($clean_plate)) if (empty($clean_plate))
{ {
$message = 'No plate number.'; $message = 'No plate number.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
// validate date purchase // validate date purchase
// (1) date purchase should not be empty // (1) date purchase should not be empty
// (2) date purchase should be of format: d-M-y // (2) date purchase should be of format: m/d/y
if (empty($date_purchase)) if (empty($date_purchase))
{ {
$message = 'No date purchase.'; $message = 'No date purchase.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
@ -231,7 +242,7 @@ class WarrantyBulkUploader
if ($purchase_date === false) if ($purchase_date === false)
{ {
$message = 'Invalid date format. Date format should be: m/d/y (example: 06/13/16)'; $message = 'Invalid date format. Date format should be: m/d/y (example: 06/13/16)';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
// validate serial // validate serial
@ -239,7 +250,7 @@ class WarrantyBulkUploader
if (empty($serial)) if (empty($serial))
{ {
$message = 'No battery serial number.'; $message = 'No battery serial number.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
// validate battery // validate battery
@ -248,14 +259,14 @@ class WarrantyBulkUploader
if (empty($sap_code)) if (empty($sap_code))
{ {
$message = 'No battery ID.'; $message = 'No battery ID.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
if (!(isset($this->batt_hash[$sap_code]))) if (!(isset($this->batt_hash[$sap_code])))
{ {
$message = 'Battery not found.'; $message = 'Battery not found.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
@ -276,7 +287,7 @@ class WarrantyBulkUploader
if (!empty($warr_results)) if (!empty($warr_results))
{ {
$message = 'Warranty already exists for serial and plate number.'; $message = 'Warranty already exists for serial and plate number.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
@ -292,7 +303,7 @@ class WarrantyBulkUploader
if (!empty($w_results)) if (!empty($w_results))
{ {
$message = 'Warranty already exists for serial.'; $message = 'Warranty already exists for serial.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors; return $errors;
} }
@ -306,8 +317,16 @@ class WarrantyBulkUploader
return $expire_date; return $expire_date;
} }
protected function setOutputInfo($fields, $status, $message) protected function setOutputInfo($fields, $status, $message, $row_num)
{ {
if (count($fields) != self::F_TOTAL)
{
// since we don't know field might be missing, we just output status and reason
return [
$row_num,'','','','','','','','','','','','','','','','','','','','', $status, $message,
];
}
$encoder = trim($fields[self::F_ENCODER]); $encoder = trim($fields[self::F_ENCODER]);
$date_encoded = trim($fields[self::F_DATE_ENCODED]); $date_encoded = trim($fields[self::F_DATE_ENCODED]);
$fname = trim($fields[self::F_FNAME]); $fname = trim($fields[self::F_FNAME]);
@ -330,6 +349,7 @@ class WarrantyBulkUploader
$owner_type = trim($fields[self::F_OWNER_TYPE]); $owner_type = trim($fields[self::F_OWNER_TYPE]);
return [ return [
$row_num,
$encoder, $encoder,
$date_encoded, $date_encoded,
$fname, $fname,