Fix adding of compatible batteries to vehicles. #270

This commit is contained in:
Korina Cordero 2019-10-03 04:12:12 +00:00
parent 2b48c702d4
commit dcd34f6f73

View file

@ -92,8 +92,6 @@ class ImportCMBBatteryDataCommand extends Command
$brand_marathoner = ''; $brand_marathoner = '';
$brand_excel = ''; $brand_excel = '';
$comp_batteries = [];
while (($fields = fgetcsv($fh)) !== false) while (($fields = fgetcsv($fh)) !== false)
{ {
if ($row_num < 1) if ($row_num < 1)
@ -163,6 +161,17 @@ class ImportCMBBatteryDataCommand extends Command
{ {
// vehicle and battery data // vehicle and battery data
// vehicle data
// check if vehicle manufacturer has been added
if (!isset($this->vmanu_hash[$manufacturer]))
$this->addVehicleManufacturer($manufacturer);
// check if vehicle make has been added
if (!isset($this->vmake_hash[$manufacturer][$make]))
{
$this->addVehicleMake($manufacturer, $make, $year);
}
// battery size // battery size
// check if battery size has been added // check if battery size has been added
if (!isset($this->bsize_hash[$sdfc])) if (!isset($this->bsize_hash[$sdfc]))
@ -181,12 +190,12 @@ class ImportCMBBatteryDataCommand extends Command
$this->addBatterySize($excel); $this->addBatterySize($excel);
// check if battery has been added // check if battery has been added
$vehicle = $this->vmake_hash[$manufacturer][$make];
if (!isset($this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc])) if (!isset($this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc]))
{ {
if (!(empty($sdfc))) if (!(empty($sdfc)))
{ {
$this->addBattery($brand_sdfc, $sdfc); $this->addBattery($brand_sdfc, $sdfc, $vehicle);
$comp_batteries[] = $this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc];
} }
} }
@ -194,8 +203,7 @@ class ImportCMBBatteryDataCommand extends Command
{ {
if (!(empty($ultramax))) if (!(empty($ultramax)))
{ {
$this->addBattery($brand_ultramax, $ultramax); $this->addBattery($brand_ultramax, $ultramax, $vehicle);
$comp_batteries[] = $this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax];
} }
} }
@ -203,8 +211,7 @@ class ImportCMBBatteryDataCommand extends Command
{ {
if (!(empty($motolite))) if (!(empty($motolite)))
{ {
$this->addBattery($brand_motolite, $motolite); $this->addBattery($brand_motolite, $motolite, $vehicle);
$comp_batteries[] = $this->batt_hash[$brand_motolite][$brand_motolite][$motolite];
} }
} }
@ -212,8 +219,7 @@ class ImportCMBBatteryDataCommand extends Command
{ {
if (!(empty($marathoner))) if (!(empty($marathoner)))
{ {
$this->addBattery($brand_marathoner, $marathoner); $this->addBattery($brand_marathoner, $marathoner, $vehicle);
$comp_batteries[] = $this->batt_hash[$brand_marathoner][$brand_marathoner][$marathoner];
} }
} }
@ -221,22 +227,10 @@ class ImportCMBBatteryDataCommand extends Command
{ {
if (!(empty($excel))) if (!(empty($excel)))
{ {
$this->addBattery($brand_excel, $excel); $this->addBattery($brand_excel, $excel, $vehicle);
$comp_batteriesp[] = $this->batt_hash[$brand_excel][$brand_excel][$excel];
} }
} }
// vehicle data
// check if vehicle manufacturer has been added
if (!isset($this->vmanu_hash[$manufacturer]))
$this->addVehicleManufacturer($manufacturer);
// check if vehicle make has been added
if (!isset($this->vmake_hash[$manufacturer][$make]))
{
$this->addVehicleMake($manufacturer, $make, $year, $comp_batteries);
}
$row_num++; $row_num++;
} }
} }
@ -280,13 +274,12 @@ class ImportCMBBatteryDataCommand extends Command
$this->em->persist($batt_size); $this->em->persist($batt_size);
$this->em->flush(); $this->em->flush();
// add new size into hash // add new size into hash
$this->bsize_hash[$name] = $batt_size; $this->bsize_hash[$name] = $batt_size;
} }
} }
protected function addBattery($brand, $size) protected function addBattery($brand, $size, $vehicle)
{ {
// save to db // save to db
$bmanu = $this->bmanu_hash[$brand]; $bmanu = $this->bmanu_hash[$brand];
@ -301,6 +294,9 @@ class ImportCMBBatteryDataCommand extends Command
->setWarrantyCommercial(6) ->setWarrantyCommercial(6)
->setWarrantyTnv(12); ->setWarrantyTnv(12);
// add compatible vehicles to battery
$battery->addVehicle($vehicle);
$this->em->persist($battery); $this->em->persist($battery);
$this->em->flush(); $this->em->flush();
@ -333,7 +329,7 @@ class ImportCMBBatteryDataCommand extends Command
$this->vmanu_hash[$name] = $vehicle_manufacturer; $this->vmanu_hash[$name] = $vehicle_manufacturer;
} }
protected function addVehicleMake($manufacturer, $make, $year, $batteries) protected function addVehicleMake($manufacturer, $make, $year)
{ {
// save to db // save to db
$vehicle = new Vehicle(); $vehicle = new Vehicle();
@ -365,15 +361,12 @@ class ImportCMBBatteryDataCommand extends Command
// add vehicle to manufacturer // add vehicle to manufacturer
$vmanu->addVehicle($vehicle); $vmanu->addVehicle($vehicle);
// add battery to vehicle
foreach ($batteries as $battery)
{
$vehicle->addBattery($battery);
}
$this->em->persist($vmanu); $this->em->persist($vmanu);
$this->em->persist($vehicle); $this->em->persist($vehicle);
$this->em->flush(); $this->em->flush();
// add to hash
$this->vmake_hash[$manufacturer][$make] = $vehicle;
} }
protected function loadBatteryManufacturers() protected function loadBatteryManufacturers()