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.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
019    
020    import org.kuali.kfs.fp.document.CashReceiptFamilyBase;
021    import org.kuali.kfs.sys.KFSKeyConstants;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.document.validation.GenericValidation;
024    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    
028    /**
029     * Validation for the Cash Receipt family of documents that checks the total amount of the document.
030     */
031    public class CashReceiptFamilyDocumentTotalValidation extends GenericValidation {
032        private CashReceiptFamilyBase cashReceiptFamilyDocumentForValidation;
033    
034        /**
035         * For Cash Receipt documents, the document is balanced if the sum total of checks and cash and coin equals the sum total of the
036         * accounting lines. In addition, the sum total of checks and cash and coin must be greater than zero.
037         * 
038         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
039         */
040        public boolean validate(AttributedDocumentEvent event) {
041            // make sure that cash reconciliation total is greater than zero
042            boolean isValid = getCashReceiptFamilyDocumentForValidation().getTotalDollarAmount().compareTo(KualiDecimal.ZERO) > 0;
043            if (!isValid) {
044                GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + KFSPropertyConstants.SUM_TOTAL_AMOUNT, KFSKeyConstants.CashReceipt.ERROR_DOCUMENT_CASH_RECEIPT_NO_CASH_RECONCILIATION_TOTAL);
045            }
046    
047            if (isValid) {
048                // make sure the document is in balance
049                isValid = getCashReceiptFamilyDocumentForValidation().getSourceTotal().compareTo(getCashReceiptFamilyDocumentForValidation().getTotalDollarAmount()) == 0;
050    
051                if (!isValid) {
052                    GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + KFSPropertyConstants.SUM_TOTAL_AMOUNT, KFSKeyConstants.CashReceipt.ERROR_DOCUMENT_CASH_RECEIPT_BALANCE);
053                }
054            }
055    
056            return isValid;
057        }
058    
059        /**
060         * Gets the cashReceiptFamilyDocumentForValidation attribute. 
061         * @return Returns the cashReceiptFamilyDocumentForValidation.
062         */
063        public CashReceiptFamilyBase getCashReceiptFamilyDocumentForValidation() {
064            return cashReceiptFamilyDocumentForValidation;
065        }
066    
067        /**
068         * Sets the cashReceiptFamilyDocumentForValidation attribute value.
069         * @param cashReceiptFamilyDocumentForValidation The cashReceiptFamilyDocumentForValidation to set.
070         */
071        public void setCashReceiptFamilyDocumentForValidation(CashReceiptFamilyBase cashReceiptFamilyDocumentForValidation) {
072            this.cashReceiptFamilyDocumentForValidation = cashReceiptFamilyDocumentForValidation;
073        }
074    }