Merge branch 'master' of gitlab.com:jankstudio/resq into 654-script-to-fulflll-job-orders

This commit is contained in:
Korina Cordero 2022-05-17 01:58:16 +00:00
commit fa02cd1a67

View file

@ -39,8 +39,12 @@ 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;
protected $sap_batt_hash;
public function __construct(EntityManagerInterface $em)
{
@ -52,6 +56,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
{
@ -144,7 +151,16 @@ 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)
// 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'];
@ -153,17 +169,41 @@ class WarrantyBulkUploader
$warr_commercial = $batt_info['warr_commercial'];
$warr_tnv = $batt_info['warr_tnv'];
// check that sap code is also in sap battery
if (isset($this->sap_batt_hash[$sap_code]))
$sap_bty_id = $sap_code;
}
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('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');
// 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');
@ -174,10 +214,24 @@ class WarrantyBulkUploader
$last_name = addslashes($lname);
$mobile_number = addslashes($mobile);
// 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 . '\',\''
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)
{
@ -238,10 +292,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;
}
@ -255,7 +309,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.';
@ -264,11 +319,16 @@ class WarrantyBulkUploader
}
if (!(isset($this->batt_hash[$sap_code])))
{
// 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
// (2) check if serial already exists even if for another plate number
@ -307,6 +367,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 +484,29 @@ class WarrantyBulkUploader
}
}
protected function 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)
{
// 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,
];
}
}
protected function cleanPlateNumber($plate)
{
return strtoupper(str_replace(' ', '', $plate));