latest update
[namibia] / module / Report / src / Report / Report / AuctionAdherence.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * Stock Control Report.
8  * @author andre.fourie
9  */
10 class AuctionAdherence extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'AuctionAdherence Report';
19         /**
20          * @var string
21          */
22         protected $_subject = 'AuctionAdherence';
23         /**
24          * @var string
25          */
26         protected $_description = 'Details of vehicles found on Bid4Cars auction that was sold/invoiced through automate.';
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     'Group',
38     'Division',
39     'Trade Centre',
40     'Dealership',
41     'Region',
42     'Stock No',
43     'Make',
44     'VIN Number',
45     'Registration',
46     'Engine Number',
47     'Total Cost (incl)',
48     'Invoiced (incl)',
49     'SPL True',
50     'Invoiced To',
51     'Sold To',
52     'Date Loaded',
53     'Date Sold',
54     'Initial Reserve',
55     'VIN Matched',
56     'Reg Matched',
57     'Loaded',
58     'Sold',
59     'Variance against Potential'
60   );
61
62   /**
63    * @var array
64    */
65   protected $_fields = array(
66     'group_name',
67     'division_name',
68     'trade_centre_name',
69     'company_name',
70     'region_name',
71     'stock_stockNumber',
72     'make_name',
73     'vin_number',
74     'reg_number',
75     'eng_number',
76     'total_cost',
77     'sales_total',
78     'spl',
79     'sold_to',
80     'sold_to_name',
81     'date_loaded',
82     'auction_endDate',
83     'auction_initial_reservePrice',
84     'vin_match',
85     'reg_match',
86     'auction_reservePrice',
87     'auction_sold_price',
88     'potential'
89   );
90
91   /**
92    * @var array
93    */
94   protected $_totals = array(
95     'total_cost',
96     'sales_total',
97     'auction_reservePrice',
98     'auction_sold_price',
99     'potential'
100   );
101
102   /**
103    * @var array
104    */
105   protected $_currencyFields = array(
106     'auction_reservePrice',
107     'auction_currentBidPrice',
108     'auction_bidIncrement'
109   );
110
111   /**
112    * Build the dataset.
113    */
114   public function build()
115   {
116     #-> Prepare parameters.
117     #-> Collect loaded and sold totals.
118     $selection = array(
119       'grp.name AS group_name',
120       'division.name AS division_name',
121       'company.name AS company_name',
122       'trade_center.name AS trade_centre_name',
123       'region.name as region_name',
124       'stock.stockNumber AS stock_stockNumber',
125       'make.name AS make_name',
126       'stock.vinNumber as vin_number',
127       'stock.registrationNumber as reg_number',
128       'stock.engineNumber as eng_number',
129       'ts.totalCost as total_cost',
130       'ts.salesTotal as sales_total',
131       '(ts.salesTotal - ts.totalCost) as spl',
132       'ts.soldTo as sold_to',
133       'sold_to_company.name as sold_to_name',
134       'auction.startDate as date_loaded',
135       '(CASE WHEN auction.jobState = :auctionState THEN auction.endDate THEN :notApplicable END) as auction_endDate',
136       'auction.reservePrice AS auction_initial_reservePrice',
137       'ts.vinMatch as vin_match',
138       'ts.regMatch as reg_match',
139       'auction.reservePrice AS auction_reservePrice',
140       '(CASE WHEN auction.jobState = :auctionState THEN auction.currentBidPrice THEN :notApplicable END) as auction_sold_price',
141       '(CASE WHEN auction.jobState = :auctionState THEN auction.currentBidPrice - ts.salesTotal THEN :notApplicable END) as potential',
142       'stock.referenceNumber as reference',
143     );
144     $join = array();
145     $leftJion = array();
146     $where = array();
147     $params = array();
148     $query = 'SELECT [SELECTION] '
149       . 'FROM \Adherence\Entity\TradeSales ts '
150       . 'LEFT JOIN ts.stock stock '
151       . 'LEFT JOIN stock.auction auction '
152       . 'LEFT JOIN auction.soldToCompany sold_to_company '
153       . 'LEFT JOIN stock.company company '
154       . 'LEFT JOIN company.region region '
155       . 'LEFT JOIN company.tradeCenter trade_center '
156       . 'LEFT JOIN stock.type type '
157       . 'LEFT JOIN type.model model '
158       . 'LEFT JOIN model.make make '
159       . 'LEFT JOIN company.group grp '
160       . 'LEFT JOIN company.groupDivision division '
161       . '[WHERE] '
162       . 'ORDER BY ts.saleDate ASC';
163
164     $params['auctionState'] = 'Sold';
165     $params['notApplicable'] = 'N/A';
166
167     $where[] = '(auction.jobState != :status OR auction.jobState IS NULL)';
168     $params['status'] = 'Undone';
169
170     $where[] = '(auction.jobState != :status2 OR auction.jobState IS NULL)';
171     $params['status2'] = 'Relist';
172
173     if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
174     {
175       $this->_queries['Date Range From'] = $this->_input['dateFrom'];
176       $where[] = 'ts.saleDate >= :dateFrom';
177       $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
178     }
179     if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
180     {
181       $this->_queries['Date Range Until'] = $this->_input['dateTo'];
182       $where[] = 'ts.saleDate <= :dateTo';
183       $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
184     }
185     if (isset($this->_input['group']) && !empty($this->_input['group']) && 'null' != $this->_input['group'])
186     {
187       $this->_queries['Group'] = $this->em
188           ->find('\Company\Entity\Group', $this->_input['group'])
189         ->name;
190       $where[] = 'grp.id = :group';
191       $params['group'] = $this->_input['group'];
192     }
193     if (isset($this->_input['groupDivision']) && !empty($this->_input['groupDivision']) && 'null' != $this->_input['groupDivision'])
194     {
195       error_log($this->_input['groupDivision']);
196       $this->_queries['Division'] = $this->em
197           ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
198         ->name;
199       $where[] = 'division.id = :division';
200       $params['division'] = $this->_input['groupDivision'];
201     }
202     if (isset($this->_input['company']) && !empty($this->_input['company']) && 'null' != $this->_input['company'])
203     {
204       error_log($this->_input['company']);
205       $this->_queries['Dealership'] = $this->em
206           ->find('\Company\Entity\Company', $this->_input['company'])
207         ->name;
208       $where[] = 'company.id = :company';
209       $params['company'] = $this->_input['company'];
210     }
211
212     #-> Finalize query.
213     $query = str_replace(array(
214       '[SELECTION]', '[WHERE]'
215       ), array(
216       implode(', ', $selection) . ' ',
217       !empty($where) ? 'WHERE ' . implode(' AND ', $where) . ' ' : ' '
218       ), $query);
219     
220     #-> Collect data.
221     $query = $this->em->createQuery($query);
222     !empty($params) && $query->setParameters($params);
223     error_log($query->getSQL());
224     $this->_data = $query->getScalarResult();
225     $this->em->clear();
226     foreach ($this->_data as $id => $row)
227     {
228       if ($row['reference'])
229       {
230         $query = 'SELECT auction.reservePrice '
231           . 'FROM \Auction\Entity\Auction auction '
232           . 'JOIN auction.stock stock '
233           . 'WHERE stock.referenceNumber = :reference '
234           . 'ORDER BY auction.endDate ASC';
235
236         $params = array();
237         $params['reference'] = $row['reference'];
238
239         $query = $this->em->createQuery($query);
240         $query->setParameters($params);
241         $temp = $query->getScalarResult();
242
243         $this->_data[$id]['auction_initial_reservePrice'] = $temp[0]['reservePrice'];
244       }
245     }
246   }
247
248 }