Untitled

 avatar
unknown
plain_text
2 months ago
11 kB
2
Indexable
here is the debug log result
System.debug('Processing Actual Volume: ' + actualVolume.TS_COA_Fee_Code__c);
Processing Actual Volume:30
System.debug('Actual Volumes Processed: ' + actualVolumeMap.size());
Actual Volumes Processed: 29
System.debug('Processing Bill Point for Fee Code: ' + billPoint.LLC_BI__LookupKey__c);
count:33
System.debug('Scenario Items to insert: ' + scenarioItemList.size());
Scenario Items to insert: 28



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 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
            ]) {
                System.debug('Processing Actual Volume: ' + actualVolume.TS_COA_Fee_Code__c);
                
                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);
                }
            }

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

            // 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,
                LLC_BI__LookupKey__c
                FROM LLC_BI__Bill_Point__c
                WHERE LLC_BI__LookupKey__c IN :feeCodeSet
            ]) {
                System.debug('Processing Bill Point for Fee Code: ' + billPoint.LLC_BI__LookupKey__c);
                productIdSet.add(billPoint.LLC_BI__Product__c);
            }

            System.debug('Product Id Set: ' + productIdSet);

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

            System.debug('Bill Points in BillPointMap: ' + billPointMap.size());

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

            System.debug('Scenario Items to insert: ' + scenarioItemList.size());

            // 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'; 
    }
}
Leave a Comment