janez89 commented on code in PR #4099: URL: https://github.com/apache/fineract/pull/4099#discussion_r1801377953
########## fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/data/InterestPeriod.java: ########## @@ -0,0 +1,110 @@ +/** + * 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.loanschedule.data; + +import jakarta.validation.constraints.NotNull; +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.Optional; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.fineract.infrastructure.core.service.MathUtil; +import org.apache.fineract.organisation.monetary.domain.Money; + +@Getter +@EqualsAndHashCode(exclude = { "repaymentPeriod" }) +public class InterestPeriod implements Comparable<InterestPeriod> { + + private final RepaymentPeriod repaymentPeriod; + private final LocalDate fromDate; + @Setter + private LocalDate dueDate; + @Setter + private BigDecimal rateFactor; + private Money disbursementAmount; + private Money balanceCorrectionAmount; + private Money outstandingLoanBalance; + + public InterestPeriod(RepaymentPeriod repaymentPeriod, LocalDate fromDate, LocalDate dueDate, BigDecimal rateFactor, + Money disbursementAmount, Money balanceCorrectionAmount, Money outstandingLoanBalance) { + this.repaymentPeriod = repaymentPeriod; + this.fromDate = fromDate; + this.dueDate = dueDate; + this.rateFactor = rateFactor; + this.disbursementAmount = disbursementAmount; + this.balanceCorrectionAmount = balanceCorrectionAmount; + this.outstandingLoanBalance = outstandingLoanBalance; + } + + public InterestPeriod(RepaymentPeriod repaymentPeriod, InterestPeriod interestPeriod) { + this(repaymentPeriod, interestPeriod.getFromDate(), interestPeriod.getDueDate(), interestPeriod.getRateFactor(), + interestPeriod.getDisbursementAmount(), interestPeriod.getBalanceCorrectionAmount(), + interestPeriod.getOutstandingLoanBalance()); + } + + @Override + public int compareTo(@NotNull InterestPeriod o) { + return dueDate.compareTo(o.dueDate); + } + + public void addBalanceCorrectionAmount(final Money balanceCorrectionAmount) { + this.balanceCorrectionAmount = MathUtil.plus(this.balanceCorrectionAmount, balanceCorrectionAmount); + } + + public void addDisbursementAmount(final Money disbursementAmount) { + this.disbursementAmount = MathUtil.plus(this.disbursementAmount, disbursementAmount); + } + + public Money getCalculatedDueInterest() { + return getOutstandingLoanBalance().multipliedBy(getRateFactor()); + } + + public void updateOutstandingLoanBalance() { + if (isFirstInterestPeriod()) { + Optional<RepaymentPeriod> previousRepaymentPeriod = getRepaymentPeriod().getPrevious(); + if (previousRepaymentPeriod.isPresent()) { + InterestPeriod previousInterestPeriod = previousRepaymentPeriod.get().getInterestPeriods() + .get(previousRepaymentPeriod.get().getInterestPeriods().size() - 1); + this.outstandingLoanBalance = previousInterestPeriod.getOutstandingLoanBalance()// + .plus(previousInterestPeriod.getDisbursementAmount())// + .plus(previousInterestPeriod.getBalanceCorrectionAmount())// + .minus(previousRepaymentPeriod.get().getDuePrincipal())// + .plus(previousRepaymentPeriod.get().getPaidPrincipal());// + } + } else { + int index = getRepaymentPeriod().getInterestPeriods().indexOf(this); + InterestPeriod previousInterestPeriod = getRepaymentPeriod().getInterestPeriods().get(index - 1); + this.outstandingLoanBalance = previousInterestPeriod.getOutstandingLoanBalance() // + .plus(previousInterestPeriod.getBalanceCorrectionAmount()) // + .plus(previousInterestPeriod.getDisbursementAmount()); // + } + } + + private boolean isFirstInterestPeriod() { + return getRepaymentPeriod().getInterestPeriods().get(0).equals(this); Review Comment: Ordering used by old EMI logic, I remove now and not necessary anymore. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
