2 namespace Stock\Service;
4 use Stock\Utility\DataStore;
5 use Stock\Utility\ImportHelper;
6 use Stock\Utility\ImportUtility;
8 use Utility\Import\Csv as ImportCsv;
9 use Workspace\Contract\UseOnce as ContractUseOnce;
10 use Workspace\Service\DataBin as DataBinCore;
11 use Workspace\UseCase\Options as UseCaseOptions;
12 use Workspace\UseCase\Requirement as UseCaseRequirement;
13 use Workspace\Utility\ServiceInputParams;
18 * @package Stock\Service
19 * @author Andre Fourie
21 class Import extends DataBinCore
25 * @var \Stock\Utility\ImportUtility
29 * @var \Stock\Utility\ImportHelper
33 * @var \Stock\Utility\DataStore
37 * @var \Utility\Import\Csv
43 public $previousMmCode;
46 #-------------------------------------------------- API INTERFACE
48 * Contract to Upload and run the TU data import.
49 * @return \Workspace\Contract\UseOnce
51 public function contractUpload()
53 $requirement = new UseCaseRequirement();
54 $requirement->addRequiredInput(
61 return new ContractUseOnce(
68 * Upload and run the TU data import.
69 * @param object|null $jobRecord
70 * @param \Workspace\Utility\ServiceInputParams $contract
73 public function executeUpload($jobRecord, ServiceInputParams $contract)
76 $uploadId = $contract->data->Upload['csvFile'];
77 $document = $this->em->find('Utility\\Entity\\Document', $uploadId);
78 if (is_null($document))
81 'TU Data Import EXCEPTION',
82 'Could not find document id: ' . $uploadId
86 $fileName = $document->filename;
88 // $this->util = new ImportUtility($this->em);
89 // $this->store = new DataStore();
90 // $this->helper = new ImportHelper($this->em, $this->store);
92 // $this->helper->executeOfflineTransunionDataImport($fileName);
94 $this->offlineTransunionDataImport($fileName);
96 return $contract->success('Import being processed.', array());
104 protected function offlineTransunionDataImport($fileName)
107 'php /var/www/namibia/public/index.php vehicle import '
109 . ' >>/log/php.log &'
115 #-------------------------------------------------- OFFLINE PROCESSING
117 * CRON functionality: Import updated transunion vehicle data.
118 * @param string $filename
120 public function scriptUpdateVehicleData($filename)
123 #-> Ensure we don't have problem with script timeout.
127 $this->util = new ImportUtility($this->em);
128 $this->store = new DataStore();
129 $this->helper = new ImportHelper($this->em, $this->store);
134 #-> Get things ready.
135 $this->prepareForCsvProcessing($filename);
138 while (($data = $this->importer->getRecord(ImportCsv::FETCH_ASSOC)) !== false)
140 $this->processCsvEntryForPersistenceIfRequired($data);
144 $this->cleanupAfterCsvProcessing();
146 catch (\Exception $e)
148 Debug::errorLog('TU Data Import EXCEPTION', $e->getMessage());
149 Debug::errorLog('TU Data Import TRACE', $e->getTraceAsString());
156 protected function prepareForCsvProcessing($filename)
158 #-> Establish new synchronization version to use.
159 $this->store->version = $this->util->getLatestSynchVersion(
160 'Stock\\Entity\\Type'
163 #-> Ensure that synchronization versions have a full sequence.
164 $this->helper->ensureSynchronizationVersionSequence();
166 #-> Open uploaded csv file.
167 $this->importer = new ImportCsv(
168 getcwd() . '/public/documents/' . $filename, true
171 #-> Prepare common variables.
172 $this->previousMmCode = false;
173 $this->em->beginTransaction();
177 * Process a csv line entry.
180 protected function processCsvEntryForPersistenceIfRequired($packet)
182 #-> Ensure null instead of empty string on numeric fields.
183 $packet = $this->helper->cleanupNumericInputFields($packet);
185 #-> Only handle an mm-code once.
186 if ($this->previousMmCode == $packet['MMCode'])
190 $this->previousMmCode = $packet['MMCode'];
192 #-> Safety check for year range.
193 $introYear = explode('/', $packet['IntroDate'])[1];
194 if ($introYear < 1970)
199 #-> Handle vehicle make.
200 $makeId = $this->helper->createOrUpdateVehicleMakeEntry($packet);
202 #-> Handle vehicle model.
203 $modelId = $this->helper->createOrUpdateVehicleModel($packet, $makeId);
205 #-> Handle vehicle type.
206 $this->helper->createOrUpdateVehicleType($packet, $modelId);
208 #-> Bump the version?
209 if ($this->store->numItems >= 200)
211 $this->store->version = $this->helper->processBatch();
217 protected function cleanupAfterCsvProcessing()
221 $this->importer = null;