Search in sources :

Example 26 with Errors

use of org.mifos.platform.validations.Errors in project head by mifos.

the class LoanBusinessService method computeExtraInterest.

public Errors computeExtraInterest(LoanBO loan, Date asOfDate) {
    Errors errors = new Errors();
    validateForComputeExtraInterestDate(loan, asOfDate, errors);
    if (!errors.hasErrors()) {
        scheduleCalculatorAdaptor.computeExtraInterest(loan, asOfDate);
    }
    return errors;
}
Also used : Errors(org.mifos.platform.validations.Errors)

Example 27 with Errors

use of org.mifos.platform.validations.Errors in project head by mifos.

the class LoanAccountServiceFacadeWebTierTest method shouldValidateInstallments.

@Test
public void shouldValidateInstallments() {
    int customerId = 121;
    Errors errors = new Errors();
    when(installmentsValidator.validateInputInstallments(anyListOf(RepaymentScheduleInstallment.class), any(InstallmentValidationContext.class))).thenReturn(errors);
    when(customerDao.findCustomerById(customerId)).thenReturn(customer);
    when(customer.getOfficeId()).thenReturn(Short.valueOf("1"));
    Date disbursementDate = null;
    Integer minGapInDays = Integer.valueOf(0);
    Integer maxGapInDays = Integer.valueOf(0);
    BigDecimal minInstallmentAmount = BigDecimal.ZERO;
    Errors actual = loanAccountServiceFacade.validateInputInstallments(disbursementDate, minGapInDays, maxGapInDays, minInstallmentAmount, new ArrayList<LoanCreationInstallmentDto>(), customerId);
    assertThat(actual, is(errors));
    verify(installmentsValidator).validateInputInstallments(anyListOf(RepaymentScheduleInstallment.class), any(InstallmentValidationContext.class));
    verify(customerDao).findCustomerById(customerId);
    verify(customer).getOfficeId();
}
Also used : Errors(org.mifos.platform.validations.Errors) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) InstallmentValidationContext(org.mifos.accounts.loan.business.service.validators.InstallmentValidationContext) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 28 with Errors

use of org.mifos.platform.validations.Errors in project head by mifos.

the class LoanServiceFacadeWebTierTest method shouldValidateForInstallmentDateBeyondCashFlowData.

@Ignore
@Test
public void shouldValidateForInstallmentDateBeyondCashFlowData() {
    ArrayList<MonthlyCashFlowDetail> monthlyCashFlows = new ArrayList<MonthlyCashFlowDetail>();
    DateTime dateTime = new DateTime().withDate(2010, 10, 30);
    monthlyCashFlows.add(new MonthlyCashFlowDetail(dateTime, new BigDecimal(123), new BigDecimal(234), ""));
    monthlyCashFlows.add(new MonthlyCashFlowDetail(dateTime.plusMonths(1), new BigDecimal(123), new BigDecimal(234), ""));
    monthlyCashFlows.add(new MonthlyCashFlowDetail(dateTime.plusMonths(2), new BigDecimal(123), new BigDecimal(234), ""));
    CashFlowDetail cashFlowDetail = new CashFlowDetail(monthlyCashFlows);
    CashFlowForm cashFlowForm = new CashFlowForm(cashFlowDetail, false, new BigDecimal(1000), 10d);
    cashFlowForm.setTotalExpenses(BigDecimal.valueOf(76));
    cashFlowForm.setTotalRevenues(BigDecimal.valueOf(55));
    RepaymentScheduleInstallment installment = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("10").withDueDateValue("30-Nov-2010").build();
    RepaymentScheduleInstallment installmentBeforeCashFlowDate = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("10").withDueDateValue("30-Nov-2010").build();
    List<RepaymentScheduleInstallment> installments = asList(installment);
    Errors errors;
    //        errors = loanServiceFacade.validateCashFlowForInstallments(installments, cashFlowForm, 1600d);
    //        assertThat(errors.hasErrorEntryWithCode(AccountConstants.INSTALLMENT_BEYOND_CASHFLOW_DATE), is(false));
    RepaymentScheduleInstallment installmentBeyondCashFlowDate = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("10").withDueDateValue("30-Jan-2011").build();
    installments = asList(installmentBeyondCashFlowDate);
//        errors = loanServiceFacade.validateCashFlowForInstallments(installments, cashFlowForm, 1600d);
//        assertThat(errors.hasErrorEntryWithCode(AccountConstants.INSTALLMENT_BEYOND_CASHFLOW_DATE), is(true));
//        errors = loanServiceFacade.validateCashFlowForInstallments(installments, cashFlowForm, 1700d);
//        assertThat(errors.hasErrorEntryWithCode(AccountConstants.INSTALLMENT_BEYOND_CASHFLOW_DATE), is(true));
}
Also used : Money(org.mifos.framework.util.helpers.Money) Errors(org.mifos.platform.validations.Errors) MonthlyCashFlowDetail(org.mifos.platform.cashflow.service.MonthlyCashFlowDetail) CashFlowDetail(org.mifos.platform.cashflow.service.CashFlowDetail) RepaymentScheduleInstallment(org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment) ArrayList(java.util.ArrayList) CashFlowForm(org.mifos.platform.cashflow.ui.model.CashFlowForm) MonthlyCashFlowDetail(org.mifos.platform.cashflow.service.MonthlyCashFlowDetail) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with Errors

use of org.mifos.platform.validations.Errors 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 30 with Errors

use of org.mifos.platform.validations.Errors in project head by mifos.

the class BranchReportParameterValidatorTest method testValidatorCallsServiceIfDataPresent.

public void testValidatorCallsServiceIfDataPresent() throws Exception {
    expect(branchReportServiceMock.isReportDataPresentForRundateAndBranchId(BRANCH_ID, VALID_RUN_DATE)).andReturn(Boolean.TRUE);
    replay(branchReportServiceMock);
    Errors errors = new Errors();
    validator.validate(new BranchReportParameterForm(BRANCH_ID, VALID_RUN_DATE), errors);
    verify(branchReportServiceMock);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.mifos.platform.validations.Errors) BranchReportParameterForm(org.mifos.reports.business.BranchReportParameterForm)

Aggregations

Errors (org.mifos.platform.validations.Errors)31 Date (java.util.Date)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 LocalDate (org.joda.time.LocalDate)7 RepaymentScheduleInstallment (org.mifos.accounts.loan.util.helpers.RepaymentScheduleInstallment)7 BigDecimal (java.math.BigDecimal)6 ActionErrors (org.apache.struts.action.ActionErrors)4 OriginalScheduleInfoDto (org.mifos.accounts.loan.business.service.OriginalScheduleInfoDto)4 LoanCreationInstallmentDto (org.mifos.dto.domain.LoanCreationInstallmentDto)4 ErrorEntry (org.mifos.platform.validations.ErrorEntry)4 LoanBO (org.mifos.accounts.loan.business.LoanBO)3 LoanOfferingBO (org.mifos.accounts.productdefinition.business.LoanOfferingBO)3 CustomerBO (org.mifos.customers.business.CustomerBO)3 Locale (java.util.Locale)2 DateTime (org.joda.time.DateTime)2 Ignore (org.junit.Ignore)2 LoanAccountActionForm (org.mifos.accounts.loan.struts.actionforms.LoanAccountActionForm)2 LoanScheduleDto (org.mifos.dto.screen.LoanScheduleDto)2 Money (org.mifos.framework.util.helpers.Money)2