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 App\Ramcar\WarrantySource;
|
||||
use App\Ramcar\WarrantyStatus;
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
use PDO;
|
||||
|
||||
class TestWarrantyUploadCommand extends Command
|
||||
|
|
@ -39,6 +42,7 @@ class TestWarrantyUploadCommand extends Command
|
|||
|
||||
protected $em;
|
||||
protected $batt_hash;
|
||||
protected $sap_batt_hash;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
|
|
@ -58,6 +62,12 @@ class TestWarrantyUploadCommand extends Command
|
|||
|
||||
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');
|
||||
$output_file = $input->getArgument('output_file');
|
||||
|
||||
|
|
@ -75,11 +85,18 @@ class TestWarrantyUploadCommand extends Command
|
|||
|
||||
// loop through rows
|
||||
// ignore first row since that's header data
|
||||
$row_num = 1;
|
||||
$row_num = 0;
|
||||
|
||||
$sql_values = '';
|
||||
while (($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
// ignore first row since that's the header
|
||||
if ($row_num == 0)
|
||||
{
|
||||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// process row
|
||||
$output_info[] = $this->processRow($fields, $sql_values);
|
||||
|
||||
|
|
@ -89,7 +106,10 @@ class TestWarrantyUploadCommand extends Command
|
|||
// check if we have values to insert
|
||||
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();
|
||||
$stmt = $conn->prepare($sql_statement);
|
||||
$stmt->execute();
|
||||
|
|
@ -105,8 +125,7 @@ class TestWarrantyUploadCommand extends Command
|
|||
|
||||
protected function processRow($fields, &$sql_values)
|
||||
{
|
||||
// hash the battery table using sap_code as index
|
||||
$this->populateBatteryIndex();
|
||||
error_log('Processing warranty with serial ' . trim($fields[self::F_SERIAL]));
|
||||
|
||||
$output_info = [];
|
||||
|
||||
|
|
@ -131,7 +150,7 @@ class TestWarrantyUploadCommand extends Command
|
|||
|
||||
// NOTE: what happens if there's more than one result?
|
||||
// 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';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute(['plate_number' => $plate_number]);
|
||||
|
|
@ -142,8 +161,8 @@ class TestWarrantyUploadCommand extends Command
|
|||
$vehicle_id = null;
|
||||
if (!empty($cv_results))
|
||||
{
|
||||
$cust_id = $cv_results[0];
|
||||
$vehicle_id = $cv_results[1];
|
||||
$cust_id = $cv_results['c_id'];
|
||||
$vehicle_id = $cv_results['v_id'];
|
||||
}
|
||||
|
||||
// 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
|
||||
$purchase_date = DateTime::createFromFormat('m/d/y', $date_purchase);
|
||||
$p_date = $purchase_date->format('Y-m-d');
|
||||
|
||||
// need to manually create the created date
|
||||
$date_create = date('Y-m-d H:i:s');
|
||||
|
|
@ -165,21 +183,35 @@ class TestWarrantyUploadCommand extends Command
|
|||
// compute expiration date
|
||||
// TODO: might need checking for what kind of warranty for the warranty period
|
||||
// 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);
|
||||
$last_name = addslashes($lname);
|
||||
$mobile_number = addslashes($mobile);
|
||||
|
||||
// populate the values string for the values to be inserted into warranty
|
||||
$value_string = '(' . $model_id . ',' . $size_id . ',\'' . $serial . ',\'' . $warranty_class . '\',\''
|
||||
. $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $p_date
|
||||
. '\',\'' . $date_expire . ',\'' . $sap_code . ',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $vehicle_id . ',' . $cust_id . ',' . WarrantySource::BULK_UPLOAD . '\')';
|
||||
$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 (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
|
||||
echo $sql_values;
|
||||
// error_log($sql_values);
|
||||
|
||||
return $output_info;
|
||||
}
|
||||
|
|
@ -214,10 +246,10 @@ class TestWarrantyUploadCommand extends Command
|
|||
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: 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);
|
||||
return $errors;
|
||||
}
|
||||
|
|
@ -232,7 +264,9 @@ class TestWarrantyUploadCommand extends Command
|
|||
// validate battery
|
||||
// (1) should not be empty
|
||||
// (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.';
|
||||
$errors = $this->setOutputInfo($fields, 'NOT ADDED', $message);
|
||||
|
|
@ -246,6 +280,14 @@ class TestWarrantyUploadCommand extends Command
|
|||
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
|
||||
$conn = $this->em->getConnection();
|
||||
|
||||
|
|
@ -321,7 +363,7 @@ class TestWarrantyUploadCommand extends Command
|
|||
$sap_code,
|
||||
$owner_type,
|
||||
$status,
|
||||
$reason,
|
||||
$message,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -382,18 +424,22 @@ class TestWarrantyUploadCommand extends Command
|
|||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute();
|
||||
|
||||
$results = $stmt->fetchAll();
|
||||
|
||||
// go through the rows
|
||||
while ($row = $stmt->fetchAll())
|
||||
foreach ($results as $row)
|
||||
{
|
||||
// breaking this down for clarity
|
||||
$battery_id = $row[0];
|
||||
$model_id = $row[1];
|
||||
$size_id = $row[2];
|
||||
$sap_code = $row[3];
|
||||
$warr_private = $row[4];
|
||||
$warr_commercial = $row[5];
|
||||
$warr_tnv = $row[6];
|
||||
$battery_id = $row['id'];
|
||||
$model_id = $row['model_id'];
|
||||
$size_id = $row['size_id'];
|
||||
$sap_code = trim($row['sap_code']);
|
||||
$warr_private = $row['warr_private'];
|
||||
$warr_commercial = $row['warr_commercial'];
|
||||
$warr_tnv = $row['warr_tnv'];
|
||||
|
||||
if(!empty($sap_code))
|
||||
{
|
||||
$this->batt_hash[$sap_code] = [
|
||||
'id' => $battery_id,
|
||||
'model_id' => $model_id,
|
||||
|
|
@ -404,6 +450,28 @@ class TestWarrantyUploadCommand extends Command
|
|||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function cleanPlateNumber($plate)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue