Merge branch '178-fix-bug-with-rider-assignment' into 'master'

Make command sap_battery:import repeatable #178

Closes #178

See merge request jankstudio/resq!214
This commit is contained in:
Kendrick Chan 2019-02-01 10:45:59 +00:00
commit 97b04a8b6e

View file

@ -18,10 +18,16 @@ use DateTime;
class ImportSAPBatteryCommand extends Command
{
private $em;
private $battery_hash;
private $batt_size_hash;
private $batt_brand_hash;
public function __construct(ObjectManager $em)
{
$this->em = $em;
$this->initBatteryHash();
$this->initBatterySizeHash();
$this->initBatteryBrandHash();
parent::__construct();
}
@ -62,16 +68,10 @@ class ImportSAPBatteryCommand extends Command
// has error?
$has_error = false;
// hashes
$brand_hash = [];
$size_hash = [];
$battery_hash = [];
// loop through rows and build hashes
while (($fields = fgetcsv($handle)) !== false)
{
// check if blank
// check if blank row
if (strlen(trim($fields[0])) == 0)
continue;
@ -83,36 +83,67 @@ class ImportSAPBatteryCommand extends Command
$output->writeln("Parsing $clean_sku...");
// brand hash
if (!isset($brand_hash[$clean_brand]))
if (!isset($this->batt_brand_hash[$clean_brand]))
{
$brand = new SAPBatteryBrand();
$brand->setName($clean_brand);
$em->persist($brand);
$brand_hash[$clean_brand] = $brand;
$this->batt_brand_hash[$clean_brand] = $brand;
}
// size hash
if (!isset($size_hash[$clean_size]))
if (!isset($this->batt_size_hash[$clean_size]))
{
$size = new SAPBatterySize();
$size->setName($clean_size);
$em->persist($size);
$size_hash[$clean_size] = $size;
$this->batt_size_hash[$clean_size] = $size;
}
// create battery entry
$battery = new SAPBattery();
$battery->setID($clean_sku)
->setSize($size_hash[$clean_size])
->setBrand($brand_hash[$clean_brand]);
// battery hash
if (!isset($this->battery_hash[$clean_sku]))
{
// create battery entry
$battery = new SAPBattery();
$battery->setID($clean_sku)
->setSize($size_hash[$clean_size])
->setBrand($brand_hash[$clean_brand]);
$em->persist($battery);
$em->persist($battery);
}
}
$em->flush();
}
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;
}
}