diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 3e45576d..38000a48 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -454,60 +454,47 @@ class WarrantyController extends Controller $file = $req->files->get('csv_file'); // process the csv file. Call the WarrantyBulkUploader service - $inv_entries = $warr_uploader->warrantyUpload($file); + $entries = $warr_uploader->warrantyUpload($file); $resp = new StreamedResponse(); - if (count($inv_entries) > 0) - { - $resp->setCallback(function() use($inv_entries) { - // csv output - $csv_handle = fopen('php://output', 'w+'); - fputcsv($csv_handle, [ - 'Encoded By', - 'Date of Encoded', - 'Owner First Name', - 'Owner Last Name', - 'Owner Email', - 'Owner Address', - 'Owner Mobile', - 'Owner Telephone', - 'Vehicle Make', - 'Vehicle Model', - 'Vehicle Year', - 'Vehicle Plate Number', - 'Battery Serial Number', - 'Battery Sales Invoice', - 'Battery Date of Purchase', - 'Distributor Name', - 'Distributor Address', - 'Application Type ID', - 'Battery ID', - 'Ownership Type', - 'Status', - 'Reason Warranty Not Added', - ]); - foreach ($inv_entries as $row) - { - fputcsv($csv_handle, $row); - } + $resp->setCallback(function() use($entries) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Row Num', + 'Encoded By', + 'Date of Encoded', + 'Owner First Name', + 'Owner Last Name', + 'Owner Email', + 'Owner Address', + 'Owner Mobile', + 'Owner Telephone', + 'Vehicle Make', + 'Vehicle Model', + 'Vehicle Year', + 'Vehicle Plate Number', + 'Battery Serial Number', + 'Battery Sales Invoice', + 'Battery Date of Purchase', + 'Distributor Name', + 'Distributor Address', + 'Application Type ID', + 'Battery ID', + 'Ownership Type', + 'Status', + 'Reason Warranty Not Added', + ]); + foreach ($entries as $row) + { + fputcsv($csv_handle, $row); + } - fclose($csv_handle); - }); + fclose($csv_handle); + }); - } - 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'; + $filename = 'upload_warranty_results' . '.csv'; $resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); diff --git a/src/Service/WarrantyBulkUploader.php b/src/Service/WarrantyBulkUploader.php index 085386a4..fec7b657 100644 --- a/src/Service/WarrantyBulkUploader.php +++ b/src/Service/WarrantyBulkUploader.php @@ -37,6 +37,8 @@ class WarrantyBulkUploader const F_BATT_ID = 18; // sap code in system const F_OWNER_TYPE = 19; + const FIELD_COUNT = 20; + protected $em; protected $batt_hash; @@ -77,7 +79,7 @@ class WarrantyBulkUploader } // process row - $output_info[] = $this->processRow($fields, $sql_values); + $output_info[] = $this->processRow($fields, $sql_values, $row_num); $row_num++; } @@ -100,7 +102,7 @@ class WarrantyBulkUploader 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])); @@ -116,7 +118,7 @@ class WarrantyBulkUploader $plate_number = $this->cleanPlateNumber($fields[self::F_PLATE_NUMBER]); // validate the necessary fields - $output_info = $this->validateFields($fields); + $output_info = $this->validateFields($fields, $row_num); if (!empty($output_info)) return $output_info; @@ -192,15 +194,24 @@ class WarrantyBulkUploader // if we reach this point, warranty is going to 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; } - protected function validateFields($fields) + protected function validateFields($fields, $row_num) { $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]); $date_purchase = trim($fields[self::F_DATE_PURCHASE]); $sap_code = trim($fields[self::F_BATT_ID]); @@ -214,16 +225,16 @@ class WarrantyBulkUploader if (empty($clean_plate)) { $message = 'No plate number.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } // validate date purchase // (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)) { $message = 'No date purchase.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } @@ -231,7 +242,7 @@ class WarrantyBulkUploader if ($purchase_date === false) { $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; } // validate serial @@ -239,7 +250,7 @@ class WarrantyBulkUploader if (empty($serial)) { $message = 'No battery serial number.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } // validate battery @@ -248,14 +259,14 @@ class WarrantyBulkUploader if (empty($sap_code)) { $message = 'No battery ID.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } if (!(isset($this->batt_hash[$sap_code]))) { $message = 'Battery not found.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } @@ -276,7 +287,7 @@ class WarrantyBulkUploader if (!empty($warr_results)) { $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; } @@ -292,7 +303,7 @@ class WarrantyBulkUploader if (!empty($w_results)) { $message = 'Warranty already exists for serial.'; - $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message); + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } @@ -306,8 +317,16 @@ class WarrantyBulkUploader 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]); $date_encoded = trim($fields[self::F_DATE_ENCODED]); $fname = trim($fields[self::F_FNAME]); @@ -330,6 +349,7 @@ class WarrantyBulkUploader $owner_type = trim($fields[self::F_OWNER_TYPE]); return [ + $row_num, $encoder, $date_encoded, $fname,