· 5 years ago · Jul 23, 2020, 04:34 PM
1/**
2 * Created by Nila on 12.06.2020.
3 */
4
5import { LightningElement, api, track, wire } from 'lwc';
6
7import getUsernameByIdApex from '@salesforce/apex/ForecastComponentController.getUsernameById';
8import getForecastDataUserApex from '@salesforce/apex/ForecastComponentController.getForecastDataUser';
9
10
11export default class ForecastUser extends LightningElement
12{
13 @track columns = [
14 { label: 'Event', fieldName: 'event' },
15 { label: 'Event Year', fieldName: 'eventYear' },
16 { label: 'Quota', fieldName: 'quota', type: 'currency', cellAttributes: { class: { fieldName: 'styleQuota' } } },
17 { label: 'Closed Won', fieldName: 'closedWonAmount', type: 'currency', cellAttributes: { class: { fieldName: 'styleClosedWonAmount' } } },
18 { label: 'Remaining Quota', fieldName: 'remainingQuota', type: 'currency', cellAttributes: { class: { fieldName: 'styleRemainingQuota' } } },
19 { label: 'Open', fieldName: 'openAmount', type: 'currency', cellAttributes: { class: { fieldName: 'styleOpen' } } }
20 ];
21
22 @track usernameById;
23 @track currentUser;
24 @track currentUserId;
25 @track forecastData;
26 @api opportunities;
27
28 onChangeSelectUsername(event)
29 {
30 this.opportunities = null;
31 this.currentUserId = event.target.value;
32 }
33
34 // Start: Serverside Functions
35 @wire(getUsernameByIdApex) getUsernameById({ error, data })
36 {
37 if(data)
38 {
39 this.usernameById = [];
40
41 for (let key in data)
42 {
43 this.usernameById.push( { value: key, label: data[key] } );
44 }
45 }
46 else if(error)
47 {
48 console.log("Error while Calling getUsernameById")
49 console.log(error)
50 }
51 };
52
53 @wire(getForecastDataUserApex, { userId: '$currentUserId' }) getForecastDataUser({ error, data })
54 {
55 if(data)
56 {
57 this.forecastData = [];
58 let tempOpportunities = [];
59
60 for (let key in data)
61 {
62 let tempData = {};
63 let closedWonAmount = 0;
64 let openAmount = 0;
65
66 for(let i = 0; i < data[key].length; i++)
67 {
68 if(i == 0)
69 {
70 tempData.event = data[key][i].Event__c;
71 tempData.eventYear = data[key][i].EventYear__c;
72 tempData.quota = data[key][i].Quota__c;
73 }
74 else
75 {
76 tempOpportunities.push(data[key][i]);
77
78 if(data[key][i].StageName == 'Closed Won')
79 closedWonAmount += data[key][i].Amount || 0;
80 else
81 openAmount += data[key][i].Amount || 0;
82 }
83 }
84
85 tempData.closedWonAmount = closedWonAmount;
86 tempData.openAmount = openAmount;
87 tempData.remainingQuota = (tempData.closedWonAmount - tempData.quota);
88 tempData.styleRemainingQuota = (tempData.remainingQuota < 0 ) ? 'slds-text-color_error' : 'slds-text-color_success';
89
90 this.forecastData.push(tempData);
91 }
92
93 if(this.forecastData.length > 0)
94 {
95 let quotaSum = 0;
96 let closedWonSum = 0;
97 let remainingQuotaSum = 0;
98 let openSum = 0;
99
100 for(let i = 0; i < this.forecastData.length; i++)
101 {
102 quotaSum += this.forecastData[i].quota;
103 closedWonSum += this.forecastData[i].closedWonAmount;
104 remainingQuotaSum += this.forecastData[i].remainingQuota;
105 openSum += this.forecastData[i].openAmount;
106 }
107
108 this.forecastData.push({
109 event: '',
110 eventYear: '',
111 quota: quotaSum,
112 closedWonAmount: closedWonSum,
113 remainingQuota: remainingQuotaSum,
114 openAmount: openSum,
115 styleQuota: 'slds-text-heading_small',
116 styleClosedWonAmount: 'slds-text-heading_small',
117 styleRemainingQuota : (remainingQuotaSum < 0 ) ? 'slds-text-color_error slds-text-heading_small' : 'slds-text-color_success slds-text-heading_small',
118 styleOpen: 'slds-text-heading_small'
119 });
120 }
121
122 if(tempOpportunities.length > 0)
123 this.opportunities = tempOpportunities;
124 }
125 else if(error)
126 {
127 console.log("Error while Calling getForecastData")
128 console.log(error)
129 }
130 };
131 // End: Serverside Functions
132}