From 9318b370770b3537f290068b2f8d1e201bcf6e5e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 06:08:50 +0000 Subject: [PATCH 1/3] Remove checking of entries. #660 --- src/Controller/WarrantyController.php | 84 +++++++++++---------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 3e45576d..b25fd14d 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -454,60 +454,46 @@ 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, [ + '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 . '"'); From 4afa21f51e7affbdbf7e9e33dbb25dcfe8455928 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 08:06:36 +0000 Subject: [PATCH 2/3] Add row number to output. Add checking for correct number of fields. #660 --- src/Controller/WarrantyController.php | 1 + src/Service/WarrantyBulkUploader.php | 50 +++++++++++++++++++-------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index b25fd14d..38000a48 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -462,6 +462,7 @@ class WarrantyController extends Controller // csv output $csv_handle = fopen('php://output', 'w+'); fputcsv($csv_handle, [ + 'Row Num', 'Encoded By', 'Date of Encoded', 'Owner First Name', diff --git a/src/Service/WarrantyBulkUploader.php b/src/Service/WarrantyBulkUploader.php index 085386a4..8ba278a3 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 F_TOTAL = 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::F_TOTAL) + { + $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::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]); $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, From 938d6ff84870c5e0728a0e1857eb2f8ae3d3417f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 May 2022 08:10:44 +0000 Subject: [PATCH 3/3] Rename field total. #660 --- src/Service/WarrantyBulkUploader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Service/WarrantyBulkUploader.php b/src/Service/WarrantyBulkUploader.php index 8ba278a3..fec7b657 100644 --- a/src/Service/WarrantyBulkUploader.php +++ b/src/Service/WarrantyBulkUploader.php @@ -37,7 +37,7 @@ class WarrantyBulkUploader const F_BATT_ID = 18; // sap code in system const F_OWNER_TYPE = 19; - const F_TOTAL = 20; + const FIELD_COUNT = 20; protected $em; protected $batt_hash; @@ -205,7 +205,7 @@ class WarrantyBulkUploader // check number of fields. // (1) should match what is in F_TOTAL - if (count($fields) != self::F_TOTAL) + if (count($fields) != self::FIELD_COUNT) { $message = 'Invalid row.'; $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); @@ -319,7 +319,7 @@ class WarrantyBulkUploader protected function setOutputInfo($fields, $status, $message, $row_num) { - if (count($fields) != self::F_TOTAL) + if (count($fields) != self::FIELD_COUNT) { // since we don't know field might be missing, we just output status and reason return [