From cfc836281ec3b1cbb9a9525d0c654df02b419300 Mon Sep 17 00:00:00 2001 From: Luke Parrott Date: Tue, 6 Dec 2022 12:41:10 -0600 Subject: [PATCH] Updating our refund logic to work with negative payments that are not created through the UI --- .../default/classes/ALLO_Allocations_TDTM.cls | 11 ++- .../default/classes/ALLO_Allocations_TEST.cls | 98 +++++++++++++++++++ 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/force-app/main/default/classes/ALLO_Allocations_TDTM.cls b/force-app/main/default/classes/ALLO_Allocations_TDTM.cls index 6e6df5fc56..a0915803a2 100644 --- a/force-app/main/default/classes/ALLO_Allocations_TDTM.cls +++ b/force-app/main/default/classes/ALLO_Allocations_TDTM.cls @@ -288,7 +288,6 @@ public class ALLO_Allocations_TDTM extends TDTM_Runnable { fillMapWrapper(pmts); paymentAmountCurrencyChange(pmts); - //paymentAmountCurrencyChange(listPmtsForProcessing); } } @@ -299,7 +298,7 @@ public class ALLO_Allocations_TDTM extends TDTM_Runnable { List refunds = new List(); List oldRefunds = new List(); for (Integer i = 0; i < newList.size(); i++) { - if (newList[i].npe01__Payment_Amount__c < 0 && String.isBlank(newList[i].Elevate_Payment_ID__c)) { + if (newList[i].npe01__Payment_Amount__c < 0 && String.isBlank(newList[i].Elevate_Payment_ID__c) && hasRefundDebitType(newList[i])) { refunds.add(newList[i]); if ( oldList != null) { oldRefunds.add(oldList[i]); @@ -327,6 +326,11 @@ public class ALLO_Allocations_TDTM extends TDTM_Runnable { } } + private Boolean hasRefundDebitType (npe01__OppPayment__c payment) { + return (payment.DebitType__c == PMT_RefundService.PARTIAL_REFUND + || payment.DebitType__c == PMT_RefundService.FULL_REFUND); + } + /******************************************************************************************************* * @description Allocations before trigger handler on GAU Allocation. Validates allocation data per * object and per parent object to avoid badly created allocations, exceeding opportunity amount, @@ -842,9 +846,10 @@ public class ALLO_Allocations_TDTM extends TDTM_Runnable { } } //if the Opportunity amount has decreased, we run the risk of allocations exceeding the total opportunity amount - if (oppWrap.totalAmount > oppWrap.parentAmount) + if (oppWrap.totalAmount > oppWrap.parentAmount) { //using addError here because we want to block opportunity update, display the error inline, and block the DML of updating all the related allocations opp.Amount.addError(Label.alloExceedsOppAmount); + } } //if we have no allocations for this opportunity, defaults are enabled, and the opportunity has an amount, make a default allocation } else if (settings.Default_Allocations_Enabled__c && opp.Amount != null) { diff --git a/force-app/main/default/classes/ALLO_Allocations_TEST.cls b/force-app/main/default/classes/ALLO_Allocations_TEST.cls index 850a75008c..f8bd1db820 100644 --- a/force-app/main/default/classes/ALLO_Allocations_TEST.cls +++ b/force-app/main/default/classes/ALLO_Allocations_TEST.cls @@ -433,6 +433,104 @@ private with sharing class ALLO_Allocations_TEST { } + /******************************************************************************************************* + * @description When working with negative and positive Payments: + * A mixture of positive and negative payments should be successfully inserted + * This test was created to ensure we are not breaking Accounting Subledger functionality + ********************************************************************************************************/ + static testMethod void pmtsWithPositiveAndNegativeAmounts() { + Date todaysDate = System.today(); + Date tomorrowsDate = System.today().addDays(1); + Date todayPlusOneMonthsDate = System.today().addMonths(1); + Date todayPlusTwoMonthsDate = System.today().addMonths(2); + Date todayPlusThreeMonthsDate = System.today().addMonths(3); + + General_Accounting_Unit__c defaultGau = new General_Accounting_Unit__c(Name='default GAU'); + insert defaultGau; + + setupSettings(new Allocations_Settings__c( + Payment_Allocations_Enabled__c = true, + Default_Allocations_Enabled__c = true, + Default__c = defaultGau.Id)); + + Account acc = new Account(Name='Account-pmtAmountChange'); + insert acc; + + List gaus = UTIL_UnitTestData_TEST.createGAUs(4); + insert gaus; + + Opportunity opp = new Opportunity(Name='Opp-pmtAmountChange', Amount = 1000, AccountID=acc.Id, CloseDate=System.today(), StageName=UTIL_UnitTestData_TEST.getOpenStage(), npe01__Do_Not_Automatically_Create_Payment__c=true); + insert opp; + + //Create Opportunity Allocation Defaults + List oppAllocations = new List(); + Allocation__c opp1Allo1 = new Allocation__c(General_Accounting_Unit__c = gaus[0].Id, + Amount__c = 200, //20% + Opportunity__c = opp.Id); + Allocation__c opp1Allo2 = new Allocation__c(General_Accounting_Unit__c = gaus[1].Id, + Amount__c = 800, //80% + Opportunity__c = opp.Id); + + oppAllocations.add(opp1Allo1); + oppAllocations.add(opp1Allo2); + + insert oppAllocations; + + List payments = new List(); + npe01__OppPayment__c p1 = new npe01__OppPayment__c(npe01__Opportunity__c = opp.Id, + npe01__Scheduled_Date__c = todaysDate, npe01__Payment_Amount__c = 400); + npe01__OppPayment__c p2 = new npe01__OppPayment__c(npe01__Opportunity__c = opp.Id, + npe01__Scheduled_Date__c = todayPlusOneMonthsDate, + npe01__Payment_Amount__c = 600); + payments.add(p1); + payments.add(p2); + + insert payments; + + opp.StageName = UTIL_UnitTestData_TEST.getClosedWonStage(); + update opp; + + payments = new List(); + + //Now Insert a mixture of payments + p1 = new npe01__OppPayment__c( + npe01__Opportunity__c = opp.Id, + npe01__Payment_Date__c = todaysDate, + npe01__Paid__c = true, + npe01__Payment_Amount__c = 300); + p2 = new npe01__OppPayment__c( + npe01__Opportunity__c = opp.Id, + npe01__Payment_Date__c = todayPlusOneMonthsDate, + npe01__Paid__c = true, + npe01__Payment_Amount__c = -200); + npe01__OppPayment__c p3 = new npe01__OppPayment__c( + npe01__Opportunity__c = opp.Id, + npe01__Payment_Date__c = todayPlusTwoMonthsDate, + npe01__Paid__c = true, + npe01__Payment_Amount__c = 500); + npe01__OppPayment__c p4 = new npe01__OppPayment__c( + npe01__Opportunity__c = opp.Id, + npe01__Payment_Date__c = todayPlusThreeMonthsDate, + npe01__Paid__c = true, + npe01__Payment_Amount__c = -600); + + payments.add(p1); + payments.add(p2); + payments.add(p3); + payments.add(p4); + + Test.startTest(); + insert payments; + Test.stopTest(); + + List queryAllo = getAllocationsOrderByPercent(p1.Id, gaus[0].Id); + System.assertEquals(300 * 0.2, queryAllo[0].Amount__c, 'The allocation amount should be 20% of the Payment'); + + queryAllo = getAllocationsOrderByPercent(p2.Id, gaus[0].Id); + System.assertEquals(-200 * 0.2, queryAllo[0].Amount__c, 'The allocation amount should be 20% of the Payment'); + + } + /******************************************************************************************************* * @description When working with negative amount Payments: * Updating the amount will adjust the percentage allocations.