diff --git a/src/Command/ImportSAPBatteryCommand.php b/src/Command/ImportSAPBatteryCommand.php index 367948ee..2a4db8d2 100644 --- a/src/Command/ImportSAPBatteryCommand.php +++ b/src/Command/ImportSAPBatteryCommand.php @@ -74,6 +74,9 @@ class ImportSAPBatteryCommand extends Command $dupe_brands = []; $dupe_sizes = []; $dupe_batteries = []; + $added_brands = []; + $added_sizes = []; + $added_batteries = []; // loop through rows and build hashes while (($fields = fgetcsv($handle)) !== false) { @@ -82,41 +85,70 @@ class ImportSAPBatteryCommand extends Command continue; // clean up fields - $clean_brand = trim($fields[0]); + $clean_brand = $this->normalizeName(trim($fields[0])); $clean_sku = strtoupper(trim($fields[1])); - $clean_size = trim($fields[2]); + $clean_size = $this->normalizeName(trim($fields[2])); - $output->writeln("Parsing $clean_sku..."); + //$output->writeln("Parsing $clean_sku..."); // brand hash if (!isset($this->batt_brand_hash[$clean_brand])) { $brand = new SAPBatteryBrand(); - $brand->setName($clean_brand); + $brand->setName(strtoupper($clean_brand)); $em->persist($brand); $this->batt_brand_hash[$clean_brand] = $brand; + + // add to list of added brands + error_log('Adding brand ' . strtoupper($clean_brand)); + $added_brands[strtoupper($clean_brand)] = strtoupper($clean_brand); } else { // add to duplicate list - $dupe_brands[] = $clean_brand; + $dupe_brands[] = strtoupper($clean_brand); } - // size hash - if (!isset($this->batt_size_hash[$clean_size])) + // need to check if size has / + $pos = stripos($clean_size, '/'); + $clean_sizes = []; + if ($pos == false) { - $size = new SAPBatterySize(); - $size->setName($clean_size); - - $em->persist($size); - - $this->batt_size_hash[$clean_size] = $size; + // no '/' in size + $clean_sizes[] = $this->normalizeName($clean_size); } else { - $dupe_sizes[] = $clean_size; + // need to explode + $sizes = explode('/', $clean_size); + foreach ($sizes as $size) + { + $clean_sizes[] = $this->normalizeName(trim($size)); + } + } + + foreach ($clean_sizes as $c_size) + { + // size hash + if (!isset($this->batt_size_hash[$c_size])) + { + $size = new SAPBatterySize(); + $size->setName(strtoupper($c_size)); + + $em->persist($size); + + $this->batt_size_hash[$c_size] = $size; + + // add to list of added sizes + error_log('Adding size ' . strtoupper($c_size)); + $added_sizes[strtoupper($c_size)] = strtoupper($c_size); + } + else + { + $dupe_sizes[] = strtoupper($c_size); + } } // battery hash @@ -129,6 +161,12 @@ class ImportSAPBatteryCommand extends Command ->setBrand($this->batt_brand_hash[$clean_brand]); $em->persist($battery); + + $this->battery_hash[$clean_sku] = $battery; + + // add to list of added batteries + error_log('Adding battery ' . $clean_sku); + $added_batteries[$clean_sku] = $clean_sku; } else { @@ -143,7 +181,29 @@ class ImportSAPBatteryCommand extends Command (count($dupe_sizes) > 0) || (count($dupe_batteries) > 0)) { - $this->writeDupeReport($report_file, $dupe_brands, $dupe_sizes, $dupe_batteries); + $this->writeDupeReport($report_file, $dupe_brands, $dupe_sizes, $dupe_batteries, + $added_brands, $added_sizes, $added_batteries); + } + + error_log('Total added brands: ' . count($added_brands)); + error_log('Added brands: '); + foreach ($added_brands as $a_brand) + { + error_log($a_brand); + } + + error_log('Total added sizes: ' . count($added_sizes)); + error_log('Added sizes: '); + foreach ($added_sizes as $a_size) + { + error_log($a_size); + } + + error_log('Total added codes: ' . count($added_batteries)); + error_log('Added codes: '); + foreach ($added_batteries as $a_battery) + { + error_log($a_battery); } return 0; @@ -164,7 +224,10 @@ class ImportSAPBatteryCommand extends Command $sizes = $this->em->getRepository(SAPBatterySize::class)->findAll(); foreach ($sizes as $size) - $this->batt_size_hash[$size->getName()] = $size; + { + $name = $this->normalizeName($size->getName()); + $this->batt_size_hash[$name] = $size; + } } protected function initBatteryBrandHash() @@ -173,10 +236,14 @@ class ImportSAPBatteryCommand extends Command $brands = $this->em->getRepository(SAPBatteryBrand::class)->findAll(); foreach ($brands as $brand) - $this->batt_brand_hash[$brand->getName()] = $brand; + { + $name = $this->normalizeName($brand->getName()); + $this->batt_brand_hash[$name] = $brand; + } } - protected function writeDupeReport($report_file, $brands, $sizes, $batts) + protected function writeDupeReport($report_file, $brands, $sizes, $batts, + $added_brands, $added_sizes, $added_batteries) { try { @@ -190,25 +257,42 @@ class ImportSAPBatteryCommand extends Command fputs($fh, 'Brands Already in Database: ' . "\n"); foreach ($brands as $brand) { - fputs($fh, $brand, strlen($brand)); - fputs($fh, "\n"); + if (!isset($added_brands[$brand])) + { + fputs($fh, $brand, strlen($brand)); + fputs($fh, "\n"); + } } fputs($fh, 'Sizes Already in Database: ' . "\n"); foreach ($sizes as $size) { - fputs($fh, $size, strlen($size)); - fputs($fh, "\n"); + if (!isset($added_sizes[$size])) + { + fputs($fh, $size, strlen($size)); + fputs($fh, "\n"); + } } fputs($fh, 'SAP Codes Already in Database: ' . "\n"); foreach ($batts as $batt) { - fputs($fh, $batt, strlen($batt)); - fputs($fh, "\n"); + if (!isset($added_batteries[$batt])) + { + fputs($fh, $batt, strlen($batt)); + fputs($fh, "\n"); + } } fclose($fh); } + + protected function normalizeName($name) + { + $normalized_key = trim(strtolower($name)); + + return $normalized_key; + } + }