initial commit
[namibia] / module / PriceGuide / src / PriceGuide / Service / Member.php
1 <?php
2 namespace PriceGuide\Service;
3
4
5
6 /**
7  * Manage Member data.
8  * @author andre.fourie
9  */
10 class Member extends \PriceGuide\DataBin\Member
11 {
12
13
14         /**
15          * Create new entry. (Override DataBind service to we can handle duplicate invites)
16          * @param array $meta
17          * @param object|null $jobRecord
18          * @param \Workspace\Utility\ServiceInputParams $contract
19          * @return array
20          */
21         public function exeCreate($meta, $jobRecord, \Workspace\Utility\ServiceInputParams $contract)
22         {
23                 #-> Context data publication.
24                 isset($contract->data->Context)
25                         && \Utility\Registry::setOnce(
26                                 'Service.' . $this->meta['Base'] . '.Context',
27                                 $contract->data->Context
28                         );
29
30                 #-> Create entry.
31                 $base = $this->meta['Base'];
32                 $data = $contract->data->$base;
33                 $exist = $this->em->getRepository('\PriceGuide\Entity\Member')
34                         ->findOneBy(array(
35                                         'club'    => $data['club'],
36                                         'company' => $data['company']
37                         ));
38                 if (!is_null($exist))
39                 {
40                         return $contract->success('Entry created.', array(
41                                         'id' => $exist->id
42                         ));
43                 }
44                 if (isset($meta['RelatedEntityFromInput']))
45                 {
46                         foreach ($meta['RelatedEntityFromInput'] as $context => $relation)
47                         {
48                                 $workflow = $relation['Workflow'];
49                                 $service  = $relation['Service'];
50                                 $service  = new $service();
51                                 $service->setWorkflow(new $workflow());
52                                 $relId = $service->create($contract->data->$context)->id;
53                                 $data[$relation['Field']] = $relId;
54                         }
55                 }
56                 if (isset($this->meta['JobField']))
57                 {
58                                 $data[$this->meta['JobField']] = $jobRecord->id;
59                 }
60                 $record = $this->create($data);
61                 $this->inviteMember($meta, $record);
62
63                 #-> Done.
64                 return $contract->success('Entry created.', array(
65                         'id' => $record->id
66                 ));
67         }
68
69         /**
70          * ExecuteAfter: Invite.
71          * Send invitation notification.
72          *
73          * @param array $meta
74          * @param object|null $record
75          * @return array
76          */
77         public function inviteMember($meta, $record)
78         {
79                 #-> Send invite notification.
80                 $profile = $this->em
81                         ->getRepository('\User\Entity\Profile')
82                         ->findOneBy(array('company' => $record->company->id));
83                 if (!is_null($profile))
84                 {
85                         $record->profile = $profile;
86                         $this->em->flush($record);
87
88                         $company = $record->club->company;
89                         $params = array(
90                                 'clubType'                              => 'Price Guide club',
91                                 'dealer'                                => $company->name,
92                                 'contact.name'                  => $company->contact->firstName,
93                                 'contact.familyName'    => $company->contact->familyName,
94                                 'contact.email'                 => $company->contact->email,
95                                 'contact.cell'                  => $company->contact->mobile
96                         );
97                         $iterator = $profile->company->profiles->getIterator();
98                         $authData = \Utility\Registry::getAuthData();
99                         foreach ($iterator as $prof)
100                         {
101                                 $params['firstName']    = $prof->firstName;
102                                 $params['familyName']   = $prof->familyName;
103                                 $email                  = $prof->email;
104                                 $oNotify = new \Utility\Comms\Notification();
105                                 $oNotify->sendFromTemplate(
106                                                 $authData['company']['id'], $authData['id'],
107                                                 $profile->company->id, $prof->id,
108                                                 $email, null,
109                                                 null,
110                                                 'club-invite-registered',
111                                                 $params
112                                 );
113                         }
114                 }
115         }
116
117         /**
118          * ExecuteAfter: Accept.
119          * Member accepted invitation.
120          *
121          * @param array $meta
122          * @param object|null $jobRecord
123          * @param object|null $record
124          * @param \Workspace\Utility\ServiceInputParams $contract
125          * @return array
126          */
127         public function acceptInvite($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
128         {
129                 #-> Change status.
130                 $record->status = 'Active';
131                 $authData = \Utility\Registry::getAuthData();
132                 if (is_null($record->company))
133                 {
134                         $record->company = $this->em
135                                 ->getReference('\Company\Entity\Company', $authData['company']['id']);
136                 }
137                 if (is_null($record->profile))
138                 {
139                         $record->profile = $this->em
140                                 ->getReference('\User\Entity\Profile', $authData['id']);
141                 }
142
143                 $email = $record->club->company->contact->email;
144                 $params = array(
145                         'firstName'                             => $record->club->company->contact->firstName,
146                         'familyName'                            => $record->club->company->contact->familyName,
147                         'clubName'                                      => $record->club->name,
148                         'clubType'                                      => 'club',
149                         'dealer'                                        => $record->company->name,
150                         'dealer.contactFirstName'       => $record->company->contact->firstName,
151                         'dealer.contactFamilyName'      => $record->company->contact->familyName
152                 );
153
154                 $oNotify = new \Utility\Comms\Notification();
155                 $oNotify->sendFromTemplate(
156                                 $authData['company']['id'], $authData['id'],
157                                 $record->club->company->id, null,
158                                 $email, null,
159                                 null,
160                                 'club-invite-accepted',
161                                 $params);
162
163                 $this->em->flush();
164         }
165
166         /**
167          * ExecuteAfter: Decline.
168          * Member declined invitation.
169          *
170          * @param array $meta
171          * @param object|null $jobRecord
172          * @param object|null $record
173          * @param \Workspace\Utility\ServiceInputParams $contract
174          * @return array
175          */
176         public function declineInvite($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
177         {
178                 #-> Change status.
179                 $record->status = 'Declined';
180                 $authData = \Utility\Registry::getAuthData();
181                 if (is_null($record->company))
182                 {
183                         $record->company = $this->em
184                                 ->getReference('\Company\Entity\Company', $authData['company']['id']);
185                 }
186                 if (is_null($record->profile))
187                 {
188                         $record->profile = $this->em
189                                 ->getReference('\User\Entity\Profile', $authData['id']);
190                 }
191                 $this->em->flush();
192         }
193
194         /**
195          * ExecuteAfter: Activate.
196          *
197          * @param array $meta
198          * @param object|null $jobRecord
199          * @param object|null $record
200          * @param \Workspace\Utility\ServiceInputParams $contract
201          * @return array
202          */
203         public function activateMembership($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
204         {
205                 #-> Change status.
206                 $record->status = 'Active';
207                 $this->em->flush();
208         }
209
210         /**
211          * ExecuteAfter: Deactivate.
212          *
213          * @param array $meta
214          * @param object|null $jobRecord
215          * @param object|null $record
216          * @param \Workspace\Utility\ServiceInputParams $contract
217          * @return array
218          */
219         public function deactivateMembership($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
220         {
221                 #-> Change status.
222                 $record->status = 'Inactive';
223                 $this->em->flush();
224         }
225
226         /**
227          * ExecuteAfter: Update.
228          * Check if settings should be applied to all club memberships.
229          *
230          * @param array $meta
231          * @param object|null $jobRecord
232          * @param object|null $record
233          * @param \Workspace\Utility\ServiceInputParams $contract
234          * @return array
235          */
236         public function checkApplyAll($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
237         {
238                 #-> Change status.
239                 if ($contract->data->Member['applyToAll'])
240                 {
241                         $authData = \Utility\Registry::getAuthData();
242                         $companyId = 0;
243                         switch (\Utility\Registry::getUserType())
244                         {
245                                 case 'B4C User':
246                                 case 'Group User':
247                                 case 'Dealer Principle':
248                                         $companyFilter = \Utility\Registry::getSudo('Company', false);
249                                         if ($companyFilter)
250                                         {
251                                                 $companyId = $companyFilter;
252                                         }
253                                         break;
254                                 default:
255                                         $companyId = $authData['company']['id'];
256                                         break;
257                         }
258                         $memberships = $this->em->getRepository('\\PriceGuide\\Entity\\Member')
259                                 ->findBy(array(
260                                         'company' => $companyId
261                                 ));
262                         foreach ($memberships as $membership)
263                         {
264                                 if ($membership->id != $record->id)
265                                 {
266                                         #-> Remove existing makes.
267                                         $iterator = $membership->makes->getIterator();
268                                         foreach ($iterator as $mk)
269                                         {
270                                                 $this->em->remove($mk);
271                                         }
272                                         if (!$record->allMakes)
273                                         {
274                                                 #-> Transfer new list of makes.
275                                                 $iterator = $record->makes->getIterator();
276                                                 foreach ($iterator as $mk)
277                                                 {
278                                                         $memberMake = new \PriceGuide\Entity\MemberMake();
279                                                         $memberMake->make = $this->em->getReference('\Stock\Entity\Make', $mk->make->id);
280                                                         $memberMake->member = $membership;
281                                                         $this->em->persist($memberMake);
282                                                 }
283                                         }
284                                         $membership->allMakes = $record->allMakes;
285                                         $membership->fromYear = $record->fromYear;
286                                         $membership->toYear = $record->toYear;
287                                         $membership->fromKm = $record->fromKm;
288                                         $membership->toKm = $record->toKm;
289                                         $membership->emailNotification = $record->emailNotification;
290                                         $membership->smsNotification = $record->smsNotification;
291
292                                 }
293                         }
294                         $this->em->flush();
295                 }
296         }
297
298         /**
299          * ExecuteAfter: Archive.
300          * Set status to Archived.
301          *
302          * @param array $meta
303          * @param object|null $jobRecord
304          * @param object|null $record
305          * @param \Workspace\Utility\ServiceInputParams $contract
306          * @return array
307          */
308         public function archiveMember($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
309         {
310                 #-> Change status.
311                 $record->status = 'Archived';
312                 $this->em->flush();
313         }
314
315
316
317 }