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: import static org.kuali.module.financial.rules.CreditCardReceiptDocumentRuleConstants.CREDIT_CARD_RECEIPT_PREFIX;
020:
021: import org.kuali.core.service.DataDictionaryService;
022: import org.kuali.core.service.DictionaryValidationService;
023: import org.kuali.core.util.ErrorMap;
024: import org.kuali.core.util.GlobalVariables;
025: import org.kuali.core.util.KualiDecimal;
026: import org.kuali.kfs.KFSKeyConstants;
027: import org.kuali.kfs.KFSPropertyConstants;
028: import org.kuali.kfs.KFSKeyConstants.CashReceipt;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.financial.bo.CreditCardDetail;
031: import org.kuali.module.financial.document.CreditCardReceiptDocument;
032:
033: /**
034: * Common Credit Card Receipt Document rule utilities.
035: */
036: public class CreditCardReceiptDocumentRuleUtil {
037: /**
038: * This method method will invoke the data dictionary validation for a CreditCardDetail bo instance, in addition to checking
039: * existence of the CreditCardType and CreditCardVendor attributes that hang off of it. This method assumes that the document
040: * hierarchy for the error map path is managed outside of this call.
041: *
042: * @param creditCardReceipt credit card detail
043: * @return true if credit card detail amount is non zero and credit card vendor and type references exist
044: */
045: public static boolean validateCreditCardReceipt(
046: CreditCardDetail creditCardReceipt) {
047: ErrorMap errorMap = GlobalVariables.getErrorMap();
048: int originalErrorCount = errorMap.getErrorCount();
049:
050: // call the DD validation which checks basic data integrity
051: SpringContext.getBean(DictionaryValidationService.class)
052: .validateBusinessObject(creditCardReceipt);
053: boolean isValid = (errorMap.getErrorCount() == originalErrorCount);
054:
055: // check that dollar amount is not zero before continuing
056: if (isValid) {
057: isValid = !creditCardReceipt
058: .getCreditCardAdvanceDepositAmount().isZero();
059: if (!isValid) {
060: String label = SpringContext
061: .getBean(DataDictionaryService.class)
062: .getAttributeLabel(
063: CreditCardDetail.class,
064: KFSPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT);
065: errorMap
066: .putError(
067: KFSPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT,
068: KFSKeyConstants.ERROR_ZERO_AMOUNT,
069: label);
070: }
071: }
072:
073: if (isValid) {
074: isValid = SpringContext.getBean(
075: DictionaryValidationService.class)
076: .validateReferenceExists(creditCardReceipt,
077: KFSPropertyConstants.CREDIT_CARD_TYPE);
078: if (!isValid) {
079: String label = SpringContext
080: .getBean(DataDictionaryService.class)
081: .getAttributeLabel(
082: CreditCardDetail.class,
083: KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE);
084: errorMap
085: .putError(
086: KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE,
087: KFSKeyConstants.ERROR_EXISTENCE, label);
088: }
089: }
090: if (isValid) {
091: isValid = SpringContext.getBean(
092: DictionaryValidationService.class)
093: .validateReferenceExists(creditCardReceipt,
094: KFSPropertyConstants.CREDIT_CARD_VENDOR);
095: if (!isValid) {
096: String label = SpringContext
097: .getBean(DataDictionaryService.class)
098: .getAttributeLabel(
099: CreditCardDetail.class,
100: KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER);
101: errorMap
102: .putError(
103: KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER,
104: KFSKeyConstants.ERROR_EXISTENCE, label);
105: }
106: }
107:
108: return isValid;
109: }
110:
111: /**
112: * Checks whether the CashReceiptDocument's cash totals are invalid, generating global errors if so.
113: *
114: * @param cashReceiptDocument submitted cash receipt document
115: * @return true if any of the cash totals on cash credit card receipt document are invalid
116: */
117: public static boolean areCashTotalsInvalid(
118: CreditCardReceiptDocument ccrDocument) {
119: String documentEntryName = ccrDocument.getDocumentHeader()
120: .getWorkflowDocument().getDocumentType();
121:
122: boolean isInvalid = isTotalInvalid(ccrDocument, ccrDocument
123: .getTotalDollarAmount(), documentEntryName,
124: KFSPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL);
125:
126: return isInvalid;
127: }
128:
129: /**
130: * Returns true if total is invalid and puts an error message in the error map for that property if the amount is negative
131: *
132: * @param cashReceiptDocument
133: * @param totalAmount
134: * @param documentEntryName
135: * @param propertyName
136: * @return true if the totalAmount is an invalid value
137: */
138: private static boolean isTotalInvalid(
139: CreditCardReceiptDocument ccrDocument,
140: KualiDecimal totalAmount, String documentEntryName,
141: String propertyName) {
142: boolean isInvalid = false;
143: String errorProperty = CREDIT_CARD_RECEIPT_PREFIX
144: + propertyName;
145:
146: // treating null totalAmount as if it were a zero
147: DataDictionaryService dds = SpringContext
148: .getBean(DataDictionaryService.class);
149: String errorLabel = dds.getAttributeLabel(documentEntryName,
150: propertyName);
151: if ((totalAmount == null) || totalAmount.isZero()) {
152: GlobalVariables.getErrorMap().putError(errorProperty,
153: CashReceipt.ERROR_ZERO_TOTAL, errorLabel);
154:
155: isInvalid = true;
156: } else {
157: int precount = GlobalVariables.getErrorMap().size();
158:
159: DictionaryValidationService dvs = SpringContext
160: .getBean(DictionaryValidationService.class);
161: dvs.validateDocumentAttribute(ccrDocument, propertyName,
162: DOCUMENT_ERROR_PREFIX);
163:
164: // replace generic error message, if any, with something more readable
165: GlobalVariables.getErrorMap().replaceError(errorProperty,
166: KFSKeyConstants.ERROR_MAX_LENGTH,
167: CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
168:
169: int postcount = GlobalVariables.getErrorMap().size();
170: isInvalid = (postcount > precount);
171: }
172:
173: return isInvalid;
174: }
175: }
|