em = $em; $this->initSAPBatteryHash(); $this->initSAPBatterySizeHash(); $this->initSAPBatteryBrandHash(); parent::__construct(); } protected function configure() { $this->setName('sap_battery:master') ->setDescription('Import SAP battery master list.') ->setHelp('Creates SAP battery, SAP battery brands, and SAP battery sizes from CSV file.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.') ->addArgument('output_file', InputArgument::REQUIRED, 'Path to the output CSV file.'); } protected function execute(InputInterface $input, OutputInterface $output) { $csv_file = $input->getArgument('file'); $report_file = $input->getArgument('output_file'); // CSV column order: // 0 - brand // 1 - sku // 2 - size // attempt to open file try { $fh = fopen($csv_file, "r"); } catch (Exception $e) { throw new Exception('The file "' . $csv_file . '" could not be read.'); } // get entity manager $em = $this->em; // loop through the rows $row_num = 0; error_log('Processing sap battery csv file...'); $added_brands = []; $added_sizes = []; $added_batteries = []; $repeat_brands = []; $repeat_sizes = []; $repeat_batteries = []; $total_processed = 0; while (($fields = fgetcsv($fh)) !== false) { // data starts at row 2 if ($row_num < 2) { $row_num++; continue; } // check if blank row if (strlen(trim($fields[0])) == 0) continue; // clean up fields $clean_brand = $this->normalizeName(trim($fields[0])); $clean_sku = $this->normalizeName(trim($fields[1])); $clean_size = $this->normalizeName(trim($fields[2])); // check if brand in system $sap_brand = null; if (!isset($this->sap_battery_brand_hash[$clean_brand])) { // add brand $sap_brand = new SAPBatteryBrand(); $sap_brand->setName($clean_brand); $em->persist($sap_brand); // add to list of added brands $added_brands[$clean_brand] = $clean_brand; //$output->writeln('Adding brand: ' . $clean_brand); // add to hash $this->sap_battery_brand_hash[$clean_brand] = $sap_brand; } else { // get the brand $sap_brand = $this->sap_battery_brand_hash[$clean_brand]; // repeat brand so no need to add but still need to count $repeat_brands[] = $clean_brand; } // check if size in system $sap_size = null; if (!isset($this->sap_battery_size_hash[$clean_size])) { // add size $sap_size = new SAPBatterySize(); $sap_size->setName($clean_size); $em->persist($sap_size); // add to list of added sizes $added_sizes[$clean_size] = $clean_size; //$output->writeln('Adding size: ' . $clean_size); // add to hash $this->sap_battery_size_hash[$clean_size] = $sap_size; } else { // get the size $sap_size = $this->sap_battery_size_hash[$clean_size]; // repeat size so no need to add to system but still need to count $repeat_sizes[] = $clean_size; } // check if sap battery code already exists if (!isset($this->sap_battery_hash[$clean_sku])) { // add the sap_battery // create sap battery entry $sap_battery = new SAPBattery(); $sap_battery->setID($clean_sku) ->setSize($sap_size) ->setBrand($sap_brand); $em->persist($sap_battery); // add to list of added batteries $added_batteries[$clean_sku] = $clean_sku; //$output->writeln('Adding battery: ' . $clean_sku); // add to hash $this->sap_battery_hash[$clean_sku] = $sap_battery; } else { $repeat_batteries[] = $clean_sku; } $total_processed++; $row_num++; } $em->flush(); // need to output the list of added and not added data $this->writeOutputFile($report_file, $added_brands, $added_sizes, $added_batteries, $total_processed, $repeat_brands, $repeat_sizes, $repeat_batteries); return 0; } protected function initSAPBatteryHash() { $this->sap_battery_hash = []; $batts = $this->em->getRepository(SAPBattery::class)->findAll(); foreach ($batts as $batt) { $id = $this->normalizeName($batt->getID()); $this->sap_battery_hash[$id] = $batt; } } protected function initSAPBatterySizeHash() { $this->sap_battery_size_hash = []; $sizes = $this->em->getRepository(SAPBatterySize::class)->findAll(); foreach ($sizes as $size) { $name = $this->normalizeName($size->getName()); $this->sap_battery_size_hash[$name] = $size; } } protected function initSAPBatteryBrandHash() { $this->sap_battery_brand_hash = []; $brands = $this->em->getRepository(SAPBatteryBrand::class)->findAll(); foreach ($brands as $brand) { $name = $this->normalizeName($brand->getName()); $this->sap_battery_brand_hash[$name] = $brand; } } protected function writeOutputFile($report_file, $added_brands, $added_sizes, $added_batteries, $total_processed, $repeat_brands, $repeat_sizes, $repeat_batteries) { try { $fh = fopen($report_file, "w"); } catch (Exception $e) { throw new Exception('The file "' . $report_file . '" could be opened.'); } fputs($fh, 'Total entries processed: ' . $total_processed . "\n"); fputs($fh, 'Total brands added: ' . count($added_brands) . "\n"); fputs($fh, 'Total sizes added: ' . count($added_sizes) . "\n"); fputs($fh, 'Total batteries added: ' . count($added_batteries) . "\n"); fputs($fh, 'Repeat brands: ' . count($repeat_brands) . "\n"); fputs($fh, 'Repeat sizes: ' . count($repeat_sizes) . "\n"); fputs($fh, 'Repeat batteries: ' . count($repeat_batteries) . "\n"); foreach ($repeat_batteries as $repeat_battery) { fputs($fh, 'Duplicate SKU: ' . $repeat_battery . "\n"); } } protected function normalizeName($name) { $normalized_key = trim(strtoupper($name)); return $normalized_key; } }