initial commit
[namibia] / public / js / app / portal-workspace / tcstock.js
1 ;(function(){
2
3         window._w.tcstock = {
4
5                 ti : null,
6                 dfrom : '',
7                 dto   : '',
8
9                 onPageDestruct : function()
10                 {
11                         App.Event.removeListener('stockGridSuFilter', 'SuChange');
12                 },
13
14                 onStockPageTemplateReady : function( template, event, eventData )
15                 {
16                         _w.tcstock.ti = _t[template];
17                         App.DataStore.setData('stockstatuses',
18                                         [
19                                          {"value":"Auction", "label":"Auction"},
20                                          {"value":"Trade Center", "label":"Trade Center"},
21                                          {"value":"Sold", "label":"Sold"},
22                                          {"value":"Relist", "label":"Relist"}
23                                          ]
24                         );
25                         App.DataStore.loadSelectListData('tcdealers', true, 'Company', 'Company.TcDealerSelectList');
26                         _w.tcstock.ti.hydrate({
27                                 'gridTitle': 'Trade Stock Control',
28                                 'gridTitleSubtext': 'Please use the filters below to narrow down the view',
29                                 'gridTitleButtons': {
30                                         constructor: 'Button',
31                                         items: {
32                                                 Export: {
33                                                         preset: 'GridExport',
34                                                         id: 'exportStockGrid',
35                                                         handler: _w.tcstock.exportStockGrid
36                                                 }
37                                         }
38                                 },
39                                 'gridContextFilter': {
40                                         constructor: 'GridContextFilter',
41                                         items: {
42                                                 RegNum: {
43                                                         style: 'filter-input-wrapper',
44                                                         id: 'gridStockContext_RegNo',
45                                                         placeholder: 'Registration number',
46                                                         context: 'gridStockContext'
47                                                 },
48                                                 RefNum: {
49                                                         style: 'filter-input-wrapper',
50                                                         id: 'gridStockContext_RefNo',
51                                                         placeholder: 'Ref number',
52                                                         context: 'gridStockContext'
53                                                 },
54                                                 DateFrom: {
55                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dfrom',
56                                                         title: '  Date created',
57                                                         id: 'gridStockContext_DateFrom',
58                                                         placeholder: 'From',
59                                                         context: 'gridStockContext',
60                                                         append: '<i class="icon-calendar"></i>',
61                                                         readonly: true,
62                                                         bind: function() {
63                                                                 $('#gridStockContext_DateFrom').change(function() {
64                                                                         _w.tcstock.dFrom = $(this).val();
65                                                                 });
66                                                         }
67                                                 },
68                                                 DateTo: {
69                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dto',
70                                                         id: 'gridStockContext_DateTo',
71                                                         placeholder: 'To',
72                                                         context: 'gridStockContext',
73                                                         append: '<i class="icon-calendar"></i>',
74                                                         readonly: true,
75                                                         bind: function() {
76                                                                 $('#gridStockContext_DateTo').change(function() {
77                                                                         _w.tcstock.dTo = $(this).val();
78                                                                 });
79                                                         }
80                                                 },
81                                                 Buttons: {
82                                                         actions: true,
83                                                         clearId: 'clearStockGridContext',
84                                                         bindClear: _w.tcstock.clearStockGridContext,
85                                                         searchId: 'searchStockGrid',
86                                                         bindSearch: _w.tcstock.searchStockGrid
87                                                 }
88                                         }
89                                 },
90                                 'gridColumnHeaders': {
91                                         constructor: 'GridColumnHeader',
92                                         items: {
93                                                 Company: {
94                                                         id: 'gridStockColumn_Company',
95                                                         title: 'DEALERSHIP',
96                                                         orderAsc: {
97                                                                 id: 'gridStockOrder_CompanyAsc',
98                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'company.name', 'ASC')
99                                                         },
100                                                         orderDesc: {
101                                                                 id: 'gridStockOrder_CompanyDesc',
102                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'company.name', 'DESC')
103                                                         }
104                                                 },
105                                                 VIN: {
106                                                         id: 'gridStockColumn_VIN',
107                                                         title: 'VIN',
108                                                         orderAsc: {
109                                                                 id: 'gridStockOrder_VinAsc',
110                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.vinNumber', 'ASC')
111                                                         },
112                                                         orderDesc: {
113                                                                 id: 'gridStockOrder_VinDesc',
114                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.vinNumber', 'DESC')
115                                                         },
116                                                         style: 'hidden-phone hidden-tablet'
117                                                 },
118                                                 Make: {
119                                                         id: 'gridStockColumn_Make',
120                                                         title: 'MAKE',
121                                                         orderAsc: {
122                                                                 id: 'gridStockOrder_MakeAsc',
123                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'make.name', 'ASC')
124                                                         },
125                                                         orderDesc: {
126                                                                 id: 'gridStockOrder_MakeDesc',
127                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'make.name', 'DESC')
128                                                         }
129                                                 },
130                                                 Model: {
131                                                         id: 'gridStockColumn_Model',
132                                                         title: 'MODEL',
133                                                         orderAsc: {
134                                                                 id: 'gridStockOrder_ModelAsc',
135                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'model.name', 'ASC')
136                                                         },
137                                                         orderDesc: {
138                                                                 id: 'gridStockOrder_ModelDesc',
139                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'model.name', 'DESC')
140                                                         },
141                                                         style: 'hidden-phone hidden-tablet'
142                                                 },
143                                                 Type: {
144                                                         id: 'gridStockColumn_Type',
145                                                         title: 'TYPE',
146                                                         orderAsc: {
147                                                                 id: 'gridStockOrder_TypeAsc',
148                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'type.name', 'ASC')
149                                                         },
150                                                         orderDesc: {
151                                                                 id: 'gridStockOrder_TypeDesc',
152                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'type.name', 'DESC')
153                                                         },
154                                                         style: 'type'
155                                                 },
156                                                 Year: {
157                                                         id: 'gridStockColumn_Year',
158                                                         title: 'YEAR',
159                                                         orderAsc: {
160                                                                 id: 'gridStockOrder_YearAsc',
161                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'vehicleYear.name', 'ASC')
162                                                         },
163                                                         orderDesc: {
164                                                                 id: 'gridStockOrder_YearDesc',
165                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'vehicleYear.name', 'DESC')
166                                                         },
167                                                         style: 'year'
168                                                 },
169                                                 KM: {
170                                                         id: 'gridStockColumn_KM',
171                                                         title: 'KM&#39;S',
172                                                         orderAsc: {
173                                                                 id: 'gridStockOrder_KmAsc',
174                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.km', 'ASC')
175                                                         },
176                                                         orderDesc: {
177                                                                 id: 'gridStockOrder_KmDesc',
178                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.km', 'DESC')
179                                                         },
180                                                         style: 'kms hidden-phone hidden-tablet'
181                                                 },
182                                                 Trade: {
183                                                         id: 'gridStockColumn_Trade',
184                                                         title: 'TRADE',
185                                                         orderAsc: {
186                                                                 id: 'gridStockOrder_TradeAsc',
187                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.tradePrice', 'ASC')
188                                                         },
189                                                         orderDesc: {
190                                                                 id: 'gridStockOrder_TradeDesc',
191                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.tradePrice', 'DESC')
192                                                         },
193                                                         style: 'hidden-phone hidden-tablet'
194                                                 },
195                                                 Retail: {
196                                                         id: 'gridStockColumn_Retail',
197                                                         title: 'RETAIL',
198                                                         orderAsc: {
199                                                                 id: 'gridStockOrder_RetailAsc',
200                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.retailPrice', 'ASC')
201                                                         },
202                                                         orderDesc: {
203                                                                 id: 'gridStockOrder_RetailDesc',
204                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.retailPrice', 'DESC')
205                                                         },
206                                                         style: 'hidden-phone hidden-tablet'
207                                                 },
208                         List: {
209                             id: 'gridStockColumn_List',
210                             title: 'NEW LIST PRICE',
211                             orderAsc: {
212                                 id: 'gridStockOrder_ListAsc',
213                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.listPrice', 'ASC')
214                             },
215                             orderDesc: {
216                                 id: 'gridStockOrder_ListDesc',
217                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.listPrice', 'DESC')
218                             },
219                             style: 'hidden-phone hidden-tablet'
220                         },
221                                                 Bid: {
222                                                         id: 'gridStockColumn_Bid',
223                                                         title: 'BID',
224                                                         orderAsc: {
225                                                                 id: 'gridStockOrder_BidAsc',
226                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.highestBid', 'ASC')
227                                                         },
228                                                         orderDesc: {
229                                                                 id: 'gridStockOrder_BidDesc',
230                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.highestBid', 'DESC')
231                                                         },
232                                                         style: 'hidden-phone hidden-tablet'
233                                                 },
234                                                 Status: {
235                                                         id: 'gridStockColumn_Status',
236                                                         title: 'STATUS',
237                                                         orderAsc: {
238                                                                 id: 'gridStockOrder_JobstateAsc',
239                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.jobState', 'ASC')
240                                                         },
241                                                         orderDesc: {
242                                                                 id: 'gridStockOrder_JobstateDesc',
243                                                                 bind: $.proxy(_w.tcstock.orderStockGrid, this, 'stock.jobState', 'DESC')
244                                                         }
245
246
247                                                 }
248                                         }
249                                 },
250                                 'gridColumnFilters': {
251                                         constructor: 'GridColumnFilter',
252                                         items: {
253                                                 Company: {
254                                                         id: 'gridStockFilter_Company',
255                                                         context: 'gridStockFilter',
256                                                         dataStoreId : 'tcdealers',
257                                                         selectEmpty : 'All'
258                                                 },
259                                                 VIN: {
260                                                         id: 'gridStockFilter_VIN',
261                                                         context: 'gridStockFilter'
262                                                 },
263                                                 Make: {
264                                                         id: 'gridStockFilter_Make',
265                                                         context: 'gridStockFilter'
266                                                 },
267                                                 Model: {
268                                                         id: 'gridStockFilter_Model',
269                                                         context: 'gridStockFilter'
270                                                 },
271                                                 Type: {
272                                                         id: 'gridStockFilter_Type',
273                                                         context: 'gridStockFilter'
274                                                 },
275                                                 Year: {
276                                                         id: 'gridStockFilter_Year',
277                                                         context: 'gridStockFilter'
278                                                 },
279                                                 KM: {
280                                                         id: 'gridStockFilter_KM',
281                                                         context: 'gridStockFilter'
282                                                 },
283                                                 Trade: {
284                                                         id: 'gridStockFilter_Trade',
285                                                         context: 'gridStockFilter'
286                                                 },
287                                                 Retail: {
288                                                         id: 'gridStockFilter_Retail',
289                                                         context: 'gridStockFilter'
290                                                 },
291                                                 Bid: {
292                                                         id: 'gridStockFilter_Bid',
293                                                         context: 'gridStockFilter'
294                                                 },
295                                                 Status: {
296                                                         id: 'gridStockFilter_Status',
297                                                         context: 'gridStockFilter',
298                                                         dataStoreId : 'stockstatuses',
299                                                         selectEmpty : 'All'
300                                                 }
301                                         }
302                                 },
303                                 'gridRowRepeater': {},
304                                 'gridPager': {}
305                         });
306
307                         // Retrieve initial grid data.
308                         if (null == App.API.taskContract('gridTCStock'))
309                         {
310                                 App.API.getTask(
311                                                 'gridTCStock', 'Stock', 'Stock.TradeCenterGrid', null,
312                                                 {}, _w.tcstock.loadStockGrid, _w.taskContractError
313                                                 );
314                         }
315                         else
316                         {
317                                 _w.tcstock.loadStockGrid( null, {}, {} );
318                         }
319                 },
320
321                 onStockPublished : function()
322                 {
323                         $(".gridStockFilter").keyup(function (evt) {
324                                 var charCode = evt.charCode || evt.keyCode;
325                                 if (charCode  == 13) { //Enter key's keycode
326                                         $('#searchStockGrid').click();
327                                 }
328                         });
329
330                         $(".gridStockContext").keyup(function (evt) {
331                                 var charCode = evt.charCode || evt.keyCode;
332                                 if (charCode  == 13) { //Enter key's keycode
333                                         $('#searchStockGrid').click();
334                                 }
335                         });
336
337                         var _p = App.permissions;
338                         var _createStockDisabled = false == _p.stockCreateNew && false == _p.stockCreateAuction
339                                                                          ? true : false;
340                         $('#newStock').prop('disabled', _createStockDisabled);
341                         $('#exportStockGrid').prop('disabled', !App.permissions.stockGridExport);
342                         App.Event.listen('stockGridSuFilter', 'SuChange', $.proxy(_w.tcstock.loadStockGrid, this), 'Recurring');
343                         $('.dfrom').datepicker({format:"yyyy-mm-dd"});
344                         $('.dto').datepicker({format:"yyyy-mm-dd"});
345                         $('#gridStockContext_DateFrom').val(_w.tcstock.dFrom);
346                         $('#gridStockContext_DateTo').val(_w.tcstock.dTo);
347
348                         setTimeout(_w.tcstock.refreshGrid, 120000);
349                 },
350
351                 refreshGrid : function()
352                 {
353                         if ('stock' != App.activePage)
354                         {
355                                 return;
356                         }
357                         _w.tcstock.loadStockGrid( null, {}, {} );
358                         setTimeout(_w.tcstock.refreshGrid, 120000);
359                 },
360
361                 pageStockGrid : function( page )
362                 {
363                         _w.tcstock.loadStockGrid( {}, {"Grid":{"Page":page}} );
364                 },
365
366                 loadStockGrid : function( contract, data, options )
367                 {
368                         App.API.execTask(
369                                         'gridTCStock', data, options,
370                                         _w.tcstock._onStockGridDataReceived, _w.taskExecError
371                                         );
372                 },
373
374                 _onStockGridDataReceived : function( response )
375                 {
376                         _w.tcstock.ti.hydrateParam('gridRowRepeater', {});
377                         _w.populateFilters(response.Data.Meta.Filters, {
378                                 'stock.registrationNumber': 'gridStockContext_RegNo',
379                                 'stock.referenceNumber': 'gridStockContext_RefNo',
380                                 'company.id': 'gridStockFilter_Company',
381                                 'stock.vinNumber': 'gridStockFilter_VIN',
382                                 'make.name': 'gridStockFilter_Make',
383                                 'model.name': 'gridStockFilter_Model',
384                                 'type.name': 'gridStockFilter_Type',
385                                 'vehicleYear.name': 'gridStockFilter_Year',
386                                 'stock.km': 'gridStockFilter_KM',
387                                 'stock.tradePrice': 'gridStockFilter_Trade',
388                                 'stock.retailPrice': 'gridStockFilter_Retail',
389                 'stock.listPrice': 'gridStockFilter_List',
390                                 'stock.highestBid': 'gridStockFilter_Bid',
391                                 'stock.jobState': 'gridStockFilter_Status'
392                         });
393                         $('select.gridStockFilter').each(function(i, elem) {
394                                 $(elem).selectpicker('refresh');
395                         });
396                         var gridData = [];
397                         for (var i = 0; i < response.Data.DataSet.length; i++)
398                         {
399                                 var row = response.Data.DataSet[i];
400                                 gridData.push({
401                                         url: ('Trade Center' == row.jobState) ? 'tcstockview' : 'limitedtcstockview',
402                                         prepend: {
403                                                 Trade  : 'R ',
404                                                 Retail : 'R ',
405                                                 Offer  : 'R '
406                                         },
407                                         style: {
408                                                 VIN     : 'hidden-phone hidden-tablet',
409                                                 Model   : 'hidden-phone hidden-tablet',
410                                                 KM      : 'kms hidden-phone hidden-tablet',
411                                                 Trade   : 'hidden-phone hidden-tablet',
412                                                 Retail  : 'hidden-phone hidden-tablet',
413                                                 Bid     : 'hidden-phone hidden-tablet'
414                                         },
415                                         items: {
416                                                 'id'      : row.id,
417                                                 'Company' : row.company.name,
418                                                 'VIN'     : row.vinNumber,
419                                                 'Make'    : row.type.model.make.name,
420                                                 'Model'   : row.type.model.name,
421                                                 'Type'    : row.type.name,
422                                                 'Year'    : row.vehicleYear.name,
423                                                 'KM'      : row.km,
424                                                 'Trade'   : row.tradePrice,
425                                                 'Retail'  : row.retailPrice,
426                         'List'   : row.listPrice,
427                                                 'Bid'     : row.highestBid,
428                                                 'Status'  : row.auction.jobState == 'Undone' || row.auction.jobState == 'Relist' ? 'Relist' : row.jobState
429                                 }
430                                 });
431                         }
432                         _w.tcstock.ti.hydrateParam('gridRowRepeater', {
433                                 constructor : 'GridDataRow',
434                                 items       : gridData
435                         });
436                         response.Data.Meta.itemName = 'stockPager';
437                         response.Data.Meta.bind = _w.tcstock.pageStockGrid;
438                         _w.tcstock.ti.hydrateParam('gridPager', {
439                                 constructor : 'GridPager',
440                                 items       : [response.Data.Meta]
441                         });
442                 },
443
444                 exportStockGrid : function()
445                 {
446                         App.API.execTask(
447                                         'gridTCStock', {}, {'ExportToExcel': true},
448                                         _w.tcstock._onExportReceived, _w.taskExecError,
449                                         'EXPORT', true
450                                         );
451                 },
452
453                 newStock : function()
454                 {
455                         window.location.hash = '/tcstockview?id=0';
456                 },
457
458                 _onExportReceived : function( response )
459                 {
460                         var win = window.open();
461             win.document.write(response);
462                 },
463
464                 clearStockGridContext : function()
465                 {
466                         try
467                         {
468                                 $('select.gridStockFilter').each(function(i, elem) {
469                                         $(elem).selectpicker('val', '');
470                                         $(elem).selectpicker('refresh');
471                                 });
472                         }
473                         catch (err) { console.log(err); }
474                         $('.gridStockContext').val('');
475                         $('.gridStockFilter').val('');
476                         _w.tcstock.searchStockGrid(true);
477                 },
478
479                 searchStockGrid : function(resetOrder)
480                 {
481                         var filter = {
482                                         count: 0,
483                                         filters: {}
484                         };
485                         filter = _w.filterIfnotEmpty(filter, 'gridStockContext_RegNo', 'stock.registrationNumber');
486                         filter = _w.filterIfnotEmpty(filter, 'gridStockContext_RefNo', 'stock.referenceNumber');
487                         filter = _w.filterIfnotEmpty(filter, 'gridStockContext_DateFrom', 'stock.created', '>=');
488                         filter = _w.filterIfnotEmpty(filter, 'gridStockContext_DateTo', 'stock.created', '<=', ' 23:59:59');
489
490                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Company', 'company.id');
491                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_VIN', 'stock.vinNumber');
492                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Make', 'make.name');
493                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Model', 'model.name');
494                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Type', 'type.name');
495                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Year', 'vehicleYear.name');
496                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_KM', 'stock.km');
497                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Trade', 'stock.tradePrice');
498                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Retail', 'stock.retailPrice');
499             filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_List', 'stock.listPrice');
500                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Offer', 'stock.highestOffer');
501                         filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Status', 'stock.jobState');
502                         var request = {"Grid":{
503                             "Page": 1,
504                             "Filter": filter.filters
505                           }};
506                         if (resetOrder)
507                         {
508                                 request.Grid.OrderBy = {'stock.created': 'DESC'};
509                         }
510                         _w.tcstock.loadStockGrid( null, request, {} );
511                 },
512
513                 orderStockGrid : function(field, direction)
514                 {
515                         var order = {};
516                         order[field] = direction;
517                         _w.tcstock.loadStockGrid( {}, {"Grid":{"Page": 1, "OrderBy": order}} );
518                 }
519
520         };
521
522 })();