use of org.mifos.accounts.productdefinition.util.helpers.InterestType in project head by mifos.
the class IndividualLoanScheduleFactory method create.
@Override
public LoanSchedule create(LocalDate disbursementDate, List<DateTime> loanScheduleDates, List<Number> totalInstallmentAmounts, LoanOfferingBO loanProduct, CustomerBO customer, MeetingBO loanMeeting, Money loanAmount, Double interestRate, Integer interestDays, Integer gracePeriodDuration, List<AccountFeesEntity> accountFees) {
GraceType graceType = loanProduct.getGraceType();
InterestType interestType = loanProduct.getInterestType();
boolean variableInstallmentLoanProduct = loanProduct.isVariableInstallmentsAllowed();
boolean roundingDifferenceInFirstPayment = loanProduct.isRoundingDifferenceInFirstPayment();
Integer numberOfInstallments = loanScheduleDates.size();
RecurringScheduledEventFactory scheduledEventFactory = new RecurringScheduledEventFactoryImpl();
ScheduledEvent meetingScheduledEvent = scheduledEventFactory.createScheduledEventFrom(loanMeeting);
Integer installmentNumber = 1;
List<InstallmentDate> dueInstallmentDates = new ArrayList<InstallmentDate>();
for (DateTime scheduledDate : loanScheduleDates) {
dueInstallmentDates.add(new InstallmentDate(installmentNumber.shortValue(), scheduledDate.toLocalDate().toDateMidnight().toDate()));
installmentNumber++;
}
if (loanProduct.isPrinDueLastInst()) {
// Principal due on last installment has been cut, so throw an exception if we reach this code.
throw new BusinessRuleException(AccountConstants.NOT_SUPPORTED_EMI_GENERATION);
}
// loan interest calculation for various interest calculation algorithms
LoanDecliningInterestAnnualPeriodCalculator decliningInterestAnnualPeriodCalculator = new LoanDecliningInterestAnnualPeriodCalculatorFactory().create(loanMeeting.getRecurrenceType());
Double decliningInterestAnnualPeriod = decliningInterestAnnualPeriodCalculator.calculate(loanMeeting.getRecurAfter().intValue(), interestDays);
Double interestFractionalRatePerInstallment = interestRate / decliningInterestAnnualPeriod / 100;
LoanDurationInAccountingYearsCalculator loanDurationInAccountingYearsCalculator = new LoanDurationInAccountingYearsCalculatorFactory().create(loanMeeting.getRecurrenceType());
Double durationInYears = loanDurationInAccountingYearsCalculator.calculate(loanMeeting.getRecurAfter().intValue(), numberOfInstallments, interestDays);
List<Money> totalInstallmentAmountsAsMoney = new ArrayList<Money>();
for (Number totalInstallmentAmount : totalInstallmentAmounts) {
Money totalAmount = new Money(loanAmount.getCurrency(), BigDecimal.valueOf(totalInstallmentAmount.doubleValue()));
totalInstallmentAmountsAsMoney.add(totalAmount);
}
LoanInterestCalculationDetails loanInterestCalculationDetails = new LoanInterestCalculationDetails(loanAmount, interestRate, graceType, gracePeriodDuration, numberOfInstallments, durationInYears, interestFractionalRatePerInstallment, disbursementDate, loanScheduleDates);
loanInterestCalculationDetails.setTotalInstallmentAmounts(totalInstallmentAmountsAsMoney);
LoanInterestCalculatorFactory loanInterestCalculatorFactory = new LoanInterestCalculatorFactoryImpl();
LoanInterestCalculator loanInterestCalculator = loanInterestCalculatorFactory.create(interestType, variableInstallmentLoanProduct);
Money loanInterest = loanInterestCalculator.calculate(loanInterestCalculationDetails);
// end of loan Interest creation
EqualInstallmentGeneratorFactory equalInstallmentGeneratorFactory = new EqualInstallmentGeneratorFactoryImpl();
PrincipalWithInterestGenerator equalInstallmentGenerator = equalInstallmentGeneratorFactory.create(interestType, loanInterest, variableInstallmentLoanProduct);
List<InstallmentPrincipalAndInterest> EMIInstallments = equalInstallmentGenerator.generateEqualInstallments(loanInterestCalculationDetails);
List<LoanScheduleEntity> unroundedLoanSchedules = createUnroundedLoanSchedulesFromInstallments(dueInstallmentDates, loanInterest, loanAmount, meetingScheduledEvent, EMIInstallments, accountFees, customer);
Money rawAmount = calculateTotalFeesAndInterestForLoanSchedules(unroundedLoanSchedules, loanAmount.getCurrency(), accountFees);
List<LoanScheduleEntity> allExistingLoanSchedules = new ArrayList<LoanScheduleEntity>();
List<LoanScheduleEntity> finalisedLoanSchedules = new ArrayList<LoanScheduleEntity>(unroundedLoanSchedules);
if (variableInstallmentLoanProduct && totalInstallmentAmounts.isEmpty()) {
// only round inital loan schedule of variable installments product.
LoanScheduleRounder loanScheduleInstallmentRounder = new VariableInstallmentLoanScheduleRounder();
finalisedLoanSchedules = loanScheduleInstallmentRounder.round(graceType, gracePeriodDuration.shortValue(), loanAmount, interestType, unroundedLoanSchedules, allExistingLoanSchedules);
} else if (!variableInstallmentLoanProduct && roundingDifferenceInFirstPayment) {
LoanScheduleRounderHelper loanScheduleRounderHelper = new DefaultLoanScheduleRounderHelper();
LoanScheduleRounder loanScheduleInstallmentRounder = new FirstInstallmentRoudingDifferenceLoanScheduleRounder(loanScheduleRounderHelper);
finalisedLoanSchedules = loanScheduleInstallmentRounder.round(graceType, gracePeriodDuration.shortValue(), loanAmount, interestType, unroundedLoanSchedules, allExistingLoanSchedules);
} else if (!variableInstallmentLoanProduct) {
LoanScheduleRounderHelper loanScheduleRounderHelper = new DefaultLoanScheduleRounderHelper();
LoanScheduleRounder loanScheduleInstallmentRounder = new DefaultLoanScheduleRounder(loanScheduleRounderHelper);
finalisedLoanSchedules = loanScheduleInstallmentRounder.round(graceType, gracePeriodDuration.shortValue(), loanAmount, interestType, unroundedLoanSchedules, allExistingLoanSchedules);
}
return new LoanSchedule(finalisedLoanSchedules, rawAmount);
}
use of org.mifos.accounts.productdefinition.util.helpers.InterestType in project head by mifos.
the class EqualInstallmentGeneratorFactoryTest method shouldAlsoUseDecliningBalanceEqualInstallmentGenerator.
@Test
public void shouldAlsoUseDecliningBalanceEqualInstallmentGenerator() {
InterestType interestType = InterestType.DECLINING_PB;
// exercise test
PrincipalWithInterestGenerator equalInstallmentGenerator = equalInstallmentGeneratorFactory.create(interestType, loanInterest, false);
// verification
assertThat(equalInstallmentGenerator, is(instanceOf(DecliningBalancePrincipalWithInterestGenerator.class)));
}
use of org.mifos.accounts.productdefinition.util.helpers.InterestType in project head by mifos.
the class EqualInstallmentGeneratorFactoryTest method shouldUseDecliningBalanceEqualPrincipalEqualInstallmentGenerator.
@Test
public void shouldUseDecliningBalanceEqualPrincipalEqualInstallmentGenerator() {
InterestType interestType = InterestType.DECLINING_EPI;
// exercise test
PrincipalWithInterestGenerator equalInstallmentGenerator = equalInstallmentGeneratorFactory.create(interestType, loanInterest, false);
// verification
assertThat(equalInstallmentGenerator, is(instanceOf(DecliningBalanceEqualPrincipalWithInterestGenerator.class)));
}
use of org.mifos.accounts.productdefinition.util.helpers.InterestType in project head by mifos.
the class EqualInstallmentGeneratorFactoryTest method shouldThrowRuntimeException.
@Test(expected = BusinessRuleException.class)
public void shouldThrowRuntimeException() {
InterestType interestType = InterestType.COMPOUND;
// exercise test
equalInstallmentGeneratorFactory.create(interestType, loanInterest, false);
}
Aggregations