latest update
[namibia] / module / Report / src / Report / Report / Login.php
1 <?php
2
3 namespace Report\Report;
4
5 /**
6  * SMS Report.
7  * @author andre.fourie
8  */
9 class Login extends \Utility\Service\Report
10 {
11
12     /**
13      * @var string
14      */
15     protected $_title = 'Login Report';
16
17     /**
18      * @var string
19      */
20     protected $_subject = 'Login';
21
22     /**
23      * @var string
24      */
25     protected $_description = 'Historical report for login notifications.';
26
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     /**
35      * @var array
36      */
37     protected $_headers = array(
38         'Group',
39         'Division',
40         'Dealership',
41         'User Type',
42         'Name',
43         'Surname',
44         'Email Address',
45         'Mobile',
46         'Last Login Date',
47 //    'Number of Logins'
48         '30 Days',
49         '60 Days',
50         '120 Days',
51         '180 Days',
52
53     );
54
55     /**
56      * @var array
57      */
58     protected $_fields = array(
59         'groupName',
60         'divisionName',
61         'companyName',
62         'permissionName',
63         'firstName',
64         'familyName',
65         'email',
66         'mobile',
67         'lastLogin',
68         'numberOfLogins',
69         'days_60',
70         'days_120',
71         'days_180',
72     );
73
74     /**
75      * @var array
76      */
77     protected $_totals = array();
78
79     /**
80      * @var array
81      */
82     protected $_currencyFields = array();
83
84     /**
85      * Build the dataset.
86      */
87     public function build()
88     {
89         #-> Prepare parameters.
90         $join = array();
91         $leftJion = array();
92         $where = array();
93         $params = array();
94
95         ini_set('memory_limit', '512M');
96
97         $query = 'SELECT '
98             . 'company.name AS companyName, grp.name AS groupName, division.name AS divisionName, permission.name AS permissionName, '
99             . 'profile.firstName AS firstName, profile.familyName AS familyName, profile.email AS email,'
100             . 'profile.mobile AS mobile, '
101             . ' (CASE WHEN (SUM(CASE WHEN auth.created >= :dateFrom THEN 1 ELSE 0 END) > 0) THEN profile.lastLogin ELSE \'none\' END) AS lastLogin, '
102             . ' SUM(CASE WHEN auth.created >= :dateFrom THEN 1 ELSE 0 END) AS numberOfLogins, '
103             . ' SUM(CASE WHEN auth.created >= :dateFrom60 THEN 1 ELSE 0 END) AS days_60, '
104             . ' SUM(CASE WHEN auth.created >= :dateFrom120 THEN 1 ELSE 0 END) AS days_120, '
105             . ' SUM(CASE WHEN auth.created >= :dateFrom180 THEN 1 ELSE 0 END) AS days_180 '
106             . ' FROM \User\Entity\Profile profile '
107             . ' LEFT JOIN profile.logins auth '
108             . ' LEFT JOIN profile.company company '
109             . ' LEFT JOIN profile.permissions permission '
110             . ' LEFT JOIN company.group grp '
111             . ' LEFT JOIN company.groupDivision division '
112             . '[WHERE] '
113             . 'GROUP BY profile.id '
114             . 'ORDER BY numberOfLogins DESC';
115
116         $params['dateFrom'] = new \DateTime('-30 days');
117         $params['dateFrom60'] = new \DateTime('-60 days');
118         $params['dateFrom120'] = new \DateTime('-120 days');
119         $params['dateFrom180'] = new \DateTime('-180 days');
120         $this->_queries['Date Created'] = new \DateTime();
121         $this->_queries['Date Created'] = $this->_queries['Date Created']->format('Y-m-d');
122
123         if (isset($this->_input['group']) && !empty($this->_input['group']) && 'null' != $this->_input['group'])
124         {
125             $this->_queries['Group'] = $this->em
126                 ->find('\Company\Entity\Group', $this->_input['group'])
127                 ->name;
128             $where[] = 'grp.id = :group';
129             $params['group'] = $this->_input['group'];
130         }
131         if (isset($this->_input['groupDivision']) && !empty($this->_input['groupDivision']) && 'null' != $this->_input['groupDivision'])
132         {
133             $this->_queries['Division'] = $this->em
134                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
135                 ->name;
136             $where[] = 'division.id = :division';
137             $params['division'] = $this->_input['groupDivision'];
138         }
139         if (isset($this->_input['company']) && !empty($this->_input['company']) && 'null' != $this->_input['company'])
140         {
141             $this->_queries['Dealership'] = $this->em
142                 ->find('\Company\Entity\Company', $this->_input['company'])
143                 ->name;
144             $where[] = 'company.id = :company';
145             $params['company'] = $this->_input['company'];
146         }
147
148         #-> Finalize query.
149         $query = str_replace(array(
150             '[WHERE]'
151         ),
152             array(
153                 !empty($where) ? 'WHERE ' . implode(' AND ', $where) . ' ' : ' '
154             ), $query);
155
156         #-> Collect data.
157         $query = $this->em->createQuery($query);
158
159         error_log(var_export($query->getSQL(),true));
160         !empty($params) && $query->setParameters($params);
161         $results = $query->getResult();
162
163 //      \Utility\Debug::errorLog('$results',$results );
164
165         for($i = 0; $i <= count($results); $i ++)
166         {
167 //            \Utility\Debug::errorLog('$results[$i]',$results[$i] );
168
169             $days_30 = 0;
170             $days_60 = 0;
171             $days_120 = 0;
172             $days_180 = 0;
173
174             if(!empty($results[$i]))
175             {
176
177                 if('0' != $results[$i]['numberOfLogins'] && 0 != $results[$i]['numberOfLogins'] && '' != $results[$i]['numberOfLogins'])
178                 {
179                     $days_30 = $results[$i]['numberOfLogins'];
180                 }
181                 if('0' != $results[$i]['days_60'] && 0 != $results[$i]['days_60'] && '' != $results[$i]['days_60'])
182                 {
183                     $days_60 = $results[$i]['days_60'];
184                 }
185                 if('0' != $results[$i]['days_120'] && 0 != $results[$i]['days_120'] && '' != $results[$i]['days_120'])
186                 {
187                     $days_120 = $results[$i]['days_120'];
188                 }
189                 if('0' != $results[$i]['days_180'] && 0 != $results[$i]['days_180'] && '' != $results[$i]['days_180'])
190                 {
191                     $days_180 = $results[$i]['days_180'];
192                 }
193
194                 $days_60 = $days_60 - $days_30;
195                 $days_120 = $days_120 - $days_60 - $days_30;
196                 $days_180 = $days_180 - $days_120 - $days_60 - $days_30;
197
198
199                 $results[$i]['days_60'] = $days_60;
200                 $results[$i]['days_120'] = $days_120;
201                 $results[$i]['days_180'] = $days_180;
202             }
203
204         }
205
206
207         $this->_data = $results;
208
209     }
210
211 }