View Javadoc
1   /*
2    * Copyright 2005-2006 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.batch.service.AccountingCycleCachingService;
23  import org.kuali.ole.gl.batch.service.BalanceCalculator;
24  import org.kuali.ole.gl.batch.service.PostTransaction;
25  import org.kuali.ole.gl.businessobject.Balance;
26  import org.kuali.ole.gl.businessobject.Transaction;
27  import org.kuali.ole.sys.businessobject.UniversityDate;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.service.ReportWriterService;
30  import org.kuali.ole.sys.service.UniversityDateService;
31  import org.kuali.rice.core.api.util.type.KualiDecimal;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  /**
35   * This implementation of PostTransaction updates the appropriate Balance
36   */
37  @Transactional
38  public class PostBalance implements PostTransaction, BalanceCalculator {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostBalance.class);
40      
41      private AccountingCycleCachingService accountingCycleCachingService;
42      private static final KualiDecimal NEGATIVE_ONE = new KualiDecimal(-1);
43      /**
44       * Constructs a PostBalance instance
45       */
46      public PostBalance() {
47          super();
48      }
49  
50      /**
51       * This posts the effect of the transaction upon the appropriate 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.ole.gl.batch.service.PostTransaction#post(org.kuali.ole.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          String postType = "U";
64  
65          KualiDecimal amount = t.getTransactionLedgerEntryAmount();
66  
67          // Subtract the amount if offset generation indicator & the debit/credit code isn't the same
68          // as the one in the object type code table
69          if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
70              if (!t.getTransactionDebitCreditCode().equals(t.getObjectType().getFinObjectTypeDebitcreditCd())) {
71                  amount = amount.multiply(NEGATIVE_ONE);
72              }
73          }
74  
75          Balance b = accountingCycleCachingService.getBalance(t);
76          if (b == null) {
77              postType = "I";
78              b = new Balance(t);
79          }
80          String period = t.getUniversityFiscalPeriodCode();
81          b.addAmount(period, amount);
82  
83          if (postType.equals("I")) {
84              accountingCycleCachingService.insertBalance(b);
85          } else {
86              accountingCycleCachingService.updateBalance(b);
87          }
88          
89          return postType;
90      }
91  
92      /**
93       * Given a list of balances, determines which one the given trsnaction should post to
94       * 
95       * @param balanceList a Collection of balances
96       * @param t the transaction that is being posted
97       * @return the balance, either found from the list, or, if not present in the list, newly created
98       * @see org.kuali.ole.gl.batch.service.BalanceCalculator#findBalance(java.util.Collection, org.kuali.ole.gl.businessobject.Transaction)
99       */
100     public Balance findBalance(Collection balanceList, Transaction t) {
101         // Try to find one that already exists
102         for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
103             Balance b = (Balance) iter.next();
104 
105             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()) && b.getBalanceTypeCode().equals(t.getFinancialBalanceTypeCode()) && b.getObjectTypeCode().equals(t.getFinancialObjectTypeCode())) {
106                 return b;
107             }
108         }
109 
110         // If we couldn't find one that exists, create a new one
111         Balance b = new Balance(t);
112         balanceList.add(b);
113 
114         return b;
115     }
116 
117     /**
118      * @param t
119      * @param enc
120      */
121     public void updateBalance(Transaction t, Balance b) {
122 
123         // The pending entries haven't been scrubbed so there could be
124         // bad data. This won't update a balance if the data it needs
125         // is invalid
126         KualiDecimal amount = t.getTransactionLedgerEntryAmount();
127         if (amount == null) {
128             amount = KualiDecimal.ZERO;
129         }
130 
131         if (t.getObjectType() == null) {
132             LOG.error("updateBalance() Invalid object type (" + t.getFinancialObjectTypeCode() + ") in pending table");
133             return;
134         }
135 
136         if (t.getBalanceType() == null) {
137             LOG.error("updateBalance() Invalid balance type (" + t.getFinancialBalanceTypeCode() + ") in pending table");
138             return;
139         }
140 
141         // Subtract the amount if offset generation indicator & the debit/credit code isn't the same
142         // as the one in the object type code table
143         if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
144             if (!t.getTransactionDebitCreditCode().equals(t.getObjectType().getFinObjectTypeDebitcreditCd())) {
145                 amount = amount.multiply(new KualiDecimal(-1));
146             }
147         }
148 
149         // update the balance amount of the cooresponding period
150         String period = t.getUniversityFiscalPeriodCode();
151         if (period == null) {
152             UniversityDate currentUniversityDate = SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate();
153             period = currentUniversityDate.getUniversityFiscalAccountingPeriod();
154         }
155 
156         b.addAmount(period, amount);
157     }
158 
159     /**
160      * @see org.kuali.ole.gl.batch.service.PostTransaction#getDestinationName()
161      */
162     public String getDestinationName() {
163         return "GL_BALANCE_T";
164     }
165 
166     public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
167         this.accountingCycleCachingService = accountingCycleCachingService;
168     }
169 }