initial commit
[namibia] / module / Company / src / Company / Service / Company.php
1 <?php
2 namespace Company\Service;
3
4
5
6 use PriceGuide;
7 /**
8  * Manage Company data.
9  * @author andre.fourie
10  */
11 class Company extends \Company\DataBin\Company
12 {
13
14
15         /**
16          * ExecuteAfter: Update.
17          * Send welcome notification with new pin for user just registered/created on system.
18          * @param array $meta
19          * @param object|null $jobRecord
20          * @param object|null $record
21          * @param \Workspace\Utility\ServiceInputParams $contract
22          * @return array
23          */
24         public function updateAuthSession($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
25         {
26                 #-> Do we need to update session data?
27                 $auth = \Utility\Registry::getAuthData();
28                 if ($auth['company']['id'] == $record->id)
29                 {
30                         $companyData = $record->toArray(array(
31                                                 'city', 'region', 'contact'
32                         ));
33                         \Utility\Registry::setAuthParam('company', $companyData);
34                 }
35         }
36
37         public function linkPublicCentreClubs($companyId)
38         {
39                 set_time_limit(0);
40                 ini_set('memory_limit','1024M');
41
42                 $record = $this->em->find('Company\\Entity\\Company', $companyId);
43                 $dealershipName = $record->name;
44                 $companyId = $record->id;
45                 $isPublicCentre = $record->publicCentre;
46                 $today = new \DateTime ();
47                 $em = \Utility\Registry::getEntityManager ();
48
49                 $dealershipName = $dealershipName . ' Traders Club';
50
51                 //\Utility\Debug::errorLog ( 'New dealer updated $dealershipName', $dealershipName );
52                 //\Utility\Debug::errorLog ( 'New dealer updated company id', $companyId );
53                 //\Utility\Debug::errorLog ( 'New dealer updated is publicCentre', $isPublicCentre );
54
55                 // create an new price guide club (dealership name Traders Club)
56                 // Do a check for club
57                 $club = $em->getRepository ( '\PriceGuide\Entity\Club' )->findOneBy ( array (
58                                 'name' => $dealershipName
59                 ) );
60                 // > Create entry.
61
62                 // Create if nothing is found
63                 if (is_null ( $club ))
64                 {
65                         //\Utility\Debug::errorLog ( 'New dealer wasnt found', $dealershipName );
66
67                         $publicClub = new \PriceGuide\Entity\Club ();
68                         $publicClubData = array (
69                                         'company' => $em->getRepository ( '\Company\Entity\Company' )->find ( $companyId ),
70                                         'name' => $dealershipName,
71                                         'status' => 'Active',
72                                         'created' => $today
73                         )
74                         ;
75
76                         $publicClub->fromArray ( $publicClubData );
77                         $em->persist ( $publicClub );
78                         $em->flush ();
79
80                         // Get id back
81                         $publicClubId = $publicClub->id;
82
83                         //\Utility\Debug::errorLog ( 'New dealer created club id', $publicClubId );
84                 }
85                 else
86                 {
87                         //\Utility\Debug::errorLog ( 'New dealer updated is $club', $club->id );
88                         $publicClubId = $club->id;
89                 }
90
91                 // Select from company where where dealer_type = "Trader"
92                 $traderSearch = "Trader";
93                 $result = $this->em->createQuery ( 'SELECT company ' . 'FROM \Company\Entity\Company company ' . 'WHERE company.dealerType = :trader ' )
94                         ->setParameter ( 'trader', $traderSearch )->getArrayResult ();
95
96                 //\Utility\Debug::errorLog ( 'add this club', $publicClubId );
97
98                 foreach ( $result as $companys )
99                 {
100
101                         //\Utility\Debug::errorLog ( 'all the $companys', $companys );
102
103                         // get all the profiles $override
104                         $overridePgSearch = '1';
105                         $profileResult = $this->em->createQuery ( 'SELECT profile, override ' . 'FROM User\Entity\Profile profile ' . 'LEFT JOIN profile.override override ' . 'WHERE IDENTITY(profile.company) = :companyID ' . 'AND override.pgMakeOffer = :overridePG ' )
106
107                         ->setParameter ( 'companyID', $companys ['id'] )->setParameter ( 'overridePG', $overridePgSearch )->
108
109                         getArrayResult ();
110
111                         foreach ( $profileResult as $clubProfiles ) {
112
113                                 //\Utility\Debug::errorLog ( 'all the profiles found', $clubProfiles );
114
115                                 $existingClubMember = $em->getRepository ( '\PriceGuide\Entity\Member' )->findOneBy ( array (
116                                                 'club' => $em->getRepository ( '\PriceGuide\Entity\Club' )->find ( $publicClubId ),
117                                                 'company' => $em->getRepository ( '\Company\Entity\Company' )->find ( $companys ['id'] ),
118                                                 'profile' => $em->getRepository ( '\User\Entity\Profile' )->find ( $clubProfiles ['id'] )
119                                 )
120                                  );
121                                 // Set all listed traders as active members with email notifications on and sms off
122                                 if (is_null ( $existingClubMember )) {
123                                         // add new member
124                                         //\Utility\Debug::errorLog ( 'add this trader', $companys );
125
126                                         $publicClubMember = new \PriceGuide\Entity\Member ();
127                                         $publicClubMemberData = array (
128                                                         'club' => $em->getRepository ( '\PriceGuide\Entity\Club' )->find ( $publicClubId ),
129                                                         'company' => $em->getRepository ( '\Company\Entity\Company' )->find ( $companys ['id'] ),
130
131                                                         'profile' => $em->getRepository ( '\User\Entity\Profile' )->find ( $clubProfiles ['id'] ),
132
133                                                         'allMakes' => '1',
134                                                         'emailNotification' => '1',
135                                                         'smsNotification' => '0',
136                                                         'status' => 'Active',
137                                                         'created' => $today
138                                         )
139                                         ;
140
141                                         $publicClubMember->fromArray ( $publicClubMemberData );
142                                         $em->persist ( $publicClubMember );
143                                         $em->flush ();
144                                 }
145                         } // ProfileClub
146                 } // For each company found
147
148         }
149
150         /**
151          * ExecuteAfter: Update.
152          * Link all the trade center clubs for this dealer that is a public centre.
153          * @param array $meta
154          * @param object|null $jobRecord
155          * @param object|null $record
156          * @param \Workspace\Utility\ServiceInputParams $contract
157          * @return array
158          */
159         public function PublicCentreClubs($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
160         {
161                 if ($record->publicCentre == 'True' || $record->publicCentre == '1')
162                 {
163                         exec("php /var/www/B4C2/public/index.php publicclubs process "
164                                 . $record->id
165                                 . " >>/log/php.log &");
166                 }// if is public centre
167         }
168
169         public function directRouteMoveToPending($jobRecord)
170         {
171                 $profile = $this->em->getRepository('\User\Entity\Profile')
172                         ->findOneBy(array('company' => $jobRecord->id), array('id' => 'ASC'));
173                 $oNotify = new \Utility\Comms\Notification();
174                 $oNotify->sendFromTemplate(
175                                 null, null,
176                                 $jobRecord->id, $profile->id,
177                                 $profile->email, null,
178                                 null,
179                                 'registration-pending',
180                                 array(
181                                         'firstName'     => $profile->firstName,
182                                         'familyName'    => $profile->familyName,
183                                         'status_reason' => $jobRecord->statusReason
184                                 )
185                 );
186         }
187
188         public function directRouteMoveToActive($jobRecord)
189         {
190                 $profile = $this->em->getRepository('\User\Entity\Profile')
191                         ->findOneBy(array('company' => $jobRecord->id), array('id' => 'ASC'));
192                 $oNotify = new \Utility\Comms\Notification();
193                 $oNotify->sendFromTemplate(
194                                 null, null,
195                                 $jobRecord->id, $profile->id,
196                                 $profile->email, null,
197                                 null,
198                                 'registration-success',
199                                 array(
200                                         'firstName'  => $profile->firstName,
201                                         'familyName' => $profile->familyName,
202                                         'email'      => $profile->email
203                                 )
204                 );
205         }
206
207         public function directRouteMoveToDeclined($jobRecord)
208         {
209                 $profile = $this->em->getRepository('\User\Entity\Profile')
210                         ->findOneBy(array('company' => $jobRecord->id), array('id' => 'ASC'));
211                 $oNotify = new \Utility\Comms\Notification();
212                 $oNotify->sendFromTemplate(
213                                 null, null,
214                                 $jobRecord->id, $profile->id,
215                                 $profile->email, null,
216                                 null,
217                                 'registration-declined',
218                                 array(
219                                         'firstName'     => $profile->firstName,
220                                         'familyName'    => $profile->familyName,
221                                         'status_reason' => $jobRecord->statusReason
222                                 )
223                 );
224         }
225
226         /**
227          * List of companies that can be invited.
228          * @param object|null $jobRecord
229          * @param array $input
230          * @return \Workspace\Contract\UseOnce
231          */
232         public function contractInviteSelectList($jobRecord, array $input = array())
233         {
234                 $options = new \Workspace\UseCase\Options();
235                 $requirement = new \Workspace\UseCase\Requirement();
236                 $requirement->addRequiredInput(array(
237                                 'Filter' => array(
238                                                 'club'  => 'Id'
239                                 )
240                 ));
241                 return new \Workspace\Contract\Recurring($options, $requirement);
242         }
243
244         /**
245          * Reset pin for user.
246          * @param object|null $jobRecord
247          * @param \Workspace\Utility\ServiceInputParams $contract
248          * @return array
249          */
250         public function executeInviteSelectList($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
251         {
252                 $em = \Utility\Registry::getEntityManager();
253                 $authData = \Utility\Registry::getAuthData();
254                 $companyId = $authData['company']['id'];
255                 $clubId = $contract->data->Filter['club'];
256                 $memberslistdrop = $em->createQuery('
257                                 SELECT c.id, c.name AS companyName
258                                 FROM \Company\Entity\Company c
259                                 WHERE c.id NOT IN (
260                                         SELECT IDENTITY(m.company)
261                                         FROM PriceGuide\Entity\Member m
262                                         WHERE IDENTITY(m.club) = :clubId
263                                 )
264                                 AND c.priceGuide = :activeid
265                                 AND c.id != :company
266                                 ORDER BY c.name'
267                 );
268                 $memberslistdrop->setParameter('clubId', $clubId);
269                 $memberslistdrop->setParameter('activeid', '1');
270                 $memberslistdrop->setParameter('company', $companyId);
271                 $memberslistu = $memberslistdrop->getArrayResult();
272                 $datax = array();
273                 foreach ( $memberslistu as $data ) {
274
275
276                         $datax[] = array(
277                                 'value' => $data['id'],
278                                 'label' => $data['companyName']
279                                 );
280                 }
281                 return $contract->success('List Retrieved.', $datax);
282         }
283
284
285
286         /**
287          * Contract to remove a document from a company.
288          * @param object|null $jobRecord
289          * @param array $input
290          * @return \Workspace\Contract\UseOnce
291          */
292         public function contractRemoveDocument($jobRecord, array $input = array())
293         {
294                 $options = new \Workspace\UseCase\Options();
295                 $requirement = new \Workspace\UseCase\Requirement();
296                 $requirement->addOptionalInput(array(
297                                 'Company' => array(
298                                                 'docStockCertificate'           => 'Boolean',
299                                                 'docAddressProof'                       => 'Boolean',
300                                                 'docCopyOfId'                           => 'Boolean',
301                                                 'docCopyOfDirectorId'           => 'Boolean',
302                                                 'docCompanyRegistration'        => 'Boolean'
303                                 )
304                 ));
305                 return new \Workspace\Contract\Recurring($options, $requirement);
306         }
307
308         /**
309          * Remove a document from a company.
310          * @param object|null $jobRecord
311          * @param \Workspace\Utility\ServiceInputParams $contract
312          * @return array
313          */
314         public function executeRemoveDocument($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
315         {
316                 $fields = array(
317                                 'docStockCertificate',
318                                 'docAddressProof',
319                                 'docCopyOfId',
320                                 'docCopyOfDirectorId',
321                                 'docCompanyRegistration'
322                 );
323                 $folder = \Utility\Registry::getConfigParam('DocumentPath');
324                 foreach ($fields as $field)
325                 {
326                         if (isset($contract->data->Company[$field])
327                                 && $contract->data->Company[$field])
328                         {
329                                 if (file_exists($folder . '/' . $jobRecord->$field->filename))
330                                 {
331                                         unlink($folder . '/' . $jobRecord->$field->filename);
332                                 }
333                                 $this->em->remove($jobRecord->$field);
334                                 $jobRecord->$field = null;
335                         }
336                 }
337                 $this->em->flush();
338                 return $contract->success('Document removed.', array());
339         }
340
341         /**
342          * List of companies that can be invited.
343          * @param object|null $jobRecord
344          * @param array $input
345          * @return \Workspace\Contract\UseOnce
346          */
347         public function checkDocuments()
348         {
349
350
351                 $result = $this->em->createQuery(
352                                 'SELECT company, docStockCertificate, docAddressProof, docCopyOfId, docCopyOfDirectorId, docCompanyRegistration  '
353                                 . 'FROM \Company\Entity\Company company '
354                                 . 'LEFT JOIN company.docStockCertificate docStockCertificate '
355                                 . 'LEFT JOIN company.docAddressProof docAddressProof '
356                                 . 'LEFT JOIN company.docCopyOfId docCopyOfId '
357                                 . 'LEFT JOIN company.docCopyOfDirectorId docCopyOfDirectorId '
358                                 . 'LEFT JOIN company.docCompanyRegistration docCompanyRegistration '
359                                 . 'WHERE docStockCertificate != :null '
360                                 . 'OR docAddressProof != :null '
361                                 . 'OR docCopyOfId != :null '
362                                 . 'OR docCopyOfDirectorId != :null '
363                                 . 'OR docCompanyRegistration != :null '
364
365                 )
366                 ->setParameter('null', 'null')
367                 ->getArrayResult();
368
369
370                 $folder = \Utility\Registry::getConfigParam('DocumentPath');
371
372
373                 $missingDocs = array();
374                 $docs = array(
375                                 'docStockCertificate',
376                                 'docAddressProof',
377                                 'docCopyOfId',
378                                 'docCopyOfDirectorId',
379                                 'docCompanyRegistration',
380                 );
381
382                 foreach ($result as $companies)
383                 {
384                         $companyId = $companies['id'];
385                         $missingDocs[$companyId] = array(
386                                         'company'                                       =>  $companies['name'] . ' / ' . $companies['businessName'],
387                                         'docStockCertificate'           => false,
388                                         'docAddressProof'                       => false,
389                                         'docCopyOfId'                           => false,
390                                         'docCopyOfDirectorId'           => false,
391                                         'docCompanyRegistration'        => false
392                         );
393                         $missing = 0;
394                         foreach ($docs as $doc)
395                         {
396                                 if (is_array($companies[$doc]) && !is_null($companies[$doc]['id']) )
397                                 {
398                                         if (!file_exists($folder . '/' . $companies[$doc]['filename']))
399                                         {
400                                                 $missing++;
401                                                 $missingDocs[$companyId][$doc] = $companies[$doc]['filename'];
402                                         }
403                                 }
404                         }
405                         if (0 == $missing)
406                         {
407                                 unset($missingDocs[$companyId]);
408                         }
409
410                 }
411
412                 $fp = fopen($folder . '/missingDocsFilelist.csv', 'w');
413                 fputcsv($fp, array(
414                                 'Company name',
415                                 'Stock Certificate',
416                                 'Document Address Proof',
417                                 'Document Copy Of Id',
418                                 'Document Copy Of DirectorId',
419                                 'Document Company Registration'
420                 ));
421                 foreach ($missingDocs as $companyId => $fields) {
422                         fputcsv($fp, $fields);
423                 }
424
425                 fclose($fp);
426
427         }
428
429
430
431
432 }