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.sys.document.validation.impl;
17  
18  import java.util.List;
19  
20  import org.kuali.ole.sys.OLEConstants;
21  import org.kuali.ole.sys.OLEKeyConstants;
22  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
23  import org.kuali.ole.sys.context.SpringContext;
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   * A validation that checks that the credits and debits of GLPEs generated by a document are balanced.
34   */
35  public class DebitsAndCreditsBalanceValidation extends GenericValidation {
36      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DebitsAndCreditsBalanceValidation.class);
37      
38      private AccountingDocument accountingDocumentForValidation;
39      
40      /**
41       * Generates the GLPEs for a given accounting document and checks that the debits total equals
42       * the credits total.
43       * <strong>Expects the document to be sent in as a property.</strong>
44       * @see org.kuali.ole.sys.document.validation.GenericValidation#validate(java.lang.Object[])
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          LOG.debug("Validation started");
48          
49          // generate GLPEs specifically here so that we can compare debits to credits
50          if (!SpringContext.getBean(GeneralLedgerPendingEntryService.class).generateGeneralLedgerPendingEntries(accountingDocumentForValidation)) {
51              throw new ValidationException("general ledger GLPE generation failed");
52          }
53  
54          // now loop through all of the GLPEs and calculate buckets for debits and credits
55          KualiDecimal creditAmount = KualiDecimal.ZERO;
56          KualiDecimal debitAmount = KualiDecimal.ZERO;
57          
58          List<GeneralLedgerPendingEntry> pendingEntries = accountingDocumentForValidation.getGeneralLedgerPendingEntries();
59          for(GeneralLedgerPendingEntry entry : pendingEntries) {
60              if(entry.isTransactionEntryOffsetIndicator()) {
61                  continue;
62              }
63              
64              if (OLEConstants.GL_CREDIT_CODE.equals(entry.getTransactionDebitCreditCode())) {
65                  creditAmount = creditAmount.add(entry.getTransactionLedgerEntryAmount());
66              }
67              else {
68                  debitAmount = debitAmount.add(entry.getTransactionLedgerEntryAmount());
69              }           
70          }
71          
72          boolean isValid = debitAmount.compareTo(creditAmount) == 0;
73  
74          if (!isValid) {
75              GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BALANCE);
76          }
77          
78          return isValid;
79      }
80  
81      /**
82       * Gets the accountingDocumentForValidation attribute. 
83       * @return Returns the accountingDocumentForValidation.
84       */
85      public AccountingDocument getAccountingDocumentForValidation() {
86          return accountingDocumentForValidation;
87      }
88  
89      /**
90       * Sets the accountingDocumentForValidation attribute value.
91       * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
92       */
93      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
94          this.accountingDocumentForValidation = accountingDocumentForValidation;
95      }
96  }