View Javadoc

1   /*
2    * Copyright 2005 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.gl.batch.service.impl;
17  
18  import java.util.Collection;
19  import java.util.Date;
20  import java.util.Iterator;
21  
22  import org.kuali.ole.gl.GeneralLedgerConstants;
23  import org.kuali.ole.gl.batch.service.AccountBalanceCalculator;
24  import org.kuali.ole.gl.batch.service.AccountingCycleCachingService;
25  import org.kuali.ole.gl.batch.service.PostTransaction;
26  import org.kuali.ole.gl.businessobject.AccountBalance;
27  import org.kuali.ole.gl.businessobject.Transaction;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.service.ReportWriterService;
30  import org.kuali.rice.krad.service.PersistenceStructureService;
31  import org.springframework.transaction.annotation.Transactional;
32  
33  @Transactional
34  public class PostAccountBalance implements PostTransaction, AccountBalanceCalculator {
35      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostAccountBalance.class);
36  
37      private AccountingCycleCachingService accountingCycleCachingService;
38      private PersistenceStructureService persistenceStructureService;
39  
40      /**
41       * Constructs a PostAccountBalance instance
42       */
43      public PostAccountBalance() {
44          super();
45      }
46  
47      /**
48       * Posts the transaction to the appropriate account balance record.
49       * 
50       * @param t the transaction which is being posted
51       * @param mode the mode the poster is currently running in
52       * @param postDate the date this transaction should post to
53       * @param posterReportWriterService the writer service where the poster is writing its report
54       * @return the accomplished post type
55       * @see org.kuali.ole.gl.batch.service.PostTransaction#post(org.kuali.ole.gl.businessobject.Transaction, int, java.util.Date)
56       */
57      public String post(Transaction t, int mode, Date postDate, ReportWriterService posterReportWriterService) {
58          LOG.debug("post() started");
59  
60          // Only post transactions where:
61          // balance type code is AC or CB
62          // or where object type isn't FB and balance type code is EX, IE, PE and CE
63          if ((t.getFinancialBalanceTypeCode().equals(t.getOption().getActualFinancialBalanceTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getBudgetCheckingBalanceTypeCd())) || (t.getFinancialBalanceTypeCode().equals(t.getOption().getExtrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getIntrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getPreencumbranceFinBalTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getCostShareEncumbranceBalanceTypeCd())) && (!t.getFinancialObjectTypeCode().equals(t.getOption().getFinObjectTypeFundBalanceCd()))) {
64              // We are posting this transaction
65              String returnCode = GeneralLedgerConstants.UPDATE_CODE;
66  
67              // Load it
68              AccountBalance ab = accountingCycleCachingService.getAccountBalance(t);
69  
70              if (ab == null) {
71                  returnCode = GeneralLedgerConstants.INSERT_CODE;
72                  ab = new AccountBalance(t);
73              }
74  
75              ab.setTimestamp(new java.sql.Date(postDate.getTime()));
76  
77              if (!updateAccountBalanceReturn(t, ab)) {
78                  return GeneralLedgerConstants.EMPTY_CODE;
79              }
80  
81              if (returnCode.equals(GeneralLedgerConstants.INSERT_CODE)) {
82                  accountingCycleCachingService.insertAccountBalance(ab);
83              } else {
84                  accountingCycleCachingService.updateAccountBalance(ab);
85              }
86  
87              return returnCode;
88          }
89          else {
90              return GeneralLedgerConstants.EMPTY_CODE;
91          }
92      }
93  
94      public AccountBalance findAccountBalance(Collection balanceList, Transaction t) {
95  
96          // Try to find one that already exists
97          for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
98              AccountBalance b = (AccountBalance) iter.next();
99  
100             if (b.getUniversityFiscalYear().equals(t.getUniversityFiscalYear()) && b.getChartOfAccountsCode().equals(t.getChartOfAccountsCode()) && b.getAccountNumber().equals(t.getAccountNumber()) && b.getSubAccountNumber().equals(t.getSubAccountNumber()) && b.getObjectCode().equals(t.getFinancialObjectCode()) && b.getSubObjectCode().equals(t.getFinancialSubObjectCode())) {
101                 return b;
102             }
103         }
104 
105         // If we couldn't find one that exists, create a new one
106         AccountBalance b = new AccountBalance(t);
107         balanceList.add(b);
108 
109         return b;
110     }
111 
112     protected boolean updateAccountBalanceReturn(Transaction t, AccountBalance ab) {
113         if (t.getFinancialBalanceTypeCode().equals(t.getOption().getBudgetCheckingBalanceTypeCd())) {
114             ab.setCurrentBudgetLineBalanceAmount(ab.getCurrentBudgetLineBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
115         }
116         else if (t.getFinancialBalanceTypeCode().equals(t.getOption().getActualFinancialBalanceTypeCd())) {
117             if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && OLEConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
118                 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
119             }
120             else {
121                 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
122             }
123         }
124         else if (t.getFinancialBalanceTypeCode().equals(t.getOption().getExtrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getIntrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getPreencumbranceFinBalTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getCostShareEncumbranceBalanceTypeCd())) {
125             if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && OLEConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
126                 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
127             }
128             else {
129                 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
130             }
131         }
132         else {
133             return false;
134         }
135         return true;
136     }
137 
138     /**
139      * @param t
140      * @param enc
141      */
142     public void updateAccountBalance(Transaction t, AccountBalance ab) {
143         updateAccountBalanceReturn(t, ab);
144     }
145 
146     public String getDestinationName() {
147         return persistenceStructureService.getTableName(AccountBalance.class);
148     }
149 
150     public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
151         this.accountingCycleCachingService = accountingCycleCachingService;
152     }
153 
154     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
155         this.persistenceStructureService = persistenceStructureService;
156     }
157 }