initial commit
[namibia] / module / Stock / src / Stock / DataBin / Stock.php
1 <?php
2 namespace Stock\DataBin;
3
4
5 /**
6  * Manage Stock data.
7  * @author andre.fourie
8  */
9 class Stock extends \Workspace\Service\DataBin
10 {
11
12         /**
13          * @var array
14          */
15         protected $meta = array(
16                 'Base'        => 'Stock',
17                 'DatasetName' => 'stock',
18                 'Entity'      => '\Stock\Entity\Stock',
19                 'References'  => array(
20                         'region'             => '\Location\Entity\Region',
21                         'type'               => '\Stock\Entity\Type',
22                         'fuelType'           => '\Stock\Entity\FuelType',
23                         'transmissionType'   => '\Stock\Entity\TransmissionType',
24                         'exteriorColour'     => '\Stock\Entity\ExteriorColour',
25                         'interiorColour'     => '\Stock\Entity\InteriorColour',
26                         'condition'          => '\Stock\Entity\Condition',
27                         'vehicleYear'        => '\Stock\Entity\Year',
28                         'upholstery'         => '\Stock\Entity\Upholstery',
29                         'papers'             => '\Stock\Entity\Paper',
30                         'natis'              => '\Stock\Entity\Natis',
31                         'fullServiceHistory' => '\Stock\Entity\FullServiceHistory',
32                         'mainImage'          => '\Utility\Entity\Image',
33                         'frontImage'         => '\Utility\Entity\Image',
34                         'rightImage'         => '\Utility\Entity\Image',
35                         'leftImage'          => '\Utility\Entity\Image',
36                         'backImage'          => '\Utility\Entity\Image',
37                         'interiorImage'      => '\Utility\Entity\Image',
38                         'engineImage'        => '\Utility\Entity\Image',
39                         'natisImage'         => '\Utility\Entity\Image'
40                 ),
41                 'Dependants'  => array(
42                         'accessories' => '\Stock\Entity\Accessory'
43                 )
44         );
45         /**
46          * @var array
47          */
48         protected $metaList = array(
49                 'Type'          => 'List',
50                 'Contract'      => 'Recurring',
51                 'RequiredInput' => array(),
52                 'OptionalInput' => array(
53                         'Filter' => array(
54                                 'created'              => 'Date',
55                                 'updated'              => 'Date',
56                                 'uvi'                  => 'String40',
57                                 'referenceNumber'      => 'String12',
58                                 'company'              => 'Id',
59                                 'createdBy'            => 'Id',
60                                 'region'               => 'Id',
61                                 'type'                 => 'Id',
62                                 'stockNumber'          => 'String15',
63                                 'registrationNumber'   => 'String15',
64                                 'vinNumber'            => 'String40',
65                                 'engineNumber'         => 'String40',
66                                 'fuelType'             => 'Id',
67                                 'transmissionType'     => 'Id',
68                                 'exteriorColour'       => 'Id',
69                                 'interiorColour'       => 'Id',
70                                 'condition'            => 'Id',
71                                 'vehicleYear'          => 'Id',
72                                 'km'                   => 'Integer',
73                                 'upholstery'           => 'Id',
74                                 'papers'               => 'Id',
75                                 'natis'                => 'Id',
76                                 'spareKeys'            => 'Boolean',
77                                 'fullServiceHistory'   => 'Id',
78                                 'tradePrice'           => 'Decimal',
79                                 'retailPrice'          => 'Decimal',
80                                 'listPrice'            => 'Decimal',
81                                 'retailAmountRequired' => 'Decimal',
82                                 'purchasePrice'        => 'Decimal',
83                                 'salePrice'            => 'Decimal',
84                                 'previousRepairsNotes' => 'String500',
85                                 'previousRepairsNoted' => 'Boolean',
86                                 'jobState'             => 'String25',
87                                 'makeManual'           => 'String150',
88                                 'modelManual'          => 'String150',
89                                 'typeManual'           => 'String150'
90
91                         )
92                 ),
93                 'Filter'        => array(),
94                 'Fields'        => array(
95                         'name', 'mmCode',
96                         'introYear', 'introMonth',
97                         'discYear', 'discMonth'
98                 ),
99                 'Expand'        => array()
100         );
101         /**
102          * @var array
103          */
104         protected $metaGrid = array(
105                 'Type'            => 'Grid',
106                 'Export'          => true,
107                 'Builder'         => '\Stock\Report\StockGrid',
108                 'Writer'          => '\Utility\Export\ExcelReport',
109                 'Output'          => 'Download',
110                 'Contract'        => 'Recurring',
111                 'Options'         => array(
112                         'Boolean' => array(
113                                 'ExportToExcel' => false
114                         )
115                 ),
116                 'RequiredInput'   => array(),
117                 'OptionalInput'   => array(
118                         'Grid' => array(
119                                 'NumberOfRecords' => 'Integer',
120                                 'Page'            => 'Integer',
121                                 'Filter'          => 'Array',
122                                 'OrderBy'         => 'Array',
123                         )
124                 ),
125                 'Base'            => 'stock',
126                 'NumberOfRecords' => 20,
127                 'Query'           => 'SELECT [SELECTION]
128                                                                 FROM \Stock\Entity\Stock stock
129                                                                 JOIN stock.company company
130                                                                 JOIN stock.vehicleYear vehicleYear
131                                                                 JOIN stock.type type
132                                                                 JOIN type.model model
133                                                                 JOIN model.make make
134                                                                 LEFT JOIN stock.auction auction
135                 LEFT JOIN stock.valuation valuation
136                                                                 LEFT JOIN stock.priceGuide priceGuide
137                                                                 LEFT JOIN company.group companyGroup
138                                                                 LEFT JOIN company.groupDivision groupDivision
139                                                                 LEFT JOIN stock.exteriorColour exteriorColour
140                                                                 [WHERE] [ORDER]',
141                 'Selection'       => 'stock, valuation, company, companyGroup, groupDivision, auction, priceGuide, exteriorColour, vehicleYear, make, model, type',
142                 'Filter'          => array(
143                         'stock.archived' => false,
144                         'stock.jobState' => '!=Valuation'
145                 ),
146                 'OrderBy'         => array('stock.created' => 'DESC'),
147                 'Fields'          => array(
148                         'id',
149                         'referenceNumber',
150                         'created'        => 'DateTime',
151                         'registrationNumber',
152                         'vinNumber',
153                         'type'           => array(
154                                 'name',
155                                 'mmCode',
156                                 'model' => array(
157                                         'name',
158                                         'make' => array(
159                                                 'name'
160                                         )
161                                 ),
162                         ),
163                         'vehicleYear'    => array(
164                                 'name'
165                         ),
166                         'exteriorColour' => array(
167                                 'name'
168                         ),
169                         'priceGuide'     => array(
170                                 'id'
171                         ),
172                         'valuation'      => array(
173                                 'amountOffered'
174                         ),
175                         'auction'        => array(
176                                 'jobState'
177                         ),
178                         'km',
179                         'tradePrice',
180                         'retailPrice',
181                         'listPrice',
182                         'retailAmountRequired',
183                         'highestBid',
184                         'highestOffer',
185                         'numberOfOffers',
186                         'jobState'
187                 )
188         );
189         /**
190          * @var array
191          */
192         protected $metaTradeCenterGrid = array(
193                 'Type'            => 'Grid',
194                 'Export'          => true,
195                 'Builder'         => '\Stock\Report\StockGrid',
196                 'Writer'          => '\Utility\Export\ExcelReport',
197                 'Output'          => 'Download',
198                 'Contract'        => 'Recurring',
199                 'Options'         => array(
200                         'Boolean' => array(
201                                 'ExportToExcel' => false
202                         )
203                 ),
204                 'RequiredInput'   => array(),
205                 'OptionalInput'   => array(
206                         'Grid' => array(
207                                 'NumberOfRecords' => 'Integer',
208                                 'Page'            => 'Integer',
209                                 'Filter'          => 'Array',
210                                 'OrderBy'         => 'Array',
211                         )
212                 ),
213                 'Base'            => 'stock',
214                 'NumberOfRecords' => 10,
215                 'Query'           => 'SELECT [SELECTION]
216                                                                 FROM \Stock\Entity\Stock stock
217                                                                 JOIN stock.company company
218                                                                 JOIN stock.vehicleYear vehicleYear
219                                                                 JOIN stock.type type
220                                                                 JOIN type.model model
221                                                                 JOIN model.make make
222                                                                 LEFT JOIN stock.auction auction
223                                                                 LEFT JOIN stock.valuation valuation
224                                                                 LEFT JOIN company.group companyGroup
225                                                                 LEFT JOIN company.groupDivision groupDivision
226                                                                 LEFT JOIN stock.exteriorColour exteriorColour
227                                                                 [WHERE] [ORDER]',
228                 'Selection'       => 'stock, valuation, company, companyGroup, groupDivision, auction, exteriorColour, vehicleYear, make, model, type',
229                 'Filter'          => array(
230                         'stock.archived' => false,
231                         'stock.jobState' => 'IN Trade Center,Auction,Sold'
232                 ),
233                 'OrderBy'         => array('stock.created' => 'DESC'),
234                 'Fields'          => array(
235                         'id',
236                         'referenceNumber',
237                         'created'     => 'DateTime',
238                         'registrationNumber',
239                         'vinNumber',
240                         'company'     => array(
241                                 'name'
242                         ),
243                         'type'        => array(
244                                 'name',
245                                 'mmCode',
246                                 'model' => array(
247                                         'name',
248                                         'make' => array(
249                                                 'name'
250                                         )
251                                 ),
252                         ),
253                         'vehicleYear' => array(
254                                 'name'
255                         ),
256                         'valuation'   => array(
257                                 'amountOffered'
258                         ),
259                         'auction'     => array(
260                                 'jobState'
261                         ),
262                         'km',
263                         'tradePrice',
264                         'retailPrice',
265                         'listPrice',
266                         'retailAmountRequired',
267                         'highestBid',
268                         'jobState'
269                 )
270         );
271         /**
272          * aht5
273          * @var array
274          */
275         protected $metaView = array(
276                 'Type'      => 'View',
277                 'Expand'    => array(
278                         'group',
279                         'groupDivision',
280                         'model',
281                         'make',
282                         'type'
283                 ),
284                 'Intersect' => array()
285         );
286         /**
287          * @var array
288          */
289         protected $metaCreate = array(
290                 'Type'          => 'Create',
291                 'Contract'      => 'Recurring',
292                 'ExecuteBefore' => array(
293                         'addCustomVehicles'
294                 ),
295                 'ExecuteAfter'  => array(
296                         'generateHistoryList',
297                         'pushCreateAction'
298                 ),
299                 'RequiredInput' => array(
300                         'Stock' => array(
301                                 'vehicleYear'        => 'Id',
302                                 'stockNumber'        => 'String15',
303                                 'registrationNumber' => 'String15',
304                                 'fuelType'           => 'Id',
305                                 'transmissionType'   => 'Id'
306                         )
307                 ),
308                 'OptionalInput' => array(
309                         'Stock' => array(
310                                 'category'             => 'Id',
311                                 'make'                 => 'Id',
312                                 'model'                => 'Id',
313                                 'type'                 => 'Id',
314                                 'region'               => 'Id',
315                                 'vinNumber'            => 'String40',
316                                 'engineNumber'         => 'String40',
317                                 'exteriorColour'       => 'Id',
318                                 'interiorColour'       => 'Id',
319                                 'condition'            => 'Id',
320                                 'km'                   => 'Integer',
321                                 'upholstery'           => 'Id',
322                                 'papers'               => 'Id',
323                                 'natis'                => 'Id',
324                                 'spareKeys'            => 'Boolean',
325                                 'fullServiceHistory'   => 'Id',
326                                 'fshNotes'             => 'String500',
327                                 'tradePrice'           => 'Decimal',
328                                 'retailPrice'          => 'Decimal',
329                                 'listPrice'            => 'Decimal',
330                                 'retailAmountRequired' => 'Decimal',
331                                 'purchasePrice'        => 'Decimal',
332                                 'salePrice'            => 'Decimal',
333                                 'mainImage'            => 'Id',
334                                 'frontImage'           => 'Id',
335                                 'rightImage'           => 'Id',
336                                 'leftImage'            => 'Id',
337                                 'backImage'            => 'Id',
338                                 'interiorImage'        => 'Id',
339                                 'engineImage'          => 'Id',
340                                 'natisImage'           => 'Id',
341                                 'accessories'          => 'Collection',
342                                 'accessoryNotes'       => 'String500',
343                                 'damages'              => 'Collection',
344                                 'damageTotal'          => 'Decimal',
345                                 'previousRepairsNoted' => 'Boolean',
346                                 'previousRepairsNotes' => 'String500',
347                                 'damageNotes'          => 'String500',
348                                 'coverReceived'        => 'String500',
349                                 'makeManual'           => 'String150',
350                                 'modelManual'          => 'String150',
351                                 'typeManual'           => 'String150'
352
353
354                         )
355                 )
356         );
357         /**
358          * @var array
359          */
360         protected $metaCreateAuction = array(
361                 'Type'          => 'Create',
362                 'Contract'      => 'Recurring',
363                 'ExecuteBefore' => array(
364                         'addCustomVehicles'
365                 ),
366                 'ExecuteAfter'  => array(
367                         'generateHistoryList',
368                         'pushCreateAction'
369                 ),
370                 'Flags'         => array(
371                         'NewStockItem.Status' => 'Auction.NewItemFromStock'
372                 ),
373                 'RequiredInput' => array(
374                         'Context' => array(
375                                 'endDate'      => 'Date',
376                                 'reservePrice' => 'Decimal'
377                         ),
378                         'Stock'   => array(
379                                 'vehicleYear'        => 'Id',
380                                 'stockNumber'        => 'String15',
381                                 'registrationNumber' => 'String15',
382                                 'fuelType'           => 'Id',
383                                 'transmissionType'   => 'Id'
384                         )
385                 ),
386                 'OptionalInput' => array(
387                         'Stock' => array(
388                                 'category'             => 'Id',
389                                 'make'                 => 'Id',
390                                 'model'                => 'Id',
391                                 'type'                 => 'Id',
392                                 'region'               => 'Id',
393                                 'vinNumber'            => 'String40',
394                                 'engineNumber'         => 'String40',
395                                 'exteriorColour'       => 'Id',
396                                 'interiorColour'       => 'Id',
397                                 'condition'            => 'Id',
398                                 'km'                   => 'Integer',
399                                 'upholstery'           => 'Id',
400                                 'papers'               => 'Id',
401                                 'natis'                => 'Id',
402                                 'spareKeys'            => 'Boolean',
403                                 'fullServiceHistory'   => 'Id',
404                                 'fshNotes'             => 'String500',
405                                 'tradePrice'           => 'Decimal',
406                                 'retailPrice'          => 'Decimal',
407                                 'listPrice'            => 'Decimal',
408                                 'retailAmountRequired' => 'Decimal',
409                                 'purchasePrice'        => 'Decimal',
410                                 'salePrice'            => 'Decimal',
411                                 'mainImage'            => 'Id',
412                                 'frontImage'           => 'Id',
413                                 'rightImage'           => 'Id',
414                                 'leftImage'            => 'Id',
415                                 'backImage'            => 'Id',
416                                 'interiorImage'        => 'Id',
417                                 'engineImage'          => 'Id',
418                                 'natisImage'           => 'Id',
419                                 'accessories'          => 'Collection',
420                                 'accessoryNotes'       => 'String500',
421                                 'damages'              => 'Collection',
422                                 'damageTotal'          => 'Decimal',
423                                 'previousRepairsNoted' => 'Boolean',
424                                 'previousRepairsNotes' => 'String500',
425                                 'damageNotes'          => 'String500',
426                                 'coverReceived'        => 'String500',
427                                 'makeManual'           => 'String150',
428                                 'modelManual'          => 'String150',
429                                 'typeManual'           => 'String150'
430                         )
431                 )
432         );
433         /**
434          * @var array
435          */
436         protected $metaUpdate = array(
437                 'Type'          => 'Update',
438                 'Contract'      => 'UseOnce',
439                 'Expand'        => array(
440                         'auction',
441                         'category',
442                         'make',
443                         'model',
444                         'type',
445                         'mainImage',
446                         'frontImage',
447                         'rightImage',
448                         'leftImage',
449                         'backImage',
450                         'interiorImage',
451                         'engineImage',
452                         'natisImage'
453                 ),
454                 'ExecuteBefore' => array(
455                         'updateCustomVehicles'
456                 ),
457                 'ExecuteAfter' => array(
458                         'pushUpdateAction'
459                 ),
460                 'RequiredInput' => array(
461                         'Stock' => array(
462                                 'vehicleYear'        => 'Id',
463                                 'stockNumber'        => 'String15',
464                                 'registrationNumber' => 'String15',
465                                 'fuelType'           => 'Id',
466                                 'transmissionType'   => 'Id'
467                         )
468                 ),
469                 'OptionalInput' => array(
470                         'Stock' => array(
471                                 'category'             => 'Id',
472                                 'make'                 => 'Id',
473                                 'model'                => 'Id',
474                                 'type'                 => 'Id',
475                                 'region'               => 'Id',
476                                 'vinNumber'            => 'String40',
477                                 'engineNumber'         => 'String40',
478                                 'exteriorColour'       => 'Id',
479                                 'interiorColour'       => 'Id',
480                                 'condition'            => 'Id',
481                                 'km'                   => 'Integer',
482                                 'upholstery'           => 'Id',
483                                 'papers'               => 'Id',
484                                 'natis'                => 'Id',
485                                 'spareKeys'            => 'Boolean',
486                                 'fullServiceHistory'   => 'Id',
487                                 'fshNotes'             => 'String500',
488                                 'tradePrice'           => 'Decimal',
489                                 'retailPrice'          => 'Decimal',
490                                 'listPrice'            => 'Decimal',
491                                 'oldTradePrice'        => 'Decimal',
492                                 'oldRetailPrice'       => 'Decimal',
493                                 'oldListPrice'         => 'Decimal',
494                                 'retailAmountRequired' => 'Decimal',
495                                 'purchasePrice'        => 'Decimal',
496                                 'salePrice'            => 'Decimal',
497                                 'mainImage'            => 'Id',
498                                 'frontImage'           => 'Id',
499                                 'rightImage'           => 'Id',
500                                 'leftImage'            => 'Id',
501                                 'backImage'            => 'Id',
502                                 'interiorImage'        => 'Id',
503                                 'engineImage'          => 'Id',
504                                 'natisImage'           => 'Id',
505                                 'accessories'          => 'Collection',
506                                 'accessoryNotes'       => 'String500',
507                                 'damages'              => 'Collection',
508                                 'damageTotal'          => 'Decimal',
509                                 'previousRepairsNoted' => 'Boolean',
510                                 'previousRepairsNotes' => 'String500',
511                                 'damageNotes'          => 'String500',
512                                 'coverReceived'        => 'String500',
513                                 'makeManual'           => 'String150',
514                                 'modelManual'          => 'String150',
515                                 'typeManual'           => 'String150'
516                         )
517                 )
518         );
519         /**
520          * @var array
521          */
522         protected $metaLimitedUpdate = array(
523                 'Type'          => 'Update',
524                 'Contract'      => 'UseOnce',
525                 'ExecuteBefore' => array(
526                         'updateCustomVehicles'
527                 ),
528                 'ExecuteAfter' => array(
529                         'pushUpdateAction'
530                 ),
531                 'Expand'        => array(
532                         'auction',
533                         'category',
534                         'make',
535                         'model',
536                         'type',
537                         'mainImage',
538                         'frontImage',
539                         'rightImage',
540                         'leftImage',
541                         'backImage',
542                         'interiorImage',
543                         'engineImage',
544                         'natisImage',
545
546                 ),
547                 'RequiredInput' => array(),
548                 'OptionalInput' => array()
549         );
550         /**
551          * @var array
552          */
553         protected $metaDelete = array(
554                 'Type'         => 'Delete',
555                 'Contract'     => 'UseOnce',
556                 'ExecuteAfter' => array(
557                         'pushDeleteAction'
558                 )
559         );
560         /**
561          * @var array
562          */
563         protected $metaSendToRetail = array(
564                 'Type'          => 'Update',
565                 'Contract'      => 'UseOnce',
566                 'ExecuteAfter'  => array(
567                         'pushUpdateAction'
568                 ),
569                 'RequiredInput' => array(
570                         'Context' => array(
571                                 'retailOutlets' => 'Array'
572                         ),
573                         'Stock'   => array(
574                                 'minimumAmount' => 'Decimal'
575                         )
576                 ),
577                 'OptionalInput' => array()
578         );
579         /**
580          * @var array
581          */
582         protected $metaRouteSendToRetail = array(
583                 'Type'        => 'Route',
584                 'Contract'    => 'UseOnce',
585                 'Destination' => 'Retail.NewItemFromStock',
586                 'Surrogate'   => 'metaSendToRetail'
587         );
588         /**
589          * @var array
590          */
591         protected $metaRouteSendToPriceGuide = array(
592                 'Type'          => 'Route',
593                 'Contract'      => 'UseOnce',
594                 'Destination'   => 'PriceGuide.NewItemFromStock',
595                 'ExecuteAfter'  => array(
596                         'pushUpdateAction'
597                 ),
598                 'RequiredInput' => array(
599                         'Context' => array(
600                                 'clubs' => 'Array'
601                         )
602                 ),
603                 'OptionalInput' => array()
604         );
605         /**
606          * @var array
607          */
608         protected $metaRouteSendToAuction = array(
609                 'Type'          => 'Route',
610                 'Contract'      => 'UseOnce',
611                 'Destination'   => 'Auction.NewItemFromStock',
612                 'ExecuteAfter'  => array(
613                         'pushUpdateAction'
614                 ),
615                 'RequiredInput' => array(
616                         'Context' => array(
617                                 'endDate'      => 'Date',
618                                 'reservePrice' => 'Decimal'
619                         )
620                 ),
621                 'OptionalInput' => array()
622         );
623
624
625
626         /**
627          * Add some dynamic filtering to our grids.
628          */
629         public function __construct()
630         {
631                 $authData = \Utility\Registry::getAuthData();
632                 switch (\Utility\Registry::getUserType())
633                 {
634                         case 'B4C User':
635                                 $groupFilter                                                             = \Utility\Registry::getSudo('Group', false);
636                                 $groupFilter
637                                 && $this->metaGrid['Filter']['IDENTITY(company.group)']                  = $groupFilter;
638                                 $divisionFilter                                                          = \Utility\Registry::getSudo('Division', false);
639                                 $divisionFilter
640                                 && $this->metaGrid['Filter']['IDENTITY(company.groupDivision)']          = $divisionFilter;
641                                 $companyFilter                                                           = \Utility\Registry::getSudo('Company', false);
642                                 $companyFilter
643                                 && $this->metaGrid['Filter']['company.id']                               = $companyFilter;
644                                 $companyFilter
645                                 && $this->metaTradeCenterGrid['Filter']['IDENTITY(company.tradeCenter)'] = $companyFilter;
646                                 break;
647                         case 'Group User':
648                                 $this->metaGrid['Filter']['IDENTITY(company.group)']                     = $authData['company']['group']['id'];
649                                 $divisionFilter                                                          = \Utility\Registry::getSudo('Division', false);
650                                 $divisionFilter
651                                 && $this->metaGrid['Filter']['IDENTITY(company.groupDivision)']          = $divisionFilter;
652                                 $companyFilter                                                           = \Utility\Registry::getSudo('Company', false);
653                                 $companyFilter
654                                 && $this->metaGrid['Filter']['company.id']                               = $companyFilter;
655                                 $companyFilter
656                                 && $this->metaTradeCenterGrid['Filter']['IDENTITY(company.tradeCenter)'] = $companyFilter;
657                                 break;
658                         case 'Dealer Principle':
659                                 $this->metaGrid['Filter']['IDENTITY(company.groupDivision)']             = $authData['company']['groupDivision']['id'];
660                                 $companyFilter                                                           = \Utility\Registry::getSudo('Company', false);
661                                 $companyFilter
662                                 && $this->metaGrid['Filter']['company.id']                               = $companyFilter;
663                                 $companyFilter
664                                 && $this->metaTradeCenterGrid['Filter']['IDENTITY(company.tradeCenter)'] = $companyFilter;
665                                 break;
666                         default:
667                                 $this->metaGrid['Filter']['company.id']                               = $authData['company']['id'];
668                                 $this->metaTradeCenterGrid['Filter']['IDENTITY(company.tradeCenter)'] = $authData['company']['id'];
669                                 break;
670                 }
671         }
672
673 }