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.apache.commons.lang.ArrayUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.gl.GeneralLedgerConstants;
25  import org.kuali.ole.gl.batch.PosterEntriesStep;
26  import org.kuali.ole.gl.batch.service.AccountingCycleCachingService;
27  import org.kuali.ole.gl.batch.service.EncumbranceCalculator;
28  import org.kuali.ole.gl.batch.service.PostTransaction;
29  import org.kuali.ole.gl.businessobject.Encumbrance;
30  import org.kuali.ole.gl.businessobject.Entry;
31  import org.kuali.ole.gl.businessobject.Transaction;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.ole.sys.service.ReportWriterService;
34  import org.kuali.rice.core.api.datetime.DateTimeService;
35  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
36  import org.kuali.rice.krad.service.PersistenceStructureService;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  /**
40   * This implementation of PostTransaction posts a transaction that could be an encumbrance
41   */
42  @Transactional
43  public class PostEncumbrance implements PostTransaction, EncumbranceCalculator {
44      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostEncumbrance.class);
45  
46      private AccountingCycleCachingService accountingCycleCachingService;
47      private DateTimeService dateTimeService;
48      private PersistenceStructureService persistenceStructureService;
49      private ParameterService parameterService;
50  
51      /**
52       * Constructs a PostEncumbrance instance
53       */
54      public PostEncumbrance() {
55          super();
56      }
57  
58      /**
59       * Called by the poster to post a transaction. The transaction might or might not be an encumbrance transaction.
60       * 
61       * @param t the transaction which is being posted
62       * @param mode the mode the poster is currently running in
63       * @param postDate the date this transaction should post to
64       * @param posterReportWriterService the writer service where the poster is writing its report
65       * @return the accomplished post type
66       */
67      public String post(Transaction t, int mode, Date postDate, ReportWriterService posterReportWriterService) {
68          LOG.debug("post() started");
69  
70          String returnCode = GeneralLedgerConstants.UPDATE_CODE;
71  
72          // If the encumbrance update code is space or N, or the object type code is FB
73          // we don't need to post an encumbrance
74          if ((StringUtils.isBlank(t.getTransactionEncumbranceUpdateCode())) || " ".equals(t.getTransactionEncumbranceUpdateCode()) || OLEConstants.ENCUMB_UPDT_NO_ENCUMBRANCE_CD.equals(t.getTransactionEncumbranceUpdateCode()) || t.getOption().getFinObjectTypeFundBalanceCd().equals(t.getFinancialObjectTypeCode())) {
75              LOG.debug("post() not posting non-encumbrance transaction");
76              return "";
77          }
78  
79          // Get the current encumbrance record if there is one
80          Entry e = new Entry(t, null);
81          if (OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode())) {
82              e.setDocumentNumber(t.getReferenceFinancialDocumentNumber());
83              e.setFinancialSystemOriginationCode(t.getReferenceFinancialSystemOriginationCode());
84              e.setFinancialDocumentTypeCode(t.getReferenceFinancialDocumentTypeCode());
85          }
86          
87          Encumbrance enc = accountingCycleCachingService.getEncumbrance(e);
88          if (enc == null) {
89              // Build a new encumbrance record
90              enc = new Encumbrance(e);
91  
92              returnCode = GeneralLedgerConstants.INSERT_CODE;
93          }
94          else {
95              // Use the one retrieved
96              if (enc.getTransactionEncumbranceDate() == null) {
97                  enc.setTransactionEncumbranceDate(t.getTransactionDate());
98              }
99  
100             returnCode = GeneralLedgerConstants.UPDATE_CODE;
101         }
102 
103         updateEncumbrance(t, enc);
104 
105         if (returnCode.equals(GeneralLedgerConstants.INSERT_CODE)) {
106             accountingCycleCachingService.insertEncumbrance(enc);
107         } else {
108             accountingCycleCachingService.updateEncumbrance(enc);
109         }
110 
111         return returnCode;
112     }
113 
114     /**
115      * Given a Collection of encumbrances, returns the encumbrance that would affected by the given transaction
116      * 
117      * @param encumbranceList a Collection of encumbrances
118      * @param t the transaction to find the appropriate encumbrance for
119      * @return the encumbrance found from the list, or, if not found, a newly created encumbrance
120      * @see org.kuali.ole.gl.batch.service.EncumbranceCalculator#findEncumbrance(java.util.Collection, org.kuali.ole.gl.businessobject.Transaction)
121      */
122     public Encumbrance findEncumbrance(Collection encumbranceList, Transaction t) {
123 
124         // If it isn't an encumbrance transaction, skip it
125         if ((!OLEConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode())) && (!OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode()))) {
126             return null;
127         }
128 
129         // Try to find one that already exists
130         for (Iterator iter = encumbranceList.iterator(); iter.hasNext();) {
131             Encumbrance e = (Encumbrance) iter.next();
132 
133             if (OLEConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode()) && e.getUniversityFiscalYear().equals(t.getUniversityFiscalYear()) && e.getChartOfAccountsCode().equals(t.getChartOfAccountsCode()) && e.getAccountNumber().equals(t.getAccountNumber()) && e.getSubAccountNumber().equals(t.getSubAccountNumber()) && e.getObjectCode().equals(t.getFinancialObjectCode()) && e.getSubObjectCode().equals(t.getFinancialSubObjectCode()) && e.getBalanceTypeCode().equals(t.getFinancialBalanceTypeCode()) && e.getDocumentTypeCode().equals(t.getFinancialDocumentTypeCode()) && e.getOriginCode().equals(t.getFinancialSystemOriginationCode()) && e.getDocumentNumber().equals(t.getDocumentNumber())) {
134                 return e;
135             }
136 
137             if (OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode()) && e.getUniversityFiscalYear().equals(t.getUniversityFiscalYear()) && e.getChartOfAccountsCode().equals(t.getChartOfAccountsCode()) && e.getAccountNumber().equals(t.getAccountNumber()) && e.getSubAccountNumber().equals(t.getSubAccountNumber()) && e.getObjectCode().equals(t.getFinancialObjectCode()) && e.getSubObjectCode().equals(t.getFinancialSubObjectCode()) && e.getBalanceTypeCode().equals(t.getFinancialBalanceTypeCode()) && e.getDocumentTypeCode().equals(t.getReferenceFinancialDocumentTypeCode()) && e.getOriginCode().equals(t.getReferenceFinancialSystemOriginationCode()) && e.getDocumentNumber().equals(t.getReferenceFinancialDocumentNumber())) {
138                 return e;
139             }
140         }
141 
142         // If we couldn't find one that exists, create a new one
143 
144         // NOTE: the date doesn't matter so there is no need to call the date service
145         // Changed to use the datetime service because of KULRNE-4183
146         Entry e = new Entry(t, dateTimeService.getCurrentDate());
147         if (OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode())) {
148             e.setDocumentNumber(t.getReferenceFinancialDocumentNumber());
149             e.setFinancialSystemOriginationCode(t.getReferenceFinancialSystemOriginationCode());
150             e.setFinancialDocumentTypeCode(t.getReferenceFinancialDocumentTypeCode());
151         }
152 
153         Encumbrance enc = new Encumbrance(e);
154         encumbranceList.add(enc);
155         return enc;
156     }
157 
158     /**
159      * @param t
160      * @param enc
161      */
162     public void updateEncumbrance(Transaction t, Encumbrance enc) {
163         //KFSMI-1571 - check parameter encumbranceOpenAmountOeverridingDocTypes
164         String[] encumbranceOpenAmountOeverridingDocTypes = parameterService.getParameterValuesAsString(PosterEntriesStep.class, GeneralLedgerConstants.PosterService.ENCUMBRANCE_OPEN_AMOUNT_OVERRIDING_DOCUMENT_TYPES).toArray(new String[] {});
165 
166         if (OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t.getTransactionEncumbranceUpdateCode()) && !ArrayUtils.contains(encumbranceOpenAmountOeverridingDocTypes, t.getFinancialDocumentTypeCode())) {
167             // If using referring doc number, add or subtract transaction amount from
168             // encumbrance closed amount
169             if (OLEConstants.GL_DEBIT_CODE.equals(t.getTransactionDebitCreditCode())) {
170                 enc.setAccountLineEncumbranceClosedAmount(enc.getAccountLineEncumbranceClosedAmount().subtract(t.getTransactionLedgerEntryAmount()));
171             }
172             else {
173                 enc.setAccountLineEncumbranceClosedAmount(enc.getAccountLineEncumbranceClosedAmount().add(t.getTransactionLedgerEntryAmount()));
174             }
175         }
176         else {
177             // If not using referring doc number, add or subtract transaction amount from
178             // encumbrance amount
179             if (OLEConstants.GL_DEBIT_CODE.equals(t.getTransactionDebitCreditCode()) || OLEConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode())) {
180                 enc.setAccountLineEncumbranceAmount(enc.getAccountLineEncumbranceAmount().add(t.getTransactionLedgerEntryAmount()));
181             }
182             else {
183                 enc.setAccountLineEncumbranceAmount(enc.getAccountLineEncumbranceAmount().subtract(t.getTransactionLedgerEntryAmount()));
184             }
185         }
186     }
187 
188     /**
189      * @see org.kuali.ole.gl.batch.service.PostTransaction#getDestinationName()
190      */
191     public String getDestinationName() {
192         return persistenceStructureService.getTableName(Encumbrance.class);
193     }
194 
195 
196     public void setDateTimeService(DateTimeService dateTimeService) {
197         this.dateTimeService = dateTimeService;
198     }
199 
200     public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
201         this.accountingCycleCachingService = accountingCycleCachingService;
202     }
203 
204     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
205         this.persistenceStructureService = persistenceStructureService;
206     }
207 
208     public void setParameterService(ParameterService parameterService) {
209         this.parameterService = parameterService;
210     }
211 }