resq/src/Command/ImportSAPBatteryCommand.php
2020-04-29 08:24:07 +00:00

214 lines
5.7 KiB
PHP

<?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 Symfony\Component\Validator\Validator\ValidatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\SAPBattery;
use App\Entity\SAPBatteryBrand;
use App\Entity\SAPBatterySize;
use DateTime;
class ImportSAPBatteryCommand extends Command
{
private $em;
private $battery_hash;
private $batt_size_hash;
private $batt_brand_hash;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->initBatteryHash();
$this->initBatterySizeHash();
$this->initBatteryBrandHash();
parent::__construct();
}
protected function configure()
{
$this->setName('sap_battery:import')
->setDescription('Import a CSV file with SAP battery data.')
->setHelp('Creates SAP battery data entries off imported CSV.')
->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
{
$handle = fopen($csv_file, "r");
}
catch (Exception $e)
{
throw new Exception('The file "' . $csv_file . '" could not be read.');
}
// get entity manager
$em = $this->em;
// row counter
$row = 1;
// has error?
$has_error = false;
$dupe_brands = [];
$dupe_sizes = [];
$dupe_batteries = [];
// loop through rows and build hashes
while (($fields = fgetcsv($handle)) !== false)
{
// check if blank row
if (strlen(trim($fields[0])) == 0)
continue;
// clean up fields
$clean_brand = trim($fields[0]);
$clean_sku = strtoupper(trim($fields[1]));
$clean_size = trim($fields[2]);
$output->writeln("Parsing $clean_sku...");
// brand hash
if (!isset($this->batt_brand_hash[$clean_brand]))
{
$brand = new SAPBatteryBrand();
$brand->setName($clean_brand);
$em->persist($brand);
$this->batt_brand_hash[$clean_brand] = $brand;
}
else
{
// add to duplicate list
$dupe_brands[] = $clean_brand;
}
// size hash
if (!isset($this->batt_size_hash[$clean_size]))
{
$size = new SAPBatterySize();
$size->setName($clean_size);
$em->persist($size);
$this->batt_size_hash[$clean_size] = $size;
}
else
{
$dupe_sizes[] = $clean_size;
}
// battery hash
if (!isset($this->battery_hash[$clean_sku]))
{
// create battery entry
$battery = new SAPBattery();
$battery->setID($clean_sku)
->setSize($this->batt_size_hash[$clean_size])
->setBrand($this->batt_brand_hash[$clean_brand]);
$em->persist($battery);
}
else
{
$dupe_batteries[] = $clean_sku;
}
}
$em->flush();
// write dupes report
if ((count($dupe_brands) > 0) ||
(count($dupe_sizes) > 0) ||
(count($dupe_batteries) > 0))
{
$this->writeDupeReport($report_file, $dupe_brands, $dupe_sizes, $dupe_batteries);
}
return 0;
}
protected function initBatteryHash()
{
$this->battery_hash = [];
$batts = $this->em->getRepository(SAPBattery::class)->findAll();
foreach ($batts as $batt)
$this->battery_hash[$batt->getID()] = $batt;
}
protected function initBatterySizeHash()
{
$this->batt_size_hash = [];
$sizes = $this->em->getRepository(SAPBatterySize::class)->findAll();
foreach ($sizes as $size)
$this->batt_size_hash[$size->getName()] = $size;
}
protected function initBatteryBrandHash()
{
$this->batt_brand_hash = [];
$brands = $this->em->getRepository(SAPBatteryBrand::class)->findAll();
foreach ($brands as $brand)
$this->batt_brand_hash[$brand->getName()] = $brand;
}
protected function writeDupeReport($report_file, $brands, $sizes, $batts)
{
try
{
$fh = fopen($report_file, "w");
}
catch (Exception $e)
{
throw new Exception('The file "' . $report_file . '" could be opened.');
}
fputs($fh, 'Brands Already in Database: ' . "\n");
foreach ($brands as $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");
}
fputs($fh, 'SAP Codes Already in Database: ' . "\n");
foreach ($batts as $batt)
{
fputs($fh, $batt, strlen($batt));
fputs($fh, "\n");
}
fclose($fh);
}
}