text changes to registration mail content
[namibia] / public / js / app / portal-workspace / valuations.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                         Offer: {
341                                                         id: 'gridValuationColumn_Offer',
342                                                         title: newtitle2,
343                                                         orderAsc: {
344                                                                 id: 'gridValuationOrder_OfferAsc',
345                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.highestOffer', 'ASC')
346                                                         },
347                                                         orderDesc: {
348                                                                 id: 'gridValuationOrder_OfferDesc',
349                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'stock.highestOffer', 'DESC')
350                                                         },
351                                                         style: 'offer'
352                                                 },
353                                                 Sales: {
354                                                         id: 'gridValuationColumn_Sales',
355                                                         title: 'SALES',
356                                                         orderAsc: {
357                                                                 id: 'gridValuationOrder_SalesAsc',
358                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'salesProfile.id', 'ASC')
359                                                         },
360                                                         orderDesc: {
361                                                                 id: 'gridValuationOrder_SalesDesc',
362                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'salesProfile.id', 'DESC')
363                                                         },
364                                                         style : 'hidden-phone'
365                                                 },
366                                                 Manager: {
367                                                         id: 'gridValuationColumn_Manager',
368                                                         title: 'MANAGER',
369                                                         orderAsc: {
370                                                                 id: 'gridValuationOrder_ManagerAsc',
371                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'managerProfile.id', 'ASC')
372                                                         },
373                                                         orderDesc: {
374                                                                 id: 'gridValuationOrder_ManagerDesc',
375                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'managerProfile.id', 'DESC')
376                                                         },
377                                                         style : 'hidden-phone'
378                                                 },
379                                                 Status: {
380                                                         id: 'gridValuationColumn_Status',
381                                                         title: 'STATUS',
382                                                         orderAsc: {
383                                                                 id: 'gridValuationOrder_StatusAsc',
384                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'valuation.jobState', 'ASC')
385                                                         },
386                                                         orderDesc: {
387                                                                 id: 'gridValuationOrder_StatusDesc',
388                                                                 bind: $.proxy(_w.valuation.orderValuationGrid, this, 'valuation.jobState', 'DESC')
389                                                         },
390                                                         style: 'status'
391                                                 },
392                                         }
393                                 },
394                                 'gridColumnFilters': {
395                                         constructor: 'GridColumnFilter',
396                                         items: {
397                                                 Make: {
398                                                         id: 'gridValuationFilter_Make',
399                                                         context: 'gridValuationFilter',
400                                                         dataStoreId : 'filtermakes',
401                                                         selectEmpty : 'All',
402                                                         bind : function(meta){
403                                                                 _w.valuation.changes = 0;
404                                                                 $('#' + meta.id).change(function(e){
405                                                                         _w.valuation.changes++;
406                                                                         if (undefined == $(e.currentTarget).val() || 1 == _w.valuation.changes || 'Active' != _w.valuation.gridStatus)
407                                                                         {
408                                                                                 return;
409                                                                         }
410                                                                         _w.valuation.searchValuationGrid();
411                                                                 });
412                                                         }
413                                                 },
414                                                 Model: {
415                                                         id: 'gridValuationFilter_Model',
416                                                         context: 'gridValuationFilter'
417                                                 },
418                                                 Type: {
419                                                         id: 'gridValuationFilter_Type',
420                                                         context: 'gridValuationFilter'
421                                                 },
422                                                 Year: {
423                                                         id: 'gridValuationFilter_Year',
424                                                         context: 'gridValuationFilter'
425                                                 },
426                                                 KM: {
427                                                         id: 'gridValuationFilter_KM',
428                                                         context: 'gridValuationFilter'
429                                                 },
430                                                 Trade: {
431                                                         id: 'gridValuationFilter_Trade',
432                                                         context: 'gridValuationFilter'
433                                                 },
434                                                 Retail: {
435                                                         id: 'gridValuationFilter_Retail',
436                                                         context: 'gridValuationFilter'
437                                                 },
438                         Offer: {
439                                                         id: 'gridValuationFilter_Offer',
440                                                         context: 'gridValuationFilter'
441                                                 },
442                                                 Sales: {
443                                                         id: 'gridValuationFilter_Sales',
444                                                         context: 'gridValuationFilter',
445                                                         dataStoreId : 'sales',
446                                                         selectEmpty : 'All',
447                                                         bind : function(meta){
448                                                                 _w.valuation.changes = 0;
449                                                                 $('#' + meta.id).change(function(e)
450                                                                 {
451                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Sales', false))
452                                                                         {
453                                                                                 return;
454                                                                         }
455                                                                         _w.valuation.searchValuationGrid();
456                                                                 });
457                                                         }
458                                                 },
459                                                 Manager: {
460                                                         id: 'gridValuationFilter_Manager',
461                                                         context: 'gridValuationFilter',
462                                                         dataStoreId : 'managers',
463                                                         selectEmpty : 'All',
464                                                         bind : function(meta){
465                                                                 _w.valuation.changes = 0;
466                                                                 $('#' + meta.id).change(function(e)
467                                                                 {
468                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Manager', false))
469                                                                         {
470                                                                                 return;
471                                                                         }
472                                                                         _w.valuation.searchValuationGrid();
473                                                                 });
474                                                         }
475                                                 },
476                                                 Status: {
477                                                         id: 'gridValuationFilter_Status',
478                                                         context: 'gridValuationFilter',
479                                                         dataStoreId : 'valuationstatuses',
480                                                         selectEmpty : 'All',
481                                                         bind : function(meta){
482                                                                 _w.valuation.changes = 0;
483                                                                 $('#' + meta.id).change(function(){
484                                                                         if (_w.valuation.publishing || App.DataStore.getItem('BuildSelect:gridValuationFilter_Status', false))
485                                                                         {
486                                                                                 return;
487                                                                         }
488
489                                                                         if (!_w.valuation.statusMenuOverride)
490                                                                         {
491                                                                                 _w.valuation.searchValuationGrid();
492                                                                         }
493
494                                                                         if ($(this).val() == 'Archived')
495                                                                         {
496                                                                                 _w.valuation.statusMenuOverride = true;
497                                                                                 $('#gridValuationContext_Archived').selectpicker('val', 'onlyArchived');
498                                                                         }
499
500                                                                 });
501                                                         }
502                                                 },
503                                         }
504                                 },
505                                 'gridRowRepeater': {},
506                                 'gridPager': {}
507                         });
508
509                         // Retrieve initial grid data.
510                         _w.valuation.publishing = true;
511                         if (null == App.API.taskContract('gridValuation'))
512                         {
513                                 /*App.API.getTask(
514                                                 'gridValuation', 'Valuation', 'Valuation.Grid', null,
515                                                 {}, $.proxy(_w.valuation.searchValuationGrid, this, null, {}, {}), _w.taskContractError
516                                                 );*/
517                         App.API.getTask(
518                                                 'gridValuation', 'Valuation', 'Valuation.Grid', null,
519                                                 {}, _w.valuation.loadValuationGrid, _w.taskContractError
520                                                 );
521                         }
522                         else
523                         {
524                                 _w.valuation.loadValuationGrid();
525                         }
526                 },
527
528                 onValuationsPublished : function()
529                 {
530
531                         // Show new menu Buttons for mobi only
532                         var isMobile = 'none' == $('.header').css('display');
533                         if (isMobile)
534                         {
535                                 // show the mobi menu && hide old menu
536                                 $("#MobiAuctionPriceGuideMenu").show();
537                                 $("#MobiPortalMenu").hide();
538
539                                 // Check active state
540                                 $("#mobiAuctionBtn").removeClass("active");
541                                 $("#mobiPriceGuideBtn").removeClass("active");
542                                 $("#mobiValuationsBtn").addClass("active");
543                         }
544
545                         $("#mobiPriceGuideBtn").click(function() {
546                                 window.location.hash = '#/priceguide';
547                         });
548
549                         $("#mobiAuctionBtn").click(function() {
550                                 window.location.hash = '#/auction';
551                         });
552
553
554                         if (_w.valuationsArchiveDrop)
555                         {
556                                 $('#gridValuationContext_Archived').selectpicker('val', _w.valuationsArchiveDrop);
557                         }
558
559
560                         $(".gridValuationFilter").keyup(function (evt) {
561                                 var charCode = evt.charCode || evt.keyCode;
562                                 if (charCode  == 13) { //Enter key's keycode
563                                         $('#searchValuationGrid').click();
564                                 }
565                         });
566
567                         $(".gridValuationContext").keyup(function (evt) {
568                                 var charCode = evt.charCode || evt.keyCode;
569                                 if (charCode  == 13) { //Enter key's keycode
570                                         $('#searchValuationGrid').click();
571                                 }
572                         });
573                         $('#gridValuationContext_notArchived').val('Archived,Stock');
574
575                         var _p = App.permissions;
576                         var _createValuationDisabled = (false == _p.valuationCreateNew        && false == _p.valuationCreatePending
577                                                                                  && false == _p.valuationCreatePriceguide && false == _p.valuationCreateSales
578                                                                                  && false == _p.valuationCreateStock)     ? true : false ;
579
580                         $('#newValuation').prop('disabled', _createValuationDisabled);
581                         $('#exportValuationGrid').prop('disabled', !App.permissions.valuationGridExport);
582
583                         $('.dfrom').datepicker({format:"yyyy-mm-dd"});
584                         $('.dto').datepicker({format:"yyyy-mm-dd"});
585                         $('#gridValuationContext_DateFrom').val(_w.valuation.dFrom);
586                         $('#gridValuationContext_DateTo').val(_w.valuation.dTo);
587                         App.Event.listen('valuationGridSuFilter', 'SuChange', $.proxy(_w.valuation.clearValuationGridContext, this), 'Recurring');
588
589                         _w.valuation.publishing = false;
590                         //setTimeout(_w.valuation.refreshGrid, 120000);
591
592                 },
593
594                 refreshGrid : function()
595                 {
596                         return;
597                         if ('valuations' != App.activePage)
598                         {
599                                 return;
600                         }
601                         _w.valuation.loadValuationGrid();
602                         setTimeout(_w.valuation.refreshGrid, 120000);
603                 },
604
605                 pageValuationGrid : function( page )
606                 {
607                         _w.valuation.loadValuationGrid( {}, {"Grid":{"Page":page}} );
608                 },
609
610                 loadValuationGrid : function( contract, data, options )
611                 {
612
613                         if ($('#gridValuationContext_Archived').val() == 'excludingArchived')
614                         {
615                                 $('#gridValuationContext_notArchived').val('Archived,Stock');
616                                 if (undefined == data)
617                                 {
618                                         data = {};
619                                 }
620                                 if (undefined == data.Grid)
621                                 {
622                                         data.Grid = {};
623                                 }
624                                 if (undefined == data.Grid.Filter)
625                                 {
626                                         data.Grid.Filter = {};
627                                 }
628                                 data.Grid.Filter['valuation.jobState'] = 'NOT IN Archived,Stock';
629                         }
630                         //This comes back undefined if you are from a different page back to valuations
631         /*              if ($('#gridValuationContext_Archived').val() == undefined)
632                         {
633                                 data.Grid.Filter['valuation.jobState'] = 'NOT IN Archived,Stock';
634                         }
635 */
636                         App.API.execTask(
637
638                                         'gridValuation', data, options,
639                                         _w.valuation._onValuationGridDataReceived, _w.taskExecError
640                                         );
641
642                 },
643
644                 _onValuationGridDataReceived : function( response )
645                 {
646                         _w.valuation.gridStatus = 'Active';
647                         _w.valuation.ti.hydrateParam('gridRowRepeater', {});
648                         var statuses = {
649                                 "New Valuation": true,
650                                 "Pending Valuation": true,
651                                 "Complete Valuation": true,
652                                 "Offer Accepted": true,
653                                 "Deal Done": true,
654                                 "Deal Not Done": true,
655                                 "Price Guide": true,
656                                 "Archived": true
657                         };
658                         if (undefined != response.Data.Meta.Filters['valuation.jobState']
659                                 && undefined != statuses[response.Data.Meta.Filters['valuation.jobState']])
660                         {
661                                 _w.populateFilters(response.Data.Meta.Filters, {
662                                         'stock.vinNumber': 'gridValuationContext_VIN',
663                                         'stock.registrationNumber': 'gridValuationContext_RegNo',
664                                         'stock.referenceNumber': 'gridValuationContext_RefNo',
665                                         'valuation.familyName': 'gridValuationContext_Name',
666                                         'make.id': 'gridValuationFilter_Make',
667                                         'model.name': 'gridValuationFilter_Model',
668                                         'type.name': 'gridValuationFilter_Type',
669                                         'vehicleYear.name': 'gridValuationFilter_Year',
670                                         'stock.km': 'gridValuationFilter_KM',
671                                         'stock.tradePrice': 'gridValuationFilter_Trade',
672                                         'stock.retailPrice': 'gridValuationFilter_Retail',
673                                         'stock.highestOffer': 'gridValuationFilter_Offer',
674                                         'salesProfile.id': 'gridValuationFilter_Sales',
675                                         'managerProfile.id': 'gridValuationFilter_Manager',
676                                         'valuation.jobState':  'gridValuationFilter_Status'
677                                 });
678                         }
679                         else
680                         {
681                                 _w.populateFilters(response.Data.Meta.Filters, {
682                                         'stock.vinNumber': 'gridValuationContext_VIN',
683                                         'stock.registrationNumber': 'gridValuationContext_RegNo',
684                                         'stock.referenceNumber': 'gridValuationContext_RefNo',
685                                         'valuation.familyName': 'gridValuationContext_Name',
686                                         'make.id': 'gridValuationFilter_Make',
687                                         'model.name': 'gridValuationFilter_Model',
688                                         'type.name': 'gridValuationFilter_Type',
689                                         'vehicleYear.name': 'gridValuationFilter_Year',
690                                         'stock.km': 'gridValuationFilter_KM',
691                                         'stock.tradePrice': 'gridValuationFilter_Trade',
692                                         'stock.retailPrice': 'gridValuationFilter_Retail',
693                                         'stock.highestOffer': 'gridValuationFilter_Offer',
694                                         'salesProfile.id': 'gridValuationFilter_Sales',
695                                         'managerProfile.id': 'gridValuationFilter_Manager'
696                                 });
697                         }
698 //valuation.jobState = "NOT IN Archived,Stock" >> excluding archived
699 //valuation.jobState = "Archived" >> only archived
700                         /*if (undefined == response.Data.Meta.Filters['valuation.jobState'])
701                         {
702                                 _w.valuation.statusMenuOverride = true;
703                                 $('#gridValuationContext_Archived').selectpicker('val', 'includingArchived');
704                         }
705                         else if ('NOT IN Archived,Stock' == response.Data.Meta.Filters['valuation.jobState'])
706                         {
707                                 _w.valuation.statusMenuOverride = true;
708                                 $('#gridValuationContext_Archived').selectpicker('val', 'excludingArchived');
709                         }
710                         else if ('Archived' == response.Data.Meta.Filters['valuation.jobState'])
711                         {
712                                 _w.valuation.statusMenuOverride = true;
713                                 $('#gridValuationContext_Archived').selectpicker('val', 'onlyArchived');
714                         }*/
715
716
717
718
719
720                         $('select.gridValuationFilter').each(function(i, elem) {
721                                 $(elem).selectpicker('refresh');
722                                 // Error: Resets status when status should be populated gridStockFilter
723                         });
724                         var gridData = [];
725                         for (var i = 0; i < response.Data.DataSet.length; i++)
726                         {
727                                 var row = response.Data.DataSet[i];
728                                 gridData.push({
729                                         lineClass: 'handy',
730                                         surl: (
731                                                         'New Valuation'         == row.jobState
732                                                         || 'Pending Valuation'  == row.jobState
733                                                         || 'Complete Valuation' == row.jobState
734                                                         || 'Deal Not Done'      == row.jobState
735                                                         || ('Price Guide' == row.jobState
736                                                                         && row.stock.numberOfOffers == 0)
737                                         )
738                                                 ? 'valuationview'
739                                                 : 'limitedvaluationview',
740                                         prepend: {
741                                                 Trade  : 'R ',
742                                                 Retail : 'R ',
743                                                 Offer  : 'R '
744                                         },
745                                         rowId: row.id,
746                                         pgId: row.stock.priceGuide.id,
747                                         offers:  row.stock.priceGuide && row.stock.priceGuide.offers
748                                                 ? row.stock.priceGuide.offers[0]
749                                                 : null,
750                                         bind: function(meta) {
751                                                 $('#row' +  meta.rowId).click(function(e){
752                                                         window.location='#/' + meta.surl + '?id=' + meta.rowId;
753                                                 });
754
755                                                 $('#ofr' + meta.rowId).click(function(e) {
756                                                         _w.PopUp = true;
757                                                         e.preventDefault();
758                                                         e.stopPropagation();
759                                                         if (!meta.pgId)
760                                                         {
761                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
762                                                                 $('#modalbody').html(modalbody);
763                                                                 return;
764                                                         }
765                                                         else
766                                                         {
767                                                                 $('#modalbody').html('<center>Loading offers...</center>');
768                                                                 $('#myModal').modal('toggle');
769                                                         }
770                                                         App.API.getTask(
771                                                                         'offerList', 'PriceGuide', 'Offer.List',
772                                                                         meta.pgId, {}, function(){
773                                                                                 App.API.execTask(
774                                                                                                 'offerList', {}, {},
775                                                                                                 function(response)
776                                                                                                 {
777                                                                                                         var offers = response.Data;
778                                                                                                         if (offers.length)
779                                                                                                         {
780                                                                                                                 var modalbody = '<table class="table table-bordered table-condensed table-striped">';
781                                                                                                                 modalbody += '<tr><td>Company</td><td>Date Offered</td><td>Amount</td></tr>';
782                                                                                                                 for (var x in offers)
783                                                                                                                 {
784                                                                                                                         var isOwn = offers[x].company.id == App.companyData.id
785                                                                                                                                 ? true
786                                                                                                                                 : false;
787                                                                                                                         modalbody +=
788                                                                                                                                 '<tr class="modalrow ' + (isOwn ? 'red' : 'black') + '">'
789                                                                                                                                 + '<td class="date">' + offers[x].company.name + '</td>'
790                                                                                                                                 + '<td class="date">' + offers[x].created + '</td>'
791                                                                                                                                 + '<td class="wide">' + offers[x].amount + '</td>'
792                                                                                                                                 + '</tr>';
793                                                                                                                 }
794                                                                                                                 modalbody += '</table>';
795                                                                                                         }
796                                                                                                         else
797                                                                                                         {
798                                                                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no offers!</div>';
799                                                                                                         }
800                                                                                                         $('#modalbody').html(modalbody);
801                                                                                                 }, _w.taskExecError
802                                                                                 );
803                                                                         }, _w.taskContractError
804                                                         );
805                                                 });
806                                         },
807                                         style: {
808                                                 Make            : 'make hidden-phone',
809                                                 Model           : 'hidden-phone',
810                                                 Type            : 'type',
811                                                 Year            : 'year',
812                                                 KM              : 'hidden-phone',
813                                                 Trade           : 'hidden-phone',
814                                                 Retail          : 'hidden-phone',
815                                                 Offer           : 'offer',
816                                                 Sales           : 'hidden-phone',
817                                                 Manager     : 'hidden-phone',
818                                                 Status          : 'status'
819
820
821                                         },
822                                         items: {
823                                                 'id'     : row.id,
824                                                 'Make'   : row.stock.type.model.make.name,
825                                                 'Model'  : row.stock.type.model.name,
826                                                 'Type'   : row.stock.type.name,
827                                                 'Year'   : row.stock.vehicleYear.name,
828                                                 'KM'     : row.stock.km,
829                                                 'Trade'  : row.stock.tradePrice,
830                                                 'Retail' : 'Dealership Sales' == App.permissions.name
831                                                                 ? row.stock.highestOffer
832                                                                 : row.stock.retailPrice,
833                                                 'Offer'  : 'Dealership Sales' == App.permissions.name
834                                                                         ? row.amountOffered
835                                                                         : (App.permissions.name == 'Dealership Manager (Used)' || App.permissions.name == 'Dealership Manager (New)'
836                                                                                         ? row.amountOffered + '&nbsp;&nbsp;&nbsp;'
837                                                                                         : row.stock.highestOffer + '&nbsp;&nbsp;&nbsp;')
838                                                                                 + '<a class="soda_pop " id="ofr' + row.id + '"><span>' + row.stock.numberOfOffers + '</span></a>',
839                                                 'Sales'  : row.salesProfile.firstName
840                                                                         ? row.salesProfile.firstName + ' ' + row.salesProfile.familyName
841                                                                         : '',
842                                                 'Manager': row.managerProfile.firstName
843                                                                         ? row.managerProfile.firstName + ' ' + row.managerProfile.familyName
844                                                                         : '',
845                                                 'Status' : row.jobState
846                                         }
847                                 });
848                         }
849
850                         _w.valuation.ti.hydrateParam('gridRowRepeater', {
851                                 constructor : 'GridDataRow',
852                                 items       : gridData
853                         });
854                         response.Data.Meta.itemName = 'stockPager';
855                         response.Data.Meta.bind = _w.valuation.pageValuationGrid;
856                         _w.valuation.ti.hydrateParam('gridPager', {
857                                 constructor : 'GridPager',
858                                 items       : [response.Data.Meta]
859                         });
860
861                         // todo: add pager hydration
862                 },
863
864                 valuationReport : function()
865                 {
866                         window.location.hash = '/valuationreport';
867                 },
868
869                 exportValuationGrid : function()
870                 {
871                         App.API.execTask(
872                                         'gridValuation', {}, {'ExportToExcel': true},
873                                         _w.valuation._onExportReceived, _w.taskExecError,
874                                         'EXPORT', true
875                                         );
876                 },
877
878                 newValuation : function()
879                 {
880                         window.location.hash = '/valuationview?id=0';
881                 },
882
883                 _onExportReceived : function( response )
884                 {
885                         var win = window.open();
886             win.document.write(response);
887                 },
888
889                 clearValuationGridContext : function()
890                 {
891                         _w.valuation.gridStatus = 'Inactive';
892                         try
893                         {
894                                 $('select.gridValuationFilter').each(function(i, elem) {
895                                         $(elem).val('');
896                                         $(elem).selectpicker('refresh');
897
898                                 });
899                         }
900                         catch(e)
901                         {
902
903                         }
904                         $('.gridValuationContext').val('');
905                         $('.gridValuationFilter').val('');
906                         $('#gridValuationContext_notArchived').val('Archived,Stock');
907
908                         _w.valuation.dFrom = '';
909                         _w.valuation.dTo   = '';
910                         _w.valuation.searchValuationGrid(true);
911
912
913                 },
914
915                 searchValuationGrid : function(resetOrder)
916                 {
917                         if ($('#gridValuationFilter_Status').val() == 'excludingArchived' ){
918                                 $('#gridValuationContext_notArchived').val('Archived,Stock');
919                         }
920                         _w.valuation.gridStatus = 'Inactive';
921                         var filter = {
922                                         count: 0,
923                                         filters: {
924
925
926                                                 }
927                         };
928
929                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_VIN', 'stock.vinNumber');
930                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_RegNo', 'stock.registrationNumber');
931                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_RefNo', 'stock.referenceNumber');
932                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_Name', 'valuation.familyName');
933                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_DateFrom', 'stock.created', '>=');
934                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_DateTo', 'stock.created', '<=', ' 23:59:59');
935                         filter = _w.filterIfnotEmpty(filter, 'gridValuationContext_notArchived', 'valuation.jobState', 'NOT IN');
936
937                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Make', 'make.id');
938                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Model', 'model.name');
939                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Type', 'type.name');
940                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Year', 'vehicleYear.name');
941                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_KM', 'stock.km');
942                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Trade', 'stock.tradePrice');
943                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Retail', 'stock.retailPrice');
944                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Offer', 'stock.highestOffer');
945                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Sales', 'salesProfile.id');
946                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Manager', 'managerProfile.id');
947                         filter = _w.filterIfnotEmpty(filter, 'gridValuationFilter_Status', 'valuation.jobState');
948
949                         var request = {"Grid":{
950                             "Page": 1,
951                             "Filter": filter.filters
952                           }};
953                         if (resetOrder)
954                         {
955                                 request.Grid.OrderBy = {'stock.created': 'DESC'};
956                         }
957
958                         _w.valuation.loadValuationGrid( null, request, {} );
959                 },
960
961                 orderValuationGrid : function(field, direction)
962                 {
963                         var order = {};
964                         order[field] = direction;
965                         _w.valuation.loadValuationGrid( {}, {"Grid":{"Page": 1, "OrderBy": order}} );
966                 }
967
968         };
969
970 })();