diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index 98c72782..d71a1307 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -9,26 +9,16 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; +use App\Entity\Battery; use App\Entity\BatteryManufacturer; use App\Entity\BatteryModel; use App\Entity\BatterySize; -use App\Entity\Battery; -use App\Entity\VehicleManufacturer; -use App\Entity\Vehicle; class ImportCMBBatteryDataCommand extends Command { - // field index in csv file - const F_VEHICLE_MANUFACTURER = 1; - const F_VEHICLE_MAKE = 2; - const F_VEHICLE_YEAR = 3; - const F_BATT_SDFC = 4; - const F_BATT_ULTRAMAX = 5; - const F_BATT_MOTOLITE = 6; - const F_BATT_MARATHONER = 7; - const F_BATT_EXCEL = 8; - - const STR_PRESENT = 'Present'; + const F_BATT_CODE = 1; + const F_BATT_DESC = 2; + const F_BATT_PRICE = 3; protected $em; @@ -37,22 +27,15 @@ class ImportCMBBatteryDataCommand extends Command protected $bsize_hash; protected $batt_hash; - protected $vmanu_hash; - protected $vmake_hash; - public function __construct(ObjectManager $om) { $this->em = $om; - // load existing battery data + // load existing batteries and sizes $this->loadBatteryManufacturers(); $this->loadBatteryModels(); - $this->loadBatterySizes(); $this->loadBatteries(); - - // load existing vehicle data - $this->loadVehicleManufacturers(); - $this->loadVehicleMakes(); + $this->loadBatterySizes(); parent::__construct(); } @@ -60,8 +43,8 @@ class ImportCMBBatteryDataCommand extends Command protected function configure() { $this->setName('cmbbatterydata:import') - ->setDescription('Retrieve from a CSV file battery and vehicle information.') - ->setHelp('Creates battery manufacturers, models, sizes, vehicle makes, and models based on data from imported CSV.') + ->setDescription('Import a CSV file with battery data.') + ->setHelp('Adds the battery data based on imported CSV.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); } @@ -82,172 +65,91 @@ class ImportCMBBatteryDataCommand extends Command // get entity manager $em = $this->em; - // find the battery manufacturer row = 2nd row + // loop through the rows $row_num = 0; - - // brand names - $brand_sdfc = ''; - $brand_ultramax = ''; - $brand_motolite = ''; - $brand_marathoner = ''; - $brand_excel = ''; - + error_log('Processing battery csv file...'); while (($fields = fgetcsv($fh)) !== false) { - $comp_batteries = []; - if ($row_num < 1) + // data starts at row 2 + if ($row_num < 2) { $row_num++; continue; } // battery info - $sdfc = trim($fields[self::F_BATT_SDFC]); - $ultramax = 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]); + $code = trim($fields[self::F_BATT_CODE]); + $desc = trim($fields[self::F_BATT_DESC]); + $price = trim($fields[self::F_BATT_PRICE]); - // vehicle info - $manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]); - $make = trim($fields[self::F_VEHICLE_MAKE]); - $year = trim($fields[self::F_VEHICLE_YEAR]); + $clean_price = trim($price, '$'); - // get battery manufacturer when row_num == 1 - if ($row_num == 1) + $battery_info = explode(' ', $desc); + + // if battery_info has 3 elements, get the last two + // [0] = battery manufacturer + // [1] = battery model + // [2] = battery size + // if only 2, get both + // [0] = battery manufacturer and battery model + // [1] = battery size + // if 4, + // [0] = battery manufacturer + // concatenate [1] and [2] for the battery model + // [3] = battery size + $battery_manufacturer = ''; + $battery_model = ''; + $battery_size = ''; + if (count($battery_info) == 3) { - // store the brand names to be used when adding a battery - $brand_sdfc = $sdfc; - $brand_ultramax = $ultramax; - $brand_motolite = $motolite; - $brand_marathoner = $marathoner; - $brand_excel = $excel; - - // check if manufacturer has been added - if (!isset($this->bmanu_hash[$sdfc])) - $this->addBatteryManufacturer($sdfc); - - if (!isset($this->bmanu_hash[$ultramax])) - $this->addBatteryManufacturer($ultramax); - - if (!isset($this->bmanu_hash[$motolite])) - $this->addBatteryManufacturer($motolite); - - if (!isset($this->bmanu_hash[$marathoner])) - $this->addBatteryManufacturer($marathoner); - - if (!isset($this->bmanu_hash[$excel])) - $this->addBatteryManufacturer($excel); - - // right now, manufacturer == model - // check if model has been added - if (!isset($this->bmodel_hash[$sdfc])) - $this->addBatteryModel($sdfc); - - if (!isset($this->bmodel_hash[$ultramax])) - $this->addBatteryModel($ultramax); - - if (!isset($this->bmodel_hash[$motolite])) - $this->addBatteryModel($motolite); - - if (!isset($this->bmodel_hash[$marathoner])) - $this->addBatteryModel($marathoner); - - if (!isset($this->bmodel_hash[$excel])) - $this->addBatteryModel($excel); - - $row_num++; + // sample: Century Marathoner 120-7L + $battery_manufacturer = trim($battery_info[0]); + $battery_model = trim($battery_info[1]); + $battery_size = trim($battery_info[2]); } - else + if (count($battery_info) == 2) { - // vehicle and battery data - - // battery size - // check if battery size has been added - if (!isset($this->bsize_hash[$sdfc])) - $this->addBatterySize($sdfc); - - if (!isset($this->bsize_hash[$ultramax])) - $this->addBatterySize($ultramax); - - if (!isset($this->bsize_hash[$motolite])) - $this->addBatterySize($motolite); - - if (!isset($this->bsize_hash[$marathoner])) - $this->addBatterySize($marathoner); - - if (!isset($this->bsize_hash[$excel])) - $this->addBatterySize($excel); - - // check if battery has been added - if (!isset($this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc])) - { - if (!(empty($sdfc))) - { - $this->addBattery($brand_sdfc, $sdfc); - } - } - - if (!isset($this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax])) - { - if (!(empty($ultramax))) - { - $this->addBattery($brand_ultramax, $ultramax); - } - } - - if (!isset($this->batt_hash[$brand_motolite][$brand_motolite][$motolite])) - { - if (!(empty($motolite))) - { - $this->addBattery($brand_motolite, $motolite); - } - } - - if (!isset($this->batt_hash[$brand_marathoner][$brand_marathoner][$marathoner])) - { - if (!(empty($marathoner))) - { - $this->addBattery($brand_marathoner, $marathoner); - } - } - - if (!isset($this->batt_hash[$brand_excel][$brand_excel][$excel])) - { - if (!(empty($excel))) - { - $this->addBattery($brand_excel, $excel); - } - } - - // vehicle data - // check if vehicle manufacturer has been added - if (!isset($this->vmanu_hash[$manufacturer])) - $this->addVehicleManufacturer($manufacturer); - - // check if vehicle make has been added - if (!isset($this->vmake_hash[$manufacturer][$make])) - { - if (!(empty($sdfc))) - $comp_batteries[] = $this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc]; - - if (!(empty($ultramax))) - $comp_batteries[] = $this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax]; - - if (!(empty($motolite))) - $comp_batteries[] = $this->batt_hash[$brand_motolite][$brand_motolite][$motolite]; - - if (!(empty($marathoner))) - $comp_batteries[] = $this->batt_hash[$brand_marathoner][$brand_marathoner][$marathoner]; - - if (!(empty($excel))) - $comp_batteries[] = $this->batt_hash[$brand_excel][$brand_excel][$excel]; - - $this->addVehicleMake($manufacturer, $make, $year, $comp_batteries); - } - - $row_num++; + // sample: Marshall DIN55R + $battery_manufacturer = trim($battery_info[0]); + $battery_model = trim($battery_info[0]); + $battery_size = trim($battery_info[1]); } + if (count($battery_info) == 4) + { + // sample: Motolite Classic Wetcharged DIN100L + $battery_manufacturer = trim($battery_info[0]); + $battery_model = trim($battery_info[1]) . ' ' . trim($battery_info[2]); + $battery_size = trim($battery_info[3]); + } + + //error_log('battery manufacturer ' . $battery_manufacturer); + //error_log('battery model ' . $battery_model); + //error_log('battery size ' . $battery_size); + + // save battery manufacturer if not yet in system + if (!isset($this->bmanu_hash[$battery_manufacturer])) + { + $this->addBatteryManufacturer($battery_manufacturer); + } + + // save battery model if not yet in system + if (!isset($this->bmodel_hash[$battery_model])) + { + $this->addBatteryModel($battery_model); + } + + // save battery size if not yet in system + if (!isset($this->bsize_hash[$battery_size])) + { + $this->addBatterySize($battery_size); + } + + // save battery if not yet in system + if (!isset($this->batt_hash[$battery_manufacturer][$battery_model][$battery_size])) + { + $this->addBattery($battery_manufacturer, $battery_model, $battery_size, $code, $clean_price); + } + } } @@ -294,10 +196,10 @@ class ImportCMBBatteryDataCommand extends Command } } - protected function addBattery($brand, $size) + protected function addBattery($manufacturer, $brand, $size, $code, $price) { // save to db - $bmanu = $this->bmanu_hash[$brand]; + $bmanu = $this->bmanu_hash[$manufacturer]; $bmodel = $this->bmodel_hash[$brand]; $bsize = $this->bsize_hash[$size]; @@ -307,7 +209,10 @@ class ImportCMBBatteryDataCommand extends Command ->setSize($bsize) ->setWarrantyPrivate(21) ->setWarrantyCommercial(6) - ->setWarrantyTnv(12); + ->setWarrantyTnv(12) + ->setProductCode($code) + ->setSAPCode($code) + ->setSellingPrice($price); $this->em->persist($battery); $this->em->flush(); @@ -327,67 +232,6 @@ class ImportCMBBatteryDataCommand extends Command $this->em->flush(); } - protected function addVehicleManufacturer($name) - { - // save to db - $vehicle_manufacturer = new VehicleManufacturer(); - - $vehicle_manufacturer->setName($name); - - $this->em->persist($vehicle_manufacturer); - $this->em->flush(); - - // add to hash - $this->vmanu_hash[$name] = $vehicle_manufacturer; - } - - protected function addVehicleMake($manufacturer, $make, $year, $batteries) - { - // save to db - $vehicle = new Vehicle(); - - $vmanu = $this->vmanu_hash[$manufacturer]; - - // parse year from and year to - $year_from = ''; - $year_to = ''; - - if (!empty($year)) - { - $model_years = explode('-', $year); - $year_from = $model_years[0]; - if (!empty($year_to)) - $year_to = $model_years[1]; - - // check if $year_to is the string "Present" - // if so, set to 0, for now - if ($year_to == self::STR_PRESENT) - $year_to = 0; - } - - $vehicle->setManufacturer($vmanu) - ->setMake($make) - ->setModelYearFrom($year_from) - ->setModelYearTo($year_to); - - // add vehicle to battery - foreach ($batteries as $battery) - { - $battery->addVehicle($vehicle); - $this->em->persist($battery); - } - - // add vehicle to manufacturer - $vmanu->addVehicle($vehicle); - - $this->em->persist($vmanu); - $this->em->persist($vehicle); - $this->em->flush(); - - // add to hash - $this->vmake_hash[$manufacturer][$make] = $vehicle; - } - protected function loadBatteryManufacturers() { $this->bmanu_hash = []; @@ -439,30 +283,5 @@ class ImportCMBBatteryDataCommand extends Command } } - protected function loadVehicleManufacturers() - { - $this->vmanu_hash = []; +} - $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); - foreach ($vmanus as $vmanu) - { - $name = $vmanu->getName(); - $this->vmanu_hash[$name] = $vmanu; - } - } - - protected function loadVehicleMakes() - { - $this->vmake_hash = []; - - $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); - foreach ($vmakes as $vmake) - { - $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); - - $this->vmake_hash[$manufacturer][$make] = $vmake; - } - } - -} diff --git a/src/Command/ImportCMBBatteryPriceCommand.php b/src/Command/ImportCMBBatteryPriceCommand.php deleted file mode 100644 index 9af64b1d..00000000 --- a/src/Command/ImportCMBBatteryPriceCommand.php +++ /dev/null @@ -1,236 +0,0 @@ -em = $om; - - // load existing batteries and sizes - $this->loadBatteryModels(); - $this->loadBatteries(); - $this->loadBatterySizes(); - - parent::__construct(); - } - - protected function configure() - { - $this->setName('cmbbatterydata:importprice') - ->setDescription('Import a CSV file with battery prices.') - ->setHelp('Adds the battery prices to existing batteries based on 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; - - // loop through the rows - $row_num = 0; - error_log('Processing battery price csv file...'); - while (($fields = fgetcsv($fh)) !== false) - { - // data starts at row 2 - if ($row_num < 2) - { - $row_num++; - continue; - } - - // battery price info - $desc = trim($fields[self::F_BATT_DESC]); - $price = trim($fields[self::F_BATT_PRICE]); - - $clean_price = trim($price, '$'); - - $battery_info = explode(' ', $desc); - - // if battery_info has 3 elements, get the last two - // if only 2, get both - // if 4, get the first and the last - $battery_model = ''; - $battery_size = ''; - if (count($battery_info) == 3) - { - // sample: Century Marathoner 120-7L - $battery_model = trim($battery_info[1]); - $battery_size = trim($battery_info[2]); - } - if (count($battery_info) == 2) - { - // sample: Marshall DIN55R - $battery_model = trim($battery_info[0]); - $battery_size = trim($battery_info[1]); - } - if (count($battery_info) == 4) - { - // sample: Motolite Classic Wetcharged DIN100L - $battery_model = trim($battery_info[0]); - $battery_size = trim($battery_info[3]); - } - - // find the battery size - $batt_size = $this->findBatterySize($battery_model, $battery_size); - - // add checking if null is returned - if ($batt_size != null) - { - //error_log('battery model ' . $battery_model); - //error_log('battery size ' . $batt_size); - - // get the battery - if (!isset($this->batt_hash[$battery_model][$battery_model][$batt_size])) - { - error_log('No battery in system with model ' . $battery_model . ' and size ' . $batt_size); - } - else - { - $batt = $this->batt_hash[$battery_model][$battery_model][$batt_size]; - - // set battery price - if ($batt != null) - { - $batt->setSellingPrice($clean_price); - - $this->em->persist($batt); - $this->em->flush(); - } - } - } - else - { - error_log('Cannot find battery with model ' . $battery_model . ' and size ' . $battery_size); - } - } - } - - protected function findBatterySize($bmodel, $bsize) - { - $batt_size = null; - // check if model is valid - if (!isset($this->bmodel_hash[$bmodel])) - { - return null; - } - - // check if battery size has parenthesis - $pos = stripos($bsize, '('); - if ($pos == true) - { - // parse battery size because of Q85(insert string here) and M42? M-42?(insert string here) - // explode and get the first element - $batts = explode('(', $bsize); - $bsize = trim($batts[0]); - //error_log('new battery size ' . $bsize); - } - - // check if size is set - if (!isset($this->batt_hash[$bmodel][$bmodel][$bsize])) - { - // loop through the hash since the size might be part of the sizes with '/' - foreach ($this->bsize_hash as $key => $data) - { - $pos = stripos($key, '/'); - if ($pos == true) - { - // explode the key - $key_strings = explode('/', $key); - foreach ($key_strings as $ks) - { - $clean_ks = trim($ks); - if (strcasecmp($bsize, $clean_ks) == 0) - { - // bsize is one of the sizes with '/' - $batt_size = $key; - return $batt_size; - } - } - } - } - } - else - { - $batt_size = $bsize; - } - - return $batt_size; - } - - 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; - } - } - - protected function loadBatteries() - { - $this->batt_hash = []; - - $batts = $this->em->getRepository(Battery::class)->findAll(); - foreach ($batts as $batt) - { - $brand = $batt->getManufacturer()->getName(); - $model = $batt->getModel()->getName(); - $size = $batt->getSize()->getName(); - - $this->batt_hash[$brand][$model][$size] = $batt; - } - } - -} -