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