initial commit
[namibia] / module / Stock / src / Stock / Service / Import.php
1 <?php
2 namespace Stock\Service;
3
4 use Stock\Utility\DataStore;
5 use Stock\Utility\ImportHelper;
6 use Stock\Utility\ImportUtility;
7 use Utility\Debug;
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;
14
15
16 /**
17  * Class Import
18  * @package Stock\Service
19  * @author  Andre Fourie
20  */
21 class Import extends DataBinCore
22 {
23
24         /**
25          * @var \Stock\Utility\ImportUtility
26          */
27         protected $util;
28         /**
29          * @var \Stock\Utility\ImportHelper
30          */
31         protected $helper;
32         /**
33          * @var \Stock\Utility\DataStore
34          */
35         protected $store;
36         /**
37          * @var \Utility\Import\Csv
38          */
39         public $importer;
40         /**
41          * @var string
42          */
43         public $previousMmCode;
44
45
46         #-------------------------------------------------- API INTERFACE
47         /**
48          * Contract to Upload and run the TU data import.
49          * @return \Workspace\Contract\UseOnce
50          */
51         public function contractUpload()
52         {
53                 $requirement = new UseCaseRequirement();
54                 $requirement->addRequiredInput(
55                         array(
56                                 'Upload' => array(
57                                         'csvFile' => 'Id'
58                                 )
59                         )
60                 );
61                 return new ContractUseOnce(
62                         new UseCaseOptions(),
63                         $requirement
64                 );
65         }
66
67         /**
68          * Upload and run the TU data import.
69          * @param object|null                           $jobRecord
70          * @param \Workspace\Utility\ServiceInputParams $contract
71          * @return array
72          */
73         public function executeUpload($jobRecord, ServiceInputParams $contract)
74         {
75                 $jobRecord = null;
76                 $uploadId  = $contract->data->Upload['csvFile'];
77                 $document  = $this->em->find('Utility\\Entity\\Document', $uploadId);
78                 if (is_null($document))
79                 {
80                         Debug::errorLog(
81                                 'TU Data Import EXCEPTION',
82                                 'Could not find document id: ' . $uploadId
83                         );
84                         return;
85                 }
86                 $fileName = $document->filename;
87
88 //        $this->util   = new ImportUtility($this->em);
89 //        $this->store  = new DataStore();
90 //        $this->helper = new ImportHelper($this->em, $this->store);
91 //
92 //              $this->helper->executeOfflineTransunionDataImport($fileName);
93
94                 $this->offlineTransunionDataImport($fileName);
95
96                 return $contract->success('Import being processed.', array());
97
98         }
99
100
101         /**
102          * @param $fileName
103          */
104         protected function offlineTransunionDataImport($fileName)
105         {
106                 exec(
107                         'php /var/www/B4C2/public/index.php vehicle import '
108                         . $fileName
109                         . ' >>/log/php.log &'
110                 );
111         }
112
113
114
115         #-------------------------------------------------- OFFLINE PROCESSING
116         /**
117          * CRON functionality: Import updated transunion vehicle data.
118          * @param string $filename
119          */
120         public function scriptUpdateVehicleData($filename)
121         {
122
123                 #-> Ensure we don't have problem with script timeout.
124                 set_time_limit(0);
125
126                 #-> Prepare helpers.
127                 $this->util   = new ImportUtility($this->em);
128                 $this->store  = new DataStore();
129                 $this->helper = new ImportHelper($this->em, $this->store);
130
131                 #-> Catch errors.
132                 try
133                 {
134                         #-> Get things ready.
135                         $this->prepareForCsvProcessing($filename);
136
137                         #-> Handle csv data.
138                         while (($data = $this->importer->getRecord(ImportCsv::FETCH_ASSOC)) !== false)
139                         {
140                                 $this->processCsvEntryForPersistenceIfRequired($data);
141                         }
142
143                         #-> Cleanup.
144                         $this->cleanupAfterCsvProcessing();
145                 }
146                 catch (\Exception $e)
147                 {
148                         Debug::errorLog('TU Data Import EXCEPTION', $e->getMessage());
149                         Debug::errorLog('TU Data Import TRACE', $e->getTraceAsString());
150                 }
151         }
152
153         /**
154          * @param $filename
155          */
156         protected function prepareForCsvProcessing($filename)
157         {
158                 #-> Establish new synchronization version to use.
159                 $this->store->version = $this->util->getLatestSynchVersion(
160                                 'Stock\\Entity\\Type'
161                         ) + 1;
162
163                 #-> Ensure that synchronization versions have a full sequence.
164                 $this->helper->ensureSynchronizationVersionSequence();
165
166                 #-> Open uploaded csv file.
167                 $this->importer = new ImportCsv(
168                         getcwd() . '/public/documents/' . $filename, true
169                 );
170
171                 #-> Prepare common variables.
172                 $this->previousMmCode = false;
173                 $this->em->beginTransaction();
174         }
175
176         /**
177          * Process a csv line entry.
178          * @param $packet
179          */
180         protected function processCsvEntryForPersistenceIfRequired($packet)
181         {
182                 #-> Ensure null instead of empty string on numeric fields.
183                 $packet = $this->helper->cleanupNumericInputFields($packet);
184
185                 #-> Only handle an mm-code once.
186                 if ($this->previousMmCode == $packet['MMCode'])
187                 {
188                         return;
189                 }
190                 $this->previousMmCode = $packet['MMCode'];
191
192                 #-> Safety check for year range.
193                 $introYear = explode('/', $packet['IntroDate'])[1];
194                 if ($introYear < 1970)
195                 {
196                         return;
197                 }
198
199                 #-> Handle vehicle make.
200                 $makeId = $this->helper->createOrUpdateVehicleMakeEntry($packet);
201
202                 #-> Handle vehicle model.
203                 $modelId = $this->helper->createOrUpdateVehicleModel($packet, $makeId);
204
205                 #-> Handle vehicle type.
206                 $this->helper->createOrUpdateVehicleType($packet, $modelId);
207
208                 #-> Bump the version?
209                 if ($this->store->numItems >= 200)
210                 {
211                         $this->store->version = $this->helper->processBatch();
212                 }
213         }
214
215         /**
216          */
217         protected function cleanupAfterCsvProcessing()
218         {
219                 $this->em->flush();
220                 $this->em->commit();
221                 $this->importer = null;
222         }
223
224 }