FINERACT-1734-LoanRepaymentDue-Event-Enhancements

This commit is contained in:
Ruchi Dhamankar
2022-12-22 12:56:16 +01:00
committed by Arnold Gálovics
parent f49a0b8e95
commit 0f41eb2c42
6 changed files with 312 additions and 15 deletions
@@ -0,0 +1,30 @@
/**
* 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.infrastructure.event.external.service.serialization.mapper.loan;
import org.apache.fineract.avro.loan.v1.RepaymentPastDueDataV1;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.support.AvroMapperConfig;
import org.apache.fineract.portfolio.loanaccount.data.LoanRepaymentPastDueData;
import org.mapstruct.Mapper;
@Mapper(config = AvroMapperConfig.class)
public interface LoanRepaymentPastDueDataMapper {
RepaymentPastDueDataV1 map(LoanRepaymentPastDueData source);
}
@@ -28,11 +28,14 @@ import org.apache.fineract.avro.loan.v1.RepaymentDueDataV1;
import org.apache.fineract.avro.loan.v1.RepaymentPastDueDataV1;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.loan.repayment.LoanRepaymentBusinessEvent;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.loan.LoanRepaymentPastDueDataMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.support.AvroDateTimeMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.AbstractBusinessEventSerializer;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.apache.fineract.portfolio.loanaccount.data.LoanRepaymentPastDueData;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.service.LoanCalculateRepaymentPastDueService;
import org.springframework.stereotype.Component;
@Component
@@ -40,6 +43,8 @@ import org.springframework.stereotype.Component;
public class LoanRepaymentBusinessEventSerializer extends AbstractBusinessEventSerializer {
private final AvroDateTimeMapper dataTimeMapper;
private final LoanRepaymentPastDueDataMapper pastDueDataMapper;
private final LoanCalculateRepaymentPastDueService pastDueService;
@Override
protected <T> ByteBufferSerializable toAvroDTO(BusinessEvent<T> rawEvent) {
@@ -55,6 +60,18 @@ public class LoanRepaymentBusinessEventSerializer extends AbstractBusinessEventS
CurrencyDataV1 currency = CurrencyDataV1.newBuilder().setCode(loanCurrency.getCode())
.setDecimalPlaces(loanCurrency.getDigitsAfterDecimal()).setInMultiplesOf(loanCurrency.getCurrencyInMultiplesOf()).build();
RepaymentDueDataV1 repaymentDue = getRepaymentDueData(repaymentInstallment, loanCurrency);
LoanRepaymentPastDueData pastDueData = pastDueService.retrieveLoanRepaymentPastDueAmountTillDate(loan);
RepaymentPastDueDataV1 pastDue = pastDueDataMapper.map(pastDueData);
LoanRepaymentDueDataV1 loanRepaymentDueDataV1 = LoanRepaymentDueDataV1.newBuilder().setLoanId(id).setLoanAccountNo(accountNo)
.setLoanExternalId(externalId).setCurrency(currency).setInstallment(repaymentDue).setPastDueAmount(pastDue).build();
return loanRepaymentDueDataV1;
}
private RepaymentDueDataV1 getRepaymentDueData(LoanRepaymentScheduleInstallment repaymentInstallment, MonetaryCurrency loanCurrency) {
Integer installmentNumber = repaymentInstallment.getInstallmentNumber();
String dueDate = dataTimeMapper.mapLocalDate(repaymentInstallment.getDueDate());
BigDecimal principalAmountDue = repaymentInstallment.getPrincipalOutstanding(loanCurrency).getAmount();
@@ -65,13 +82,7 @@ public class LoanRepaymentBusinessEventSerializer extends AbstractBusinessEventS
RepaymentDueDataV1 repaymentDue = new RepaymentDueDataV1(installmentNumber, dueDate, principalAmountDue, interestAmountDue,
feeChargeAmountDue, penaltyChargeAmountDue, totalAmountDue);
RepaymentPastDueDataV1 pastDue = new RepaymentPastDueDataV1(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO,
BigDecimal.ZERO);
LoanRepaymentDueDataV1 loanRepaymentDueDataV1 = LoanRepaymentDueDataV1.newBuilder().setLoanId(id).setLoanAccountNo(accountNo)
.setLoanExternalId(externalId).setCurrency(currency).setInstallment(repaymentDue).setPastDueAmount(pastDue).build();
return loanRepaymentDueDataV1;
return repaymentDue;
}
@Override
@@ -0,0 +1,35 @@
/**
* 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.loanaccount.data;
import java.math.BigDecimal;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class LoanRepaymentPastDueData {
private final BigDecimal totalAmount;
private final BigDecimal principalAmount;
private final BigDecimal interestAmount;
private final BigDecimal feeAmount;
private final BigDecimal penaltyAmount;
}
@@ -0,0 +1,72 @@
/**
* 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.loanaccount.service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.apache.fineract.portfolio.loanaccount.data.LoanRepaymentPastDueData;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.springframework.stereotype.Component;
@Component
public class LoanCalculateRepaymentPastDueService {
public LoanRepaymentPastDueData retrieveLoanRepaymentPastDueAmountTillDate(Loan loan) {
List<LoanRepaymentScheduleInstallment> pastDueRepayments = getPastDueRepayments(loan);
MonetaryCurrency loanCurrency = loan.getCurrency();
LoanRepaymentPastDueData pastDueData = calculatePastDueAmountsForRepayments(pastDueRepayments, loanCurrency);
return pastDueData;
}
private LoanRepaymentPastDueData calculatePastDueAmountsForRepayments(List<LoanRepaymentScheduleInstallment> pastDueRepayments,
MonetaryCurrency currency) {
BigDecimal totalAmount = BigDecimal.ZERO;
BigDecimal principalAmount = BigDecimal.ZERO;
BigDecimal interestAmount = BigDecimal.ZERO;
BigDecimal feeAmount = BigDecimal.ZERO;
BigDecimal penaltyAmount = BigDecimal.ZERO;
if (!pastDueRepayments.isEmpty()) {
totalAmount = pastDueRepayments.stream().map(repayment -> repayment.getTotalOutstanding(currency).getAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
principalAmount = pastDueRepayments.stream().map(repayment -> repayment.getPrincipalOutstanding(currency).getAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
interestAmount = pastDueRepayments.stream().map(repayment -> repayment.getInterestOutstanding(currency).getAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
feeAmount = pastDueRepayments.stream().map(repayment -> repayment.getFeeChargesOutstanding(currency).getAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
penaltyAmount = pastDueRepayments.stream().map(repayment -> repayment.getPenaltyChargesOutstanding(currency).getAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
return new LoanRepaymentPastDueData(totalAmount, principalAmount, interestAmount, feeAmount, penaltyAmount);
}
private List<LoanRepaymentScheduleInstallment> getPastDueRepayments(Loan loan) {
List<LoanRepaymentScheduleInstallment> loanRepayments = loan.getRepaymentScheduleInstallments();
LocalDate currentBusinessDate = DateUtils.getBusinessLocalDate();
return loanRepayments.stream()
.filter((repayment) -> (!repayment.isObligationsMet()
&& (repayment.getDueDate().isBefore(currentBusinessDate) || repayment.getDueDate().isEqual(currentBusinessDate))))
.collect(Collectors.toList());
}
}