alberto-art3ch commented on code in PR #3902:
URL: https://github.com/apache/fineract/pull/3902#discussion_r1616109653


##########
fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/ratefactor/RateFactorFunctions.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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.ratefactor;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.common.domain.DaysInYearType;
+import 
org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
+
+public class RateFactorFunctions {
+
+    public static final Integer PRECISION = 8;
+
+    protected RateFactorFunctions() {}
+
+    /**
+     * To calculate the monthly payment, we first need to calculate something 
called the Rate Factor. We're going to be
+     * using simple interest. The Rate Factor for simple interest is 
calculated by the following formula:
+     *
+     *
+     * R = 1 + (r * d / y)
+     *
+     * @param interestRate
+     *            (r)
+     * @param daysInPeriod
+     *            (d)
+     * @param daysInYear
+     *            (y)
+     */
+    public static BigDecimal rateFactor(final BigDecimal interestRate, final 
Long daysInPeriod, final Integer daysInYear,
+            final Integer precision) {
+        final BigDecimal daysPeriod = BigDecimal.valueOf(daysInPeriod);
+        BigDecimal daysYear = BigDecimal.valueOf(daysInYear);
+        final MathContext mc = new MathContext(precision, 
MoneyHelper.getRoundingMode());
+
+        return 
BigDecimal.ONE.add(interestRate.multiply(daysPeriod.divide(daysYear, mc), mc), 
mc);
+    }
+
+    /**
+     * To calculate the function value for each period, we are going to use 
the next formula:
+     *
+     * fn = 1 + fnValueFrom * rateFactorEnd
+     *
+     * @param periodFrom
+     *
+     * @param periodEnd
+     *
+     * @param mathContext
+     *
+     */
+    public static RateFactorPeriodData fnValue(RateFactorPeriodData 
periodFrom, final RateFactorPeriodData periodEnd,
+            final MathContext mc) {
+        BigDecimal fnValue = BigDecimal.ONE;
+        if (periodEnd.getPeriod() > 1) {
+            fnValue = 
BigDecimal.ONE.add(periodFrom.getFnValue().multiply(periodEnd.getRateFactor(), 
mc));
+        }
+        periodEnd.setFnValue(fnValue);
+        return periodEnd;
+    }
+
+    /**
+     *
+     *
+     * emi = (R^n * principal) / fnValue
+     *
+     * @param periods
+     *
+     * @param principal
+     *
+     * @param mathContext
+     *
+     */
+    public static BigDecimal calculateEMI(final List<RateFactorPeriodData> 
periods, final BigDecimal principal, final MathContext mc,
+            final Integer precision) {
+        final BigDecimal lastFnValue = periods.get(periods.size() - 
1).getFnValue();
+        BigDecimal rnValue = BigDecimal.ONE;
+        for (RateFactorPeriodData rateFactorPeriod : periods) {
+            if (rateFactorPeriod.getPeriod() == 1) {

Review Comment:
   Done! code removed from this PR



##########
fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/ratefactor/RateFactorFunctions.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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.ratefactor;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.common.domain.DaysInYearType;
+import 
org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
+
+public class RateFactorFunctions {
+
+    public static final Integer PRECISION = 8;
+
+    protected RateFactorFunctions() {}
+
+    /**
+     * To calculate the monthly payment, we first need to calculate something 
called the Rate Factor. We're going to be
+     * using simple interest. The Rate Factor for simple interest is 
calculated by the following formula:
+     *
+     *
+     * R = 1 + (r * d / y)
+     *
+     * @param interestRate
+     *            (r)
+     * @param daysInPeriod
+     *            (d)
+     * @param daysInYear
+     *            (y)
+     */
+    public static BigDecimal rateFactor(final BigDecimal interestRate, final 
Long daysInPeriod, final Integer daysInYear,
+            final Integer precision) {
+        final BigDecimal daysPeriod = BigDecimal.valueOf(daysInPeriod);
+        BigDecimal daysYear = BigDecimal.valueOf(daysInYear);
+        final MathContext mc = new MathContext(precision, 
MoneyHelper.getRoundingMode());
+
+        return 
BigDecimal.ONE.add(interestRate.multiply(daysPeriod.divide(daysYear, mc), mc), 
mc);
+    }
+
+    /**
+     * To calculate the function value for each period, we are going to use 
the next formula:
+     *
+     * fn = 1 + fnValueFrom * rateFactorEnd
+     *
+     * @param periodFrom
+     *
+     * @param periodEnd
+     *
+     * @param mathContext
+     *
+     */
+    public static RateFactorPeriodData fnValue(RateFactorPeriodData 
periodFrom, final RateFactorPeriodData periodEnd,
+            final MathContext mc) {
+        BigDecimal fnValue = BigDecimal.ONE;
+        if (periodEnd.getPeriod() > 1) {
+            fnValue = 
BigDecimal.ONE.add(periodFrom.getFnValue().multiply(periodEnd.getRateFactor(), 
mc));
+        }
+        periodEnd.setFnValue(fnValue);
+        return periodEnd;
+    }
+
+    /**
+     *
+     *
+     * emi = (R^n * principal) / fnValue
+     *
+     * @param periods
+     *
+     * @param principal
+     *
+     * @param mathContext
+     *
+     */
+    public static BigDecimal calculateEMI(final List<RateFactorPeriodData> 
periods, final BigDecimal principal, final MathContext mc,
+            final Integer precision) {
+        final BigDecimal lastFnValue = periods.get(periods.size() - 
1).getFnValue();
+        BigDecimal rnValue = BigDecimal.ONE;
+        for (RateFactorPeriodData rateFactorPeriod : periods) {
+            if (rateFactorPeriod.getPeriod() == 1) {
+                rnValue = rateFactorPeriod.getRateFactor();
+            } else {
+                rnValue = rnValue.multiply(rateFactorPeriod.getRateFactor(), 
mc);
+            }
+        }
+        BigDecimal emiAmount = rnValue.multiply(principal.divide(lastFnValue, 
mc), mc);
+        return emiAmount.setScale(precision, MoneyHelper.getRoundingMode());
+    }
+
+    public static BigDecimal calculateEMI(final 
List<LoanRepaymentScheduleInstallment> installments, final BigDecimal principal,
+            final BigDecimal interestRate, final DaysInYearType 
daysInYearType, final MathContext mc) {
+        final List<RateFactorPeriodData> rateFactorDatas = new ArrayList<>();
+        RateFactorPeriodData rateFactorPeriodDataFrom = null;
+        // Calculate the Rate Factor and fnValue for each Installment
+        for (LoanRepaymentScheduleInstallment installment : installments) {
+            final Long daysInPeriod = 
DateUtils.getDifferenceInDays(installment.getFromDate(), 
installment.getDueDate());
+            final Integer daysInYear = 
RateFactorFunctions.daysInYear(daysInYearType, installment.getFromDate());
+            BigDecimal rateFactor = 
RateFactorFunctions.rateFactor(interestRate, daysInPeriod, daysInYear, 
PRECISION);
+
+            RateFactorPeriodData rateFactorPeriodDataEnd = new 
RateFactorPeriodData(installment.getInstallmentNumber(),
+                    installment.getFromDate(), installment.getDueDate(), 
rateFactor);
+            RateFactorFunctions.fnValue(rateFactorPeriodDataFrom, 
rateFactorPeriodDataEnd, mc);
+
+            rateFactorDatas.add(rateFactorPeriodDataEnd);
+            rateFactorPeriodDataFrom = rateFactorPeriodDataEnd;
+        }
+
+        return calculateEMI(rateFactorDatas, principal, mc, 2);

Review Comment:
   Done! code removed from this PR



-- 
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]

Reply via email to