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.sys.document.validation.impl; 017 018 import java.util.Iterator; 019 import java.util.List; 020 021 import org.kuali.kfs.fp.document.validation.impl.CapitalAssetInformationValidation; 022 import org.kuali.kfs.sys.KFSConstants; 023 import org.kuali.kfs.sys.KFSKeyConstants; 024 import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry; 025 import org.kuali.kfs.sys.context.SpringContext; 026 import org.kuali.kfs.sys.document.AccountingDocument; 027 import org.kuali.kfs.sys.document.validation.GenericValidation; 028 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent; 029 import org.kuali.kfs.sys.service.GeneralLedgerPendingEntryService; 030 import org.kuali.rice.kns.exception.ValidationException; 031 import org.kuali.rice.kns.util.GlobalVariables; 032 import org.kuali.rice.kns.util.KualiDecimal; 033 034 /** 035 * A validation that checks that the credits and debits of GLPEs generated by a document are balanced. 036 */ 037 public class DebitsAndCreditsBalanceValidation extends GenericValidation { 038 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DebitsAndCreditsBalanceValidation.class); 039 040 private AccountingDocument accountingDocumentForValidation; 041 042 /** 043 * Generates the GLPEs for a given accounting document and checks that the debits total equals 044 * the credits total. 045 * <strong>Expects the document to be sent in as a property.</strong> 046 * @see org.kuali.kfs.sys.document.validation.GenericValidation#validate(java.lang.Object[]) 047 */ 048 public boolean validate(AttributedDocumentEvent event) { 049 LOG.debug("Validation started"); 050 051 // generate GLPEs specifically here so that we can compare debits to credits 052 if (!SpringContext.getBean(GeneralLedgerPendingEntryService.class).generateGeneralLedgerPendingEntries(accountingDocumentForValidation)) { 053 throw new ValidationException("general ledger GLPE generation failed"); 054 } 055 056 // now loop through all of the GLPEs and calculate buckets for debits and credits 057 KualiDecimal creditAmount = KualiDecimal.ZERO; 058 KualiDecimal debitAmount = KualiDecimal.ZERO; 059 060 List<GeneralLedgerPendingEntry> pendingEntries = accountingDocumentForValidation.getGeneralLedgerPendingEntries(); 061 for(GeneralLedgerPendingEntry entry : pendingEntries) { 062 if(entry.isTransactionEntryOffsetIndicator()) { 063 continue; 064 } 065 066 if (KFSConstants.GL_CREDIT_CODE.equals(entry.getTransactionDebitCreditCode())) { 067 creditAmount = creditAmount.add(entry.getTransactionLedgerEntryAmount()); 068 } 069 else { 070 debitAmount = debitAmount.add(entry.getTransactionLedgerEntryAmount()); 071 } 072 } 073 074 boolean isValid = debitAmount.compareTo(creditAmount) == 0; 075 076 if (!isValid) { 077 GlobalVariables.getMessageMap().putError(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_DOCUMENT_BALANCE); 078 } 079 080 return isValid; 081 } 082 083 /** 084 * Gets the accountingDocumentForValidation attribute. 085 * @return Returns the accountingDocumentForValidation. 086 */ 087 public AccountingDocument getAccountingDocumentForValidation() { 088 return accountingDocumentForValidation; 089 } 090 091 /** 092 * Sets the accountingDocumentForValidation attribute value. 093 * @param accountingDocumentForValidation The accountingDocumentForValidation to set. 094 */ 095 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) { 096 this.accountingDocumentForValidation = accountingDocumentForValidation; 097 } 098 }