FINERACT-1968: Advanced payment allocation strategy for Loan

This commit is contained in:
Adam Saghy
2023-08-30 13:47:30 +02:00
parent 349103015f
commit e2e10d2008
11 changed files with 341 additions and 40 deletions
@@ -43,6 +43,8 @@ import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository;
import org.apache.fineract.portfolio.loanproduct.data.AdvancedPaymentData;
import org.apache.fineract.portfolio.loanproduct.mapper.AdvancedPaymentDataMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@@ -59,6 +61,7 @@ public class InternalLoanInformationApiResource implements InitializingBean {
private final ToApiJsonSerializer<Map> toApiJsonSerializerForMap;
private final ToApiJsonSerializer<List> toApiJsonSerializerForList;
private final ApiRequestParameterHelper apiRequestParameterHelper;
private final AdvancedPaymentDataMapper advancedPaymentDataMapper;
@Override
public void afterPropertiesSet() {
@@ -126,4 +129,20 @@ public class InternalLoanInformationApiResource implements InitializingBean {
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializerForList.serialize(settings, loanIds);
}
@GET
@Path("{loanId}/advanced-payment-allocation-rules")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public List<AdvancedPaymentData> getAdvancedPaymentAllocationRulesOfLoan(@Context final UriInfo uriInfo,
@PathParam("loanId") Long loanId) {
log.warn("------------------------------------------------------------");
log.warn(" ");
log.warn("Fetching advanced payment allocation rules by loanId {}", loanId);
log.warn(" ");
log.warn("------------------------------------------------------------");
final Loan loan = loanRepositoryWrapper.findOneWithNotFoundDetection(loanId);
return advancedPaymentDataMapper.mapLoanPaymentAllocationRule(loan.getPaymentAllocationRules());
}
}
@@ -66,9 +66,11 @@ import org.apache.fineract.portfolio.loanaccount.domain.LoanCharge;
import org.apache.fineract.portfolio.loanaccount.domain.LoanCollateralManagement;
import org.apache.fineract.portfolio.loanaccount.domain.LoanDisbursementDetails;
import org.apache.fineract.portfolio.loanaccount.domain.LoanLifecycleStateMachine;
import org.apache.fineract.portfolio.loanaccount.domain.LoanPaymentAllocationRule;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleTransactionProcessorFactory;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanaccount.domain.LoanSummaryWrapper;
import org.apache.fineract.portfolio.loanaccount.domain.transactionprocessor.impl.AdvancedPaymentScheduleTransactionProcessor;
import org.apache.fineract.portfolio.loanaccount.exception.ExceedingTrancheCountException;
import org.apache.fineract.portfolio.loanaccount.exception.InvalidAmountOfCollaterals;
import org.apache.fineract.portfolio.loanaccount.exception.MultiDisbursementDataNotAllowedException;
@@ -234,7 +236,7 @@ public class LoanAssembler {
BigDecimal fixedPrincipalPercentagePerInstallment = fromApiJsonHelper
.extractBigDecimalWithLocaleNamed(LoanApiConstants.fixedPrincipalPercentagePerInstallmentParamName, element);
Loan loanApplication = null;
Loan loanApplication;
Client client = null;
Group group = null;
@@ -273,21 +275,19 @@ public class LoanAssembler {
syncDisbursementWithMeeting, fixedEmiAmount, disbursementDetails, maxOutstandingLoanBalance,
createStandingInstructionAtDisbursement, isFloatingInterestRate, interestRateDifferential, rates,
fixedPrincipalPercentagePerInstallment);
loanApplication.updateTransactionProcessingStrategy(transactionProcessingStrategyCode, transactionProcessingStrategyName);
} else if (group != null) {
loanApplication = Loan.newGroupLoanApplication(accountNo, group, loanType.getId().intValue(), loanProduct, fund, loanOfficer,
loanPurpose, transactionProcessingStrategyCode, loanProductRelatedDetail, loanCharges, null,
syncDisbursementWithMeeting, fixedEmiAmount, disbursementDetails, maxOutstandingLoanBalance,
createStandingInstructionAtDisbursement, isFloatingInterestRate, interestRateDifferential, rates,
fixedPrincipalPercentagePerInstallment);
loanApplication.updateTransactionProcessingStrategy(transactionProcessingStrategyCode, transactionProcessingStrategyName);
} else if (client != null) {
loanApplication = Loan.newIndividualLoanApplication(accountNo, client, loanType.getId().intValue(), loanProduct, fund,
loanOfficer, loanPurpose, transactionProcessingStrategyCode, loanProductRelatedDetail, loanCharges, collateral,
fixedEmiAmount, disbursementDetails, maxOutstandingLoanBalance, createStandingInstructionAtDisbursement,
isFloatingInterestRate, interestRateDifferential, rates, fixedPrincipalPercentagePerInstallment);
loanApplication.updateTransactionProcessingStrategy(transactionProcessingStrategyCode, transactionProcessingStrategyName);
} else {
loanApplication = null;
}
final String externalIdStr = this.fromApiJsonHelper.extractStringNamed("externalId", element);
@@ -297,6 +297,8 @@ public class LoanAssembler {
if (loanApplication == null) {
throw new IllegalStateException("No loan application exists for either a client or group (or both).");
}
loanApplication.updateTransactionProcessingStrategy(transactionProcessingStrategyCode, transactionProcessingStrategyName);
copyAdvancedPaymentRulesIfApplicable(transactionProcessingStrategyCode, loanProduct, loanApplication);
loanApplication.setHelpers(defaultLoanLifecycleStateMachine, this.loanSummaryWrapper,
this.loanRepaymentScheduleTransactionProcessorFactory);
@@ -359,4 +361,15 @@ public class LoanAssembler {
loanApplication.validateExpectedDisbursementForHolidayAndNonWorkingDay(workingDays, allowTransactionsOnHoliday, holidays,
allowTransactionsOnNonWorkingDay);
}
private void copyAdvancedPaymentRulesIfApplicable(String transactionProcessingStrategyCode, LoanProduct loanProduct,
Loan loanApplication) {
if (transactionProcessingStrategyCode.equals(AdvancedPaymentScheduleTransactionProcessor.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)) {
List<LoanPaymentAllocationRule> loanPaymentAllocationRules = loanProduct.getPaymentAllocationRules().stream()
.map(r -> new LoanPaymentAllocationRule(loanApplication, r.getTransactionType(), r.getAllocationTypes(),
r.getFutureInstallmentAllocationRule()))
.toList();
loanApplication.setPaymentAllocationRules(loanPaymentAllocationRules);
}
}
}
@@ -0,0 +1,49 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.mapper;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.portfolio.loanaccount.domain.LoanPaymentAllocationRule;
import org.apache.fineract.portfolio.loanproduct.data.AdvancedPaymentData;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductPaymentAllocationRule;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationType;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(config = MapstructMapperConfig.class)
public interface AdvancedPaymentDataMapper {
List<AdvancedPaymentData> mapLoanProductPaymentAllocationRule(List<LoanProductPaymentAllocationRule> paymentAllocationRule);
List<AdvancedPaymentData> mapLoanPaymentAllocationRule(List<LoanPaymentAllocationRule> paymentAllocationRule);
@Mapping(target = "paymentAllocationOrder", source = "allocationTypes")
AdvancedPaymentData mapLoanProductPaymentAllocationRule(LoanProductPaymentAllocationRule paymentAllocationRule);
@Mapping(target = "paymentAllocationOrder", source = "allocationTypes")
AdvancedPaymentData mapLoanPaymentAllocationRule(LoanPaymentAllocationRule paymentAllocationRule);
default List<AdvancedPaymentData.PaymentAllocationOrder> mapAllocationTypes(List<PaymentAllocationType> allocationTypes) {
AtomicInteger counter = new AtomicInteger(1);
return allocationTypes.stream().map(a -> new AdvancedPaymentData.PaymentAllocationOrder(a.name(), counter.getAndIncrement()))
.toList();
}
}
@@ -37,9 +37,8 @@ public class LoanProductPaymentAllocationRuleMerger {
return false;
}
boolean updated = false;
Map<PaymentAllocationTransactionType, LoanProductPaymentAllocationRule> originalItems = loanProduct
.getLoanProductPaymentAllocationRules().stream()
.collect(Collectors.toMap(LoanProductPaymentAllocationRule::getTransactionType, Function.identity()));
Map<PaymentAllocationTransactionType, LoanProductPaymentAllocationRule> originalItems = loanProduct.getPaymentAllocationRules()
.stream().collect(Collectors.toMap(LoanProductPaymentAllocationRule::getTransactionType, Function.identity()));
Map<PaymentAllocationTransactionType, LoanProductPaymentAllocationRule> newItems = newLoanProductPaymentAllocationRules.stream()
.collect(Collectors.toMap(LoanProductPaymentAllocationRule::getTransactionType, Function.identity()));
@@ -50,7 +49,7 @@ public class LoanProductPaymentAllocationRuleMerger {
if (existing.size() > 0) {
updated = true;
existing.forEach(type -> {
loanProduct.getLoanProductPaymentAllocationRules().remove(originalItems.get(type));
loanProduct.getPaymentAllocationRules().remove(originalItems.get(type));
});
}
@@ -61,7 +60,7 @@ public class LoanProductPaymentAllocationRuleMerger {
if (newSet.size() > 0) {
updated = true;
newSet.forEach(type -> {
loanProduct.getLoanProductPaymentAllocationRules().add(newItems.get(type));
loanProduct.getPaymentAllocationRules().add(newItems.get(type));
});
}
@@ -48,8 +48,8 @@ class LoanProductPaymentAllocationRuleMergerTest {
// then
Assertions.assertTrue(result);
Assertions.assertEquals(1, loanProduct.getLoanProductPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getLoanProductPaymentAllocationRules().get(0));
Assertions.assertEquals(1, loanProduct.getPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getPaymentAllocationRules().get(0));
}
@Test
@@ -61,15 +61,15 @@ class LoanProductPaymentAllocationRuleMergerTest {
LoanProductPaymentAllocationRule rule1 = createRule(DEFAULT, LAST_INSTALLMENT, List.of(DUE_INTEREST));
LoanProductPaymentAllocationRule rule2 = createRule(DEFAULT, LAST_INSTALLMENT, List.of(PAST_DUE_FEE));
loanProduct.getLoanProductPaymentAllocationRules().add(rule1);
loanProduct.getPaymentAllocationRules().add(rule1);
// when
boolean result = underTest.updateProductPaymentAllocationRules(loanProduct, List.of(rule2));
// then
Assertions.assertTrue(result);
Assertions.assertEquals(1, loanProduct.getLoanProductPaymentAllocationRules().size());
Assertions.assertEquals(PAST_DUE_FEE, loanProduct.getLoanProductPaymentAllocationRules().get(0).getAllocationTypes().get(0));
Assertions.assertEquals(1, loanProduct.getPaymentAllocationRules().size());
Assertions.assertEquals(PAST_DUE_FEE, loanProduct.getPaymentAllocationRules().get(0).getAllocationTypes().get(0));
}
@Test
@@ -81,16 +81,16 @@ class LoanProductPaymentAllocationRuleMergerTest {
LoanProductPaymentAllocationRule rule1 = createRule(DEFAULT, LAST_INSTALLMENT, List.of(DUE_INTEREST));
LoanProductPaymentAllocationRule rule2 = createRule(REPAYMENT, LAST_INSTALLMENT, List.of(PAST_DUE_FEE));
loanProduct.getLoanProductPaymentAllocationRules().addAll(List.of(rule1, rule2));
loanProduct.getPaymentAllocationRules().addAll(List.of(rule1, rule2));
// when
boolean result = underTest.updateProductPaymentAllocationRules(loanProduct, List.of(rule2, rule1));
// then
Assertions.assertFalse(result);
Assertions.assertEquals(2, loanProduct.getLoanProductPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getLoanProductPaymentAllocationRules().get(0));
Assertions.assertEquals(rule2, loanProduct.getLoanProductPaymentAllocationRules().get(1));
Assertions.assertEquals(2, loanProduct.getPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getPaymentAllocationRules().get(0));
Assertions.assertEquals(rule2, loanProduct.getPaymentAllocationRules().get(1));
}
@Test
@@ -99,14 +99,14 @@ class LoanProductPaymentAllocationRuleMergerTest {
LoanProductPaymentAllocationRuleMerger underTest = new LoanProductPaymentAllocationRuleMerger();
LoanProduct loanProduct = new LoanProduct();
LoanProductPaymentAllocationRule rule1 = createRule(DEFAULT, LAST_INSTALLMENT, List.of(DUE_INTEREST));
loanProduct.getLoanProductPaymentAllocationRules().add(rule1);
loanProduct.getPaymentAllocationRules().add(rule1);
// when
boolean result = underTest.updateProductPaymentAllocationRules(loanProduct, List.of());
// then
Assertions.assertTrue(result);
Assertions.assertEquals(0, loanProduct.getLoanProductPaymentAllocationRules().size());
Assertions.assertEquals(0, loanProduct.getPaymentAllocationRules().size());
}
@Test
@@ -115,7 +115,7 @@ class LoanProductPaymentAllocationRuleMergerTest {
LoanProductPaymentAllocationRuleMerger underTest = new LoanProductPaymentAllocationRuleMerger();
LoanProduct loanProduct = new LoanProduct();
LoanProductPaymentAllocationRule rule1 = createRule(DEFAULT, LAST_INSTALLMENT, List.of(DUE_INTEREST));
loanProduct.getLoanProductPaymentAllocationRules().add(rule1);
loanProduct.getPaymentAllocationRules().add(rule1);
LoanProductPaymentAllocationRule rule2 = createRule(REPAYMENT, LAST_INSTALLMENT, List.of(DUE_INTEREST));
// when
@@ -123,9 +123,9 @@ class LoanProductPaymentAllocationRuleMergerTest {
// then
Assertions.assertTrue(result);
Assertions.assertEquals(2, loanProduct.getLoanProductPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getLoanProductPaymentAllocationRules().get(0));
Assertions.assertEquals(rule2, loanProduct.getLoanProductPaymentAllocationRules().get(1));
Assertions.assertEquals(2, loanProduct.getPaymentAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getPaymentAllocationRules().get(0));
Assertions.assertEquals(rule2, loanProduct.getPaymentAllocationRules().get(1));
}
public LoanProductPaymentAllocationRule createRule(PaymentAllocationTransactionType transactionType,