initial commit
[namibia] / module / Valuation / src / Valuation / Report / Valuations.php
1 <?php
2 namespace Valuation\Report;
3
4
5
6 use Zend\Console\Console;
7 /**
8  * Stock Contyrol Report.
9  * @author andre.fourie
10  */
11 class Valuations extends \Utility\Service\Report
12 {
13
14
15
16         /**
17          * @var string
18          */
19         protected $_title  = 'Valuations Report';
20         /**
21          * @var string
22          */
23         protected $_subject = 'Valuations';
24         /**
25          * @var string
26          */
27         protected $_description = 'Historical report for valuations.';
28         /**
29          * @var array
30          */
31         protected $_notes = array(
32                         'Confidential information, generated using Bid 4 Cars, for more information visit bid4cars.co.za'
33                         );
34         /**
35          * @var array
36          */
37         protected $_headers = array(
38                         'Registration Number',
39                         'Make',
40                         'Model',
41                         'Derivative',
42                         'MM Code',
43                         'Year',
44                         'KM\'s',
45                         'Trade Price',
46                         'Retail Price',
47                         'Valuation Date',
48                         'Loaded on Price Guide',
49                         'Date First Offer Received',
50                         'Highest Offer Received',
51                         'Lowest Offer Received',
52                         'Total Offers Received',
53                         'Customer Name',
54                         'Customer Surname',
55                         'Valuator Name',
56                         'Valuator Surname',
57                         'Sales Person Name',
58                         'Sales Person Surname'
59                         );
60         /**
61          * @var array
62          */
63         protected $_fields = array(
64                         'stock_registrationNumber',
65                         'make_name',
66                         'model_name',
67                         'type_name',
68                         'type_mmCode',
69                         'year_name',
70                         'stock_km',
71                         'stock_tradePrice',
72                         'stock_retailPrice',
73             'stock_listPrice',
74                         'valuation_created',
75                         'priceGuide_created',
76                         'firstOfferDate',
77                         'highestOffer',
78                         'lowestOffer',
79                         'numOffers',
80                         'valuation_firstName',
81                         'valuation_familyName',
82                         'valuator_firstName',
83                         'valuator_familyName',
84                         'sales_firstName',
85                         'sales_familyName'
86                         );
87         /**
88          * @var array
89          */
90         protected $_totals = array(
91                         'stock_tradePrice',
92                         'stock_retailPrice',
93             'stock_listPrice',
94                         'highestOffer',
95                         'lowestOffer'
96                         );
97         /**
98          * @var array
99          */
100         protected $_currencyFields = array(
101                         'stock_tradePrice',
102                         'stock_retailPrice',
103             'stock_listPrice',
104                         'highestOffer',
105                         'lowestOffer'
106                         );
107
108
109
110         /**
111          * Build the dataset.
112          */
113         public function build()
114         {
115                 #-> Prepare parameters.
116                 // hier 
117                 $selection = array(
118                                 'stock', 'type', 'damageTotal', 'model', 'make', 'valuation', 'sales', 'valuator', 'priceGuide',
119                                 'MAX(offer.amount) AS highestOffer',
120                                 'MIN(offer.amount) AS lowestOffer',
121                                 'COUNT(offer.id) AS numOffers',
122                                 'MIN(offer.created) AS firstOfferDate'
123                 );
124                 $join      = array();
125                 $leftJion  = array();
126                 $where     = array();
127                 $params    = array();
128                 $query = 'SELECT [SELECTION] '
129                                 . 'FROM \Valuation\Entity\Valuation valuation '
130                                 . ' JOIN valuation.stock stock '
131                                 . ' JOIN stock.type type '
132                                 . ' JOIN stock.damageTotal damageTotal '
133                                 . ' JOIN type.model model '
134                                 . ' JOIN model.make make '
135                                 . ' [JOIN] '
136                                 . ' LEFT JOIN valuation.valuatedBy valuator '
137                                 . ' LEFT JOIN valuation.salesProfile sales '
138                                 . ' LEFT JOIN stock.priceGuide priceGuide '
139                                 . ' LEFT JOIN priceGuide.offers offer '
140                                 . ' [LEFT-JOIN] '
141                                 . '[WHERE] '
142                                 . 'GROUP BY valuation.id '
143                                 . 'ORDER BY valuation.created ASC';
144
145                 #-> Construct details.
146                 $authData = \Utility\Registry::getAuthData();
147                 switch (\Utility\Registry::getUserType())
148                 {
149                         case 'B4C User':
150                                 $groupFilter = \Utility\Registry::getSudo('Group', false);
151                                 if ($groupFilter)
152                                 {
153                                         $this->_queries['Group'] = \Utility\Registry::getSudo('GroupName');
154                                         $join[]  = 'JOIN stock.company company';
155                                         $where[] = 'IDENTITY(company.group) = :group';
156                                         $params['group'] = $groupFilter;
157                                 }
158                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
159                                 if ($divisionFilter)
160                                 {
161                                         $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName');
162                                         $join[]  = 'JOIN stock.company company';
163                                         $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
164                                         $params['groupDivision'] = $divisionFilter;
165                                 }
166                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
167                                 if ($companyFilter)
168                                 {
169                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
170                                         $where[] = 'IDENTITY(stock.company) = :company';
171                                         $params['company'] = $companyFilter;
172                                 }
173                                 break;
174                         case 'Group User':
175                                 $join[]  = 'JOIN stock.company company';
176                                 $where[] = 'IDENTITY(company.group) = :group';
177                                 $params['group'] = $authData['company']['group']['id'];
178                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
179                                 if ($divisionFilter)
180                                 {
181                                         $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName');
182                                         $join[]  = 'JOIN stock.company company';
183                                         $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
184                                         $params['groupDivision'] = $divisionFilter;
185                                 }
186                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
187                                 if ($companyFilter)
188                                 {
189                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
190                                         $where[] = 'IDENTITY(stock.company) = :company';
191                                         $params['company'] = $companyFilter;
192                                 }
193                                 break;
194                         case 'Dealer Principle':
195                                 $join[]  = 'JOIN stock.company company';
196                                 $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
197                                 $params['groupDivision'] = $authData['company']['groupDivision']['id'];
198                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
199                                 if ($companyFilter)
200                                 {
201                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
202                                         $where[] = 'IDENTITY(stock.company) = :company';
203                                         $params['company'] = $companyFilter;
204                                 }
205                                 break;
206                         default:
207                                 $where[] = 'IDENTITY(stock.company) = :company';
208                                 $params['company'] = $authData['company']['id'];
209                                 break;
210                 }
211                 if (isset($this->_input['jobState']))
212                 {
213                         $this->_queries['Status'] = $this->_input['jobState'];
214                         $where[] = 'valuation.jobState >= :jobState';
215                         $params['jobState'] = $this->_input['jobState'];
216                 }
217                 if (isset($this->_input['dateFrom']))
218                 {
219                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
220                         $where[] = 'valuation.created >= :dateFrom';
221                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
222                 }
223                 if (isset($this->_input['dateTo']))
224                 {
225                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
226                         $where[] = 'valuation.created <= :dateTo';
227                         $params['dateTo'] = new \DateTime($this->_input['dateTo']);
228                 }
229
230                 #-> Finalize query.
231                 $query = str_replace(array(
232                                 '[SELECTION]', '[JOIN]', '[LEFT-JOIN]', '[WHERE]'
233                 ), array(
234                                 implode(', ', $selection) . ' ',
235                                 implode(' ', $join) . ' ',
236                                 implode(' ', $leftJion) . ' ',
237                                 !empty($where)
238                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
239                                         : ' '
240                 ), $query);
241
242                 #-> Collect data.
243                 $query = $this->em->createQuery($query);
244                 !empty($params)
245                         && $query->setParameters($params);
246                 $this->_data = $query->getScalarResult();
247                 $data = $this->_data = $query->getScalarResult();
248                 //console.log($this->_data = $query->getScalarResult());
249         
250                 var_dump('test',$data);
251         }
252
253 }