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 java.util.Iterator;
019import java.util.Map;
020
021import org.kuali.ole.fp.document.BudgetAdjustmentDocument;
022import org.kuali.ole.sys.OLEConstants;
023import org.kuali.ole.sys.OLEKeyConstants;
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.core.api.util.type.KualiInteger;
028import org.kuali.rice.krad.util.GlobalVariables;
029import org.kuali.rice.krad.util.MessageMap;
030
031/**
032 * A validation which checks if a Budget Adjustment document is balanced before heading to routing
033 */
034public 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.ole.sys.document.validation.Validation#validate(org.kuali.ole.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        KualiDecimal sourceBaseBudgetTotal = getAccountingDocumentForValidation().getSourceBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getSourceBaseBudgetExpenseTotal());
050        KualiDecimal targetBaseBudgetTotal = getAccountingDocumentForValidation().getTargetBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getTargetBaseBudgetExpenseTotal());
051        if (sourceBaseBudgetTotal.compareTo(targetBaseBudgetTotal) != 0) {
052            GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.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(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.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}