1global class TPM_RTRFixedFunds_AMS{
2global static List<TPM_RTRReportingWrapper_AMS.OutputRecord> getBrand(Date inputDateBegin, Date inputDateEnd, TPM_RTRReportingWrapper_AMS.InputPayload payload, List<TPM_RTRReportingWrapper_AMS.OutputRecord> output, Map<String, TPM_RTRRouting__mdt> fixedFunds){
3//This code creates a list based on the map that was passed into the method. In this List only the Fund Template
4//Names are stored so that the list can be used in the AggregateResult query (where clause). The goal is to only load records that
5//are related to the Fund Templates we're looking at.
6List<String> FundTemplateTypes = New List<String>();
7String SalesOrg;
8For(String cmID : fixedFunds.keyset()){
9FundTemplateTypes.add(fixedFunds.get(cmID).Fund_Template__c);
10if(Salesorg == Null){
11Salesorg = fixedFunds.get(cmID).Sales_Org__c;
12}
13}
14//The query returns Fund_Transaction_Row records based on the selected timeframe, customer, category and templates
15//The results are aggregated TPM_RTR_Amount values grouped by BRAND, FUND TEMPLATE
16AggregateResult[] groupedResults = [SELECT SUM(TPM_RTRAmount__c) amount,
17cgcloud__Target_Fund__r.cgcloud__Fund_Template__r.Name template,
18cgcloud__Target_Fund__r.cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c RTRRoutingFundType,
19cgcloud__Fund_Transaction__r.cgcloud__Fund_Transaction_Header__r.TPM_Product__c brand
20FROM cgcloud__Fund_Transaction_Row__c
21WHERE cgcloud__Target_Fund__r.cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c in :FundTemplateTypes
22AND cgcloud__Target_Fund__r.cgcloud__Sales_Org__c= :SalesOrg
23AND cgcloud__Fund_Transaction__r.cgcloud__Fund_Transaction_Header__r.TPM_Product__r.cgcloud__Criterion_1_Product__c IN :payload.productsfids
24AND cgcloud__Target_Fund__r.cgcloud__Anchor_Account__c IN :payload.accountsfids
25AND cgcloud__Transaction_Type__c IN ('Deposit','Withdraw')
26AND ((cgcloud__Target_Fund__r.cgcloud__Valid_From__c <= :inputDateEnd
27AND cgcloud__Target_Fund__r.cgcloud__Valid_From__c >= :inputDateBegin) OR (cgcloud__Target_Fund__r.cgcloud__Valid_Thru__c <= :inputDateEnd
28AND cgcloud__Target_Fund__r.cgcloud__Valid_Thru__c >= :inputDateBegin))
29GROUP BY cgcloud__Fund_Transaction__r.cgcloud__Fund_Transaction_Header__r.TPM_Product__c, cgcloud__Target_Fund__r.cgcloud__Fund_Template__r.Name, cgcloud__Target_Fund__r.cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c];
30String brand, template, RTRRoutingFundType;
31Double amount;
32Map<String, Decimal> templateMap;
33Map<String, Map<String, Decimal>> cmap = New Map<String, Map<String, Decimal>>();
34Map<String, String> RTRRoutingFundTypeMap = New Map<String, String>();
35//This For-Loop works on the aggregateResults and allocates all the records to their respective "place" -
36//it basically sorts all data into the right maps
37For(AggregateResult ar : groupedResults){
38brand = String.valueOf(ar.get('brand')); //the last part of this line retrieved the brand id of our query (brand is an alias)
39template = String.valueOf(ar.get('template')); //the last part of this line retrieved the template Name of our query (template is an alias)
40RTRRoutingFundType = String.valueOf(ar.get('RTRRoutingFundType')); //the last part of this line retrieved the RTR Routing Fund Type of our query (RTRRoutingFundType is an alias)'));
41amount = Double.valueOf(ar.get('amount')); //the last part of this line retrieved the aggregated amount of our query (amount is an alias)
42templateMap = New Map<String, Decimal>();
43If(!cmap.containsKey(brand)){
44templateMap.put(template, amount);
45cmap.put(brand, templateMap);
46}
47else {
48templateMap = cmap.get(brand);
49if(!templateMap.containsKey(template)){
50templateMap.put(template, amount);
51cmap.put(brand, templateMap);
52}
53}
54if(!RTRRoutingFundTypeMap.containskey(RTRRoutingFundType)){
55RTRRoutingFundTypeMap.put(RTRRoutingFundType, template);
56}
57}
58//Once all the results of our query are allocated and sorted, the data needs to be written back to the report
59//For each brand in cmap,
60For(String brandId : cmap.keyset()){
61//and each template for that brand,
62For(String templateId : cmap.get(brandId).keyset()){
63//Now we loop trough the map that was passed to this method
64For(String ffID : fixedFunds.keyset()){
65//to check to which KPI the map we're currently in is corrosponding to
66If(templateId == RTRRoutingFundTypeMap.get(fixedFunds.get(ffID).Fund_Template__c)){
67//If the map we're working on right now has the RDF Fund Template, and one of the records in our map has
68//the RDF Fund Template assigned, we write back this KPI to the corrosponding KPI Definition of the Custom Meta Data
69output.add(new TPM_RTRReportingWrapper_AMS.OutputRecord(String.valueOf(brandId), String.valueOf(fixedFunds.get(ffID).KPI_Definition__c), 'Total', Double.valueOf(cmap.get(brandId).get(templateId))));
70}
71}
72}
73}
74return output;
75}
76global static List<TPM_RTRReportingWrapper_AMS.OutputRecord> getCategory(Date inputDateBegin, Date inputDateEnd, TPM_RTRReportingWrapper_AMS.InputPayload payload, List<TPM_RTRReportingWrapper_AMS.OutputRecord> output, Map<String, TPM_RTRRouting__mdt> fixedFunds){
77//This code creates a list based on the map that was passed into the method. In this List only the Fund Template
78//Names are stored so that the list can be used in the AggregateResult query. The goal is to only load records that
79//are related to the Fund Templates we're looking at.
80List<String> FundTemplateTypes = New List<String>();
81String SalesOrg;
82For(String cmID : fixedFunds.keyset()){
83FundTemplateTypes.add(fixedFunds.get(cmID).Fund_Template__c);
84if(SalesOrg == Null){
85SalesOrg = fixedFunds.get(cmID).Sales_Org__c;
86}
87}
88//Calculation of the current week number
89Date todaysDate = date.today();
90Date todaysDateInstance = date.newInstance(todaysdate.year(), todaysdate.month(), todaysdate.day());
91todaysdate.day();
92Integer currentyear = todaysdate.year();
93Date startDate = date.newInstance(currentyear, 1,1);
94Integer numberDaysDue = startDate.daysBetween(todaysDateInstance);
95Integer numberOfWeek = math.mod(Integer.valueOf(math.floor((numberDaysDue))/7),52)+1;
96//Support Map for NA_REP_FixedFund_A Calculation
97Map<String, Decimal> mPrdAmounts = new Map<String, Decimal>();
98//Query
99AggregateResult[] groupedResults = [SELECT SUM(cgcloud__Deposits_Approved__c) sumValue,
100cgcloud__Anchor_Product__c,
101cgcloud__Fund_Template__c,
102cgcloud__Fund_Template__r.Name tmplName,
103cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c RTRRoutingFundType
104FROM cgcloud__Fund__c
105WHERE cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c IN :FundTemplateTypes
106AND cgcloud__Sales_Org__c= :SalesOrg
107AND cgcloud__Anchor_Product__c IN :payload.productsfids
108AND cgcloud__Anchor_Account__c IN :payload.accountsfids
109AND ((cgcloud__Valid_From__c <= :inputDateEnd
110AND cgcloud__Valid_From__c >= :inputDateBegin)
111OR (cgcloud__Valid_Thru__c <= :inputDateEnd
112AND cgcloud__Valid_Thru__c >= :inputDateBegin))
113GROUP BY cgcloud__Anchor_Product__c, cgcloud__Fund_Template__c, cgcloud__Fund_Template__r.Name, cgcloud__Fund_Template__r.TPM_RTRRoutingFundType__c];
114For (AggregateResult ar : groupedResults){
115//Check if current product is not already in the map, if so, add to the map with value 0
116If(!mPrdAmounts.containsKey(String.valueOf(ar.get('cgcloud__Anchor_Product__c')))){
117mPrdAmounts.put(String.valueOf(ar.get('cgcloud__Anchor_Product__c')), 0);
118}
119//Add the sum of the aggregateResult to the existing value in map for current product
120mPrdAmounts.put(String.valueOf(ar.get('cgcloud__Anchor_Product__c')), mPrdAmounts.get(String.valueOf(ar.get('cgcloud__Anchor_Product__c'))) + Double.valueOf(ar.get('sumValue')));
121//Check Fund Template and add KPI
122For(String qKPI : fixedFunds.keyset()){
123If(String.valueOf(ar.get('RTRRoutingFundType')) == fixedFunds.get(qKPI).Fund_Template__c){
124output.add(new TPM_RTRReportingWrapper_AMS.OutputRecord(String.valueOf(ar.get('cgcloud__Anchor_Product__c')), String.valueOf(fixedFunds.get(qKPI).KPI_Definition__c), 'Total', Double.valueOf(ar.get('sumValue'))));
125}
126}
127}
128//Add NA_REP_FixedFund_A if required
129If(fixedFunds.containsKey('NA_REP_FixedFund_A')){
130For(String cdr : mPrdAmounts.keyset()){
131output.add(new TPM_RTRReportingWrapper_AMS.OutputRecord(String.valueOf(cdr), 'NA_REP_FixedFund_A', 'Total', Double.valueOf((mPrdAmounts.get(cdr)*numberOfWeek/52))));
132}
133}
134return output;
135}
136}