addRequiredInput(
array(
'Upload' => array(
'csvFile' => 'Id',
'importType' => 'String255'
)
)
);
return new ContractUseOnce(
new UseCaseOptions(),
$requirement
);
}
/**
* Upload and run the TU data import.
* @param object|null $jobRecord
* @param \Workspace\Utility\ServiceInputParams $contract
* @return array
*/
public function executeUpload($jobRecord, ServiceInputParams $contract)
{
// \Utility\Debug::errorLog('executeUpload $contract->data', $contract->data);
$jobRecord = null;
$uploadId = $contract->data->Upload['csvFile'];
$document = $this->em->find('Utility\\Entity\\Document', $uploadId);
if (is_null($document))
{
Debug::errorLog(
'Bulk Data Import EXCEPTION',
'Could not find document id: ' . $uploadId
);
return;
}
$fileName = $document->filename;
$importType = $contract->data->Upload['importType'];
$this->offlineBulkDataImport($fileName,$importType);
return $contract->success('Import being processed.', array());
}
/**
* @param $fileName
*/
protected function offlineBulkDataImport($fileName,$importType)
{
// exec(
// 'php /var/www/namibia/public/index.php bulk vehicle import '
// . $fileName . ' ' . $importType
// . ' >>/log/php.log &'
// );
$authData = \Utility\Registry::getAuthData();
$companyId = isset($authData['company']['id']) ? $authData['company']['id'] : null;
$userId = isset($authData['id']) ? $authData['id'] : null;
// exec(
// 'php /var/www/NIRPH/Bid4Cars2Repo/public/index.php bulk vehicle import '
// . $fileName . ' ' . $importType . ' ' . $userId . ' ' . $companyId
// . ' >>/log/php.log &'
// );
exec(
'php /var/www/namibia/public/index.php bulk vehicle import '
. $fileName . ' ' . $importType . ' ' . $userId . ' ' . $companyId
. ' >>/log/php.log &'
);
}
#-------------------------------------------------- OFFLINE PROCESSING
/**
* CRON functionality: Import updated transunion vehicle data.
* @param string $filename
*/
public function scriptUpdateVehicleData($filename,$importType,$userId,$companyId)
{
// \Utility\Debug::errorLog('$filename', $filename);
// \Utility\Debug::errorLog('$importType', $importType);
#-> Ensure we don't have problem with script timeout.
set_time_limit(0);
#-> Prepare helpers.
$this->util = new ImportUtility($this->em);
$this->store = new DataStore();
$this->helper = new BulkImportHelper($this->em, $this->store);
$this->exceptions = array();
$this->userId = $userId;
$this->companyId = $companyId;
#-> Catch errors.
try
{
#-> Get things ready.
$this->prepareForCsvProcessing($filename);
switch ($importType)
{
case 'Drive':
//Read File
$file = fopen(getcwd() . '/public/documents/' . $filename, 'r');
$data=array();
while (($data_tmp = fgetcsv($file, 1000, ";")) !== FALSE) {
// \Utility\Debug::errorLog('$data_tmp', $data_tmp);
$data[] = $data_tmp;
}
fclose($file);
//Remove first line
array_shift($data);
//Write File
$file = fopen(getcwd() . '/public/documents/' . $filename, 'w');
foreach($data as $d){
$d = str_replace('"','',$d);
// \Utility\Debug::errorLog('$d', $d);
fputcsv($file, $d, ';');
}
fclose($file);
break;
}
#-> Open uploaded csv file.
$data = $this->importer = new ImportCsv(
getcwd() . '/public/documents/' . $filename, true, ';'
);
#-> Handle csv data.
ini_set('auto_detect_line_endings',TRUE);
while (($data = $this->importer->getRecord(ImportCsv::FETCH_ASSOC)) !== false)
{
$this->processCsvEntryForPersistenceIfRequired($data,$importType);
}
// ini_set('auto_detect_line_endings',FALSE);
#-> Cleanup.
$this->cleanupAfterCsvProcessing();
}
catch (\Exception $e)
{
Debug::errorLog('Bulk Data Import EXCEPTION', $e->getMessage());
Debug::errorLog('Bulk Data Import TRACE', $e->getTraceAsString());
}
\Utility\Debug::errorLog('$importType', $importType);
\Utility\Debug::errorLog('$this->exceptions', $this->exceptions);
\Utility\Debug::errorLog('$this->recordCounter', $this->recordCounter);
\Utility\Debug::errorLog('$this->successCounter', $this->successCounter);
\Utility\Debug::errorLog('$this->failedCounter', $this->failedCounter);
/*
* send email with exceptions to user
*/
if(!empty($this->exceptions))
{
$profileEntry = $this->em->getRepository('\\User\\Entity\\Profile')
->find($this->userId);
// \Utility\Debug::errorLog('$profileEntry->email', $profileEntry->email);
$toCompanyId = $this->companyId;
$toProfileId = $this->userId;
$email = $profileEntry->email;
$mobile = null;
$subject = "Bulk import of $importType system";
$templateName = 'general';
$params['body'] = '
Good Day ' . $profileEntry->firstName . ' ' . $profileEntry->familyName . ',
|
|
The bulk import for ' . $importType . ' is complete
|
|
Total records processed: ' . $this->recordCounter . '
Number of records successfully imported: ' . $this->successCounter . '
Number of records that could not be imported: ' . $this->successCounter . '
|
|
';
foreach($this->exceptions as $exceptions)
{
$params['body'] .= '
MM Code:
|
' . $exceptions['MmCode'] . '
|
Error(s):
|
';
if(is_array($exceptions['Error']))
{
foreach($exceptions['Error'] as $errors)
{
foreach($errors as $error)
{
$params['body'] .= '
' . $error . '
|
';
}
}
}
else
{
$params['body'] .= '
' . $exceptions['Error'] . '
|
';
}
$params['body'] .= '
|
';
}
$params['body'] .= '
';
$params['smsBody'] = '';
// \Utility\Debug::errorLog('email body', $params['body']);
$oNotify = new \Utility\Comms\Notification();
$oNotify->sendFromTemplate(
null,
null,
$toCompanyId,
$toProfileId,
$email,
$mobile,
$subject,
$templateName,
$params
);
}
}
/**
* @param $filename
*/
protected function prepareForCsvProcessing($filename)
{
#-> Establish new synchronization version to use.
$this->store->version = $this->util->getLatestSynchVersion(
'Stock\\Entity\\Type'
) + 1;
#-> Ensure that synchronization versions have a full sequence.
$this->helper->ensureSynchronizationVersionSequence();
#-> Prepare common variables.
$this->previousMmCode = false;
$this->em->beginTransaction();
}
/**
* Process a csv line entry.
* @param $packet
*/
protected function processCsvEntryForPersistenceIfRequired($packet,$importType)
{
// \Utility\Debug::errorLog('$packet', $packet);
$this->helper->errors = array();
$vehicleType = '';
$vehicleMmCode = '';
#-> Ensure null instead of empty string on numeric fields.
$packet = $this->helper->cleanupNumericInputFields($packet);
switch ($importType)
{
case 'Pinnacle':
/*
* prepare vehicle year data
*/
$vehicleYearArray = explode('/',$packet['Registration Date']);
$vehicleYearArray = explode(' ',$vehicleYearArray[2]);
/*
* define variables
*/
$stockNumber = $packet['Stock Number'];
$vehicleMmCode = $packet['MM Code'];
$vehicleYear = $vehicleYearArray[0];
$vehicleMake = $packet['Make'];
$vehicleModel = $packet['Model'];
$vehicleType = $packet['Specification'];
$vehicleTransmission = $packet['Transmission'];
$vehicleExteriorColour = $packet['Colour'];
$vehicleInteriorColour = '';
$vehicleUpholstery = $packet['Interior'];
$vehicleCondition = $packet[''];
$vehicleReg = $packet['Registration Number'];
$vehicleVin = $packet['VIN'];
$vehicleMileage = $packet['Odometer'];
$vehicleFuelType = $packet['Fuel Type'];
$vehicleEngineNumber = $packet['Engine Number'];
$empty = $packet[''];
break;
case 'Drive':
/*
* define variables
*/
$stockNumber = $packet['Suffix to Use'];
$vehicleMmCode = $packet['MM Code'];
$vehicleYear = $packet['Model Year'];
$vehicleMake = $packet['Fran'];
$vehicleModel = $packet['Model'];
$vehicleType = $packet['Description'];
$vehicleTransmission = '';
$vehicleExteriorColour = $packet['Colour'];
$vehicleInteriorColour = $packet['Trim'];
$vehicleUpholstery = '';
$vehicleCondition = $packet['Condition'];
$vehicleReg = $packet['Reg'];
$vehicleVin = $packet['VIN'];
$vehicleMileage = $packet['Mileage'];
$vehicleFuelType = '';
$vehicleEngineNumber = $packet['Engine'];
break;
}
if('' == $vehicleType)
{
return;
}
#-> Safety check for year range.
if ($vehicleYear < 1970)
{
return;
}
$this->recordCounter++;
// \Utility\Debug::errorLog('$vehicleMmCode', $vehicleMmCode);
if('' != $vehicleMmCode && 0 != $vehicleMmCode)
{
$yearId = '';
$exteriorColourId = '';
$interiorColourId = '';
$upholsteryId = '';
$conditionId = '';
$transmissionId = '';
$fuelTypeId = '';
#-> Get vehicle type ID.
$typeId = $this->helper->getVehicleType($vehicleMmCode,$vehicleMake);
if('' != $vehicleYear)
{
#-> Get vehicle year ID.
$yearId = $this->helper->getYearEntry($vehicleYear);
}
if('' != $vehicleExteriorColour)
{
#-> Get vehicle exterior colour ID.
$exteriorColourId = $this->helper->getExteriorColourEntry($vehicleExteriorColour);
}
if('' != $vehicleInteriorColour)
{
#-> Get vehicle interior colour ID.
$interiorColourId = $this->helper->getInteriorColourEntry($vehicleInteriorColour);
}
if('' != $vehicleUpholstery)
{
#-> Get vehicle upholstery ID.
$upholsteryId = $this->helper->getUpholsteryEntry($vehicleUpholstery);
}
if('' != $vehicleCondition)
{
#-> Get vehicle condition ID.
$conditionId = $this->helper->getConditionEntry($vehicleCondition);
}
if('' != $vehicleTransmission)
{
#-> Get vehicle transmission ID.
$transmissionId = $this->helper->getTransmissionEntry($vehicleTransmission);
}
if('' != $vehicleFuelType)
{
#-> Get vehicle fuel type ID.
$fuelTypeId = $this->helper->getFuelTypeEntry($vehicleFuelType);
}
if(!empty($this->helper->errors))
{
array_push($this->exceptions,array('MmCode' => $vehicleMmCode,'Error' => $this->helper->errors));
$this->failedCounter++;
return;
}
else
{
$data = \Utility\Comms\TransUnion::searchByMmCode(
$vehicleMmCode,
$vehicleYear
);
$retailPrice = 0.0;
$tradePrice = 0.0;
$listPrice = 0.0;
\Utility\Debug::errorLog('TransUnion $data', $data);
if (is_array($data) && isset($data['VehicleDetails'])
&& isset($data['VehicleDetails'][0])
&& isset($data['VehicleDetails'][0]['Value'])
)
{
$retailPrice = isset($data['VehicleDetails'][0]['Value']['RetailPrice']) ? $data['VehicleDetails'][0]['Value']['RetailPrice'] : 0.0;
$tradePrice = isset($data['VehicleDetails'][0]['Value']['TradePrice']) ? $data['VehicleDetails'][0]['Value']['TradePrice'] : 0.0;
$listPrice = isset($data['VehicleDetails'][0]['Value']['NewPrice']) ? $data['VehicleDetails'][0]['Value']['NewPrice'] : 0.0;
}
// \Utility\Debug::errorLog('$this->userId', $this->userId);
// \Utility\Debug::errorLog('$this->companyId', $this->companyId);
//
// \Utility\Debug::errorLog('$typeId', $typeId);
// \Utility\Debug::errorLog('$categoryId', $categoryId);
// \Utility\Debug::errorLog('$modelId', $modelId);
// \Utility\Debug::errorLog('$makeId', $makeId);
// \Utility\Debug::errorLog('$yearId', $yearId);
// \Utility\Debug::errorLog('$exteriorColourId', $exteriorColourId);
// \Utility\Debug::errorLog('$interiorColourId', $interiorColourId);
// \Utility\Debug::errorLog('$upholsteryId', $upholsteryId);
// \Utility\Debug::errorLog('$conditionId', $conditionId);
// \Utility\Debug::errorLog('$transmissionId', $transmissionId);
// \Utility\Debug::errorLog('$fuelTypeId', $fuelTypeId);
//
// \Utility\Debug::errorLog('$retailPrice', $retailPrice);
// \Utility\Debug::errorLog('$tradePrice', $tradePrice);
\Utility\Debug::errorLog('$listPrice', $listPrice);
// \Utility\Debug::errorLog('$stockNumber', $stockNumber);
// \Utility\Debug::errorLog('$vehicleReg', $vehicleReg);
// \Utility\Debug::errorLog('$vehicleVin', $vehicleVin);
// \Utility\Debug::errorLog('$vehicleEngineNumber', $vehicleEngineNumber);
// \Utility\Debug::errorLog('$vehicleMileage', $vehicleMileage);
/*
* insert into stock table
*/
\Utility\Registry::set('IsBulkImport', true);
$stockEntry = $this->em->getRepository('\\Stock\\Entity\\Stock')
->findOneBy(array(
'stockNumber' => $stockNumber,
'createdBy' => $this->em->getRepository('\User\Entity\Profile')->find($this->userId),
'company' => $this->em->getRepository('\Company\Entity\Company')->find($this->companyId),
'archived' => false
));
if(empty($stockEntry))
{
$stockEntry = new \Stock\Entity\Stock();
$stockArray = array(
'stockNumber' => $stockNumber,
'stockNumber' => $stockNumber,
'jobState' => 'Stock',
'tradePriceDate' => new \DateTime('now'),
'tradePrice' => $tradePrice,
'retailPrice' => $retailPrice,
'listPriceDate' => new \DateTime('now'),
'listPrice' => $listPrice,
'registrationNumber'=> $vehicleReg,
'vinNumber' => $vehicleVin,
'engineNumber' => $vehicleEngineNumber,
'km' => $vehicleMileage,
'archived' => false
);
$stockArray['createdBy'] = $this->em->getRepository('\User\Entity\Profile')->find($this->userId);
$stockArray['company'] = $this->em->getRepository('\Company\Entity\Company')->find($this->companyId);
if('' != $typeId){$stockArray['type'] = $this->em->getRepository('\Stock\Entity\Type')->find($typeId);}
// if('' != $categoryId){$stockArray[] = $categoryId;}
// if('' != $modelId){$stockArray[] = $modelId;}
// if('' != $makeId){$stockArray[] = $makeId;}
if('' != $yearId){$stockArray['vehicleYear'] = $this->em->getRepository('\Stock\Entity\Year')->find($yearId);}
if('' != $exteriorColourId){$stockArray['exteriorColour'] = $this->em->getRepository('\Stock\Entity\ExteriorColour')->find($exteriorColourId);}
if('' != $interiorColourId){$stockArray['interiorColour'] = $this->em->getRepository('\Stock\Entity\InteriorColour')->find($interiorColourId);}
if('' != $upholsteryId){$stockArray['upholstery'] = $this->em->getRepository('\Stock\Entity\Upholstery')->find($upholsteryId);}
if('' != $conditionId){$stockArray['condition'] = $this->em->getRepository('\Stock\Entity\Condition')->find($conditionId);}
if('' != $transmissionId){$stockArray['transmissionType'] = $this->em->getRepository('\Stock\Entity\TransmissionType')->find($transmissionId);}
if('' != $fuelTypeId){$stockArray['fuelType'] = $this->em->getRepository('\Stock\Entity\FuelType')->find($fuelTypeId);}
try{
$stockEntry->fromArray($stockArray);
$this->em->persist($stockEntry);
$this->em->flush();
}
catch (\Exception $e)
{
$this->failedCounter++;
array_push($this->exceptions,array('MmCode' => $vehicleMmCode,'Error' => 'Could not insert stock entry for MM Code ' . $vehicleMmCode));
return;
}
$this->successCounter++;
}
else
{
$this->failedCounter++;
array_push($this->exceptions,array('MmCode' => $vehicleMmCode,'Error' => 'Could not insert stock entry for MM Code ' . $vehicleMmCode . ' stock number ' . $stockNumber . ' already exists.'));
return;
}
}
}
else
{
$this->failedCounter++;
array_push($this->exceptions,array('MmCode' => $vehicleMmCode,'Error' => 'Could not find MM Code for vehicle ' . $vehicleType));
return;
}
#-> Bump the version?
if ($this->store->numItems >= 200)
{
$this->store->version = $this->helper->processBatch();
}
}
/**
*/
protected function cleanupAfterCsvProcessing()
{
$this->em->flush();
$this->em->commit();
$this->importer = null;
}
}