text changes to registration mail content
[namibia] / module / User / src / User / Service / Import.php
1 <?php
2 namespace User\Service;
3
4
5
6 /**
7  * Manage Adherence data.
8  * @author andre.fourie
9  */
10 class Import extends \Workspace\Service\DataBin
11 {
12
13         /*
14          * Utilities.
15          */
16         protected $regions = array();
17         protected function getRegionId($regionName)
18         {
19                 if (isset($this->regions[strtolower($regionName)]))
20                 {
21                         return $this->regions[strtolower($regionName)];
22                 }
23                 $region = $this->em->getRepository('\Location\Entity\Region')
24                         ->findOneBy(array(
25                                 'name' => $regionName
26                         ));
27                 $this->regions[strtolower($regionName)] = !is_null($region)
28                         ? $region->id
29                         : null;
30                 return $this->regions[strtolower($regionName)];
31         }
32
33         protected $cities = array();
34         protected function getCityId($regionId, $cityName)
35         {
36                 if (isset($this->cities[strtolower($cityName)]))
37                 {
38                         return $this->cities[strtolower($cityName)];
39                 }
40                 $city = $this->em->getRepository('\Location\Entity\Town')
41                         ->findOneBy(array(
42                                 'region'        => $regionId,
43                                 'name'          => $cityName
44                         ));
45                 if (is_null($city))
46                 {
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);
52                 }
53                 $this->cities[strtolower($cityName)] = $city->id;
54                 return $this->cities[strtolower($cityName)];
55         }
56
57         protected $groups = array();
58         protected function getGroupId($groupName)
59         {
60                 if (isset($this->groups[strtolower($groupName)]))
61                 {
62                         return $this->groups[strtolower($groupName)];
63                 }
64                 $group = $this->em->getRepository('\Company\Entity\Group')
65                         ->findOneBy(array(
66                                 'name'          => $groupName
67                         ));
68                 if (is_null($group))
69                 {
70                         $group = new \Company\Entity\Group();
71                         $group->name = trim($groupName);
72                         $this->em->persist($group);
73                         $this->em->flush($group);
74                 }
75                 $this->groups[strtolower($groupName)] = $group->id;
76                 return $this->groups[strtolower($groupName)];
77         }
78
79         protected $divisions = array();
80         protected function getDivisionId($groupId, $divisionName)
81         {
82                 if (isset($this->divisions[strtolower($divisionName)]))
83                 {
84                         return $this->divisions[strtolower($divisionName)];
85                 }
86                 $division = $this->em->getRepository('\Company\Entity\GroupDivision')
87                         ->findOneBy(array(
88                                 'group'         => $groupId,
89                                 'name'          => $divisionName
90                         ));
91                 if (is_null($division))
92                 {
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);
98                 }
99                 $this->divisions[strtolower($divisionName)] = $division->id;
100                 return $this->divisions[strtolower($divisionName)];
101         }
102
103         public function notifyProcess()
104         {
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();
110                 //$i = 0;
111                 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false)
112                 {
113                         /* $i++;
114                         if (11 == $i)
115                         {
116                                 echo "TaDaaaa!\n";
117                                 exit();
118                         } */
119                         $profile = $this->em->find('\User\Entity\Profile', $record['id']);
120                         $password = mt_rand(10000, 99999);
121                         $profile->fromArray(array(
122                                 'password' => $password
123                         ));
124                         $profile->lastLogin = null;
125                         $this->em->flush($profile);
126                         $oNotify->sendFromTemplate(
127                                         null, null,
128                                         $profile->company->id, $profile->id,
129                                         $profile->email, null, null,
130                                         'new-profile-auction',
131                                         array(
132                                                         'firstName'     => $profile->firstName,
133                                                         'familyName'    => $profile->familyName,
134                                                         'email'                 => $profile->email,
135                                                         'pin'                   => $password
136                                         )
137                         );
138                 }
139         }
140
141
142         /**
143          * CRON functionality: Import auction users from csv.
144         */
145         public function cronProcess()
146         {
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';
154
155                 #-> Some data mappings.
156                 $regionMap = array(
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'
166                 );
167                 $docTypeMap = array(
168                                 'ID'    => 'docCopyOfId',
169                                 'POA'   => 'docAddressProof',
170                                 'COREG' => 'docCompanyRegistration'
171                 );
172
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);
182
183                 #-> Import
184                 echo "Processing $importFile\n";
185                 if (!file_exists($importFile))
186                 {
187                         echo "File not found!\n";
188                         return;
189                 }
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'));
197                 $exceptions = 0;
198                 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false)
199                 {
200                         #-> Safety checks.
201                         $errors = array();
202                         foreach ($record as $field => $value)
203                         {
204                                 $record[$field] = trim($value);
205                         }
206                         if (!$emailValidator->isValid($record['email']))
207                         {
208                                 $errors[] = 'V:email';
209                         }
210                         if (0 == strlen($record['first_name']))
211                         {
212                                 $errors[] = 'V:first_name';
213                         }
214                         if (0 == strlen($record['family_name']))
215                         {
216                                 $errors[] = 'V:family_name';
217                         }
218                         if (0 == strlen($record['company_name']))
219                         {
220                                 $errors[] = 'V:company_name';
221                         }
222                         if (0 == strlen($record['street']))
223                         {
224                                 $errors[] = 'V:street';
225                         }
226                         if (1111111111111 == strlen($record['id_number']))
227                         {
228                                 $errors[] = 'V:id_number';
229                         }
230                         if ('Seller & Buyer' != $record['type']
231                                 && 'Buyer' != $record['type'])
232                         {
233                                 $errors[] = 'V:type';
234                         }
235                         if (!is_numeric($record['postal_code']))
236                         {
237                                 $record['postal_code'] = '';
238                         }
239                         $regionId = $this->getRegionId($regionMap[$record['region']]);
240                         if (is_null($regionId))
241                         {
242                                 $errors[] = 'V:region';
243                         }
244                         if (10 < strlen($record['mobile'])
245                                 || !is_numeric($record['mobile']))
246                         {
247                                 $errors[] = 'V:mobile';
248                         }
249                         else
250                         {
251                                 $record['mobile'] = '+27' . $record['mobile'];
252                         }
253                         !empty($record['office'])
254                                 && $record['office'] = '+27' . $record['office'];
255                         !empty($record['fax'])
256                                 && $record['fax'] = '+27' . $record['fax'];
257
258                         #-> Check if this will cause duplicate.
259                         $existing = $this->em
260                                 ->getRepository('\User\Entity\Profile')
261                                 ->findOneBy(array(
262                                         'email'         => $record['email']
263                                 ));
264                         if (!is_null($existing))
265                         {
266                                 $errors[] = 'Existing Email on Profile';
267                         }
268                         $existing = $this->em
269                                 ->getRepository('\Company\Entity\Company')
270                                 ->findOneBy(array(
271                                         'name'  => $record['company_name']
272                                 ));
273                         if (!is_null($existing))
274                         {
275                                 $errors[] = 'Existing Name on Company';
276                         }
277                         if (0 < count($errors))
278                         {
279                                 echo "! \n";
280                                 $record[] = implode('; ', $errors);
281                                 $record[] = 'No';
282                                 $exceptionLog->putRecord($record);
283                                 $exceptions++;
284                                 continue;
285                         }
286
287                         #-> Build datasets.
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']
296                         );
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(
305                                                 1, 1, 1,
306                                                 substr($record['id_number'], 2, 2),
307                                                 substr($record['id_number'], 4, 2),
308                                                 '19' . substr($record['id_number'], 0, 2)
309                                 ))
310                         );
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',
322                                 'group'                 => $groupId,
323                                 'groupDivision' => $this->getDivisionId($groupId, $record['division']),
324                                 'jobState'              => 'Active',
325                                 'statusReason'  => 'Auction user automated import.'
326                         );
327                         $folderId = array_shift($record);
328                         array_unshift($record, $folderId);
329                         if (file_exists($docsFolder . $folderId))
330                         {
331                                 echo "\nFound folder " . $folderId . ":\n";
332                                 $dir = dir($docsFolder . $folderId);
333                                 while ($entry = $dir->read())
334                                 {
335                                         if ('.' == $entry || '..' == $entry || 'Thumbs.db' == $entry)
336                                         {
337                                                 continue;
338                                         }
339                                         $parts = explode('.', $entry);
340                                         if (count($parts) > 2)
341                                         {
342                                                 $errors[] = "Found file of unknown type: $entry";
343                                                 continue;
344                                         }
345                                         $fileName = $parts[0];
346                                         $fileExt = $parts[1];
347                                         if (in_array(strtoupper($fileName), array('ID', 'POA', 'COREG')))
348                                         {
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);
356                                                 file_put_contents(
357                                                         $docsPut . $document->filename,
358                                                         file_get_contents($docsFolder . $folderId . '/' . $entry)
359                                                 );
360                                                 $docField = $docTypeMap[strtoupper($fileName)];
361                                                 $session->Company[$docField] = $document->id;
362                                         }
363                                         else
364                                         {
365                                                 $errors[] = "Found file of unknown type: $entry";
366                                         }
367                                 }
368                         }
369                         else
370                         {
371                                 $errors[] = 'No document folder found.';
372                         }
373
374                         #-> Create relevant entries.
375                         try
376                         {
377                                 #-> Transaction based for safety.
378                                 $this->em->beginTransaction();
379
380                                 #-> Create entries.
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']
388                                         ? 12
389                                         : 13;
390                                 $session->Profile['password'] = $password;
391                                 $profile = $profileService->create($session->Profile);
392
393                                 #-> Send welcome notification.
394                                 $notificationLog->putRecord(array($profile->id, $profile->email));
395                                 echo '.';
396
397
398                                 #-> Cleanup.
399                                 $this->em->commit();
400                                 $this->em->clear();
401
402                                 #-> Log errors.
403                                 if (0 < count($errors))
404                                 {
405                                         $record[] = implode('; ', $errors);
406                                         $record[] = 'Yes';
407                                         $exceptionLog->putRecord($record);
408                                         $exceptions++;
409                                 }
410                         }
411                         catch (\Exception $e)
412                         {
413                                 $this->em->rollback();
414                                 $errors[] = $e->getMessage();
415                                 $record[] = implode('; ', $errors);
416                                 $record[] = 'No';
417                                 $exceptionLog->putRecord($record);
418                                 $exceptions++;
419                                 echo "-----------------------\n";
420                                 echo $e->getMessage() . "\n";
421                                 var_dump($record);
422                                 echo "\n";
423                                 exit();
424                         }
425                 }
426                 echo "\n\n------------------------------------------------------\n";
427                 if (0 == $exceptions)
428                 {
429                         echo "No exceptions for import file: $importFile \n";
430                         $exceptionLog->__destruct();
431                         unlink($exceptionFile);
432                 }
433                 else
434                 {
435                         echo "$exceptions exceptions for import file: $importFile \n";
436                         echo "Exception file: $exceptionFile \n";
437                 }
438                 $csvImporter = null;
439                 echo "Finished processing: $importFile \n";
440         }
441
442 }