initial commit
[namibia] / module / Report / src / Report / Report / Valuation.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * Valuation Report.
8  * @author andre.fourie
9  */
10 class Valuation extends \Utility\Service\Report
11 {
12
13
14
15     /**
16      * @var string
17      */
18     protected $_title  = 'Valuation Report';
19     /**
20      * @var string
21      */
22     protected $_subject = 'Valuations';
23     /**
24      * @var string
25      */
26     protected $_description = 'Historical report for valuations.';
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         'Dealer',
38         'Customer Name',
39         'Customer Surname',
40         'Customer Phone Number',
41         'Customer Email Address',
42         'Registration Number',
43         'Make',
44         'Type',
45         'MM Code',
46         'Year',
47         'KM',
48         'Customer Requested Price',
49         'Trade Price',
50         'Recon costs',
51         'Price Offered to Sales',
52         '% of Trade',
53         'Date created',
54         'Creator',
55         'Sent to Sales',
56         'Sales Person',
57         'Manager',
58         'Comments',
59         'Status',
60         'Deal Not Done',
61         'Reason',
62         'Sent To Stock From'
63     );
64     /**
65      * @var array
66      */
67     protected $_fields = array(
68         'company_name',
69         'valuation_firstName',
70         'valuation_familyName',
71         'valuation_mobile',
72         'valuation_email',
73         'stock_registrationNumber',
74         'make_name',
75         'type_name',
76         'type_mmCode',
77         'vehicleYear_name',
78         'stock_km',
79         'valuation_requiredPrice',
80         'stock_tradePrice',
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         'valuation_requiredPrice',
100         'stock_tradePrice',
101         'stock_damageTotal',
102         'stock_highestOffer',
103         'valuation_amountOffered'
104     );
105     /**
106      * @var array
107      */
108     protected $_currencyFields = array(
109         'valuation_requiredPrice',
110         'stock_tradePrice',
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         ini_set('memory_limit','512M');
125         $authData = \Utility\Registry::getAuthData();
126         $selection = array(
127             'valuation', 'stock', 'createdBy', 'company', 'type', 'model', 'make',
128             'vehicleYear', 'salesProfile', 'managerProfile', 'companyGroup', 'companyDivision',
129             'sendToStockFrom', 'dealNotDoneSelection'
130         );
131         $join      = array();
132         $leftJion  = array();
133         $where     = array();
134         $params    = array();
135         $query = 'SELECT [SELECTION] '
136             . 'FROM \Valuation\Entity\Valuation valuation '
137             . 'JOIN valuation.stock stock '
138             . 'JOIN valuation.createdBy createdBy '
139             . 'JOIN stock.company company '
140             . 'JOIN stock.vehicleYear vehicleYear '
141             . 'JOIN stock.type type '
142             . 'JOIN type.model model '
143             . 'JOIN model.make make '
144             . 'LEFT JOIN valuation.salesProfile salesProfile '
145             . 'LEFT JOIN valuation.managerProfile managerProfile '
146             . 'LEFT JOIN company.group companyGroup '
147             . 'LEFT JOIN company.groupDivision companyDivision '
148             . 'LEFT JOIN valuation.sendToStockFrom sendToStockFrom '
149             . 'LEFT JOIN valuation.dealNotDoneSelection dealNotDoneSelection '
150             . '[WHERE] '
151             . 'ORDER BY valuation.created ASC';
152
153         #-> Construct details.
154         $authData = \Utility\Registry::getAuthData();
155         if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
156         {
157             $this->_queries['Date Range From'] = $this->_input['dateFrom'];
158             $where[] = 'valuation.created >= :dateFrom';
159             $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
160         }
161         if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
162         {
163             $this->_queries['Date Range Until'] = $this->_input['dateTo'];
164             $where[] = 'valuation.created <= :dateTo';
165             $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
166         }
167         #-> Construct details.
168         $authData = \Utility\Registry::getAuthData();
169         switch (\Utility\Registry::getUserType())
170         {
171             case 'B4C User':
172
173                 $groupFilter = \Utility\Registry::getSudo('Group', false);
174                 if ($groupFilter && 0 != $groupFilter)
175                 {
176                     $this->_queries['Group'] = \Utility\Registry::getSudo('GroupName', false);
177                     $join[]  = 'JOIN stock.company company';
178                     $where[] = 'IDENTITY(company.group) = :group';
179                     $params['group'] = $groupFilter;
180                 }
181                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
182                 if ($divisionFilter && 0 != $divisionFilter)
183                 {
184                     $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName', false);
185                     $join[]  = 'JOIN stock.company company';
186                     $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
187                     $params['groupDivision'] = $divisionFilter;
188                 }
189                 $companyFilter = \Utility\Registry::getSudo('Company', false);
190                 if ($companyFilter && 0 != $companyFilter)
191                 {
192                     $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false);
193                     $where[] = 'IDENTITY(stock.company) = :company';
194                     $params['company'] = $companyFilter;
195                 }
196                 break;
197             case 'Group User':
198                 $join[]  = 'JOIN stock.company company';
199                 $where[] = 'IDENTITY(company.group) = :group';
200                 $params['group'] = $authData['company']['group']['id'];
201                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
202                 if ($divisionFilter && 0 != $divisionFilter)
203                 {
204                     $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName', false);
205                     $join[]  = 'JOIN stock.company company';
206                     $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
207                     $params['groupDivision'] = $divisionFilter;
208                 }
209                 $companyFilter = \Utility\Registry::getSudo('Company', false);
210                 if ($companyFilter && 0 != $companyFilter)
211                 {
212                     $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false);
213                     $where[] = 'IDENTITY(stock.company) = :company';
214                     $params['company'] = $companyFilter;
215                 }
216                 break;
217             case 'Dealer Principle':
218                 $join[]  = 'JOIN stock.company company';
219                 $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
220                 $params['groupDivision'] = $authData['company']['groupDivision']['id'];
221                 $companyFilter = \Utility\Registry::getSudo('Company', false);
222                 if ($companyFilter && 0 != $companyFilter)
223                 {
224                     $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false);
225                     $where[] = 'IDENTITY(stock.company) = :company';
226                     $params['company'] = $companyFilter;
227                 }
228                 break;
229             default:
230                 $where[] = 'IDENTITY(stock.company) = :company';
231                 $params['company'] = $authData['company']['id'];
232                 break;
233         }
234
235         #-> Finalize query.
236         $query = str_replace(array(
237             '[SELECTION]', '[WHERE]'
238         ), array(
239             implode(', ', $selection) . ' ',
240             !empty($where)
241                 ? 'WHERE ' . implode(' AND ', $where) . ' '
242                 : ' '
243         ), $query);
244
245         #-> Collect data.
246         $query = $this->em->createQuery($query);
247         !empty($params)
248         && $query->setParameters($params);
249
250         $this->_data = $query->getScalarResult();
251         $this->em->clear();
252
253         foreach ($this->_data as $rowId => $rowData)
254         {
255             $this->_data[$rowId]['createdBy_fullName'] = $rowData['createdBy_firstName'] . ' ' . $rowData['createdBy_familyName'];
256             $this->_data[$rowId]['sales_person'] = isset($rowData['salesProfile_firstName'])
257             && !empty($rowData['salesProfile_firstName'])
258                 ? $rowData['salesProfile_firstName'] . ' ' . $rowData['salesProfile_familyName']
259                 : '';
260             $this->_data[$rowId]['manager'] = isset($rowData['managerProfile_firstName'])
261             && !empty($rowData['managerProfile_firstName'])
262                 ? $rowData['managerProfile_firstName'] . ' ' . $rowData['managerProfile_familyName']
263                 : '';
264             if ($this->_data[$rowId]['stock_tradePrice'] == 0)
265             {
266                 $this->_data[$rowId]['percentage_trade'] = '0 %';
267             }
268             else
269             {
270                 $this->_data[$rowId]['percentage_trade'] = round(($this->_data[$rowId]['valuation_amountOffered'] * 100) / $this->_data[$rowId]['stock_tradePrice']) . ' %' ;
271             }
272         }
273     }
274
275 }