initial commit
[namibia] / public / js / app / portal-workspace / offerview.js
1 ;(function(){
2
3         window._w.offerview = {
4
5                 itemId        : null,
6                 itemData      : null,
7                 formMeta      : null,
8                 offerId       : null,
9                 offerData     : null,
10                 formOfferMeta : null,
11                 ti            : null,
12                 limited       : false,
13                 newItem       : false,
14
15                 onOfferViewTemplateReady : function( template, event, eventData )
16                 {
17                         _w.offerview.ti = _t[template];
18                         _w.offerview.limited = ('limitedvaluationview' == template)
19                                 ? true
20                                 : false;
21
22                         _w.offerview.setStockViewFormMeta();
23                         _w.offerview.setOfferViewFormMeta();
24                         _w.offerview.ti.hydrate(_w.offerview.formMeta);
25
26                         _w.offerview.formOfferMeta = {
27                                         type  : 'input',
28                                         id    : 'amountOffered',
29                                         title : 'Amount offered',
30                                         value : '0.00'
31                         };
32
33                         // Retrieve initial view data.
34                         _w.offerview.itemId = App.Util.getUrlParam('pgId');
35                         _w.offerview.offerId   = App.Util.getUrlParam('id');
36
37                         // Offer data
38                         if (0 == _w.offerview.offerId || null == _w.offerview.offerId)
39                         {
40                                 $('#btnUnarchive').hide();
41                                 $('#btnArchive').hide();
42                                 _w.offerview.newItem = true;
43                                 _w.offerview.offerData = {};
44                         }
45                         else
46                         {
47                                 _w.offerview.newItem = false;
48                         }
49
50                         // Stock data
51                         var task = 'PriceGuide.View';
52                         App.API.getTask(
53                                 'viewPg:' + _w.offerview.itemId, 'PriceGuide', task, _w.offerview.itemId,
54                                 {}, $.proxy( _w.offerview._onStockViewDataReceived, this ), _w.taskContractError
55                         );
56                 },
57
58                 loadHistoricalGrid : function( contract, data, options )
59                 {
60                         App.API.execTask(
61                                         'gridStockHistory', {Grid:{Filter:{"PricingHistory.stockId":_w.offerview.itemData.stock.id}}}, options,
62                                         _w.offerview._onHistoryGridDataReceived, _w.taskExecError
63                                         );
64
65                 },
66                 _onHistoryGridDataReceived : function( response )
67                 {
68                         _w.offerview.gridStatus = 'Active';
69                         _w.offerview.ti.hydrateParam('gridPricingHistoryRowRepeater', {});
70                         var gridData = [];
71
72                         console.log(response.Data.DataSet.length);
73
74                         if (response.Data.DataSet.length == 0 || response.Data.DataSet.length == null) {
75                                 $('#pricing_history_none_found').show();
76                                 $('#PHColumnHeaders').hide();
77                         }
78
79                         for (var i = 0; i < response.Data.DataSet.length; i++)
80                         {
81                                 var row = response.Data.DataSet[i];
82                                 gridData.push({
83                                         prepend: {
84                                                 Trade                                   : 'R ',
85                                                 TotalRecon                              : 'R ',
86                                                 PriceguideOffer                 : 'R ',
87                                                 AuctionWinningBid               : 'R '
88                                         },
89                                         style: {
90                                                 Date                                    : '',
91                                                 Trade                                   : '',
92                                                 TotalRecon                              : '',
93                                                 km                                              : '',
94                                                 PriceguideOffer                 : '',
95                                                 AuctionWinningBid               : '',
96
97                                         },
98                                         items: {
99                                                 'Date'                          : row.date,
100                                                 'Trade'                         : row.trade,
101                                                 'TotalRecon'            : row.totalRecon,
102                                                 'KM'                            : row.km,
103                                                 'PriceguideOffer'       : row.pricegOffer,
104                                                 'AuctionWinningBid' : row.auctionWinningBid
105                                 }
106                                 });
107                         }
108                         _w.offerview.ti.hydrateParam('gridPricingHistoryRowRepeater', {
109                                 constructor : 'GridDataRow',
110                                 items       : gridData
111                         });
112                         response.Data.Meta.bind = _w.offerview.pageStockGrid;
113                 },
114
115                 onOfferViewPublished : function( template, event, eventData )
116                 {
117
118                         $('#btnRequestHPI').click(function () {
119
120                                 if ('' == $('#HPIusername').val() || '' ==  $('#HPIpin').val())
121                                 {
122                                         alert('Please enter a valid username and password.');
123                                         return;
124                                 }
125
126                                 $('#HPIdropConfirm').slideToggle();
127                                 $('#HPIresponse').html('');
128                         });
129
130
131
132                         App.Util.updateCheckboxStyle($('#acceptHPIdisclaimer'));
133                         $('#HPIbtnConfirm').click(function () {
134                                 if ( $('#engineNumber').html() == '' || $('#vinNumber').html() == '' ) {
135                                         alert('Cannot request report without Vehicle Engine Number and VIN Number.');
136                                         return;
137                                 }
138                                 if (!$('#acceptHPIdisclaimer').is(':checked'))
139                                 {
140                                         alert('Please accept the conditions before sending.');
141                                         return;
142                                 }
143
144                                 var user = $('#HPIusername').val();
145                                 var pass = $('#HPIpin').val();
146
147                                 App.API.getTask(
148                                                 'TransUnionForHPI', 'Stock', 'Utility.TransUnionForHPI', null,
149                                                 {'Filter' : {'username' : user ,'pass' : pass , 'stock' : _w.offerview.itemData.stock.id}},
150                                                 function() {
151                                                         App.API.execTask(
152                                                                         'TransUnionForHPI',{}, {},
153                                                                         function(data)
154                                                                         {
155                                                                                 $('#HPIdropConfirm').hide();
156                                                                                 if ('Success' == data.Status)
157                                                                                 {
158                                                                                         $('#HPIresponse').html('Success on HPI Report');
159                                                                                         alert('Success on HPI Report');
160                                                                                         App.DataStore.loadSelectListData(
161                                                                                                         'tuhpi', false, 'Stock', 'Tuhpi.List', null,{"Filter":
162                                                                                                         {
163                                                                                                                 "stock"                 : _w.offerview.itemData.stock.id,
164                                                                                                                 "company"               : App.companyData.id
165                                                                                                         }}, {},function() {
166                                                                                                         // data is retrieved
167                                                                                                         var tuHpiData = App.DataStore.getData('tuhpi');
168                                                                                                         for (var i = 0; i < tuHpiData.length; i++)
169                                                                                                         {
170                                                                                                                 var tuHpiId = tuHpiData[i].id
171                                                                                                                 var tuHpiCreated = tuHpiData[i].created
172                                                                                                                 if (tuHpiId)
173                                                                                                                 {
174                                                                                                                         $('#tuhpiDate').html(tuHpiCreated);
175                                                                                                                         $('#downloadTuhpi').show();
176                                                                                                                         $('#HPIbtnConfirm').prop('disabled', true);
177                                                                                                                         return;
178                                                                                                                 }
179                                                                                                                 else
180                                                                                                                 {
181                                                                                                                         $('#tuhpiDate').html('Not Requested Yet');
182                                                                                                                         $('#downloadTuhpi').hide();
183                                                                                                                         $('#HPIbtnConfirm').prop('disabled', false);
184                                                                                                                 }
185                                                                                                         }
186                                                                                         });
187                                                                                 }
188                                                                                 else
189                                                                                 {
190                                                                                         $('#HPIresponse').html('API call to remote host failed. No data could be retrieved from remote service.');
191                                                                                 }
192                                                                         }, function(response)
193                                                                         {
194                                                                                 $('#HPIbtnConfirm').prop('disabled', false);
195                                                                                 alert(
196                                                                                         undefined != response.StatusReason
197                                                                                                 ? response.StatusReason
198                                                                                                 : 'API call to remote host failed. No data could be retrieved from remote service.'
199                                                                                 );
200                                                                         }
201                                                         );
202                                                 }
203
204
205                                                 , _w.taskContractError
206                                 );
207                         });
208
209
210                         $('#downloadTuhpi').click(function () {
211                                 App.API.getTask(
212                                                 'printTuhpi:' + _w.offerview.itemData.stock.id, 'Stock', 'Tuhpi.Print', _w.offerview.itemData.stock.id,
213                                                 {}, function() {
214                                                         App.API.execTask(
215                                                                         'printTuhpi:' + _w.offerview.itemData.stock.id, {}, {},
216                                                                         function () {}, _w.taskExecError,
217                                                                         'EXPORT', true
218                                                         );
219                                                 }, _w.taskContractError
220                                 );
221                         });
222
223
224
225                         $('#btnMakeOffer').click(function () {
226                                 amount = $('#amountOffered').val();
227                                 if ('' == amount || isNaN(amount)  || parseFloat(amount) <= 0 )
228                                 {
229                                         alert('Please enter a valid amount.');
230                                         return;
231                                 }
232                                 
233                                 if(amount.indexOf(".")==-1)
234                                 {
235                                         amount = amount+'.00'
236                                 }
237                                 
238                                 _w.offerview.ti.hydrateParam('infAmountOffered', amount);
239                                 $('#dropCmp').slideToggle();
240                                 $('#dropConfirm').slideToggle();
241                         });
242
243                         $('#btnConfirm').click(function () {
244                                 var data = {
245                                                 "Offer": {
246                                                         "amount": $('#amountOffered').val()
247                                                 }
248                                 };
249                                 if ('' == data.Offer.amount || isNaN(data.Offer.amount))
250                                 {
251                                         alert('Please enter a valid amount.');
252                                         return;
253                                 }
254                                 var oldOffer = parseFloat( _w.offerview.offerData.amount);
255                                 var newOffer = parseFloat( data.Offer.amount);
256 console.log('old vs new', '.' + oldOffer + '...' + newOffer + '.');
257                                 if (!_w.offerview.newItem && (newOffer <= oldOffer))
258                                 {
259                                         alert('Your updated offer must be higher than your previous offer.');
260                                         return;
261                                 }
262                                 if (_w.offerview.newItem)
263                                 {
264                                         App.API.execTask(
265                                                         'createOffer', data, {},
266                                                         _w.offerview._onOfferUpdated, _w.taskExecError
267                                                 );
268                                 }
269                                 else
270                                 {
271                                         App.API.execTask(
272                                                         'updateOffer:' + _w.offerview.offerId, data, {},
273                                                         _w.offerview._onOfferUpdated, _w.taskExecError
274                                                 );
275                                 }
276                         });
277
278                         $('.mobile-header-bottom .bottom-heading').text('OFFER DETAILS');
279
280                         $('#btnArchive').click(function () {
281                                 App.API.getTask(
282                                                 'archivePriceGuide:' + _w.offerview.offerId,
283                                                 'PriceGuide', 'Offer.Archive', _w.offerview.itemId,
284                                                 {"id":_w.offerview.offerId},
285                                                 function () {
286                                                         App.API.execTask(
287                                                                         'archivePriceGuide:' + _w.offerview.offerId, {}, {},
288                                                                         _w.offerview._onOfferUpdated, _w.taskExecError
289                                                                 );
290                                                 },
291                                                 _w.taskContractError
292                                         );
293                         });
294                         $('#btnUnarchive').click(function () {
295                                 App.API.getTask(
296                                                 'unarchivePriceGuide:' + _w.offerview.offerId,
297                                                 'PriceGuide', 'Offer.Unarchive', _w.offerview.itemId,
298                                                 {"id":_w.offerview.offerId},
299                                                 function () {
300                                                         App.API.execTask(
301                                                                         'unarchivePriceGuide:' + _w.offerview.offerId, {}, {},
302                                                                         _w.offerview._onOfferUpdated, _w.taskExecError
303                                                                 );
304                                                 },
305                                                 _w.taskContractError
306                                         );
307                         });
308
309                         $('.back-btn').on('click', function(){
310                                 window.history.back();
311                         });
312
313                         //console.log(_w.offerview.newItem);
314
315
316                                 _w.offerview.ti.hydrate({
317                                         'gridPricingHistoryColumnHeaders': {
318                                                 constructor: 'GridColumnHeader',
319                                                 items: {
320                                                         Date: {
321                                                                 id: 'gridPricingHistoryColumn_Date',
322                                                                 title: 'Date',
323                                                         },
324                                                         Trade: {
325                                                                 id: 'gridPricingHistoryColumn_Trade',
326                                                                 title: 'Trade',
327                                                         },
328                                                         TotalRecon: {
329                                                                 id: 'gridPricingHistoryColumn_totalRecon',
330                                                                 title: 'Total Recon',
331                                                         },
332                                                         km: {
333                                                                 id: 'gridPricingHistoryColumn_km',
334                                                                 title: 'km',
335                                                         },
336                                                         PriceguideOffer: {
337                                                                 id: 'gridPricingHistoryColumn_pricegOffer',
338                                                                 title: 'Priceguide Offer',
339                                                         },
340                                                         AuctionWinningBid: {
341                                                                 id: 'gridPricingHistoryColumn_auctionWinningBid',
342                                                                 title: 'Auction Winning Bid',
343                                                         },
344
345                                                 }
346                                         },
347                                         'gridPricingHistoryRowRepeater': {}
348                                 });
349
350
351
352
353
354
355                         /*
356                          * Permissions
357                          */
358                         if (0 == _w.offerview.offerId || null == _w.offerview.offerId)
359                         {
360                                 true == App.permissions.pgMakeOffer ? $('#optMakeOffer').show() : $('#optMakeOffer').hide();
361                         }
362                         else
363                         {
364                                 true == App.permissions.pgUpdateOffer ? $('#optMakeOffer').show() : $('#optMakeOffer').hide();
365                         }
366
367                         true == App.permissions.pgPrint ? $('#printPriceGuidePdf').show() : $('#printPriceGuidePdf').hide();
368
369
370                         if (_w.offerview.newItem)
371                         {
372                                 _w.offerview.setOfferViewFormMeta();
373                         }
374                         if (!_w.offerview.newItem && null != _w.offerview.itemData)
375                         {
376                                 _w.offerview._onStockDataAndTemplate();
377                         }
378                         if (!_w.offerview.newItem && null != _w.offerview.offerData)
379                         {
380                                 _w.offerview._onOfferDataAndTemplate();
381                         }
382                 },
383
384                 setStockViewFormMeta : function( data )
385                 {
386                         data = (undefined == data)
387                                 ? {"stock":{}}
388                                 : data;
389                         var damagesHtml = '';
390                         var accessoriesHtml = '';
391                         if (undefined != data && undefined != data.stock && undefined != data.stock.damages)
392                         {
393                                 for (var i = 0; i < data.stock.damages.length; i++)
394                                 {
395                                         damagesHtml += '<div class="control-group">';
396                                         damagesHtml += '<label class="control-label valuation-line">' + data.stock.damages[i].damage.name + ':</label>';
397                                         damagesHtml += '<div class="controls">';
398                                         damagesHtml += '<p class="information-lines">R ' + data.stock.damages[i].amount + '</p>';
399                                         damagesHtml += '</div>';
400                                         damagesHtml += '</div>';
401                                 }
402                         }
403                         if (undefined != data && undefined != data.stock && undefined != data.stock.accessories)
404                         {
405                                 for (var i = 0; i < data.stock.accessories.length; i+=2)
406                                 {
407                                         var acc1 = data.stock.accessories[i]
408                                                 ? data.stock.accessories[i].accessory.name
409                                                 : '&nbsp;';
410                                         var acc2 = data.stock.accessories[i+1]
411                                                 ? data.stock.accessories[i+1].accessory.name
412                                                 : '&nbsp;';
413                                         accessoriesHtml += '<div class="row-fluid">';
414                                         accessoriesHtml += '<div class="span6 textshow">' + acc1 + '</div>';
415                                         accessoriesHtml += '<div class="span6 textshow">' + acc2 + '</div>';
416                                         accessoriesHtml += '</div>';
417                                 }
418                         }
419
420
421                         console.log(data);
422                         _w.offerview.formMeta = {
423
424
425
426                                 'vehicle' : data.stock.type
427                                         ? data.stock.type.model.make.name + ', '
428                                                         + data.stock.type.model.name + ', '
429                                                         + data.stock.type.name
430                                         : '',
431                                 'vehicleRef' : {
432                                         title       : 'Reference Number',
433                                         value       : data.stock.referenceNumber
434                                 },
435                                 'vehicleYear' : {
436                                         title       : 'Year',
437                                         value       : data.stock.vehicleYear ? data.stock.vehicleYear.name : ''
438                                 },
439                                 'category' : {
440                                         title       : 'Category',
441                                         value       : data.stock.type ? data.stock.type.category.name : ''
442                                 },
443                                 'make' : {
444                                         title       : 'Vehicle Make',
445                                         value       : data.stock.type ? data.stock.type.model.make.name : ''
446                                 },
447                                 'model' : {
448                                         id          : 'vehicleModel',
449                                         title       : 'Vehicle Model',
450                                         value       : data.stock.type ? data.stock.type.model.name : ''
451                                 },
452                                 'type' : {
453                                         title       : 'Vehicle Type',
454                                         value       : data.stock.type ? data.stock.type.name : ''
455                                 },
456                                 'mmCode' : {
457                                         title       : 'MM Code',
458                                         value       : data.stock.type ? data.stock.type.mmCode : ''
459                                 },
460                                 'fuelType' : {
461                                         title       : 'Fuel Type',
462                                         value       : data.stock.fuelType ? data.stock.fuelType.name : ''
463                                 },
464                                 'transmissionType' : {
465                                         title       : 'Transmission Type',
466                                         value       : data.stock.transmissionType ? data.stock.transmissionType.name : ''
467                                 },
468                                 'condition' : {
469                                         title       : 'Condition',
470                                         value       : data.stock.condition ? data.stock.condition.name : ''
471                                 },
472                                 'exteriorColour' : {
473                                         title       : 'Main ext. colour',
474                                         value       : data.stock.exteriorColour ? data.stock.exteriorColour.name : ''
475                                 },
476                                 'interiorColour' : {
477                                         title : 'Main int. colour',
478                                         value : data.stock.interiorColour ? data.stock.interiorColour.name : ''
479                                 },
480                                 'upholstery' : {
481                                         title       : 'Upholstery',
482                                         value       : data.stock.upholstery ? data.stock.upholstery.name : ''
483                                 },
484                                 'papers' : {
485                                         title       : 'Papers',
486                                         value       : data.stock.papers ? data.stock.papers.name : ''
487                                 },
488                                 'natis' : {
489                                         title       : 'Natis',
490                                         value       : data.stock.natis ? data.stock.natis.name : ''
491                                 },
492                                 'spareKeys' : {
493                                         title       : 'Spare keys',
494                                         value       : undefined == data.stock.spareKeys
495                                                                         ? ''
496                                                                         : (data.stock.spareKeys ? 'Yes' : 'No')
497                                 },
498                                 'fullServiceHistory' : {
499                                         title       : 'Full service history',
500                                         value       : undefined == data.stock.fullServiceHistory
501                                                                         ? ''
502                                                                         : data.stock.fullServiceHistory.name
503                                 },
504                                 'registrationNumber' : {
505                                         title : 'Registration no',
506                                         value : data.stock.registrationNumber ? data.stock.registrationNumber : ''
507                                 },
508                                 'vinNumber' : {
509                                         title : 'Vin no',
510                                         value : data.stock.vinNumber ? data.stock.vinNumber : ''
511                                 },
512                                 'engineNumber' : {
513                                         title : 'Engine no',
514                                         value : data.stock.engineNumber ? data.stock.engineNumber : ''
515                                 },
516                                 'km' : {
517                                         title : 'Kms',
518                                         value : data.stock.km ? data.stock.km : ''
519                                 },
520                                 'fshNotes' : {
521                                         title : 'FSH notes',
522                                         value : data.stock.fshNotes ? data.stock.fshNotes : ''
523                                 },
524                                 'damages' : damagesHtml,
525                                 'damageTotal' : {
526                                         title : 'Total estimated repair price',
527                                         value : 'R ' + (data.stock ? data.stock.damageTotal : '0.00')
528                                 },
529                                 'damageNotes' : {
530                                         title : 'Damages comments',
531                                         value : data.stock.damageNotes ? data.stock.damageNotes : ''
532                                 },
533                                 'previousRepairsNoted' : {
534                                         title : 'Previous repairs noted',
535                                         value : data.stock.previousRepairsNoted ? 'Yes' : 'No'
536                                 },
537                                 'previousRepairsNotes' : {
538                                         title : 'Previous repairs comments',
539                                         value : data.stock.previousRepairsNotes ? data.stock.previousRepairsNotes : ''
540                                 },
541                                 'accessories' : accessoriesHtml,
542                                 'accessoryNotes' : {
543                                         title     : 'Comments',
544                                         value     : data.stock.accessoryNotes ? data.stock.accessoryNotes : ''
545                                 },
546                                 'mainImage' : {
547                                         type  : 'image',
548                                         view  : true,
549                                         id    : 'mainImage',
550                                         baseImage : 'main_img_car.jpg',
551                                         value : data.stock.mainImage ? data.stock.mainImage : ''
552                                 },
553                                 'frontImage' : {
554                                         type  : 'image',
555                                         view  : true,
556                                         id    : 'frontImage',
557                                         baseImage : 'front_car.jpg',
558                                         value : data.stock.frontImage ? data.stock.frontImage : ''
559                                 },
560                                 'leftImage' : {
561                                         type  : 'image',
562                                         view  : true,
563                                         id    : 'leftImage',
564                                         baseImage : 'left_car.jpg',
565                                         value : data.stock.leftImage ? data.stock.leftImage : ''
566                                 },
567                                 'rightImage' : {
568                                         type  : 'image',
569                                         view  : true,
570                                         id    : 'rightImage',
571                                         baseImage : 'right_car.jpg',
572                                         value : data.stock.rightImage ? data.stock.rightImage : ''
573                                 },
574                                 'backImage' : {
575                                         type  : 'image',
576                                         view  : true,
577                                         id    : 'backImage',
578                                         baseImage : 'back_car.jpg',
579                                         value : data.stock.backImage ? data.stock.backImage : ''
580                                 },
581                                 'interiorImage' : {
582                                         type  : 'image',
583                                         view  : true,
584                                         id    : 'interiorImage',
585                                         baseImage : 'interior_car.jpg',
586                                         value : data.stock.interiorImage ? data.stock.interiorImage : ''
587                                 },
588                                 'engineImage' : {
589                                         type  : 'image',
590                                         view  : true,
591                                         id    : 'engineImage',
592                                         baseImage : 'engine_car.jpg',
593                                         value : data.stock.engineImage ? data.stock.engineImage : ''
594                                 },
595                                 'natisImage' : {
596                                         type  : 'image',
597                                         view  : true,
598                                         id    : 'natisImage',
599                                         baseImage : 'copy_of_natis_car.jpg',
600                                         value : data.stock.natisImage ? data.stock.natisImage : ''
601                                 },
602
603                                 'coverReceived': data.stock.coverReceived ? data.stock.coverReceived : '',
604
605                                 'infCreated': data.created ? ((data.created).split(' '))[0] : '',
606                                 'infLoadedOnPriceGuide': data.stock.loadedOnPriceGuide
607                                         ? ((data.stock.loadedOnPriceGuide).split(' '))[0] : '',
608                                 'infTradePrice': {
609                                         title : 'Trade',
610                                         value : data.stock.tradePrice ? 'R ' + data.stock.tradePrice : '0.00'
611                                 },
612                                 'infRetailPrice': {
613                                         title : 'Retail',
614                                         value : data.stock.retailPrice ? 'R ' + data.stock.retailPrice : '0.00'
615                                 },
616                 'infListPrice': {
617                     title : 'New list price',
618                     value : data.stock.listPrice ? 'R ' + data.stock.listPrice : '0.00'
619                 },
620                                 'infCustomerPrice': {
621                                         title : 'Customer Request',
622                                         value : data.stock.valuation && data.stock.valuation.requiredPrice
623                                                         ? 'R ' + data.stock.valuation.requiredPrice
624                                                         : 'R 0.00'
625                                 },
626
627                 'infSighted': {
628                     title : 'Sighted',
629                     value : data.stock.valuation && data.stock.valuation.sighted
630                         ? (data.stock.valuation.sighted == true ? "Yes" : "No")
631                         : "No"
632                 },
633                 'infLicenseDiscExpired': {
634                     title : 'License Disc Expired',
635                     value : data.stock.valuation && data.stock.valuation.licenseDiscExpired
636                         ? (data.stock.valuation.licenseDiscExpired == true ? "Yes" : "No")
637                         : "No"
638                 },
639
640                                 'companyName' : {
641                                         title : 'Dealership name',
642                                         value : data.company ? data.company.name : ''
643                                 },
644                                 'companyContact' : {
645                                         title : 'Name and surname',
646                                         value : data.company && data.company.contact
647                                                                 ? data.company.contact.firstName + ' ' + data.company.contact.familyName
648                                                                 : ''
649                                 },
650                                 'companyMobile' : {
651                                         title : 'Mobile',
652                                         value : data.company && data.company.contact
653                                                                 ? data.company.contact.mobile
654                                                                 : ''
655                                 },
656                                 'companyEmail' : {
657                                         title : 'Email',
658                                         value : data.company && data.company.contact
659                                                                 ? data.company.contact.email
660                                                                 : ''
661                                 },
662
663                                 // Added region here
664                                 'region' : {
665                                         title : 'Region',
666                                         value : data.stock && data.stock.company
667                                                                 ? data.stock.company.region.name
668                                                                 : ''
669                                 },
670
671                                 'amountOffered' : _w.offerview.formOfferMeta ? _w.offerview.formOfferMeta : {},
672                                 'infAmountOffered' : _w.offerview.formOfferMeta ? _w.offerview.formOfferMeta.value : '0.00',
673                                 'print': {
674                                         constructor: 'Button',
675                                         items: {
676                                                 Export: {
677                                                         preset: 'Print',
678                                                         id: 'printPriceGuidePdf',
679                                                         handler: _w.offerview.printPriceGuidePdf
680                                                 }
681                                         }
682                                 }
683                         };
684                 },
685
686                 printPriceGuidePdf : function()
687                 {
688                         App.API.getTask(
689                                         'printPriceGuidePdf:' + _w.offerview.itemId, 'PriceGuide', 'Pdf.Print', _w.offerview.itemId,
690                                         {}, function() {
691                                                 App.API.execTask(
692                                                                 'printPriceGuidePdf:' + _w.offerview.itemId, {}, {},
693                                                                 function () {}, _w.offerview,
694                                                                 'EXPORT', true
695                                                 );
696                                         }, _w.taskContractError
697                         );
698                 },
699
700                 setOfferViewFormMeta : function( data )
701                 {
702                         data = (undefined == data)
703                                 ? {"stock":{}}
704                                 : data;
705                         _w.offerview.formOfferMeta = {
706                                         type  : 'input',
707                                         id    : 'amountOffered',
708                                         title : 'Offer',
709                                         value : data.amount ? data.amount : '0.00',
710                                         onChange : function () {
711                                                 _w.offerview.ti.hydrateParam('infAmountOffered', $(this).val());
712                                         },
713                                         onFocus : function()
714                                                 {
715                                                         var $_this = $(this),
716                                                                         value  = $_this.val();
717
718                                                         if( value == '0.00' )
719                                                                 {
720                                                                         $_this.val('');
721                                                                 }
722                                                 },
723                                         onBlur : function()
724                                                 {
725                                                         var $_this = $(this),
726                                                                         value  = $_this.val();
727
728                                                         if( value === '' )
729                                                                 {
730                                                                         $_this.val('0.00');
731                                                                 }
732                                                 }
733                         };
734                         _w.offerview.ti.hydrateParam('amountOffered', _w.offerview.formOfferMeta);
735                         _w.offerview.ti.hydrateParam('infAmountOffered', _w.offerview.formOfferMeta.value);
736
737                 },
738
739                 _onOfferDataAndTemplate : function ()
740                 {
741
742
743                         if ('Archived' == _w.offerview.offerData.status)
744                         {
745                                 true == App.permissions.pgUnArchive ? $('#btnUnarchive').show() : $('#btnUnarchive').hide();
746                                 $('#btnArchive').hide();
747                                 $('#optMakeOffer').hide();
748                         }
749                         else
750                         {
751                                 true == App.permissions.pgArchive ? $('#btnArchive').show() : $('#btnArchive').hide();
752                                 $('#btnUnarchive').hide();
753                         }
754                 },
755
756                 _onOfferViewDataReceived : function( response )
757                 {
758                         _w.offerview.offerId = response.Data.id;
759                         _w.offerview.offerData = response.Data;
760                         _w.offerview.setOfferViewFormMeta(response.Data);
761                         if (_w.offerview.ti.published)
762                         {
763                                 _w.offerview._onOfferDataAndTemplate();
764                         }
765                 },
766
767                 _onStockDataAndTemplate : function ()
768                 {
769                         App.DataStore.loadSelectListData(
770                                         'tuhpi', false, 'Stock', 'Tuhpi.List', null,{"Filter":
771                                         {
772                                                 "stock"                 : _w.offerview.itemData.stock.id,
773                                                 "company"               : App.companyData.id
774
775                                         }}, {},function() {
776                                         // data is retrieved
777                                         var tuHpiData = App.DataStore.getData('tuhpi');
778                                         for (var i = 0; i < tuHpiData.length; i++)
779                                         {
780                                                 var tuHpiId = tuHpiData[i].id
781                                                 var tuHpiCreated = tuHpiData[i].created
782                                                 //created
783                                                 if (tuHpiId)
784                                                 {
785                                                         $('#tuhpiDate').html(tuHpiCreated);
786                                                         $('#downloadTuhpi').show();
787                                                 }
788                                                 else
789                                                 {
790                                                         $('#tuhpiDate').html('Not Requested Yet');
791                                                         $('#downloadTuhpi').hide();
792                                                 }
793                                         }
794                         });
795
796                         if (App.permissions.settingsHpiReport == false)
797                         {
798                                 $('#requestHPIreport').hide();
799                         }
800
801
802                         if (App.permissions.settingsHpiReport)
803                         {
804                                 $('#requestHPIreport').show();
805                         }
806                         if (_w.offerview.newItem)
807                         {
808                                 if ('Open4Offers' == _w.offerview.itemData.jobState
809                                                 || 'Updateable Offers' == _w.offerview.itemData.jobState)
810                                 {
811                                         App.API.getTask(
812                                                         'createOffer', 'PriceGuide', 'Offer.Create',
813                                                         _w.offerview.itemId, {}, function(){}, _w.taskContractError
814                                                 );
815                                 }
816                         }
817                         else
818                         {
819                                 if ('Updateable Offers' == _w.offerview.itemData.jobState)
820                                 {
821                                         App.API.getTask(
822                                                 'updateOffer:' + _w.offerview.offerId, 'PriceGuide', 'Offer.Update', _w.offerview.itemId,
823                                                 {"id":_w.offerview.offerId}, $.proxy( _w.offerview._onOfferViewDataReceived, this ), _w.taskContractError
824                                         );
825                                 }
826                         }
827                         if ('Open4Offers' != _w.offerview.itemData.jobState
828                                         && 'Updateable Offers' != _w.offerview.itemData.jobState)
829                         {
830                                 $('#optMakeOffer').hide();
831                         }
832                         true == App.permissions.pgPrint ? $('#printPriceGuidePdf').show() : $('#printPriceGuidePdf').hide();
833                         if (null == App.API.taskContract('gridStockHistory'))
834                         {
835                                 App.API.getTask(
836                                                 'gridStockHistory', 'Stock', 'PricingHistory.Grid', null,
837                                                 {}, _w.offerview.loadHistoricalGrid, _w.taskContractError
838                                                 );
839                         }
840                         else
841                         {
842                                 _w.offerview.loadHistoricalGrid( null, {}, {} );
843                         }
844                 },
845
846                 _onStockViewDataReceived : function( response )
847                 {
848                         _w.offerview.itemId = response.Data.id;
849                         _w.offerview.itemData = response.Data;
850                         _w.offerview.setStockViewFormMeta(response.Data);
851                         _w.offerview.ti.hydrate(_w.offerview.formMeta);
852                         if (_w.offerview.ti.published)
853                         {
854                                 _w.offerview._onStockDataAndTemplate();
855                         }
856                 },
857
858                 _onOfferUpdated : function( response )
859                 {
860                         window.location.hash = '/priceguide';
861                 }
862
863         };
864
865 })();