Search in sources :

Example 16 with InstallmentPrincipalAndInterest

use of org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest in project head by mifos.

the class VariableInstallmentPrincipalWithInterestGenerator method generateEqualInstallments.

@Override
public List<InstallmentPrincipalAndInterest> generateEqualInstallments(LoanInterestCalculationDetails loanInterestCalculationDetails) {
    List<InstallmentPrincipalAndInterest> principalAndInterestDetails = new ArrayList<InstallmentPrincipalAndInterest>();
    Double interestRate = loanInterestCalculationDetails.getInterestRate();
    Money principalOutstanding = loanInterestCalculationDetails.getLoanAmount();
    LocalDate interestPeriodStartDate = loanInterestCalculationDetails.getDisbursementDate();
    Money totalPrincipal = new Money(principalOutstanding.getCurrency(), Double.valueOf("0"));
    if (loanInterestCalculationDetails.getTotalInstallmentAmounts().isEmpty()) {
        // if empty just divide loan amount by number of installments to get principal due per installment
        Money installmentPrincipalDue = principalOutstanding.divide(loanInterestCalculationDetails.getNumberOfInstallments());
        int index = 0;
        for (DateTime installmentDueDate : loanInterestCalculationDetails.getLoanScheduleDates()) {
            Money interestForInstallment = formula.calculate(principalOutstanding, interestRate, interestPeriodStartDate, new LocalDate(installmentDueDate));
            principalOutstanding = principalOutstanding.subtract(installmentPrincipalDue);
            interestPeriodStartDate = new LocalDate(installmentDueDate);
            totalPrincipal = totalPrincipal.add(installmentPrincipalDue);
            if (index == loanInterestCalculationDetails.getLoanScheduleDates().size() - 1) {
                // last installment
                Money realTotalPrincipal = new Money(totalPrincipal.getCurrency(), totalPrincipal.toString());
                if (realTotalPrincipal.isLessThan(loanInterestCalculationDetails.getLoanAmount())) {
                    Money difference = loanInterestCalculationDetails.getLoanAmount().subtract(realTotalPrincipal);
                    installmentPrincipalDue = installmentPrincipalDue.add(difference);
                } else if (realTotalPrincipal.isGreaterThan(loanInterestCalculationDetails.getLoanAmount())) {
                    Money difference = realTotalPrincipal.subtract(loanInterestCalculationDetails.getLoanAmount());
                    installmentPrincipalDue = installmentPrincipalDue.subtract(difference);
                }
            }
            principalAndInterestDetails.add(new InstallmentPrincipalAndInterest(installmentPrincipalDue, interestForInstallment));
            index++;
        }
    } else {
        int index = 0;
        List<Money> totalInstallmentAmounts = loanInterestCalculationDetails.getTotalInstallmentAmounts();
        for (DateTime installmentDueDate : loanInterestCalculationDetails.getLoanScheduleDates()) {
            Money totalInstallmentPayment = totalInstallmentAmounts.get(index);
            Money interestForInstallment = formula.calculate(principalOutstanding, interestRate, interestPeriodStartDate, new LocalDate(installmentDueDate));
            principalOutstanding = principalOutstanding.subtract(totalInstallmentPayment);
            interestPeriodStartDate = new LocalDate(installmentDueDate);
            Money installmentPrincipalDue = totalInstallmentPayment.subtract(interestForInstallment);
            totalPrincipal = totalPrincipal.add(installmentPrincipalDue);
            if (index == loanInterestCalculationDetails.getLoanScheduleDates().size() - 1) {
                // last installment
                Money realTotalPrincipal = new Money(totalPrincipal.getCurrency(), totalPrincipal.toString());
                if (realTotalPrincipal.isLessThan(loanInterestCalculationDetails.getLoanAmount())) {
                    Money difference = loanInterestCalculationDetails.getLoanAmount().subtract(realTotalPrincipal);
                    installmentPrincipalDue = installmentPrincipalDue.add(difference);
                } else if (realTotalPrincipal.isGreaterThan(loanInterestCalculationDetails.getLoanAmount())) {
                    Money difference = realTotalPrincipal.subtract(loanInterestCalculationDetails.getLoanAmount());
                    installmentPrincipalDue = installmentPrincipalDue.subtract(difference);
                }
            }
            principalAndInterestDetails.add(new InstallmentPrincipalAndInterest(installmentPrincipalDue, interestForInstallment));
            index++;
        }
    }
    return principalAndInterestDetails;
}
Also used : Money(org.mifos.framework.util.helpers.Money) ArrayList(java.util.ArrayList) InstallmentPrincipalAndInterest(org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime)

Example 17 with InstallmentPrincipalAndInterest

use of org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest in project head by mifos.

the class DecliningBalanceEqualPrincipalWithInterestGenerator method generateDecliningInstallmentsInterestOnly_v2.

private List<InstallmentPrincipalAndInterest> generateDecliningInstallmentsInterestOnly_v2(Money loanAmount, Integer gracePeriodDuration, Double interestFractionalRatePerInstallment, Integer days, Integer durationInDays) {
    List<InstallmentPrincipalAndInterest> emiInstallments = new ArrayList<InstallmentPrincipalAndInterest>();
    Money zeroPrincipal = MoneyUtils.zero(loanAmount.getCurrency());
    for (int i = 0; i < gracePeriodDuration; i++) {
        if (i < 1) {
            int noOfDaysBetweenDisbursaldateAndNextMeetingDate = days;
            Money interestPerInstallment = loanAmount.multiply(interestFractionalRatePerInstallment, noOfDaysBetweenDisbursaldateAndNextMeetingDate, durationInDays);
            InstallmentPrincipalAndInterest installment = new InstallmentPrincipalAndInterest(zeroPrincipal, interestPerInstallment);
            emiInstallments.add(installment);
        } else {
            InstallmentPrincipalAndInterest installment = new InstallmentPrincipalAndInterest(zeroPrincipal, loanAmount.multiply(interestFractionalRatePerInstallment));
            emiInstallments.add(installment);
        }
    }
    return emiInstallments;
}
Also used : Money(org.mifos.framework.util.helpers.Money) ArrayList(java.util.ArrayList) InstallmentPrincipalAndInterest(org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest)

Aggregations

InstallmentPrincipalAndInterest (org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest)17 Money (org.mifos.framework.util.helpers.Money)16 ArrayList (java.util.ArrayList)15 DateTime (org.joda.time.DateTime)6 LocalDate (org.joda.time.LocalDate)5 GraceType (org.mifos.accounts.productdefinition.util.helpers.GraceType)4 InstallmentDate (org.mifos.accounts.util.helpers.InstallmentDate)4 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)2 RateAmountFlag (org.mifos.accounts.fees.util.helpers.RateAmountFlag)2 LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)2 InterestType (org.mifos.accounts.productdefinition.util.helpers.InterestType)2 FeeInstallment (org.mifos.accounts.util.helpers.FeeInstallment)2 ConfigurationPersistence (org.mifos.config.persistence.ConfigurationPersistence)2 ScheduledEvent (org.mifos.schedule.ScheduledEvent)2 BigDecimal (java.math.BigDecimal)1 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)1 AccountException (org.mifos.accounts.exceptions.AccountException)1 LoanFeeScheduleEntity (org.mifos.accounts.loan.business.LoanFeeScheduleEntity)1 DefaultLoanScheduleRounder (org.mifos.clientportfolio.newloan.domain.DefaultLoanScheduleRounder)1 DefaultLoanScheduleRounderHelper (org.mifos.clientportfolio.newloan.domain.DefaultLoanScheduleRounderHelper)1