10 onPageDestruct : function()
12 App.Event.removeListener('stockGridSuFilter', 'SuChange');
16 onStockPageTemplateReady : function( template, event, eventData )
18 _w.stock.ti = _t[template];
19 App.DataStore.loadSelectListData('filtermakes', true, 'Stock', 'Make.SelectList');
20 if (App.companyData.tC)
22 App.DataStore.setData('stockstatuses',
24 {"value":"Stock", "label":"Stock"},
25 {"value":"Auction", "label":"Auction"},
26 {"value":"Trade Center", "label":"Trade Center"},
27 {"value":"Sold", "label":"Sold"},
28 {"value":"Relist", "label":"Relist"},
29 {"value":"Price Guide", "label":"Price Guide"},
30 {"value":"Retail", "label":"Retail"},
31 {"value":"Archived", "label":"Archived"}
37 App.DataStore.setData('stockstatuses',
39 {"value":"Stock", "label":"Stock"},
40 {"value":"Auction", "label":"Auction"},
41 {"value":"Sold", "label":"Sold"},
42 {"value":"Relist", "label":"Relist"},
43 {"value":"Price Guide", "label":"Price Guide"},
44 {"value":"Retail", "label":"Retail"},
45 {"value":"Archived", "label":"Archived"}
50 'gridTitle': 'Stock Control',
51 'gridTitleSubtext': 'Please use the filters below to narrow down the view',
53 constructor: 'Button',
56 preset: 'BulkImporter',
57 id: 'bulkImporterStockGrid',
58 handler: _w.stock.bulkImporterStockGrid
61 preset: 'AuctionReport',
62 id: 'auctionReportStockGrid',
63 handler: _w.stock.auctionReportStockGrid
67 id: 'exportStockGrid',
68 handler: _w.stock.exportStockGrid
73 handler: _w.stock.newStock
77 'gridContextFilter': {
78 constructor: 'GridContextFilter',
81 title: 'Search here:',
82 style: 'filter-input-wrapper',
83 id: 'gridStockContext_RefNo',
84 placeholder: 'Ref number',
85 context: 'gridStockContext'
88 style: 'filter-input-wrapper',
89 id: 'gridStockContext_RegNo',
90 placeholder: 'Registration number',
91 context: 'gridStockContext'
94 style : 'input-append date filter-input-wrapper-date mobile-width12p dfrom',
95 title: 'Search date created here',
96 id: 'gridStockContext_DateFrom',
98 context: 'gridStockContext',
99 append: '<i class="icon-calendar"></i>',
102 $('#gridStockContext_DateFrom').change(function() {
103 _w.stock.dFrom = $(this).val();
108 style : 'input-append date filter-input-wrapper-date mobile-width12p dto',
109 id: 'gridStockContext_DateTo',
111 context: 'gridStockContext',
112 append: '<i class="icon-calendar"></i>',
115 $('#gridStockContext_DateTo').change(function() {
116 _w.stock.dTo = $(this).val();
122 searchId: 'searchStockGrid',
123 bindSearch: _w.stock.searchStockGrid,
124 clearId: 'clearStockGridContext',
125 bindClear: _w.stock.clearStockGridContext
129 style : 'filter-input-wrapper hidden',
130 id: 'gridStockContext_RelistFilter',
131 placeholder: 'Relist Filter',
132 context: 'gridStockContext',
133 value: 'Relist,Undone'
137 'gridColumnHeaders': {
138 constructor: 'GridColumnHeader',
141 id: 'gridStockColumn_VIN',
144 id: 'gridStockOrder_VinAsc',
145 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.vinNumber', 'ASC')
148 id: 'gridStockOrder_VinDesc',
149 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.vinNumber', 'DESC')
151 style: 'hidden-phone'
154 id: 'gridStockColumn_Make',
157 id: 'gridStockOrder_MakeAsc',
158 bind: $.proxy(_w.stock.orderStockGrid, this, 'make.name', 'ASC')
161 id: 'gridStockOrder_MakeDesc',
162 bind: $.proxy(_w.stock.orderStockGrid, this, 'make.name', 'DESC')
167 id: 'gridStockColumn_Model',
170 id: 'gridStockOrder_ModelAsc',
171 bind: $.proxy(_w.stock.orderStockGrid, this, 'model.name', 'ASC')
174 id: 'gridStockOrder_ModelDesc',
175 bind: $.proxy(_w.stock.orderStockGrid, this, 'model.name', 'DESC')
177 style: 'hidden-phone'
180 id: 'gridStockColumn_Type',
183 id: 'gridStockOrder_TypeAsc',
184 bind: $.proxy(_w.stock.orderStockGrid, this, 'type.name', 'ASC')
187 id: 'gridStockOrder_TypeDesc',
188 bind: $.proxy(_w.stock.orderStockGrid, this, 'type.name', 'DESC')
193 id: 'gridStockColumn_Year',
196 id: 'gridStockOrder_YearAsc',
197 bind: $.proxy(_w.stock.orderStockGrid, this, 'vehicleYear.name', 'ASC')
200 id: 'gridStockOrder_YearDesc',
201 bind: $.proxy(_w.stock.orderStockGrid, this, 'vehicleYear.name', 'DESC')
206 id: 'gridStockColumn_KM',
209 id: 'gridStockOrder_KmAsc',
210 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.km', 'ASC')
213 id: 'gridStockOrder_KmDesc',
214 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.km', 'DESC')
216 style: 'kms hidden-phone'
219 id: 'gridStockColumn_Trade',
222 id: 'gridStockOrder_TradeAsc',
223 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.tradePrice', 'ASC')
226 id: 'gridStockOrder_TradeDesc',
227 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.tradePrice', 'DESC')
229 style: 'hidden-phone'
232 id: 'gridStockColumn_Retail',
235 id: 'gridStockOrder_RetailAsc',
236 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.retailPrice', 'ASC')
239 id: 'gridStockOrder_RetailDesc',
240 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.retailPrice', 'DESC')
242 style: 'hidden-phone'
245 id: 'gridStockColumn_Offer',
248 id: 'gridStockOrder_OfferAsc',
249 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.highestOffer', 'ASC')
252 id: 'gridStockOrder_OfferDesc',
253 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.highestOffer', 'DESC')
255 style: 'hidden-phone'
258 id: 'gridStockColumn_Bid',
261 id: 'gridStockOrder_BidAsc',
262 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.highestBid', 'ASC')
265 id: 'gridStockOrder_BidDesc',
266 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.highestBid', 'DESC')
268 style: 'hidden-phone'
271 id: 'gridStockColumn_Status',
274 id: 'gridStockOrder_JobstateAsc',
275 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.jobState', 'ASC')
278 id: 'gridStockOrder_JobstateDesc',
279 bind: $.proxy(_w.stock.orderStockGrid, this, 'stock.jobState', 'DESC')
284 'gridColumnFilters': {
285 constructor: 'GridColumnFilter',
288 id: 'gridStockFilter_VIN',
289 context: 'gridStockFilter'
292 id: 'gridStockFilter_Make',
293 context: 'gridStockFilter',
294 dataStoreId : 'filtermakes',
296 bind : function(meta){
297 _w.stock.changes = 0;
298 $('#' + meta.id).change(function(){
300 if (1 == _w.stock.changes || 'Active' != _w.stock.gridStatus)
304 _w.stock.searchStockGrid();
309 id: 'gridStockFilter_Model',
310 context: 'gridStockFilter'
313 id: 'gridStockFilter_Type',
314 context: 'gridStockFilter'
317 id: 'gridStockFilter_Year',
318 context: 'gridStockFilter'
321 id: 'gridStockFilter_KM',
322 context: 'gridStockFilter'
325 id: 'gridStockFilter_Trade',
326 context: 'gridStockFilter'
329 id: 'gridStockFilter_Retail',
330 context: 'gridStockFilter'
333 id: 'gridStockFilter_Offer',
334 context: 'gridStockFilter'
337 id: 'gridStockFilter_Bid',
338 context: 'gridStockFilter'
341 id: 'gridStockFilter_Status',
342 context: 'gridStockFilter',
343 dataStoreId : 'stockstatuses',
345 bind : function(meta){
346 _w.stock.changes = 0;
347 $('#' + meta.id).change(function(){
349 if (1 == _w.stock.changes || 'Active' != _w.stock.gridStatus)
353 _w.stock.searchStockGrid();
359 'gridRowRepeater': {},
363 // Retrieve initial grid data.
364 if (null == App.API.taskContract('gridStock'))
367 'gridStock', 'Stock', 'Stock.Grid', null,
368 {}, _w.stock.loadStockGrid, _w.taskContractError
373 _w.stock.loadStockGrid( null, {}, {} );
377 onStockPublished : function()
380 $(".gridStockFilter").keyup(function (evt) {
381 var charCode = evt.charCode || evt.keyCode;
382 if (charCode == 13) { //Enter key's keycode
383 $('#searchStockGrid').click();
387 $(".gridStockContext").keyup(function (evt) {
388 var charCode = evt.charCode || evt.keyCode;
389 if (charCode == 13) { //Enter key's keycode
390 $('#searchStockGrid').click();
394 var _p = App.permissions;
395 var _createStockDisabled = false == _p.stockCreateNew && false == _p.stockCreateAuction
397 $('#newStock').prop('disabled', _createStockDisabled);
398 $('#exportStockGrid').prop('disabled', !App.permissions.stockGridExport);
400 $('#bulkImporterStockGrid').prop('disabled', !App.permissions.stockBulkImportDrive || !App.permissions.stockBulkImportPinnacle);
401 $('#bulkImporterStockGrid').hide();
402 if(App.permissions.stockBulkImportDrive || App.permissions.stockBulkImportPinnacle)
404 $('#bulkImporterStockGrid').show();
407 $('#auctionReportStockGrid').prop('disabled', !App.permissions.stockAuctionReportGridExport);
408 $('#auctionReportStockGrid').hide();
409 if(App.permissions.stockAuctionReportGridExport)
411 $('#auctionReportStockGrid').show();
414 App.Event.listen('stockGridSuFilter', 'SuChange', $.proxy(_w.stock.loadStockGrid, this), 'Recurring');
415 $('.dfrom').datepicker({format:"yyyy-mm-dd"});
416 $('.dto').datepicker({format:"yyyy-mm-dd"});
417 $('#gridStockContext_DateFrom').val(_w.stock.dFrom);
418 $('#gridStockContext_DateTo').val(_w.stock.dTo);
420 //setTimeout(_w.stock.refreshGrid, 120000);
423 refreshGrid : function()
426 if ('stock' != App.activePage)
430 _w.stock.loadStockGrid( null, {}, {} );
431 setTimeout(_w.stock.refreshGrid, 120000);
434 pageStockGrid : function( page )
436 _w.stock.loadStockGrid( {}, {"Grid":{"Page":page}} );
439 loadStockGrid : function( contract, data, options )
442 'gridStock', data, options,
443 _w.stock._onStockGridDataReceived, _w.taskExecError
447 _onStockGridDataReceived : function( response )
449 _w.stock.gridStatus = 'Active';
450 _w.stock.ti.hydrateParam('gridRowRepeater', {});
451 if (undefined != response.Data.Meta.Filters['auction.jobState'])
453 _w.populateFilters(response.Data.Meta.Filters, {
454 'stock.registrationNumber': 'gridStockContext_RegNo',
455 'stock.referenceNumber': 'gridStockContext_RefNo',
456 'stock.vinNumber': 'gridStockFilter_VIN',
457 'make.id': 'gridStockFilter_Make',
458 'model.name': 'gridStockFilter_Model',
459 'type.name': 'gridStockFilter_Type',
460 'vehicleYear.name': 'gridStockFilter_Year',
461 'stock.km': 'gridStockFilter_KM',
462 'stock.tradePrice': 'gridStockFilter_Trade',
463 'stock.retailPrice': 'gridStockFilter_Retail',
464 'stock.highestOffer': 'gridStockFilter_Offer',
465 'stock.highestBid': 'gridStockFilter_Bid',
466 'auction.jobState': 'gridStockFilter_Status'
471 _w.populateFilters(response.Data.Meta.Filters, {
472 'stock.registrationNumber': 'gridStockContext_RegNo',
473 'stock.referenceNumber': 'gridStockContext_RefNo',
474 'stock.vinNumber': 'gridStockFilter_VIN',
475 'make.id': 'gridStockFilter_Make',
476 'model.name': 'gridStockFilter_Model',
477 'type.name': 'gridStockFilter_Type',
478 'vehicleYear.name': 'gridStockFilter_Year',
479 'stock.km': 'gridStockFilter_KM',
480 'stock.tradePrice': 'gridStockFilter_Trade',
481 'stock.retailPrice': 'gridStockFilter_Retail',
482 'stock.highestOffer': 'gridStockFilter_Offer',
483 'stock.highestBid': 'gridStockFilter_Bid',
484 'stock.jobState': 'gridStockFilter_Status'
487 $('select.gridStockFilter').each(function(i, elem) {
488 $(elem).selectpicker('refresh');
491 for (var i = 0; i < response.Data.DataSet.length; i++)
493 var row = response.Data.DataSet[i];
495 //console.log('row',row);
499 surl: ('Stock' == row.jobState || ('Auction' == row.jobState && "0.00" == row.highestBid) || 'Relist' == row.jobState)
501 ? 'stockview' : 'limitedstockview',
508 pgId: row.priceGuide ? row.priceGuide.id : false,
509 offers: row.priceGuide && row.priceGuide.offers
510 ? row.priceGuide.offers[0]
512 bind: function(meta) {
513 $('#row' + meta.rowId).click(function(e){
514 window.location='#/' + meta.surl + '?id=' + meta.rowId;
517 $('#ofr' + meta.rowId).click(function(e) {
523 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
524 $('#modalbody').html(modalbody);
529 $('#modalbody').html('<center>Loading offers...</center>');
530 $('#myModal').modal('toggle');
533 'offerList', 'PriceGuide', 'Offer.List',
534 meta.pgId, {}, function(){
539 var offers = response.Data;
542 var modalbody = '<table class="table table-bordered table-condensed table-striped">';
543 modalbody += '<tr><td>Company</td><td>Date Offered</td><td>Amount</td></tr>';
544 for (var x in offers)
546 var isOwn = offers[x].company.id == App.companyData.id
550 '<tr class="modalrow ' + (isOwn ? 'red' : 'black') + '">'
551 + '<td class="date">' + offers[x].company.name + '</td>'
552 + '<td class="date">' + offers[x].created + '</td>'
553 + '<td class="wide">' + offers[x].amount + '</td>'
556 modalbody += '</table>';
560 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
562 $('#modalbody').html(modalbody);
565 }, _w.taskContractError
570 VIN : 'hidden-phone',
572 Model : 'hidden-phone',
576 Trade : 'hidden-phone',
577 Retail : 'hidden-phone',
578 Offer : 'hidden-phone offer',
579 Bid : 'hidden-phone',
585 'VIN' : row.vinNumber,
586 'Make' : row.type.model.make.name,
587 'Model' : row.type.model.name,
588 'Type' : row.type.name,
589 'Year' : row.vehicleYear.name,
591 'Trade' : row.tradePrice,
592 'Retail' : row.retailPrice,
593 'Offer' : row.highestOffer + ' <span id="ofr' + row.id + '">' + row.numberOfOffers + '</span>',
594 'Bid' : row.highestBid,
595 'Status' : ('Stock' == row.jobState || 'Trade Center' == row.jobState) && row.auction && (row.auction.jobState == 'Undone' || row.auction.jobState == 'Relist') ? 'Relist' : row.jobState
596 //'Status' : row.auction.jobState == 'Undone' || row.auction.jobState == 'Relist' ? 'Relist' : row.jobState // Made this the same as portal-workspace/tcstock.js
600 _w.stock.ti.hydrateParam('gridRowRepeater', {
601 constructor : 'GridDataRow',
604 response.Data.Meta.itemName = 'stockPager';
605 response.Data.Meta.bind = _w.stock.pageStockGrid;
606 _w.stock.ti.hydrateParam('gridPager', {
607 constructor : 'GridPager',
608 items : [response.Data.Meta]
612 exportStockGrid : function()
615 'gridStock', {}, {'ExportToExcel': true},
616 _w.stock._onExportReceived, _w.taskExecError,
620 bulkImporterStockGrid : function()
622 window.location.hash = '/stockbulkimporter';
624 auctionReportStockGrid : function()
626 window.location.hash = '/stockauctionreport';
629 newStock : function()
631 window.location.hash = '/stockview?id=0';
634 _onExportReceived : function( response )
636 var win = window.open();
637 win.document.write(response);
640 clearStockGridContext : function()
642 _w.stock.gridStatus = 'Inactive';
645 $('select.gridStockFilter').each(function(i, elem) {
646 $(elem).selectpicker('val', '');
647 $(elem).selectpicker('refresh');
650 catch (err) { console.log(err); }
651 $('.gridStockContext').val('');
652 $('.gridStockFilter').val('');
653 _w.stock.searchStockGrid(true);
656 searchStockGrid : function(resetOrder)
658 _w.stock.gridStatus = 'Inactive';
663 filter = _w.filterIfnotEmpty(filter, 'gridStockContext_RegNo', 'stock.registrationNumber');
664 filter = _w.filterIfnotEmpty(filter, 'gridStockContext_RefNo', 'stock.referenceNumber');
665 filter = _w.filterIfnotEmpty(filter, 'gridStockContext_DateFrom', 'stock.created', '>=');
666 filter = _w.filterIfnotEmpty(filter, 'gridStockContext_DateTo', 'stock.created', '<=', ' 23:59:59');
668 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_VIN', 'stock.vinNumber');
669 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Make', 'make.id');
670 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Model', 'model.name');
671 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Type', 'type.name');
672 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Year', 'vehicleYear.name');
673 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_KM', 'stock.km');
674 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Trade', 'stock.tradePrice');
675 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Retail', 'stock.retailPrice');
676 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Offer', 'stock.highestOffer');
677 var status = $('#gridStockFilter_Status').val();
678 if ('Undone' == status || 'Relist' == status)
680 $('#gridValuationContext_notArchived').val('Relist,Undone');
681 filter = _w.filterIfnotEmpty(filter, 'gridStockContext_RelistFilter', 'auction.jobState', 'IN');
685 filter = _w.filterIfnotEmpty(filter, 'gridStockFilter_Status', 'stock.jobState');
687 var request = {"Grid":{
689 "Filter": filter.filters
693 request.Grid.OrderBy = {'stock.created': 'DESC'};
695 _w.stock.loadStockGrid( null, request, {} );
698 orderStockGrid : function(field, direction)
701 order[field] = direction;
702 _w.stock.loadStockGrid( {}, {"Grid":{"Page": 1, "OrderBy": order}} );