Search in sources :

Example 36 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class LocalizationConverterTest method testParseDoubleForMoney.

@Test
public void testParseDoubleForMoney() {
    String doubleValueString = "2.5";
    Double dValue = 2.5;
    Short digitsAfterForMoneySaved = AccountingRules.getDigitsAfterDecimal();
    Short digitsAfterForMoney = 1;
    AccountingRules.setDigitsAfterDecimal(digitsAfterForMoney);
    Locale locale = Localization.getInstance().getConfiguredLocale();
    DoubleConversionResult result = converter.parseDoubleForMoney(doubleValueString);
    if (locale.getCountry().equalsIgnoreCase("GB") && locale.getLanguage().equalsIgnoreCase("EN")) {
        Assert.assertEquals(result.getDoubleValue(), dValue);
        // if the wrong decimal separator is entered, error will be returned
        doubleValueString = "2,59";
        result = converter.parseDoubleForMoney(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.NOT_ALL_NUMBER);
        doubleValueString = "2a59";
        result = converter.parseDoubleForMoney(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.NOT_ALL_NUMBER);
        doubleValueString = "123456789111111.59";
        result = converter.parseDoubleForMoney(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.EXCEEDING_NUMBER_OF_DIGITS_AFTER_DECIMAL_SEPARATOR_FOR_MONEY);
        Assert.assertEquals(result.getErrors().get(1), ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_MONEY);
        doubleValueString = "222222222111111.5";
        result = converter.parseDoubleForMoney(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_MONEY);
    }
    AccountingRules.setDigitsAfterDecimal(digitsAfterForMoneySaved);
    converter.setCurrentLocale(locale);
}
Also used : Locale(java.util.Locale) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) Test(org.junit.Test)

Example 37 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class InterOfficeTransferActionForm method mandatoryCheck.

private ActionErrors mandatoryCheck(UserContext userContext) {
    Locale locale = userContext.getPreferredLocale();
    ResourceBundle resources = ResourceBundle.getBundle(FilePaths.SIMPLE_ACCOUNTING_RESOURCE, locale);
    String trxn_Date = resources.getString(SimpleAccountingConstants.TRXNDATE);
    String trxn_Type = resources.getString(SimpleAccountingConstants.TRXNTYPE);
    String office_Hierarchy = resources.getString(SimpleAccountingConstants.OFFICE_HIERARCHY);
    String officeId = resources.getString(SimpleAccountingConstants.OFFICE);
    String main_Account = resources.getString(SimpleAccountingConstants.MAIN_ACCOUNT);
    String subAccount = resources.getString(SimpleAccountingConstants.ACCOUNT_HEAD);
    String Amount = resources.getString(SimpleAccountingConstants.AMOUNT);
    String Notes = resources.getString(SimpleAccountingConstants.TRXN_NOTES);
    ActionErrors errors = new ActionErrors();
    java.sql.Date currentDate = null;
    try {
        currentDate = DateUtils.getLocaleDate(userContext.getPreferredLocale(), DateUtils.getCurrentDate(userContext.getPreferredLocale()));
    } catch (InvalidDateException ide) {
        errors.add(SimpleAccountingConstants.INVALIDDATE, new ActionMessage(SimpleAccountingConstants.INVALIDDATE));
    }
    java.sql.Date trxnDate = null;
    if (getTrxnDate() == null || "".equals(getTrxnDate())) {
        errors.add(SimpleAccountingConstants.MANDATORYENTER, new ActionMessage(SimpleAccountingConstants.MANDATORYENTER, trxn_Date));
    } else if (getTrxnDate() != null && !getTrxnDate().equals("") && !DateUtils.isValidDate(getTrxnDate())) {
        errors = trxnDateValidate(errors, locale);
    } else if (DateUtils.isValidDate(getTrxnDate())) {
        try {
            trxnDate = DateUtils.getDateAsSentFromBrowser(getTrxnDate());
        } catch (InvalidDateException ide) {
            errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.INVALID_TRXN_DATE, trxn_Date));
        }
        if (trxnDate.compareTo(currentDate) > 0) {
            errors.add(SimpleAccountingConstants.INVALID_FUTURE, new ActionMessage(SimpleAccountingConstants.INVALID_FUTURE, trxn_Date));
        }
    }
    if (fromOfficeHierarchy == null || "".equals(fromOfficeHierarchy.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, office_Hierarchy));
    }
    if (fromOffice == null || "".equals(fromOffice.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, officeId));
    }
    if (toOfficeHierarchy == null || "".equals(toOfficeHierarchy.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, office_Hierarchy));
    }
    if (toOffice == null || "".equals(toOffice.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, officeId));
    }
    if (debitAccountHead == null || "".equals(debitAccountHead.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, main_Account));
    }
    if (creditAccountHead == null || "".equals(creditAccountHead.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, subAccount));
    }
    if (amount == null || "".equals(amount.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, Amount));
    }
    if (StringUtils.isNotBlank(getAmount())) {
        DoubleConversionResult conversionResult = validateAmount(getAmount(), Amount, errors);
        if (conversionResult.getErrors().size() == 0 && !(conversionResult.getDoubleValue() > 0.0)) {
            addError(errors, SimpleAccountingConstants.AMOUNT, SimpleAccountingConstants.ERRORS_MUST_BE_GREATER_THAN_ZERO, Amount);
        }
    }
    if (notes == null || "".equals(notes.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, Notes));
    }
    if (getChequeDate() != null && !getChequeDate().equals("") && !DateUtils.isValidDate(getChequeDate()))
        errors = chequeDateValidate(errors, locale);
    return errors;
}
Also used : Locale(java.util.Locale) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) ActionMessage(org.apache.struts.action.ActionMessage) ResourceBundle(java.util.ResourceBundle) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) ActionErrors(org.apache.struts.action.ActionErrors)

Example 38 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class ViewStageTransactionActionForm method validateAmount.

protected DoubleConversionResult validateAmount(String amountString, MifosCurrency currency, String fieldPropertyKey, ActionErrors errors, String installmentNo) {
    String fieldName = fieldPropertyKey;
    DoubleConversionResult conversionResult = parseDoubleDecimalForMoney(amountString, currency);
    for (ConversionError error : conversionResult.getErrors()) {
        String errorText = error.toLocalizedMessage(currency);
        addError(errors, fieldName, "errors.generic", fieldName, errorText);
    }
    return conversionResult;
}
Also used : ConversionError(org.mifos.framework.util.helpers.ConversionError) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult)

Example 39 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class JournalVoucherActionForm method mandatoryCheck.

private ActionErrors mandatoryCheck(UserContext userContext) {
    Locale locale = userContext.getPreferredLocale();
    ResourceBundle resources = ResourceBundle.getBundle(FilePaths.SIMPLE_ACCOUNTING_RESOURCE, locale);
    String voucher_date = resources.getString(SimpleAccountingConstants.VOUCHERDATE);
    String office_Hierarchy = resources.getString(SimpleAccountingConstants.OFFICE_HIERARCHY);
    String officeId = resources.getString(SimpleAccountingConstants.OFFICE);
    String main_Account = resources.getString(SimpleAccountingConstants.DEBIT_ACCOUNT);
    String subAccount = resources.getString(SimpleAccountingConstants.CREDIT_ACCOUNT);
    String Amount = resources.getString(SimpleAccountingConstants.AMOUNT);
    String Notes = resources.getString(SimpleAccountingConstants.VOUCHER_NOTES);
    ActionErrors errors = new ActionErrors();
    java.sql.Date currentDate = null;
    try {
        currentDate = DateUtils.getLocaleDate(userContext.getPreferredLocale(), DateUtils.getCurrentDate(userContext.getPreferredLocale()));
    } catch (InvalidDateException ide) {
        errors.add(SimpleAccountingConstants.INVALIDDATE, new ActionMessage(SimpleAccountingConstants.INVALIDDATE));
    }
    java.sql.Date voucherDate = null;
    if (getVoucherDate() == null || "".equals(getVoucherDate())) {
        errors.add(SimpleAccountingConstants.MANDATORYENTER, new ActionMessage(SimpleAccountingConstants.MANDATORYENTER, voucher_date));
    } else if (getVoucherDate() != null && !getVoucherDate().equals("") && !DateUtils.isValidDate(getVoucherDate())) {
        errors = voucherDateValidate(errors, locale);
    } else if (DateUtils.isValidDate(getVoucherDate())) {
        try {
            voucherDate = DateUtils.getDateAsSentFromBrowser(getVoucherDate());
        } catch (InvalidDateException ide) {
            errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.INVALIDDATE, voucher_date));
        }
        if (voucherDate.compareTo(currentDate) > 0) {
            errors.add(SimpleAccountingConstants.INVALID_FUTURE, new ActionMessage(SimpleAccountingConstants.INVALID_FUTURE, voucher_date));
        }
    }
    if (officeHierarchy == null || "".equals(officeHierarchy.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, office_Hierarchy));
    }
    if (office == null || "".equals(office.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, officeId));
    }
    if (debitAccountHead == null || "".equals(debitAccountHead.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, main_Account));
    }
    if (creditAccountHead == null || "".equals(creditAccountHead.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, subAccount));
    }
    if (amount == null || "".equals(amount.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, Amount));
    }
    if (StringUtils.isNotBlank(getAmount())) {
        DoubleConversionResult conversionResult = validateAmount(getAmount(), Amount, errors);
        if (conversionResult.getErrors().size() == 0 && !(conversionResult.getDoubleValue() > 0.0)) {
            addError(errors, SimpleAccountingConstants.AMOUNT, SimpleAccountingConstants.ERRORS_MUST_BE_GREATER_THAN_ZERO, Amount);
        }
    }
    if (voucherNotes == null || "".equals(voucherNotes.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, Notes));
    }
    return errors;
}
Also used : Locale(java.util.Locale) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) ActionMessage(org.apache.struts.action.ActionMessage) ResourceBundle(java.util.ResourceBundle) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) ActionErrors(org.apache.struts.action.ActionErrors)

Example 40 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class MultipleGeneralLedgerActionForm method mandatoryCheck.

private ActionErrors mandatoryCheck(UserContext userContext) {
    Locale locale = userContext.getPreferredLocale();
    ResourceBundle resources = ResourceBundle.getBundle(FilePaths.SIMPLE_ACCOUNTING_RESOURCE, locale);
    String trxn_Date = resources.getString(SimpleAccountingConstants.TRXNDATE);
    String trxn_Type = resources.getString(SimpleAccountingConstants.TRXNTYPE);
    String office_Hierarchy = resources.getString(SimpleAccountingConstants.OFFICE_HIERARCHY);
    String officeId = resources.getString(SimpleAccountingConstants.OFFICE);
    String main_Account = resources.getString(SimpleAccountingConstants.MAIN_ACCOUNT);
    String subAccount = resources.getString(SimpleAccountingConstants.ACCOUNT_HEAD);
    String Amount = resources.getString(SimpleAccountingConstants.AMOUNT);
    String Notes = resources.getString(SimpleAccountingConstants.TRXN_NOTES);
    ActionErrors errors = new ActionErrors();
    java.sql.Date currentDate = null;
    try {
        currentDate = DateUtils.getLocaleDate(userContext.getPreferredLocale(), DateUtils.getCurrentDate(userContext.getPreferredLocale()));
    } catch (InvalidDateException ide) {
        errors.add(SimpleAccountingConstants.INVALIDDATE, new ActionMessage(SimpleAccountingConstants.INVALIDDATE));
    }
    java.sql.Date trxnDate = null;
    if (getTrxnDate() == null || "".equals(getTrxnDate())) {
        errors.add(SimpleAccountingConstants.MANDATORYENTER, new ActionMessage(SimpleAccountingConstants.MANDATORYENTER, trxn_Date));
    } else if (getTrxnDate() != null && !getTrxnDate().equals("") && !DateUtils.isValidDate(getTrxnDate())) {
        errors = trxnDateValidate(errors, locale);
    } else if (DateUtils.isValidDate(getTrxnDate())) {
        try {
            trxnDate = DateUtils.getDateAsSentFromBrowser(getTrxnDate());
        } catch (InvalidDateException ide) {
            errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.INVALID_TRXN_DATE, trxn_Date));
        }
        if (trxnDate.compareTo(currentDate) > 0) {
            errors.add(SimpleAccountingConstants.INVALID_FUTURE, new ActionMessage(SimpleAccountingConstants.INVALID_FUTURE, trxn_Date));
        }
    }
    if (trxnType == null || "".equals(trxnType.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, trxn_Type));
    }
    if (officeHierarchy == null || "".equals(officeHierarchy.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, office_Hierarchy));
    }
    if (office == null || "".equals(office.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, officeId));
    }
    if (mainAccount == null || "".equals(mainAccount.trim())) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, main_Account));
    }
    /*if (accountHead == null || "".equals(accountHead.trim())) {
			errors.add(SimpleAccountingConstants.MANDATORYFIELDS,
					new ActionMessage(
							SimpleAccountingConstants.MANDATORYFIELDS,
							subAccount));
		}*/
    String[] amounts123 = getAmount1();
    String[] accountHead123 = getAccountHead1();
    String[] notes123 = getNotes1();
    for (int i = 0; i < amounts123.length; i++) {
        double amoun = Double.parseDouble(amounts123[i]);
        //String accounthead=accountHead123[i];
        if (amoun == 0) {
            errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, Amount));
        }
        if (accountHead123[i].isEmpty()) {
            errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.MANDATORYFIELDS, subAccount));
        }
    }
    //memberId == null || "".equals(memberId.trim())||
    if (memberId.length() != 10 && memberId.length() > 0) {
        errors.add(SimpleAccountingConstants.MANDATORYFIELDS, new ActionMessage(SimpleAccountingConstants.ENTER_GRETERTHAN, memberId));
    }
    if (StringUtils.isNotBlank(getAmount())) {
        DoubleConversionResult conversionResult = validateAmount(getAmount(), Amount, errors);
        if (conversionResult.getErrors().size() == 0 && !(conversionResult.getDoubleValue() > 0.0)) {
            addError(errors, SimpleAccountingConstants.AMOUNT, SimpleAccountingConstants.ERRORS_MUST_BE_GREATER_THAN_ZERO, Amount);
        }
    }
    if (getChequeDate() != null && !getChequeDate().equals("") && !DateUtils.isValidDate(getChequeDate()))
        errors = chequeDateValidate(errors, locale);
    return errors;
}
Also used : Locale(java.util.Locale) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) ActionMessage(org.apache.struts.action.ActionMessage) ResourceBundle(java.util.ResourceBundle) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) ActionErrors(org.apache.struts.action.ActionErrors)

Aggregations

DoubleConversionResult (org.mifos.framework.util.helpers.DoubleConversionResult)43 ConversionError (org.mifos.framework.util.helpers.ConversionError)20 Locale (java.util.Locale)9 ArrayList (java.util.ArrayList)7 ActionErrors (org.apache.struts.action.ActionErrors)7 ActionMessage (org.apache.struts.action.ActionMessage)7 Test (org.junit.Test)7 ResourceBundle (java.util.ResourceBundle)6 InvalidDateException (org.mifos.application.admin.servicefacade.InvalidDateException)5 MifosCurrency (org.mifos.application.master.business.MifosCurrency)4 LocalizationConverter (org.mifos.framework.util.LocalizationConverter)4 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 List (java.util.List)1 Map (java.util.Map)1 FeeDto (org.mifos.accounts.fees.business.FeeDto)1 LoanAccountDetailsDto (org.mifos.dto.domain.LoanAccountDetailsDto)1 Money (org.mifos.framework.util.helpers.Money)1