Add method for getting default vehicle because of em->clear #274

This commit is contained in:
Kendrick Chan 2019-11-21 09:27:35 +08:00
parent 9a4ead6ab9
commit fba5d9cb86

View file

@ -27,10 +27,21 @@ class CreateCustomerFromWarrantyCommand extends Command
protected $em; protected $em;
protected $cust_index; protected $cust_index;
protected $cvu_mfg_id;
protected $cvu_brand_id;
public function __construct(ObjectManager $em) public function __construct(ObjectManager $em)
{ {
$this->em = $em; $this->em = $em;
// get the default ids from .env
// TODO: DO NOT USE $_ENV
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/../../.env');
$this->cvu_mfg_id = $_ENV['CVU_MFG_ID'];
$this->cvu_brand_id = $_ENV['CVU_BRAND_ID'];
parent::__construct(); parent::__construct();
} }
@ -41,16 +52,9 @@ class CreateCustomerFromWarrantyCommand extends Command
->setHelp('Creates customers from existing warranties.') ->setHelp('Creates customers from existing warranties.')
->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.'); ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.');
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
// get the default ids from .env
// TODO: DO NOT USE $_ENV
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/../../.env');
$cvu_mfg_id = $_ENV['CVU_MFG_ID'];
$cvu_brand_id = $_ENV['CVU_BRAND_ID'];
$csv_file = $input->getArgument('file'); $csv_file = $input->getArgument('file');
// attempt to open file // attempt to open file
@ -69,13 +73,6 @@ class CreateCustomerFromWarrantyCommand extends Command
$total_cust_added = 0; $total_cust_added = 0;
$total_cv_added = 0; $total_cv_added = 0;
// get default vehicle
$default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id);
if (empty($default_vehicle))
{
$output->writeln("Need to add vehicle with default values.");
return;
}
/* /*
// load all customers // load all customers
@ -183,7 +180,7 @@ class CreateCustomerFromWarrantyCommand extends Command
// customer exists but not customer vehicle // customer exists but not customer vehicle
// add customer vehicle to existing customer with unknown manufacturer and make // add customer vehicle to existing customer with unknown manufacturer and make
error_log('new vehicle - ' . $w_plate_number); error_log('new vehicle - ' . $w_plate_number);
$this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); $this->createCustomerVehicle($customer, $this->getDefaultVehicle(), $w_plate_number);
$total_cv_added++; $total_cv_added++;
} }
} }
@ -208,7 +205,7 @@ class CreateCustomerFromWarrantyCommand extends Command
$this->em->persist($new_cust); $this->em->persist($new_cust);
$this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); $this->createCustomerVehicle($new_cust, $this->getDefaultVehicle(), $w_plate_number);
// add latest customer to hash // add latest customer to hash
$this->cust_index[$w_mobile_num][] = $new_cust; $this->cust_index[$w_mobile_num][] = $new_cust;
@ -262,6 +259,19 @@ class CreateCustomerFromWarrantyCommand extends Command
$output->writeln('Total customer vehicles added: ' . $total_cv_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) protected function findCustomerByNumber($number)
{ {
$customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);