initial commit
[namibia] / module / PriceGuide / src / PriceGuide / DataBin / PriceGuide.php
1 <?php
2 namespace PriceGuide\DataBin;
3
4
5
6 /**
7  * Manage PriceGuide data.
8  * @author andre.fourie
9  */
10 class PriceGuide extends \Workspace\Service\DataBin
11 {
12
13         /**
14          * @var array
15          */
16         protected $meta = array(
17             'Base'        => 'PriceGuide',
18             'DatasetName' => 'valuations',
19             'Entity'      => '\PriceGuide\Entity\PriceGuide',
20             'References'  => array(
21                 'valuation'      => '\Valuation\Entity\Valuation',
22                 'stock'          => '\Stock\Entity\Stock',
23                 'createdBy'      => '\User\Entity\Profile',
24                 'company'        => '\Company\Entity\Company'
25             ),
26             'Dependants'  => array()
27         );
28         /**
29          * @var array
30         */
31         protected $metaGrid = array(
32             'Type'          => 'Grid',
33                 'Export'        => true,
34                 'Builder'       => '\PriceGuide\Report\PriceGuideGrid',
35                 'Writer'        => '\Utility\Export\ExcelReport',
36                 'Output'        => 'Download',
37             'Contract'      => 'Recurring',
38                 'Options'       => array(
39                                 'Boolean' => array(
40                                                 'ExportToExcel' => false
41                                 )
42                 ),
43             'RequiredInput' => array(),
44             'OptionalInput' => array(
45                 'Grid'  => array(
46                     'NumberOfRecords' => 'Integer',
47                     'Page'            => 'Integer',
48                     'Filter'          => 'Array',
49                     'OrderBy'         => 'Array'
50                 )
51             ),
52             'Base'            => 'priceGuide',
53             'NumberOfRecords' => 20,
54         'Query'           => 'SELECT [SELECTION]
55                                               FROM \PriceGuide\Entity\PriceGuide priceGuide
56                                               JOIN priceGuide.members members
57                                               JOIN members.member mmbr
58                                               JOIN mmbr.company memberCompany
59                                                           JOIN priceGuide.company company
60                                                           JOIN company.region region
61                                                           JOIN priceGuide.stock stock
62                                                           JOIN stock.vehicleYear vehicleYear
63                                                           JOIN stock.type type
64                                                           JOIN type.model model
65                                                           JOIN model.make make
66                                                           LEFT JOIN company.group companyGroup
67                                                           LEFT JOIN company.groupDivision groupDivision
68                                                           LEFT JOIN stock.exteriorColour exteriorColour
69                                                           LEFT JOIN priceGuide.offers offer WITH IDENTITY(offer.company) = [company]
70                                                           LEFT JOIN offer.company offercompany
71                                                   LEFT JOIN stock.valuation valuation
72                                               [WHERE] [ORDER]',
73             'Selection'       => 'priceGuide, mmbr, company, companyGroup, groupDivision, region, stock, vehicleYear, make, model, type, exteriorColour, offer, offercompany, valuation',
74             'Filter'          => array(
75                                                                 'mmbr.status' => 'Active',
76                                                                 'statusFilter' => array(
77                                                                         'priceGuide.jobState' => 'IN Open4Offers,Updateable Offers',
78                                                                         'offer.id' => '!NULL'
79                                                                 ),
80                                                 'priceGuide.archived' => 0
81                                              ),
82             'OrderBy'         => array('priceGuide.created' => 'DESC'),
83             'Fields'          => array(
84                                                 'id',
85                                                 'created' => 'DateTime',
86                                                 'updated' => 'DateTime',
87                                                 'jobState',
88                                                 'offers' => array(array(
89                                                         'id',
90                                                         'amount',
91                                                         'status',
92                                                         'created' => 'DateTime',
93                                                         'company' => array(
94                                                                         'id',
95                                                                         'name'
96                                                         )
97                                                 )),
98                                                 'company' => array(
99                                                     'name',
100                                                     'region' => array(
101                                                             'name'
102                                                     )
103                                                 ),
104                                                 'stock' => array(
105                                                     'created' => 'DateTime',
106                                                     'type' => array(
107                                                         'name',
108                                                         'model' => array(
109                                                             'name',
110                                                             'make' => array(
111                                                                   'name'
112                                                             )
113                                                         ),
114                                                     ),
115                                                     'vehicleYear' => array(
116                                                         'name'
117                                                     ),
118                                                     'exteriorColour' => array(
119                                                         'name'
120                                                     ),
121                                                     'km',
122                                                     'tradePrice',
123                                                     'retailPrice',
124                                     'listPrice',
125                                                     'numberOfOffers',
126                                                     'highestOffer',
127                                                     'valuation' => array(
128                                                         'requiredPrice'
129                                                     )
130                                                 )
131                                             )
132         );
133         /**
134          * @var array
135          */
136         protected $metaView = array(
137             'Type'        => 'Update',
138             'Contract'    => 'Recurring',
139             'Expand'      => array(
140                                         'company',
141                                         'contact',
142                                                 'group',
143                                                 'groupDivision',
144                                                 'stock',
145                                                 'damages',
146                                                 'accessories',
147                                                 'category',
148                                                 'make',
149                                                 'model',
150                                                 'type',
151                                                 'mainImage',
152                                                 'frontImage',
153                                                 'rightImage',
154                                                 'leftImage',
155                                                 'backImage',
156                                                 'interiorImage',
157                                                 'engineImage',
158                                                 'natisImage',
159                                         'contact',
160                                         'fuelType',
161                                         'transmissionType',
162                                         'condition',
163                                         'fullServiceHistory',
164                                         'exteriorColour',
165                                         'interiorColour',
166                                         'upholstery',
167                                         'papers',
168                                         'natis',
169                                         'spareKeys',
170                                         'vehicleYear',
171                                         'valuation_'
172             ),
173             'RequiredInput' => array(),
174             'OptionalInput' => array()
175         );
176         /**
177          * @var array
178         */
179         protected $metaCreate = array(
180             'Type'          => 'Create',
181             'Contract'      => 'Recurring',
182             'ExecuteAfter' => array(
183                 'sendToMembers'
184             ),
185             'RequiredInput' => array(
186                 'PriceGuide' => array(
187                     'company' => 'Id',
188                     'stock'   => 'Id'
189                 )
190             ),
191             'OptionalInput' => array()
192         );
193         /**
194          * @var array
195         */
196         protected $metaUpdate = array(
197             'Type'          => 'Update',
198             'Contract'      => 'UseOnce',
199             'RequiredInput' => array(
200                 'PriceGuide' => array(
201                     'company' => 'Id',
202                     'stock'   => 'Id'
203                 )
204             ),
205             'OptionalInput' => array()
206         );
207         /**
208          * @var array
209         */
210         protected $metaSendToSales = array(
211                         'Type'          => 'Update',
212                         'Contract'      => 'UseOnce',
213                         'ConditionalContract' => array(
214                                         'noSendToSalesWithoutValuation'
215                         ),
216                         'ExecuteAfter' => array(
217                                         'setPreviousStateToComplete'
218                         ),
219                         'RelatedEntityFromInput' => array(
220                                         'Valuation' => array(
221                                                         'Workflow'  => '\Valuation\Workflow',
222                                                         'Service'   => '\Valuation\Service\Valuation',
223                                                         'Field'     => 'valuation'
224                                         )
225                         ),
226                         'RequiredInput' => array(
227                                         'Valuation' => array(
228                                                         'amountOffered' => 'Decimal',
229                                                         'salesProfile'  => 'Id'
230                                         )
231                         ),
232                         'OptionalInput' => array(
233                                         'Valuation' => array(
234                                                         'salesComments'   => 'String500'
235                                         )
236                         )
237         );
238
239
240
241         /**
242          * Add some dynamic filtering to our grids.
243          */
244         public function __construct()
245         {
246                 $authData = \Utility\Registry::getAuthData();
247                 switch (\Utility\Registry::getUserType())
248                 {
249                         case 'B4C User':
250                                 $groupFilter = \Utility\Registry::getSudo('Group', false);
251                                 $groupFilter
252                                         && $this->metaGrid['Filter']['IDENTITY(memberCompany.group)'] = $groupFilter;
253                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
254                                 $divisionFilter
255                                         && $this->metaGrid['Filter']['IDENTITY(memberCompany.groupDivision)'] = $divisionFilter;
256                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
257                                 if ($groupFilter && $divisionFilter && $companyFilter)
258                                 {
259                                         $this->metaGrid['Filter']['memberCompany.id'] = $companyFilter;
260                                         $this->metaGrid['Query'] = str_replace(
261                                                 '[company]', $companyFilter, $this->metaGrid['Query']
262                                                 );
263                                 }
264                                 else
265                                 {
266                                         $this->metaGrid['Query'] = str_replace(
267                                                 '[company]', '0', $this->metaGrid['Query']
268                                                 );
269                                 }
270                                 break;
271                         case 'Group User':
272                                 $this->metaGrid['Filter']['IDENTITY(memberCompany.group)'] = $authData['company']['group']['id'];
273                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
274                                 $divisionFilter
275                                         && $this->metaGrid['Filter']['IDENTITY(memberCompany.groupDivision)'] = $divisionFilter;
276                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
277                                 if ($divisionFilter && $companyFilter)
278                                 {
279                                         $this->metaGrid['Filter']['memberCompany.id'] = $companyFilter;
280                                         $this->metaGrid['Query'] = str_replace(
281                                                 '[company]', $companyFilter, $this->metaGrid['Query']
282                                                 );
283                                 }
284                                 else
285                                 {
286                                         $this->metaGrid['Query'] = str_replace(
287                                                 '[company]', '0', $this->metaGrid['Query']
288                                                 );
289                                 }
290                                 break;
291                         case 'Dealer Principle':
292                                 $this->metaGrid['Filter']['IDENTITY(memberCompany.groupDivision)'] = $authData['company']['groupDivision']['id'];
293                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
294                                 if ($companyFilter)
295                                 {
296                                         $this->metaGrid['Filter']['memberCompany.id'] = $companyFilter;
297                                         $this->metaGrid['Query'] = str_replace(
298                                                 '[company]', $companyFilter, $this->metaGrid['Query']
299                                                 );
300                                 }
301                                 else
302                                 {
303                                         $this->metaGrid['Query'] = str_replace(
304                                                 '[company]', '0', $this->metaGrid['Query']
305                                                 );
306                                 }
307                                 break;
308                         default:
309                                 $this->metaGrid['Filter']['memberCompany.id'] = $authData['company']['id'];
310                                 $this->metaGrid['Query'] = str_replace(
311                                                 '[company]', $authData['company']['id'], $this->metaGrid['Query']
312                                                 );
313                                 break;
314                         }
315         }
316
317
318 }