1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  
42  
43  
44  
45  
46      public boolean validate(AttributedDocumentEvent event) {
47          LOG.debug("Validation started");
48          
49          
50          if (!SpringContext.getBean(GeneralLedgerPendingEntryService.class).generateGeneralLedgerPendingEntries(accountingDocumentForValidation)) {
51              throw new ValidationException("general ledger GLPE generation failed");
52          }
53  
54          
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  
83  
84  
85      public AccountingDocument getAccountingDocumentForValidation() {
86          return accountingDocumentForValidation;
87      }
88  
89      
90  
91  
92  
93      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
94          this.accountingDocumentForValidation = accountingDocumentForValidation;
95      }
96  }