Search in sources :

Example 6 with InterestType

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);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) InstallmentDate(org.mifos.accounts.util.helpers.InstallmentDate) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) GraceType(org.mifos.accounts.productdefinition.util.helpers.GraceType) ScheduledEvent(org.mifos.schedule.ScheduledEvent) InterestType(org.mifos.accounts.productdefinition.util.helpers.InterestType) InstallmentPrincipalAndInterest(org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest)

Example 7 with InterestType

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)));
}
Also used : InterestType(org.mifos.accounts.productdefinition.util.helpers.InterestType) Test(org.junit.Test)

Example 8 with InterestType

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)));
}
Also used : InterestType(org.mifos.accounts.productdefinition.util.helpers.InterestType) Test(org.junit.Test)

Example 9 with InterestType

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);
}
Also used : InterestType(org.mifos.accounts.productdefinition.util.helpers.InterestType) Test(org.junit.Test)

Aggregations

InterestType (org.mifos.accounts.productdefinition.util.helpers.InterestType)9 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 DateTime (org.joda.time.DateTime)3 GraceType (org.mifos.accounts.productdefinition.util.helpers.GraceType)3 PersistenceException (org.mifos.framework.exceptions.PersistenceException)3 LocalDate (org.joda.time.LocalDate)2 AccountException (org.mifos.accounts.exceptions.AccountException)2 FeeBO (org.mifos.accounts.fees.business.FeeBO)2 FundBO (org.mifos.accounts.fund.business.FundBO)2 InstallmentPrincipalAndInterest (org.mifos.accounts.loan.util.helpers.InstallmentPrincipalAndInterest)2 PenaltyBO (org.mifos.accounts.penalties.business.PenaltyBO)2 LoanOfferingBO (org.mifos.accounts.productdefinition.business.LoanOfferingBO)2 InstallmentDate (org.mifos.accounts.util.helpers.InstallmentDate)2 InterestTypesEntity (org.mifos.application.master.business.InterestTypesEntity)2 BigDecimal (java.math.BigDecimal)1 LinkedHashMap (java.util.LinkedHashMap)1 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)1 AmountFeeBO (org.mifos.accounts.fees.business.AmountFeeBO)1 FeeFrequencyTypeEntity (org.mifos.accounts.fees.business.FeeFrequencyTypeEntity)1