Merge branch '660-add-customer-and-customer-vehicle-in-warranty-upload' into 'master'

Remove checking of entries. #660

Closes #660

See merge request jankstudio/resq!779
This commit is contained in:
Kendrick Chan 2022-05-12 08:19:39 +00:00
commit 1c22b8b96f
2 changed files with 71 additions and 64 deletions

View file

@ -454,60 +454,47 @@ class WarrantyController extends Controller
$file = $req->files->get('csv_file'); $file = $req->files->get('csv_file');
// process the csv file. Call the WarrantyBulkUploader service // process the csv file. Call the WarrantyBulkUploader service
$inv_entries = $warr_uploader->warrantyUpload($file); $entries = $warr_uploader->warrantyUpload($file);
$resp = new StreamedResponse(); $resp = new StreamedResponse();
if (count($inv_entries) > 0) $resp->setCallback(function() use($entries) {
{ // csv output
$resp->setCallback(function() use($inv_entries) { $csv_handle = fopen('php://output', 'w+');
// csv output fputcsv($csv_handle, [
$csv_handle = fopen('php://output', 'w+'); 'Row Num',
fputcsv($csv_handle, [ 'Encoded By',
'Encoded By', 'Date of Encoded',
'Date of Encoded', 'Owner First Name',
'Owner First Name', 'Owner Last Name',
'Owner Last Name', 'Owner Email',
'Owner Email', 'Owner Address',
'Owner Address', 'Owner Mobile',
'Owner Mobile', 'Owner Telephone',
'Owner Telephone', 'Vehicle Make',
'Vehicle Make', 'Vehicle Model',
'Vehicle Model', 'Vehicle Year',
'Vehicle Year', 'Vehicle Plate Number',
'Vehicle Plate Number', 'Battery Serial Number',
'Battery Serial Number', 'Battery Sales Invoice',
'Battery Sales Invoice', 'Battery Date of Purchase',
'Battery Date of Purchase', 'Distributor Name',
'Distributor Name', 'Distributor Address',
'Distributor Address', 'Application Type ID',
'Application Type ID', 'Battery ID',
'Battery ID', 'Ownership Type',
'Ownership Type', 'Status',
'Status', 'Reason Warranty Not Added',
'Reason Warranty Not Added', ]);
]); foreach ($entries as $row)
foreach ($inv_entries as $row) {
{ fputcsv($csv_handle, $row);
fputcsv($csv_handle, $row); }
}
fclose($csv_handle); fclose($csv_handle);
}); });
} $filename = 'upload_warranty_results' . '.csv';
else
{
$resp->setCallback(function() {
// csv output
$csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, ['No Invalid Warranties']);
fclose($csv_handle);
});
}
$filename = 'invalid_warranties' . '.csv';
$resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); $resp->headers->set('Content-Type', 'text/csv; charset=utf-8');
$resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');

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 FIELD_COUNT = 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::FIELD_COUNT)
{
$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::FIELD_COUNT)
{
// 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,