resq/src/Command/CreateCustomerFromWarrantyCommand.php

165 lines
6.1 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\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\Warranty;
use App\Entity\Customer;
use App\Entity\CustomerVehicle;
use App\Entity\VehicleManufacturer;
use App\Entity\Vehicle;
class CreateCustomerFromWarrantyCommand extends Command
{
protected $em;
protected $cust_index;
public function __construct(ObjectManager $em)
{
$this->em = $em;
$this->loadCustomers();
parent::__construct();
}
protected function configure()
{
$this->setName('customer:createfromwarranty')
->setDescription('Create customers from existing warranties.')
->setHelp('Creates customers from existing warranties.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// get all warranties
$warranties = $this->em->getRepository(Warranty::class)->findAll();
foreach($warranties as $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, do nothing
continue;
}
// parse warranty mobile in case of multiple numbers
// check for spaces, slash, and forward slash
$w_mobile_array = [];
if (preg_match('/[\/\\]/', $w_mobile))
{
$w_mobile_array = preg_split('/[\/\\]/', $w_mobile);
}
else
{
// only one mobile number
$w_mobile_array[] = $w_mobile;
}
$cust_found = false;
// set values for new customer vehicle
$w_plate_number = $warr->getPlateNumber();
$default_cv_color = 'White';
// TODO: add checking that default manufacturer is not null
$default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy(['name' =>'Unknown']);
if (empty($default_manufacturer))
{
$output->writeln("Need to add manufacturer with Unknown name");
}
$default_make = 'Unknown';
// search cust_index for numbers in mobile_array
foreach ($w_mobile_array as $w_mobile_num)
{
// if present, check if customer vehicle plate number matches warranty plate number?
foreach ($this->cust_index as $key => $customer)
{
$c_mobile = $customer->getPhoneMobile();
if (!(empty($c_mobile)))
{
if (strpos($c_mobile, $w_mobile_num))
{
// mobile number belongs to existing customer
// get customer vehicles
$c_vehicles = $customer->getVehicles();
if (!(empty($c_vehicles)))
{
// check if plate number of customer vehicle matches warranty plate number
foreach ($c_vehicles as $c_vehicle)
{
$cv_plate_number = $c_vehicle->getPlateNumber();
if ($cv_plate_number == $w_plate_number)
{
// move to the next warranty since current warranty belongs to an
// existing customer and customer vehicle
break 3;
}
}
}
// add customer vehicle to existing customer with unknown manufacturer and make
$this->createCustomerVehicle($customer, $default_manufacturer,
$default_make, $w_plate_number, $default_cv_color);
}
$cust_found = true;
}
// no customer mobile number, ignore for now
}
// if customer not found, add customer and customer vehicle
if ($cust_found != true)
{
// get warranty first name, last name
$w_first_name = $warr->getFirstName();
$w_last_name = $warr->getLastName();
$new_cust = new Customer();
$new_cust->setFirstName($w_first_name)
->setLastName($w_last_name);
$this->em->persist($cust);
$this->createCustomerVehicle($cust, $default_manufacturer,
$default_make, $w_plate_number, $default_cv_color);
}
}
}
}
protected function loadCustomers()
{
// get all customers
$customers = $this->em->getRepository(Customer::class)->findAll();
$this->cust_index = [];
foreach ($customers as $customer)
{
$cust_id = $customer->getID();
$this->cust_index[$cust_id] = $customer;
}
}
protected function createCustomerVehicle(Customer $cust, $manufacturer, $make,
$plate_number, $color)
{
$new_cv = new CustomerVehicle();
// get manufacturer and make with name 'unknown'
// TODO: remove the assert not blank for color and model year
$new_cv->setCustomer($cust)
->setPlateNumber($plate_number)
->setModelYear(0)
->setColor($color)
->setStatusCondition(VehicleStatusCondition::BRAND_NEW)
->setFuelType(FuelType::GAS)
->setVehicle($vehicle);
}
}