View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.gl.batch.service.impl;
20  
21  import java.util.Collection;
22  import java.util.Date;
23  import java.util.Iterator;
24  
25  import org.kuali.kfs.gl.GeneralLedgerConstants;
26  import org.kuali.kfs.gl.batch.service.AccountBalanceCalculator;
27  import org.kuali.kfs.gl.batch.service.AccountingCycleCachingService;
28  import org.kuali.kfs.gl.batch.service.PostTransaction;
29  import org.kuali.kfs.gl.businessobject.AccountBalance;
30  import org.kuali.kfs.gl.businessobject.Transaction;
31  import org.kuali.kfs.sys.KFSConstants;
32  import org.kuali.kfs.sys.service.ReportWriterService;
33  import org.kuali.rice.krad.service.PersistenceStructureService;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  @Transactional
37  public class PostAccountBalance implements PostTransaction, AccountBalanceCalculator {
38      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostAccountBalance.class);
39  
40      private AccountingCycleCachingService accountingCycleCachingService;
41      private PersistenceStructureService persistenceStructureService;
42  
43      /**
44       * Constructs a PostAccountBalance instance
45       */
46      public PostAccountBalance() {
47          super();
48      }
49  
50      /**
51       * Posts the transaction to the appropriate account balance record.
52       * 
53       * @param t the transaction which is being posted
54       * @param mode the mode the poster is currently running in
55       * @param postDate the date this transaction should post to
56       * @param posterReportWriterService the writer service where the poster is writing its report
57       * @return the accomplished post type
58       * @see org.kuali.kfs.gl.batch.service.PostTransaction#post(org.kuali.kfs.gl.businessobject.Transaction, int, java.util.Date)
59       */
60      public String post(Transaction t, int mode, Date postDate, ReportWriterService posterReportWriterService) {
61          LOG.debug("post() started");
62  
63          // Only post transactions where:
64          // balance type code is AC or CB
65          // or where object type isn't FB and balance type code is EX, IE, PE and CE
66          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()))) {
67              // We are posting this transaction
68              String returnCode = GeneralLedgerConstants.UPDATE_CODE;
69  
70              // Load it
71              AccountBalance ab = accountingCycleCachingService.getAccountBalance(t);
72  
73              if (ab == null) {
74                  returnCode = GeneralLedgerConstants.INSERT_CODE;
75                  ab = new AccountBalance(t);
76              }
77  
78              ab.setTimestamp(new java.sql.Date(postDate.getTime()));
79  
80              if (!updateAccountBalanceReturn(t, ab)) {
81                  return GeneralLedgerConstants.EMPTY_CODE;
82              }
83  
84              if (returnCode.equals(GeneralLedgerConstants.INSERT_CODE)) {
85                  accountingCycleCachingService.insertAccountBalance(ab);
86              } else {
87                  accountingCycleCachingService.updateAccountBalance(ab);
88              }
89  
90              return returnCode;
91          }
92          else {
93              return GeneralLedgerConstants.EMPTY_CODE;
94          }
95      }
96  
97      public AccountBalance findAccountBalance(Collection balanceList, Transaction t) {
98  
99          // Try to find one that already exists
100         for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
101             AccountBalance b = (AccountBalance) iter.next();
102 
103             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())) {
104                 return b;
105             }
106         }
107 
108         // If we couldn't find one that exists, create a new one
109         AccountBalance b = new AccountBalance(t);
110         balanceList.add(b);
111 
112         return b;
113     }
114 
115     protected boolean updateAccountBalanceReturn(Transaction t, AccountBalance ab) {
116         if (t.getFinancialBalanceTypeCode().equals(t.getOption().getBudgetCheckingBalanceTypeCd())) {
117             ab.setCurrentBudgetLineBalanceAmount(ab.getCurrentBudgetLineBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
118         }
119         else if (t.getFinancialBalanceTypeCode().equals(t.getOption().getActualFinancialBalanceTypeCd())) {
120             if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && KFSConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
121                 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
122             }
123             else {
124                 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
125             }
126         }
127         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())) {
128             if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && KFSConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
129                 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
130             }
131             else {
132                 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
133             }
134         }
135         else {
136             return false;
137         }
138         return true;
139     }
140 
141     /**
142      * @param t
143      * @param enc
144      */
145     public void updateAccountBalance(Transaction t, AccountBalance ab) {
146         updateAccountBalanceReturn(t, ab);
147     }
148 
149     public String getDestinationName() {
150         return persistenceStructureService.getTableName(AccountBalance.class);
151     }
152 
153     public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
154         this.accountingCycleCachingService = accountingCycleCachingService;
155     }
156 
157     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
158         this.persistenceStructureService = persistenceStructureService;
159     }
160 }