Search in sources :

Example 11 with Errors

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

the class BranchReportParameterValidatorTest method setUp.

@Override
protected void setUp() throws Exception {
    branchReportServiceMock = createMock(BranchReportService.class);
    validator = new BranchReportParamValidator(new ArrayList<String>(), branchReportServiceMock);
    errors = new Errors();
    validForm = new BranchReportParameterForm(BRANCH_ID, VALID_RUN_DATE);
    invalidForm = new BranchReportParameterForm(INVALID_BRANCH_ID, VALID_RUN_DATE);
}
Also used : Errors(org.mifos.platform.validations.Errors) ArrayList(java.util.ArrayList) BranchReportParameterForm(org.mifos.reports.business.BranchReportParameterForm) BranchReportService(org.mifos.reports.business.service.BranchReportService) IBranchReportService(org.mifos.reports.business.service.IBranchReportService)

Example 12 with Errors

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

the class LoanServiceFacadeWebTierTest method shouldValidateForRepaymentCapacity.

@Ignore
@Test
public void shouldValidateForRepaymentCapacity() {
    CashFlowDetail cashFlowDetail = new CashFlowDetail(Collections.EMPTY_LIST);
    BigDecimal loanAmount = new BigDecimal(1000);
    CashFlowForm cashFlowForm = new CashFlowForm(cashFlowDetail, false, loanAmount, 10d);
    cashFlowForm.setTotalExpenses(BigDecimal.valueOf(76));
    cashFlowForm.setTotalRevenues(BigDecimal.valueOf(55).add(loanAmount));
    RepaymentScheduleInstallment installment1 = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("10").build();
    RepaymentScheduleInstallment installment2 = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("20").build();
    RepaymentScheduleInstallment installment3 = installmentBuilder.reset(locale).withPrincipal(new Money(rupee, "4.9")).withTotalValue("30").build();
    List<RepaymentScheduleInstallment> installments = asList(installment1, installment2, installment3);
    // calcuated repayment capacity is 3298.33
    Errors errors;
//        errors = loanServiceFacade.validateCashFlowForInstallments(installments, cashFlowForm, 1600d);
//        assertThat(errors.hasErrorEntryWithCode(AccountConstants.REPAYMENT_CAPACITY_LESS_THAN_ALLOWED), is(false));
//        errors = loanServiceFacade.validateCashFlowForInstallments(installments, cashFlowForm, 3300d);
//        assertThat(errors.hasErrorEntryWithCode(AccountConstants.REPAYMENT_CAPACITY_LESS_THAN_ALLOWED), 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) CashFlowForm(org.mifos.platform.cashflow.ui.model.CashFlowForm) BigDecimal(java.math.BigDecimal) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with Errors

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

the class BirtReportValidationAction method validateReportParameters.

private void validateReportParameters(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ReportParameterValidator<ReportParameterForm> validator = new ReportParameterValidatorFactory().getValidator(request.getParameter("__report"));
    addDatabaseInformationsToRequestAttributes(request);
    if (validator == null || validator.isAFreshRequest(request)) {
        // go to report parameter page
        request.getRequestDispatcher("/preview").forward(request, response);
        return;
    }
    ReportParameterForm form = validator.buildReportParameterForm(request);
    Errors errors = new Errors();
    validator.validate(form, errors);
    ModifiableParameterServletRequest modifiedRequest = new ModifiableParameterServletRequest(request);
    if (errors.hasErrors()) {
        request.setAttribute(ERRORS, errors);
        validator.removeRequestParameters(modifiedRequest, form, errors);
    }
    request.getRequestDispatcher("/preview").forward(modifiedRequest, response);
}
Also used : Errors(org.mifos.platform.validations.Errors) ReportParameterForm(org.mifos.reports.business.ReportParameterForm) ReportParameterValidatorFactory(org.mifos.reports.business.validator.ReportParameterValidatorFactory) ModifiableParameterServletRequest(org.mifos.framework.servlet.ModifiableParameterServletRequest)

Example 14 with Errors

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

the class CashFlowSummaryFormBean method validateSummaryOfCashflow.

/**
     * validateXXXX is invoked on transition from state
     */
public void validateSummaryOfCashflow(ValidationContext context) {
    MessageContext messageContext = context.getMessageContext();
    if (this.variableInstallmentsAllowed) {
        prevalidateDueDateIsNonNull(messageContext);
        prevalidateActualPaymentDateIsNonNull(messageContext);
        prevalidateTotalIsNonNull(messageContext);
        prevalidateAmountPaidIsNonNull(messageContext);
        if (!messageContext.hasErrorMessages()) {
            LoanScheduleDto recalculatedLoanSchedule = this.loanAccountController.retrieveLoanSchedule(customerId, productId, loanAccountFormBean, this, false);
            // 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);
        if (inputInstallmentsErrors.hasErrors()) {
            for (ErrorEntry fieldError : inputInstallmentsErrors.getErrorEntries()) {
                addErrorMessageToContext(messageContext, fieldError);
            }
        }
        if (scheduleErrors.hasErrors()) {
            for (ErrorEntry fieldError : scheduleErrors.getErrorEntries()) {
                addErrorMessageToContext(messageContext, fieldError);
            }
        }
    } else {
        prevalidateTotalIsNonNull(messageContext);
        for (int index = 0; index < this.installmentAmounts.size(); index++) {
            Double newTotal = Double.valueOf("0.0");
            Number newTotalEntry = this.installmentAmounts.get(index);
            if (newTotalEntry != null) {
                newTotal = newTotalEntry.doubleValue();
            } else {
                this.installmentAmounts.set(index, newTotal);
            }
        }
    }
    if (!messageContext.hasErrorMessages()) {
        DateTime firstInstallmentDueDate = installments.get(0);
        DateTime lastInstallmentDueDate = installments.get(installments.size() - 1);
        this.loanInstallmentsDto = new LoanInstallmentsDto(this.loanInstallmentsDto.getLoanAmount(), this.loanInstallmentsDto.getTotalInstallmentAmount(), firstInstallmentDueDate.toDate(), lastInstallmentDueDate.toDate());
        Errors warnings = loanAccountServiceFacade.validateCashFlowForInstallmentsForWarnings(cashFlowDataDtos, productId);
        Errors errors = loanAccountServiceFacade.validateCashFlowForInstallments(loanInstallmentsDto, monthlyCashFlows, repaymentCapacity, cashFlowTotalBalance);
        if (warnings.hasErrors()) {
            for (ErrorEntry fieldError : warnings.getErrorEntries()) {
                addErrorMessageToContext(messageContext, fieldError);
            }
        }
        if (errors.hasErrors()) {
            for (ErrorEntry fieldError : errors.getErrorEntries()) {
                addErrorMessageToContext(messageContext, fieldError);
            }
        }
    }
    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) LoanInstallmentsDto(org.mifos.dto.screen.LoanInstallmentsDto) ErrorEntry(org.mifos.platform.validations.ErrorEntry) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Errors(org.mifos.platform.validations.Errors) MessageContext(org.springframework.binding.message.MessageContext)

Example 15 with Errors

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

the class LoanAccountFormBeanTest method setUp.

@Before
public void setUp() {
    MifosBeanValidator validator = new MifosBeanValidator();
    LocalValidatorFactoryBean targetValidator = new LocalValidatorFactoryBean();
    targetValidator.afterPropertiesSet();
    validator.setTargetValidator(targetValidator);
    loanAccountFormBean = new LoanAccountFormBean();
    Integer productId = Integer.valueOf(1);
    Integer customerId = Integer.valueOf(1);
    loanAccountFormBean.setProductId(productId);
    loanAccountFormBean.setCustomerId(customerId);
    loanAccountFormBean.setPurposeOfLoanMandatory(false);
    loanAccountFormBean.setSourceOfFundsMandatory(false);
    loanAccountFormBean.setExternalIdMandatory(false);
    loanAccountFormBean.setCollateralTypeAndNotesHidden(false);
    loanAccountFormBean.setAmount(Double.valueOf("1000.0"));
    loanAccountFormBean.setMinAllowedAmount(Integer.valueOf(400));
    loanAccountFormBean.setMaxAllowedAmount(Integer.valueOf(20000));
    loanAccountFormBean.setInterestRate(Double.valueOf(10.0));
    loanAccountFormBean.setMinAllowedInterestRate(Double.valueOf(1.0));
    loanAccountFormBean.setMaxAllowedInterestRate(Double.valueOf(20.0));
    loanAccountFormBean.setDigitsBeforeDecimalForInterest(10);
    loanAccountFormBean.setDigitsAfterDecimalForInterest(5);
    loanAccountFormBean.setDigitsBeforeDecimalForMonetaryAmounts(14);
    loanAccountFormBean.setDigitsAfterDecimalForMonetaryAmounts(1);
    loanAccountFormBean.setAdditionalFees(createAdditionalFeesMocks());
    loanAccountFormBean.setDisbursementDateDD(24);
    loanAccountFormBean.setDisbursementDateMM(02);
    loanAccountFormBean.setDisbursementDateYY(2011);
    loanAccountFormBean.setNumberOfInstallments(12);
    loanAccountFormBean.setMinNumberOfInstallments(1);
    loanAccountFormBean.setMaxNumberOfInstallments(12);
    loanAccountFormBean.setValidator(validator);
    loanAccountFormBean.setLoanDisbursementDateValidationServiceFacade(loanDisbursementDateValidationServiceFacade);
    context = new StubValidationContext();
    when(loanDisbursementDateValidationServiceFacade.validateLoanDisbursementDate((LocalDate) anyObject(), anyInt(), anyInt())).thenReturn(new Errors());
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) Errors(org.mifos.platform.validations.Errors) MifosBeanValidator(org.mifos.platform.validation.MifosBeanValidator) StubValidationContext(org.mifos.ui.validation.StubValidationContext) Before(org.junit.Before)

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