Add ImportSAPDeltaCommand. #504

This commit is contained in:
Korina Cordero 2020-09-22 11:17:25 +00:00
parent d42723955a
commit 7c49bd9f0a

View file

@ -0,0 +1,325 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\SAPBattery;
use App\Entity\SAPBatteryBrand;
use App\Entity\SAPBatterySize;
class ImportSAPDeltaCommand extends Command
{
private $em;
private $sap_battery_hash;
private $sap_battery_size_hash;
private $sap_battery_brand_hash;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->initSAPBatteryHash();
$this->initSAPBatterySizeHash();
$this->initSAPBatteryBrandHash();
parent::__construct();
}
protected function configure()
{
$this->setName('sap_battery:delta')
->setDescription('Update SAP battery table with new SAP battery data.')
->setHelp('Creates SAP battery data entries 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...');
$existing_brands = [];
$existing_sizes = [];
$existing_batteries = [];
$added_brands = [];
$added_sizes = [];
$added_batteries = [];
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 sap battery code already exists
if (!isset($this->sap_battery_hash[$clean_sku]))
{
// 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;
// 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];
// need to check if this is in added_brands because the hash contains
// existing + added. What we need is the list of existing only
if (!isset($added_brands[$clean_brand]))
{
// add to list of existing brands
$existing_brands[$clean_brand] = $sap_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;
// 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];
// need to check if this is in added_sizes because the hash contains
// existing + added. What we need is the list of existing only
if (!isset($added_sizes[$clean_size]))
{
// add to list of existing sizes
$existing_sizes[$clean_size] = $sap_size;
}
}
// 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;
// add to hash
$this->sap_battery_hash[$clean_sku] = $sap_battery;
}
else
{
// TODO: for now, assume that the brand and size in system == brand and size in csv file
// need to check if this is in added_batteries because the hash contains
// existing + added. What we need is the list of existing only
if (!isset($added_batteries[$clean_sku]))
{
// add to list of existing batteries
$existing_batteries[$clean_sku] = $this->sap_battery_hash[$clean_sku];
}
}
$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,
$existing_brands, $existing_sizes, $existing_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,
$existing_brands, $existing_sizes, $existing_batteries)
{
try
{
$fh = fopen($report_file, "w");
}
catch (Exception $e)
{
throw new Exception('The file "' . $report_file . '" could be opened.');
}
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");
// write the added batteries
// check the existing brands array for the added ones
$not_added_batteries = [];
fputs($fh, 'Added Batteries: ' . "\n");
foreach($added_batteries as $added_battery)
{
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
// check the existing arrays for the added ones
$not_added_brands = [];
fputs($fh, 'Added Brands: ' . "\n");
foreach ($added_brands as $added_brand)
{
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
// check the existing array for the added ones
$not_added_sizes = [];
fputs($fh, 'Added Sizes: ' . "\n");
foreach ($added_sizes as $added_size)
{
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)
{
fputs($fh, $not_added_battery, strlen($not_added_battery));
fputs($fh, "\n");
}
// write the not added brands
fputs($fh, 'Brands already in system: ' . "\n");
foreach($not_added_brands as $not_added_brand)
{
fputs($fh, $not_added_brand, strlen($not_added_brand));
fputs($fh, "\n");
}
// write the not added sizes
fputs($fh, 'Sizes already in system: ' . "\n");
foreach($not_added_sizes as $not_added_size)
{
fputs($fh, $not_added_size, strlen($not_added_size));
fputs($fh, "\n");
}
fclose($fh);
}
protected function normalizeName($name)
{
$normalized_key = trim(strtoupper($name));
return $normalized_key;
}
}