Search in sources :

Example 36 with RepaymentScheduleInstallment

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

the class StandardAccountService method handleLoanDisbursal.

public void handleLoanDisbursal(Locale locale, LoanBO loan, PersonnelBO personnelBO, BigDecimal paymentAmount, PaymentTypeDto paymentType, LocalDate receiptLocalDate, LocalDate paymentLocalDate, String receiptId, Short paymentTypeIdForFees, Integer accountForTransferId) throws PersistenceException, AccountException {
    if ("MPESA".equals(paymentType.getName())) {
        paymentAmount = computeWithdrawnForMPESA(paymentAmount, loan);
    }
    PaymentTypeEntity paymentTypeEntity = legacyMasterDao.getPersistentObject(PaymentTypeEntity.class, paymentType.getValue());
    Money amount = new Money(loan.getCurrency(), paymentAmount);
    Date receiptDate = null;
    if (null != receiptLocalDate) {
        receiptDate = receiptLocalDate.toDateMidnight().toDate();
    }
    Date transactionDate = paymentLocalDate.toDateMidnight().toDate();
    AccountPaymentEntity disbursalPayment = new AccountPaymentEntity(loan, amount, receiptId, receiptDate, paymentTypeEntity, transactionDate);
    disbursalPayment.setCreatedByUser(personnelBO);
    Double interestRate = loan.getInterestRate();
    Date oldDisbursementDate = loan.getDisbursementDate();
    List<RepaymentScheduleInstallment> originalInstallments = loan.toRepaymentScheduleDto(locale);
    loan.disburseLoan(disbursalPayment, paymentTypeIdForFees, accountForTransferId);
    if (!loan.isVariableInstallmentsAllowed()) {
        originalInstallments = loan.toRepaymentScheduleDto(locale);
    }
    Date newDisbursementDate = loan.getDisbursementDate();
    boolean variableInstallmentsAllowed = loan.isVariableInstallmentsAllowed();
    loanBusinessService.adjustDatesForVariableInstallments(variableInstallmentsAllowed, loan.isFixedRepaymentSchedule(), originalInstallments, oldDisbursementDate, newDisbursementDate, loan.getOfficeId());
    Date today = new LocalDate().toDateMidnight().toDate();
    Date disburseDay = new LocalDate(oldDisbursementDate).toDateMidnight().toDate();
    if (!today.equals(disburseDay)) {
        loanBusinessService.applyDailyInterestRatesWhereApplicable(new LoanScheduleGenerationDto(newDisbursementDate, loan, variableInstallmentsAllowed, amount, interestRate), originalInstallments);
    }
    loanBusinessService.persistOriginalSchedule(loan);
}
Also used : PaymentTypeEntity(org.mifos.application.master.business.PaymentTypeEntity) Money(org.mifos.framework.util.helpers.Money) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) LoanScheduleGenerationDto(org.mifos.accounts.loan.business.service.LoanScheduleGenerationDto) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) LocalDate(org.joda.time.LocalDate) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 37 with RepaymentScheduleInstallment

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

the class GroupLoanAccountServiceFacadeWebTier method fixMemberAndParentInstallmentDetails.

// fix installment details in order to match sum of interest of member accounts to interest of parent account
// update member fee amounts in order to match parent fee amount
private void fixMemberAndParentInstallmentDetails(LoanBO loan, List<LoanBO> memberLoans) {
    Map<Integer, LoanScheduleEntity> parentScheduleEntities = loan.getLoanScheduleEntityMap();
    List<RepaymentScheduleInstallment> correctedInstallments = new ArrayList<RepaymentScheduleInstallment>();
    for (Integer installmentId : parentScheduleEntities.keySet()) {
        LoanScheduleEntity parentEntity = parentScheduleEntities.get(installmentId);
        Map<Short, BigDecimal> feeAmountsForInstallment = new HashMap<Short, BigDecimal>();
        for (AccountFeesActionDetailEntity feesActionDetailEntity : parentEntity.getAccountFeesActionDetails()) {
            feeAmountsForInstallment.put(feesActionDetailEntity.getFee().getFeeId(), feesActionDetailEntity.getFeeAmount().getAmount());
        }
        RepaymentScheduleInstallment correctedInstallment = new RepaymentScheduleInstallment();
        correctedInstallment.setInstallment(installmentId);
        correctedInstallment.setDueDateValue(parentEntity.getActionDate());
        BigDecimal principal = BigDecimal.ZERO;
        BigDecimal interest = BigDecimal.ZERO;
        for (LoanBO memberLoan : memberLoans) {
            LoanScheduleEntity memberEntity = memberLoan.getLoanScheduleEntityMap().get(installmentId);
            principal = principal.add(memberEntity.getPrincipal().getAmount());
            interest = interest.add(memberEntity.getInterest().getAmount());
            for (AccountFeesActionDetailEntity feesActionDetailEntity : memberEntity.getAccountFeesActionDetails()) {
                if (feesActionDetailEntity.getFee().getFeeType().equals(RateAmountFlag.RATE)) {
                    continue;
                }
                BigDecimal currentAmount = feeAmountsForInstallment.get(feesActionDetailEntity.getFee().getFeeId());
                currentAmount = currentAmount.subtract(feesActionDetailEntity.getFeeAmount().getAmount());
                if (currentAmount.compareTo(BigDecimal.ZERO) == -1) {
                    BigDecimal toUpdate = feesActionDetailEntity.getFeeAmount().getAmount().add(currentAmount);
                    feesActionDetailEntity.updateFeeAmount(toUpdate);
                    currentAmount = BigDecimal.ZERO;
                }
                feeAmountsForInstallment.put(feesActionDetailEntity.getFee().getFeeId(), currentAmount);
            }
        }
        correctedInstallment.setPrincipal(new Money(parentEntity.getPrincipal().getCurrency(), principal));
        correctedInstallment.setInterest(new Money(parentEntity.getInterest().getCurrency(), interest));
        correctedInstallments.add(correctedInstallment);
    }
    loan.updateInstallmentSchedule(correctedInstallments);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) HashMap(java.util.HashMap) AccountFeesActionDetailEntity(org.mifos.accounts.business.AccountFeesActionDetailEntity) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment)

Example 38 with RepaymentScheduleInstallment

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

the class LoanAccountServiceFacadeWebTier method validateInputInstallments.

@Override
public Errors validateInputInstallments(Date disbursementDate, Integer minGapInDays, Integer maxGapInDays, BigDecimal minInstallmentAmount, List<LoanCreationInstallmentDto> dtoInstallments, Integer customerId) {
    Short officeId = customerDao.findCustomerById(customerId).getOfficeId();
    VariableInstallmentDetailsBO variableInstallmentDetails = new VariableInstallmentDetailsBO();
    variableInstallmentDetails.setMinGapInDays(minGapInDays);
    variableInstallmentDetails.setMaxGapInDays(maxGapInDays);
    InstallmentValidationContext context = new InstallmentValidationContext(disbursementDate, variableInstallmentDetails, minInstallmentAmount, holidayServiceFacade, officeId);
    MifosCurrency currency = Money.getDefaultCurrency();
    List<RepaymentScheduleInstallment> installments = new ArrayList<RepaymentScheduleInstallment>();
    for (LoanCreationInstallmentDto dto : dtoInstallments) {
        Money principal = new Money(currency, dto.getPrincipal());
        Money interest = new Money(currency, dto.getInterest());
        Money fees = new Money(currency, dto.getFees());
        Money miscFees = new Money(currency);
        Money miscPenalty = new Money(currency);
        RepaymentScheduleInstallment installment = new RepaymentScheduleInstallment(dto.getInstallmentNumber(), dto.getDueDate(), principal, interest, fees, miscFees, miscPenalty);
        installment.setTotalAndTotalValue(new Money(currency, dto.getTotal()));
        installments.add(installment);
    }
    return installmentsValidator.validateInputInstallments(installments, context);
}
Also used : Money(org.mifos.framework.util.helpers.Money) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) VariableInstallmentDetailsBO(org.mifos.accounts.productdefinition.business.VariableInstallmentDetailsBO) ArrayList(java.util.ArrayList) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) InstallmentValidationContext(org.mifos.accounts.loan.business.service.validators.InstallmentValidationContext) MifosCurrency(org.mifos.application.master.business.MifosCurrency)

Example 39 with RepaymentScheduleInstallment

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

the class InstallmentsValidatorTest method shouldValidateInstallmentSchedule.

@Test
public void shouldValidateInstallmentSchedule() {
    RepaymentScheduleInstallment installment1 = installmentBuilder.reset(locale).withInstallment(1).withDueDateValue("01-Nov-2010").build();
    RepaymentScheduleInstallment installment2 = installmentBuilder.reset(locale).withInstallment(2).withDueDateValue("06-Nov-2010").build();
    RepaymentScheduleInstallment installment3 = installmentBuilder.reset(locale).withInstallment(3).withDueDateValue("08-Nov-2010").build();
    List<RepaymentScheduleInstallment> installments = asList(installment1, installment2, installment3);
    installmentsValidator.validateInstallmentSchedule(installments, BigDecimal.ZERO);
    verify(installmentRulesValidator).validateForMinimumInstallmentAmount(installments, BigDecimal.ZERO);
}
Also used : RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) Test(org.junit.Test)

Example 40 with RepaymentScheduleInstallment

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

the class InstallmentsValidatorTest method validateShouldCallFormatListOfAndRulesValidators.

@Test
public void validateShouldCallFormatListOfAndRulesValidators() throws Exception {
    RepaymentScheduleInstallment installment1 = installmentBuilder.reset(locale).withInstallment(1).withDueDateValue("01-Nov-2010").build();
    RepaymentScheduleInstallment installment2 = installmentBuilder.reset(locale).withInstallment(2).withDueDateValue("06-Nov-2010").build();
    RepaymentScheduleInstallment installment3 = installmentBuilder.reset(locale).withInstallment(3).withDueDateValue("08-Nov-2010").build();
    List<RepaymentScheduleInstallment> installments = asList(installment1, installment2, installment3);
    Errors errors = installmentsValidator.validateInputInstallments(installments, getValidationContext(null));
    for (RepaymentScheduleInstallment installment : installments) {
        verify(installmentFormatValidator).validateDueDateFormat(installment);
        verify(installmentFormatValidator).validateTotalAmountFormat(installment);
    }
    verify(listOfInstallmentsValidator).validateDuplicateDueDates(installments);
    verify(listOfInstallmentsValidator).validateOrderingOfDueDates(installments);
    verify(installmentRulesValidator).validateForDisbursementDate(eq(installments), any(Date.class));
    verify(installmentRulesValidator).validateDueDatesForVariableInstallments(eq(installments), any(VariableInstallmentDetailsBO.class), any(Date.class));
    verify(installmentRulesValidator).validateForHolidays(eq(installments), any(HolidayServiceFacade.class), eq(officeId));
    assertThat(errors.hasErrors(), is(false));
}
Also used : Errors(org.mifos.platform.validations.Errors) HolidayServiceFacade(org.mifos.application.admin.servicefacade.HolidayServiceFacade) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) VariableInstallmentDetailsBO(org.mifos.accounts.productdefinition.business.VariableInstallmentDetailsBO) Date(java.util.Date) Test(org.junit.Test)

Aggregations

RepaymentScheduleInstallment (org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment)66 Test (org.junit.Test)44 Money (org.mifos.framework.util.helpers.Money)34 Date (java.util.Date)27 ArrayList (java.util.ArrayList)19 ErrorEntry (org.mifos.platform.validations.ErrorEntry)19 LoanBO (org.mifos.accounts.loan.business.LoanBO)7 VariableInstallmentDetailsBO (org.mifos.accounts.productdefinition.business.VariableInstallmentDetailsBO)7 OriginalScheduleInfoDto (org.mifos.accounts.loan.business.service.OriginalScheduleInfoDto)6 LoanAccountActionForm (org.mifos.accounts.loan.struts.actionforms.LoanAccountActionForm)6 Errors (org.mifos.platform.validations.Errors)6 MifosCurrency (org.mifos.application.master.business.MifosCurrency)5 MifosRuntimeException (org.mifos.core.MifosRuntimeException)5 BigDecimal (java.math.BigDecimal)4 ActionErrors (org.apache.struts.action.ActionErrors)4 Ignore (org.junit.Ignore)4 HashSet (java.util.HashSet)3 LocalDate (org.joda.time.LocalDate)3 OriginalLoanScheduleEntity (org.mifos.accounts.loan.business.OriginalLoanScheduleEntity)3 MessageLookup (org.mifos.application.master.MessageLookup)3