View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import static org.kuali.ole.sys.OLEConstants.ACCOUNTING_LINE_ERRORS;
19  import static org.kuali.ole.sys.OLEConstants.GL_CREDIT_CODE;
20  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_BALANCE;
21  
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
24  import org.kuali.ole.sys.document.AccountingDocument;
25  import org.kuali.ole.sys.document.validation.GenericValidation;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
28  import org.kuali.rice.core.api.util.type.KualiDecimal;
29  import org.kuali.rice.krad.exception.ValidationException;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  /**
33   * Validation that checks that the general ledger pending entries associated with an auxiliary voucher
34   * document balance.
35   */
36  public class AuxiliaryVoucherGeneralLedgerPendingEntriesBalanceValdiation extends GenericValidation {
37      private AccountingDocument accountingDocumentForValidation;
38      private GeneralLedgerPendingEntryService generalLedgerPendingEntryService;
39  
40      /**
41       * Returns true if the explicit, non-DI credit and debit GLPEs derived from the document's accountingLines are in balance
42       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
43       */
44      public boolean validate(AttributedDocumentEvent event) {
45  //      generate GLPEs specifically here so that we can compare debits to credits
46          if (!getGeneralLedgerPendingEntryService().generateGeneralLedgerPendingEntries(getAccountingDocumentForValidation())) {
47              throw new ValidationException("general ledger GLPE generation failed");
48          }
49  
50          // now loop through all of the GLPEs and calculate buckets for debits and credits
51          KualiDecimal creditAmount = KualiDecimal.ZERO;
52          KualiDecimal debitAmount = KualiDecimal.ZERO;
53  
54          for (GeneralLedgerPendingEntry glpe : getAccountingDocumentForValidation().getGeneralLedgerPendingEntries()) {
55              // make sure we are looking at only the explicit entries that aren't DI types
56              if (!glpe.isTransactionEntryOffsetIndicator() && !glpe.getFinancialDocumentTypeCode().equals(OLEConstants.FinancialDocumentTypeCodes.DISTRIBUTION_OF_INCOME_AND_EXPENSE)) {
57                  if (GL_CREDIT_CODE.equals(glpe.getTransactionDebitCreditCode())) {
58                      creditAmount = creditAmount.add(glpe.getTransactionLedgerEntryAmount());
59                  }
60                  else { // DEBIT
61                      debitAmount = debitAmount.add(glpe.getTransactionLedgerEntryAmount());
62                  }
63              }
64          }
65  
66          boolean balanced = debitAmount.equals(creditAmount);
67          if (!balanced) {
68              String errorParams[] = { creditAmount.toString(), debitAmount.toString() };
69              GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE, errorParams);
70          }
71          return balanced;
72      }
73  
74      /**
75       * Gets the accountingDocumentForValidation attribute. 
76       * @return Returns the accountingDocumentForValidation.
77       */
78      public AccountingDocument getAccountingDocumentForValidation() {
79          return accountingDocumentForValidation;
80      }
81  
82      /**
83       * Sets the accountingDocumentForValidation attribute value.
84       * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
85       */
86      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
87          this.accountingDocumentForValidation = accountingDocumentForValidation;
88      }
89  
90      /**
91       * Gets the generalLedgerPendingEntryService attribute. 
92       * @return Returns the generalLedgerPendingEntryService.
93       */
94      public GeneralLedgerPendingEntryService getGeneralLedgerPendingEntryService() {
95          return generalLedgerPendingEntryService;
96      }
97  
98      /**
99       * Sets the generalLedgerPendingEntryService attribute value.
100      * @param generalLedgerPendingEntryService The generalLedgerPendingEntryService to set.
101      */
102     public void setGeneralLedgerPendingEntryService(GeneralLedgerPendingEntryService generalLedgerPendingEntryService) {
103         this.generalLedgerPendingEntryService = generalLedgerPendingEntryService;
104     }
105 }