initial commit
[namibia] / public / js / app / portal-workspace / priceguide.js
1 ;(function(){
2
3         window._w.priceguide = {
4
5                 ti : null,
6                 dfrom : '',
7                 dto   : '',
8                 gridStatus : null,
9
10                 onPageDestruct : function()
11                 {
12                         App.Event.removeListener('priceguideGridSuFilter', 'SuChange');
13                 },
14
15                 onPriceGuidePageTemplateReady : function( template, event, eventData )
16                 {
17                         App.DataStore.loadSelectListData('filtermakes', true, 'Stock', 'Make.SelectList');
18                         App.DataStore.loadSelectListData('regions', true, 'Location', 'Region.SelectList');
19                         _w.priceguide.ti = _t[template];
20                         App.DataStore.setData('pgstatuses',
21                                         [
22                                          {"value":"Updateable Offer", "label":"Updateable Offer"},
23                                          {"value":"Valid Offer", "label":"Valid Offer"},
24                                          {"value":"Offer Accepted", "label":"Offer Accepted"},
25                                          {"value":"Expired", "label":"Expired"},
26                                          {"value":"Archived", "label":"Archived"}
27                                          ]
28                         );
29                         _w.priceguide.ti.hydrate({
30                                 'gridTitle': 'Price Guide',
31                                 'gridTitleSubtext': 'Please use the filters below to narrow down the view',
32                                 'gridTitleButtons': {
33                                         constructor: 'Button',
34                                         items: {
35                                                 Export: {
36                                                         preset: 'GridExport',
37                                                         id: 'exportPriceGuideGrid',
38                                                         handler: _w.priceguide.exportPriceGuideGrid
39                                                 }
40                                         }
41                                 },
42                                 'gridContextFilter': {
43                                         constructor: 'GridContextFilter',
44                                         items: {
45                                                 RefNum: {
46                                                         title: 'Search here:',
47                                                         style: 'filter-input-wrapper hidden-tablet hidden-phone',
48                                                         id: 'gridPriceGuideContext_RefNo',
49                                                         placeholder: 'Ref number',
50                                                         context: 'gridPriceGuideContext'
51                                                 },
52                                                 RegNum: {
53                                                         style: 'filter-input-wrapper hidden-tablet hidden-phone',
54                                                         id: 'gridPriceGuideContext_RegNo',
55                                                         placeholder: 'Registration number',
56                                                         context: 'gridPriceGuideContext'
57                                                 },
58                                                 DateFrom: {
59                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dfrom hidden-tablet hidden-phone',
60                                                         title: 'Search date created here',
61                                                         id: 'gridPriceGuideContext_DateFrom',
62                                                         placeholder: 'From',
63                                                         context: 'gridPriceGuideContext',
64                                                         append: '<i class="icon-calendar"></i>',
65                                                         readonly: true,
66                                                         bind: function() {
67                                                                 $('#gridPriceGuideContext_DateFrom').change(function() {
68                                                                         _w.priceguide.dFrom = $(this).val();
69                                                                 });
70                                                         }
71                                                 },
72                                                 DateTo: {
73                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dto hidden-tablet hidden-phone',
74                                                         id: 'gridPriceGuideContext_DateTo',
75                                                         placeholder: 'To',
76                                                         context: 'gridPriceGuideContext',
77                                                         append: '<i class="icon-calendar"></i>',
78                                                         readonly: true,
79                                                         bind: function() {
80                                                                 $('#gridPriceGuideContext_DateTo').change(function() {
81                                                                         _w.priceguide.dTo = $(this).val();
82                                                                 });
83                                                         }
84                                                 },
85                                                 RegNumMobi: {
86                                                         style: 'filter-input-wrapper hidden-desktop',
87                                                         id: 'gridPriceGuideContext_RegNoMobi',
88                                                         placeholder: 'Registration number',
89                                                         context: 'gridPriceGuideContext'
90                                                 },
91                                                 
92                                                 MakeMobi: {
93                                                         style: 'filter-input-wrapper filter-input-select',
94                                                         placeholder: 'Make Mobi',
95                                                         id: 'gridPriceGuideContext_MakeMobi',
96                                                         context: 'gridPriceGuideContext',
97                                                         dataStoreId : 'filtermakes',
98                                                         selectEmpty : 'All Makes',
99                                                         style: 'MakeMobi hidden-desktop ',
100                                                         bind : function(meta){
101                                                                 $('#gridAuctionFilter_MakeMobi').change(function(){
102                                                                         $('#gridAuctionFilter_Make').selectpicker('val', $(this).val());
103                                                                 });
104                                                         }
105                                                 },
106                                                 RegionMobi: {
107                                                         style: 'filter-input-wrapper filter-input-select ',
108                                                         placeholder: 'Region Mobi',
109                                                         id: 'gridPriceGuideContext_RegionMobi',
110                                                         context: 'gridPriceGuideContext',
111                                                         dataStoreId : 'regions',
112                                                         selectEmpty : 'Regions',
113                                                         style: 'RegionMobi hidden-desktop ',
114                                                         bind : function(meta){
115                                                                 $('#gridAuctionFilter_RegionMobi').change(function(){
116                                                                         $('#gridAuctionFilter_Region').selectpicker('val', $(this).val());
117                                                                 });
118                                                         }
119                                                 },
120                                                 
121                                                 Buttons: {
122                                                         actions                 :true,
123                                                         searchId                :'btnSearch',
124                                                         bindSearch              :_w.priceguide.searchPriceGuideGrid,
125                                                         clearId                 :'btnClearSearch',
126                                                         bindClear               :_w.priceguide.clearPriceGuideGridContext
127
128                                                 }
129                                         }
130                                 },
131
132                                 'gridColumnHeaders': {
133                                         constructor: 'GridColumnHeader',
134                                         items: {
135                                                 Make: {
136                                                         id: 'gridPriceGuideColumn_Make',
137                                                         title: 'MAKE',
138                                                         orderAsc: {
139                                                                 id: 'gridPriceGuideOrder_MakeAsc',
140                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'make.name', 'ASC')
141                                                         },
142                                                         orderDesc: {
143                                                                 id: 'gridPriceGuideOrder_MakeDesc',
144                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'make.name', 'DESC')
145                                                         },
146                                                         style: ' hidden-phone'
147                                                 },
148                                                 Type: {
149                                                         id: 'gridPriceGuideColumn_Type',
150                                                         title: 'TYPE',
151                                                         orderAsc: {
152                                                                 id: 'gridPriceGuideOrder_TypeAsc',
153                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'type.name', 'ASC')
154                                                         },
155                                                         orderDesc: {
156                                                                 id: 'gridPriceGuideOrder_TypeDesc',
157                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'type.name', 'DESC')
158                                                         },
159                                                         style: 'type'
160                                                 },
161                                                 Year: {
162                                                         id: 'gridPriceGuideColumn_Year',
163                                                         title: 'YEAR',
164                                                         orderAsc: {
165                                                                 id: 'gridPriceGuideOrder_YearAsc',
166                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'vehicleYear.name', 'ASC')
167                                                         },
168                                                         orderDesc: {
169                                                                 id: 'gridPriceGuideOrder_YearDesc',
170                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'vehicleYear.name', 'DESC')
171                                                         },
172                                                         style: 'year'
173                                                 },
174                                                 KM: {
175                                                         id: 'gridPriceGuideColumn_KM',
176                                                         title: 'KM&#39;S',
177                                                         orderAsc: {
178                                                                 id: 'gridPriceGuideOrder_KMAsc',
179                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.km', 'ASC')
180                                                         },
181                                                         orderDesc: {
182                                                                 id: 'gridPriceGuideOrder_KMDesc',
183                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.km', 'DESC')
184                                                         },
185                                                         style: 'kms hidden-phone'
186                                                 },
187                                                 Trade: {
188                                                         id: 'gridPriceGuideColumn_Trade',
189                                                         title: 'TRADE',
190                                                         orderAsc: {
191                                                                 id: 'gridPriceGuideOrder_TradeAsc',
192                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.tradePrice', 'ASC')
193                                                         },
194                                                         orderDesc: {
195                                                                 id: 'gridPriceGuideOrder_TradeDesc',
196                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.tradePrice', 'DESC')
197                                                         },
198                                                         style: 'hidden-phone'
199                                                 },
200                         List: {
201                             id: 'gridPriceGuideColumn_List',
202                             title: 'NEW LIST PRICE',
203                             orderAsc: {
204                                 id: 'gridPriceGuideOrder_ListAsc',
205                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.listPrice', 'ASC')
206                             },
207                             orderDesc: {
208                                 id: 'gridPriceGuideOrder_ListDesc',
209                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.listPrice', 'DESC')
210                             },
211                             style: 'hidden-phone'
212                         },
213                                                 Company: {
214                                                         id: 'gridPriceGuideColumn_Retail',
215                                                         title: 'DEALERSHIP',
216                                                         orderAsc: {
217                                                                 id: 'gridPriceGuideOrder_CompanyAsc',
218                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'company.name', 'ASC')
219                                                         },
220                                                         orderDesc: {
221                                                                 id: 'gridPriceGuideOrder_CompanyDesc',
222                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'company.name', 'DESC')
223                                                         },
224                                                         style: 'hidden-phone'
225                                                 },
226                                                 Region: {
227                                                         id: 'gridPriceGuideColumn_Retail',
228                                                         title: 'REGION',
229                                                         orderAsc: {
230                                                                 id: 'gridPriceGuideOrder_RegionAsc',
231                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'region.name', 'ASC')
232                                                         },
233                                                         orderDesc: {
234                                                                 id: 'gridPriceGuideOrder_RegionDesc',
235                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'region.name', 'DESC')
236                                                         },
237                                                         style: 'hidden-phone'
238                                                 },
239                                                 CustomerRequest: {
240                                                         id: 'gridPriceGuideColumn_Request',
241                                                         title: 'CUSTOMER REQUEST',
242                                                         orderAsc: {
243                                                                 id: 'gridPriceGuideOrder_RequestAsc',
244                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'valuation.requiredPrice', 'ASC')
245                                                         },
246                                                         orderDesc: {
247                                                                 id: 'gridPriceGuideOrder_RequestDesc',
248                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'valuation.requiredPrice', 'DESC')
249                                                         },
250                                                         style: 'hidden-phone'
251                                                 },
252                                                 Offer: {
253                                                         id: 'gridPriceGuideColumn_Offer',
254                                                         title: 'MY OFFER',
255                                                         orderAsc: {
256                                                                 id: 'gridPriceGuideOrder_OfferAsc',
257                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'offer.amount', 'ASC')
258                                                         },
259                                                         orderDesc: {
260                                                                 id: 'gridPriceGuideOrder_OfferDesc',
261                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'offer.amount', 'DESC')
262                                                         },
263                                                         style: 'offer-submitted'
264                                                 },
265                                                 HighestOffer: {
266                                                         id: 'gridPriceGuideColumn_HighestOffer',
267                                                         title: 'HIGHEST OFFER',
268                                                         orderAsc: {
269                                                                 id: 'gridPriceGuideOrder_HighestOfferAsc',
270                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.highestOffer', 'ASC')
271                                                         },
272                                                         orderDesc: {
273                                                                 id: 'gridPriceGuideOrder_HighestOfferDesc',
274                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'stock.highestOffer', 'DESC')
275                                                         },
276                                                         style: 'offer-submitted hidden-phone'
277                                                 },
278                                                 Time: {
279                                                         id: 'gridPriceGuideColumn_Time',
280                                                         title: 'TIME LEFT',
281                                                         style: 'Time '
282                                                 },
283                                                 
284                                                 Status: {
285                                                         id: 'gridPriceGuideColumn_Status',
286                                                         title: 'STATUS',
287                                                         orderAsc: {
288                                                                 id: 'gridPriceGuideOrder_StatusAsc',
289                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'offer.status', 'ASC')
290                                                         },
291                                                         orderDesc: {
292                                                                 id: 'gridPriceGuideOrder_StatusDesc',
293                                                                 bind: $.proxy(_w.priceguide.orderPriceGuideGrid, this, 'offer.status', 'DESC')
294                                                         },
295                                                         style: ' hidden-phone'
296
297                                                 },
298                                         }
299                                 },
300                                 'gridColumnFilters': {
301                                         constructor: 'GridColumnFilter',
302                                         items: {
303                                                 Make: {
304                                                         id: 'gridPriceGuideFilter_Make',
305                                                         context: 'gridPriceGuideFilter',
306                                                         dataStoreId : 'filtermakes',
307                                                         selectEmpty : 'All',
308                                                         bind : function(meta){
309                                                                 _w.priceguide.changes = 0;
310                                                                 $('#' + meta.id).change(function(){
311                                                                         _w.priceguide.changes++;
312                                                                         if (1 == _w.priceguide.changes || 'Active' != _w.priceguide.gridStatus)
313                                                                         {
314                                                                                 return;
315                                                                         }
316                                                                         _w.priceguide.searchPriceGuideGrid();
317                                                                 });
318                                                         }
319                                                 },
320                                                 Type: {
321                                                         id: 'gridPriceGuideFilter_Type',
322                                                         context: 'gridPriceGuideFilter'
323                                                 },
324                                                 Year: {
325                                                         id: 'gridPriceGuideFilter_Year',
326                                                         context: 'gridPriceGuideFilter'
327                                                 },
328                                                 KM: {
329                                                         id: 'gridPriceGuideFilter_KM',
330                                                         context: 'gridPriceGuideFilter'
331                                                 },
332                                                 Trade: {
333                                                         id: 'gridPriceGuideFilter_Trade',
334                                                         context: 'gridPriceGuideFilter'
335                                                 },
336
337                                                 Company: {
338                                                         id: 'gridPriceGuideFilter_Company',
339                                                         context: 'gridPriceGuideFilter'
340                                                 },
341                                                 Region: {
342                                                         id: 'gridPriceGuideFilter_Region',
343                                                         context: 'gridPriceGuideFilter',
344                                                         dataStoreId : 'regions',
345                                                         selectEmpty : 'All',
346                                                         bind : function(meta){
347                                                                 _w.priceguide.changes = 0;
348                                                                 $('#' + meta.id).change(function(){
349                                                                         _w.priceguide.changes++;
350                                                                         if (1 == _w.priceguide.changes || 'Active' != _w.priceguide.gridStatus)
351                                                                         {
352                                                                                 return;
353                                                                         }
354                                                                         _w.priceguide.searchPriceGuideGrid();
355                                                                 });
356                                                         }
357                                                 },
358
359                                                 CustomerRequest: {
360                                                         id: 'gridPriceGuideFilter_Request',
361                                                         context: 'gridPriceGuideFilter'
362                                                 },
363                                                 Offer: {
364                                                         id: 'gridPriceGuideFilter_Offer',
365                                                         context: 'gridPriceGuideFilter'
366                                                 },
367                                                 HighestOffer: {
368                                                         id: 'gridPriceGuideFilter_HighestOffer',
369                                                         context: 'gridPriceGuideFilter'
370                                                 },
371                                                 Time: {},
372                                                 Status: {
373                                                         id: 'gridPriceGuideFilter_Status',
374                                                         context: 'gridPriceGuideFilter',
375                                                         dataStoreId : 'pgstatuses',
376                                                         selectEmpty : 'All',
377                                                         bind : function(meta){
378                                                                 _w.priceguide.changes = 0;
379                                                                 $('#' + meta.id).change(function(){
380                                                                         _w.priceguide.changes++;
381                                                                         if (1 == _w.priceguide.changes || 'Active' != _w.priceguide.gridStatus)
382                                                                         {
383                                                                                 return;
384                                                                         }
385                                                                         _w.priceguide.searchPriceGuideGrid();
386                                                                 });
387                                                         }
388                                                 },
389                                         }
390                                 },
391                                 'gridRowRepeater': {},
392                                 'gridPager': {}
393                         });
394
395                         // Retrieve initial grid data.
396                         if (null == App.API.taskContract('gridPriceGuide'))
397                         {
398                                 App.API.getTask(
399                                                 'gridPriceGuide', 'PriceGuide', 'PriceGuide.Grid', null,
400                                                 {}, _w.priceguide.loadPriceGuideGrid, _w.taskContractError
401                                                 );
402                         }
403                         else
404                         {
405                                 _w.priceguide.loadPriceGuideGrid( null, {}, {} );
406                         }
407                 },
408
409                 onPriceGuideGridPublished : function()
410                 {
411                         
412                         //mobi menu
413                         
414                         var isMobile = 'none' == $('#mobiCheck').css('display');
415                         
416                         console.log("isMobile", isMobile);
417                         
418                         
419                         //var isMobile = 'none' == $('.header').css('display');
420                         if (isMobile)
421                         {
422                                 // show the mobi menu && hide old menu 
423                                 $("#MobiAuctionPriceGuideMenu").show();
424                                 $("#MobiPortalMenu").hide();
425                                  
426                                 // Check active state
427                                 $("#mobiAuctionBtn").removeClass("active");
428                                 $("#mobiPriceGuideBtn").addClass("active");
429                                 $("#mobiValuationsBtn").removeClass("active");
430                         }
431                         
432                         $("#mobiAuctionBtn").click(function() {
433                                 window.location.hash = '#/auction';
434                         });
435                         
436                         $("#mobiValuationsBtn").click(function() {
437                                 window.location.hash = '#/valuations';
438                         });
439                         
440                         
441                         $(".gridPriceGuideFilter").keyup(function (evt) {
442                                 var charCode = evt.charCode || evt.keyCode;
443                                 if (charCode  == 13) { //Enter key's keycode
444                                         $('#btnSearch').click();
445                                 }
446                         });
447                         $(".gridPriceGuideContext").keyup(function (evt) {
448                                 var charCode = evt.charCode || evt.keyCode;
449                                 if (charCode  == 13) { //Enter key's keycode
450                                         $('#btnSearch').click();
451                                 }
452                         });
453
454
455                         $('#exportPriceGuideGrid').prop('disabled', !App.permissions.pgGridExport);
456
457                         var setTableFormWidth = function()
458                                 {
459                                         $('.table-form').css({
460                                                 width : $(window).width() * 0.96
461                                         });
462                                 };
463
464                         $(window).on('resize', setTableFormWidth );
465                         setTableFormWidth();
466
467                         App.Event.listen('priceguideGridSuFilter', 'SuChange', $.proxy(_w.priceguide.loadPriceGuideGrid, this), 'Recurring');
468
469                         setTimeout(_w.priceguide.refreshGrid, 120000);
470                         $('.dfrom').datepicker({format:"yyyy-mm-dd"});
471                         $('.dto').datepicker({format:"yyyy-mm-dd"});
472                         $('#gridPriceGuideContext_DateFrom').val(_w.priceguide.dFrom);
473                         $('#gridPriceGuideContext_DateTo').val(_w.priceguide.dTo);
474                 },
475
476                 refreshGrid : function()
477                 {
478                         if ('priceguide' != App.activePage)
479                         {
480                                 return;
481                         }
482                         _w.priceguide.loadPriceGuideGrid( null, {}, {} );
483                         setTimeout(_w.priceguide.refreshGrid, 120000);
484                 },
485
486                 pagePriceGuideGrid : function( page )
487                 {
488                         _w.priceguide.loadPriceGuideGrid( {}, {"Grid":{"Page":page}} );
489                 },
490
491                 loadPriceGuideGrid : function( contract, data, options )
492                 {
493                         App.API.execTask(
494                                         'gridPriceGuide', data, options,
495                                         _w.priceguide._onPriceGuideGridDataReceived, _w.taskExecError
496                                         );
497                 },
498
499                 _onPriceGuideGridDataReceived : function( response )
500                 {
501                         _w.priceguide.gridStatus = 'Active';
502                         _w.priceguide.ti.hydrateParam('gridRowRepeater', {});
503                         _w.populateFilters(response.Data.Meta.Filters, {
504                                 'stock.registrationNumber': 'gridPriceGuideContext_RegNo',
505                                 'stock.referenceNumber': 'gridPriceGuideContext_RefNo',
506                                 'make.id': 'gridPriceGuideFilter_Make',
507                                 'type.name': 'gridPriceGuideFilter_Type',
508                                 'vehicleYear.name': 'gridPriceGuideFilter_Year',
509                                 'stock.km': 'gridPriceGuideFilter_KM',
510                                 'stock.tradePrice': 'gridPriceGuideFilter_Trade',
511                 'stock.listPrice': 'gridPriceGuideFilter_List',
512                                 'company.name': 'gridPriceGuideFilter_Company',
513                                 'region.id': 'gridPriceGuideFilter_Region',
514                                 'valuation.requiredPrice': 'gridPriceGuideFilter_Request',
515                                 'offer.amount': 'gridPriceGuideFilter_Offer',
516                                 'stock.highestOffer': 'gridPriceGuideFilter_HighestOffer',
517                                 'offer.status': 'gridPriceGuideFilter_Status'
518                         });
519                         $('select.gridPriceGuideFilter').each(function(i, elem) {
520                                 $(elem).selectpicker('refresh');
521                         });
522                         var gridData = [];
523                         for (var i = 0; i < response.Data.DataSet.length; i++)
524                         {
525                                 var row = response.Data.DataSet[i];
526                                 var myOffer = false;
527                                 if (row.offers[0])
528                                 {
529                                         for (var z in row.offers[0])
530                                         {
531                                                 if (row.offers[0][z].company.id == App.companyData.id)
532                                                 {
533                                                         myOffer = row.offers[0][z];
534                                                 }
535                                         }
536                                 }
537                                 if ('Open4Offers' != row.jobState && 'Updateable Offers' != row.jobState)
538                                 {
539                                         var numDays = App.serverConfig.priceGuideCompletionDays;
540                                         var hoursLeft = App.Util.calculateTimeLeft(row.created, numDays);
541                                 }
542                                 else
543                                 {
544                                         var numHours = App.serverConfig.priceGuideOpenDays;
545                                         var hoursLeft = App.Util.calculateTimeLeftHours(row.created, numHours);
546                                 }
547
548                                 gridData.push({
549                                         lineClass: 'handy',
550                                         surl: myOffer
551                                                 ? 'offerview'
552                                                 : 'offerview',
553                                         prepend: {
554                                                 Trade  : 'R ',
555                                                 CustomerRequest  : 'R ',
556                                                 Offer  : 'R ',
557                                                 HighestOffer  : 'R '
558                                         },
559                                         rowId: row.id,
560                                         myOffer: myOffer,
561                                         jobState: row.jobState,
562                                         bind: function(meta) {
563                                                 $('#row' +  meta.rowId).click(function(e){
564                                                         window.location='#/' + meta.surl + '?pgId=' + meta.rowId + '&id=' + (meta.myOffer ? meta.myOffer.id : 0);
565                                                 });
566                                                 if ('Open4Offers' == meta.jobState || 'Updateable Offers' == meta.jobState)
567                                                 {
568                                                         $('#ofr' + meta.rowId).click(function(e) {
569                                                                 _w.PopUp = true;
570                                                                 e.preventDefault();
571                                                                 e.stopPropagation();
572                                                         });
573                                                 }
574                                                 else
575                                                 {
576                                                         $('#ofr' + meta.rowId).click(function(e) {
577                                                                 _w.PopUp = true;
578                                                                 e.preventDefault();
579                                                                 e.stopPropagation();
580                                                                 $('#modalbody').html('<center>Loading offers...</center>');
581                                                                 $('#myModal').modal('toggle');
582                                                                 App.API.getTask(
583                                                                                 'offerList', 'PriceGuide', 'Offer.List',
584                                                                                 meta.rowId, {}, function(){
585                                                                                         App.API.execTask(
586                                                                                                 'offerList', {}, {},
587                                                                                                 function(response)
588                                                                                                 {
589                                                                                                         console.log("meta.rowId", meta.rowId);
590                                                                                                         var offers = response.Data;
591                                                                                                         if (offers.length)
592                                                                                                         {
593                                                                                                                 var modalbody = '<table class="table table-bordered table-condensed table-striped">';
594                                                                                                                 modalbody += '<tr><td>Date Offered</td><td>Amount</td></tr>';
595                                                                                                                 for (var x in offers)
596                                                                                                                 {
597                                                                                                                         var isOwn = offers[x].company.id == App.companyData.id
598                                                                                                                                 ? true
599                                                                                                                                 : false;
600                                                                                                                         modalbody +=
601                                                                                                                                 '<tr class="modalrow ' + (isOwn ? 'red' : 'black') + '">'
602                                                                                                                                 + '<td class="date">' + offers[x].created + '</td>'
603                                                                                                                                 + '<td class="wide">R' + offers[x].amount + '</td>'
604                                                                                                                                 + '</tr>';
605                                                                                                                 }
606                                                                                                                 modalbody += '</table>';
607                                                                                                         }
608                                                                                                         else
609                                                                                                         {
610                                                                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
611                                                                                                         }
612                                                                                                         $('#modalbody').html(modalbody);
613                                                                                                 }, _w.taskExecError
614                                                                                         );
615                                                                                 }, _w.taskContractError
616                                                                 );
617                                                         });
618                                                 }
619
620                                         },
621                                         style: {
622
623                                                 Make            : 'make hidden-phone',
624                                                 Type            : 'type',
625                                                 Year            : 'year',
626                                                 KM              : 'hidden-phone',
627                                                 Trade           : 'hidden-phone',
628                                                 Company         : 'hidden-phone',
629                                                 Region          : 'hidden-phone',
630                                                 CustomerRequest: 'hidden-phone',
631                                                 Offer           : 'offer',
632                                                 HighestOffer: 'offer hidden-phone',
633                                                 Time            : 'Time ',
634                                                 Status          : 'status hidden-phone'
635                                         },
636                                         items: {
637                                                 'id'      : {'pgId' : row.id, 'id' : myOffer ? myOffer.id : 0},
638                                                 'Make'    : row.stock.type.model.make.name,
639                                                 'Type'    : row.stock.type.name,
640                                                 'Year'    : row.stock.vehicleYear.name,
641                                                 'KM'      : row.stock.km,
642                                                 'Trade'   : row.stock.tradePrice,
643                         'List'   : row.stock.listPrice,
644                                                 'Company' : row.company.name,
645                                                 'Region'  : row.company.region.name,
646                                                 'CustomerRequest': row.stock.valuation && row.stock.valuation.requiredPrice
647                                                                 ? row.stock.valuation.requiredPrice
648                                                                 : 0.00,
649                                                 'Offer'   : myOffer
650                                                                 ? myOffer.amount + '<a class="soda_pop hidden-phone hidden-tablet" id="ofr' + row.id + '"><span>' + row.stock.numberOfOffers + '</span></a>'
651                                                                 : '<a class="soda_pop hidden-phone hidden-tablet" id="ofr' + row.id + '"><span>' + row.stock.numberOfOffers + '</span></a>',
652                                                 'HighestOffer' : ('Open4Offers' != row.jobState && 'Updateable Offers' != row.jobState)
653                                                                                         && row.stock.highestOffer
654                                                                 ? row.stock.highestOffer
655                                                                 : '',
656                                                 'Time'    : hoursLeft,
657                                                 'Status'  : myOffer
658                                                                 ? myOffer.status
659                                                                 : ('Open4Offers' == row.jobState || 'Updateable Offers' == row.jobState ? '* Open4Offers' : '* Expired')
660                                 }
661                                 });
662                         }
663                         _w.PopUp = false;
664                         _w.priceguide.ti.hydrateParam('gridRowRepeater', {
665                                 constructor : 'GridDataRow',
666                                 items       : gridData
667                         });
668                         response.Data.Meta.itemName = 'stockPager';
669                         response.Data.Meta.bind = _w.priceguide.pagePriceGuideGrid;
670                         _w.priceguide.ti.hydrateParam('gridPager', {
671                                 constructor : 'GridPager',
672                                 items       : [response.Data.Meta]
673                         });
674
675                         // todo: add pager hydration
676
677
678                         /*
679                          * Permissions
680                          */
681
682
683                 },
684
685                 exportPriceGuideGrid : function()
686                 {
687                         App.API.execTask(
688                                         'gridPriceGuide', {}, {'ExportToExcel': true},
689                                         _w.priceguide._onExportReceived, _w.taskExecError,
690                                         'EXPORT', true
691                                         );
692                 },
693
694                 _onExportReceived : function( response )
695                 {
696                         var win = window.open();
697                                                 win.document.write(response);
698                 },
699
700                 clearPriceGuideGridContext : function()
701                 {
702                         _w.priceguide.gridStatus = 'Inactive';
703                         try
704                         {
705                                 $('select.gridPriceGuideFilter').each(function(i, elem) {
706                                         $(elem).selectpicker('val', '');
707                                         $(elem).selectpicker('refresh');
708                                 });
709                         }
710                         catch (err) { console.log(err); }
711                         $('.gridPriceGuideContext').val('');
712                         $('.gridPriceGuideFilter').val('');
713                         _w.priceguide.searchPriceGuideGrid(true);
714                 },
715
716                 searchPriceGuideGrid : function(resetOrder)
717                 {
718                         _w.priceguide.gridStatus = 'Inactive';
719                         var filter = {
720                                         count: 0,
721                                         filters: {}
722                         };
723                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideContext_RegNo', 'stock.registrationNumber');
724                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideContext_RefNo', 'stock.referenceNumber');
725                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideContext_DateFrom', 'priceGuide.created', '>=');
726                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideContext_DateTo', 'priceGuide.created', '<=', ' 23:59:59');
727                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Make', 'make.id');
728                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Type', 'type.name');
729                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Year', 'vehicleYear.name');
730                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_KM', 'stock.km');
731                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Trade', 'stock.tradePrice');
732             filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_List', 'stock.listPrice');
733                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Company', 'company.name');
734                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Region', 'region.id');
735                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Request', 'valuation.requiredPrice');
736                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Offer', 'offer.amount');
737                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_HighestOffer', 'stock.highestOffer');
738                         filter = _w.filterIfnotEmpty(filter, 'gridPriceGuideFilter_Status', 'offer.status');
739                         var request = {"Grid":{
740                                         "Page": 1,
741                                         "Filter": filter.filters
742                                 }};
743                         if (resetOrder)
744                         {
745                                 request.Grid.OrderBy = {'priceGuide.created': 'DESC'};
746                         }
747                         _w.priceguide.loadPriceGuideGrid( null, request, {} );
748                 },
749
750                 orderPriceGuideGrid : function(field, direction)
751                 {
752                         var order = {};
753                         order[field] = direction;
754                         _w.priceguide.loadPriceGuideGrid( {}, {"Grid":{"Page": 1, "OrderBy": order}} );
755                 }
756
757         };
758
759 })();