Compare commits
2 commits
master
...
502-resq-n
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae6c2da1de | ||
|
|
bb06937a44 |
1 changed files with 80 additions and 17 deletions
|
|
@ -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,49 @@ 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]))
|
||||
{
|
||||
$size = new SAPBatterySize();
|
||||
$size->setName($clean_size);
|
||||
$size->setName(strtoupper($clean_size));
|
||||
|
||||
$em->persist($size);
|
||||
|
||||
$this->batt_size_hash[$clean_size] = $size;
|
||||
|
||||
// add to list of added sizes
|
||||
error_log('Adding size ' . strtoupper($clean_size));
|
||||
$added_sizes[strtoupper($clean_size)] = strtoupper($clean_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
$dupe_sizes[] = $clean_size;
|
||||
$dupe_sizes[] = strtoupper($clean_size);
|
||||
}
|
||||
|
||||
// battery hash
|
||||
|
|
@ -129,6 +140,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 +160,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 +203,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 +215,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 +236,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue