001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.fp.document.validation.impl;
017    
018    import static org.kuali.kfs.fp.document.validation.impl.CreditCardReceiptDocumentRuleConstants.CREDIT_CARD_RECEIPT_PREFIX;
019    import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
020    
021    import org.kuali.kfs.fp.businessobject.CreditCardDetail;
022    import org.kuali.kfs.fp.document.CreditCardReceiptDocument;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.KFSKeyConstants.CashReceipt;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.service.DataDictionaryService;
028    import org.kuali.rice.kns.service.DictionaryValidationService;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.KualiDecimal;
031    import org.kuali.rice.kns.util.MessageMap;
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(CreditCardDetail creditCardReceipt) {
046            MessageMap errorMap = GlobalVariables.getMessageMap();
047            int originalErrorCount = errorMap.getErrorCount();
048    
049            // call the DD validation which checks basic data integrity
050            SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(creditCardReceipt);
051            boolean isValid = (errorMap.getErrorCount() == originalErrorCount);
052    
053            // check that dollar amount is not zero before continuing
054            if (isValid) {
055                isValid = !creditCardReceipt.getCreditCardAdvanceDepositAmount().isZero();
056                if (!isValid) {
057                    String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, KFSPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT);
058                    errorMap.putError(KFSPropertyConstants.CREDIT_CARD_ADVANCE_DEPOSIT_AMOUNT, KFSKeyConstants.ERROR_ZERO_AMOUNT, label);
059                }
060            }
061    
062            if (isValid) {
063                isValid = SpringContext.getBean(DictionaryValidationService.class).validateReferenceExists(creditCardReceipt, KFSPropertyConstants.CREDIT_CARD_TYPE);
064                if (!isValid) {
065                    String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE);
066                    errorMap.putError(KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_TYPE_CODE, KFSKeyConstants.ERROR_EXISTENCE, label);
067                }
068            }
069            if (isValid) {
070                isValid = SpringContext.getBean(DictionaryValidationService.class).validateReferenceExists(creditCardReceipt, KFSPropertyConstants.CREDIT_CARD_VENDOR);
071                if (!isValid) {
072                    String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(CreditCardDetail.class, KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER);
073                    errorMap.putError(KFSPropertyConstants.FINANCIAL_DOCUMENT_CREDIT_CARD_VENDOR_NUMBER, KFSKeyConstants.ERROR_EXISTENCE, label);
074                }
075            }
076    
077            return isValid;
078        }
079    
080    
081        /**
082         * Checks whether the CashReceiptDocument's cash totals are invalid, generating global errors if so.
083         * 
084         * @param cashReceiptDocument submitted cash receipt document
085         * @return true if any of the cash totals on cash credit card receipt document are invalid
086         */
087        public static boolean areCashTotalsInvalid(CreditCardReceiptDocument ccrDocument) {
088            String documentEntryName = ccrDocument.getDocumentHeader().getWorkflowDocument().getDocumentType();
089    
090            boolean isInvalid = isTotalInvalid(ccrDocument, ccrDocument.getTotalDollarAmount(), documentEntryName, KFSPropertyConstants.CREDIT_CARD_RECEIPTS_TOTAL);
091    
092            return isInvalid;
093        }
094    
095        /**
096         * Returns true if total is invalid and puts an error message in the error map for that property if the amount is negative
097         * 
098         * @param cashReceiptDocument
099         * @param totalAmount
100         * @param documentEntryName
101         * @param propertyName
102         * @return true if the totalAmount is an invalid value
103         */
104        private static boolean isTotalInvalid(CreditCardReceiptDocument ccrDocument, KualiDecimal totalAmount, String documentEntryName, String propertyName) {
105            boolean isInvalid = false;
106            String errorProperty = CREDIT_CARD_RECEIPT_PREFIX + propertyName;
107    
108            // treating null totalAmount as if it were a zero
109            DataDictionaryService dds = SpringContext.getBean(DataDictionaryService.class);
110            String errorLabel = dds.getAttributeLabel(documentEntryName, propertyName);
111            if ((totalAmount == null) || totalAmount.isZero()) {
112                GlobalVariables.getMessageMap().putError(errorProperty, CashReceipt.ERROR_ZERO_TOTAL, errorLabel);
113    
114                isInvalid = true;
115            }
116            else {
117                int precount = GlobalVariables.getMessageMap().size();
118    
119                DictionaryValidationService dvs = SpringContext.getBean(DictionaryValidationService.class);
120                dvs.validateDocumentAttribute(ccrDocument, propertyName, DOCUMENT_ERROR_PREFIX);
121    
122                // replace generic error message, if any, with something more readable
123                GlobalVariables.getMessageMap().replaceError(errorProperty, KFSKeyConstants.ERROR_MAX_LENGTH, CashReceipt.ERROR_EXCESSIVE_TOTAL, errorLabel);
124    
125                int postcount = GlobalVariables.getMessageMap().size();
126                isInvalid = (postcount > precount);
127            }
128    
129            return isInvalid;
130        }
131    }