Untitled

 avatar
unknown
plain_text
2 months ago
6.6 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
            ];

            System.debug('Retrieved Scenario: ' + scenario);

            // 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.';
            }

            // 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
            ];

            System.debug('Existing Scenario Items: ' + oldSeneriosList.size());

            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;
                System.debug('Deleted old Scenario Items for the Proposed Month');
            }

            // 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);

            System.debug('Deposit Accounts List: ' + depositAccountList);

            // 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
            ]) {
                System.debug('Processing Deposit Account: ' + depositAccount.Id);
                
                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;
                }
            }

            System.debug('Lowest Earnings Credit Rate: ' + lowestEarningsCreditRate);
            System.debug('Total Amount: ' + totalAmount);

            // 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;
                System.debug('Updated Scenario with new Earnings Credit Allowance');
            }

            // Loop through all Actual Volume records
            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
            ]) {
                if (actualVolume.TS_COA_Fee_Code__c == null) {
                    System.debug('Skipped Actual Volume with null Fee Code: ' + actualVolume.Id);
                    continue;
                }
                if (!actualVolumeMap.containsKey(actualVolume.TS_COA_Fee_Code__c)) {
                    actualVolumeMap.put(actualVolume.TS_COA_Fee_Code__c, actualVolume);
                }
            }

            System.debug('Actual Volumes Processed: ' + actualVolumeMap.size());

            // Additional mappings and item creations here...

        } catch (Exception e) {
            System.debug('Exception occurred: ' + e.getMessage());
            return e.getMessage();
        }
        return 'Success'; 
    }
}
Leave a Comment