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 static org.kuali.kfs.sys.KFSConstants.ACCOUNTING_LINE_ERRORS;
019 import static org.kuali.kfs.sys.KFSConstants.GL_CREDIT_CODE;
020 import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_BALANCE;
021
022 import org.kuali.kfs.fp.document.DistributionOfIncomeAndExpenseDocument;
023 import org.kuali.kfs.sys.KFSConstants;
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.service.DataDictionaryService;
032 import org.kuali.rice.kns.util.GlobalVariables;
033 import org.kuali.rice.kns.util.KualiDecimal;
034
035 /**
036 * Validation that checks that the general ledger pending entries associated with an auxiliary voucher
037 * document balance.
038 */
039 public class AuxiliaryVoucherGeneralLedgerPendingEntriesBalanceValdiation extends GenericValidation {
040 private AccountingDocument accountingDocumentForValidation;
041 private GeneralLedgerPendingEntryService generalLedgerPendingEntryService;
042
043 /**
044 * Returns true if the explicit, non-DI credit and debit GLPEs derived from the document's accountingLines are in balance
045 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
046 */
047 public boolean validate(AttributedDocumentEvent event) {
048 // generate GLPEs specifically here so that we can compare debits to credits
049 if (!getGeneralLedgerPendingEntryService().generateGeneralLedgerPendingEntries(getAccountingDocumentForValidation())) {
050 throw new ValidationException("general ledger GLPE generation failed");
051 }
052
053 // now loop through all of the GLPEs and calculate buckets for debits and credits
054 KualiDecimal creditAmount = KualiDecimal.ZERO;
055 KualiDecimal debitAmount = KualiDecimal.ZERO;
056
057 for (GeneralLedgerPendingEntry glpe : getAccountingDocumentForValidation().getGeneralLedgerPendingEntries()) {
058 // make sure we are looking at only the explicit entries that aren't DI types
059 if (!glpe.isTransactionEntryOffsetIndicator() && !glpe.getFinancialDocumentTypeCode().equals(KFSConstants.FinancialDocumentTypeCodes.DISTRIBUTION_OF_INCOME_AND_EXPENSE)) {
060 if (GL_CREDIT_CODE.equals(glpe.getTransactionDebitCreditCode())) {
061 creditAmount = creditAmount.add(glpe.getTransactionLedgerEntryAmount());
062 }
063 else { // DEBIT
064 debitAmount = debitAmount.add(glpe.getTransactionLedgerEntryAmount());
065 }
066 }
067 }
068
069 boolean balanced = debitAmount.equals(creditAmount);
070 if (!balanced) {
071 String errorParams[] = { creditAmount.toString(), debitAmount.toString() };
072 GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE, errorParams);
073 }
074 return balanced;
075 }
076
077 /**
078 * Gets the accountingDocumentForValidation attribute.
079 * @return Returns the accountingDocumentForValidation.
080 */
081 public AccountingDocument getAccountingDocumentForValidation() {
082 return accountingDocumentForValidation;
083 }
084
085 /**
086 * Sets the accountingDocumentForValidation attribute value.
087 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
088 */
089 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
090 this.accountingDocumentForValidation = accountingDocumentForValidation;
091 }
092
093 /**
094 * Gets the generalLedgerPendingEntryService attribute.
095 * @return Returns the generalLedgerPendingEntryService.
096 */
097 public GeneralLedgerPendingEntryService getGeneralLedgerPendingEntryService() {
098 return generalLedgerPendingEntryService;
099 }
100
101 /**
102 * Sets the generalLedgerPendingEntryService attribute value.
103 * @param generalLedgerPendingEntryService The generalLedgerPendingEntryService to set.
104 */
105 public void setGeneralLedgerPendingEntryService(GeneralLedgerPendingEntryService generalLedgerPendingEntryService) {
106 this.generalLedgerPendingEntryService = generalLedgerPendingEntryService;
107 }
108 }