Modify the ImportSAPDeltaCommand to make import checking easier. #504

This commit is contained in:
Korina Cordero 2020-09-23 04:47:15 +00:00
parent 7c49bd9f0a
commit beee87c3b4

View file

@ -71,6 +71,7 @@ class ImportSAPDeltaCommand extends Command
$added_brands = [];
$added_sizes = [];
$added_batteries = [];
$total_processed = 0;
while (($fields = fgetcsv($fh)) !== false)
{
// data starts at row 2
@ -105,6 +106,8 @@ class ImportSAPDeltaCommand extends Command
// 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;
}
@ -118,7 +121,8 @@ class ImportSAPDeltaCommand extends Command
if (!isset($added_brands[$clean_brand]))
{
// add to list of existing brands
$existing_brands[$clean_brand] = $sap_brand;
//$output->writeln('Brand already in db ' . $clean_brand);
$existing_brands[$clean_brand] = $clean_brand;
}
}
@ -135,6 +139,8 @@ class ImportSAPDeltaCommand extends Command
// 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;
}
@ -148,7 +154,8 @@ class ImportSAPDeltaCommand extends Command
if (!isset($added_sizes[$clean_size]))
{
// add to list of existing sizes
$existing_sizes[$clean_size] = $sap_size;
//$output->writeln('Size already in db ' . $clean_size);
$existing_sizes[$clean_size] = $clean_size;
}
}
@ -164,6 +171,8 @@ class ImportSAPDeltaCommand extends Command
// 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;
}
@ -175,10 +184,16 @@ class ImportSAPDeltaCommand extends Command
if (!isset($added_batteries[$clean_sku]))
{
// add to list of existing batteries
//$output->writeln('Battery already in db ' . $clean_sku);
$existing_batteries[$clean_sku] = $this->sap_battery_hash[$clean_sku];
if (!isset($existing_brands[$clean_brand]))
$existing_brands[$clean_brand] = $clean_brand;
if (!isset($existing_sizes[$clean_size]))
$existing_sizes[$clean_size] = $clean_size;
}
}
$total_processed++;
$row_num++;
}
@ -187,7 +202,7 @@ class ImportSAPDeltaCommand extends Command
// need to output the list of added and not added data
$this->writeOutputFile($report_file, $added_brands, $added_sizes, $added_batteries,
$existing_brands, $existing_sizes, $existing_batteries);
$existing_brands, $existing_sizes, $existing_batteries, $total_processed);
return 0;
}
@ -229,7 +244,7 @@ class ImportSAPDeltaCommand extends Command
}
protected function writeOutputFile($report_file, $added_brands, $added_sizes, $added_batteries,
$existing_brands, $existing_sizes, $existing_batteries)
$existing_brands, $existing_sizes, $existing_batteries, $total_processed)
{
try
{
@ -240,10 +255,16 @@ class ImportSAPDeltaCommand extends Command
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, 'Total number of brands in csv file that are in the system: ' . count($existing_brands) . "\n");
fputs($fh, 'Total number of sizes in csv file that are in the system: ' . count($existing_sizes) . "\n");
fputs($fh, 'Total number of batteries in csv file that are in the system: ' . count($existing_batteries) . "\n");
// write the added batteries
// check the existing brands array for the added ones
$not_added_batteries = [];
@ -252,11 +273,6 @@ class ImportSAPDeltaCommand extends Command
{
fputs($fh, $added_battery, strlen($added_battery));
fputs($fh, "\n");
if (isset($existing_batteries[$added_battery]))
{
$not_added_batteries[] = $added_battery;
}
}
// write the added brands
@ -267,11 +283,6 @@ class ImportSAPDeltaCommand extends Command
{
fputs($fh, $added_brand, strlen($added_brand));
fputs($fh, "\n");
if (isset($existing_brands[$added_brand]))
{
$not_added_brands[] = $added_brand;
}
}
// write the added sizes
@ -282,24 +293,19 @@ class ImportSAPDeltaCommand extends Command
{
fputs($fh, $added_size, strlen($added_size));
fputs($fh, "\n");
if (isset($existing_sizes[$added_size]))
{
$not_added_sizes[] = $added_size;
}
}
// write the not added batteries
fputs($fh, 'Batteries already in system: ' . "\n");
foreach($not_added_batteries as $not_added_battery)
foreach($existing_batteries as $not_added_battery)
{
fputs($fh, $not_added_battery, strlen($not_added_battery));
fputs($fh, $not_added_battery->getID(), strlen($not_added_battery->getID()));
fputs($fh, "\n");
}
// write the not added brands
fputs($fh, 'Brands already in system: ' . "\n");
foreach($not_added_brands as $not_added_brand)
foreach($existing_brands as $not_added_brand)
{
fputs($fh, $not_added_brand, strlen($not_added_brand));
fputs($fh, "\n");
@ -307,7 +313,7 @@ class ImportSAPDeltaCommand extends Command
// write the not added sizes
fputs($fh, 'Sizes already in system: ' . "\n");
foreach($not_added_sizes as $not_added_size)
foreach($existing_sizes as $not_added_size)
{
fputs($fh, $not_added_size, strlen($not_added_size));
fputs($fh, "\n");