initial commit
[namibia] / public / js / app / portal-workspace / valuations_filter_fix.js
1 ;(function(){
2
3         window._w.valuation = {
4
5                 ti : null,
6                 dfrom : '',
7                 dto   : '',
8                 gridStatus : null,
9
10                 onPageDestruct : function()
11                 {
12                         App.Event.removeListener('valuationGridSuFilter', 'SuChange');
13                 },
14
15                 onValuationPageTemplateReady : function( template, event, eventData )
16                 {
17                         _w.valuation.allowedDifference = 4000;
18                         _w.valuation.templateReadyTime = new Date().getTime();
19                         var newtitle = 'RETAIL';
20                         var newtitle2 = 'OFFER';
21
22                         if ('Dealership Sales' == App.permissions.name) {
23                                 var newtitle = 'HIGEST PRICE GUIDE OFFER';
24                                 var newtitle2 = 'MANAGER\'s OFFER';
25                         }
26                         _w.valuation.ti = _t[template];
27                         App.DataStore.loadSelectListData('filtermakes', true, 'Stock', 'Make.SelectList');
28                         App.DataStore.loadSelectListData('sales', false, 'User', 'Profile.SalesList');
29                         App.DataStore.loadSelectListData('managers', false, 'User', 'Profile.ManagerList');
30                         App.DataStore.setData('valuationstatuses',
31                                         [
32                                          {"value":"New Valuation", "label":"New Valuation"},
33                                          {"value":"Pending Valuation", "label":"Pending Valuation"},
34                                          {"value":"Complete Valuation", "label":"Complete Valuation"},
35                                          {"value":"Offer Accepted", "label":"Offer Accepted"},
36                                          {"value":"Deal Done", "label":"Deal Done"},
37                                          {"value":"Deal Not Done", "label":"Deal Not Done"},
38                                          {"value":"Price Guide", "label":"Price Guide"},
39                                          {"value":"Archived", "label":"Archived"}
40                                          ]
41                         );
42
43                         App.DataStore.setData('archivedStatuses',
44                                         [
45                                          {"value":"includingArchived", "label":"View all vehicles including archived"},
46                                          {"value":"excludingArchived", "label":"View all vehicles excluding archived"},
47                                          {"value":"onlyArchived", "label":"View only archived"}
48                                          ]
49                         );
50
51                         _w.valuation.ti.hydrate({
52                                 'gridTitle': 'Valuations',
53                                 'gridTitleSubtext': 'Please use the filters below to narrow down the view',
54                                 'gridTitleButtons': {
55                                         constructor: 'Button',
56                                         items: {
57                                                 Report: {
58                                                         permission: App.permissions.settingsReport,
59                                                         preset: 'ValuationReport',
60                                                         id: 'valuationReport',
61                                                         handler: _w.valuation.valuationReport
62                                                 },
63                                                 Export: {
64                                                         preset: 'GridExport',
65                                                         id: 'exportValuationGrid',
66                                                         handler: _w.valuation.exportValuationGrid
67                                                 },
68                                                 New: {
69                                                         preset: 'NewValuation',
70                                                         id: 'newValuation',
71                                                         handler: _w.valuation.newValuation
72                                                 }
73                                         }
74                                 },
75                                 'gridContextFilter': {
76                                         constructor: 'GridContextFilter',
77                                         items: {
78                                                 VinNum: {
79                                                         title: 'Search here:',
80                                                         style : 'filter-input-wrapper hidden-phone',
81                                                         id: 'gridValuationContext_VIN',
82                                                         placeholder: 'VIN number',
83                                                         context: 'gridValuationContext'
84                                                 },
85                                                 RefNum: {
86                                                         style : 'filter-input-wrapper hidden-phone',
87                                                         id: 'gridValuationContext_RefNo',
88                                                         placeholder: 'Ref number',
89                                                         context: 'gridValuationContext'
90                                                 },
91                                                 RegNum: {
92                                                         style : 'filter-input-wrapper hidden-phone',
93                                                         id: 'gridValuationContext_RegNo',
94                                                         placeholder: 'Registration number',
95                                                         context: 'gridValuationContext'
96                                                 },
97                                                 RegNumMobi: {
98                                                         style : 'filter-input-wrapper hidden-desktop',
99                                                         id: 'gridValuationContext_RegNoMobi',
100                                                         placeholder: 'Registration number',
101                                                         context: 'gridValuationContext'
102                                                 },
103                                                 Name: {
104                                                         style : 'filter-input-wrapper hidden-phone',
105                                                         id: 'gridValuationContext_Name',
106                                                         placeholder: 'Customer Surname',
107                                                         context: 'gridValuationContext'
108                                                 },
109                                                 DateFrom: {
110                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dfrom hidden-phone',
111                                                         title: 'Search date created here',
112                                                         id: 'gridValuationContext_DateFrom',
113                                                         placeholder: 'From',
114                                                         context: 'gridValuationContext',
115                                                         append: '<i class="icon-calendar"></i>',
116                                                         readonly: true,
117                                                         bind: function() {
118                                                                 $('#gridValuationContext_DateFrom').change(function() {
119                                                                         _w.valuation.dFrom = $(this).val();
120                                                                 });
121                                                         }
122                                                 },
123                                                 DateTo: {
124                                                         style : 'input-append date filter-input-wrapper-date mobile-width12p dto hidden-phone',
125                                                         id: 'gridValuationContext_DateTo',
126                                                         placeholder: 'To',
127                                                         context: 'gridValuationContext',
128                                                         append: '<i class="icon-calendar"></i>',
129                                                         readonly: true,
130                                                         bind: function() {
131                                                                 $('#gridValuationContext_DateTo').change(function() {
132                                                                         _w.valuation.dTo = $(this).val();
133                                                                 });
134                                                         }
135                                                 },
136                                                 MakeMobi: {
137                                                         style: 'filter-input-wrapper filter-input-select',
138                                                         placeholder: 'Make Mobi',
139                                                         id: 'gridPriceGuideContext_MakeMobi',
140                                                         context: 'gridValuationContext',
141                                                         dataStoreId : 'filtermakes',
142                                                         selectEmpty : 'All Makes',
143                                                         style: 'MakeMobi hidden-desktop ',
144                                                         bind : function(meta){
145                                                                 $('#gridAuctionFilter_MakeMobi').change(function(){
146                                                                         $('#gridAuctionFilter_Make').selectpicker('val', $(this).val());
147                                                                 });
148                                                         }
149                                                 },
150                                                 // status
151                                                 Status: {
152                                                         style: 'filter-input-wrapper filter-input-select ',
153                                                         placeholder: 'status Mobi',
154                                                         id: 'gridValuationContext_StatusMobi',
155                                                         context: 'gridValuationContext',
156                                                         dataStoreId : 'valuationstatuses',
157                                                         selectEmpty : 'All',
158                                                         style: 'StatusMobi hidden-desktop ',
159                                                         bind : function(meta){
160                                                                 $('#gridValuationContext_StatusMobi').change(function()
161                                                                 {
162                                                                         $('#gridValuationFilter_Status').selectpicker('val', $(this).val());
163                                                                 });
164                                                         }
165                                                 },
166
167                                                 ValuationNotArchived: {
168                                                         style : 'filter-input-wrapper hidden',
169                                                         id: 'gridValuationContext_notArchived',
170                                                         placeholder: 'Valuation Not Archived',
171                                                         context: 'gridValuationContext',
172                                                         value: 'Archived,Stock'
173                                                 },
174
175                                                 Archived: {
176                                                         style: 'filter-input-wrapper filter-input-select ',
177                                                         placeholder: 'Archived ',
178                                                         id: 'gridValuationContext_Archived',
179                                                         context: 'gridValuationContext',
180                                                         dataStoreId : 'archivedStatuses',
181                                                         value: 'excludingArchived',
182                                                         style: 'Archived hidden-phone hidden-tablet',
183                                                         bind : function(meta){
184                                                                 $('#gridValuationContext_Archived').change(function(){
185                                                                         _w.valuationsArchiveDrop = $(this).val();
186                                                                         if ($(this).val() == 'includingArchived')
187                                                                         {
188                                                                                 //includingArchived
189                                                                                 //remove the excludingArchived input value in hidden field
190                                                                                 $('#gridValuationContext_notArchived').val('');
191                                                                                 //set the grid filter status to All and show
192                                                                                 if (!_w.valuation.statusMenuOverride)
193                                                                                 {
194                                                                                         $('#gridValuationFilter_Status').selectpicker('val', 'All');
195                                                                                 }
196                                                                                 $('[data-id="gridValuationFilter_Status"]').removeClass("hidden");
197
198                                                                         }
199                                                                         if ($(this).val() == 'excludingArchived')
200                                                                         {
201                                                                                 $('[data-id="gridValuationFilter_Status"]').removeClass("hidden");
202                                                                                 //excludingArchived
203                                                                                 // The not Archive view all and trigger search
204                                                                                 // Has operator in seach !(not) so search for Archive only
205                                                                                 $('#gridValuationContext_notArchived').val('Archived,Stock');
206                                                                                 if ('All' != $('#gridValuationFilter_Status').selectpicker('val'))
207                                                                                 {
208                                                                                         if (!_w.valuation.statusMenuOverride)
209                                                                                         {
210                                                                                                 $('#gridValuationFilter_Status').selectpicker('val', 'All');
211                                                                                         }
212                                                                                 }
213                                                                                 _w.valuation.searchValuationGrid();
214                                                                         }
215                                                                         if ($(this).val() == 'onlyArchived')
216                                                                         {
217                                                                                 //onlyArchived
218                                                                                 //remove the excludingArchived input value in hidden field
219                                                                                 $('#gridValuationContext_notArchived').val('');
220                                                                                 //set the grid filter status to Archived and hide
221                                                                                 if (!_w.valuation.statusMenuOverride)
222                                                                                 {
223                                                                                         $('#gridValuationFilter_Status').selectpicker('val', 'Archived');
224                                                                                 }
225                                                                                 //data-id = gridValuationFilter_Status hide
226                                                                                 $('[data-id="gridValuationFilter_Status"]').addClass("hidden");
227                                                                         }
228                                                                         _w.valuation.statusMenuOverride = false;
229
230
231
232                                                                 });
233                                                         }
234                                                 },
235
236                                                 Buttons: {
237                                                         actions: true,
238                                                         clearId: 'clearValuationGridContext',
239                                                         bindClear: _w.valuation.clearValuationGridContext,
240                                                         searchId: 'searchValuationGrid',
241                                                         bindSearch: _w.valuation.searchValuationGrid
242                                                 }
243                                         }
244                                 },
245                                 'gridColumnHeaders': {
246                                         constructor: 'GridColumnHeader',
247                                         items: {
248                                                 Make: {
249                                                         id: 'gridValuationColumn_Make',
250                                                         title: 'MAKE',
251                                                         orderAsc: {
252                                                                 id: 'gridValuationOrder_MakeAsc',
253                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'make.name', 'ASC')
254                                                         },
255                                                         orderDesc: {
256                                                                 id: 'gridValuationOrder_MakeDesc',
257                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'make.name', 'DESC')
258                                                         },
259                                                         style: 'hidden-phone'
260                                                 },
261                                                 Model: {
262                                                         id: 'gridValuationColumn_Model',
263                                                         title: 'MODEL',
264                                                         orderAsc: {
265                                                                 id: 'gridValuationOrder_ModelAsc',
266                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'model.name', 'ASC')
267                                                         },
268                                                         orderDesc: {
269                                                                 id: 'gridValuationOrder_ModelDesc',
270                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'model.name', 'DESC')
271                                                         },
272                                                         style: 'hidden-phone'
273                                                 },
274                                                 Type: {
275                                                         id: 'gridValuationColumn_Type',
276                                                         title: 'TYPE',
277                                                         orderAsc: {
278                                                                 id: 'gridValuationOrder_TypeAsc',
279                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'type.name', 'ASC')
280                                                         },
281                                                         orderDesc: {
282                                                                 id: 'gridValuationOrder_TypeDesc',
283                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'type.name', 'DESC')
284                                                         },
285                                                         style: 'type'
286
287                                                 },
288                                                 Year: {
289                                                         id: 'gridValuationColumn_Year',
290                                                         title: 'YEAR',
291                                                         orderAsc: {
292                                                                 id: 'gridValuationOrder_YearAsc',
293                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'vehicleYear.name', 'ASC')
294                                                         },
295                                                         orderDesc: {
296                                                                 id: 'gridValuationOrder_YearDesc',
297                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'vehicleYear.name', 'DESC')
298                                                         },
299                                                         style : 'year'
300                                                 },
301                                                 KM: {
302                                                         id: 'gridValuationColumn_KM',
303                                                         title: 'KM&#39;S',
304                                                         orderAsc: {
305                                                                 id: 'gridValuationOrder_KMAsc',
306                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.km', 'ASC')
307                                                         },
308                                                         orderDesc: {
309                                                                 id: 'gridValuationOrder_KMDesc',
310                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.km', 'DESC')
311                                                         },
312                                                         style : 'hidden-phone kms'
313                                                 },
314                                                 Trade: {
315                                                         id: 'gridValuationColumn_Trade',
316                                                         title: 'TRADE',
317                                                         orderAsc: {
318                                                                 id: 'gridValuationOrder_TradeAsc',
319                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.tradePrice', 'ASC')
320                                                         },
321                                                         orderDesc: {
322                                                                 id: 'gridValuationOrder_TradeDesc',
323                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.tradePrice', 'DESC')
324                                                         },
325                                                         style : 'hidden-phone'
326                                                 },
327                                                 Retail: {
328                                                         id: 'gridValuationColumn_Retail',
329                                                         title: newtitle,
330                                                         orderAsc: {
331                                                                 id: 'gridValuationOrder_RetailAsc',
332                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.retailPrice', 'ASC')
333                                                         },
334                                                         orderDesc: {
335                                                                 id: 'gridValuationOrder_RetailDesc',
336                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.retailPrice', 'DESC')
337                                                         },
338                                                         style: 'hidden-phone'
339                                                 },
340                         List: {
341                             id: 'gridValuationColumn_List',
342                             title: 'NEW LIST PRICE',
343                             orderAsc: {
344                                 id: 'gridValuationOrder_ListAsc',
345                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.listPrice', 'ASC')
346                             },
347                             orderDesc: {
348                                 id: 'gridValuationOrder_ListDesc',
349                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.listPrice', 'DESC')
350                             },
351                             style : 'hidden-phone'
352                         },
353                                                 Offer: {
354                                                         id: 'gridValuationColumn_Offer',
355                                                         title: newtitle2,
356                                                         orderAsc: {
357                                                                 id: 'gridValuationOrder_OfferAsc',
358                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.highestOffer', 'ASC')
359                                                         },
360                                                         orderDesc: {
361                                                                 id: 'gridValuationOrder_OfferDesc',
362                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.highestOffer', 'DESC')
363                                                         },
364                                                         style: 'offer'
365                                                 },
366                                                 Sales: {
367                                                         id: 'gridValuationColumn_Sales',
368                                                         title: 'SALES',
369                                                         orderAsc: {
370                                                                 id: 'gridValuationOrder_SalesAsc',
371                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'salesProfile.id', 'ASC')
372                                                         },
373                                                         orderDesc: {
374                                                                 id: 'gridValuationOrder_SalesDesc',
375                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'salesProfile.id', 'DESC')
376                                                         },
377                                                         style : 'hidden-phone'
378                                                 },
379                                                 Manager: {
380                                                         id: 'gridValuationColumn_Manager',
381                                                         title: 'MANAGER',
382                                                         orderAsc: {
383                                                                 id: 'gridValuationOrder_ManagerAsc',
384                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'managerProfile.id', 'ASC')
385                                                         },
386                                                         orderDesc: {
387                                                                 id: 'gridValuationOrder_ManagerDesc',
388                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'managerProfile.id', 'DESC')
389                                                         },
390                                                         style : 'hidden-phone'
391                                                 },
392                                                 Status: {
393                                                         id: 'gridValuationColumn_Status',
394                                                         title: 'STATUS',
395                                                         orderAsc: {
396                                                                 id: 'gridValuationOrder_StatusAsc',
397                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'valuation.jobState', 'ASC')
398                                                         },
399                                                         orderDesc: {
400                                                                 id: 'gridValuationOrder_StatusDesc',
401                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'valuation.jobState', 'DESC')
402                                                         },
403                                                         style: 'status'
404                                                 },
405                                         }
406                                 },
407                                 'gridColumnFilters': {
408                                         constructor: 'GridColumnFilter',
409                                         items: {
410                                                 Make: {
411                                                         id: 'gridValuationFilter_Make',
412                                                         context: 'gridValuationFilter',
413                                                         dataStoreId : 'filtermakes',
414                                                         selectEmpty : 'All',
415                                                         bind : function(meta){
416                                                                 _w.valuation.changes = 0;
417                                                                 $('#' + meta.id).change(function(e){
418                                                                         _w.valuation.changes++;
419                                                                         if (undefined == $(e.currentTarget).val() || 1 == _w.valuation.changes || 'Active' != _w.valuation.gridStatus)
420                                                                         {
421                                                                                 return;
422                                                                         }
423                                                                         _w.valuation.searchValuationGrid();
424                                                                 });
425                                                         }
426                                                 },
427                                                 Model: {
428                                                         id: 'gridValuationFilter_Model',
429                                                         context: 'gridValuationFilter'
430                                                 },
431                                                 Type: {
432                                                         id: 'gridValuationFilter_Type',
433                                                         context: 'gridValuationFilter'
434                                                 },
435                                                 Year: {
436                                                         id: 'gridValuationFilter_Year',
437                                                         context: 'gridValuationFilter'
438                                                 },
439                                                 KM: {
440                                                         id: 'gridValuationFilter_KM',
441                                                         context: 'gridValuationFilter'
442                                                 },
443                                                 Trade: {
444                                                         id: 'gridValuationFilter_Trade',
445                                                         context: 'gridValuationFilter'
446                                                 },
447                                                 Retail: {
448                                                         id: 'gridValuationFilter_Retail',
449                                                         context: 'gridValuationFilter'
450                                                 },
451                                                 Offer: {
452                                                         id: 'gridValuationFilter_Offer',
453                                                         context: 'gridValuationFilter'
454                                                 },
455                                                 Sales: {
456                                                         id: 'gridValuationFilter_Sales',
457                                                         context: 'gridValuationFilter',
458                                                         dataStoreId : 'sales',
459                                                         selectEmpty : 'All',
460                                                         bind : function(meta){
461                                                                 _w.valuation.changes = 0;
462                                                                 $('#' + meta.id).change(function(e)
463                                                                 {
464                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Sales', false))
465                                                                         {
466                                                                                 return;
467                                                                         }
468                                                                         _w.valuation.searchValuationGrid();
469                                                                 });
470                                                         }
471                                                 },
472                                                 Manager: {
473                                                         id: 'gridValuationFilter_Manager',
474                                                         context: 'gridValuationFilter',
475                                                         dataStoreId : 'managers',
476                                                         selectEmpty : 'All',
477                                                         bind : function(meta){
478                                                                 _w.valuation.changes = 0;
479                                                                 $('#' + meta.id).change(function(e)
480                                                                 {
481                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Manager', false))
482                                                                         {
483                                                                                 return;
484                                                                         }
485                                                                         _w.valuation.searchValuationGrid();
486                                                                 });
487                                                         }
488                                                 },
489                                                 Status: {
490                                                         id: 'gridValuationFilter_Status',
491                                                         context: 'gridValuationFilter',
492                                                         dataStoreId : 'valuationstatuses',
493                                                         selectEmpty : 'All',
494                                                         bind : function(meta){
495                                                                 _w.valuation.changes = 0;
496                                                                 $('#' + meta.id).change(function(){
497                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Status', false))
498                                                                         {
499                                                                                 return;
500                                                                         }
501                                                                         
502                                                                         if (!_w.valuation.statusMenuOverride)
503                                                                         {
504                                                                                 _w.valuation.searchValuationGrid();
505                                                                         }
506
507                                                                         if ($(this).val() == 'Archived')
508                                                                         {
509                                                                                 _w.valuation.statusMenuOverride = true;
510                                                                                 $('#gridValuationContext_Archived').selectpicker('val', 'onlyArchived');                                                                                
511                                                                         }
512
513                                                                 });
514                                                         }
515                                                 },
516                                         }
517                                 },
518                                 'gridRowRepeater': {},
519                                 'gridPager': {}
520                         });
521
522                         // Retrieve initial grid data.
523                         _w.valuation.publishing = true;
524                         if (null == App.API.taskContract('gridValuation'))
525                         {
526                                 /*App.API.getTask(
527                                                 'gridValuation', 'Valuation', 'Valuation.Grid', null,
528                                                 {}, $.proxy(_w.valuation.searchValuationGrid, this, null, {}, {}), _w.taskContractError
529                                                 );*/
530                         App.API.getTask(
531                                                 'gridValuation', 'Valuation', 'Valuation.Grid', null,
532                                                 {}, _w.valuation.loadValuationGrid, _w.taskContractError
533                                                 );
534                         }
535                         else
536                         {
537                                 _w.valuation.loadValuationGrid();
538                         }
539                 },
540
541                 onValuationsPublished : function()
542                 {
543
544                         // Show new menu Buttons for mobi only
545                         var isMobile = 'none' == $('.header').css('display');
546                         if (isMobile)
547                         {
548                                 // show the mobi menu && hide old menu
549                                 $("#MobiAuctionPriceGuideMenu").show();
550                                 $("#MobiPortalMenu").hide();
551
552                                 // Check active state
553                                 $("#mobiAuctionBtn").removeClass("active");
554                                 $("#mobiPriceGuideBtn").removeClass("active");
555                                 $("#mobiValuationsBtn").addClass("active");
556                         }
557
558                         $("#mobiPriceGuideBtn").click(function() {
559                                 window.location.hash = '#/priceguide';
560                         });
561
562                         $("#mobiAuctionBtn").click(function() {
563                                 window.location.hash = '#/auction';
564                         });
565
566
567                         if (_w.valuationsArchiveDrop)
568                         {
569                                 $('#gridValuationContext_Archived').selectpicker('val', _w.valuationsArchiveDrop);
570                         }
571
572
573                         $(".gridValuationFilter").keyup(function (evt) {
574                                 var charCode = evt.charCode || evt.keyCode;
575                                 if (charCode  == 13) { //Enter key's keycode
576                                         $('#searchValuationGrid').click();
577                                 }
578                         });
579
580                         $(".gridValuationContext").keyup(function (evt) {
581                                 var charCode = evt.charCode || evt.keyCode;
582                                 if (charCode  == 13) { //Enter key's keycode
583                                         $('#searchValuationGrid').click();
584                                 }
585                         });
586                         $('#gridValuationContext_notArchived').val('Archived,Stock');
587
588                         var _p = App.permissions;
589                         var _createValuationDisabled = (false == _p.valuationCreateNew        && false == _p.valuationCreatePending
590                                                                                  && false == _p.valuationCreatePriceguide && false == _p.valuationCreateSales
591                                                                                  && false == _p.valuationCreateStock)     ? true : false ;
592
593                         $('#newValuation').prop('disabled', _createValuationDisabled);
594                         $('#exportValuationGrid').prop('disabled', !App.permissions.valuationGridExport);
595
596                         $('.dfrom').datepicker({format:"yyyy-mm-dd"});
597                         $('.dto').datepicker({format:"yyyy-mm-dd"});
598                         $('#gridValuationContext_DateFrom').val(_w.valuation.dFrom);
599                         $('#gridValuationContext_DateTo').val(_w.valuation.dTo);
600                         App.Event.listen('valuationGridSuFilter', 'SuChange', $.proxy(_w.valuation.clearValuationGridContext, this), 'Recurring');
601
602                         _w.valuation.publishing = false;
603                         setTimeout(_w.valuation.refreshGrid, 120000);
604
605                 },
606
607                 refreshGrid : function()
608                 {
609                         if ('valuations' != App.activePage)
610                         {
611                                 return;
612                         }
613                         _w.valuation.loadValuationGrid();
614                         setTimeout(_w.valuation.refreshGrid, 120000);
615                 },
616
617                 pageValuationGrid : function( page )
618                 {
619                         _w.valuation.loadValuationGrid( {}, {"Grid":{"Page":page}} );
620                 },
621
622                 loadValuationGrid : function( contract, data, options )
623                 {
624                         //console.log('Filter  value is', $('#gridValuationContext_Archived').val());
625                         if ($('#gridValuationFilter_Status').val() == 'excludingArchived')
626                         {
627                                 $('#gridValuationContext_notArchived').val('Archived,Stock');
628                                 if (undefined == data)
629                                 {
630                                         data = {};
631                                 }
632                                 if (undefined == data.Grid)
633                                 {
634                                         data.Grid = {};
635                                 }
636                                 if (undefined == data.Grid.Filter)
637                                 {
638                                         data.Grid.Filter = {};
639                                 }
640                                 data.Grid.Filter['valuation.jobState'] = 'NOT IN Archived,Stock';
641                         }
642                         //This comes back undefined if you are from a different page back to valuations
643         /*              if ($('#gridValuationContext_Archived').val() == undefined)
644                         {
645                                 data.Grid.Filter['valuation.jobState'] = 'NOT IN Archived,Stock';
646                         }
647 */
648                         App.API.execTask(
649
650                                         'gridValuation', data, options,
651                                         _w.valuation._onValuationGridDataReceived, _w.taskExecError
652                                         );
653
654                 },
655
656                 _onValuationGridDataReceived : function( response )
657                 {
658
659                 
660                         console.log('Filter responses ',response.Data.Meta.Filters);
661                         
662                         _w.valuation.gridStatus = 'Active';
663                         _w.valuation.ti.hydrateParam('gridRowRepeater', {});
664                         var statuses = {
665                                 "New Valuation": true,
666                                 "Pending Valuation": true,
667                                 "Complete Valuation": true,
668                                 "Offer Accepted": true,
669                                 "Deal Done": true,
670                                 "Deal Not Done": true,
671                                 "Price Guide": true,
672                                 "Archived": true
673                         };
674                         if (undefined != response.Data.Meta.Filters['valuation.jobState']
675                                 && undefined != statuses[response.Data.Meta.Filters['valuation.jobState']])
676                         {
677                                 _w.populateFilters(response.Data.Meta.Filters, {
678                                         'stock.vinNumber': 'gridValuationContext_VIN',
679                                         'stock.registrationNumber': 'gridValuationContext_RegNo',
680                                         'stock.referenceNumber': 'gridValuationContext_RefNo',
681                                         'valuation.familyName': 'gridValuationContext_Name',
682                                         'make.id': 'gridValuationFilter_Make',
683                                         'model.name': 'gridValuationFilter_Model',
684                                         'type.name': 'gridValuationFilter_Type',
685                                         'vehicleYear.name': 'gridValuationFilter_Year',
686                                         'stock.km': 'gridValuationFilter_KM',
687                                         'stock.tradePrice': 'gridValuationFilter_Trade',
688                                         'stock.retailPrice': 'gridValuationFilter_Retail',
689                     'stock.listPrice': 'gridValuationFilter_List',
690                                         'stock.highestOffer': 'gridValuationFilter_Offer',
691                                         'salesProfile.id': 'gridValuationFilter_Sales',
692                                         'managerProfile.id': 'gridValuationFilter_Manager',
693                                         'valuation.jobState':  'gridValuationFilter_Status'
694                                 });
695                         }
696                         else
697                         {
698                                 _w.populateFilters(response.Data.Meta.Filters, {
699                                         'stock.vinNumber': 'gridValuationContext_VIN',
700                                         'stock.registrationNumber': 'gridValuationContext_RegNo',
701                                         'stock.referenceNumber': 'gridValuationContext_RefNo',
702                                         'valuation.familyName': 'gridValuationContext_Name',
703                                         'make.id': 'gridValuationFilter_Make',
704                                         'model.name': 'gridValuationFilter_Model',
705                                         'type.name': 'gridValuationFilter_Type',
706                                         'vehicleYear.name': 'gridValuationFilter_Year',
707                                         'stock.km': 'gridValuationFilter_KM',
708                                         'stock.tradePrice': 'gridValuationFilter_Trade',
709                                         'stock.retailPrice': 'gridValuationFilter_Retail',
710                     'stock.listPrice': 'gridValuationFilter_List',
711                                         'stock.highestOffer': 'gridValuationFilter_Offer',
712                                         'salesProfile.id': 'gridValuationFilter_Sales',
713                                         'managerProfile.id': 'gridValuationFilter_Manager'
714                                 });
715                         }
716 //valuation.jobState = "NOT IN Archived,Stock" >> excluding archived
717 //valuation.jobState = "Archived" >> only archived
718                         /*if (undefined == response.Data.Meta.Filters['valuation.jobState'])
719                         {
720                                 _w.valuation.statusMenuOverride = true;
721                                 $('#gridValuationContext_Archived').selectpicker('val', 'includingArchived');
722                         }
723                         else if ('NOT IN Archived,Stock' == response.Data.Meta.Filters['valuation.jobState'])
724                         {
725                                 _w.valuation.statusMenuOverride = true;
726                                 $('#gridValuationContext_Archived').selectpicker('val', 'excludingArchived');
727                         }
728                         else if ('Archived' == response.Data.Meta.Filters['valuation.jobState'])
729                         {
730                                 _w.valuation.statusMenuOverride = true;
731                                 $('#gridValuationContext_Archived').selectpicker('val', 'onlyArchived');
732                         }*/
733                         
734
735                 
736                         
737
738                         $('select.gridValuationFilter').each(function(i, elem) {
739                                 $(elem).selectpicker('refresh');
740                                 // Error: Resets status when status should be populated gridStockFilter
741                         });
742                         var gridData = [];
743                         for (var i = 0; i < response.Data.DataSet.length; i++)
744                         {
745                                 var row = response.Data.DataSet[i];
746                                 gridData.push({
747                                         lineClass: 'handy',
748                                         surl: (
749                                                         'New Valuation'         == row.jobState
750                                                         || 'Pending Valuation'  == row.jobState
751                                                         || 'Complete Valuation' == row.jobState
752                                                         || 'Deal Not Done'      == row.jobState
753                                                         || ('Price Guide' == row.jobState
754                                                                         && row.stock.numberOfOffers == 0)
755                                         )
756                                                 ? 'valuationview'
757                                                 : 'limitedvaluationview',
758                                         prepend: {
759                                                 Trade  : 'R ',
760                                                 Retail : 'R ',
761                                                 Offer  : 'R '
762                                         },
763                                         rowId: row.id,
764                                         pgId: row.stock.priceGuide.id,
765                                         offers:  row.stock.priceGuide && row.stock.priceGuide.offers
766                                                 ? row.stock.priceGuide.offers[0]
767                                                 : null,
768                                         bind: function(meta) {
769                                                 $('#row' +  meta.rowId).click(function(e){
770                                                         window.location='#/' + meta.surl + '?id=' + meta.rowId;
771                                                 });
772
773                                                 $('#ofr' + meta.rowId).click(function(e) {
774                                                         _w.PopUp = true;
775                                                         e.preventDefault();
776                                                         e.stopPropagation();
777                                                         if (!meta.pgId)
778                                                         {
779                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
780                                                                 $('#modalbody').html(modalbody);
781                                                                 return;
782                                                         }
783                                                         else
784                                                         {
785                                                                 $('#modalbody').html('<center>Loading offers...</center>');
786                                                                 $('#myModal').modal('toggle');
787                                                         }
788                                                         App.API.getTask(
789                                                                         'offerList', 'PriceGuide', 'Offer.List',
790                                                                         meta.pgId, {}, function(){
791                                                                                 App.API.execTask(
792                                                                                                 'offerList', {}, {},
793                                                                                                 function(response)
794                                                                                                 {
795                                                                                                         var offers = response.Data;
796                                                                                                         if (offers.length)
797                                                                                                         {
798                                                                                                                 var modalbody = '<table class="table table-bordered table-condensed table-striped">';
799                                                                                                                 modalbody += '<tr><td>Company</td><td>Date Offered</td><td>Amount</td></tr>';
800                                                                                                                 for (var x in offers)
801                                                                                                                 {
802                                                                                                                         var isOwn = offers[x].company.id == App.companyData.id
803                                                                                                                                 ? true
804                                                                                                                                 : false;
805                                                                                                                         modalbody +=
806                                                                                                                                 '<tr class="modalrow ' + (isOwn ? 'red' : 'black') + '">'
807                                                                                                                                 + '<td class="date">' + offers[x].company.name + '</td>'
808                                                                                                                                 + '<td class="date">' + offers[x].created + '</td>'
809                                                                                                                                 + '<td class="wide">' + offers[x].amount + '</td>'
810                                                                                                                                 + '</tr>';
811                                                                                                                 }
812                                                                                                                 modalbody += '</table>';
813                                                                                                         }
814                                                                                                         else
815                                                                                                         {
816                                                                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
817                                                                                                         }
818                                                                                                         $('#modalbody').html(modalbody);
819                                                                                                 }, _w.taskExecError
820                                                                                 );
821                                                                         }, _w.taskContractError
822                                                         );
823                                                 });
824                                         },
825                                         style: {
826                                                 Make            : 'make hidden-phone',
827                                                 Model           : 'hidden-phone',
828                                                 Type            : 'type',
829                                                 Year            : 'year',
830                                                 KM              : 'hidden-phone',
831                                                 Trade           : 'hidden-phone',
832                                                 Retail          : 'hidden-phone',
833                                                 Offer           : 'offer',
834                                                 Sales           : 'hidden-phone',
835                                                 Manager     : 'hidden-phone',
836                                                 Status          : 'status'
837
838
839                                         },
840                                         items: {
841                                                 'id'     : row.id,
842                                                 'Make'   : row.stock.type.model.make.name,
843                                                 'Model'  : row.stock.type.model.name,
844                                                 'Type'   : row.stock.type.name,
845                                                 'Year'   : row.stock.vehicleYear.name,
846                                                 'KM'     : row.stock.km,
847                                                 'Trade'  : row.stock.tradePrice,
848                         'List'  : row.stock.listPrice,
849                                                 'Retail' : 'Dealership Sales' == App.permissions.name
850                                                                 ? row.stock.highestOffer
851                                                                 : row.stock.retailPrice,
852                                                 'Offer'  : 'Dealership Sales' == App.permissions.name
853                                                                         ? row.amountOffered
854                                                                         : (App.permissions.name == 'Dealership Manager (Used)' || App.permissions.name == 'Dealership Manager (New)'
855                                                                                         ? row.amountOffered + '&nbsp;&nbsp;&nbsp;'
856                                                                                         : row.stock.highestOffer + '&nbsp;&nbsp;&nbsp;')
857                                                                                 + '<a class="soda_pop " id="ofr' + row.id + '"><span>' + row.stock.numberOfOffers + '</span></a>',
858                                                 'Sales'  : row.salesProfile.firstName
859                                                                         ? row.salesProfile.firstName + ' ' + row.salesProfile.familyName
860                                                                         : '',
861                                                 'Manager': row.managerProfile.firstName
862                                                                         ? row.managerProfile.firstName + ' ' + row.managerProfile.familyName
863                                                                         : '',
864                                                 'Status' : row.jobState
865                                         }
866                                 });
867                         }
868
869                         _w.valuation.ti.hydrateParam('gridRowRepeater', {
870                                 constructor : 'GridDataRow',
871                                 items       : gridData
872                         });
873                         response.Data.Meta.itemName = 'stockPager';
874                         response.Data.Meta.bind = _w.valuation.pageValuationGrid;
875                         _w.valuation.ti.hydrateParam('gridPager', {
876                                 constructor : 'GridPager',
877                                 items       : [response.Data.Meta]
878                         });
879
880                         // todo: add pager hydration
881                 },
882
883                 valuationReport : function()
884                 {
885                         window.location.hash = '/valuationreport';
886                 },
887
888                 exportValuationGrid : function()
889                 {
890                         App.API.execTask(
891                                         'gridValuation', {}, {'ExportToExcel': true},
892                                         _w.valuation._onExportReceived, _w.taskExecError,
893                                         'EXPORT', true
894                                         );
895                 },
896
897                 newValuation : function()
898                 {
899                         window.location.hash = '/valuationview?id=0';
900                 },
901
902                 _onExportReceived : function( response )
903                 {
904                         var win = window.open();
905             win.document.write(response);
906                 },
907
908                 clearValuationGridContext : function()
909                 {
910                         _w.valuation.gridStatus = 'Inactive';
911                         try
912                         {
913                                 $('select.gridValuationFilter').each(function(i, elem) {
914                                         $(elem).val('');
915                                         $(elem).selectpicker('refresh');
916
917                                 });
918                         }
919                         catch(e)
920                         {
921                                 
922                         }
923                         $('.gridValuationContext').val('');
924                         $('.gridValuationFilter').val('');
925                         $('#gridValuationContext_notArchived').val('Archived,Stock');
926
927                         _w.valuation.dFrom = '';
928                         _w.valuation.dTo   = '';
929                         _w.valuation.searchValuationGrid(true);
930
931
932                 },
933
934                 searchValuationGrid : function(resetOrder)
935                 {
936                         if ($('#gridValuationFilter_Status').val() == 'excludingArchived' ){
937                                 $('#gridValuationContext_notArchived').val('Archived,Stock');
938                         }
939                         _w.valuation.gridStatus = 'Inactive';
940                         var filter = {
941                                         count: 0,
942                                         filters: {
943
944
945                                                 }
946                         };
947                         
948                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_VIN', 'stock.vinNumber');
949                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_RegNo', 'stock.registrationNumber');
950                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_RefNo', 'stock.referenceNumber');
951                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_Name', 'valuation.familyName');
952                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_DateFrom', 'stock.created', '>=');
953                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_DateTo', 'stock.created', '<=', ' 23:59:59');
954                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_notArchived', 'valuation.jobState', 'NOT IN');
955         
956                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Make', 'make.id');
957                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Model', 'model.name');
958                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Type', 'type.name');
959                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Year', 'vehicleYear.name');
960                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_KM', 'stock.km');
961                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Trade', 'stock.tradePrice');
962                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Retail', 'stock.retailPrice');
963             filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_List', 'stock.listPrice');
964                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Offer', 'stock.highestOffer');
965                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Sales', 'salesProfile.id');
966                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Manager', 'managerProfile.id');
967                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Status', 'valuation.jobState');
968                         
969                         var request = {"Grid":{
970                             "Page": 1,
971                             "Filter": filter.filters
972                           }};
973                         if (resetOrder)
974                         {
975                                 request.Grid.OrderBy = {'stock.created': 'DESC'};
976                         }
977                         
978                         _w.valuation.loadValuationGrid( null, request, {} );
979                 },
980
981                 orderValuationGrid : function(field, direction)
982                 {
983                         var order = {};
984                         order[field] = direction;
985                         _w.valuation.loadValuationGrid( {}, {"Grid":{"Page": 1, "OrderBy": order}} );
986                 }
987
988
989         };
990
991 })();