From b8602a5c28579af1676dc82ba5e5c09a54bdc3b8 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 5 Apr 2018 01:53:45 +0800 Subject: [PATCH] Update import commands to new format #70 --- src/Command/ImportBatteryPriceCommand.php | 178 ++++++++++ src/Command/ImportVehicleBatteryCommand.php | 154 ++------- .../ImportVehicleCompatibilityCommand.php | 322 ++++++++++++++++++ 3 files changed, 529 insertions(+), 125 deletions(-) create mode 100644 src/Command/ImportBatteryPriceCommand.php create mode 100644 src/Command/ImportVehicleCompatibilityCommand.php diff --git a/src/Command/ImportBatteryPriceCommand.php b/src/Command/ImportBatteryPriceCommand.php new file mode 100644 index 00000000..02bbbea9 --- /dev/null +++ b/src/Command/ImportBatteryPriceCommand.php @@ -0,0 +1,178 @@ +em = $om; + $this->size_index = []; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('battery:import') + ->setDescription('Import a CSV file with batteries and prices.') + ->setHelp('Creates battery sizes and models based on imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.') + ->addArgument('model', InputArgument::REQUIRED, 'Model to match to CSV contents.'); + } + + protected function populateSizeIndex() + { + $this->size_index = []; + + $sizes = $this->em->getRepository(BatterySize::class)->findAll(); + + foreach ($sizes as $size) + { + $key = trim(strtolower($size->getName())); + $this->size_index[$key] = $size; + } + } + + protected function getWarrantyPrivate($model_name) + { + switch (strtolower($model_name)) + { + case 'excel': + return 24; + case 'gold': + return 21; + case 'enduro': + return 15; + } + + return 15; + } + + protected function getWarrantyCommercial($model_name) + { + switch (strtolower($model_name)) + { + case 'excel': + return 8; + case 'gold': + return 6; + case 'enduro': + return 6; + } + + return 6; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + $this->populateSizeIndex(); + + /* + CSV column order: + 0 - size + 1 - sap code + 2 - price + 3 - trade in price other + 4 - trade in price motolite + 5 - trade in price premium + */ + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + // manufacturer + $bmfg = $em->getRepository(BatteryManufacturer::class)->find(1); + + // model + $model_name = $input->getArgument('model'); + $model = new BatteryModel(); + $model->setName($model_name); + $em->persist($model); + + $row_num = 1; + while (($fields = fgetcsv($fh)) !== false) + { + $output->writeln("Parsing row " . $row_num . "..."); + $size_name = $fields[self::F_SIZE]; + + // check if in size index + $size_key = trim(strtolower($size_name)); + if (isset($this->size_index[$size_key])) + $size = $this->size_index[$size_key]; + else + { + // create new size + $size = new BatterySize(); + $size->setName($size_name) + ->setTIPriceOther($fields[self::F_TIP_OTHER]) + ->setTIPriceMotolite($fields[self::F_TIP_MOTOLITE]) + ->setTIPricePremium($fields[self::F_TIP_PREMIUM]); + $em->persist($size); + + // put in our index + $this->size_index[$size_key] = $size; + } + + // create battery + // TODO: check if dupe exists? + $price = trim(str_replace(',', '', $fields[self::F_SRP])); + $batt = new Battery(); + $batt->setManufacturer($bmfg) + ->setModel($model) + ->setSize($size) + ->setSAPCode($fields[self::F_SAP_CODE]) + ->setWarrantyPrivate($this->getWarrantyPrivate($model_name)) + ->setWarrantyCommercial($this->getWarrantyCommercial($model_name)) + ->setSellingPrice($price) + ->setReserveCapacity(0) + ->setLength(0) + ->setWidth(0) + ->setHeight(0) + ->setTotalHeight(0) + ->setProductCode(''); + $em->persist($batt); + + $row_num++; + } + + $em->flush(); + } +} diff --git a/src/Command/ImportVehicleBatteryCommand.php b/src/Command/ImportVehicleBatteryCommand.php index f7ce69b5..93c0bd77 100644 --- a/src/Command/ImportVehicleBatteryCommand.php +++ b/src/Command/ImportVehicleBatteryCommand.php @@ -21,19 +21,26 @@ use App\Entity\BatterySize; class ImportVehicleBatteryCommand extends Command { + const F_V_BRAND = 0; + const F_V_MAKE = 1; + const F_V_MODEL_YEAR = 2; + const F_B_SIZE = 3; + const F_B_MODEL = 4; + // the rest of the fields are irrelevant + + protected $em; protected $vmfg_index; protected $v_index; - protected $bmodel_index; - protected $bsize_index; - protected $b_index; + + protected $batt_index; public function __construct(ObjectManager $om) { $this->em = $om; $this->vmfg_index = []; $this->v_index = []; - $this->b_index = []; + $this->batt_index = []; parent::__construct(); } @@ -67,53 +74,19 @@ class ImportVehicleBatteryCommand extends Command { $bs = $this->em->getRepository(Battery::class)->findAll(); - $this->b_index = []; - $this->bmodel_index = []; - $this->bsize_index = []; + $this->batt_index = []; foreach ($bs as $b) { - $this->bsize_index[$b->getSize()->getName()] = $b->getSize(); - $this->bmodel_index[$b->getModel()->getName()] = $b->getModel(); - $this->b_index[$b->getSize()->getName() . '|' . $b->getModel()->getName()] = $b; + $key = $this->getBatteryKey($b->getSize()->getName(), $b->getModel()->getName()); + $this->batt_index[$key] = $b; } } - protected function handleBatteryData(&$batteries, &$b_models, $batt_field, $row_num) + protected function getBatteryKey($size_name, $model_name) { - // split battery into parts - $batt = trim($batt_field); - if ($batt == 'AGM') - { - $b_model = 'AGM'; - $b_size = 'AGM'; - } - else - { - $res = preg_match_all("/(.*)(GOLD|EXCEL|ENDURO|TRUCKMASTER|EFB)(.*)/", $batt, $matches); - - if ($res) - { - $b_size = trim($matches[1][0]); - $b_model = trim(trim($matches[2][0]) . ' ' . trim($matches[3][0])); - } - else - { - $b_size = $batt; - $b_model = ''; - } - - // print_r($b_size); - // print_r($b_model); - } - - // link battery to row num for vehicle matching later - if (!isset($batteries[$b_size])) - $batteries[$b_size] = []; - if (!isset($batteries[$b_size][$b_model])) - $batteries[$b_size][$b_model] = []; - - $batteries[$b_size][$b_model][] = $row_num; - $b_models[$b_model] = null; + return trim(strtolower(str_replace(' ', '', $size_name))) . + '|' . + trim(strtolower(str_replace(' ', '', $model_name))); } protected function execute(InputInterface $input, OutputInterface $output) @@ -122,16 +95,6 @@ class ImportVehicleBatteryCommand extends Command $this->populateVehicleIndex(); $this->populateBatteryIndex(); - /* - CSV column order: - 0 - brandh - 1 - make - 2 - model - 3 - recommended battery - 4 - alternative battery - 5 - ignore - */ - // attempt to open file try { @@ -155,13 +118,6 @@ class ImportVehicleBatteryCommand extends Command { $output->writeln("Parsing row " . $row_num . "..."); - // ignore first row - if ($row_num == 1) - { - $row_num++; - continue; - } - // parse it $brand = trim($fields[0]); if (!isset($vbrands[$brand])) @@ -184,15 +140,16 @@ class ImportVehicleBatteryCommand extends Command ]; // battery - $this->handleBatteryData($batteries, $b_models, $fields[3], $row_num); - $this->handleBatteryData($batteries, $b_models, $fields[4], $row_num); - + $batt_key = $this->getBatteryKey($fields[self::F_B_SIZE], $fields[self::F_B_MODEL]); + if (!isset($this->batt_index[$batt_key])) + $output->writeln('Could not find battery - ' . $fields[self::F_B_SIZE] . ' - ' . $fields[self::F_B_MODEL]); + else + $vbrands[$brand]['vehicles'][$row_num]['battery'] = $this->batt_index[$batt_key]; $row_num++; } // print_r($b_models); // print_r($batteries); - // save to db // vehicles @@ -234,8 +191,8 @@ class ImportVehicleBatteryCommand extends Command if (!isset($this->v_index[$brand_name][$vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to)])) { - echo "NO MATCH "; - echo $vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to) + 0; + echo "NO MATCH $m_year_from - $m_year_to"; + echo $vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to); echo "\n"; // vehicle $vehicle = new Vehicle(); @@ -250,6 +207,10 @@ class ImportVehicleBatteryCommand extends Command $vehicle = $this->v_index[$brand_name][$vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to)]; } + // battery + $vdata['battery']->addVehicle($vehicle); + // $vehicle->addBattery($vdata['battery']); + // update our data for battery linking $vehicle_index[$row_num] = $vehicle; } @@ -257,63 +218,6 @@ class ImportVehicleBatteryCommand extends Command $em->flush(); } - // battery models - foreach ($b_models as $b_model => $throw_away) - { - if (!isset($this->bmodel_index[$b_model])) - { - $bmodel = new BatteryModel(); - $bmodel->setName($b_model); - $em->persist($bmodel); - } - else - $bmodel = $this->bmodel_index[$b_model]; - - $b_models[$b_model] = $bmodel; - } - - // batteries - $bmfg = $em->getRepository(BatteryManufacturer::class)->find(1); - foreach ($batteries as $b_size => $batt_models) - { - if (!isset($this->bsize_index[$b_size])) - { - $bsize = new BatterySize(); - $bsize->setName($b_size); - $em->persist($bsize); - } - else - $bsize = $this->bsize_index[$b_size]; - - foreach ($batt_models as $b_model => $vehicle_rows) - { - $bmodel = $b_models[$b_model]; - - if (!isset($this->b_index[$bsize->getName() . '|' . $bmodel->getName()])) - { - $battery = new Battery(); - $battery->setManufacturer($bmfg) - ->setModel($bmodel) - ->setSize($bsize) - ->setProductCode('') - ->setSAPCode('') - ->setWarrantyPrivate(21) - ->setWarrantyCommercial(6) - ->setSellingPrice(5000); - - $em->persist($battery); - } - else - $battery = $this->b_index[$bsize->getName() . '|' . $bmodel->getName()]; - - // link to vehicles - foreach ($vehicle_rows as $row_num) - { - $vehicle = $vehicle_index[$row_num]; - $battery->addVehicle($vehicle); - } - } - } $em->flush(); diff --git a/src/Command/ImportVehicleCompatibilityCommand.php b/src/Command/ImportVehicleCompatibilityCommand.php new file mode 100644 index 00000000..29d74417 --- /dev/null +++ b/src/Command/ImportVehicleCompatibilityCommand.php @@ -0,0 +1,322 @@ +em = $om; + $this->vmfg_index = []; + $this->v_index = []; + $this->b_index = []; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('vehicle:import_old') + ->setDescription('Import a CSV file with vehicles and batteries.') + ->setHelp('Creates vehicles and batteries based on imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + protected function populateVehicleIndex() + { + $vs = $this->em->getRepository(Vehicle::class)->findAll(); + + $this->v_index = []; + $this->vmfg_index = []; + foreach ($vs as $v) + { + $mfg_name = $v->getManufacturer()->getName(); + $this->vmfg_index[$mfg_name] = $v->getManufacturer(); + if (!isset($this->v_index[$mfg_name])) + $this->v_index[$mfg_name] = []; + + $this->v_index[$mfg_name][$v->getMake() . '|' . $v->getModelYearFrom() . '|' . $v->getModelYearTo()] = $v; + } + } + + protected function populateBatteryIndex() + { + $bs = $this->em->getRepository(Battery::class)->findAll(); + + $this->b_index = []; + $this->bmodel_index = []; + $this->bsize_index = []; + foreach ($bs as $b) + { + $this->bsize_index[$b->getSize()->getName()] = $b->getSize(); + $this->bmodel_index[$b->getModel()->getName()] = $b->getModel(); + $this->b_index[$b->getSize()->getName() . '|' . $b->getModel()->getName()] = $b; + } + } + + protected function handleBatteryData(&$batteries, &$b_models, $batt_field, $row_num) + { + // split battery into parts + $batt = trim($batt_field); + if ($batt == 'AGM') + { + $b_model = 'AGM'; + $b_size = 'AGM'; + } + else + { + $res = preg_match_all("/(.*)(GOLD|EXCEL|ENDURO|TRUCKMASTER|EFB)(.*)/", $batt, $matches); + + if ($res) + { + $b_size = trim($matches[1][0]); + $b_model = trim(trim($matches[2][0]) . ' ' . trim($matches[3][0])); + } + else + { + $b_size = $batt; + $b_model = ''; + } + + // print_r($b_size); + // print_r($b_model); + } + + // link battery to row num for vehicle matching later + if (!isset($batteries[$b_size])) + $batteries[$b_size] = []; + if (!isset($batteries[$b_size][$b_model])) + $batteries[$b_size][$b_model] = []; + + $batteries[$b_size][$b_model][] = $row_num; + $b_models[$b_model] = null; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + $this->populateVehicleIndex(); + $this->populateBatteryIndex(); + + /* + CSV column order: + 0 - brandh + 1 - make + 2 - model + 3 - recommended battery + 4 - alternative battery + 5 - ignore + */ + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + $vbrands = []; + $batteries = []; + $bmodels = []; + + // loop through rows + $row_num = 1; + while (($fields = fgetcsv($fh)) !== false) + { + $output->writeln("Parsing row " . $row_num . "..."); + + // ignore first row + if ($row_num == 1) + { + $row_num++; + continue; + } + + // parse it + $brand = trim($fields[0]); + if (!isset($vbrands[$brand])) + { + // build array + $vbrands[$brand] = [ + 'vehicles' => [], + ]; + } + + $make = trim($fields[1]); + + $model = trim($fields[2]); + if (empty($model)) + $model = 'NONE'; + + $vbrands[$brand]['vehicles'][$row_num] = [ + 'make' => $make, + 'model' => $model, + ]; + + // battery + $this->handleBatteryData($batteries, $b_models, $fields[3], $row_num); + $this->handleBatteryData($batteries, $b_models, $fields[4], $row_num); + + + $row_num++; + } + // print_r($b_models); + // print_r($batteries); + + // save to db + + // vehicles + $vehicle_index = []; + foreach ($vbrands as $brand_name => $vbrand) + { + // get manufacturer or make one + if (!isset($this->vmfg_index[$brand_name])) + { + // manufacturer + $mfg = new VehicleManufacturer(); + $mfg->setName($brand_name); + $em->persist($mfg); + } + else + { + $mfg = $this->vmfg_index[$brand_name]; + } + + // vehicles + foreach ($vbrand['vehicles'] as $row_num => $vdata) + { + $model = $vdata['model']; + if ($model == 'NONE') + { + $m_year_from = 0; + $m_year_to = 0; + } + else + { + $ex_model = explode('-', $model); + $m_year_from = trim($ex_model[0]); + + if (isset($ex_model[1])) + $m_year_to = trim($ex_model[1]); + else + $m_year_to = 0; + } + + if (!isset($this->v_index[$brand_name][$vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to)])) + { + echo "NO MATCH "; + echo $vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to) + 0; + echo "\n"; + // vehicle + $vehicle = new Vehicle(); + $vehicle->setManufacturer($mfg) + ->setMake($vdata['make']) + ->setModelYearFrom($m_year_from) + ->setModelYearTo($m_year_to); + $em->persist($vehicle); + } + else + { + $vehicle = $this->v_index[$brand_name][$vdata['make'] . '|' . intval($m_year_from) . '|' . intval($m_year_to)]; + } + + // update our data for battery linking + $vehicle_index[$row_num] = $vehicle; + } + + $em->flush(); + } + + // battery models + foreach ($b_models as $b_model => $throw_away) + { + if (!isset($this->bmodel_index[$b_model])) + { + $bmodel = new BatteryModel(); + $bmodel->setName($b_model); + $em->persist($bmodel); + } + else + $bmodel = $this->bmodel_index[$b_model]; + + $b_models[$b_model] = $bmodel; + } + + // batteries + $bmfg = $em->getRepository(BatteryManufacturer::class)->find(1); + foreach ($batteries as $b_size => $batt_models) + { + if (!isset($this->bsize_index[$b_size])) + { + $bsize = new BatterySize(); + $bsize->setName($b_size); + $em->persist($bsize); + } + else + $bsize = $this->bsize_index[$b_size]; + + foreach ($batt_models as $b_model => $vehicle_rows) + { + $bmodel = $b_models[$b_model]; + + if (!isset($this->b_index[$bsize->getName() . '|' . $bmodel->getName()])) + { + $battery = new Battery(); + $battery->setManufacturer($bmfg) + ->setModel($bmodel) + ->setSize($bsize) + ->setProductCode('') + ->setSAPCode('') + ->setWarrantyPrivate(21) + ->setWarrantyCommercial(6) + ->setSellingPrice(5000); + + $em->persist($battery); + } + else + $battery = $this->b_index[$bsize->getName() . '|' . $bmodel->getName()]; + + // link to vehicles + foreach ($vehicle_rows as $row_num) + { + $vehicle = $vehicle_index[$row_num]; + $battery->addVehicle($vehicle); + } + } + } + + $em->flush(); + + // print_r($batteries); + } +}