initial commit
[namibia] / module / Report / src / Report / Report / TuhpiLog.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * Tuhpi Log Report.
8  * @author andre.fourie
9  */
10 class TuhpiLog extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'HPI Log Report';
19         /**
20          * @var string
21          */
22         protected $_subject = 'HPI';
23         /**
24          * @var string
25          */
26         protected $_description = 'Historical report for HPI report requests.';
27         /**
28          * @var array
29          */
30         protected $_notes = array(
31                         'Confidential information, generated using Bid 4 Cars, for more information visit bid4cars.co.za'
32         );
33         /**
34          * @var array
35          */
36         protected $_headers = array(
37                         'Group',
38                         'Division',
39                         'Dealership',
40                         'User Name',
41                         'User Surname',
42                         'VIN No.',
43                         'Engine No.',
44                         'Registration No.',
45                         'Vehicle Make',
46                         'Model',
47                         'Type',
48                         'MM Code',
49                         'Date requested',
50                         'Transaction Number',
51                         'Status'
52         );
53         /**
54          * @var array
55          */
56         protected $_fields = array(
57                         'companyGroup_name',
58                         'companyDivision_name',
59                         'company_name',
60                         'profile_firstName',
61                         'profile_familyName',
62                         'stock_vinNumber',
63                         'stock_engineNumber',
64                         'stock_registrationNumber',
65                         'make_name',
66                         'model_name',
67                         'type_name',
68                         'type_mmCode',
69                         'log_created',
70                         'tuhpi_transactionNumber',
71                         'log_status'
72         );
73         /**
74          * @var array
75          */
76         protected $_totals = array();
77         /**
78          * @var array
79          */
80         protected $_currencyFields = array();
81
82
83
84         /**
85          * Build the dataset.
86          */
87         public function build()
88         {
89                 #-> Prepare parameters.
90                 $selection = array(
91                                 'log', 'tuhpi', 'stock', 'type', 'model', 'make',
92                                 'company', 'companyGroup', 'companyDivision', 'profile'
93                 );
94                 $join      = array();
95                 $leftJion  = array();
96                 $where     = array();
97                 $params    = array();
98                 $query = 'SELECT [SELECTION] '
99                                 . 'FROM \Stock\Entity\TuhpiLog log '
100                                 . ' JOIN log.stock stock '
101                                 . ' JOIN log.company company '
102                                 . ' JOIN log.createdBy profile '
103                                 . ' LEFT JOIN log.tuhpi tuhpi '
104                                 . ' LEFT JOIN stock.type type '
105                                 . ' LEFT JOIN type.model model '
106                                 . ' LEFT JOIN model.make make '
107                                 . ' LEFT JOIN company.group companyGroup '
108                                 . ' LEFT JOIN company.groupDivision companyDivision '
109                                 . '[WHERE] '
110                                 . 'ORDER BY log.created ASC';
111
112         $where[] = ' (tuhpi.hpi_type is null OR tuhpi.hpi_type = :hpiType) ';
113         $params['hpiType'] = 'HPI';
114
115                 #-> Construct details.
116                 $authData = \Utility\Registry::getAuthData();
117                 if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
118                 {
119                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
120                         $where[] = 'log.created >= :dateFrom';
121                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
122                 }
123                 if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
124                 {
125                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
126                         $where[] = 'log.created <= :dateTo';
127                         $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
128                 }
129                 if (isset($this->_input['group'])
130                                 && !empty($this->_input['group'])
131                                 && 'null' != $this->_input['group'])
132                 {
133                         $this->_queries['Group'] = $this->em
134                                 ->find('\Company\Entity\Group', $this->_input['group'])
135                                 ->name;
136                         $where[] = 'companyGroup.id = :group';
137                         $params['group'] = $this->_input['group'];
138                 }
139                 if (isset($this->_input['groupDivision'])
140                                 && !empty($this->_input['groupDivision'])
141                                 && 'null' != $this->_input['groupDivision'])
142                 {
143 //                      error_log($this->_input['groupDivision']);
144                         $this->_queries['Division'] = $this->em
145                                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
146                                 ->name;
147                         $where[] = 'companyDivision.id = :division';
148                         $params['division'] = $this->_input['groupDivision'];
149                 }
150                 if (isset($this->_input['company'])
151                                 && !empty($this->_input['company'])
152                                 && 'null' != $this->_input['company'])
153                 {
154                         $this->_queries['Dealership'] = $this->em
155                                 ->find('\Company\Entity\Company', $this->_input['company'])
156                                 ->name;
157                         $where[] = 'company.id = :company';
158                         $params['company'] = $this->_input['company'];
159                 }
160
161                 #-> Finalize query.
162                 $query = str_replace(array(
163                                 '[SELECTION]', '[WHERE]'
164                         ), array(
165                                 implode(', ', $selection) . ' ',
166                                 !empty($where)
167                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
168                                         : ' '
169                         ), $query);
170
171                 #-> Collect data.
172                 $query = $this->em->createQuery($query);
173                 !empty($params)
174                         && $query->setParameters($params);
175
176 //        \Utility\Debug::errorLog('$query->getSQL()', $query->getSQL());
177 //        \Utility\Debug::errorLog('$query->getParameters()', $query->getParameters());
178
179
180 //        \Utility\Debug::errorLog('$query->getScalarResult()', $query->getScalarResult());
181
182                 $this->_data = $query->getScalarResult();
183                 $this->em->clear();
184         }
185
186 }