Add checking for sap battery in hash. #660

This commit is contained in:
Korina Cordero 2022-05-16 06:16:23 +00:00
parent 3f9fd88729
commit 4803152fb1

View file

@ -39,6 +39,8 @@ class WarrantyBulkUploader
const FIELD_COUNT = 20;
const DEFAULT_SAP_WARRANTY = 12; // default warranty period for sap batteries
protected $em;
protected $batt_hash;
protected $serial_hash;
@ -149,14 +151,31 @@ class WarrantyBulkUploader
$vehicle_id = $cv_results['v_id'];
}
// get battery info from hash (battery model id, battery size id, warranty periods (needed for expiration date)
$batt_info = $this->batt_hash[$sap_code];
$b_id = $batt_info['id'];
$model_id = $batt_info['model_id'];
$size_id = $batt_info['size_id'];
$warr_private = $batt_info['warr_private'];
$warr_commercial = $batt_info['warr_commercial'];
$warr_tnv = $batt_info['warr_tnv'];
// initialize bty id, size id and sap bty id to NULL by default
$model_id = 'NULL';
$size_id = 'NULL';
$sap_bty_id = 'NULL';
// get battery info from hash if sap_code in hash
// (battery model id, battery size id, warranty periods (needed for expiration date)
// if not, use info from sap_batt_hash
if (isset($this->batt_hash[$sap_code]))
{
$batt_info = $this->batt_hash[$sap_code];
$b_id = $batt_info['id'];
$model_id = $batt_info['model_id'];
$size_id = $batt_info['size_id'];
$warr_private = $batt_info['warr_private'];
$warr_commercial = $batt_info['warr_commercial'];
$warr_tnv = $batt_info['warr_tnv'];
}
else
{
// we get id and warranty period
$batt_info = $this->sap_batt_hash[$sap_code];
$sap_bty_id = $sap_code;
$warr = $batt_info['warranty'];
}
// format purchase date to DateTime and then change the format to Y-m-d
$purchase_date = DateTime::createFromFormat('d-M-y', $date_purchase);
@ -166,9 +185,21 @@ class WarrantyBulkUploader
// compute expiration date
// TODO: might need checking for what kind of warranty for the warranty period
// by default, we use private
if (isset($this->batt_hash[$sap_code]))
{
// by default, we use private for resq batteries for bulk upload
$warranty_period = $warr_private;
}
else
{
// set warranty class to the default sap battery
$warranty_period = $warr;
}
// by default we set warranty class to private
$warranty_class = WarrantyClass::WTY_PRIVATE;
$date_expire = $this->computeDateExpire($purchase_date, $warr_private);
$date_expire = $this->computeDateExpire($purchase_date, $warranty_period);
// convert all dates to string for the values string for the insert statement
//$str_date_create = $date_create->format('Y-m-d H:i:s');
@ -179,14 +210,24 @@ 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
. '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::BULK_UPLOAD . '\')';
if ($sap_bty_id == 'NULL')
{
// set the sap_bty_id with no quotes since it's null
$value_string = '(' . $model_id . ',' . $size_id . ',' . $sap_bty_id . ',\'' . $serial . '\',\'' . $warranty_class . '\',\''
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $str_date_purchase
. '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::BULK_UPLOAD . '\')';
}
else
{
// set the sap_bty_id with quotes since it's a string
$value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_bty_id . '\',\'' . $serial . '\',\'' . $warranty_class . '\',\''
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $str_date_purchase
. '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::BULK_UPLOAD . '\')';
}
if (strlen($sql_values) == 0)
{
@ -264,7 +305,8 @@ class WarrantyBulkUploader
}
// validate battery
// (1) should not be empty
// (2) should find battery using sap_code.
// (2) should find battery using sap_code. But if no battery with sap_code,
// we check sap_battery hash
if (empty($sap_code))
{
$message = 'No battery ID.';
@ -274,9 +316,14 @@ class WarrantyBulkUploader
if (!(isset($this->batt_hash[$sap_code])))
{
$message = 'Battery not found.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors;
// check sap_battery_hash for sap battery. We need to at least set either:
// (1) bty model + bty size or (2) sap bty id.
if (!(isset($this->sap_batt_hash[$sap_code])))
{
$message = 'Battery not found.';
$errors = $this->setOutputInfo($fields, 'NOT UPLOADED', $message, $row_num);
return $errors;
}
}
// (1) check if warranty exists using serial + plate number
@ -433,7 +480,7 @@ class WarrantyBulkUploader
}
}
protected funciton populateSAPBatteryIndex()
protected function populateSAPBatteryIndex()
{
$conn = $this->em->getConnection();
@ -447,9 +494,11 @@ class WarrantyBulkUploader
// go through the rows
foreach ($results as $row)
{
// set warranty period to default warranty period for SAP batteries
$this->sap_batt_hash[$row['id']] = [
'sap_brand' => $row['brand_id'],
'sap_size' => $row['size_id'],
'warranty' => self::DEFAULT_SAP_WARRANTY,
];
}
}