2 namespace User\Service;
7 * Manage Adherence data.
10 class Import extends \Workspace\Service\DataBin
16 protected $regions = array();
17 protected function getRegionId($regionName)
19 if (isset($this->regions[strtolower($regionName)]))
21 return $this->regions[strtolower($regionName)];
23 $region = $this->em->getRepository('\Location\Entity\Region')
27 $this->regions[strtolower($regionName)] = !is_null($region)
30 return $this->regions[strtolower($regionName)];
33 protected $cities = array();
34 protected function getCityId($regionId, $cityName)
36 if (isset($this->cities[strtolower($cityName)]))
38 return $this->cities[strtolower($cityName)];
40 $city = $this->em->getRepository('\Location\Entity\Town')
42 'region' => $regionId,
47 $city = new \Location\Entity\Town();
48 $city->region = $this->em->getReference('\Location\Entity\Region', $regionId);
49 $city->name = ucwords(strtolower($cityName));
50 $this->em->persist($city);
51 $this->em->flush($city);
53 $this->cities[strtolower($cityName)] = $city->id;
54 return $this->cities[strtolower($cityName)];
57 protected $groups = array();
58 protected function getGroupId($groupName)
60 if (isset($this->groups[strtolower($groupName)]))
62 return $this->groups[strtolower($groupName)];
64 $group = $this->em->getRepository('\Company\Entity\Group')
70 $group = new \Company\Entity\Group();
71 $group->name = trim($groupName);
72 $this->em->persist($group);
73 $this->em->flush($group);
75 $this->groups[strtolower($groupName)] = $group->id;
76 return $this->groups[strtolower($groupName)];
79 protected $divisions = array();
80 protected function getDivisionId($groupId, $divisionName)
82 if (isset($this->divisions[strtolower($divisionName)]))
84 return $this->divisions[strtolower($divisionName)];
86 $division = $this->em->getRepository('\Company\Entity\GroupDivision')
89 'name' => $divisionName
91 if (is_null($division))
93 $division = new \Company\Entity\GroupDivision();
94 $division->group = $this->em->getReference('\Location\Entity\Region', $groupId);
95 $division->name = trim($divisionName);
96 $this->em->persist($division);
97 $this->em->flush($division);
99 $this->divisions[strtolower($divisionName)] = $division->id;
100 return $this->divisions[strtolower($divisionName)];
103 public function notifyProcess()
105 $oNotify = new \Utility\Comms\Notification();
106 $dataFolder = __DIR__ . '/../../../../../data/'; // '/tmp/'
107 $notificationFile = $dataFolder . 'auction-users-notify.csv';
108 $csvImporter = new \Utility\Import\Csv($notificationFile, true);
109 $columnHeaders = $csvImporter->getHeaders();
111 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false)
119 $profile = $this->em->find('\User\Entity\Profile', $record['id']);
120 $password = mt_rand(10000, 99999);
121 $profile->fromArray(array(
122 'password' => $password
124 $profile->lastLogin = null;
125 $this->em->flush($profile);
126 $oNotify->sendFromTemplate(
128 $profile->company->id, $profile->id,
129 $profile->email, null, null,
130 'new-profile-auction',
132 'firstName' => $profile->firstName,
133 'familyName' => $profile->familyName,
134 'email' => $profile->email,
143 * CRON functionality: Import auction users from csv.
145 public function cronProcess()
147 #-> Files to work with.
148 $docsFolder = __DIR__ . '/../../../../../../docs/'; // '/tmp/'
149 $docsPut = __DIR__ . '/../../../../../public/documents/'; // '/tmp/'
150 $dataFolder = __DIR__ . '/../../../../../data/'; // '/tmp/'
151 $importFile = $dataFolder . 'auction-users.csv';
152 $exceptionFile = $dataFolder . 'auction-users-exceptions.csv';
153 $notificationFile = $dataFolder . 'auction-users-notify.csv';
155 #-> Some data mappings.
157 'KwaZulu-Natal' => 'KwaZulu-Natal',
158 'Free-State' => 'Free State',
159 'Eastern Cape' => 'Eastern Cape',
160 'Gauteng' => 'Gauteng',
161 'Mpumalanga' => 'Mpumalanga',
162 'Northern Cape' => 'Northern Cape',
163 'Limpopo' => 'Limpopo',
164 'North West' => 'North-West',
165 'Western Cape' => 'Western Cape'
168 'ID' => 'docCopyOfId',
169 'POA' => 'docAddressProof',
170 'COREG' => 'docCompanyRegistration'
173 #-> Some classes we will need.
174 $emailValidator = new \Zend\Validator\EmailAddress();
175 $contactService = new \Person\Service\Contact();
176 $contactService->setWorkflow(new \Person\Workflow());
177 $companyService = new \Company\Service\Company();
178 $companyService->setWorkflow(new \Company\Workflow());
179 $profileService = new \User\Service\Profile();
180 $profileService->setWorkflow(new \User\Workflow());
181 $finfo = finfo_open(FILEINFO_MIME_TYPE);
184 echo "Processing $importFile\n";
185 if (!file_exists($importFile))
187 echo "File not found!\n";
190 $exceptionLog = new \Utility\Export\Csv($exceptionFile);
191 $notificationLog = new \Utility\Export\Csv($notificationFile);
192 $csvImporter = new \Utility\Import\Csv($importFile, true);
193 $columnHeaders = $csvImporter->getHeaders();
194 $columnHeaders[] = 'errors';
195 $exceptionLog->putRecord($columnHeaders);
196 $notificationLog->putRecord(array('id', 'email'));
198 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false)
202 foreach ($record as $field => $value)
204 $record[$field] = trim($value);
206 if (!$emailValidator->isValid($record['email']))
208 $errors[] = 'V:email';
210 if (0 == strlen($record['first_name']))
212 $errors[] = 'V:first_name';
214 if (0 == strlen($record['family_name']))
216 $errors[] = 'V:family_name';
218 if (0 == strlen($record['company_name']))
220 $errors[] = 'V:company_name';
222 if (0 == strlen($record['street']))
224 $errors[] = 'V:street';
226 if (1111111111111 == strlen($record['id_number']))
228 $errors[] = 'V:id_number';
230 if ('Seller & Buyer' != $record['type']
231 && 'Buyer' != $record['type'])
233 $errors[] = 'V:type';
235 if (!is_numeric($record['postal_code']))
237 $record['postal_code'] = '';
239 $regionId = $this->getRegionId($regionMap[$record['region']]);
240 if (is_null($regionId))
242 $errors[] = 'V:region';
244 if (10 < strlen($record['mobile'])
245 || !is_numeric($record['mobile']))
247 $errors[] = 'V:mobile';
251 $record['mobile'] = '+27' . $record['mobile'];
253 !empty($record['office'])
254 && $record['office'] = '+27' . $record['office'];
255 !empty($record['fax'])
256 && $record['fax'] = '+27' . $record['fax'];
258 #-> Check if this will cause duplicate.
259 $existing = $this->em
260 ->getRepository('\User\Entity\Profile')
262 'email' => $record['email']
264 if (!is_null($existing))
266 $errors[] = 'Existing Email on Profile';
268 $existing = $this->em
269 ->getRepository('\Company\Entity\Company')
271 'name' => $record['company_name']
273 if (!is_null($existing))
275 $errors[] = 'Existing Name on Company';
277 if (0 < count($errors))
280 $record[] = implode('; ', $errors);
282 $exceptionLog->putRecord($record);
288 $session = new \Workspace\Utility\Param();
289 $session->Contact = array(
290 'firstName' => $record['first_name'],
291 'familyName' => $record['family_name'],
292 'mobile' => $record['mobile'],
293 'office' => $record['office'],
294 'fax' => $record['fax'],
295 'email' => $record['contact_email']
297 $session->Profile = array(
298 'firstName' => $record['first_name'],
299 'familyName' => $record['family_name'],
300 'mobile' => $record['mobile'],
301 'office' => $record['office'],
302 'email' => $record['email'],
303 'idNumber' => $record['id_number'],
304 'dateOfBirth' => date('Y-m-d', mktime(
306 substr($record['id_number'], 2, 2),
307 substr($record['id_number'], 4, 2),
308 '19' . substr($record['id_number'], 0, 2)
311 $groupId = $this->getGroupId($record['group']);
312 $session->Company = array(
313 'name' => $record['company_name'],
314 'businessName' => $record['business_name'],
315 'region' => $regionId,
316 'city' => $this->getCityId($regionId, $record['city']),
317 'postalCode' => $record['postal_code'],
318 'street' => $record['street'],
319 'dealerType' => 'Trader',
320 'clientType' => 'Buyer' == $record['type'] ? 'Buyer' : 'Buyer & Seller',
321 'turmiNumber' => '7777',
323 'groupDivision' => $this->getDivisionId($groupId, $record['division']),
324 'jobState' => 'Active',
325 'statusReason' => 'Auction user automated import.'
327 $folderId = array_shift($record);
328 array_unshift($record, $folderId);
329 if (file_exists($docsFolder . $folderId))
331 echo "\nFound folder " . $folderId . ":\n";
332 $dir = dir($docsFolder . $folderId);
333 while ($entry = $dir->read())
335 if ('.' == $entry || '..' == $entry || 'Thumbs.db' == $entry)
339 $parts = explode('.', $entry);
340 if (count($parts) > 2)
342 $errors[] = "Found file of unknown type: $entry";
345 $fileName = $parts[0];
346 $fileExt = $parts[1];
347 if (in_array(strtoupper($fileName), array('ID', 'POA', 'COREG')))
349 $document = new \Utility\Entity\Document();
350 $document->filename = $entry;
351 $document->mimeType = finfo_file($finfo, $docsFolder . $folderId . '/' . $entry);
352 $this->em->persist($document);
353 $this->em->flush($document);
354 $document->filename = $document->id . '.' . $fileExt;
355 $this->em->flush($document);
357 $docsPut . $document->filename,
358 file_get_contents($docsFolder . $folderId . '/' . $entry)
360 $docField = $docTypeMap[strtoupper($fileName)];
361 $session->Company[$docField] = $document->id;
365 $errors[] = "Found file of unknown type: $entry";
371 $errors[] = 'No document folder found.';
374 #-> Create relevant entries.
377 #-> Transaction based for safety.
378 $this->em->beginTransaction();
381 $password = 1111; //mt_rand(10000, 99999);
382 $contact = $contactService->create($session->Contact);
383 $session->Company['contact'] = $contact->id;
384 $company = $companyService->create($session->Company);
385 $company->jobState = 'Active';
386 $session->Profile['company'] = $company->id;
387 $session->Profile['permissions'] = 'Buyer' == $record['type']
390 $session->Profile['password'] = $password;
391 $profile = $profileService->create($session->Profile);
393 #-> Send welcome notification.
394 $notificationLog->putRecord(array($profile->id, $profile->email));
403 if (0 < count($errors))
405 $record[] = implode('; ', $errors);
407 $exceptionLog->putRecord($record);
411 catch (\Exception $e)
413 $this->em->rollback();
414 $errors[] = $e->getMessage();
415 $record[] = implode('; ', $errors);
417 $exceptionLog->putRecord($record);
419 echo "-----------------------\n";
420 echo $e->getMessage() . "\n";
426 echo "\n\n------------------------------------------------------\n";
427 if (0 == $exceptions)
429 echo "No exceptions for import file: $importFile \n";
430 $exceptionLog->__destruct();
431 unlink($exceptionFile);
435 echo "$exceptions exceptions for import file: $importFile \n";
436 echo "Exception file: $exceptionFile \n";
439 echo "Finished processing: $importFile \n";