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;
}
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;
}
Aggregations