From a29ea8756bcb2cd760bcb643d54a132bd2878b9d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Oct 2019 07:41:19 +0000 Subject: [PATCH] Add command to add batteries. #270 --- src/Command/ImportBatteriesCommand.php | 174 +++++++++++++++++++++++++ src/Entity/Battery.php | 5 +- 2 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 src/Command/ImportBatteriesCommand.php diff --git a/src/Command/ImportBatteriesCommand.php b/src/Command/ImportBatteriesCommand.php new file mode 100644 index 00000000..a7c78cc3 --- /dev/null +++ b/src/Command/ImportBatteriesCommand.php @@ -0,0 +1,174 @@ +em = $om; + + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('battery:create') + ->setDescription('Retrieve from a CSV file battery information.') + ->setHelp('Creates batteries based on data from imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // 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; + + // go through the file + // data starts at row 3 + $row_num = 0; + $sdfc = ''; + $ultra = ''; + $motolite = ''; + $marathoner = ''; + $excel = ''; + + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get manufacturer names + if ($row_num == 1) + { + $sdfc = trim($fields[self::F_BATT_SDFC]); + $ultra = trim($fields[self::F_BATT_ULTRAMAX]); + $motolite = trim($fields[self::F_BATT_MOTOLITE]); + $marathoner = trim($fields[self::F_BATT_MARATHONER]); + $excel = trim($fields[self::F_BATT_EXCEL]); + + $row_num++; + } + else + { + $sdfc_size = trim($fields[self::F_BATT_SDFC]); + $ultra_size = trim($fields[self::F_BATT_ULTRAMAX]); + $motolite_size = trim($fields[self::F_BATT_MOTOLITE]); + $marathoner_size = trim($fields[self::F_BATT_MARATHONER]); + $excel_size = trim($fields[self::F_BATT_EXCEL]); + + //$output->writeln('moogle batt brand ' . $sdfc); + //$output->writeln('moogle batt size ' . $sdfc_size); + + // check if battery has been added + if (!isset($this->batt_hash[$sdfc][$sdfc][$sdfc_size])) + { + //$output->writeln('moogle new batt'); + if (!(empty($sdfc_size))) + { + //$output->writeln('moogle about to add'); + $this->addBattery($sdfc, $sdfc_size); + } + } + } + } + } + + protected function addBattery($brand, $size) + { + // save to db + $battery = new Battery(); + $battery->setManufacturer($this->bmanu_hash[$brand]) + ->setModel($this->bmodel_hash[$brand]) + ->setSize($this->bsize_hash[$size]) + ->setWarrantyPrivate(21) + ->setWarrantyCommercial(6) + ->setWarrantyTnv(12); + + $this->em->persist($battery); + $this->em->flush(); + + + // insert into hash + $this->batt_hash[$brand][$brand][$size] = $battery; + } + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $batt_manu->getName(); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $batt_model->getName(); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $batt_size->getName(); + $this->bsize_hash[$name] = $batt_size; + } + } +} diff --git a/src/Entity/Battery.php b/src/Entity/Battery.php index e05bff81..4c78fb08 100644 --- a/src/Entity/Battery.php +++ b/src/Entity/Battery.php @@ -60,14 +60,13 @@ class Battery // product code /** - * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() + * @ORM\Column(type="string", length=80, nullable=true) */ protected $prod_code; // sap code /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=80, nullable=true) */ protected $sap_code;