001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.financial.rules;
017:
018: import static org.kuali.kfs.rules.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
019:
020: import org.kuali.core.service.DataDictionaryService;
021: import org.kuali.core.service.DictionaryValidationService;
022: import org.kuali.core.util.GlobalVariables;
023: import org.kuali.core.util.KualiDecimal;
024: import org.kuali.kfs.KFSKeyConstants;
025: import org.kuali.kfs.KFSPropertyConstants;
026: import org.kuali.kfs.KFSKeyConstants.CashReceipt;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.module.financial.document.CashReceiptDocument;
029: import org.kuali.module.financial.document.CashReceiptFamilyBase;
030:
031: /**
032: * Utility class used for Cash Receipt Document
033: *
034: */
035: public class CashReceiptDocumentRuleUtil {
036:
037: /**
038: * Checks whether the CashReceiptDocument's cash totals are invalid, generating global errors if so.
039: *
040: * @param cashReceiptDocument submitted cash receipt document
041: * @return true if CashReceiptDocument's cash totals are valid
042: */
043: public static boolean areCashTotalsInvalid(
044: CashReceiptDocument cashReceiptDocument) {
045: String documentEntryName = cashReceiptDocument
046: .getDocumentHeader().getWorkflowDocument()
047: .getDocumentType();
048:
049: boolean isInvalid = isTotalInvalid(cashReceiptDocument,
050: cashReceiptDocument.getTotalCheckAmount(),
051: documentEntryName,
052: KFSPropertyConstants.TOTAL_CHECK_AMOUNT);
053: isInvalid |= isTotalInvalid(cashReceiptDocument,
054: cashReceiptDocument.getTotalCashAmount(),
055: documentEntryName,
056: KFSPropertyConstants.TOTAL_CASH_AMOUNT);
057: isInvalid |= isTotalInvalid(cashReceiptDocument,
058: cashReceiptDocument.getTotalCoinAmount(),
059: documentEntryName,
060: KFSPropertyConstants.TOTAL_COIN_AMOUNT);
061:
062: isInvalid |= isTotalInvalid(cashReceiptDocument,
063: cashReceiptDocument.getTotalDollarAmount(),
064: documentEntryName,
065: KFSPropertyConstants.SUM_TOTAL_AMOUNT);
066:
067: return isInvalid;
068: }
069:
070: /**
071: * Puts an error message in the error map for that property if the amount is negative.
072: *
073: * @param cashReceiptDocument submitted cash receipt document
074: * @param totalAmount total amount (cash total, check total, etc)
075: * @param documentEntryName document type
076: * @param propertyName property type (i.e totalCashAmount, totalCheckAmount, etc)
077: * @return true if the totalAmount is an invalid value
078: */
079: private static boolean isTotalInvalid(
080: CashReceiptFamilyBase cashReceiptDocument,
081: KualiDecimal totalAmount, String documentEntryName,
082: String propertyName) {
083: boolean isInvalid = false;
084: String errorProperty = DOCUMENT_ERROR_PREFIX + propertyName;
085:
086: if (totalAmount != null) {
087: DataDictionaryService dds = SpringContext
088: .getBean(DataDictionaryService.class);
089: String errorLabel = dds.getAttributeLabel(
090: documentEntryName, propertyName);
091:
092: if (totalAmount.isNegative()) {
093: GlobalVariables.getErrorMap().putError(errorProperty,
094: CashReceipt.ERROR_NEGATIVE_TOTAL, errorLabel);
095:
096: isInvalid = true;
097: } else {
098: int precount = GlobalVariables.getErrorMap().size();
099:
100: DictionaryValidationService dvs = SpringContext
101: .getBean(DictionaryValidationService.class);
102: dvs.validateDocumentAttribute(cashReceiptDocument,
103: propertyName, DOCUMENT_ERROR_PREFIX);
104:
105: // replace generic error message, if any, with something more readable
106: GlobalVariables.getErrorMap().replaceError(
107: errorProperty,
108: KFSKeyConstants.ERROR_MAX_LENGTH,
109: CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
110:
111: int postcount = GlobalVariables.getErrorMap().size();
112: isInvalid = (postcount > precount);
113: }
114: }
115:
116: return isInvalid;
117: }
118: }
|