Untitled
/* Apex Controller class for the Aura component wmx_ScenarionHandler. West Monroe, April 2022 */ public without sharing class LightningScenarioHandler { @AuraEnabled public static String getScenarioItems(String recordId) { //local vars List<Id> depositAccountList = new List<Id>(); List<LLC_BI__Scenario_Item__c> scenarioItemList = new List<LLC_BI__Scenario_Item__c>(); Set<String> feeCodeSet = new Set<String>(); Set<id> productIdSet = new Set<id>(); Map<String, TS_COA_Actual_Volumes__c> actualVolumeMap = new Map<String, TS_COA_Actual_Volumes__c>(); Boolean isChanged = false; Map<String, LLC_BI__Bill_Point__c> billPointMap = new Map<String, LLC_BI__Bill_Point__c>(); try{ //Retrieve current Scenario record LLC_BI__Scenario__c scenario = [ SELECT Id, WMX_Proposed_Month__c, LLC_BI__Earnings_Credit_Allowance__c, LLC_BI__Average_Ledger_Balance__c, TS_COA_Effective_Date__c, WMX_Deposit_Account__c, WMX_Deposit_Account_2__c, WMX_Deposit_Account_3__c, WMX_Deposit_Account_4__c, WMX_Deposit_Account_5__c FROM LLC_BI__Scenario__c WHERE Id = :recordId ]; // Ensure Proposed Month and Deposit Account are not null if(scenario.WMX_Deposit_Account__c == null || scenario.WMX_Proposed_Month__c == null){ return 'Lead Account and Proposed Month are required to import actual volumes.'; } // Check if Proposed Month year matches the current year String proposedMonthYear = scenario.WMX_Proposed_Month__c != null ? scenario.WMX_Proposed_Month__c.substring(0, 4) : ''; String currentYear = String.valueOf(Date.today().year()); if (proposedMonthYear != currentYear) { return 'The Proposed Month must be within the current year (' + currentYear + ').'; } // Retrieve existing Scenario Items for the proposed month List<LLC_BI__Scenario_Item__c> oldSeneriosList = [ SELECT id, LLC_BI__Is_Monthly__c, WMX_Proposed_Month__c FROM LLC_BI__Scenario_Item__c WHERE LLC_BI__Scenario__c = :recordId AND TS_COA_Imported_Volume__c = TRUE ]; Boolean isReturnMsg = false; for(LLC_BI__Scenario_Item__c sItem:oldSeneriosList){ if(sItem.WMX_Proposed_Month__c == scenario.WMX_Proposed_Month__c){ isReturnMsg = true; } } if(isReturnMsg){ return 'Volumes for the month of '+scenario.WMX_Proposed_Month__c+' have already been imported. Please change the Proposed Month and try again.'; } else if(oldSeneriosList.size() >0 ){ delete oldSeneriosList; } } //Add all Deposit Accounts tied to this Proforma in a list depositAccountList.add(scenario.WMX_Deposit_Account__c); depositAccountList.add(scenario.WMX_Deposit_Account_2__c); depositAccountList.add(scenario.WMX_Deposit_Account_3__c); depositAccountList.add(scenario.WMX_Deposit_Account_4__c); depositAccountList.add(scenario.WMX_Deposit_Account_5__c); //Loop through the Deposit Accounts and get the lowest Earnings Credit Rate and a Sum of all Amounts Decimal lowestEarningsCreditRate = 0.0; Decimal totalAmount = 0.0; isChanged = false; for (LLC_BI__Deposit__c depositAccount : [ SELECT TS_COA_Earnings_Credit_Rate__c, LLC_BI__Amount__c FROM LLC_BI__Deposit__c WHERE Id IN :depositAccountList ]) { if ((lowestEarningsCreditRate == 0.0) && (depositAccount.TS_COA_Earnings_Credit_Rate__c != null)) { lowestEarningsCreditRate = Decimal.valueOf(depositAccount.TS_COA_Earnings_Credit_Rate__c); } else if ( (depositAccount.TS_COA_Earnings_Credit_Rate__c != null) && (Decimal.valueOf(depositAccount.TS_COA_Earnings_Credit_Rate__c) < lowestEarningsCreditRate) ) { lowestEarningsCreditRate = Decimal.valueOf(depositAccount.TS_COA_Earnings_Credit_Rate__c); } if (depositAccount.LLC_BI__Amount__c != null) { totalAmount += depositAccount.LLC_BI__Amount__c; } } //Copy the lowest earnings credit rate from the deposit accounts and total amount to the Scenario record if (lowestEarningsCreditRate > 0.0) { scenario.LLC_BI__Earnings_Credit_Allowance__c = lowestEarningsCreditRate; isChanged = true; } /*if (totalAmount > 0.0) { scenario.LLC_BI__Average_Ledger_Balance__c = totalAmount; isChanged = true; }*/ if (isChanged) { update scenario; } //Loop through all Actual Volume records for the Scenario's Proposed Month and any of the Deposit accounts on the Proforma for (TS_COA_Actual_Volumes__c actualVolume : [ SELECT TS_COA_Fee_Code__c, TS_COA_Net_Fee_Amount__c, TS_COA_S_C_Date__c, TS_COA_Counter__c FROM TS_COA_Actual_Volumes__c WHERE TS_COA_Proposed_Month_Name__c = :scenario.WMX_Proposed_Month__c AND TS_COA_Deposit_Account__c IN :depositAccountList AND CALENDAR_YEAR(TS_COA_S_C_Date__c) = :Date.today().year() // Ensure volumes are from the current year ]) { //Add Found Fee Codes to a set to query on feeCodeSet.add(actualVolume.TS_COA_Fee_Code__c); if (!actualVolumeMap.containsKey(actualVolume.TS_COA_Fee_Code__c) && actualVolume.TS_COA_Fee_Code__c != null ) { //Add to Map that will be used to match a BSID to Actual Volumes record actualVolumeMap.put(actualVolume.TS_COA_Fee_Code__c, actualVolume); } } //Loop through Bill Point records where BSID = Fee Code from Actual Volume record and add Product Id from matching Bill Point records into a Set for (LLC_BI__Bill_Point__c billPoint : [ SELECT LLC_BI__Product__c FROM LLC_BI__Bill_Point__c WHERE LLC_BI__LookupKey__c IN :feeCodeSet ]) { //Add all product IDs associated with found BillPoints productIdSet.add(billPoint.LLC_BI__Product__c); } //Loop through Bill Point records where Product Id = Product Ids from previously retrieved Bill Point records for (LLC_BI__Bill_Point__c billPoint : [ SELECT Id, LLC_BI__LookupKey__c, LLC_BI__Price__c, Name, LLC_BI__Product__c, LLC_BI__Product__r.LLC_BI__Product_Type__r.Name, LLC_BI__Is_Monthly__c, LLC_BI__lookup_Key__c FROM LLC_BI__Bill_Point__c WHERE LLC_BI__Product__c IN :productIdSet ORDER BY LLC_BI__lookup_Key__c ]) { String bsid = billPoint.LLC_BI__LookupKey__c != null ? (billPoint.LLC_BI__LookupKey__c).trim() : ''; String lookupKey = billPoint.LLC_BI__lookup_Key__c != null ? (billPoint.LLC_BI__lookup_Key__c).trim() : ''; System.debug('CPA Processing Bill Point record with BSID: ' + bsid + ' and Lookup Key: ' + lookupKey); //Skip record if bsid is a null string if (bsid.equals('')) { continue; } //In case more than one Bill Point record exists for the same BSID, we want to choose the one where BSID = Lookup Key if (billPointMap.containsKey(bsid)) { System.debug('CPA billPointMap already contains a record for BSID: ' + bsid); System.debug( 'CPA existing Bill Point record has lookup key: ' + (billPointMap.get(bsid)).LLC_BI__lookup_Key__c ); if (bsid.equalsIgnoreCase(lookupKey)) { System.debug('CPA replacing existing Bill Point record with the incoming one'); billPointMap.put(bsid, billPoint); } else { System.debug('CPA skipping this Bill Point with duplicate BSID'); } } else { System.debug('CPA adding Bill Point to billPointMap for BSID: ' + bsid + ' and Lookup Key: ' + lookupKey); billPointMap.put(bsid, billPoint); } } //Loop through the Bill Point records queried last for (LLC_BI__Bill_Point__c billPoint : billPointMap.values()) { //get the actual Volume record associated with that billpoint TS_COA_Actual_Volumes__c actualVolume = actualVolumeMap.get(billPoint.LLC_BI__LookupKey__c); if (actualVolume != null) { //Create Scenario Item based on each billpoint record, including Actual volume information scenarioItemList.add( new LLC_BI__Scenario_Item__c( LLC_BI__Bill_Point__c = billPoint.Id, LLC_BI__Exception_Price__c = actualVolume.TS_COA_Net_Fee_Amount__c, poc__c = actualVolume.TS_COA_S_C_Date__c, LLC_BI__Volumes__c = Decimal.valueOf(actualVolume.TS_COA_Counter__c), WMX_Proposed_Month__c = scenario.WMX_Proposed_Month__c, LLC_BI__Scenario__c = scenario.Id, LLC_BI__Product__c = billPoint.LLC_BI__Product__c, Name = billPoint.Name, TS_COA_Imported_Volume__c = True, TS_COA_Imported__c = 'Yes', LLC_BI__Standard_Price__c = billPoint.LLC_BI__Price__c, LLC_BI__Product_Type_Name__c = billPoint.LLC_BI__Product__r.LLC_BI__Product_Type__r.Name, LLC_BI__Is_Monthly__c = billPoint.LLC_BI__Is_Monthly__c ) ); } } System.debug('CPA ScenarioItemList to create is: ' + scenarioItemList); //Insert Scenario Item records if (scenarioItemList.size() > 0) { insert scenarioItemList; } else { return 'No volumes available to import for month of '+scenario.WMX_Proposed_Month__c+'. Please update the Proposed Month and try again.'; } } catch(Exception e){ return e.getMessage(); } return 'Success'; } //getScenarioItems }
Leave a Comment