text changes to registration mail content
[namibia] / module / Report / src / Report / Report / AdminValuation.php
1 <?php
2 namespace Report\Report;
3
4
5 /**
6  * Valuation Report.
7  * @author andre.fourie
8  */
9 class AdminValuation extends \Utility\Service\Report
10 {
11         /**
12          * @var string
13          */
14         protected $_title  = 'Valuation Report';
15         /**
16          * @var string
17          */
18         protected $_subject = 'Valuations';
19         /**
20          * @var string
21          */
22         protected $_description = 'Historical report for valuations.';
23         /**
24          * @var array
25          */
26         protected $_notes = array(
27                         'Confidential information, generated using Bid 4 Cars, for more information visit bid4cars.com.na'
28         );
29         /**
30          * @var array
31          */
32         protected $_headers = array(
33                         'Group',
34                         'Division',
35                         'Dealer',
36                         'Customer Name',
37                         'Customer Surname',
38                         'Customer Phone Number',
39                         'Registration Number',
40                         'Make',
41                         'Model',
42                         'Type',
43                         'MM Code',
44                         'Year',
45                         'KM',
46                         'Trade Price',
47                         'List Price',
48                         'Recon costs',
49                         'Price Offered to Sales',
50                         '% of Trade',
51                         'Date created',
52                         'Creator',
53                         'Sent to Sales',
54                         'Sales Person',
55                         'Manager',
56                         'Comments',
57             'Status',
58             'Deal Not Done',
59             'Reason',
60             'Sent To Stock From'
61                         );
62         /**
63          * @var array
64          */
65         protected $_fields = array(
66                         'companyGroup_name',
67                         'companyDivision_name',
68                         'company_name',
69                         'valuation_firstName',
70                         'valuation_familyName',
71                         'valuation_mobile',
72                         'stock_registrationNumber',
73                         'make_name',
74                         'model_name',
75                         'type_name',
76                         'type_mmCode',
77                         'vehicleYear_name',
78                         'stock_km',
79                         'stock_tradePrice',
80             'stock_listPrice',
81                         'stock_damageTotal',
82                         'valuation_amountOffered',
83                         'percentage_trade',
84                         'valuation_created',
85                         'createdBy_fullName',
86                         'valuation_sentToSales',
87                         'sales_person',
88                         'manager',
89                         'valuation_salesComments',
90             'valuation_jobState',
91             'dealNotDoneSelection_name',
92             'valuation_dealNotDoneReason',
93             'sendToStockFrom_name'
94                         );
95         /**
96          * @var array
97          */
98         protected $_totals = array(
99                         'stock_tradePrice',
100             'stock_listPrice',
101                         'stock_damageTotal',
102                         'stock_highestOffer',
103                         'valuation_amountOffered'
104                         );
105         /**
106          * @var array
107          */
108         protected $_currencyFields = array(
109                         'stock_tradePrice',
110             'stock_listPrice',
111                         'stock_damageTotal',
112                         'stock_highestOffer',
113                         'valuation_amountOffered'
114                         );
115
116
117
118         /**
119          * Build the dataset.
120          */
121         public function build()
122         {
123                 #-> Prepare parameters.
124                 $selection = array(
125                                 'valuation', 'stock', 'createdBy', 'company', 'type', 'model', 'make',
126                                 'vehicleYear', 'salesProfile', 'managerProfile', 'companyGroup', 'companyDivision',
127                 'sendToStockFrom', 'dealNotDoneSelection'
128                 );
129                 $where     = array();
130                 $params    = array();
131                 $query = 'SELECT [SELECTION] '
132                                 . 'FROM \Valuation\Entity\Valuation valuation '
133                                 . 'JOIN valuation.stock stock '
134                                 . 'JOIN valuation.createdBy createdBy '
135                                 . 'JOIN stock.company company '
136                                 . 'JOIN stock.vehicleYear vehicleYear '
137                                 . 'JOIN stock.type type '
138                                 . 'JOIN type.model model '
139                                 . 'JOIN model.make make '
140                                 . 'LEFT JOIN valuation.salesProfile salesProfile '
141                                 . 'LEFT JOIN valuation.managerProfile managerProfile '
142                                 . 'LEFT JOIN company.group companyGroup '
143                                 . 'LEFT JOIN company.groupDivision companyDivision '
144                 . 'LEFT JOIN valuation.sendToStockFrom sendToStockFrom '
145                 . 'LEFT JOIN valuation.dealNotDoneSelection dealNotDoneSelection '
146                                 . '[WHERE] '
147                                 . 'ORDER BY valuation.created ASC';
148
149                 #-> Construct details.
150                 if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
151                 {
152                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
153                         $where[] = 'valuation.created >= :dateFrom';
154                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
155                 }
156                 if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
157                 {
158                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
159                         $where[] = 'valuation.created <= :dateTo';
160                         $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
161                 }
162                 if (isset($this->_input['group'])
163                                 && !empty($this->_input['group'])
164                                 && 'null' != $this->_input['group'])
165                 {
166                         $this->_queries['Group'] = $this->em
167                                 ->find('\Company\Entity\Group', $this->_input['group'])
168                                 ->name;
169                         $where[] = 'companyGroup.id = :group';
170                         $params['group'] = $this->_input['group'];
171                 }
172                 if (isset($this->_input['groupDivision'])
173                                 && !empty($this->_input['groupDivision'])
174                                 && 'null' != $this->_input['groupDivision'])
175                 {
176                         $this->_queries['Division'] = $this->em
177                                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
178                                 ->name;
179                         $where[] = 'companyDivision.id = :division';
180                         $params['division'] = $this->_input['groupDivision'];
181                 }
182                 if (isset($this->_input['company'])
183                                 && !empty($this->_input['company'])
184                                 && 'null' != $this->_input['company'])
185                 {
186                         $this->_queries['Dealership'] = $this->em
187                                 ->find('\Company\Entity\Company', $this->_input['company'])
188                                 ->name;
189                         $where[] = 'company.id = :company';
190                         $params['company'] = $this->_input['company'];
191                 }
192
193                 #-> Finalize query.
194                 $query = str_replace(array(
195                                 '[SELECTION]', '[WHERE]'
196                         ), array(
197                                 implode(', ', $selection) . ' ',
198                                 !empty($where)
199                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
200                                         : ' '
201                         ), $query);
202
203                 #-> Collect data.
204                 $query = $this->em->createQuery($query);
205                 !empty($params)
206                         && $query->setParameters($params);
207
208                 $this->_data = $query->getScalarResult();
209                 $this->em->clear();
210
211                 foreach ($this->_data as $rowId => $rowData)
212                 {
213 //            \Utility\Debug::errorLog('$rowId', $rowId);
214 //            \Utility\Debug::errorLog('$rowData', $rowData);
215
216                         $this->_data[$rowId]['createdBy_fullName'] = $rowData['createdBy_firstName'] . ' ' . $rowData['createdBy_familyName'];
217                         $this->_data[$rowId]['sales_person'] = isset($rowData['salesProfile_firstName'])
218                                                                                                         && !empty($rowData['salesProfile_firstName'])
219                                 ? $rowData['salesProfile_firstName'] . ' ' . $rowData['salesProfile_familyName']
220                                 : '';
221                         $this->_data[$rowId]['manager'] = isset($rowData['managerProfile_firstName'])
222                                                                                                         && !empty($rowData['managerProfile_firstName'])
223                                 ? $rowData['managerProfile_firstName'] . ' ' . $rowData['managerProfile_familyName']
224                                 : '';
225                         if ($this->_data[$rowId]['stock_tradePrice'] == 0)
226                         {
227                                 $this->_data[$rowId]['percentage_trade'] = '0 %';
228                         }
229                         else
230                         {
231                                 $this->_data[$rowId]['percentage_trade'] = round(($this->_data[$rowId]['valuation_amountOffered'] * 100) / $this->_data[$rowId]['stock_tradePrice']) . ' %' ;
232                         }
233                 }
234         }
235
236 }