text changes to registration mail content
[namibia] / module / Stock / src / Stock / Report / StockControl.php
1 <?php
2 namespace Stock\Report;
3
4
5
6 /**
7  * Stock Contyrol Report.
8  * @author andre.fourie
9  */
10 class StockControl extends \Utility\Service\Report
11 {
12
13
14
15         /**
16          * @var string
17          */
18         protected $_title  = 'Stock Control Report';
19         /**
20          * @var string
21          */
22         protected $_subject = 'Vehicles in stock.';
23         /**
24          * @var string
25          */
26         protected $_description = 'Historical report for vehicles in stock.';
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                         'Reference Number',
38                         'Stock Nuber',
39                         'Make',
40                         'Model',
41                         'Derivative',
42                         'Registration Number',
43                         'Status',
44                         'Sales Offer',
45                         'Price Guide Highest Offer',
46                         'Retail Amount Required',
47                         'Trade Price',
48                         'Retail Price',
49                         'Sales Person Name',
50                         'Sales Person Surname'
51                         );
52         /**
53          * @var array
54          */
55         protected $_fields = array(
56                         'stock_referenceNumber',
57                         'stock_stockNumber',
58                         'make_name',
59                         'model_name',
60                         'type_name',
61                         'stock_registrationNumber',
62                         'stock_jobState',
63                         'valuation_amountOffered',
64                         'stock_highestOffer',
65                         'stock_retailAmountRequired',
66                         'stock_tradePrice',
67                         'stock_retailPrice',
68             'stock_listPrice',
69                         'sales_firstName',
70                         'sales_familyName'
71                         );
72         /**
73          * @var array
74          */
75         protected $_totals = array(
76                         'stock_tradePrice',
77                         'stock_retailPrice',
78                         'stock_listPrice'
79                         );
80         /**
81          * @var array
82          */
83         protected $_currencyFields = array(
84                         'valuation_amountOffered',
85                         'stock_highestOffer',
86                         'stock_retailAmountRequired',
87                         'stock_tradePrice',
88                         'stock_retailPrice',
89             'stock_listPrice'
90                         );
91
92
93
94         /**
95          * Build the dataset.
96          */
97         public function build()
98         {
99                 #-> Prepare parameters.
100                 $selection = array(
101                                 'stock', 'type', 'model', 'make', 'valuation', 'sales'
102                 );
103                 $join      = array();
104                 $leftJion  = array();
105                 $where     = array();
106                 $params    = array();
107                 $query = 'SELECT [SELECTION] '
108                                 . 'FROM \Stock\Entity\Stock stock '
109                                 . ' JOIN stock.type type '
110                                 . ' JOIN type.model model '
111                                 . ' JOIN model.make make '
112                                 . ' [JOIN] '
113                                 . ' LEFT JOIN stock.valuation valuation '
114                                 . ' LEFT JOIN valuation.salesProfile sales '
115                                 . ' [LEFT-JOIN] '
116                                 . '[WHERE] '
117                                 . 'ORDER BY stock.created';
118
119                 #-> Construct details.
120                 $authData = \Utility\Registry::getAuthData();
121                 switch (\Utility\Registry::getUserType())
122                 {
123                         case 'B4C User':
124                                 $groupFilter = \Utility\Registry::getSudo('Group', false);
125                                 if ($groupFilter)
126                                 {
127                                         $this->_queries['Group'] = \Utility\Registry::getSudo('GroupName');
128                                         $join[]  = 'JOIN stock.company company';
129                                         $where[] = 'IDENTITY(company.group) = :group';
130                                         $params['group'] = $groupFilter;
131                                 }
132                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
133                                 if ($divisionFilter)
134                                 {
135                                         $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName');
136                                         $join[]  = 'JOIN stock.company company';
137                                         $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
138                                         $params['groupDivision'] = $divisionFilter;
139                                 }
140                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
141                                 if ($companyFilter)
142                                 {
143                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
144                                         $where[] = 'IDENTITY(stock.company) = :company';
145                                         $params['company'] = $companyFilter;
146                                 }
147                                 break;
148                         case 'Group User':
149                                 $join[]  = 'JOIN stock.company company';
150                                 $where[] = 'IDENTITY(company.group) = :group';
151                                 $params['group'] = $authData['company']['group']['id'];
152                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
153                                 if ($divisionFilter)
154                                 {
155                                         $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName');
156                                         $join[]  = 'JOIN stock.company company';
157                                         $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
158                                         $params['groupDivision'] = $divisionFilter;
159                                 }
160                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
161                                 if ($companyFilter)
162                                 {
163                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
164                                         $where[] = 'IDENTITY(stock.company) = :company';
165                                         $params['company'] = $companyFilter;
166                                 }
167                                 break;
168                         case 'Dealer Principle':
169                                 $join[]  = 'JOIN stock.company company';
170                                 $where[] = 'IDENTITY(company.groupDivision) = :groupDivision';
171                                 $params['groupDivision'] = $authData['company']['groupDivision']['id'];
172                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
173                                 if ($companyFilter)
174                                 {
175                                         $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName');
176                                         $where[] = 'IDENTITY(stock.company) = :company';
177                                         $params['company'] = $companyFilter;
178                                 }
179                                 break;
180                         default:
181                                 $where[] = 'IDENTITY(stock.company) = :company';
182                                 $params['company'] = $authData['company']['id'];
183                                 break;
184                 }
185                 if (isset($this->_input['jobState']))
186                 {
187                         $this->_queries['Status'] = $this->_input['jobState'];
188                         $where[] = 'stock.jobState >= :jobState';
189                         $params['jobState'] = $this->_input['jobState'];
190                 }
191                 if (isset($this->_input['dateFrom']))
192                 {
193                         $this->_queries['Date Range From'] = $this->_input['dateFrom'];
194                         $where[] = 'stock.created >= :dateFrom';
195                         $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
196                 }
197                 if (isset($this->_input['dateTo']))
198                 {
199                         $this->_queries['Date Range Until'] = $this->_input['dateTo'];
200                         $where[] = 'stock.created <= :dateTo';
201                         $params['dateTo'] = new \DateTime($this->_input['dateTo']);
202                 }
203
204                 #-> Finalize query.
205                 $query = str_replace(array(
206                                 '[SELECTION]', '[JOIN]', '[LEFT-JOIN]', '[WHERE]'
207                 ), array(
208                                 implode(', ', $selection) . ' ',
209                                 implode(' ', $join) . ' ',
210                                 implode(' ', $leftJion) . ' ',
211                                 !empty($where)
212                                         ? 'WHERE ' . implode(' AND ', $where) . ' '
213                                         : ' '
214                 ), $query);
215
216                 #-> Collect data.
217                 $query = $this->em->createQuery($query);
218                 !empty($params)
219                         && $query->setParameters($params);
220                 $this->_data = $query->getScalarResult();
221         }
222
223 }