From 144156e377aa4fa89955f4533298ee2f4b9eabb4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 16 May 2019 07:05:21 +0000 Subject: [PATCH 1/2] Create command that generates a JSON file of vehicles and their models with their compatible battery. #212 --- .../GenerateBatteryCompatibilityCommand.php | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/Command/GenerateBatteryCompatibilityCommand.php diff --git a/src/Command/GenerateBatteryCompatibilityCommand.php b/src/Command/GenerateBatteryCompatibilityCommand.php new file mode 100644 index 00000000..dca7f36b --- /dev/null +++ b/src/Command/GenerateBatteryCompatibilityCommand.php @@ -0,0 +1,97 @@ +em = $om; + + parent::__construct(); + } + + protected function populateVehicleManufacturerIndex() + { + $vms = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + + $this->vmfg_index = []; + foreach ($vms as $vm) + { + $mfg_name = $vm->getName(); + $this->vmfg_index[$mfg_name] = $vm; + + // get vehicles from manufacturer + $make_array = []; + $vehicles = $vm->getVehicles(); + foreach ($vehicles as $vehicle) + { + $batteries = $vehicle->getBatteries(); + $comp_batt = []; + foreach ($batteries as $battery) + { + // set to the first compatible battery found until a more expensive one is found + $comp_batt['id'] = $battery->getID(); + $comp_batt['name'] = $battery->getModel()->getName() . ' ' . + $battery->getSize()->getName(); + $comp_batt['image_url'] = $battery->getImageFile(); + //$comp_batt['description'] = $battery->getDescription(); + + // store the selling price for comparison + $batt_price = $battery->getSellingPrice(); + + // find the most expensive compatible battery + if ($battery->getSellingPrice() > $batt_price) + { + $comp_batt['id'] = $battery->getID(); + $comp_batt['name'] = $battery->getModel()->getName() . ' ' . + $battery->getSize()->getName(); + $comp_batt['image_url'] = $battery->getImageFile(); + //$comp_batt['description'] = $battery->getDescription(); + } + } + $make_array[$vehicle->getMake()][$vehicle->getModelYearFormatted()] = $comp_batt; + + } + $this->vmfg_index[$mfg_name] = $make_array; + + + } + } + + protected function configure() + { + $this->setName('battery:generate_compatibility_json') + ->setDescription('Generate a json file containing the vehicles and their compatible batteries') + ->setHelp('Generate a json file containing the vehicles and their compatible batteries') + ->addArgument('file', InputArgument::REQUIRED, 'Path and name for JSON file.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $filename = $input->getArgument('file'); + $this->populateVehicleManufacturerIndex(); + + $json_file = fopen($filename, 'w'); + fwrite($json_file, json_encode($this->vmfg_index, JSON_PRETTY_PRINT)); + fclose($json_file); + + } +} From 92c0f2d1699a13b99c49c8cd1f35aae61ad9e622 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Mon, 27 May 2019 09:57:20 +0800 Subject: [PATCH 2/2] Fix bug in battery:generate_compatibility_json command #212 --- .../GenerateBatteryCompatibilityCommand.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Command/GenerateBatteryCompatibilityCommand.php b/src/Command/GenerateBatteryCompatibilityCommand.php index dca7f36b..57e20076 100644 --- a/src/Command/GenerateBatteryCompatibilityCommand.php +++ b/src/Command/GenerateBatteryCompatibilityCommand.php @@ -36,7 +36,7 @@ class GenerateBatteryCompatibilityCommand extends Command foreach ($vms as $vm) { $mfg_name = $vm->getName(); - $this->vmfg_index[$mfg_name] = $vm; + // $this->vmfg_index[$mfg_name] = []; // get vehicles from manufacturer $make_array = []; @@ -51,7 +51,8 @@ class GenerateBatteryCompatibilityCommand extends Command $comp_batt['id'] = $battery->getID(); $comp_batt['name'] = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName(); - $comp_batt['image_url'] = $battery->getImageFile(); + $comp_batt['image_url'] = '/assets/img/products/' . strtolower($battery->getModel()->getName()) . '.png'; + $comp_batt['description'] = ''; //$comp_batt['description'] = $battery->getDescription(); // store the selling price for comparison @@ -67,10 +68,16 @@ class GenerateBatteryCompatibilityCommand extends Command //$comp_batt['description'] = $battery->getDescription(); } } - $make_array[$vehicle->getMake()][$vehicle->getModelYearFormatted()] = $comp_batt; + + // check if no compatible batteries + if (!empty($comp_batt)) + $make_array[$vehicle->getMake()][$vehicle->getModelYearFormatted()] = $comp_batt; } - $this->vmfg_index[$mfg_name] = $make_array; + + // check if empty + if (!empty($make_array)) + $this->vmfg_index[$mfg_name] = $make_array; }