Search in sources :

Example 11 with LoanCreationInstallmentDto

use of org.mifos.dto.domain.LoanCreationInstallmentDto in project head by mifos.

the class LoanScheduleFormBean method validateCalculateAndReviewLoanSchedule.

/**
     * validateXXXX is invoked on transition from state
     */
public void validateCalculateAndReviewLoanSchedule(ValidationContext context) {
    MessageContext messageContext = context.getMessageContext();
    if (this.variableInstallmentsAllowed) {
        prevalidateDueDateIsNonNull(messageContext);
        prevalidateActualPaymentDateIsNonNull(messageContext);
        prevalidateTotalIsNonNull(messageContext);
        prevalidateAmountPaidIsNonNull(messageContext);
        if (!messageContext.hasErrorMessages()) {
            boolean resetRedoLoanAccountDetails = false;
            LoanScheduleDto recalculatedLoanSchedule = this.loanAccountController.retrieveLoanSchedule(customerId, loanAccountFormBean.getProductId(), loanAccountFormBean, this, resetRedoLoanAccountDetails);
            // set values on fields
            this.variableInstallments = recalculatedLoanSchedule.getInstallments();
            int installIndex = 0;
            for (LoanCreationInstallmentDto installment : this.variableInstallments) {
                this.installmentAmounts.set(installIndex, installment.getTotal());
                installIndex++;
            }
        }
        Errors inputInstallmentsErrors = loanAccountServiceFacade.validateInputInstallments(disbursementDate, minGapInDays, maxGapInDays, minInstallmentAmount, variableInstallments, customerId);
        Errors scheduleErrors = loanAccountServiceFacade.validateInstallmentSchedule(variableInstallments, minInstallmentAmount);
        handleErrors(messageContext, inputInstallmentsErrors, scheduleErrors);
    } else {
        prevalidateAmountPaidIsNonNull(messageContext);
        for (int index = 0; index < this.actualPaymentAmounts.size(); index++) {
            Double newTotal = Double.valueOf("0.0");
            Number newTotalEntry = this.actualPaymentAmounts.get(index);
            if (newTotalEntry != null) {
                newTotal = newTotalEntry.doubleValue();
            } else {
                this.actualPaymentAmounts.set(index, newTotal);
            }
        }
    }
    List<LoanRepaymentTransaction> loanRepaymentTransaction = new ArrayList<LoanRepaymentTransaction>();
    this.loanRepaymentPaidInstallmentsWithRunningBalance = new ArrayList<LoanRepaymentRunningBalance>();
    this.loanRepaymentFutureInstallments = new ArrayList<LoanRepaymentFutureInstallments>();
    // if any actual payment data exists, calculate
    validatePaymentsAndAmounts(messageContext, this.actualPaymentDates, this.actualPaymentAmounts);
    List<LoanInstallmentPostPayment> installmentsPostPayment = new ArrayList<LoanInstallmentPostPayment>();
    int paymentIndex = 0;
    if (!messageContext.hasErrorMessages()) {
        for (Number actualPayment : this.actualPaymentAmounts) {
            BigDecimal remainingPayment = BigDecimal.valueOf(actualPayment.doubleValue());
            LocalDate paymentDate = new LocalDate(this.actualPaymentDates.get(paymentIndex));
            loanRepaymentTransaction.add(new LoanRepaymentTransaction(paymentDate, remainingPayment));
            int installmentIndex = 0;
            while (remainingPayment.doubleValue() > BigDecimal.ZERO.doubleValue() && installmentIndex < this.actualPaymentAmounts.size()) {
                LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installmentIndex);
                Double installmentTotalAmount = this.installmentAmounts.get(installmentIndex).doubleValue();
                LocalDate dueDate = new LocalDate(this.installments.get(installmentIndex));
                if (installmentsPostPayment.isEmpty() || installmentsPostPayment.size() <= installmentIndex) {
                    BigDecimal feesPaid = BigDecimal.valueOf(installmentDetails.getFees());
                    if (remainingPayment.doubleValue() >= installmentDetails.getFees()) {
                        remainingPayment = remainingPayment.subtract(feesPaid);
                    } else {
                        feesPaid = remainingPayment;
                        remainingPayment = remainingPayment.subtract(feesPaid);
                    }
                    BigDecimal interestPaid = BigDecimal.valueOf(installmentDetails.getInterest());
                    if (remainingPayment.doubleValue() >= installmentDetails.getInterest()) {
                        remainingPayment = remainingPayment.subtract(interestPaid);
                    } else {
                        interestPaid = remainingPayment;
                        remainingPayment = remainingPayment.subtract(interestPaid);
                    }
                    BigDecimal principalPaid = BigDecimal.valueOf(installmentDetails.getPrincipal());
                    if (remainingPayment.doubleValue() >= installmentDetails.getPrincipal()) {
                        remainingPayment = remainingPayment.subtract(principalPaid);
                    } else {
                        principalPaid = remainingPayment;
                        remainingPayment = remainingPayment.subtract(principalPaid);
                    }
                    BigDecimal totalInstallmentPaid = feesPaid.add(interestPaid).add(principalPaid);
                    LoanInstallmentPostPayment loanInstallmentPostPayment = new LoanInstallmentPostPayment(installmentDetails.getInstallmentNumber(), dueDate, paymentDate, feesPaid, interestPaid, principalPaid, totalInstallmentPaid, installmentTotalAmount);
                    installmentsPostPayment.add(loanInstallmentPostPayment);
                } else {
                    LoanInstallmentPostPayment paidInstallment = installmentsPostPayment.get(installmentIndex);
                    if (paidInstallment.isNotFullyPaid()) {
                        BigDecimal feesToBePaid = BigDecimal.valueOf(installmentDetails.getFees()).subtract(paidInstallment.getFeesPaid());
                        if (remainingPayment.doubleValue() >= feesToBePaid.doubleValue()) {
                            remainingPayment = remainingPayment.subtract(feesToBePaid);
                        } else {
                            feesToBePaid = remainingPayment;
                            remainingPayment = remainingPayment.subtract(feesToBePaid);
                        }
                        BigDecimal interestToBePaid = BigDecimal.valueOf(installmentDetails.getInterest()).subtract(paidInstallment.getInterestPaid());
                        if (remainingPayment.doubleValue() >= interestToBePaid.doubleValue()) {
                            remainingPayment = remainingPayment.subtract(interestToBePaid);
                        } else {
                            interestToBePaid = remainingPayment;
                            remainingPayment = remainingPayment.subtract(interestToBePaid);
                        }
                        BigDecimal principalToBePaid = BigDecimal.valueOf(installmentDetails.getPrincipal()).subtract(paidInstallment.getPrincipalPaid());
                        if (remainingPayment.doubleValue() >= principalToBePaid.doubleValue()) {
                            remainingPayment = remainingPayment.subtract(principalToBePaid);
                        } else {
                            principalToBePaid = remainingPayment;
                            remainingPayment = remainingPayment.subtract(principalToBePaid);
                        }
                        BigDecimal totalInstallmentPaid = feesToBePaid.add(interestToBePaid).add(principalToBePaid);
                        paidInstallment.setLastPaymentDate(paymentDate);
                        paidInstallment.setFeesPaid(paidInstallment.getFeesPaid().add(feesToBePaid));
                        paidInstallment.setInterestPaid(paidInstallment.getInterestPaid().add(interestToBePaid));
                        paidInstallment.setPrincipalPaid(paidInstallment.getPrincipalPaid().add(principalToBePaid));
                        paidInstallment.setTotalInstallmentPaid(paidInstallment.getTotalInstallmentPaid().add(totalInstallmentPaid));
                    }
                }
                installmentIndex++;
            }
            paymentIndex++;
        }
    }
    // remaining running balance
    BigDecimal cumulativeFeesPaid = BigDecimal.ZERO;
    BigDecimal cumulativeInterestPaid = BigDecimal.ZERO;
    BigDecimal cumulativePrincipalPaid = BigDecimal.ZERO;
    BigDecimal cumulativeTotalInstallmentPaid = BigDecimal.ZERO;
    for (LoanInstallmentPostPayment installment : installmentsPostPayment) {
        cumulativeFeesPaid = cumulativeFeesPaid.add(installment.getFeesPaid());
        cumulativeInterestPaid = cumulativeInterestPaid.add(installment.getInterestPaid());
        cumulativePrincipalPaid = cumulativePrincipalPaid.add(installment.getPrincipalPaid());
        cumulativeTotalInstallmentPaid = cumulativeTotalInstallmentPaid.add(installment.getTotalInstallmentPaid());
        if (installment.isNotFullyPaid()) {
            BigDecimal remainingFees = this.totalLoanFees.subtract(cumulativeFeesPaid);
            BigDecimal remainingInterest = this.totalLoanInterest.subtract(cumulativeInterestPaid);
            BigDecimal remainingTotalInstallment = this.loanPrincipal.add(this.totalLoanFees).add(this.totalLoanInterest).subtract(cumulativeTotalInstallmentPaid);
            BigDecimal remainingPrincipal = remainingTotalInstallment.subtract(remainingInterest).subtract(remainingFees);
            LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installment.getInstallmentNumber() - 1);
            LoanCreationInstallmentDto installmentPaidDetails = new LoanCreationInstallmentDto(installment.getInstallmentNumber(), new LocalDate(installmentDetails.getDueDate()), installment.getPrincipalPaid().doubleValue(), installment.getInterestPaid().doubleValue(), installment.getFeesPaid().doubleValue(), BigDecimal.ZERO.doubleValue(), installment.getTotalInstallmentPaid().doubleValue());
            this.loanRepaymentPaidInstallmentsWithRunningBalance.add(new LoanRepaymentRunningBalance(installmentPaidDetails, installment.getTotalInstallmentPaid(), remainingPrincipal, remainingInterest, remainingFees, remainingTotalInstallment, installment.getLastPaymentDate(), this.actualPaymentTypes.get(installment.getInstallmentNumber() - 1)));
            BigDecimal outstandingInstallmentPrincipal = BigDecimal.valueOf(installmentDetails.getPrincipal()).subtract(installment.getPrincipalPaid());
            BigDecimal outstandingInstallmentInterest = BigDecimal.valueOf(installmentDetails.getInterest()).subtract(installment.getInterestPaid());
            BigDecimal outstandingInstallmentFees = BigDecimal.valueOf(installmentDetails.getFees()).subtract(installment.getFeesPaid());
            this.loanRepaymentFutureInstallments.add(new LoanRepaymentFutureInstallments(installmentDetails.getInstallmentNumber(), installmentDetails.getDueDate(), outstandingInstallmentPrincipal, outstandingInstallmentInterest, outstandingInstallmentFees, outstandingInstallmentPrincipal.add(outstandingInstallmentInterest).add(outstandingInstallmentFees)));
        } else {
            BigDecimal remainingFees = this.totalLoanFees.subtract(cumulativeFeesPaid);
            BigDecimal remainingInterest = this.totalLoanInterest.subtract(cumulativeInterestPaid);
            BigDecimal remainingTotalInstallment = this.loanPrincipal.add(this.totalLoanFees).add(this.totalLoanInterest).subtract(cumulativeTotalInstallmentPaid);
            BigDecimal remainingPrincipal = remainingTotalInstallment.subtract(remainingInterest).subtract(remainingFees);
            LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installment.getInstallmentNumber() - 1);
            this.loanRepaymentPaidInstallmentsWithRunningBalance.add(new LoanRepaymentRunningBalance(installmentDetails, installment.getTotalInstallmentPaid(), remainingPrincipal, remainingInterest, remainingFees, remainingTotalInstallment, installment.getLastPaymentDate(), this.actualPaymentTypes.get(installment.getInstallmentNumber() - 1)));
        }
    }
    int lastHandledFutureInstallmentNumber = loanRepaymentPaidInstallmentsWithRunningBalance.size();
    if (!this.loanRepaymentFutureInstallments.isEmpty()) {
        lastHandledFutureInstallmentNumber = this.loanRepaymentFutureInstallments.get(this.loanRepaymentFutureInstallments.size() - 1).getInstallmentNumber();
    }
    for (LoanCreationInstallmentDto installmentDto : this.repaymentInstallments) {
        if (installmentDto.getInstallmentNumber() > lastHandledFutureInstallmentNumber) {
            this.loanRepaymentFutureInstallments.add(new LoanRepaymentFutureInstallments(installmentDto.getInstallmentNumber(), installmentDto.getDueDate(), BigDecimal.valueOf(installmentDto.getPrincipal()), BigDecimal.valueOf(installmentDto.getInterest()), BigDecimal.valueOf(installmentDto.getFees()), BigDecimal.valueOf(installmentDto.getTotal())));
        }
    }
}
Also used : LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) ArrayList(java.util.ArrayList) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Errors(org.mifos.platform.validations.Errors) MessageContext(org.springframework.binding.message.MessageContext)

Example 12 with LoanCreationInstallmentDto

use of org.mifos.dto.domain.LoanCreationInstallmentDto in project head by mifos.

the class LoanAccountServiceFacadeWebTier method createLoanSchedule.

@Override
public LoanScheduleDto createLoanSchedule(CreateLoanSchedule createLoanSchedule) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    // assemble into domain entities
    LoanOfferingBO loanProduct = this.loanProductDao.findById(createLoanSchedule.getProductId());
    CustomerBO customer = this.customerDao.findCustomerById(createLoanSchedule.getCustomerId());
    Money loanAmountDisbursed = new Money(loanProduct.getCurrency(), createLoanSchedule.getLoanAmount());
    List<AccountFeesEntity> accountFeeEntities = assembleAccountFees(createLoanSchedule.getAccountFeeEntities());
    LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(loanAmountDisbursed, createLoanSchedule.getDisbursementDate(), createLoanSchedule.getInterestRate(), createLoanSchedule.getNumberOfInstallments(), createLoanSchedule.getGraceDuration(), accountFeeEntities, new ArrayList<AccountPenaltiesEntity>());
    Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
    boolean loanScheduleIndependentOfCustomerMeetingEnabled = createLoanSchedule.isRepaymentIndependentOfCustomerMeetingSchedule();
    MeetingBO loanMeeting = null;
    if (loanScheduleIndependentOfCustomerMeetingEnabled) {
        loanMeeting = this.createNewMeetingForRepaymentDay(createLoanSchedule.getDisbursementDate(), createLoanSchedule, customer);
        if (loanProduct.isVariableInstallmentsAllowed()) {
            loanMeeting.setMeetingStartDate(createLoanSchedule.getDisbursementDate().toDateMidnight().toDate());
        }
    } else {
        MeetingDto customerMeetingDto = customer.getCustomerMeetingValue().toDto();
        loanMeeting = new MeetingFactory().create(customerMeetingDto);
        Short recurAfter = loanProduct.getLoanOfferingMeeting().getMeeting().getRecurAfter();
        loanMeeting.getMeetingDetails().setRecurAfter(recurAfter);
    }
    LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
    LoanSchedule loanSchedule = this.loanScheduleService.generate(loanProduct, customer, loanMeeting, overridenDetail, configuration, userContext.getBranchId(), accountFeeEntities, createLoanSchedule.getDisbursementDate());
    // translate to DTO form
    List<LoanCreationInstallmentDto> installments = new ArrayList<LoanCreationInstallmentDto>();
    Short digitsAfterDecimal = AccountingRules.getDigitsAfterDecimal();
    for (LoanScheduleEntity loanScheduleEntity : loanSchedule.getRoundedLoanSchedules()) {
        Integer installmentNumber = loanScheduleEntity.getInstallmentId().intValue();
        LocalDate dueDate = new LocalDate(loanScheduleEntity.getActionDate());
        String principal = loanScheduleEntity.getPrincipal().toString(digitsAfterDecimal);
        String interest = loanScheduleEntity.getInterest().toString(digitsAfterDecimal);
        String fees = loanScheduleEntity.getTotalFees().toString(digitsAfterDecimal);
        String penalty = "0.0";
        String total = loanScheduleEntity.getPrincipal().add(loanScheduleEntity.getInterest()).add(loanScheduleEntity.getTotalFees()).toString(digitsAfterDecimal);
        LoanCreationInstallmentDto installment = new LoanCreationInstallmentDto(installmentNumber, dueDate, Double.valueOf(principal), Double.valueOf(interest), Double.valueOf(fees), Double.valueOf(penalty), Double.valueOf(total));
        installments.add(installment);
    }
    return new LoanScheduleDto(customer.getDisplayName(), Double.valueOf(createLoanSchedule.getLoanAmount().doubleValue()), createLoanSchedule.getDisbursementDate(), loanProduct.getGraceType().getValue().intValue(), installments);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) MeetingBO(org.mifos.application.meeting.business.MeetingBO) ArrayList(java.util.ArrayList) MeetingFactory(org.mifos.application.meeting.business.MeetingFactory) LocalDate(org.joda.time.LocalDate) LoanScheduleConfiguration(org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration) Money(org.mifos.framework.util.helpers.Money) CustomerBO(org.mifos.customers.business.CustomerBO) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity) CreateLoanSchedule(org.mifos.clientportfolio.loan.service.CreateLoanSchedule) LoanSchedule(org.mifos.clientportfolio.newloan.domain.LoanSchedule) UserContext(org.mifos.security.util.UserContext) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) MifosUser(org.mifos.security.MifosUser) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) LoanProductOverridenDetail(org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail) MeetingDto(org.mifos.dto.domain.MeetingDto) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO)

Aggregations

LoanCreationInstallmentDto (org.mifos.dto.domain.LoanCreationInstallmentDto)12 ArrayList (java.util.ArrayList)10 LocalDate (org.joda.time.LocalDate)8 BigDecimal (java.math.BigDecimal)7 LoanScheduleDto (org.mifos.dto.screen.LoanScheduleDto)5 Money (org.mifos.framework.util.helpers.Money)5 DateTime (org.joda.time.DateTime)4 RepaymentScheduleInstallment (org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment)4 Errors (org.mifos.platform.validations.Errors)4 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)3 AccountPenaltiesEntity (org.mifos.accounts.business.AccountPenaltiesEntity)3 LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)3 MeetingBO (org.mifos.application.meeting.business.MeetingBO)3 LoanProductOverridenDetail (org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail)3 LoanSchedule (org.mifos.clientportfolio.newloan.domain.LoanSchedule)3 LoanScheduleConfiguration (org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration)3 CustomerBO (org.mifos.customers.business.CustomerBO)3 MifosUser (org.mifos.security.MifosUser)3 UserContext (org.mifos.security.util.UserContext)3 Test (org.junit.Test)2