text changes to registration mail content
[namibia] / module / Report / src / Report / Report / Sms.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * SMS Report.
8  * @author andre.fourie
9  */
10 class Sms extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'Sms Report';
19         /**
20          * @var string
21          */
22         protected $_subject = 'Sms';
23         /**
24          * @var string
25          */
26         protected $_description = 'Historical report for sms notifications.';
27         /**
28          * @var array
29          */
30         protected $_notes = array(
31                         'Confidential information, generated using Bid 4 Cars, for more information visit bid4cars.com.na'
32         );
33         /**
34          * @var array
35          */
36         protected $_headers = array(
37                         'Originator Group',
38                         'Originator Division',
39                         'Originator Dealership',
40                         'Originator User Name',
41                         'Originator User Surname',
42                         'Date & Time Sent',
43                         'Receiver Group',
44                         'Receiver Division',
45                         'Receiver Dealership',
46                         'Receiver Name',
47                         'Receiver Surname',
48                         'Receiver Mobile Number',
49                         'Message',
50                         'Character Count',
51                         'Click Count',
52                         'Value per Click',
53                         'Total'
54         );
55         /**
56          * @var array
57          */
58         protected $_fields = array(
59                         'fromGroup_name',
60                         'fromDivision_name',
61                         'fromCompany_name',
62                         'fromProfile_firstName',
63                         'fromProfile_familyName',
64                         'log_created',
65                         'toGroup_name',
66                         'toDivision_name',
67                         'toCompany_name',
68                         'toProfile_firstName',
69                         'toProfile_familyName',
70                         'log_smsTo',
71                         'log_smsBody',
72                         'charCount',
73                         'clickCount',
74                         'clickValue',
75                         'total'
76         );
77         /**
78          * @var array
79          */
80         protected $_totals = array(
81                         'charCount',
82                         'clickCount',
83                         'clickValue',
84                         'total'
85         );
86         /**
87          * @var array
88          */
89         protected $_currencyFields = array(
90                         'clickValue',
91                         'total'
92         );
93
94
95
96         /**
97          * Build the dataset.
98          */
99         public function build()
100         {
101                 #-> Prepare parameters.
102                 $selection = array(
103                                 'log',
104                                 'fromCompany', 'fromGroup', 'fromDivision', 'fromProfile',
105                                 'toCompany', 'toGroup', 'toDivision', 'toProfile',
106                 );
107                 $join      = array();
108                 $leftJion  = array();
109                 $where     = array();
110                 $params    = array();
111
112                 ini_set('memory_limit','512M');
113
114                 $query = 'SELECT '
115                                 . 'fromCompany.name AS fromCompany_name, fromGroup.name AS fromGroup_name, fromDivision.name AS fromDivision_name, '
116                                 . 'fromProfile.firstName AS fromProfile_firstName, fromProfile.familyName AS fromProfile_familyName, '
117                                 . 'log.created AS log_created, log.smsTo AS log_smsTo, log.smsBody AS log_smsBody, '
118                                 . 'toCompany.name AS toCompany_name, toGroup.name AS toGroup_name, toDivision.name AS toDivision_name, '
119                                 . 'toProfile.firstName AS toProfile_firstName, toProfile.familyName AS toProfile_familyName '
120                                 . 'FROM \Utility\Entity\NotificationLog log '
121                                 . ' LEFT JOIN log.fromCompany fromCompany '
122                                 . ' LEFT JOIN fromCompany.group fromGroup '
123                                 . ' LEFT JOIN fromCompany.groupDivision fromDivision '
124                                 . ' LEFT JOIN log.fromProfile fromProfile '
125                                 . ' LEFT JOIN log.toCompany toCompany '
126                                 . ' LEFT JOIN toCompany.group toGroup '
127                                 . ' LEFT JOIN toCompany.groupDivision toDivision '
128                                 . ' LEFT JOIN log.toProfile toProfile '
129                                 . '[WHERE] '
130                                 . 'ORDER BY log_created ASC';
131                 $where[] = 'log.smsTo IS NOT NULL AND log.smsBody != \'\'';
132
133                 #-> Construct details.
134                 $authData = \Utility\Registry::getAuthData();
135                 $clickVal = isset($this->_input['clickVal']) && is_numeric($this->_input['clickVal'])
136                         ? $this->_input['clickVal']
137                         : 0.25;
138                 if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
139                 {
140                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
141                         $where[] = 'log.created >= :dateFrom';
142                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
143                 }
144                 if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
145                 {
146                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
147                         $where[] = 'log.created <= :dateTo';
148                         $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
149                 }
150                 if (isset($this->_input['group'])
151                                 && !empty($this->_input['group'])
152                                 && 'null' != $this->_input['group'])
153                 {
154                         $this->_queries['Group'] = $this->em
155                                 ->find('\Company\Entity\Group', $this->_input['group'])
156                                 ->name;
157                         $where[] = 'fromGroup.id = :group';
158                         $params['group'] = $this->_input['group'];
159                 }
160                 if (isset($this->_input['groupDivision'])
161                                 && !empty($this->_input['groupDivision'])
162                                 && 'null' != $this->_input['groupDivision'])
163                 {
164                         error_log($this->_input['groupDivision']);
165                         $this->_queries['Division'] = $this->em
166                                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
167                                 ->name;
168                         $where[] = 'fromDivision.id = :division';
169                         $params['division'] = $this->_input['groupDivision'];
170                 }
171                 if (isset($this->_input['company'])
172                                 && !empty($this->_input['company'])
173                                 && 'null' != $this->_input['company'])
174                 {
175                         $this->_queries['Dealership'] = $this->em
176                                 ->find('\Company\Entity\Company', $this->_input['company'])
177                                 ->name;
178                         $where[] = 'fromCompany.id = :company';
179                         $params['company'] = $this->_input['company'];
180                 }
181
182                 #-> Finalize query.
183                 $query = str_replace(array(
184                                 '[WHERE]'
185                         ), array(
186                                 !empty($where)
187                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
188                                         : ' '
189                         ), $query);
190
191                 #-> Collect data.
192                 $query = $this->em->createQuery($query);
193                 !empty($params)
194                         && $query->setParameters($params);
195                 $result = $query->getResult();
196                 $data = array();
197                 $counter = 1;
198                 while(($record = array_shift($result)) != null)
199                 {
200                         $tmpRecord = array();
201                         $tmpRecord['fromGroup_name']             = $record['fromGroup_name'];
202                         $tmpRecord['fromDivision_name']          = $record['fromDivision_name'];
203                         $tmpRecord['fromCompany_name']           = $record['fromCompany_name'];
204
205                         $tmpRecord['fromProfile_firstName']  = $record['fromProfile_firstName'];
206                         $tmpRecord['fromProfile_familyName'] = $record['fromProfile_firstName'];
207
208                         $tmpRecord['log_created']                        = $record['log_created'];
209
210                         $tmpRecord['toGroup_name']                       = $record['toGroup_name'];
211                         $tmpRecord['toDivision_name']            = $record['toDivision_name'];
212                         $tmpRecord['toCompany_name']             = $record['toCompany_name'];
213
214                         $tmpRecord['toProfile_firstName']        = $record['toProfile_firstName'];
215                         $tmpRecord['toProfile_familyName']       = $record['toProfile_familyName'];
216
217                         $tmpRecord['log_smsTo']                          = $record['log_smsTo'];
218                         $tmpRecord['log_smsBody']                        = $record['log_smsBody'];
219                         $tmpRecord['charCount']                          = strlen($record['log_smsBody']);
220                         $tmpRecord['clickCount']                         = strlen($record['log_smsBody']) > 160
221                                 ? ceil(strlen($record['log_smsBody']) / (160 - 7))
222                                 : 1;
223
224                         $tmpRecord['clickValue']                         = $clickVal;
225                         $tmpRecord['total']                                      = round($tmpRecord['charCount'] * $clickVal, 2);
226
227                         $data[] = $tmpRecord;
228                         $counter++;
229                 }
230                 $this->_data = $data;
231         }
232
233 }