diff --git a/src/Service/WarrantyBulkUploader.php b/src/Service/WarrantyBulkUploader.php index fec7b657..7ebb3278 100644 --- a/src/Service/WarrantyBulkUploader.php +++ b/src/Service/WarrantyBulkUploader.php @@ -41,6 +41,8 @@ class WarrantyBulkUploader protected $em; protected $batt_hash; + protected $serial_hash; + protected $sap_batt_hash; public function __construct(EntityManagerInterface $em) { @@ -52,6 +54,9 @@ class WarrantyBulkUploader // hash the battery table using sap_code as index $this->populateBatteryIndex(); + // hash the sap battery table + $this->populateSAPBatteryIndex(); + // attempt to open file try { @@ -154,7 +159,7 @@ class WarrantyBulkUploader $warr_tnv = $batt_info['warr_tnv']; // format purchase date to DateTime and then change the format to Y-m-d - $purchase_date = DateTime::createFromFormat('m/d/y', $date_purchase); + $purchase_date = DateTime::createFromFormat('d-M-y', $date_purchase); // need to manually create the created date $date_create = date('Y-m-d H:i:s'); @@ -174,6 +179,10 @@ class WarrantyBulkUploader $last_name = addslashes($lname); $mobile_number = addslashes($mobile); + // TODO: add checking for sap code in sap battery hash + // if it exists in sap battery hash, we insert sap_code in warranty + // if not, we insert NULL in its place. + // populate the values string for the values to be inserted into warranty $value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $serial . '\',\'' . $warranty_class . '\',\'' . $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $str_date_purchase @@ -238,10 +247,10 @@ class WarrantyBulkUploader return $errors; } - $purchase_date = DateTime::createFromFormat('m/d/y', $date_purchase); + $purchase_date = DateTime::createFromFormat('d-M-y', $date_purchase); 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: dd-mmm-yyyy (example: 06-Jun-16)'; $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); return $errors; } @@ -307,6 +316,17 @@ class WarrantyBulkUploader return $errors; } + // check if serial is a duplicate of serial within the file + // add serial to hash, in case csv file has duplicate serials in it. + if (!isset($this->serial_hash[$serial])) + $this->serial_hash[$serial] = $serial; + else + { + $message = 'Duplicate serial in file.'; + $errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num); + return $errors; + } + return $errors; } @@ -413,6 +433,27 @@ class WarrantyBulkUploader } } + protected funciton populateSAPBatteryIndex() + { + $conn = $this->em->getConnection(); + + // get all the sap batteries + $sql = 'SELECT sap.id, sap.brand_id, sap.size_id FROM sap_battery sap'; + $stmt = $conn->prepare($sql); + $stmt->execute(); + + $results = $stmt->fetchAll(); + + // go through the rows + foreach ($results as $row) + { + $this->sap_batt_hash[$row['id']] = [ + 'sap_brand' => $row['brand_id'], + 'sap_size' => $row['size_id'], + ]; + } + } + protected function cleanPlateNumber($plate) { return strtoupper(str_replace(' ', '', $plate));