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.List;
019
020import org.kuali.ole.fp.businessobject.CapitalAccountingLines;
021import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
022import org.kuali.ole.fp.document.CapitalAccountingLinesDocumentBase;
023import org.kuali.ole.fp.document.CapitalAssetEditable;
024import org.kuali.ole.sys.OLEConstants;
025import org.kuali.ole.sys.OLEKeyConstants;
026import org.kuali.ole.sys.document.AccountingDocument;
027import org.kuali.ole.sys.document.validation.GenericValidation;
028import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
029import org.kuali.rice.core.api.util.type.KualiDecimal;
030import org.kuali.rice.krad.util.GlobalVariables;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033/**
034 * validate the capital accounting lines associated with the accounting document for validation
035 */
036public class CapitalAccountingLinesValidations extends GenericValidation {
037    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CapitalAccountingLinesValidations.class);
038
039    private AccountingDocument accountingDocumentForValidation;
040
041    /**
042     * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
043     */
044    public boolean validate(AttributedDocumentEvent event) {
045        return this.validateCapitalAccountingLines(accountingDocumentForValidation);
046    }
047
048    // determine whether the given document's all capital accounting lines are processed.
049    protected boolean validateCapitalAccountingLines(AccountingDocument accountingDocumentForValidation) {
050        LOG.debug("validateCapitalAccountingLines - start");
051
052        boolean isValid = true;
053        
054        if (accountingDocumentForValidation instanceof CapitalAssetEditable == false) {
055            return true;
056        }
057
058        CapitalAccountingLinesDocumentBase capitalAccountingLinesDocumentBase = (CapitalAccountingLinesDocumentBase) accountingDocumentForValidation;
059        List <CapitalAccountingLines> capitalAccountingLines = capitalAccountingLinesDocumentBase.getCapitalAccountingLines();
060        
061        CapitalAssetEditable capitalAssetEditable = (CapitalAssetEditable) accountingDocumentForValidation;
062
063        List<CapitalAssetInformation> capitalAssets = capitalAssetEditable.getCapitalAssetInformation();
064
065        isValid &= allCapitalAccountingLinesProcessed(capitalAccountingLines, capitalAssets);
066        isValid &= capitalAssetExistsForCapitalAccountingLinesProcessed(capitalAccountingLines, capitalAssets);
067        isValid &= totalAmountMatchForCapitalAccountingLinesAndCapitalAssets(capitalAccountingLines, capitalAssets);
068        
069        return isValid;
070    }
071
072    protected boolean allCapitalAccountingLinesProcessed(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
073        LOG.debug("allCapitalAccountingLinesProcessed - start");
074        
075        boolean processed = true;
076        
077        for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
078            if (!capitalAccountingLine.isSelectLine()) {
079                processed = false;
080                GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_NOT_PROCESSED);
081                
082                break;
083            }
084        }
085        
086        return processed;
087    }
088
089    protected boolean capitalAssetExistsForCapitalAccountingLinesProcessed(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
090        LOG.debug("capitalAssetExistsForCapitalAccountingLinesProcessed - start");
091        
092        boolean exists = true;
093        
094        for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
095            if (!capitalAssetExist(capitalAccountingLine, capitalAssets)) {
096                exists = false;
097                GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_HAS_NO_CAPITAL_ASSET, capitalAccountingLine.getSequenceNumber().toString(), capitalAccountingLine.getLineType(), capitalAccountingLine.getChartOfAccountsCode(), capitalAccountingLine.getAccountNumber(), capitalAccountingLine.getFinancialObjectCode());
098                
099                break;
100            }
101        }
102        
103        return exists;
104    }
105    
106    /**
107     * 
108     * @param capitalAccountingLine
109     * @param capitalAssetInformation
110     * @return true if accounting line has a capital asset information
111     */
112    protected boolean capitalAssetExist(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetInformation) {
113        boolean exists = false;
114        
115        if (ObjectUtils.isNull(capitalAssetInformation) && capitalAssetInformation.size() <= 0) {
116            return exists;
117        }
118        
119        for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
120          //  if (capitalAsset.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
121         //           capitalAsset.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) && 
122         //           capitalAsset.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) && 
123         //           capitalAsset.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) && 
124         //           capitalAsset.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
125                return true;
126          //  }
127        }
128        
129        return exists;
130    }
131    
132    protected boolean totalAmountMatchForCapitalAccountingLinesAndCapitalAssets(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
133        boolean totalAmountMatched = true;
134        
135        KualiDecimal capitalAccountingLinesTotals = KualiDecimal.ZERO;
136        KualiDecimal capitalAAssetTotals = KualiDecimal.ZERO;
137        
138        for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
139            capitalAccountingLinesTotals = capitalAccountingLinesTotals.add(capitalAccountingLine.getAmount());
140        }
141    
142        for (CapitalAssetInformation capitalAsset : capitalAssets) {
143            capitalAAssetTotals = capitalAAssetTotals.add(capitalAsset.getCapitalAssetLineAmount());
144        }
145        
146        if (capitalAccountingLinesTotals.isGreaterThan(capitalAAssetTotals)) {
147            //not all the accounting lines amounts have been distributed to capital assets
148            GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINES_NOT_ALL_TOTALS_DISTRIBUTED_TO_CAPITAL_ASSETS);
149            return false;
150        }
151        
152        if (capitalAccountingLinesTotals.isLessEqual(capitalAAssetTotals)) {
153            //not all the accounting lines amounts have been distributed to capital assets
154            GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_HAS_NO_CAPITAL_ASSET);
155            return false;
156        }
157        
158        return totalAmountMatched;
159    }
160    
161    
162    /**
163     * Sets the accountingDocumentForValidation attribute value.
164     * 
165     * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
166     */
167    public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
168        this.accountingDocumentForValidation = accountingDocumentForValidation;
169    }
170}