Fix issues found during testing of bulk upload. #660
This commit is contained in:
parent
ecff27524a
commit
86ef1ec646
1 changed files with 104 additions and 36 deletions
|
|
@ -10,8 +10,11 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
use App\Ramcar\WarrantySource;
|
use App\Ramcar\WarrantySource;
|
||||||
|
use App\Ramcar\WarrantyStatus;
|
||||||
|
use App\Ramcar\WarrantyClass;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use DateInterval;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
class TestWarrantyUploadCommand extends Command
|
class TestWarrantyUploadCommand extends Command
|
||||||
|
|
@ -39,6 +42,7 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
protected $em;
|
protected $em;
|
||||||
protected $batt_hash;
|
protected $batt_hash;
|
||||||
|
protected $sap_batt_hash;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
|
|
@ -58,6 +62,12 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
|
// hash the battery table using sap_code as index
|
||||||
|
$this->populateBatteryIndex();
|
||||||
|
|
||||||
|
// hash the sap battery table using id aka sap code as index
|
||||||
|
$this->populateSAPBatteryIndex();
|
||||||
|
|
||||||
$csv_file = $input->getArgument('input_file');
|
$csv_file = $input->getArgument('input_file');
|
||||||
$output_file = $input->getArgument('output_file');
|
$output_file = $input->getArgument('output_file');
|
||||||
|
|
||||||
|
|
@ -75,11 +85,18 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
// loop through rows
|
// loop through rows
|
||||||
// ignore first row since that's header data
|
// ignore first row since that's header data
|
||||||
$row_num = 1;
|
$row_num = 0;
|
||||||
|
|
||||||
$sql_values = '';
|
$sql_values = '';
|
||||||
while (($fields = fgetcsv($fh)) !== false)
|
while (($fields = fgetcsv($fh)) !== false)
|
||||||
{
|
{
|
||||||
|
// ignore first row since that's the header
|
||||||
|
if ($row_num == 0)
|
||||||
|
{
|
||||||
|
$row_num++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// process row
|
// process row
|
||||||
$output_info[] = $this->processRow($fields, $sql_values);
|
$output_info[] = $this->processRow($fields, $sql_values);
|
||||||
|
|
||||||
|
|
@ -89,11 +106,14 @@ class TestWarrantyUploadCommand extends Command
|
||||||
// check if we have values to insert
|
// check if we have values to insert
|
||||||
if (strlen($sql_values) > 0)
|
if (strlen($sql_values) > 0)
|
||||||
{
|
{
|
||||||
$sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,sap_code,first_name,last_name,mobile_number,flag_activated,create_source,vehicle_id,customer_id, create_source) VALUES ' . $sql_values . ';' . "\n";
|
$sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $sql_values . ';' . "\n";
|
||||||
|
|
||||||
|
// error_log($sql_statement);
|
||||||
|
|
||||||
$conn = $this->em->getConnection();
|
$conn = $this->em->getConnection();
|
||||||
$stmt = $conn->prepare($sql_statement);
|
$stmt = $conn->prepare($sql_statement);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to output file
|
// write to output file
|
||||||
$this->outputWarrantyInfo($output_file, $output_info);
|
$this->outputWarrantyInfo($output_file, $output_info);
|
||||||
|
|
@ -105,8 +125,7 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
protected function processRow($fields, &$sql_values)
|
protected function processRow($fields, &$sql_values)
|
||||||
{
|
{
|
||||||
// hash the battery table using sap_code as index
|
error_log('Processing warranty with serial ' . trim($fields[self::F_SERIAL]));
|
||||||
$this->populateBatteryIndex();
|
|
||||||
|
|
||||||
$output_info = [];
|
$output_info = [];
|
||||||
|
|
||||||
|
|
@ -131,7 +150,7 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
// NOTE: what happens if there's more than one result?
|
// NOTE: what happens if there's more than one result?
|
||||||
// for now, get the first one
|
// for now, get the first one
|
||||||
$sql = 'SELECT c.id AS c_id, v.id FROM customer_vehicle cv, customer c, vehicle v
|
$sql = 'SELECT c.id AS c_id, v.id AS v_id FROM customer_vehicle cv, customer c, vehicle v
|
||||||
WHERE cv.customer_id = c.id AND cv.vehicle_id = v.id AND cv.plate_number = :plate_number LIMIT 1';
|
WHERE cv.customer_id = c.id AND cv.vehicle_id = v.id AND cv.plate_number = :plate_number LIMIT 1';
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = $conn->prepare($sql);
|
||||||
$stmt->execute(['plate_number' => $plate_number]);
|
$stmt->execute(['plate_number' => $plate_number]);
|
||||||
|
|
@ -142,8 +161,8 @@ class TestWarrantyUploadCommand extends Command
|
||||||
$vehicle_id = null;
|
$vehicle_id = null;
|
||||||
if (!empty($cv_results))
|
if (!empty($cv_results))
|
||||||
{
|
{
|
||||||
$cust_id = $cv_results[0];
|
$cust_id = $cv_results['c_id'];
|
||||||
$vehicle_id = $cv_results[1];
|
$vehicle_id = $cv_results['v_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get battery info from hash (battery model id, battery size id, warranty periods (needed for expiration date)
|
// get battery info from hash (battery model id, battery size id, warranty periods (needed for expiration date)
|
||||||
|
|
@ -157,7 +176,6 @@ class TestWarrantyUploadCommand extends Command
|
||||||
|
|
||||||
// format purchase date to DateTime and then change the format to Y-m-d
|
// 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('m/d/y', $date_purchase);
|
||||||
$p_date = $purchase_date->format('Y-m-d');
|
|
||||||
|
|
||||||
// need to manually create the created date
|
// need to manually create the created date
|
||||||
$date_create = date('Y-m-d H:i:s');
|
$date_create = date('Y-m-d H:i:s');
|
||||||
|
|
@ -165,21 +183,35 @@ class TestWarrantyUploadCommand extends Command
|
||||||
// compute expiration date
|
// compute expiration date
|
||||||
// TODO: might need checking for what kind of warranty for the warranty period
|
// TODO: might need checking for what kind of warranty for the warranty period
|
||||||
// by default, we use private
|
// by default, we use private
|
||||||
$date_expire = $this->computeDateExpire($p_date, $warr_private);
|
$warranty_class = WarrantyClass::WTY_PRIVATE;
|
||||||
|
$date_expire = $this->computeDateExpire($purchase_date, $warr_private);
|
||||||
|
|
||||||
|
// 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');
|
||||||
|
$str_date_purchase = $purchase_date->format('Y-m-d H:i:s');
|
||||||
|
$str_date_expire = $date_expire->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
$first_name = addslashes($fname);
|
$first_name = addslashes($fname);
|
||||||
$last_name = addslashes($lname);
|
$last_name = addslashes($lname);
|
||||||
$mobile_number = addslashes($mobile);
|
$mobile_number = addslashes($mobile);
|
||||||
|
|
||||||
// populate the values string for the values to be inserted into warranty
|
// populate the values string for the values to be inserted into warranty
|
||||||
$value_string = '(' . $model_id . ',' . $size_id . ',\'' . $serial . ',\'' . $warranty_class . '\',\''
|
$value_string = '(' . $model_id . ',' . $size_id . ',\'' . $sap_code . '\',\'' . $serial . '\',\'' . $warranty_class . '\',\''
|
||||||
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $p_date
|
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $str_date_purchase
|
||||||
. '\',\'' . $date_expire . ',\'' . $sap_code . ',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',' . WarrantySource::BULK_UPLOAD . '\')';
|
. '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',\'' . WarrantySource::BULK_UPLOAD . '\')';
|
||||||
|
|
||||||
$sql_values = $sql_values . ',' . $value_string;
|
if (strlen($sql_values) == 0)
|
||||||
|
{
|
||||||
|
// first entry to insert, should have no comma before
|
||||||
|
$sql_values = $value_string;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// need to insert a comma after the existing string
|
||||||
|
$sql_values = $sql_values . ',' . $value_string;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: remove this later, this is for debugging
|
// error_log($sql_values);
|
||||||
echo $sql_values;
|
|
||||||
|
|
||||||
return $output_info;
|
return $output_info;
|
||||||
}
|
}
|
||||||
|
|
@ -214,10 +246,10 @@ class TestWarrantyUploadCommand extends Command
|
||||||
return $errors;
|
return $errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
$purchase_date = DateTime::createFromFormat('m/d/y', $date_purchase);
|
$purchase_date = DateTime::createFromFormat('d-M-y', $date_purchase);
|
||||||
if ($purchase_date === false)
|
if ($purchase_date === false)
|
||||||
{
|
{
|
||||||
$message = 'Invalid date format. Date format should be: mm/dd/yy';
|
$message = 'Invalid date format. Date format should be: dd-mon-yy (example: 27-Nov-21)';
|
||||||
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
||||||
return $errors;
|
return $errors;
|
||||||
}
|
}
|
||||||
|
|
@ -232,7 +264,9 @@ class TestWarrantyUploadCommand extends Command
|
||||||
// validate battery
|
// validate battery
|
||||||
// (1) should not be empty
|
// (1) should not be empty
|
||||||
// (2) should find battery using sap_code.
|
// (2) should find battery using sap_code.
|
||||||
if ($empty($sap_code))
|
// (3) TODO: depending on Myla's answer. If she says sap_code is id of sap battery
|
||||||
|
// should find sap battery using sap_code
|
||||||
|
if (empty($sap_code))
|
||||||
{
|
{
|
||||||
$message = 'No battery ID.';
|
$message = 'No battery ID.';
|
||||||
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
||||||
|
|
@ -246,6 +280,14 @@ class TestWarrantyUploadCommand extends Command
|
||||||
return $errors;
|
return $errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remove this if Myla says sap code is not id of sap battery
|
||||||
|
if (!(isset($this->sap_batt_hash[$sap_code])))
|
||||||
|
{
|
||||||
|
$message = 'SAP Battery not found.';
|
||||||
|
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
|
|
||||||
// check if warranty exists using serial + plate number
|
// check if warranty exists using serial + plate number
|
||||||
$conn = $this->em->getConnection();
|
$conn = $this->em->getConnection();
|
||||||
|
|
||||||
|
|
@ -321,7 +363,7 @@ class TestWarrantyUploadCommand extends Command
|
||||||
$sap_code,
|
$sap_code,
|
||||||
$owner_type,
|
$owner_type,
|
||||||
$status,
|
$status,
|
||||||
$reason,
|
$message,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -382,25 +424,51 @@ class TestWarrantyUploadCommand extends Command
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = $conn->prepare($sql);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
|
$results = $stmt->fetchAll();
|
||||||
|
|
||||||
// go through the rows
|
// go through the rows
|
||||||
while ($row = $stmt->fetchAll())
|
foreach ($results as $row)
|
||||||
{
|
{
|
||||||
// breaking this down for clarity
|
// breaking this down for clarity
|
||||||
$battery_id = $row[0];
|
$battery_id = $row['id'];
|
||||||
$model_id = $row[1];
|
$model_id = $row['model_id'];
|
||||||
$size_id = $row[2];
|
$size_id = $row['size_id'];
|
||||||
$sap_code = $row[3];
|
$sap_code = trim($row['sap_code']);
|
||||||
$warr_private = $row[4];
|
$warr_private = $row['warr_private'];
|
||||||
$warr_commercial = $row[5];
|
$warr_commercial = $row['warr_commercial'];
|
||||||
$warr_tnv = $row[6];
|
$warr_tnv = $row['warr_tnv'];
|
||||||
|
|
||||||
$this->batt_hash[$sap_code] = [
|
if(!empty($sap_code))
|
||||||
'id' => $battery_id,
|
{
|
||||||
'model_id' => $model_id,
|
$this->batt_hash[$sap_code] = [
|
||||||
'size_id' => $size_id,
|
'id' => $battery_id,
|
||||||
'warr_private' => $warr_private,
|
'model_id' => $model_id,
|
||||||
'warr_commercial' => $warr_commercial,
|
'size_id' => $size_id,
|
||||||
'warr_tnv' => $warr_tnv,
|
'warr_private' => $warr_private,
|
||||||
|
'warr_commercial' => $warr_commercial,
|
||||||
|
'warr_tnv' => $warr_tnv,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$this->sap_batt_hash[$row['id']] = [
|
||||||
|
'sap_brand' => $row['brand_id'],
|
||||||
|
'sap_size' => $row['size_id'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue