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 java.util.Iterator;
019    import java.util.Map;
020    
021    import org.kuali.kfs.fp.document.BudgetAdjustmentDocument;
022    import org.kuali.kfs.sys.KFSConstants;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.document.validation.GenericValidation;
025    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
026    import org.kuali.rice.kns.util.GlobalVariables;
027    import org.kuali.rice.kns.util.KualiDecimal;
028    import org.kuali.rice.kns.util.KualiInteger;
029    import org.kuali.rice.kns.util.MessageMap;
030    
031    /**
032     * A validation which checks if a Budget Adjustment document is balanced before heading to routing
033     */
034    public class BudgetAdjustmentDocumentBalancedValidation extends GenericValidation {
035        public BudgetAdjustmentDocument accountingDocumentForValidation;
036    
037        /**
038         * Validates that the budget adjustment document is balanced, based on whether the source base amount equals the target base amount
039         * and that the income stream balance map has no non-zero values.
040         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
041         */
042        public boolean validate(AttributedDocumentEvent event) {
043            MessageMap errors = GlobalVariables.getMessageMap();
044    
045            boolean balanced = true;
046    
047            // check base amounts are equal
048            //KFSMI-3036
049            KualiInteger sourceBaseBudgetTotal = getAccountingDocumentForValidation().getSourceBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getSourceBaseBudgetExpenseTotal());
050            KualiInteger targetBaseBudgetTotal = getAccountingDocumentForValidation().getTargetBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getTargetBaseBudgetExpenseTotal());
051            if (sourceBaseBudgetTotal.compareTo(targetBaseBudgetTotal) != 0) {
052                GlobalVariables.getMessageMap().putError(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_DOCUMENT_BA_BASE_AMOUNTS_BALANCED);
053                balanced = false;
054            }
055    
056            // check current amounts balance, income stream balance Map should add to 0
057            Map incomeStreamMap = getAccountingDocumentForValidation().buildIncomeStreamBalanceMapForDocumentBalance();
058            KualiDecimal totalCurrentAmount = new KualiDecimal(0);
059            for (Iterator iter = incomeStreamMap.values().iterator(); iter.hasNext();) {
060                KualiDecimal streamAmount = (KualiDecimal) iter.next();
061                totalCurrentAmount = totalCurrentAmount.add(streamAmount);
062            }
063    
064            if (totalCurrentAmount.isNonZero()) {
065                GlobalVariables.getMessageMap().putError(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_DOCUMENT_BA_CURRENT_AMOUNTS_BALANCED);
066                balanced = false;
067            }
068    
069            return balanced;
070        }
071    
072        /**
073         * Gets the accountingDocumentForValidation attribute. 
074         * @return Returns the accountingDocumentForValidation.
075         */
076        public BudgetAdjustmentDocument getAccountingDocumentForValidation() {
077            return accountingDocumentForValidation;
078        }
079    
080        /**
081         * Sets the accountingDocumentForValidation attribute value.
082         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
083         */
084        public void setAccountingDocumentForValidation(BudgetAdjustmentDocument accountingDocumentForValidation) {
085            this.accountingDocumentForValidation = accountingDocumentForValidation;
086        }
087    }