resq/src/Command/CreateCustomerFromWarrantyCommand.php

455 lines
15 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Warranty;
use App\Entity\Customer;
use App\Entity\CustomerVehicle;
use App\Entity\VehicleManufacturer;
use App\Entity\Vehicle;
use App\Ramcar\FuelType;
use App\Ramcar\VehicleStatusCondition;
use DateTime;
class CreateCustomerFromWarrantyCommand extends Command
{
const CV_FOUND = 'Vehicle found';
const CV_NEW = 'New vehicle';
const CUST_NEW = 'New customer and vehicle.';
protected $em;
protected $cust_index;
protected $cvu_mfg_id;
protected $cvu_brand_id;
public function __construct(EntityManagerInterface $em, $cvu_mfg_id, $cvu_brand_id)
{
$this->em = $em;
$this->cvu_mfg_id = $cvu_mfg_id;
$this->cvu_brand_id = $cvu_brand_id;
parent::__construct();
}
protected function configure()
{
$this->setName('customer:createfromwarranty')
->setDescription('Create customers from existing warranties.')
->setHelp('Creates customers from existing warranties.')
->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv_file = $input->getArgument('file');
// attempt to open file
try
{
$fh = fopen($csv_file, "w");
}
catch (Exception $e)
{
throw new Exception('The file "' . $csv_file . '" could be opened.');
}
// counters for warranties, customers, customer vehicles
$total_warr = 0;
$total_inv_warr = 0;
$total_cust_added = 0;
$total_cv_added = 0;
/*
// load all customers
$output->writeln('Loading customer data...');
$this->loadCustomers($output);
*/
// get all warranties
error_log('Getting warranties...');
$warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.mobile_number is not null');
$warranties = $warr_q->iterate();
// $warranties = $warr_q->getResult();
// $warranties = $this->em->getRepository(Warranty::class)->findAll();
//$invalid_warranties = [];
// $warr_count = count($warranties);
$created_objs = [];
$output->writeln("Processing warranties... ");
foreach($warranties as $row)
{
$warr = $row[0];
$total_warr++;
// check if warranty mobile already exists in customer
$w_mobile = $warr->getMobileNumber();
if (empty($w_mobile))
{
// TODO: for now, if warranty mobile number is empty, add to list of invalid entries
//$invalid_warranties[] = $this->processInvalidEntries($warr);
continue;
}
// parse warranty mobile in case of multiple numbers
// check for spaces, slash, and forward slash
$w_mobile_array = [];
if (preg_match('/[\\\s\/]/', $w_mobile))
{
$w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile);
}
else
{
// only one mobile number
$w_mobile_array[] = $w_mobile;
}
// set values for new customer vehicle
$w_plate_number = $this->cleanPlateNumber($warr->getPlateNumber());
$cust_found = false;
foreach ($w_mobile_array as $w_mobile_num)
{
$w_mobile_num = trim($w_mobile_num);
// empty mobile num
if (empty($w_mobile_num))
continue;
// does it fit our 09XXXXXXXXX pattern?
if (preg_match('/^09[0-9]{9}$/', $w_mobile_num))
{
// remove first '0'
$w_mobile_num = substr($w_mobile_num, 1);
error_log("CONVERTED TO $w_mobile_num");
}
// does it fit our 9XXXXXXXXX pattern?
if (!preg_match('/^9[0-9]{9}$/', $w_mobile_num))
continue;
/*
// min length 2
// TODO: we need to check proper phone number format
// format should be '9XXXXXXXXX'
// TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert
if (strlen($w_mobile_num <= 2))
continue;
*/
error_log('');
error_log("($total_warr) processing $w_mobile_num from warranty...");
$customers = $this->findCustomerByNumber($w_mobile_num);
if (!empty($customers))
{
error_log('found customer for ' . $w_mobile_num);
foreach ($customers as $customer)
{
// get customer vehicles for customer
$c_vehicles = $customer->getVehicles();
$cv_found = false;
if (!empty($c_vehicles))
{
// check if plate number of customer vehicle matches warranty plate number
foreach ($c_vehicles as $c_vehicle)
{
$clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber());
// check if it's already there
if ($clean_cv_plate == $w_plate_number)
{
// customer and customer vehicle already exists
$cv_found = true;
break;
}
}
}
// if there was a customer vehicle matched
if ($cv_found)
{
// vehicle found, do nothing.
error_log('vehicle found - ' . $w_plate_number);
$created_objs[] = $this->createReportData($warr, self::CV_FOUND);
}
else
{
// customer exists but not customer vehicle
// add customer vehicle to existing customer with unknown manufacturer and make
error_log('new vehicle - ' . $w_plate_number);
$this->createCustomerVehicle($customer, $this->getDefaultVehicle(), $w_plate_number);
$created_objs[] = $this->createReportData($warr, self::CV_NEW);
$total_cv_added++;
}
}
}
// customer not found
else
{
error_log('NEW customer and vehicle - ' . $w_plate_number);
// customer not found, add customer and customer vehicle
// get warranty first name, last name
$w_first_name = $warr->getFirstName();
$w_last_name = $warr->getLastName();
//$output->writeln($w_first_name);
//$output->writeln($w_last_name);
//$output->writeln($w_plate_number);
$new_cust = new Customer();
$new_cust->setFirstName($w_first_name)
->setLastName($w_last_name)
->setPhoneMobile($w_mobile_num);
$this->em->persist($new_cust);
$this->createCustomerVehicle($new_cust, $this->getDefaultVehicle(), $w_plate_number);
$created_objs[] = $this->createReportData($warr, self::CUST_NEW);
// add latest customer to hash
//$this->cust_index[$w_mobile_num][] = $new_cust;
$total_cust_added++;
$total_cv_added++;
}
}
$this->em->flush();
$this->em->clear();
}
/*
// process invalid warranties, if any
if (count($invalid_warranties) > 0)
{
fputcsv($fh, [
'ID',
'Serial',
'Warranty Class',
'Last Name',
'First Name',
'Mobile Number',
'Plate Number',
'Battery Model',
'Battery Size',
'SAP Battery',
'Status',
'Date Created',
'Date Purchased',
'Expiry Date',
'Date Claimed',
'Claimed From',
'Privacy Policy',
]);
foreach($invalid_warranties as $row)
{
$total_inv_warr++;
fputcsv($fh, $row);
}
}
*/
// process the report data
if (count($created_objs) > 0)
{
fputcsv($fh, [
'ID',
'Mobile Number',
'Plate Number',
'Action Done',
]);
foreach($created_objs as $row)
{
fputcsv($fh, $row);
}
}
fclose($fh);
$output->writeln('');
$output->writeln('Total warranties: ' . $total_warr);
//$output->writeln('Total warranties with no mobile number: ' . $total_inv_warr);
$output->writeln('Total customers added: ' . $total_cust_added);
$output->writeln('Total customer vehicles added: ' . $total_cv_added);
}
protected function getDefaultVehicle()
{
// get default vehicle
$default_vehicle = $this->em->getRepository(Vehicle::class)->find($this->cvu_brand_id);
if ($default_vehicle == null)
{
$output->writeln("Need to add vehicle with default values.");
return null;
}
return $default_vehicle;
}
protected function findCustomerByNumber($number)
{
$customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
return $customers;
}
protected function loadCustomers()
{
// get all customers
$customers = $this->em->getRepository(Customer::class)->findAll();
$cust_q = $this->em->createQuery('select c from App\Entity\Customer c');
$cust_iter = $q->iterate();
$this->cust_index = [];
foreach ($cust_iter as $customer)
{
$mobile = trim($customer->getPhoneMobile());
if (!empty($mobile))
{
$mobile_array = [];
// need to check if multiple numbers in mobile
if (preg_match('/[\\\s\/]/', $mobile))
{
$mobile_array = preg_split('/[\\\s\/]/', $mobile);
}
else
{
// only one mobile number
$mobile_array[] = $mobile;
}
foreach($mobile_array as $number)
{
if (!(empty($number)))
{
if (!isset($this->cust_index[$number]))
$this->cust_index[$number] = [];
$this->cust_index[$number][] = $customer;
}
}
}
}
}
protected function createCustomerVehicle(Customer $cust, $vehicle, $plate_number)
{
$new_cv = new CustomerVehicle();
$new_cv->setCustomer($cust)
->setPlateNumber($plate_number)
->setStatusCondition(VehicleStatusCondition::BRAND_NEW)
->setModelYear('')
->setColor('')
->setFuelType(FuelType::GAS)
->setHasMotoliteBattery(true)
->setVehicle($vehicle);
$this->em->persist($new_cv);
}
protected function createReportData($warr, $action)
{
$obj = [
'id' => $warr->getID(),
'mobile_number' => $warr->getMobileNumber(),
'plate_number' => $warr->getPlateNumber(),
'action_done' => $action,
];
return $obj;
}
protected function processInvalidEntries($warr)
{
$batt_model = '';
$batt_size = '';
$sap_batt = '';
$policy = '';
$date_purchased = '';
$date_expire = '';
$date_claim = '';
$create_date = $warr->getDateCreate();
$date_create = $create_date->format('d/M/y');
if ($warr->getDatePurchase() != null)
{
$p_date = $warr->getDatePurchase();
$date_purchased = $p_date->format('d/M/y');
}
if ($warr->getDateClaim() != null)
{
$c_date = $warr->getDateClaim();
$date_claim = $c_date->format('d/M/y');
}
if ($warr->getDateExpire() != null)
{
$e_date = $warr->getDateExpire();
$date_expire = $e_date->format('d/M/y');
}
if ($warr->getBatteryModel() != null)
{
$batt_model = $warr->getBatteryModel()->getName();
}
if ($warr->getBatterySize() != null)
{
$batt_size = $warr->getBatterySize()->getName();
}
if ($warr->getSAPBattery() != null)
{
$sap_batt = $warr->getSAPBattery()->getBrand()->getName();
}
if ($warr->getPrivacyPolicy() != null)
{
$policy = $warr->getPrivacyPolicy()->getName();
}
$invalid_warranty = [
'id' => $warr->getID(),
'serial' => $warr->getSerial(),
'warranty_class' => $warr->getWarrantyClass(),
'last_name' => $warr->getLastName(),
'first_name' => $warr->getFirstName(),
'mobile_number' => $warr->getMobileNumber(),
'plate_number' => $warr->getPlateNumber(),
'battery_model' => $batt_model,
'battery_size' => $batt_size,
'sap_battery' => $sap_batt,
'status' => $warr->getStatus(),
'date_create' => $date_create,
'date_purchase' => $date_purchased,
'date_expire' => $date_expire,
'date_claim' => $date_claim,
'claimed_from' => $warr->getClaimedFrom(),
'privacy_policy' => $policy,
];
return $invalid_warranty;
}
protected function cleanPlateNumber($plate)
{
// remove spaces and make upper case
return strtoupper(str_replace(' ', '', $plate));
}
}