initial commit
[namibia] / module / Report / src / Report / Report / StockAuction.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * Stock Control Report.
8  * @author andre.fourie
9  */
10 class StockAuction extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'Stock Auction Report';
19         /**
20          * @var string
21          */
22         protected $_subject = 'Stock Auction';
23         /**
24          * @var string
25          */
26         protected $_description = 'Historical report for vehicles sold on auction.';
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                         'Auction ID',
38                         'Buyer Dealership',
39                         'Vehicle Make',
40                         'Type',
41                         'Year',
42                         'Stock No.',
43                         'Registration No.',
44             'Kilometres',
45                         'Date Loaded',
46                         'Date Sold',
47                         'Trade Price',
48                         'Reserve Price',
49                         'Total Recon',
50                         'Highest Bid'
51         );
52         /**
53          * @var array
54          */
55         protected $_fields = array(
56                         'auction_id',
57                         'buyerCompany_name',
58                         'make_name',
59                         'type_name',
60                         'year_name',
61                         'stock_stockNumber',
62                         'stock_registrationNumber',
63             'stock_km',
64                         'auction_created',
65                         'auction_endDate',
66                         'stock_tradePrice',
67                         'auction_reservePrice',
68             'stock_damageTotal',
69                         'auction_currentBidPrice',
70         );
71         /**
72          * @var array
73          */
74         protected $_totals = array(
75                         'auction_currentBidPrice',
76                         'stock_tradePrice',
77                         'auction_reservePrice',
78             'stock_damageTotal',
79         );
80         /**
81          * @var array
82          */
83         protected $_currencyFields = array(
84                         'auction_currentBidPrice',
85                         'stock_tradePrice',
86                         'auction_reservePrice',
87                         'stock_damageTotal'
88         );
89
90
91
92         /**
93          * Build the dataset.
94          */
95         public function build()
96         {
97     ini_set('memory_limit','1024M');
98                 #-> Prepare parameters.
99                 $selection = array(
100                                 'auction', 'stock', 'type', 'year', 'make',
101                                 'buyerCompany', 'buyerGroup', 'buyerDivision'
102                 );
103                 $join      = array();
104                 $leftJion  = array();
105                 $where     = array();
106                 $params    = array();
107                 $query = 'SELECT [SELECTION] '
108                                 . 'FROM \Auction\Entity\Auction auction '
109                                 . ' JOIN auction.stock stock '
110                                 . ' JOIN stock.company sellerCompany '
111                                 . ' JOIN auction.bids bid '
112                                 . ' LEFT JOIN stock.type type '
113                                 . ' LEFT JOIN stock.vehicleYear year '
114                                 . ' LEFT JOIN type.model model '
115                                 . ' LEFT JOIN model.make make '
116                                 . ' LEFT JOIN auction.createdBy sellerProfile '
117                                 . ' LEFT JOIN auction.currentBid currentBid '
118                                 . ' LEFT JOIN currentBid.profile buyerProfile '
119                                 . ' LEFT JOIN currentBid.company buyerCompany '
120                                 . ' LEFT JOIN sellerCompany.group sellerGroup '
121                                 . ' LEFT JOIN sellerCompany.groupDivision sellerDivision '
122                                 . ' LEFT JOIN buyerProfile.group buyerGroup '
123                                 . ' LEFT JOIN buyerProfile.groupDivision buyerDivision '
124                                 . '[WHERE] '
125                                 . 'GROUP BY auction.id '
126                                 . 'ORDER BY auction.created ASC';
127                 $where[] = 'auction.jobState = \'Sold\'';
128
129                 #-> Construct details.
130                 $authData = \Utility\Registry::getAuthData();
131                 if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
132                 {
133                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
134                         $where[] = 'auction.endDate >= :dateFrom';
135                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
136                 }
137                 if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
138                 {
139                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
140                         $where[] = 'auction.endDate <= :dateTo';
141                         $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
142                 }
143                 if (isset($this->_input['group'])
144                                 && !empty($this->_input['group'])
145                                 && 'null' != $this->_input['group'])
146                 {
147                         $this->_queries['Group'] = $this->em
148                                 ->find('\Company\Entity\Group', $this->_input['group'])
149                                 ->name;
150                         $where[] = 'sellerGroup.id = :group';
151                         $params['group'] = $this->_input['group'];
152                 }
153                 if (isset($this->_input['groupDivision'])
154                                 && !empty($this->_input['groupDivision'])
155                                 && 'null' != $this->_input['groupDivision'])
156                 {
157                         error_log($this->_input['groupDivision']);
158                         $this->_queries['Division'] = $this->em
159                                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
160                                 ->name;
161                         $where[] = 'sellerDivision.id = :division';
162                         $params['division'] = $this->_input['groupDivision'];
163                 }
164                 if (isset($this->_input['company'])
165                                 && !empty($this->_input['company'])
166                                 && 'null' != $this->_input['company'])
167                 {
168                         $this->_queries['Dealership'] = $this->em
169                                 ->find('\Company\Entity\Company', $this->_input['company'])
170                                 ->name;
171                         $where[] = 'sellerCompany.id = :company';
172                         $params['company'] = $this->_input['company'];
173                 }
174
175                 #-> Finalize query.
176                 $query = str_replace(array(
177                                 '[SELECTION]', '[WHERE]'
178                         ), array(
179                                 implode(', ', $selection) . ' ',
180                                 !empty($where)
181                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
182                                         : ' '
183                         ), $query);
184
185                 #-> Collect data.
186                 $query = $this->em->createQuery($query);
187                 !empty($params)
188                         && $query->setParameters($params);
189
190                 $this->_data = $query->getScalarResult();
191
192                 $this->em->clear();
193 //              foreach ($this->_data as $id => $row)
194 //              {
195 //                      $this->_data[$id]['bidPerc'] = 0 < $row['stock_tradePrice']
196 //                              ? round((100 / $row['stock_tradePrice']) * $row['auction_currentBidPrice'], 1) . ' %'
197 //                              : '0.0 %';
198 //              }
199         }
200
201 }