2 namespace Stock\Utility;
4 use Doctrine\ORM\EntityManager;
6 use Workspace\Service\DataBin as DataBinCore;
10 * Class BulkImportHelper
11 * @package Stock\Utility
13 class BulkImportHelper extends DataBinCore
19 * @var \Doctrine\ORM\EntityManager
23 * @var \Stock\Utility\ImportUtility
27 * @var \Stock\Utility\DataStore
33 protected $simulateExecution = false;
37 protected $categoryMap = array(
49 protected $typeFieldsToMatch = array(
50 'cylinders', 'cubicCapacity', 'kilowatts', 'bodyType', 'doors'
55 protected $numericFields = array(
56 'cylinders', 'cubicCapacity', 'kilowatts', 'doors'
61 protected $makes = array();
65 protected $models = array();
70 public $errors = array();
72 public function __construct(EntityManager $em, DataStore &$store)
75 $this->util = new ImportUtility($this->em);
76 $this->store = $store;
77 $this->errors = array();
81 #-------------------------------------------------------------------------- SECTION
85 public function executeOfflineTransunionDataImport($fileName)
87 if ($this->simulateExecution)
89 error_log('php /var/www/namibia/public/index.php vehicle import '
91 . ' >>/log/php.log &');
96 'php /var/www/namibia/public/index.php vehicle import '
104 #-------------------------------------------------------------------------- SECTION
106 * Ensure that synchronization versions have a full sequence.
109 public function ensureSynchronizationVersionSequence()
111 #-> Get current latest versions.
112 $makeVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Make');
113 $modelVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Model');
114 $typeVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Type');
116 #-> Establish highest current version.
117 $maxVersion = $this->getMaxVersion($makeVersion, $modelVersion, $typeVersion);
119 #-> Where do we need updates?
120 if ($maxVersion > $makeVersion)
122 $this->updateSynchVersionSequence('Stock\\Entity\\Make', $makeVersion + 1, $maxVersion);
124 if ($maxVersion > $modelVersion)
126 $this->updateSynchVersionSequence('Stock\\Entity\\Model', $modelVersion + 1, $maxVersion);
128 if ($maxVersion > $typeVersion)
130 $this->updateSynchVersionSequence('Stock\\Entity\\Type', $typeVersion + 1, $maxVersion);
135 * @param $makeVersion
136 * @param $modelVersion
137 * @param $typeVersion
140 public function getMaxVersion($makeVersion, $modelVersion, $typeVersion)
142 $maxVersion = $makeVersion > $modelVersion
145 $maxVersion = $maxVersion > $typeVersion
152 * @param string $entityName
153 * @param integer $startVersion
154 * @param integer $endVersion
157 public function updateSynchVersionSequence($entityName, $startVersion, $endVersion)
159 #-> Get entries we can update.
160 $numEntriesNeeded = ((int)$endVersion) - ((int)$startVersion) + 1;
161 $entries = $this->em->getRepository($entityName)
163 array('updateVersion' => null),
164 array('id' => 'ASC'),
168 $this->store->version = ((int)$startVersion) + 1;
169 if ($numEntriesNeeded != count($entries))
171 throw new \Exception(
172 'Cannot synchronize data versions on ' . $entityName . '. Not enough entries available.'
175 foreach ($entries as $entry)
177 $entry->updateVersion = $this->store->version;
178 $this->store->version++;
184 #-------------------------------------------------------------------------- SECTION
186 * MAKE THIS PUBLIC!!!!!!
187 * @param array $packet
190 public function cleanupNumericInputFields(array $packet)
192 foreach ($this->numericFields as $field)
194 if (array_key_exists($field, $packet))
196 $packet[$field] = ('' == $packet[$field])
204 #-------------------------------------------------------------------------- SECTION
207 * MAKE THIS PUBLIC!!!!!!
208 * @param array $packet
211 public function getYearEntry($vehicleYear)
213 $yearId = $this->util->getId(
214 'Stock\\Entity\\Year',
216 'name' => $vehicleYear
219 if (false == $yearId)
221 array_push($this->errors,array('Could not find Year ' . $vehicleYear));
223 return $this->errors;
225 // throw new \Exception(
226 // 'Could not find Year ' . $vehicleYear
232 #-------------------------------------------------------------------------- SECTION
235 * MAKE THIS PUBLIC!!!!!!
236 * @param array $packet
239 public function getMakeEntry($vehicleMmCode,$modelId)
243 $makeEntity = $this->em->createQuery(
244 'SELECT model.id as MODELID,model.name as MODELNAME,make.id as MAKEID '
245 . 'FROM Stock\\Entity\\Model model '
246 . 'LEFT JOIN model.make make '
247 . 'WHERE model.id = :ModelId '
248 . 'AND model.archived = :Archived '
250 ->setParameter('ModelId', $modelId)
251 ->setParameter('Archived', false)
254 foreach($makeEntity as $make)
256 // \Utility\Debug::errorLog('$model', $model);
258 $makeId = $make['MAKEID'];
261 if (false == $makeId)
263 array_push($this->errors,array('Could not find Make for MM Code ' . $vehicleMmCode));
265 return $this->errors;
267 // throw new \Exception(
268 // 'Could not find Model ' . $vehicleModel
274 #-------------------------------------------------------------------------- SECTION
277 * @param array $packet
281 public function getCategoryEntry($vehicleMmCode,$typeId)
285 $categoryEntity = $this->em->createQuery(
286 'SELECT type.id as TYPEID,category.id as CATEGORYID,category.name as CATEGORYNAME '
287 . 'FROM Stock\\Entity\\Type type '
288 . 'LEFT JOIN type.category category '
289 . 'WHERE type.id = :TypeId '
290 . 'AND category.archived = :Archived '
292 ->setParameter('TypeId', $typeId)
293 ->setParameter('Archived', false)
296 foreach($categoryEntity as $category)
298 // \Utility\Debug::errorLog('$model', $model);
300 $categoryId = $category['CATEGORYID'];
303 if (false == $categoryId)
305 array_push($this->errors,array('Could not find Category for MM Code ' . $vehicleMmCode));
307 return $this->errors;
309 // throw new \Exception(
310 // 'Could not find Model ' . $vehicleModel
316 #-------------------------------------------------------------------------- SECTION
319 * @param array $packet
323 public function getModelEntry($vehicleMmCode,$typeId)
327 $modelEntity = $this->em->createQuery(
328 'SELECT type.id as TYPEID,model.id as MODELID,model.name as MODELNAME '
329 . 'FROM Stock\\Entity\\Type type '
330 . 'LEFT JOIN type.model model '
331 . 'WHERE type.id = :TypeId '
332 . 'AND model.archived = :Archived '
334 ->setParameter('TypeId', $typeId)
335 ->setParameter('Archived', false)
338 foreach($modelEntity as $model)
340 // \Utility\Debug::errorLog('$model', $model);
342 $modelId = $model['MODELID'];
345 if (false == $modelId)
347 array_push($this->errors,array('Could not find Model for MM Code' . $vehicleMmCode));
349 return $this->errors;
351 // throw new \Exception(
352 // 'Could not find Model ' . $vehicleModel
359 #-------------------------------------------------------------------------- SECTION
361 * @param array $packet
364 * @throws \Doctrine\ORM\ORMException
366 public function getVehicleType($vehicleMmCode,$vehicleMake)
369 #-> Do data preparations.
371 * Here we ensure that we remove spaces and retain leading zeroes to prevent data duplication.
372 * Previously, this field was cast to int which lead to duplications. - FP
374 $vehicleMmCode = (string)$vehicleMmCode;
375 $vehicleMmCode = preg_replace('/\s+/', '', $vehicleMmCode);
378 // $typeId = $this->util->getId(
379 // 'Stock\\Entity\\Type',
381 // 'mmCode' => $vehicleMmCode
385 $typeEntity = $this->em->createQuery(
386 'SELECT type.id as TYPEID,type.name as TYPENAME '
387 . 'FROM Stock\\Entity\\Type type '
388 . 'WHERE type.mmCode = :MmCode '
389 . 'AND type.archived = :Archived '
391 ->setParameter('MmCode', $vehicleMmCode)
392 ->setParameter('Archived', false)
395 foreach($typeEntity as $type)
397 // \Utility\Debug::errorLog('$type', $type);
399 $typeId = $type['TYPEID'];
402 if (false == $typeId)
404 array_push($this->errors,array('Could not find Type for MM Code ' .$vehicleMmCode));
406 return $this->errors;
407 // throw new \Exception(
408 // 'Could not find Type for MM Code ' .$vehicleMmCode
416 #-------------------------------------------------------------------------- SECTION
419 * @param array $packet
423 public function getInteriorColourEntry($colour)
427 $colourEntity = $this->em->createQuery(
428 'SELECT interiorColour.id,interiorColour.name '
429 . 'FROM Stock\\Entity\\InteriorColour interiorColour '
430 . 'WHERE interiorColour.archived = :Archived '
432 ->setParameter('Archived', false)
435 foreach($colourEntity as $colours)
437 if (strpos(strtolower($colour), strtolower($colours['name'])) !== false)
439 $colourId = $colours['id'];
443 if(false != $colourId)
448 // if (false == $colourId)
450 // array_push($this->errors,array('Could not find colour ' . $colour));
452 // return $this->errors;
454 //// throw new \Exception(
455 //// 'Could not find colour ' . $colour
461 #-------------------------------------------------------------------------- SECTION
464 * @param array $packet
468 public function getUpholsteryEntry($upholstery)
470 if('cloth' == strtolower($upholstery))
472 $upholstery = 'Material';
475 $upholsteryId = false;
477 $upholsteryEntity = $this->em->createQuery(
478 'SELECT upholstery.id,upholstery.name '
479 . 'FROM Stock\\Entity\\Upholstery upholstery '
480 . 'WHERE upholstery.archived = :Archived '
482 ->setParameter('Archived', false)
485 foreach($upholsteryEntity as $upholsteries)
487 if (strpos(strtolower($upholstery), strtolower($upholsteries['name'])) !== false)
489 $upholsteryId = $upholsteries['id'];
493 if(false != $upholsteryId)
495 return $upholsteryId;
497 // if (false == $upholsteryId)
499 // array_push($this->errors,array('Could not find upholstery ' . $upholstery));
501 // return $this->errors;
503 //// throw new \Exception(
504 //// 'Could not find colour ' . $upholstery
507 // return $upholsteryId;
510 #-------------------------------------------------------------------------- SECTION
513 * @param array $packet
517 public function getConditionEntry($condition)
519 $condition = substr($condition, 1);
521 $conditionId = false;
523 $conditionEntity = $this->em->createQuery(
524 'SELECT condition.id,condition.name '
525 . 'FROM Stock\\Entity\\Condition condition '
526 . 'WHERE condition.name LIKE :Condition '
527 . 'AND condition.archived = :Archived '
529 ->setParameter('Condition', $condition.'%')
530 ->setParameter('Archived', false)
533 foreach($conditionEntity as $conditions)
535 // \Utility\Debug::errorLog('$conditions', $conditions);
537 $conditionId = $conditions['id'];
540 if(false != $conditionId)
545 // if (false == $conditionId)
547 // array_push($this->errors,array('Could not find condition ' . $condition));
549 // return $this->errors;
551 //// throw new \Exception(
552 //// 'Could not find condition ' . $condition
555 // return $conditionId;
558 #-------------------------------------------------------------------------- SECTION
561 * @param array $packet
565 public function getTransmissionEntry($transmission)
567 $transmissionId = false;
569 $transmissionEntity = $this->em->createQuery(
570 'SELECT transmissionType.id,transmissionType.name '
571 . 'FROM Stock\\Entity\\TransmissionType transmissionType '
572 . 'WHERE transmissionType.name = :TransmissionType '
573 . 'AND transmissionType.archived = :Archived '
575 ->setParameter('TransmissionType', $transmission)
576 ->setParameter('Archived', false)
579 foreach($transmissionEntity as $transmissions)
581 // \Utility\Debug::errorLog('$transmissions', $transmissions);
583 $transmissionId = $transmissions['id'];
586 if(false != $transmissionId)
588 return $transmissionId;
591 // if (false == $transmissionId)
593 // array_push($this->errors,array('Could not find transmission ' . $transmission));
595 // return $this->errors;
598 // return $transmissionId;
601 #-------------------------------------------------------------------------- SECTION
604 * @param array $packet
608 public function getFuelTypeEntry($fuelType)
612 $fuelTypeEntity = $this->em->createQuery(
613 'SELECT fuelType.id,fuelType.name '
614 . 'FROM Stock\\Entity\\FuelType fuelType '
615 . 'WHERE fuelType.name = :FuelType '
616 . 'AND fuelType.archived = :Archived '
618 ->setParameter('FuelType', $fuelType)
619 ->setParameter('Archived', false)
622 foreach($fuelTypeEntity as $fuelTypes)
624 // \Utility\Debug::errorLog('$fuelTypes', $fuelTypes);
626 $fuelTypeId = $fuelTypes['id'];
629 if(false != $fuelTypeId)
634 // if (false == $fuelTypeId)
636 // array_push($this->errors,array('Could not find fuel type ' . $fuelType));
638 // return $this->errors;
640 // return $fuelTypeId;
643 #-------------------------------------------------------------------------- SECTION
646 * @param array $packet
650 public function getExteriorColourEntry($colour)
654 $colourEntity = $this->em->createQuery(
655 'SELECT exteriorColour.id,exteriorColour.name '
656 . 'FROM Stock\\Entity\\ExteriorColour exteriorColour '
657 . 'WHERE exteriorColour.archived = :Archived '
659 ->setParameter('Archived', false)
662 foreach($colourEntity as $colours)
664 if (strpos(strtolower($colour), strtolower($colours['name'])) !== false)
666 $colourId = $colours['id'];
670 if(false != $colourId)
675 // if (false == $colourId)
677 // array_push($this->errors,array('Could not find colour ' . $colour));
679 // return $this->errors;
681 //// throw new \Exception(
682 //// 'Could not find colour ' . $colour
689 * @param array $packet
690 * @param array $years
692 * @throws \Doctrine\ORM\ORMException
694 public function prepareDateVehicleTypeData(array $packet, $years)
696 list($introMonth, $introYear) = explode('/', $packet['IntroDate']);
697 $discontinueYear = false;
698 $discontinueMonth = false;
699 if (!empty($packet['DiscDate']))
701 list($discontinueMonth, $discontinueYear) = explode('/', $packet['DiscDate']);
703 unset($packet['IntroDate']);
704 unset($packet['DiscDate']);
705 $packet['introYear'] = $this->em->getReference(
706 '\Stock\Entity\Year',
709 $packet['introMonth'] = $introMonth;
710 $packet['discYear'] = null;
711 if ($discontinueYear)
713 $packet['discYear'] = $this->em->getReference(
714 'Stock\\Entity\\Year', $years[$discontinueYear]
716 $packet['discMonth'] = $discontinueMonth;
723 * @param array $packet
726 * @throws \Doctrine\ORM\ORMException
728 public function prepareVehicleTypeDataForNewEntry(array $packet, $modelId)
730 $packet['VehicleType'] = $this->em->getReference(
731 'Stock\\Entity\\Category',
732 $this->categoryMap[$packet['VehicleType']]
734 $packet['Model'] = $this->em->getReference(
735 '\Stock\\Entity\\Model',
738 $packet['name'] = $packet['Variant'];
739 $packet['createVersion'] = $this->store->version;
740 $packet['updateVersion'] = $this->store->version;
745 * @param array $packet
749 public function vehicleTypeUpdateIsNeeded(array $packet, $type)
752 foreach ($this->typeFieldsToMatch as $field)
754 if ($packet[$field] != $type->$field)
757 $type->$field = $packet[$field];
761 if (false != $packet['discYear'] && is_null($type->discYear))
764 $type->discYear = $packet['discYear'];
765 $type->discMonth = $packet['discMonth'];
773 #-------------------------------------------------------------------------- SECTION
777 public function processBatch()
783 $this->em->getConnection()->close();
784 $this->em->beginTransaction();
785 $this->store->version++;
786 $this->store->numItems = 0;