initial commit
[namibia] / public / js / app / portal-workspace / valuationview-branch.js
1 ;(function(){
2
3         window._w.valuationview = {
4
5                 itemId : null,
6                 itemData : null,
7                 formMeta : null,
8                 ti : null,
9                 limited : false,
10                 newItem : false,
11                 validations : 0,
12                 highestOfferId : false,
13                 priceGuideId : false,
14
15                 onValuationViewTemplateReady : function( template, event, eventData )
16                 {
17                         _w.valuationview.itemData = null;
18                         _w.valuationview.ti = _t[template];
19                         _w.valuationview.limited = ('limitedvaluationview' == template)
20                                 ? true
21                                 : false;
22
23                         App.DataStore.setData('departments', [{"value":"New", "label":"New"}, {"value":"Used", "label":"Used"}]);
24                         App.DataStore.setData('sparekeys', [{"value":"1", "label":"Yes"}, {"value":"0", "label":"No"}]);
25                         App.DataStore.loadSelectListData('years', true, 'Stock', 'Year.SelectList');
26                         App.DataStore.loadSelectListData('categories', true, 'Stock', 'Category.SelectList');
27                         App.DataStore.loadSelectListData('conditions', true, 'Stock', 'Condition.SelectList');
28                         App.DataStore.loadSelectListData('exteriorColours', true, 'Stock', 'ExteriorColour.SelectList');
29                         App.DataStore.loadSelectListData('interiorColours', true, 'Stock', 'InteriorColour.SelectList');
30                         App.DataStore.loadSelectListData('upholsteries', true, 'Stock', 'Upholstery.SelectList');
31                         App.DataStore.loadSelectListData('papers', true, 'Stock', 'Paper.SelectList');
32                         App.DataStore.loadSelectListData('natis', true, 'Stock', 'Natis.SelectList');
33                         App.DataStore.loadSelectListData('fsh', true, 'Stock', 'FullServiceHistory.SelectList');
34                         App.DataStore.loadSelectListData('fuelTypes', true, 'Stock', 'FuelType.SelectList');
35                         App.DataStore.loadSelectListData('transmissionTypes', true, 'Stock', 'TransmissionType.SelectList');
36                         App.DataStore.loadSelectListData('damages', true, 'Stock', 'Damage.SelectList');
37                         App.DataStore.loadSelectListData('accessories', true, 'Stock', 'Accessory.SelectList');
38                         App.DataStore.loadSelectListData('clubs', true, 'PriceGuide', 'Club.SelectList');
39                         App.DataStore.loadSelectListData('valuators', true, 'User', 'Profile.ValuatorList');
40                         App.DataStore.loadSelectListData('sales', true, 'User', 'Profile.SalesList');
41
42
43
44                         _w.valuationview.setValuationViewFormMeta();
45                         _w.valuationview.ti.hydrate(_w.valuationview.formMeta);
46                         // Retrieve initial view data.
47                         _w.valuationview.itemId = App.Util.getUrlParam('id');
48                         if (0 == _w.valuationview.itemId || null == _w.valuationview.itemId)
49                         {
50                                 _w.valuationview.newItem = true;
51                                 _w.valuationview.itemData = {stock:{}};
52                                 App.API.getTask(
53                                         'createValuationNew', 'Valuation', 'Valuation.CreateNew',
54                                         null, {}, function(){}, _w.taskContractError
55                                 );
56                                 App.API.getTask(
57                                         'createValuationPending', 'Valuation', 'Valuation.CreatePending',
58                                         null, {}, function(){}, _w.taskContractError
59                                 );
60                                 App.API.getTask(
61                                         'createValuationComplete', 'Valuation', 'Valuation.CreateComplete',
62                                         null, {}, function(){}, _w.taskContractError
63                                 );
64                                 App.API.getTask(
65                                         'createValuationPriceGuide', 'Valuation', 'Valuation.CreatePriceGuide',
66                                         null, {}, function(){}, _w.taskContractError
67                                 );
68                                 App.API.getTask(
69                                         'createValuationStock', 'Valuation', 'Valuation.CreateStock',
70                                         null, {}, function(){}, _w.taskContractError
71                                 );
72
73                         }
74                         else
75                         {
76                                 _w.valuationview.newItem = false;
77                                 var task = _w.valuationview.limited
78                                         ? 'Valuation.LimitedUpdate'
79                                         : 'Valuation.Update';
80                                 App.API.getTask(
81                                         'updateValuation:' + _w.valuationview.itemId, 'Valuation', task, _w.valuationview.itemId,
82                                         {}, $.proxy( _w.valuationview._onValuationViewDataReceived, this ), _w.taskContractError
83                                 );
84                         }
85
86                 },
87
88                 onValuationViewPublished : function( template, event, eventData )
89                 {
90                         _w.valuationview.limited = ('limitedvaluationview' == template)
91                                 ? true
92                                 : false;
93
94                         if (_w.valuationview.newItem)
95                         {
96                                 _w.valuationview._setupValidation();
97                                 $('#btnSaveAsSales').show();
98                                 $('#btnSaveAsPending').show();
99                                 $('#optSendToSales').show();
100                                 $('#optSendToPg').show();
101                                 $('#btnSendToStock').show();
102                                 $('#btnUpdate').prop('disabled', false);
103                                 $('#printValuationPdf').hide();
104
105
106                                 /*
107                                  * Permissions
108                                  */
109                                 true == App.permissions.valuationCreateSales ? $('#sectionSelectValuator').show() : $('#sectionSelectValuator').hide();
110                                 true == App.permissions.valuationCreatePriceguide ? $('#optSendToPg').show() : $('#optSendToPg').hide();
111                                 true == App.permissions.valuationCreateComplete ? $('#optSendToSales').show() : $('#optSendToSales').hide();
112                                 true == App.permissions.valuationCreateNew ? $('#btnUpdate').show() : $('#btnUpdate').hide();
113                                 true == App.permissions.valuationCreateStock ? $('#btnSendToStock').show() : $('#btnSendToStock').hide();
114                                 true == App.permissions.valuationCreatePending ? $('#btnSaveAsPending').show() : $('#btnSaveAsPending').hide();
115
116                                 true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_overAllowance').show() : $('#' + _w.valuationview.ti.tid  + '_overAllowance').hide();
117                                 true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_standInValue').show() : $('#' + _w.valuationview.ti.tid  + '_standInValue').hide();
118                                 true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_projectedRetail').show() : $('#' + _w.valuationview.ti.tid  + '_projectedRetail').hide();
119                                 true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_plannedMargin').show() : $('#' + _w.valuationview.ti.tid  + '_plannedMargin').hide();
120
121                                 if (!App.permissions.valuationUpdateDamages)
122                                 {
123                                         $('.damages-form').hide();
124                                 }
125                                 if (!App.permissions.valuationUploadPhotos)
126                                 {
127                                         $('.photos-form').hide();
128                                 }
129                         }
130                         $('#btnUpdate').prop('disabled', true);
131                         $('#btnFetchTu').click(function () {
132                                 if (!$('#vehicleYear').val()
133                                         || !$('#vehicleType').val())
134                                 {
135                                         alert('Vehicle year and type is required to fetch Trade and Retail pricing.');
136                                         return;
137                                 }
138                                 $('#btnFetchTu').prop('disabled', true);
139                                 App.API.getTask(
140                                                 'TransUnionFromMmCode', 'Stock', 'Utility.TransUnionFromMmCode', null,
141                                                 {}, _w.valuationview._onTuReady, _w.taskContractError
142                                 );
143                         });
144                         $('#doAcceptOffer').click(function () {
145                                 if (!$('#frmValuationView').valid())
146                                 {
147                                         return;
148                                 }
149                                 /*
150                                 highestOfferId : false,
151                                 priceGuideId : false, */
152                                 $('#btnUpdate').prop('disabled', true);
153                                 $('#btnSendToDealDone').prop('disabled', true);
154                                 $('#btnSendToDealNotDone').prop('disabled', true);
155                                 var data = _w.valuationview._prepValuationUpdateData();
156                                 App.API.execTask(
157                                         'updateValuation:' + _w.valuationview.itemId, data, {},
158                                         function() {
159                                                 App.API.getRoute(
160                                                                 'offerAcceptedOnValuation:' + _w.valuationview.priceGuideId, 'PriceGuide', 'Offer.AcceptOffer', _w.valuationview.priceGuideId,
161                                                                 {id: _w.valuationview.highestOfferId}, function() {
162                                                                         App.API.execRoute(
163                                                                                         'offerAcceptedOnValuation:' + _w.valuationview.priceGuideId, {}, {},
164                                                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
165                                                                         );
166                                                                 }, _w.taskContractError
167                                                 );
168                                         }, _w.taskExecError
169                                 );
170
171                         });
172                         $('#btnSendToDealNotDone').click(function () {
173                                 if (!$('#frmValuationView').valid())
174                                 {
175                                         return;
176                                 }
177                                 $('#btnUpdate').prop('disabled', true);
178                                 $('#btnSendToDealDone').prop('disabled', true);
179                                 $('#btnSendToDealNotDone').prop('disabled', true);
180                                 var data = _w.valuationview._prepValuationUpdateData();
181                                 App.API.execTask(
182                                         'updateValuation:' + _w.valuationview.itemId, data, {},
183                                         function() {
184                                                 App.API.directRoute(
185                                                                 'dealnotdoneonValuation:' + _w.valuationview.itemId,
186                                                                 'Valuation', 'Valuation.MoveToDealNotDone', _w.valuationview.itemId, {},
187                                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
188                                                 );
189                                         }, _w.taskExecError
190                                 );
191
192                         });
193                         $('#btnArchive').click(function () {
194                                 App.API.directRoute(
195                                                 'archiveValuation:' + _w.valuationview.itemId,
196                                                 'Valuation', 'Valuation.MoveToArchive', _w.valuationview.itemId, {},
197                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
198                                 );
199                         });
200                         $('#btnUnarchive').click(function () {
201                                 App.API.directRoute(
202                                                 'unarchiveValuation:' + _w.valuationview.itemId,
203                                                 'Valuation', 'Valuation.UnArchive', _w.valuationview.itemId, {},
204                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
205                                 );
206                         });
207                         $('#btnSaveAsPending').click(function () {
208                                 if (!$('#frmValuationView').valid())
209                                 {
210                                         return;
211                                 }
212                                 $('#btnUpdate').prop('disabled', true);
213                                 $('#btnSendToSales').prop('disabled', true);
214                                 $('#btnSaveAsPending').prop('disabled', true);
215                                 $('#doSendToStock').prop('disabled', true);
216                                 $('#btnSendToPg').prop('disabled', true);
217                                 if (_w.valuationview.newItem)
218                                 {
219                                         var data = _w.valuationview._prepValuationUpdateData();
220                                         App.API.execTask(
221                                                 'createValuationPending', data, {},
222                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
223                                         );
224                                 }
225                                 else
226                                 {
227                                         _w.valuationview._routeToPending();
228                                 }
229                         });
230                         $('#btnSaveAsSales').click(function () {
231                                 var valuators = $('#valuators').val();
232                                 if (null == valuators)
233                                 {
234                                         alert('Please select at least one valuator.');
235                                         return;
236                                 }
237                                 if (!$('#frmValuationView').valid())
238                                 {
239                                         return;
240                                 }
241                                 $('#btnUpdate').prop('disabled', true);
242                                 $('#btnSendToSales').prop('disabled', true);
243                                 $('#btnSaveAsPending').prop('disabled', true);
244                                 $('#doSendToStock').prop('disabled', true);
245                                 $('#btnSendToPg').prop('disabled', true);
246                                 if (_w.valuationview.newItem)
247                                 {
248                                         var data = _w.valuationview._prepValuationUpdateData();
249                                         var valuators = $('#valuators').val();
250                                         data.Valuation.valuators = [];
251                                         for (var i = 0; i < valuators.length; i++)
252                                         {
253                                                 data.Valuation.valuators.push({id: valuators[i]});
254                                         }
255                                         App.API.execTask(
256                                                 'createValuationNew', data, {},
257                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
258                                         );
259                                 }
260                                 else
261                                 {
262                                         var data = _w.valuationview._prepValuationUpdateData();
263                                         var valuators = $('#valuators').val();
264                                         data.Valuation.valuators = [];
265                                         for (var i = 0; i < valuators.length; i++)
266                                         {
267                                                 data.Valuation.valuators.push({id: valuators[i]});
268                                         }
269                                         App.API.execTask(
270                                                 'updateValuation:' + _w.valuationview.itemId, data, {},
271                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
272                                         );
273                                 }
274                         });
275                         $('#btnSendToSales').click(function () {
276                                 var profile = $('#salesProfile').val();
277                                 var amount  = $('#salesAmountOffered').val();
278                                 if (null == profile || '' == amount)
279                                 {
280                                         alert('Please select Sales person and enter an Amount.');
281                                         return;
282                                 }
283                                 if (!$('#frmValuationView').valid())
284                                 {
285                                         return;
286                                 }
287                                 $('#btnUpdate').prop('disabled', true);
288                                 $('#btnSendToSales').prop('disabled', true);
289                                 $('#btnSaveAsPending').prop('disabled', true);
290                                 $('#doSendToStock').prop('disabled', true);
291                                 $('#btnSendToPg').prop('disabled', true);
292                                 if (_w.valuationview.newItem)
293                                 {
294                                         var data = _w.valuationview._prepValuationUpdateData();
295                                         data.Valuation.amountOffered = amount;
296                                         App.API.execTask(
297                                                         'createValuationComplete', data, {},
298                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
299                                                 );
300                                 }
301                                 else
302                                 {
303                                         if ('Price Guide' == _w.valuationview.itemData.jobState)
304                                         {
305                                                 App.API.getTask(
306                                                                 'sendToSalesFull:' + _w.valuationview.itemId, 'Valuation', 'Valuation.SendToSalesFull', _w.valuationview.itemId,
307                                                                 {}, function() {
308                                                                         var data = _w.valuationview._prepValuationUpdateData();
309                                                                         data.Valuation.amountOffered = $('#salesAmountOffered').val();
310                                                                         App.API.execTask(
311                                                                                         'sendToSalesFull:' + _w.valuationview.itemId, data, {},
312                                                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
313                                                                                 );
314                                                                 }, _w.taskContractError
315                                                         );
316                                         }
317                                         else
318                                         {
319                                                 _w.valuationview._routeToComplete();
320                                         }
321                                 }
322                         });
323                         $('#doSendToStock').click(function () {
324                                 var stockNo = $('#stockNumber').val();
325                                 if (null == stockNo || '' == stockNo)
326                                 {
327                                         alert('Please enter a stock number.');
328                                         return;
329                                 }
330                                 if (!$('#frmValuationView').valid())
331                                 {
332                                         return;
333                                 }
334                                 $('#btnUpdate').prop('disabled', true);
335                                 $('#btnSendToSales').prop('disabled', true);
336                                 $('#btnSaveAsPending').prop('disabled', true);
337                                 $('#doSendToStock').prop('disabled', true);
338                                 $('#btnSendToPg').prop('disabled', true);
339                                 if (_w.valuationview.newItem)
340                                 {
341                                         var data = _w.valuationview._prepValuationUpdateData();
342                                         App.API.execTask(
343                                                 'createValuationStock', data, {},
344                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
345                                         );
346                                 }
347                                 else
348                                 {
349                                         _w.valuationview._routeToStock();
350                                 }
351                         });
352                         $('#btnSendToPg').click(function () {
353                                 var clubs = $('#clubs').val();
354                                 if (null == clubs)
355                                 {
356                                         alert('Please select at least one club.');
357                                         return;
358                                 }
359                                 if (!$('#frmValuationView').valid())
360                                 {
361                                         return;
362                                 }
363                                 $('#btnUpdate').prop('disabled', true);
364                                 $('#btnSendToSales').prop('disabled', true);
365                                 $('#btnSaveAsPending').prop('disabled', true);
366                                 $('#doSendToStock').prop('disabled', true);
367                                 $('#btnSendToPg').prop('disabled', true);
368                                 if (_w.valuationview.newItem)
369                                 {
370                                         var data = _w.valuationview._prepValuationUpdateData();
371                                         data.Context = {clubs:[]};
372                                         var clubList = $('#clubs').val();
373                                         for (var i = 0; i < clubList.length; i++)
374                                         {
375                                                 data.Context.clubs.push({id: clubList[i]});
376                                         }
377                                         App.API.execTask(
378                                                 'createValuationPriceGuide', data, {},
379                                                 _w.valuationview._onValuationUpdated, _w.taskExecError
380                                         );
381                                 }
382                                 else
383                                 {
384                                         _w.valuationview._routeToPg();
385                                 }
386                         });
387
388                         $('.back-btn').on('click', function(){
389                                 window.history.back();
390                         });
391
392                         if (!_w.valuationview.newItem && null != _w.valuationview.itemData)
393                         {
394                                 _w.valuationview._onDataAndTemplate();
395                         }
396                 },
397
398                 _routeToPg : function()
399                 {
400                         App.API.getRoute(
401                                 'sendToPg:' + _w.valuationview.itemId, 'Valuation', 'Valuation.SendToPriceGuide', _w.valuationview.itemId,
402                                 {}, function() {
403                                         var data = _w.valuationview._prepValuationUpdateData();
404                                         data.Context = {clubs:[]};
405                                         var clubList = $('#clubs').val();
406                                         for (var i = 0; i < clubList.length; i++)
407                                         {
408                                                 data.Context.clubs.push({id: clubList[i]});
409                                         }
410                                         App.API.execRoute(
411                                                         'sendToPg:' + _w.valuationview.itemId, data, {},
412                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
413                                         );
414                                 }, _w.taskContractError
415                         );
416                 },
417
418                 _routeToPending : function()
419                 {
420                         App.API.getRoute(
421                                 'sendToPending:' + _w.valuationview.itemId, 'Valuation', 'Valuation.MoveToPending', _w.valuationview.itemId,
422                                 {}, function() {
423                                         var data = _w.valuationview._prepValuationUpdateData();
424                                         App.API.execRoute(
425                                                         'sendToPending:' + _w.valuationview.itemId, data, {},
426                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
427                                         );
428                                 }, _w.taskContractError
429                         );
430                 },
431
432                 _routeToComplete : function()
433                 {
434                         App.API.getRoute(
435                                 'sendToComplete:' + _w.valuationview.itemId, 'Valuation', 'Valuation.MoveToComplete', _w.valuationview.itemId,
436                                 {}, function() {
437                                         var data = _w.valuationview._prepValuationUpdateData();
438                                         data.Valuation.amountOffered = $('#salesAmountOffered').val();
439                                         App.API.execRoute(
440                                                         'sendToComplete:' + _w.valuationview.itemId, data, {},
441                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
442                                         );
443                                 }, _w.taskContractError
444                         );
445                 },
446
447                 _routeToStock : function()
448                 {
449                         App.API.getRoute(
450                                 'sendToStock:' + _w.valuationview.itemId, 'Valuation', 'Valuation.SendToStock', _w.valuationview.itemId,
451                                 {}, function() {
452                                         var data = _w.valuationview._prepValuationUpdateData();
453                                         App.API.execRoute(
454                                                         'sendToStock:' + _w.valuationview.itemId, data, {},
455                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
456                                         );
457                                 }, _w.taskContractError
458                         );
459                 },
460
461
462
463
464                 setValuationViewFormMeta : function( data )
465                 {
466
467                         var damages = {};
468
469                         if (undefined != data && undefined != data.stock)
470                         {
471                                 for (var i = 0; i < data.stock.damages.length; i++)
472                                 {
473                                         damages['d' + data.stock.damages[i].damage] = data.stock.damages[i].amount;
474                                 }
475                         }
476                         else
477                         {
478                                 data = {"stock":{}};
479                         }
480
481                                 _w.valuationview.formMeta = {
482                                 'vehicleYear' : {
483                                         type        : 'select',
484                                         id          : 'vehicleYear',
485                                         title       : 'Year',
486                                         dataStoreId : 'years',
487                                         value       : data.stock.vehicleYear ? data.stock.vehicleYear : '',
488                                         onChange    : function () {
489                                                 if (!App.DataStore.getItem('BuildSelect:vehicleYear', false))
490                                                 {
491                                                         _w.valuationview._listMakesModelsTypes(false, function () {
492                                                                 if ($('#vehicleYear').val() && $('#vehicleType').val())
493                                                                 {
494                                                                         App.API.getTask(
495                                                                                         'TransUnionFromMmCode', 'Stock', 'Utility.TransUnionFromMmCode', null,
496                                                                                         {}, _w.valuationview._onTuReady, _w.taskContractError
497                                                                         );
498                                                                 }
499                                                         });
500                                                 }
501                                         }
502                                 },
503                                 'category' : {
504                                         type        : 'select',
505                                         id          : 'vehicleCategory',
506                                         title       : 'Category',
507                                         dataStoreId : 'categories',
508                                         value       : data.stock.type ? data.stock.type.category.id : '',
509                                         onChange    : function () {
510                                                 
511                                                 
512                                                 if ($(this).val() == 8 && _w.valuationview.itemId == 0) {
513                                                         // show the hidden input fields
514                                                         $("#manualVehicle").show();
515                                                         $("#selectionVehicle").hide();
516                                                         $('#mmCode').html("Other");
517                                                 }
518                                                 if ($(this).val() != 8 && _w.valuationview.itemId != 0) {
519                                                         // show the hidden input fields
520                                                         $("#manualVehicle").hide();
521                                                         $("#selectionVehicle").show();
522                                                         $('#mmCode').html("");
523                                                 }
524                                                 
525                                                 if (!App.DataStore.getItem('BuildSelect:vehicleCategory', false) && $(this).val() != 8)
526                                                 {
527                                                         _w.valuationview._listMakesModelsTypes();
528                                                 }
529                                         }
530                                 },
531                                 'make' : {
532                                         type        : 'select',
533                                         id          : 'vehicleMake',
534                                         title       : 'Vehicle Make',
535                                         dataStoreId : 'makes',
536                                         value       : data.stock.type ? data.stock.type.model.make.id : '',
537                                         onChange    : function () {
538                                                 if (!App.DataStore.getItem('BuildSelect:vehicleMake', false))
539                                                 {
540                                                         _w.valuationview._listMakesModelsTypes();
541                                                 }
542                                         }
543                                 },
544                                 'model' : {
545                                         type        : 'select',
546                                         id          : 'vehicleModel',
547                                         title       : 'Vehicle Model',
548                                         dataStoreId : 'models',
549                                         value       : data.stock.type ? data.stock.type.model.id : '',
550                                         onChange    : function () {
551                                                 if (!App.DataStore.getItem('BuildSelect:vehicleModel', false))
552                                                 {
553                                                         _w.valuationview._listMakesModelsTypes();
554                                                 }
555                                         }
556                                 },
557                                 'type' : {
558                                         type        : 'select',
559                                         id          : 'vehicleType',
560                                         title       : 'Vehicle Type',
561                                         dataStoreId : 'types',
562                                         value       : data.stock.type ? data.stock.type.id : '',
563                                         onChange    : function () {
564                                                 if ('' != $(this).val() && null != $(this).val())
565                                                 {
566                                                         App.API.getTask(
567                                                                         'GetMmCode' + $(this).val(), 'Stock', 'Type.GetMmCode', null,
568                                                                         {id:$(this).val()}, function(response){
569                                                                                 $('#mmCode').html(response.Data.mmCode);
570                                                                         }, _w.taskContractError
571                                                         );
572                                                 }
573                                                 else
574                                                 {
575                                                         $('#mmCode').html('');
576                                                 }
577                                                 if (!App.DataStore.getItem('BuildSelect:vehicleType', false)
578                                                         && $('#vehicleYear').val()
579                                                         && $('#vehicleType').val())
580                                                 {
581                                                         $('#btnFetchTu').prop('disabled', true);
582                                                         App.API.getTask(
583                                                                         'TransUnionFromMmCode', 'Stock', 'Utility.TransUnionFromMmCode', null,
584                                                                         {}, _w.valuationview._onTuReady, _w.taskContractError
585                                                         );
586                                                 }
587                                         }
588                                 },
589                                 
590                                 
591                                 'makeManual' : {
592                                         type        : 'input',
593                                         id          : 'vehicleMakeManual',
594                                         title       : 'Vehicle Make',
595                                         //dataStoreId : 'makes',
596                                         value       : data.stock.type ? data.stock.type.model.make.id : '',
597                                         
598                                 },
599                                 'modelManual' : {
600                                         type        : 'input',
601                                         id          : 'vehicleModelManual',
602                                         title       : 'Vehicle Model',
603                                         //dataStoreId : 'models',
604                                         value       : data.stock.type ? data.stock.type.model.id : '',
605                                         
606                                 },
607                                 'typeManual' : {
608                                         type        : 'input',
609                                         id          : 'vehicleTypeManual',
610                                         title       : 'Vehicle Type',
611                                         //dataStoreId : 'types',
612                                         value       : data.stock.type ? data.stock.type.id : '',
613                                 
614                                 },
615                                 
616                                 
617                                 
618                                 'mmCode' : {
619                                         type        : 'input',
620                                         id          : 'mmCode',
621                                         title       : 'MM Code',
622                                         style           : 'disable',
623                                         setAttrib       : 'readonly',
624                                         value       : data.stock.type ? data.stock.type.mmCode : ''
625                                 },
626                                 'fuelType' : {
627                                         type        : 'select',
628                                         id          : 'vehicleFuelType',
629                                         title       : 'Fuel Type',
630                                         dataStoreId : 'fuelTypes',
631                                         value       : data.stock.fuelType ? data.stock.fuelType : ''
632                                 },
633                                 'transmissionType' : {
634                                         type        : 'select',
635                                         id          : 'vehicleTransmissionType',
636                                         title       : 'Transmission Type',
637                                         dataStoreId : 'transmissionTypes',
638                                         value       : data.stock.transmissionType ? data.stock.transmissionType : ''
639                                 },
640                                 'registrationNumber' : {
641                                         type  : 'input',
642                                         id    : 'registrationNumber',
643                                         title : 'Registration no',
644                                         value : data.stock.registrationNumber ? data.stock.registrationNumber : ''
645                                 },
646                                 'vinNumber' : {
647                                         type  : 'input',
648                                         id    : 'vinNumber',
649                                         title : 'Vin no',
650                                         value : data.stock.vinNumber ? data.stock.vinNumber : ''
651                                 },
652                                 'engineNumber' : {
653                                         type  : 'input',
654                                         id    : 'engineNumber',
655                                         title : 'Eng. no',
656                                         value : data.stock.engineNumber ? data.stock.engineNumber : ''
657                                 },
658                                 'km' : {
659                                         type  : 'input',
660                                         id    : 'km',
661                                         title : 'Kms',
662                                         value : data.stock.km ? data.stock.km : ''
663                                 },
664                                 'condition' : {
665                                         type        : 'select',
666                                         id          : 'vehicleCondition',
667                                         title       : 'Condition',
668                                         dataStoreId : 'conditions',
669                                         value       : data.stock.condition ? data.stock.condition : ''
670                                 },
671                                 'exteriorColour' : {
672                                         type        : 'select',
673                                         id          : 'exteriorColour',
674                                         title       : 'Main ext. colour',
675                                         dataStoreId : 'exteriorColours',
676                                         value       : data.stock.exteriorColour ? data.stock.exteriorColour : ''
677                                 },
678                                 'interiorColour' : {
679                                         type  : 'select',
680                                         id    : 'interiorColour',
681                                         title : 'Main int. colour',
682                                         dataStoreId : 'interiorColours',
683                                         value : data.stock.interiorColour ? data.stock.interiorColour : ''
684                                 },
685                                 'upholstery' : {
686                                         type        : 'select',
687                                         id          : 'vehicleUpholstery',
688                                         title       : 'Upholstery',
689                                         dataStoreId : 'upholsteries',
690                                         value       : data.stock.upholstery ? data.stock.upholstery : ''
691                                 },
692                                 'papers' : {
693                                         type        : 'select',
694                                         id          : 'vehiclePapers',
695                                         title       : 'Papers',
696                                         dataStoreId : 'papers',
697                                         value       : data.stock.papers ? data.stock.papers : ''
698                                 },
699                                 'natis' : {
700                                         type        : 'select',
701                                         id          : 'vehicleNatis',
702                                         title       : 'Natis',
703                                         dataStoreId : 'natis',
704                                         value       : data.stock.natis ? data.stock.natis : ''
705                                 },
706                                 'spareKeys' : {
707                                         type        : 'select',
708                                         id          : 'spareKeys',
709                                         title       : 'Spare keys',
710                                         dataStoreId : 'sparekeys',
711                                         value       : undefined != data.stock.spareKeys && null != data.stock.spareKeys
712                                                                         ? (data.stock.spareKeys == true ? "1" : "0")
713                                                                         : null
714                                 },
715                                 'fullServiceHistory' : {
716                                         type        : 'select',
717                                         id          : 'fullServiceHistory',
718                                         title       : 'Full service history',
719                                         dataStoreId : 'fsh',
720                                         value       : data.stock.fullServiceHistory ? data.stock.fullServiceHistory : ''
721                                 },
722                                 'fshNotes' : {
723                                         type  : 'textarea-small',
724                                         id    : 'fshNotes',
725                                         title : 'FSH notes',
726                                         value : data.stock.fshNotes ? data.stock.fshNotes : ''
727                                 },
728                                 'damages' : {
729                                         type        : 'damageDetails',
730                                         disabled        : !App.permissions.valuationUpdateDamages,
731                                         id          : 'damages',
732                                         dataStoreId : 'damages',
733                                         data        : data.stock.damages ? data.stock.damages : {}
734                                 },
735                                 'damageTotal' : 'R 0.00',
736                                 'damageNotes' : {
737                                         type  : 'textarea-styled',
738                                         disabled        : !App.permissions.valuationUpdateDamages,
739                                         id    : 'damageNotes',
740                                         title : 'Damages comments',
741                                         style : 'damages-comments',
742                                         value : data.stock.damageNotes ? data.stock.damageNotes : ''
743                                 },
744
745                                 'previousRepairsNoted' : {
746                                         type  : 'checkbox',
747                                         id    : 'previousRepairsNoted',
748                                         title : 'Previous repairs',
749                                         style : 'damages-comments',
750                                         value : data.stock.previousRepairsNoted ? data.stock.previousRepairsNoted : false
751                                 },
752                                 'previousRepairsNotes' : {
753                                         type  : 'textarea-styled',
754                                         disabled        : !App.permissions.valuationUpdateDamages,
755                                         id    : 'previousRepairsNotes',
756                                         title : 'Previous repairs notes',
757                                         style : 'damages-comments',
758                                         value : data.stock.previousRepairsNotes ? data.stock.previousRepairsNotes : '',
759                                         onChange: function(){
760                                                 var haveData = ('' != $(this).val() ? true : false);
761                                                 $('#previousRepairsNoted').prop('checked', haveData);
762                                         }
763                                 },
764                                 'accessories' : {
765                                         type        : 'checkGroup',
766                                         id          : 'accessories',
767                                         dataStoreId : 'accessories',
768                                         itemName    : 'accessory',
769                                         data        : data.stock.accessories ? data.stock.accessories : {}
770                                 },
771                                 'accessoryNotes' : {
772                                         type      : 'textarea-styled',
773                                         id        : 'accessoryNotes',
774                                         title     : 'Comments',
775                                         style     : 'comment mobile-comment',
776                                         areaStyle : 'comment-text-box',
777                                         value     : data.stock.accessoryNotes ? data.stock.accessoryNotes : ''
778                                 },
779                                 'mainImage' : {
780                                         type  : 'image',
781                                         id    : 'mainImage',
782                                         baseImage : 'main_img_car.jpg',
783                                         view  : !App.permissions.valuationUploadPhotos,
784                                         value : data.stock.mainImage ? data.stock.mainImage : ''
785                                 },
786                                 'frontImage' : {
787                                         type  : 'image',
788                                         id    : 'frontImage',
789                                         baseImage : 'front_car.jpg',
790                                         view  : !App.permissions.valuationUploadPhotos,
791                                         value : data.stock.frontImage ? data.stock.frontImage : ''
792                                 },
793                                 'leftImage' : {
794                                         type  : 'image',
795                                         id    : 'leftImage',
796                                         baseImage : 'left_car.jpg',
797                                         view  : !App.permissions.valuationUploadPhotos,
798                                         value : data.stock.leftImage ? data.stock.leftImage : ''
799                                 },
800                                 'rightImage' : {
801                                         type  : 'image',
802                                         id    : 'rightImage',
803                                         baseImage : 'right_car.jpg',
804                                         view  : !App.permissions.valuationUploadPhotos,
805                                         value : data.stock.rightImage ? data.stock.rightImage : ''
806                                 },
807                                 'backImage' : {
808                                         type  : 'image',
809                                         id    : 'backImage',
810                                         baseImage : 'back_car.jpg',
811                                         view  : !App.permissions.valuationUploadPhotos,
812                                         value : data.stock.backImage ? data.stock.backImage : ''
813                                 },
814                                 'interiorImage' : {
815                                         type  : 'image',
816                                         id    : 'interiorImage',
817                                         baseImage : 'interior_car.jpg',
818                                         view  : !App.permissions.valuationUploadPhotos,
819                                         value : data.stock.interiorImage ? data.stock.interiorImage : ''
820                                 },
821                                 'engineImage' : {
822                                         type  : 'image',
823                                         id    : 'engineImage',
824                                         baseImage : 'engine_car.jpg',
825                                         view  : !App.permissions.valuationUploadPhotos,
826                                         value : data.stock.engineImage ? data.stock.engineImage : ''
827                                 },
828                                 'natisImage' : {
829                                         type  : 'image',
830                                         id    : 'natisImage',
831                                         baseImage : 'copy_of_natis_car.jpg',
832                                         view  : !App.permissions.valuationUploadPhotos,
833                                         value : data.stock.natisImage ? data.stock.natisImage : ''
834                                 },
835                                 'clubs' : {
836                                         type        : 'select',
837                                         id          : 'clubs',
838                                         title       : 'Select clubs',
839                                         dataStoreId : 'clubs',
840                                         multiple    : true,
841                                         value       : ''
842                                 },
843                                 'print': {
844                                         constructor: 'Button',
845                                         items: ('New Valuation' == data.jobState
846                                                         && 'Dealership Valuator' != App.permissions.name
847                                                         && 'Dealership Sales' != App.permissions.name)
848                                         ? {
849                                                 Export: {
850                                                         preset: 'Print',
851                                                         id: 'printValuationPdf',
852                                                         handler: _w.valuationview.printValuationPdf
853                                                 },
854                                                 ExportTC: {
855                                                         preset: 'PrintTC',
856                                                         id: 'printValuationPdfTC',
857                                                         handler: _w.valuationview.printValuationPdfTC
858                                                 }
859                                         }
860                                         : {
861                                                 Export: {
862                                                         preset: 'Print',
863                                                         id: 'printValuationPdf',
864                                                         handler: _w.valuationview.printValuationPdf
865                                                 }
866                                         }
867                                 },
868
869                                 'infCreated': data.created ? ((data.created).split(' '))[0] : '',
870                                 'infValuationNumber': data.valuationNumber ? data.valuationNumber : '',
871                                 'infReferenceNumber': data.stock.referenceNumber ? data.stock.referenceNumber : '',
872                                 'infStatus': data.jobState ? data.jobState : '',
873                                 'infCreatedBy': data.createdBy && data.createdBy.firstName
874                                         ? data.createdBy.firstName + ' ' +  data.createdBy.familyName
875                                         : '',
876                                 'infValuator': data.valuatedBy && data.valuatedBy.firstName
877                                         ? data.valuatedBy.firstName + ' ' +  data.valuatedBy.familyName
878                                         : '',
879                                 'infSalesUser': data.salesProfile && data.salesProfile.firstName
880                                         ? data.salesProfile.firstName + ' ' +  data.salesProfile.familyName
881                                         : '',
882                                 'infLoadedOnPriceGuide': data.stock.loadedOnPriceGuide
883                                         ? ((data.stock.loadedOnPriceGuide).split(' '))[0] : '',
884                                 'infTradePrice': data.stock.tradePrice ? 'R ' + data.stock.tradePrice : '0.00',
885                                 'infRetailPrice': data.stock.retailPrice ? 'R ' + data.stock.retailPrice : '0.00',
886                                 'firstName' : {
887                                         type  : 'input',
888                                         id    : 'firstName',
889                                         title : 'Customer name',
890                                         value : data.firstName ? data.firstName : ''
891                                 },
892                                 'familyName' : {
893                                         type  : 'input',
894                                         id    : 'familyName',
895                                         title : 'Customer surname',
896                                         value : data.familyName ? data.familyName : ''
897                                 },
898                                 'idNumber' : {
899                                         type  : 'input',
900                                         id    : 'idNumber',
901                                         title : 'ID',
902                                         value : data.idNumber ? data.idNumber : ''
903                                 },
904                                 'mobile' : {
905                                         type  : 'mobile',
906                                         id    : 'mobile',
907                                         title : 'Mobile',
908                                         value : data.mobile ? data.mobile : ''
909                                 },
910                                 'email' : {
911                                         type  : 'input',
912                                         id    : 'email',
913                                         title : 'Email',
914                                         value : data.email ? data.email : ''
915                                 },
916                                 'department' : {
917                                         type        : 'select',
918                                         id          : 'department',
919                                         title       : 'Department',
920                                         dataStoreId : 'departments',
921                                         value       : data.department ? data.department : ''
922                                 },
923
924                                 'amountOffered' : {
925                                         type  : 'input',
926                                         disabled  : true,
927                                         style : 'rand-input',
928                                         id    : 'amountOffered',
929                                         title : 'Amount offered',
930                                         value : data.amountOffered ? data.amountOffered : '0.00'
931                                 },
932                                 'overAllowance' : {
933                                         type  : 'input',
934                                         style : 'rand-input',
935                                         id    : 'overAllowance',
936                                         title : 'O.A',
937                                         value : data.overAllowance ? data.overAllowance : '0.00'
938                                 },
939                                 'standInValue' : {
940                                         type  : 'input',
941                                         style : 'rand-input',
942                                         id    : 'standInValue',
943                                         title : 'Stand-in-value',
944                                         value : data.standInValue ? data.standInValue : '0.00'
945                                 },
946                                 'projectedRetail' : {
947                                         type  : 'input',
948                                         style : 'rand-input',
949                                         id    : 'projectedRetail',
950                                         title : 'Projected retail',
951                                         value : data.projectedRetail ? data.projectedRetail : '0.00'
952                                 },
953                                 'plannedMargin' : {
954                                         type  : 'input',
955                                         style : 'rand-input',
956                                         id    : 'plannedMargin',
957                                         title : 'Planned margin',
958                                         value : data.plannedMargin ? data.plannedMargin : '0.00'
959                                 },
960                                 'valuators' : {
961                                         type        : 'select',
962                                         id          : 'valuators',
963                                         title       : 'Select valuator',
964                                         dataStoreId : 'valuators',
965                                         multiple    : true,
966                                         value       : data.valuators ? data.valuators : []
967                                 },
968                                 'salesProfile' : {
969                                         type        : 'select',
970                                         id          : 'salesProfile',
971                                         title       : 'Sales person',
972                                         dataStoreId : 'sales',
973                                         value       : ''
974                                 },
975                                 'salesAmountOffered' : {
976                                         type  : 'input',
977                                         style : 'rand-inputs',
978                                         id    : 'salesAmountOffered',
979                                         title : 'Amount',
980                                         value : data.amountOffered ? data.amountOffered : '0.00',
981                                         onFocus : function()
982                                                 {
983                                                         var $_this = $(this),
984                                                                         value  = $_this.val();
985
986                                                         if( value == '0.00' )
987                                                                 {
988                                                                         $_this.val('');
989                                                                 }
990                                                 },
991                                         onBlur : function()
992                                                 {
993                                                         var $_this = $(this),
994                                                                         value  = $_this.val();
995
996                                                         if( value === '' )
997                                                                 {
998                                                                         $_this.val('0.00');
999                                                                 }
1000                                                 }
1001                                 },
1002                                 'salesComments' : {
1003                                         type      : 'textarea-styled',
1004                                         id        : 'salesComments',
1005                                         title     : 'Comments',
1006                                         style     : 'comment',
1007                                         areaStyle : 'comment-text-box',
1008                                         value     : data.salesComments ? data.salesComments : ''
1009                                 },
1010                                 'stockNumber' : {
1011                                         type  : 'input',
1012                                         id    : 'stockNumber',
1013                                         title : 'Stock number',
1014                                         value : data.stock.stockNumber ? data.stock.stockNumber : ''
1015                                 }
1016                         };
1017                 },
1018
1019                 printValuationPdf : function()
1020                 {
1021                         var task = 'Pdf.Print003';
1022                         if ('New Valuation' == _w.valuationview.itemData.jobState
1023                                         && ('Dealership Valuator' == App.permissions.name
1024                                                         || 'Dealership Sales' == App.permissions.name))
1025                         {
1026                                 // T&C version
1027                                 var task = 'Pdf.Print001';
1028                         }
1029                         else if ('Dealership Valuator' == App.permissions.name
1030                                         || 'Dealership Sales' == App.permissions.name)
1031                         {
1032                                 var task = 'Pdf.Print002';
1033                         }
1034                         App.API.getTask(
1035                                         'printValuation:' + _w.valuationview.itemData.stock.id,
1036                                         'Valuation', task, _w.valuationview.itemId,
1037                                         {}, function() {
1038                                                 App.API.execTask(
1039                                                                 'printValuation:' + _w.valuationview.itemData.stock.id, {}, {},
1040                                                                 _w.valuationview._onExportReceived, _w.taskExecError,
1041                                                                 'EXPORT', true
1042                                                 );
1043                                         }, _w.taskContractError
1044                         );
1045                 },
1046
1047                 printValuationPdfTC : function()
1048                 {
1049                         // Consider the T&C requirement.
1050                         var task = 'Pdf.Print001';
1051                         App.API.getTask(
1052                                         'printValuation:' + _w.valuationview.itemData.stock.id,
1053                                         'Valuation', task, _w.valuationview.itemId,
1054                                         {}, function() {
1055                                                 App.API.execTask(
1056                                                                 'printValuation:' + _w.valuationview.itemData.stock.id, {}, {},
1057                                                                 _w.valuationview._onExportReceived, _w.taskExecError,
1058                                                                 'EXPORT', true
1059                                                 );
1060                                         }, _w.taskContractError
1061                         );
1062                 },
1063
1064                 _listMakesModelsTypes : function( fromItemData, callback )
1065                 {
1066                         if (fromItemData)
1067                         {
1068                                 var year     = _w.valuationview.itemData.stock.vehicleYear;
1069                                 var category = _w.valuationview.itemData.stock.type.category.id;
1070                                 var make     = _w.valuationview.itemData.stock.type.model.make.id;
1071                                 var model    = _w.valuationview.itemData.stock.type.model.id;
1072                                 var type   = _w.valuationview.itemData.stock.type.id;
1073                                 var mmcode   = _w.valuationview.itemData.stock.type.mmcode;
1074                         }
1075                         else
1076                         {
1077                                 var year     = $('#vehicleYear').val();
1078                                 var category = $('#vehicleCategory').val();
1079                                 var make     = $('#vehicleMake').val();
1080                                 var model    = $('#vehicleModel').val();
1081                                 var type     = $('#vehicleType').val();
1082                                 var mmcode   = $('#mmCode').val();
1083                         }
1084                         if (year && category)
1085                         {
1086                                 App.DataStore.loadSelectListData(
1087                                                 'makes', false, 'Stock', 'Utility.ListMakes', null,
1088                                                 {"Filter": {
1089                                                         "category" : category,
1090                                                         "year" : year
1091                                                         }}
1092                                                 );
1093                         }
1094                         if (make && year && category)
1095                         {
1096                                 App.DataStore.loadSelectListData(
1097                                                 'models', false, 'Stock', 'Utility.ListModels', null,
1098                                                 {"Filter": {
1099                                                         "make" : make,
1100                                                         "category" : category,
1101                                                         "year" : year
1102                                                         }}
1103                                                 );
1104                         }
1105                         if (model && year && category)
1106                         {
1107                                 App.DataStore.loadSelectListData(
1108                                                 'types', false, 'Stock', 'Utility.ListTypes', null,
1109                                                 {"Filter": {
1110                                                         "model" : model,
1111                                                         "category" : category,
1112                                                         "year" : year
1113                                                         }}, {}, callback
1114                                                 );
1115                         }
1116                 },
1117
1118                 _onDataAndTemplate : function()
1119                 {
1120                         var total = 0.00;
1121                         $.each($.find('.damage-input'), function(i, input) {
1122                                 total += parseFloat($(input).val());
1123                         });
1124                         $('#' + _w.valuationview.ti.tid + '_damageTotal').html('R ' + total);
1125                         $('#doAcceptOffer').prop('disabled', true);
1126                         if (null != _w.valuationview.itemData.stock.priceGuide
1127                                         && 0 < _w.valuationview.itemData.stock.highestOffer)
1128                         {
1129                                 _w.valuationview.priceGuideId = _w.valuationview.itemData.stock.priceGuide;
1130                                 App.API.getTask(
1131                                                 'offerList', 'PriceGuide', 'Offer.List',
1132                                                 _w.valuationview.itemData.stock.priceGuide, {}, function(){
1133                                                         App.API.execTask(
1134                                                                         'offerList', {}, {},
1135                                                                         function(response)
1136                                                                         {
1137                                                                                 var offers = response.Data;
1138                                                                                 if (offers.length)
1139                                                                                 {
1140                                                                                         var highest = 0;
1141                                                                                         var lowestDate = 0;
1142                                                                                         var highestId = 0;
1143                                                                                         var valid = 0;
1144                                                                                         for (var x in offers)
1145                                                                                         {
1146                                                                                                 if ('Valid Offer' != offers[x].status)
1147                                                                                                 {
1148                                                                                                         continue;
1149                                                                                                 }
1150                                                                                                 valid++;
1151                                                                                                 var dateInt = parseInt(
1152                                                                                                                 offers[x].created.split(' ')[0].split('-').join('')
1153                                                                                                                 + offers[x].created.split(' ')[1].split(':').join('')
1154                                                                                                                 );
1155                                                                                                 if  ( (parseInt(offers[x].amount) == highest && dateInt < lowestDate)
1156                                                                                                                 || parseInt(offers[x].amount) > highest)
1157                                                                                                 {
1158                                                                                                         highest = parseInt(offers[x].amount);
1159                                                                                                         highestId = x;
1160                                                                                                         lowestDate = dateInt;
1161                                                                                                 }
1162                                                                                         }
1163                                                                                         if (0 == valid)
1164                                                                                         {
1165                                                                                                 var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no valid offers!</div>';
1166                                                                                                 $('#dealDoneOfferList').html(modalbody);
1167                                                                                                 return;
1168                                                                                         }
1169                                                                                         _w.valuationview.highestOfferId = highestId;
1170                                                                                         var modalbody = '<table class="table table-bordered table-condensed table-striped">';
1171                                                                                         modalbody += '<tr class="table-header-red"><td>Company</td><td>Date Offered</td><td>Amount</td></tr>';
1172                                                                                         for (var x in offers)
1173                                                                                         {
1174                                                                                                 if ('Valid Offer' != offers[x].status)
1175                                                                                                 {
1176                                                                                                         continue;
1177                                                                                                 }
1178                                                                                                 var isOwn = highestId == x
1179                                                                                                         ? true
1180                                                                                                         : false;
1181                                                                                                 modalbody +=
1182                                                                                                         '<tr class="modalrow ' + (isOwn ? 'red' : 'black') + '">'
1183                                                                                                         + '<td class="date">' + offers[x].company.name + '</td>'
1184                                                                                                         + '<td class="date">' + offers[x].created.split(' ')[0] + '</td>'
1185                                                                                                         + '<td class="wide">R' + offers[x].amount.split('.')[0] + '</td>'
1186                                                                                                         + '</tr>';
1187                                                                                         }
1188                                                                                         modalbody += '</table>';
1189                                                                                         $('#doAcceptOffer').prop('disabled', false);
1190                                                                                 }
1191                                                                                 else
1192                                                                                 {
1193                                                                                         var modalbody = '<div style="font-weight:bold;color:#9B2022;">There are no valid offers!</div>';
1194                                                                                 }
1195                                                                                 $('#dealDoneOfferList').html(modalbody);
1196                                                                         }, _w.taskExecError
1197                                                         );
1198                                                 }, _w.taskContractError
1199                                 );
1200                         }
1201
1202                         if (_w.valuationview.limited)
1203                         {
1204                                 $('input').prop('disabled', true);
1205                                 $('button').prop('disabled', true);
1206                                 $('checkbox').prop('disabled', true);
1207                                 $('textarea').prop('disabled', true);
1208                                 $('#btnUnarchive').prop('disabled', false);
1209                                 $('#printValuationPdf').prop('disabled', false);
1210                                 $('#optSendToSales').prop('disabled', false);
1211                                 $('#salesProfile').prop('disabled', false);
1212                                 $('button[data-id="salesProfile"]').prop('disabled', false);
1213                                 $('#salesAmountOffered').prop('disabled', false);
1214                                 $('#salesComments').prop('disabled', false);
1215                                 $('#btnSendToSales').prop('disabled', false);
1216                                 $('#printValuationPdf').prop('disabled', false);
1217                                 
1218                                 // Mobi buttons
1219                                 $('#mobiAuctionBtn').prop('disabled', false);
1220                                 $('#mobiPriceGuideBtn').prop('disabled', false);
1221                                 $('#mobiValuationsBtn').prop('disabled', false);
1222                         }
1223                         $('#sectionSelectValuator').hide();
1224                         if (!_w.valuationview.newItem)
1225                         {
1226                                 $('#retailPrice').val(_w.valuationview.itemData.stock.retailPrice);
1227                                 $('#tradePrice').val(_w.valuationview.itemData.stock.tradePrice);
1228                                 switch (_w.valuationview.itemData.jobState)
1229                                 {
1230                                 case 'New Valuation':
1231                                         $('#btnSaveAsSales').show();
1232                                         $('#btnSaveAsPending').show();
1233                                         $('#optSendToPg').show();
1234                                         $('#btnSendToStock').show();
1235                                         $('#btnArchive').show();
1236                                         true == App.permissions.valuationCreateSales
1237                                                         ? $('#sectionSelectValuator').show()
1238                                                         : $('#sectionSelectValuator').hide();
1239                                         if (!App.permissions.valuationUpdateDamages)
1240                                         {
1241                                                 $('.damages-form').hide();
1242                                         }
1243                                         if (!App.permissions.valuationUploadPhotos)
1244                                         {
1245                                                 $('.photos-form').hide();
1246                                         }
1247                                         break;
1248                                 case 'Pending Valuation':
1249                                         $('#optSendToSales').show();
1250                                         $('#optSendToPg').show();
1251                                         $('#btnSendToStock').show();
1252                                         $('#btnArchive').show();
1253                                         if (null != _w.valuationview.itemData.stock.priceGuide
1254                                                         && 0 < _w.valuationview.itemData.stock.highestOffer)
1255                                         {
1256                                                 $('#btnSendToDealDone').show();
1257                                                 $('#btnSendToDealNotDone').show();
1258                                         }
1259                                         if (!App.permissions.valuationUpdateDamages)
1260                                         {
1261                                                 $('.damages-form').hide();
1262                                         }
1263                                         if (!App.permissions.valuationUploadPhotos)
1264                                         {
1265                                                 $('.photos-form').hide();
1266                                         }
1267                                         break;
1268                                 case 'Complete Valuation':
1269                                         $('#optSendToSales').show();
1270                                         $('#optSendToPg').show();
1271                                         $('#btnSendToStock').show();
1272                                         $('#btnArchive').show();
1273                                         if (null != _w.valuationview.itemData.stock.priceGuide
1274                                                         && 0 < _w.valuationview.itemData.stock.highestOffer)
1275                                         {
1276                                                 $('#btnSendToDealDone').show();
1277                                                 $('#btnSendToDealNotDone').show();
1278                                         }
1279                                         break;
1280                                 case 'Offer Accepted':
1281                                         $('#btnSendToStock').show();
1282                                         $('#btnSendToStock').prop('disabled', false);
1283                                         $('#stockNumber').prop('disabled', false);
1284                                         $('#doSendToStock').prop('disabled', false);
1285                                         $('#btnArchive').show();
1286                                         $('#btnArchive').prop('disabled', false);
1287                                         break;
1288                                 case 'Deal Done':
1289                                         $('#btnSendToStock').show();
1290                                         $('#btnSendToStock').prop('disabled', false);
1291                                         $('#stockNumber').prop('disabled', false);
1292                                         $('#doSendToStock').prop('disabled', false);
1293                                         $('#btnArchive').show();
1294                                         $('#btnArchive').prop('disabled', false);
1295                                         break;
1296                                 case 'Deal Not Done':
1297                                         $('#btnSaveAsPending').show();
1298                                         $('#optSendToPg').show();
1299                                         $('#btnSendToStock').show();
1300                                         $('#btnArchive').show();
1301                                         break;
1302                                 case 'Archived':
1303                                         $('#btnUnarchive').show();
1304                                         break;
1305                                 case 'Price Guide':
1306                                         $('#optSendToSales').show();
1307                                         break;
1308                                 }
1309                         }
1310
1311                         /*
1312                          * Permissions
1313                          */
1314                         $('#optSendToPg').prop('disabled', true == App.permissions.valuationRoutePriceguide ? false : true);
1315                         $('#optSendToSales').prop('disabled', true == App.permissions.valuationRouteComplete ? false : true);
1316                         $('#btnUpdate').prop('disabled', true == App.permissions.valuationUpdate ? false : true);
1317                         $('#btnSendToStock').prop('disabled', true == App.permissions.valuationRouteStock ? false : true);
1318                         $('#btnSaveAsPending').prop('disabled', true == App.permissions.valuationRoutePending ? false : true);
1319                         $('#btnSendToDealDone').prop('disabled', true == App.permissions.valuationRouteDealDone ? false : true);
1320                         $('#btnSendToDealNotDone').prop('disabled', true == App.permissions.valuationRouteDealNotDone ? false : true);
1321                         if ('Dealership Sales' == App.permissions.name && !_w.valuationview.newItem)
1322                         {
1323                                 $('#btnSaveAsPending').hide();
1324                         }
1325
1326                         $('#btnArchive').prop('disabled', true == App.permissions.valuationArchive ? false : true);
1327                         $('#printValuationPdf').prop('disabled', true == App.permissions.valuationPrint ? false : true);
1328                         $('#btnB4cAccidentReport').prop('disabled', true == App.permissions.valuationB4cReport ? false : true);
1329                         $('#btnBwmAccidentReport').prop('disabled', true == App.permissions.valuationBmw ? false : true);
1330
1331
1332
1333                         true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_overAllowance').show() : $('#' + _w.valuationview.ti.tid  + '_overAllowance').hide();
1334                         true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_standInValue').show() : $('#' + _w.valuationview.ti.tid  + '_standInValue').hide();
1335                         true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_projectedRetail').show() : $('#' + _w.valuationview.ti.tid  + '_projectedRetail').hide();
1336                         true == App.permissions.valuationPricingDetails ? $('#' + _w.valuationview.ti.tid  + '_plannedMargin').show() : $('#' + _w.valuationview.ti.tid  + '_plannedMargin').hide();
1337
1338
1339                         _w.valuationview._setupValidation();
1340                 },
1341
1342                 _onValuationViewDataReceived : function( response )
1343                 {
1344                         _w.valuationview.itemId = response.Data.id;
1345                         _w.valuationview.itemData = response.Data;
1346                         _w.valuationview._listMakesModelsTypes(true);
1347                         _w.valuationview.setValuationViewFormMeta(response.Data);
1348                         _w.valuationview.ti.hydrate(_w.valuationview.formMeta);
1349                         if (_w.valuationview.ti.published)
1350                         {
1351                                 _w.valuationview._onDataAndTemplate();
1352                         }
1353                 },
1354
1355                 _setupValidation : function ()
1356                 {
1357                         App.DataStore.listen('regValidation', 'damages', function(id, damages) {
1358                                 var rules = {
1359                                         firstName: {required: true, minlength: 3},
1360                                         familyName: {required: true, minlength: 3},
1361                                         idNumber: {required: false, minlength: 13, maxlength: 13, digits: true},
1362                                         mobile: {required: true, mobile: true},
1363                                         salesAmountOffered: {number: true},
1364                                         overAllowance: {number: true},
1365                                         standInValue: {number: true},
1366                                         projectedRetail: {number: true},
1367                                         plannedMargin: {number: true},
1368                                         vehicleYear: "required",
1369                                         vehicleCategory: "required",
1370                                         
1371                                         vehicleMake: { required: function(){ return $('#vehicleCategory').val() != '8'; } },
1372                                         vehicleModel: { required: function(){ return $('#vehicleCategory').val() != '8'; } },
1373                                         vehicleType: { required: function(){ return $('#vehicleCategory').val() != '8'; } },
1374                                         
1375                                         vehicleMakeManual: { required: function(){ return $('#vehicleCategory').val() == '8' && _w.valuationview.itemId == 0; } },
1376                                         vehicleModelManual: { required: function(){ return $('#vehicleCategory').val() == '8' && _w.valuationview.itemId == 0; } },
1377                                         vehicleTypeManual: { required: function(){ return $('#vehicleCategory').val() == '8' && _w.valuationview.itemId == 0; } },
1378                                         
1379                                         
1380                                         vehicleFuelType: "required",
1381                                         vehicleTransmissionType: "required",
1382                                         registrationNumber: "required",
1383                                         km: { required: true, digits: true }
1384                                 };
1385                                 for (var i = 0; i < damages.length; i++)
1386                                 {
1387                                         rules['damage' + damages[i].value + 'Amount'] = {number: true};
1388                                 }
1389                                 $('#frmValuationView').validate({
1390                                         "rules": rules,
1391                                         messages: {
1392                                                 firstName: {
1393                                                         minlength: "This field is required."
1394                                                 },
1395                                                 familyName: {
1396                                                         minlength: "This field is required."
1397                                                 },
1398                                                 idNumber: {
1399                                                         minlength: "Must be 13 digits.",
1400                                                         maxlength: "Must be 13 digits."
1401                                                 },
1402                                                 vehicleYear : "This field is required.",
1403                                                 vehicleCategory : "This field is required.",
1404                                                 
1405                                                 vehicleMake : "This field is required.",
1406                                                 vehicleModel : "This field is required.",
1407                                                 vehicleType : "This field is required.",
1408                                                 
1409                                                 vehicleMakeManual : "This field is required.",
1410                                                 vehicleModelManual : "This field is required.",
1411                                                 vehicleTypeManual : "This field is required.",
1412                                                 
1413                                                 
1414                                                 vehicleFuelType : "This field is required.",
1415                                                 vehicleTransmissionType : "This field is required.",
1416                                                 registrationNumber : "This field is required.",
1417                                                 km : {required: "This field is required."}
1418                                         },
1419                                         invalidHandler: function(event, validator) {
1420                                             var errors = validator.numberOfInvalids();
1421                                             if (0 < _w.valuationview.validations && errors) {
1422                                               var message = errors == 1
1423                                                 ? 'You missed 1 field. It has been highlighted'
1424                                                 : 'You missed ' + errors + ' fields. They have been highlighted';
1425                                               alert(message);
1426                                             }
1427                                             _w.valuationview.validations++;
1428                                         },
1429                                         submitHandler: function( form ) {
1430                                                 $('#btnUpdate').prop('disabled', true);
1431                                                 var data = _w.valuationview._prepValuationUpdateData();
1432                                                 App.API.execTask(
1433                                                         'updateValuation:' + _w.valuationview.itemId, data, {},
1434                                                         _w.valuationview._onValuationUpdated, _w.taskExecError
1435                                                 );
1436                                         }
1437                                 });
1438                                 try {
1439                                         _w.valuationview.validations = 0;
1440                                         $('#frmValuationView').valid();
1441                                 } catch(err) {}
1442                                 $('label.error').remove();
1443                                 $('.valid').removeClass('valid');
1444                         });
1445                 },
1446
1447                 _onTuReady : function ()
1448                 {
1449                         App.API.execTask(
1450                                         'TransUnionFromMmCode', {
1451                                                 Filter : {
1452                                                         vehicleYear : parseInt($('#vehicleYear').val()),
1453                                                         type : parseInt($('#vehicleType').val())
1454                                                 }
1455                                 }, {},
1456                                 _w.valuationview._onTuReceived
1457                         );
1458                 },
1459
1460                 _onTuReceived : function ( response )
1461                 {
1462                         $('#btnFetchTu').prop('disabled', false);
1463                         if ('Success' != response.Data.Status)
1464                         {
1465                                 alert(response.Data.Status);
1466                                 return;
1467                         }
1468                         var vals = response.Data.VehicleDetails[0].Value;
1469                         $('#retailPrice').val(vals.RetailPrice);
1470                         $('#tradePrice').val(vals.TradePrice);
1471                         $('#MmCode').val(vals.MmCode);
1472                         _w.valuationview.ti.hydrateParam('infRetailPrice', 'R ' + vals.RetailPrice);
1473                         _w.valuationview.ti.hydrateParam('infTradePrice', 'R ' + vals.TradePrice);
1474                         _w.valuationview.ti.hydrateParam('infMmCode', ' ' + vals.MmCode);
1475
1476                 },
1477
1478                 _prepValuationUpdateData : function()
1479                 {
1480                         var data = _w.valuationview.ti.harvest();
1481                         data.km = '' == data.km
1482                                 ? 0
1483                                 : parseInt(data.km);
1484                         var retailPrice = $('#retailPrice').val();
1485                         var tradePrice  = $('#tradePrice').val();
1486                         var mmCode = $('#MmCode').val();
1487
1488                         data.retailPrice = ('' == retailPrice)
1489                                 ? 0.00
1490                                 : parseFloat(retailPrice);
1491                         data.tradePrice = ('' == tradePrice)
1492                                 ? 0.00
1493                                 : parseFloat(tradePrice);
1494                         delete data.damageTotal;
1495
1496                         delete data.amountOffered;
1497 //                      if ('' == data.overAllowance)
1498 //                      {
1499 //                              delete data.overAllowance;
1500 //                      }
1501
1502                         if ('' == data.overAllowance)
1503                         {
1504                             delete data.overAllowance;
1505                         }
1506                         else
1507                         {
1508                                 data.overAllowance = parseFloat(data.overAllowance);
1509                         }
1510
1511                         if ('' == data.standInValue)
1512                         {
1513                                 delete data.standInValue;
1514                         }
1515                         else
1516                         {
1517                                 data.standInValue = parseFloat(data.standInValue);
1518                         }
1519
1520                         if ('' == data.projectedRetail)
1521                         {
1522                                 delete data.projectedRetail;
1523                         }
1524                         else
1525                         {
1526                                 data.projectedRetail = parseFloat(data.projectedRetail);
1527                         }
1528
1529                         if ('' == data.plannedMargin)
1530                         {
1531                                 delete data.plannedMargin;
1532                         }
1533                         else
1534                         {
1535                                 data.plannedMargin = parseFloat(data.plannedMargin);
1536                         }
1537
1538                         if ('' == data.mainImage)
1539                         {
1540                                 delete data.mainImage;
1541                         }
1542                         if ('' == data.leftImage)
1543                         {
1544                                 delete data.leftImage;
1545                         }
1546                         if ('' == data.backImage)
1547                         {
1548                                 delete data.backImage;
1549                         }
1550                         if ('' == data.engineImage)
1551                         {
1552                                 delete data.engineImage;
1553                         }
1554                         if ('' == data.frontImage)
1555                         {
1556                                 delete data.frontImage;
1557                         }
1558                         if ('' == data.rightImage)
1559                         {
1560                                 delete data.rightImage;
1561                         }
1562                         if ('' == data.interiorImage)
1563                         {
1564                                 delete data.interiorImage;
1565                         }
1566                         if ('' == data.natisImage)
1567                         {
1568                                 delete data.natisImage;
1569                         }
1570                         delete data.clubs;
1571
1572
1573                         var postData = {Stock:{}, Valuation:{}};
1574                         var valuationFields = [
1575                                                "firstName","familyName","idNumber","mobile",
1576                                                "email","department","amountOffered","overAllowance",
1577                                                "standInValue","projectedRetail","plannedMargin",
1578                                                "salesProfile","amountOffered","salesComments"
1579                                                ];
1580                         for (var field in data)
1581                         {
1582                                 if (-1 == valuationFields.indexOf(field))
1583                                 {
1584                                         postData.Stock[field] = data[field];
1585                                 }
1586                                 else
1587                                 {
1588                                         postData.Valuation[field] = data[field];
1589                                 }
1590                         }
1591                         if ('' == postData.Valuation.idNumber)
1592                         {
1593                                 delete postData.Valuation.idNumber;
1594                         }
1595                         if ('' == postData.Valuation.email)
1596                         {
1597                                 delete postData.Valuation.email;
1598                         }
1599
1600                         return postData;
1601                 },
1602
1603                 _onValuationUpdated : function( response )
1604                 {
1605                         window.location.hash = '/valuations';
1606                 }
1607
1608         };
1609
1610 })();