1export function onAfterCalculate(quote, lineModels) {
2
3 var maxEffectiveEndDate = null;
4 var maxEffectiveTerm = 0;
5 if (lineModels != null) {
6 lineModels.forEach(function (line) {
7 var trueEndDate = calculateEndDate(quote, line);
8 var trueTerm = getEffectiveSubscriptionTerm(quote, line);
9 if (maxEffectiveEndDate == null || (maxEffectiveEndDate < trueEndDate)) {
10 maxEffectiveEndDate = trueEndDate;
11 }
12 if (maxEffectiveTerm < trueTerm) {
13 maxEffectiveTerm = trueTerm;
14 }
15 line.record["True_Effective_End_Date__c"] = toApexDate(trueEndDate);
16 line.record["True_Effective_Term__c"] = trueTerm;
17 });
18 quote.record["True_Effective_End_Date__c"] = toApexDate(maxEffectiveEndDate);
19 quote.record["True_Effective_Term__c"] = maxEffectiveTerm;
20 }
21 return Promise.resolve()
22}
23
24function calculateEndDate(quote, line) {
25 var sd = new Date(line.record["SBQQ__EffectiveStartDate__c"]);
26 var ed = new Date(line.record["SBQQ__EffectiveEndDate__c"]);
27 if (sd != null && ed != null ) {
28 ed = sd;
29 ed.setUTCMonth(ed.getUTCMonth() + getEffectiveSubscriptionTerm(quote, line));
30 ed.setUTCDate(ed.getUTCDate() - 1);
31 }
32 return ed;
33}
34
35function getEffectiveSubscriptionTerm(quote, line) {
36 if (line.record["SBQQ__EffectiveStartDate__c"] != null){
37 var sd = new Date(line.record["SBQQ__EffectiveStartDate__c"]);
38 }
39 if (line.record["SBQQ__EffectiveEndDate__c"] != null){
40 var ed = new Date(line.record["SBQQ__EffectiveEndDate__c"]);
41 }
42 if (sd != null && ed != null ) {
43 ed.setUTCDate(ed.getUTCDate() + 1);
44 return monthsBetween(sd, ed);
45 } else if (line.SubscriptionTerm__c != null) {
46 return line.SubscriptionTerm__c;
47 } else if (quote.SubscriptionTerm__c != null) {
48 return quote.SubscriptionTerm__c;
49 } else {
50 return line.DefaultSubscriptionTerm__c;
51 }
52}
53
54/**
55 * Takes a JS Date object and turns it into a string of the type 'YYYY-MM-DD', which is what Apex is expecting.
56 * @param {Date} date The date to be stringified
57 * @returns {string}
58 */
59function toApexDate(/*Date*/ date) {
60 if (date == null) {
61 return null;
62 }
63 // Get the ISO formatted date string.
64 // This will be formatted: YYYY-MM-DDTHH:mm:ss.sssZ
65 var dateIso = date.toISOString();
66
67 // Replace everything after the T with an empty string
68 return dateIso.replace(new RegExp('[Tt].*'), "");
69}
70
71function monthsBetween(/*Date*/ startDate, /*Date*/ endDate) {
72 if(startDate != null && endDate != null ){
73 // If the start date is actually after the end date, reverse the arguments and multiply the result by -1
74 if (startDate > endDate) {
75 return -1 * this.monthsBetween(endDate, startDate);
76 }
77 var result = 0;
78 // Add the difference in years * 12
79 result += ((endDate.getUTCFullYear() - startDate.getUTCFullYear()) * 12);
80 // Add the difference in months. Note: If startDate was later in the year than endDate, this value will be
81 // subtracted.
82 result += (endDate.getUTCMonth() - startDate.getUTCMonth());
83 return result;
84 }
85 return 0;
86}