latest update
[namibia] / module / Valuation / src / Valuation / Report / ValuationGrid.php
1 <?php
2 namespace Valuation\Report;
3
4
5
6 /**
7  * Stock Contyrol Report.
8  * @author andre.fourie
9  */
10 class ValuationGrid extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'Valuation Data Export';
19         /**
20          * @var string
21          */
22         protected $_subject = 'Valuations';
23         /**
24          * @var string
25          */
26         protected $_description = 'Export of valuations.';
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          * @var array
35          */
36         protected $_headers = array(
37                         'Dealer',
38                         'Group',
39                         'Division',
40                         'Reference Number',
41                         'Registration Number',
42                         'VIN Number',
43                         'Make',
44                         'Model',
45                         'Type',
46                         'MM Code',
47                         'Year',
48                         'Main Exterior Colour',
49                         'KM',
50                         'Trade Price',
51                         'Recon costs',
52                         'Price Offered to Sales',
53                         '% of Trade',
54                         'Date loaded on price guide',
55                         'Number of price guide offers',
56                         'Highest price guide offer',
57                         'Highest price Dealer Name(s)',
58                         'Status',
59                         'Sales Person',
60                         'Manager'
61                         );
62         /**
63          * @var array
64          */
65         protected $_fields = array(
66                         'company_name',
67                         'companyGroup_name',
68                         'groupDivision_name',
69                         'stock_referenceNumber',
70                         'stock_registrationNumber',
71                         'stock_vinNumber',
72                         'make_name',
73                         'model_name',
74                         'type_name',
75                         'type_mmCode',
76                         'vehicleYear_name',
77                         'exteriorColour_name',
78                         'stock_km',
79                         'stock_tradePrice',
80                         'stock_damageTotal',
81                         'valuation_amountOffered',
82                         'valuation_updated',
83                         'stock_loadedOnPriceGuide',
84                         'stock_numberOfOffers',
85                         'stock_highestOffer',
86                         'higestoffernames',
87                         'valuation_jobState',
88                         'salesProfile_fullName',
89                         'managerProfile_fullName'
90                         );
91         /**
92          * @var array
93          */
94         protected $_totals = array(
95                         'stock_tradePrice',
96             'stock_listPrice',
97                         'stock_damageTotal',
98                         'stock_retailPrice',
99
100                         'stock_highestOffer'
101                         );
102         /**
103          * @var array
104          */
105         protected $_currencyFields = array(
106                         'stock_tradePrice',
107             'stock_listPrice',
108                         'stock_damageTotal',
109                         'stock_retailPrice',
110                         'stock_highestOffer',
111                         'valuation_amountOffered'
112                         );
113
114
115
116         /**
117          * Build the dataset.
118          */
119         public function build()
120         {
121                 #-> Data passed by DataBin Grid functionality.
122                 $this->em->clear();
123                 ini_set('memory_limit','512M');
124                 foreach ($this->_data as $rowId => $rowData)
125                 {
126                         $this->_data[$rowId]['salesProfile_fullName'] = isset($rowData['salesProfile_firstName'])
127                                                                                                         && !empty($rowData['salesProfile_firstName'])
128                                 ? $rowData['salesProfile_firstName'] . ' ' . $rowData['salesProfile_familyName']
129                                 : '';
130                         $this->_data[$rowId]['managerProfile_fullName'] = isset($rowData['managerProfile_firstName'])
131                                                                                                         && !empty($rowData['managerProfile_firstName'])
132                                 ? $rowData['managerProfile_firstName'] . ' ' . $rowData['managerProfile_familyName']
133                                 : '';
134                         if (is_null($rowData['priceGuide_id']))
135                         {
136                                 $this->_data[$rowId]['higestoffernames'] = '';
137                                 continue;
138                         }
139                         $query = 'SELECT priceGuideOffer, company '
140                                         . 'FROM \PriceGuide\Entity\Offer priceGuideOffer '
141                                         . 'JOIN priceGuideOffer.company company '
142                                         . 'WHERE IDENTITY(priceGuideOffer.priceGuideStock) = :priceGuideId'
143                                         . ' AND priceGuideOffer.amount = :highestOffer';
144                         $query = $this->em->createQuery($query);
145                         $query->setParameter('priceGuideId', $rowData['priceGuide_id']);
146                         $query->setParameter('highestOffer', $rowData['stock_highestOffer']);
147                         $resultx = $query->getArrayResult();
148                         $this->em->clear();
149                         if (empty($resultx))
150                         {
151                                 $this->_data[$rowId]['higestoffernames'] = '';
152                                 continue;
153                         }
154                         $dealers = array();
155                         foreach($resultx as $itemx)
156                         {
157                                 $dealers[] = $itemx['company']['name'];
158                         }
159                         $this->_data[$rowId]['higestoffernames'] = implode(', ', $dealers);
160
161                 }
162
163                 foreach ($this->_data as $rowId => $rowData)
164                 {
165                         if ($this->_data[$rowId]['stock_tradePrice'] == 0)
166                         {
167                                 $this->_data[$rowId]['valuation_updated'] = '0 %';
168                         }
169                         else
170                         {
171                                 $this->_data[$rowId]['valuation_updated'] = round(($this->_data[$rowId]['valuation_amountOffered'] * 100) / $this->_data[$rowId]['stock_tradePrice']) . ' %' ;
172                         }
173                 }
174
175         }
176
177 }