FINERACT-2042 Configurable CreditAllocations for Loan Product

This commit is contained in:
Peter Bagrij
2024-01-31 09:25:23 +01:00
committed by Peter Bagrij
parent 02cf6749af
commit cca520125b
19 changed files with 1206 additions and 39 deletions
@@ -81,6 +81,8 @@ import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanSchedul
import org.apache.fineract.portfolio.loanproduct.LoanProductConstants;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductData;
import org.apache.fineract.portfolio.loanproduct.data.TransactionProcessingStrategyData;
import org.apache.fineract.portfolio.loanproduct.domain.AllocationType;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationType;
@@ -417,6 +419,8 @@ public class LoanProductsApiResource {
final List<EnumOptionData> advancedPaymentAllocationFutureInstallmentAllocationRules = FutureInstallmentAllocationRule
.getValuesAsEnumOptionDataList();
final List<EnumOptionData> advancedPaymentAllocationTypes = PaymentAllocationType.getValuesAsEnumOptionDataList();
final List<EnumOptionData> creditAllocationTransactionTypes = CreditAllocationTransactionType.getValuesAsEnumOptionDataList();
final List<EnumOptionData> creditAllocationAllocationTypes = AllocationType.getValuesAsEnumOptionDataList();
return new LoanProductData(productData, chargeOptions, penaltyOptions, paymentTypeOptions, currencyOptions, amortizationTypeOptions,
interestTypeOptions, interestCalculationPeriodTypeOptions, repaymentFrequencyTypeOptions, interestRateFrequencyTypeOptions,
@@ -427,7 +431,8 @@ public class LoanProductsApiResource {
interestRecalculationDayOfWeekTypeOptions, isRatesEnabled, delinquencyBucketOptions, repaymentStartDateTypeOptions,
advancedPaymentAllocationTransactionTypes, advancedPaymentAllocationFutureInstallmentAllocationRules,
advancedPaymentAllocationTypes, LoanScheduleType.getValuesAsEnumOptionDataList(),
LoanScheduleProcessingType.getValuesAsEnumOptionDataList());
LoanScheduleProcessingType.getValuesAsEnumOptionDataList(), creditAllocationTransactionTypes,
creditAllocationAllocationTypes);
}
}
@@ -114,6 +114,7 @@ final class LoanProductsApiResourceSwagger {
@Schema(example = "mifos-standard-strategy")
public String transactionProcessingStrategyCode;
public List<AdvancedPaymentData> paymentAllocation;
public List<CreditAllocationData> creditAllocation;
@Schema(example = "false")
public Boolean isLinkedToFloatingInterestRates;
@Schema(example = "false")
@@ -1042,6 +1043,10 @@ final class LoanProductsApiResourceSwagger {
public List<EnumOptionData> advancedPaymentAllocationTypes;
public List<EnumOptionData> loanScheduleTypeOptions;
public List<EnumOptionData> loanScheduleProcessingTypeOptions;
public List<EnumOptionData> creditAllocationAllocationTypes;
public List<EnumOptionData> creditAllocationTransactionTypes;
}
@Schema(description = "GetLoanProductsProductIdResponse")
@@ -1222,6 +1227,8 @@ final class LoanProductsApiResourceSwagger {
@Schema(example = "[]")
public List<AdvancedPaymentData> paymentAllocation;
@Schema(example = "[]")
public List<CreditAllocationData> creditAllocation;
@Schema(example = "[]")
public List<Integer> charges;
public Set<GetLoanProductsPrincipalVariationsForBorrowerCycle> productsPrincipalVariationsForBorrowerCycle;
@Schema(example = "[]")
@@ -1343,7 +1350,10 @@ final class LoanProductsApiResourceSwagger {
public Integer interestCalculationPeriodType;
@Schema(example = "mifos-standard-strategy")
public String transactionProcessingStrategyCode;
@Schema(example = "[]")
public List<AdvancedPaymentData> paymentAllocation;
@Schema(example = "[]")
public List<CreditAllocationData> creditAllocation;
@Schema(example = "false")
public Boolean isLinkedToFloatingInterestRates;
@Schema(example = "false")
@@ -1564,6 +1574,23 @@ final class LoanProductsApiResourceSwagger {
public Integer order;
}
public static final class CreditAllocationData {
@Schema(example = "Chargeback")
public String transactionType;
@Schema(example = "[]")
public List<CreditAllocationOrder> creditAllocationOrder;
}
public static class CreditAllocationOrder {
@Schema(example = "PENALTY")
public String creditAllocationRule;
@Schema(example = "1")
public Integer order;
}
@Schema(description = "PutLoanProductsProductIdResponse")
public static final class PutLoanProductsProductIdResponse {
@@ -0,0 +1,40 @@
/**
* 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.data;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class CreditAllocationData implements Serializable {
private final String transactionType;
private final List<CreditAllocationOrder> creditAllocationOrder;
@Getter
@AllArgsConstructor
public static class CreditAllocationOrder implements Serializable {
private final String creditAllocationRule;
private final Integer order;
}
}
@@ -46,7 +46,9 @@ import org.apache.fineract.portfolio.fund.data.FundData;
import org.apache.fineract.portfolio.loanaccount.data.LoanInterestRecalculationData;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanScheduleProcessingType;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanScheduleType;
import org.apache.fineract.portfolio.loanproduct.domain.AllocationType;
import org.apache.fineract.portfolio.loanproduct.domain.AmortizationMethod;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule;
import org.apache.fineract.portfolio.loanproduct.domain.InterestCalculationPeriodMethod;
import org.apache.fineract.portfolio.loanproduct.domain.InterestMethod;
@@ -116,6 +118,7 @@ public class LoanProductData implements Serializable {
private final String transactionProcessingStrategyCode;
private final String transactionProcessingStrategyName;
private final Collection<AdvancedPaymentData> paymentAllocation;
private final Collection<CreditAllocationData> creditAllocation;
private final Integer graceOnPrincipalPayment;
private final Integer recurringMoratoriumOnPrincipalPeriods;
private final Integer graceOnInterestPayment;
@@ -175,6 +178,10 @@ public class LoanProductData implements Serializable {
private final List<EnumOptionData> advancedPaymentAllocationTransactionTypes;
private final List<EnumOptionData> advancedPaymentAllocationFutureInstallmentAllocationRules;
private final List<EnumOptionData> advancedPaymentAllocationTypes;
private final List<EnumOptionData> creditAllocationTransactionTypes;
private final List<EnumOptionData> creditAllocationAllocationTypes;
private final List<EnumOptionData> loanScheduleTypeOptions;
private final List<EnumOptionData> loanScheduleProcessingTypeOptions;
@@ -306,6 +313,7 @@ public class LoanProductData implements Serializable {
final boolean enableDownPayment = false;
final BigDecimal disbursedAmountPercentageDownPayment = null;
final Collection<AdvancedPaymentData> paymentAllocation = null;
final Collection<CreditAllocationData> creditAllocation = null;
final boolean enableAutoRepaymentForDownPayment = false;
final EnumOptionData repaymentStartDateType = null;
final boolean enableInstallmentLevelDelinquency = false;
@@ -332,7 +340,8 @@ public class LoanProductData implements Serializable {
syncExpectedWithDisbursementDate, canUseForTopup, isEqualAmortization, rateOptions, rates, isRatesEnabled,
fixedPrincipalPercentagePerInstallment, delinquencyBucketOptions, delinquencyBucket, dueDaysForRepaymentEvent,
overDueDaysForRepaymentEvent, enableDownPayment, disbursedAmountPercentageDownPayment, enableAutoRepaymentForDownPayment,
paymentAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType, loanScheduleProcessingType);
paymentAllocation, creditAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType,
loanScheduleProcessingType);
}
@@ -426,6 +435,7 @@ public class LoanProductData implements Serializable {
final BigDecimal disbursedAmountPercentageDownPayment = null;
final boolean enableAutoRepaymentForDownPayment = false;
final Collection<AdvancedPaymentData> paymentAllocation = null;
final Collection<CreditAllocationData> creditAllocation = null;
final EnumOptionData repaymentStartDateType = null;
final boolean enableInstallmentLevelDelinquency = false;
final EnumOptionData loanScheduleType = null;
@@ -449,7 +459,8 @@ public class LoanProductData implements Serializable {
syncExpectedWithDisbursementDate, canUseForTopup, isEqualAmortization, rateOptions, rates, isRatesEnabled,
fixedPrincipalPercentagePerInstallment, delinquencyBucketOptions, delinquencyBucket, dueDaysForRepaymentEvent,
overDueDaysForRepaymentEvent, enableDownPayment, disbursedAmountPercentageDownPayment, enableAutoRepaymentForDownPayment,
paymentAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType, loanScheduleProcessingType);
paymentAllocation, creditAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType,
loanScheduleProcessingType);
}
@@ -550,6 +561,7 @@ public class LoanProductData implements Serializable {
final BigDecimal disbursedAmountPercentageDownPayment = null;
final boolean enableAutoRepaymentForDownPayment = false;
final Collection<AdvancedPaymentData> paymentAllocation = null;
final Collection<CreditAllocationData> creditAllocation = null;
final EnumOptionData repaymentStartDateType = LoanEnumerations.repaymentStartDateType(RepaymentStartDateType.DISBURSEMENT_DATE);
final boolean enableInstallmentLevelDelinquency = false;
final EnumOptionData loanScheduleType = LoanScheduleType.CUMULATIVE.asEnumOptionData();
@@ -573,7 +585,8 @@ public class LoanProductData implements Serializable {
syncExpectedWithDisbursementDate, canUseForTopup, isEqualAmortization, rateOptions, rates, isRatesEnabled,
fixedPrincipalPercentagePerInstallment, delinquencyBucketOptions, delinquencyBucket, dueDaysForRepaymentEvent,
overDueDaysForRepaymentEvent, enableDownPayment, disbursedAmountPercentageDownPayment, enableAutoRepaymentForDownPayment,
paymentAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType, loanScheduleProcessingType);
paymentAllocation, creditAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType,
loanScheduleProcessingType);
}
@@ -668,6 +681,7 @@ public class LoanProductData implements Serializable {
final BigDecimal disbursedAmountPercentageDownPayment = null;
final boolean enableAutoRepaymentForDownPayment = false;
final Collection<AdvancedPaymentData> paymentAllocation = null;
final Collection<CreditAllocationData> creditAllocationData = null;
final EnumOptionData repaymentStartDateType = LoanEnumerations.repaymentStartDateType(RepaymentStartDateType.DISBURSEMENT_DATE);
final boolean enableInstallmentLevelDelinquency = false;
final EnumOptionData loanScheduleType = null;
@@ -691,7 +705,8 @@ public class LoanProductData implements Serializable {
syncExpectedWithDisbursementDate, canUseForTopup, isEqualAmortization, rateOptions, rates, isRatesEnabled,
fixedPrincipalPercentagePerInstallment, delinquencyBucketOptions, delinquencyBucket, dueDaysForRepaymentEvent,
overDueDaysForRepaymentEvent, enableDownPayment, disbursedAmountPercentageDownPayment, enableAutoRepaymentForDownPayment,
paymentAllocation, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType, loanScheduleProcessingType);
paymentAllocation, creditAllocationData, repaymentStartDateType, enableInstallmentLevelDelinquency, loanScheduleType,
loanScheduleProcessingType);
}
public static LoanProductData withAccountingDetails(final LoanProductData productData, final Map<String, Object> accountingMappings,
@@ -739,9 +754,9 @@ public class LoanProductData implements Serializable {
final DelinquencyBucketData delinquencyBucket, final Integer dueDaysForRepaymentEvent,
final Integer overDueDaysForRepaymentEvent, final boolean enableDownPayment,
final BigDecimal disbursedAmountPercentageForDownPayment, final boolean enableAutoRepaymentForDownPayment,
final Collection<AdvancedPaymentData> paymentAllocation, final EnumOptionData repaymentStartDateType,
final boolean enableInstallmentLevelDelinquency, final EnumOptionData loanScheduleType,
final EnumOptionData loanScheduleProcessingType) {
final Collection<AdvancedPaymentData> paymentAllocation, final Collection<CreditAllocationData> creditAllocation,
final EnumOptionData repaymentStartDateType, final boolean enableInstallmentLevelDelinquency,
final EnumOptionData loanScheduleType, final EnumOptionData loanScheduleProcessingType) {
this.id = id;
this.name = name;
this.shortName = shortName;
@@ -862,12 +877,15 @@ public class LoanProductData implements Serializable {
this.enableDownPayment = enableDownPayment;
this.disbursedAmountPercentageForDownPayment = disbursedAmountPercentageForDownPayment;
this.paymentAllocation = paymentAllocation;
this.creditAllocation = creditAllocation;
this.enableAutoRepaymentForDownPayment = enableAutoRepaymentForDownPayment;
this.repaymentStartDateType = repaymentStartDateType;
this.repaymentStartDateTypeOptions = null;
this.advancedPaymentAllocationTransactionTypes = PaymentAllocationTransactionType.getValuesAsEnumOptionDataList();
this.advancedPaymentAllocationFutureInstallmentAllocationRules = FutureInstallmentAllocationRule.getValuesAsEnumOptionDataList();
this.advancedPaymentAllocationTypes = PaymentAllocationType.getValuesAsEnumOptionDataList();
this.creditAllocationTransactionTypes = CreditAllocationTransactionType.getValuesAsEnumOptionDataList();
this.creditAllocationAllocationTypes = AllocationType.getValuesAsEnumOptionDataList();
this.enableInstallmentLevelDelinquency = enableInstallmentLevelDelinquency;
this.loanScheduleType = loanScheduleType;
this.loanScheduleProcessingType = loanScheduleProcessingType;
@@ -893,7 +911,9 @@ public class LoanProductData implements Serializable {
final List<EnumOptionData> advancedPaymentAllocationTransactionTypes,
final List<EnumOptionData> advancedPaymentAllocationFutureInstallmentAllocationRules,
final List<EnumOptionData> advancedPaymentAllocationTypes, final List<EnumOptionData> loanScheduleTypeOptions,
final List<EnumOptionData> loanScheduleProcessingTypeOptions) {
final List<EnumOptionData> loanScheduleProcessingTypeOptions, final List<EnumOptionData> creditAllocationTransactionTypes,
final List<EnumOptionData> creditAllocationAllocationTypes) {
this.id = productData.id;
this.name = productData.name;
this.shortName = productData.shortName;
@@ -1032,11 +1052,14 @@ public class LoanProductData implements Serializable {
this.disbursedAmountPercentageForDownPayment = productData.disbursedAmountPercentageForDownPayment;
this.enableAutoRepaymentForDownPayment = productData.enableAutoRepaymentForDownPayment;
this.paymentAllocation = productData.paymentAllocation;
this.creditAllocation = productData.creditAllocation;
this.repaymentStartDateType = productData.repaymentStartDateType;
this.repaymentStartDateTypeOptions = repaymentStartDateTypeOptions;
this.advancedPaymentAllocationTransactionTypes = advancedPaymentAllocationTransactionTypes;
this.advancedPaymentAllocationFutureInstallmentAllocationRules = advancedPaymentAllocationFutureInstallmentAllocationRules;
this.advancedPaymentAllocationTypes = advancedPaymentAllocationTypes;
this.creditAllocationAllocationTypes = creditAllocationAllocationTypes;
this.creditAllocationTransactionTypes = creditAllocationTransactionTypes;
this.enableInstallmentLevelDelinquency = productData.enableInstallmentLevelDelinquency;
this.loanScheduleType = productData.getLoanScheduleType();
this.loanScheduleProcessingType = productData.getLoanScheduleProcessingType();
@@ -91,6 +91,7 @@ public final class LoanProductDataValidator {
public static final String IN_ARREARS_TOLERANCE = "inArrearsTolerance";
public static final String TRANSACTION_PROCESSING_STRATEGY_CODE = "transactionProcessingStrategyCode";
public static final String ADVANCED_PAYMENT_ALLOCATIONS = "paymentAllocation";
public static final String CREDIT_ALLOCATIONS = "creditAllocation";
public static final String GRACE_ON_PRINCIPAL_PAYMENT = "graceOnPrincipalPayment";
public static final String GRACE_ON_INTEREST_PAYMENT = "graceOnInterestPayment";
public static final String GRACE_ON_INTEREST_CHARGED = "graceOnInterestCharged";
@@ -114,17 +115,18 @@ public final class LoanProductDataValidator {
NUMBER_OF_REPAYMENTS, MIN_NUMBER_OF_REPAYMENTS, MAX_NUMBER_OF_REPAYMENTS, REPAYMENT_FREQUENCY_TYPE, INTEREST_RATE_PER_PERIOD,
MIN_INTEREST_RATE_PER_PERIOD, MAX_INTEREST_RATE_PER_PERIOD, INTEREST_RATE_FREQUENCY_TYPE, AMORTIZATION_TYPE, INTEREST_TYPE,
INTEREST_CALCULATION_PERIOD_TYPE, LoanProductConstants.ALLOW_PARTIAL_PERIOD_INTEREST_CALCUALTION_PARAM_NAME,
IN_ARREARS_TOLERANCE, TRANSACTION_PROCESSING_STRATEGY_CODE, ADVANCED_PAYMENT_ALLOCATIONS, GRACE_ON_PRINCIPAL_PAYMENT,
"recurringMoratoriumOnPrincipalPeriods", GRACE_ON_INTEREST_PAYMENT, GRACE_ON_INTEREST_CHARGED, "charges", ACCOUNTING_RULE,
INCLUDE_IN_BORROWER_CYCLE, "startDate", "closeDate", "externalId", IS_LINKED_TO_FLOATING_INTEREST_RATES, FLOATING_RATES_ID,
INTEREST_RATE_DIFFERENTIAL, MIN_DIFFERENTIAL_LENDING_RATE, DEFAULT_DIFFERENTIAL_LENDING_RATE, MAX_DIFFERENTIAL_LENDING_RATE,
IS_FLOATING_INTEREST_RATE_CALCULATION_ALLOWED, "syncExpectedWithDisbursementDate",
LoanProductAccountingParams.FEES_RECEIVABLE.getValue(), LoanProductAccountingParams.FUND_SOURCE.getValue(),
LoanProductAccountingParams.INCOME_FROM_FEES.getValue(), LoanProductAccountingParams.INCOME_FROM_PENALTIES.getValue(),
LoanProductAccountingParams.INTEREST_ON_LOANS.getValue(), LoanProductAccountingParams.INTEREST_RECEIVABLE.getValue(),
LoanProductAccountingParams.LOAN_PORTFOLIO.getValue(), LoanProductAccountingParams.OVERPAYMENT.getValue(),
LoanProductAccountingParams.TRANSFERS_SUSPENSE.getValue(), LoanProductAccountingParams.LOSSES_WRITTEN_OFF.getValue(),
LoanProductAccountingParams.GOODWILL_CREDIT.getValue(), LoanProductAccountingParams.PENALTIES_RECEIVABLE.getValue(),
IN_ARREARS_TOLERANCE, TRANSACTION_PROCESSING_STRATEGY_CODE, ADVANCED_PAYMENT_ALLOCATIONS, CREDIT_ALLOCATIONS,
GRACE_ON_PRINCIPAL_PAYMENT, "recurringMoratoriumOnPrincipalPeriods", GRACE_ON_INTEREST_PAYMENT, GRACE_ON_INTEREST_CHARGED,
"charges", ACCOUNTING_RULE, INCLUDE_IN_BORROWER_CYCLE, "startDate", "closeDate", "externalId",
IS_LINKED_TO_FLOATING_INTEREST_RATES, FLOATING_RATES_ID, INTEREST_RATE_DIFFERENTIAL, MIN_DIFFERENTIAL_LENDING_RATE,
DEFAULT_DIFFERENTIAL_LENDING_RATE, MAX_DIFFERENTIAL_LENDING_RATE, IS_FLOATING_INTEREST_RATE_CALCULATION_ALLOWED,
"syncExpectedWithDisbursementDate", LoanProductAccountingParams.FEES_RECEIVABLE.getValue(),
LoanProductAccountingParams.FUND_SOURCE.getValue(), LoanProductAccountingParams.INCOME_FROM_FEES.getValue(),
LoanProductAccountingParams.INCOME_FROM_PENALTIES.getValue(), LoanProductAccountingParams.INTEREST_ON_LOANS.getValue(),
LoanProductAccountingParams.INTEREST_RECEIVABLE.getValue(), LoanProductAccountingParams.LOAN_PORTFOLIO.getValue(),
LoanProductAccountingParams.OVERPAYMENT.getValue(), LoanProductAccountingParams.TRANSFERS_SUSPENSE.getValue(),
LoanProductAccountingParams.LOSSES_WRITTEN_OFF.getValue(), LoanProductAccountingParams.GOODWILL_CREDIT.getValue(),
LoanProductAccountingParams.PENALTIES_RECEIVABLE.getValue(),
LoanProductAccountingParams.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue(),
LoanProductAccountingParams.FEE_INCOME_ACCOUNT_MAPPING.getValue(), LoanProductAccountingParams.INCOME_FROM_RECOVERY.getValue(),
LoanProductAccountingParams.PENALTY_INCOME_ACCOUNT_MAPPING.getValue(),
@@ -0,0 +1,92 @@
/**
* 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.service;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductCreditAllocationRule;
public class LoanProductCreditAllocationRuleMerger {
public boolean updateCreditAllocationRules(LoanProduct loanProduct,
final List<LoanProductCreditAllocationRule> newLoanProductCreditAllocationRules) {
if (newLoanProductCreditAllocationRules == null) {
return false;
}
boolean updated = false;
Map<CreditAllocationTransactionType, LoanProductCreditAllocationRule> originalItems = loanProduct.getCreditAllocationRules()
.stream().collect(Collectors.toMap(LoanProductCreditAllocationRule::getTransactionType, Function.identity()));
Map<CreditAllocationTransactionType, LoanProductCreditAllocationRule> newItems = newLoanProductCreditAllocationRules.stream()
.collect(Collectors.toMap(LoanProductCreditAllocationRule::getTransactionType, Function.identity()));
// elements to be deleted
Set<CreditAllocationTransactionType> existing = new HashSet<>(originalItems.keySet());
Set<CreditAllocationTransactionType> newSet = new HashSet<>(newItems.keySet());
existing.removeAll(newSet);
if (existing.size() > 0) {
updated = true;
existing.forEach(type -> {
loanProduct.getCreditAllocationRules().remove(originalItems.get(type));
});
}
// elements to be added
existing = new HashSet<>(originalItems.keySet());
newSet = new HashSet<>(newItems.keySet());
newSet.removeAll(existing);
if (newSet.size() > 0) {
updated = true;
newSet.forEach(type -> {
loanProduct.getCreditAllocationRules().add(newItems.get(type));
});
}
// elements to be merged
existing = new HashSet<>(originalItems.keySet());
newSet = new HashSet<>(newItems.keySet());
existing.retainAll(newSet);
for (CreditAllocationTransactionType type : existing) {
boolean result = mergeLoanProductCreditAllocationRule(originalItems.get(type), newItems.get(type));
if (result) {
updated = true;
}
}
return updated;
}
private boolean mergeLoanProductCreditAllocationRule(LoanProductCreditAllocationRule into, LoanProductCreditAllocationRule newElement) {
boolean changed = false;
if (!Objects.equals(into.getAllocationTypes(), newElement.getAllocationTypes())) {
into.setAllocationTypes(newElement.getAllocationTypes());
changed = true;
}
return changed;
}
}
@@ -21,6 +21,7 @@ package org.apache.fineract.portfolio.loanproduct.service;
import java.util.Collection;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.portfolio.loanproduct.data.AdvancedPaymentData;
import org.apache.fineract.portfolio.loanproduct.data.CreditAllocationData;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductBorrowerCycleVariationData;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductData;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
@@ -53,5 +54,7 @@ public interface LoanProductReadPlatformService {
Collection<AdvancedPaymentData> retrieveAdvancedPaymentData(Long loanProductId);
Collection<CreditAllocationData> retrieveCreditAllocationData(Long loanProductId);
LoanProductData retrieveLoanProductFloatingDetails(Long loanProductId);
}
@@ -47,6 +47,7 @@ import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanSchedul
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanScheduleType;
import org.apache.fineract.portfolio.loanproduct.data.AdvancedPaymentData;
import org.apache.fineract.portfolio.loanproduct.data.AdvancedPaymentData.PaymentAllocationOrder;
import org.apache.fineract.portfolio.loanproduct.data.CreditAllocationData;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductBorrowerCycleVariationData;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductData;
import org.apache.fineract.portfolio.loanproduct.data.LoanProductGuaranteeData;
@@ -84,10 +85,11 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
final Collection<LoanProductBorrowerCycleVariationData> borrowerCycleVariationDatas = retrieveLoanProductBorrowerCycleVariations(
loanProductId);
final Collection<AdvancedPaymentData> advancedPaymentData = retrieveAdvancedPaymentData(loanProductId);
final Collection<CreditAllocationData> creditAllocationData = retrieveCreditAllocationData(loanProductId);
final Collection<DelinquencyBucketData> delinquencyBucketOptions = this.delinquencyReadPlatformService
.retrieveAllDelinquencyBuckets();
final LoanProductMapper rm = new LoanProductMapper(charges, borrowerCycleVariationDatas, rates, delinquencyBucketOptions,
advancedPaymentData);
advancedPaymentData, creditAllocationData);
final String sql = "select " + rm.loanProductSchema() + " where lp.id = ?";
return this.jdbcTemplate.queryForObject(sql, rm, loanProductId); // NOSONAR
@@ -116,12 +118,19 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
return this.jdbcTemplate.query(sql, apdm, loanProductId); // NOSONAR
}
@Override
public List<CreditAllocationData> retrieveCreditAllocationData(final Long loanProductId) {
final CreditAllocationDataMapper cadm = new CreditAllocationDataMapper();
final String sql = "select " + cadm.schema() + " where loan_product_id = ?";
return this.jdbcTemplate.query(sql, cadm, loanProductId); // NOSONAR
}
@Override
public Collection<LoanProductData> retrieveAllLoanProducts() {
this.context.authenticatedUser();
final LoanProductMapper rm = new LoanProductMapper(null, null, null, null, null);
final LoanProductMapper rm = new LoanProductMapper(null, null, null, null, null, null);
String sql = "select " + rm.loanProductSchema();
@@ -200,18 +209,22 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
private final Collection<AdvancedPaymentData> advancedPaymentData;
private final Collection<CreditAllocationData> creditAllocationData;
private final Collection<RateData> rates;
private final Collection<DelinquencyBucketData> delinquencyBucketOptions;
LoanProductMapper(final Collection<ChargeData> charges,
final Collection<LoanProductBorrowerCycleVariationData> borrowerCycleVariationDatas, final Collection<RateData> rates,
final Collection<DelinquencyBucketData> delinquencyBucketOptions, Collection<AdvancedPaymentData> advancedPaymentData) {
final Collection<DelinquencyBucketData> delinquencyBucketOptions, Collection<AdvancedPaymentData> advancedPaymentData,
Collection<CreditAllocationData> creditAllocationData) {
this.charges = charges;
this.borrowerCycleVariationDatas = borrowerCycleVariationDatas;
this.rates = rates;
this.delinquencyBucketOptions = delinquencyBucketOptions;
this.advancedPaymentData = advancedPaymentData;
this.creditAllocationData = creditAllocationData;
}
public String loanProductSchema() {
@@ -530,8 +543,8 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
maximumGap, syncExpectedWithDisbursementDate, canUseForTopup, isEqualAmortization, rateOptions, this.rates,
isRatesEnabled, fixedPrincipalPercentagePerInstallment, delinquencyBucketOptions, delinquencyBucket,
dueDaysForRepaymentEvent, overDueDaysForRepaymentEvent, enableDownPayment, disbursedAmountPercentageForDownPayment,
enableAutoRepaymentForDownPayment, advancedPaymentData, repaymentStartDateType, enableInstallmentLevelDelinquency,
loanScheduleType.asEnumOptionData(), loanScheduleProcessingType.asEnumOptionData());
enableAutoRepaymentForDownPayment, advancedPaymentData, creditAllocationData, repaymentStartDateType,
enableInstallmentLevelDelinquency, loanScheduleType.asEnumOptionData(), loanScheduleProcessingType.asEnumOptionData());
}
}
@@ -589,8 +602,8 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
return new AdvancedPaymentData(transactionType, futureInstallmentAllocationRule, convert(allocationTypes));
}
private List<PaymentAllocationOrder> convert(String futureInstallmentAllocationRule) {
String[] allocationRule = futureInstallmentAllocationRule.split(",");
private List<PaymentAllocationOrder> convert(String allocationOrders) {
String[] allocationRule = allocationOrders.split(",");
AtomicInteger order = new AtomicInteger(1);
return Arrays.stream(allocationRule) //
.map(s -> new PaymentAllocationOrder(s, order.getAndIncrement())) //
@@ -599,6 +612,29 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
}
private static final class CreditAllocationDataMapper implements RowMapper<CreditAllocationData> {
public String schema() {
return "transaction_type, allocation_types from m_loan_product_credit_allocation_rule";
}
@Override
public CreditAllocationData mapRow(ResultSet rs, int rowNum) throws SQLException {
final String transactionType = rs.getString("transaction_type");
final String allocationTypes = rs.getString("allocation_types");
return new CreditAllocationData(transactionType, convert(allocationTypes));
}
private List<CreditAllocationData.CreditAllocationOrder> convert(String allocationOrders) {
String[] allocationRule = allocationOrders.split(",");
AtomicInteger order = new AtomicInteger(1);
return Arrays.stream(allocationRule) //
.map(s -> new CreditAllocationData.CreditAllocationOrder(s, order.getAndIncrement())) //
.toList();
}
}
private static final class LoanProductBorrowerCycleMapper implements RowMapper<LoanProductBorrowerCycleVariationData> {
public String schema() {
@@ -630,7 +666,7 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
public Collection<LoanProductData> retrieveAllLoanProductsForCurrency(String currencyCode) {
this.context.authenticatedUser();
final LoanProductMapper rm = new LoanProductMapper(null, null, null, null, null);
final LoanProductMapper rm = new LoanProductMapper(null, null, null, null, null, null);
String sql = "select " + rm.loanProductSchema() + " where lp.currency_code= ? ";
@@ -55,6 +55,7 @@ import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.AprCalculator;
import org.apache.fineract.portfolio.loanproduct.LoanProductConstants;
import org.apache.fineract.portfolio.loanproduct.domain.AdvancedPaymentAllocationsJsonParser;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationsJsonParser;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductCreditAllocationRule;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductPaymentAllocationRule;
@@ -88,7 +89,9 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro
private final DelinquencyBucketRepository delinquencyBucketRepository;
private final LoanRepaymentScheduleTransactionProcessorFactory loanRepaymentScheduleTransactionProcessorFactory;
private final AdvancedPaymentAllocationsJsonParser advancedPaymentJsonParser;
private final CreditAllocationsJsonParser creditAllocationsJsonParser;
private final LoanProductPaymentAllocationRuleMerger loanProductPaymentAllocationRuleMerger = new LoanProductPaymentAllocationRuleMerger();
private final LoanProductCreditAllocationRuleMerger loanProductCreditAllocationRuleMerger = new LoanProductCreditAllocationRuleMerger();
@Transactional
@Override
@@ -110,11 +113,8 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro
final List<Rate> rates = assembleListOfProductRates(command);
final List<LoanProductPaymentAllocationRule> loanProductPaymentAllocationRules = advancedPaymentJsonParser
.assembleLoanProductPaymentAllocationRules(command, loanTransactionProcessingStrategyCode);
List<LoanProductCreditAllocationRule> loanProductCreditAllocationRules = new ArrayList<>(); // TODO: this
// shall be
// parsed from
// the json
// command
final List<LoanProductCreditAllocationRule> loanProductCreditAllocationRules = creditAllocationsJsonParser
.assembleLoanProductCreditAllocationRules(command, loanTransactionProcessingStrategyCode);
FloatingRate floatingRate = null;
if (command.parameterExists("floatingRatesId")) {
floatingRate = this.floatingRateRepository
@@ -239,6 +239,17 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro
}
}
if (changes.containsKey("creditAllocation")) {
final List<LoanProductCreditAllocationRule> loanProductCreditAllocationRules = creditAllocationsJsonParser
.assembleLoanProductCreditAllocationRules(command, product.getTransactionProcessingStrategyCode());
loanProductCreditAllocationRules.forEach(lpcar -> lpcar.setLoanProduct(product));
final boolean updated = loanProductCreditAllocationRuleMerger.updateCreditAllocationRules(product,
loanProductCreditAllocationRules);
if (!updated) {
changes.remove("creditAllocation");
}
}
// accounting related changes
final boolean accountingTypeChanged = changes.containsKey("accountingRule");
final Map<String, Object> accountingMappingChanges = this.accountMappingWritePlatformService
@@ -33,6 +33,7 @@ import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleTra
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.AprCalculator;
import org.apache.fineract.portfolio.loanproduct.domain.AdvancedPaymentAllocationsJsonParser;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationsJsonParser;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
import org.apache.fineract.portfolio.loanproduct.serialization.LoanProductDataValidator;
import org.apache.fineract.portfolio.loanproduct.service.LoanDropdownReadPlatformService;
@@ -78,10 +79,10 @@ public class LoanProductConfiguration {
LoanRepositoryWrapper loanRepositoryWrapper, BusinessEventNotifierService businessEventNotifierService,
DelinquencyBucketRepository delinquencyBucketRepository,
LoanRepaymentScheduleTransactionProcessorFactory loanRepaymentScheduleTransactionProcessorFactory,
AdvancedPaymentAllocationsJsonParser advancedPaymentJsonParser) {
AdvancedPaymentAllocationsJsonParser advancedPaymentJsonParser, CreditAllocationsJsonParser creditAllocationsJsonParser) {
return new LoanProductWritePlatformServiceJpaRepositoryImpl(context, fromApiJsonDeserializer, loanProductRepository, aprCalculator,
fundRepository, chargeRepository, rateRepository, accountMappingWritePlatformService, fineractEntityAccessUtil,
floatingRateRepository, loanRepositoryWrapper, businessEventNotifierService, delinquencyBucketRepository,
loanRepaymentScheduleTransactionProcessorFactory, advancedPaymentJsonParser);
loanRepaymentScheduleTransactionProcessorFactory, advancedPaymentJsonParser, creditAllocationsJsonParser);
}
}
@@ -0,0 +1,106 @@
/**
* 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.service;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.FEE;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.INTEREST;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.PENALTY;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.PRINCIPAL;
import java.util.List;
import org.apache.fineract.portfolio.loanproduct.domain.AllocationType;
import org.apache.fineract.portfolio.loanproduct.domain.CreditAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductCreditAllocationRule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class LoanProductCreditAllocationRuleMergerTest {
private LoanProductCreditAllocationRuleMerger underTest = new LoanProductCreditAllocationRuleMerger();
@Test
public void testMergerOneNewAdded() {
LoanProduct loanProduct = new LoanProduct();
LoanProductCreditAllocationRule rule1 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(PENALTY, FEE, INTEREST, PRINCIPAL));
boolean result = underTest.updateCreditAllocationRules(loanProduct, List.of(rule1));
Assertions.assertTrue(result);
Assertions.assertEquals(1, loanProduct.getCreditAllocationRules().size());
Assertions.assertEquals(rule1, loanProduct.getCreditAllocationRules().get(0));
}
@Test
public void testMergerExistingUpdated() {
LoanProduct loanProduct = new LoanProduct();
LoanProductCreditAllocationRule rule1 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(PENALTY, FEE, INTEREST, PRINCIPAL));
LoanProductCreditAllocationRule rule2 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(FEE, INTEREST, PRINCIPAL, PENALTY));
loanProduct.getCreditAllocationRules().add(rule1);
boolean result = underTest.updateCreditAllocationRules(loanProduct, List.of(rule2));
Assertions.assertTrue(result);
Assertions.assertEquals(1, loanProduct.getCreditAllocationRules().size());
Assertions.assertEquals(rule2.getTransactionType(), loanProduct.getCreditAllocationRules().get(0).getTransactionType());
Assertions.assertEquals(rule2.getAllocationTypes(), loanProduct.getCreditAllocationRules().get(0).getAllocationTypes());
}
@Test
public void testMergerNothingIsChanged() {
LoanProduct loanProduct = new LoanProduct();
LoanProductCreditAllocationRule rule1 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(PENALTY, FEE, INTEREST, PRINCIPAL));
LoanProductCreditAllocationRule rule2 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(PENALTY, FEE, INTEREST, PRINCIPAL));
loanProduct.getCreditAllocationRules().add(rule1);
boolean result = underTest.updateCreditAllocationRules(loanProduct, List.of(rule2));
Assertions.assertFalse(result);
Assertions.assertEquals(1, loanProduct.getCreditAllocationRules().size());
Assertions.assertEquals(rule1.getTransactionType(), loanProduct.getCreditAllocationRules().get(0).getTransactionType());
Assertions.assertEquals(rule1.getAllocationTypes(), loanProduct.getCreditAllocationRules().get(0).getAllocationTypes());
}
@Test
public void testMergerExistingDeleted() {
LoanProduct loanProduct = new LoanProduct();
LoanProductCreditAllocationRule rule1 = createRule(CreditAllocationTransactionType.CHARGEBACK,
List.of(PENALTY, FEE, INTEREST, PRINCIPAL));
loanProduct.getCreditAllocationRules().add(rule1);
boolean result = underTest.updateCreditAllocationRules(loanProduct, List.of());
Assertions.assertTrue(result);
Assertions.assertEquals(0, loanProduct.getCreditAllocationRules().size());
}
public LoanProductCreditAllocationRule createRule(CreditAllocationTransactionType transactionType,
List<AllocationType> allocationTypeList) {
return new LoanProductCreditAllocationRule(null, transactionType, allocationTypeList);
}
}