Untitled
unknown
plain_text
2 months ago
10 kB
5
Indexable
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; } // Update Scenario record if needed 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 ]) { feeCodeSet.add(actualVolume.TS_COA_Fee_Code__c); if (!actualVolumeMap.containsKey(actualVolume.TS_COA_Fee_Code__c) && actualVolume.TS_COA_Fee_Code__c != null) { 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 ]) { 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('Processing Bill Point with BSID: ' + bsid + ' and Lookup Key: ' + lookupKey); if (bsid.equals('')) { continue; } if (billPointMap.containsKey(bsid)) { System.debug('BillPointMap already contains record for BSID: ' + bsid); if (bsid.equalsIgnoreCase(lookupKey)) { billPointMap.put(bsid, billPoint); } } else { billPointMap.put(bsid, billPoint); } } // Create Scenario Items and insert for (LLC_BI__Bill_Point__c billPoint : billPointMap.values()) { TS_COA_Actual_Volumes__c actualVolume = actualVolumeMap.get(billPoint.LLC_BI__LookupKey__c); if (actualVolume != null) { 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 ) ); } } // Insert Scenario Item records if (scenarioItemList.size() > 0) { insert scenarioItemList; } return 'Success'; } catch(Exception e) { return e.getMessage(); } } }
Editor is loading...
Leave a Comment