001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.gl.batch.poster.impl;
017:
018: import java.util.Collection;
019: import java.util.Date;
020: import java.util.Iterator;
021:
022: import org.apache.ojb.broker.metadata.MetadataManager;
023: import org.kuali.kfs.KFSConstants;
024: import org.kuali.module.gl.GLConstants;
025: import org.kuali.module.gl.batch.poster.AccountBalanceCalculator;
026: import org.kuali.module.gl.batch.poster.PostTransaction;
027: import org.kuali.module.gl.bo.AccountBalance;
028: import org.kuali.module.gl.bo.Transaction;
029: import org.kuali.module.gl.dao.AccountBalanceDao;
030: import org.springframework.transaction.annotation.Transactional;
031:
032: @Transactional
033: public class PostGlAccountBalance implements PostTransaction,
034: AccountBalanceCalculator {
035: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(PostGlAccountBalance.class);
037:
038: private AccountBalanceDao accountBalanceDao;
039:
040: public void setAccountBalanceDao(AccountBalanceDao abd) {
041: accountBalanceDao = abd;
042: }
043:
044: /**
045: * Constructs a PostGlAccountBalance instance
046: */
047: public PostGlAccountBalance() {
048: super ();
049: }
050:
051: /**
052: * Posts the transaction to the appropriate account balance record.
053: *
054: * @param t the transaction which is being posted
055: * @param mode the mode the poster is currently running in
056: * @param postDate the date this transaction should post to
057: * @return the accomplished post type
058: * @see org.kuali.module.gl.batch.poster.PostTransaction#post(org.kuali.module.gl.bo.Transaction, int, java.util.Date)
059: */
060: public String post(Transaction t, int mode, Date postDate) {
061: LOG.debug("post() started");
062:
063: // Only post transactions where:
064: // balance type code is AC or CB
065: // or where object type isn't FB and balance type code is EX, IE, PE and CE
066: if ((t.getFinancialBalanceTypeCode().equals(
067: t.getOption().getActualFinancialBalanceTypeCd()) || t
068: .getFinancialBalanceTypeCode().equals(
069: t.getOption().getBudgetCheckingBalanceTypeCd()))
070: || (t.getFinancialBalanceTypeCode().equals(
071: t.getOption().getExtrnlEncumFinBalanceTypCd())
072: || t
073: .getFinancialBalanceTypeCode()
074: .equals(
075: t
076: .getOption()
077: .getIntrnlEncumFinBalanceTypCd())
078: || t
079: .getFinancialBalanceTypeCode()
080: .equals(
081: t
082: .getOption()
083: .getPreencumbranceFinBalTypeCd()) || t
084: .getFinancialBalanceTypeCode()
085: .equals(
086: t
087: .getOption()
088: .getCostShareEncumbranceBalanceTypeCd()))
089: && (!t.getFinancialObjectTypeCode().equals(
090: t.getOption().getFinObjectTypeFundBalanceCd()))) {
091: // We are posting this transaction
092: String returnCode = GLConstants.UPDATE_CODE;
093:
094: // Load it
095: AccountBalance ab = accountBalanceDao.getByTransaction(t);
096:
097: if (ab == null) {
098: returnCode = GLConstants.INSERT_CODE;
099: ab = new AccountBalance(t);
100: }
101:
102: ab.setTimestamp(new java.sql.Date(postDate.getTime()));
103:
104: if (!updateAccountBalanceReturn(t, ab)) {
105: return GLConstants.EMPTY_CODE;
106: }
107:
108: accountBalanceDao.save(ab);
109:
110: return returnCode;
111: } else {
112: return GLConstants.EMPTY_CODE;
113: }
114: }
115:
116: public AccountBalance findAccountBalance(Collection balanceList,
117: Transaction t) {
118:
119: // Try to find one that already exists
120: for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
121: AccountBalance b = (AccountBalance) iter.next();
122:
123: if (b.getUniversityFiscalYear().equals(
124: t.getUniversityFiscalYear())
125: && b.getChartOfAccountsCode().equals(
126: t.getChartOfAccountsCode())
127: && b.getAccountNumber()
128: .equals(t.getAccountNumber())
129: && b.getSubAccountNumber().equals(
130: t.getSubAccountNumber())
131: && b.getObjectCode().equals(
132: t.getFinancialObjectCode())
133: && b.getSubObjectCode().equals(
134: t.getFinancialSubObjectCode())) {
135: return b;
136: }
137: }
138:
139: // If we couldn't find one that exists, create a new one
140: AccountBalance b = new AccountBalance(t);
141: balanceList.add(b);
142:
143: return b;
144: }
145:
146: private boolean updateAccountBalanceReturn(Transaction t,
147: AccountBalance ab) {
148: if (t.getFinancialBalanceTypeCode().equals(
149: t.getOption().getBudgetCheckingBalanceTypeCd())) {
150: ab.setCurrentBudgetLineBalanceAmount(ab
151: .getCurrentBudgetLineBalanceAmount().add(
152: t.getTransactionLedgerEntryAmount()));
153: } else if (t.getFinancialBalanceTypeCode().equals(
154: t.getOption().getActualFinancialBalanceTypeCd())) {
155: if (t.getObjectType().getFinObjectTypeDebitcreditCd()
156: .equals(t.getTransactionDebitCreditCode())
157: || ((!t.getBalanceType()
158: .isFinancialOffsetGenerationIndicator()) && KFSConstants.GL_BUDGET_CODE
159: .equals(t.getTransactionDebitCreditCode()))) {
160: ab.setAccountLineActualsBalanceAmount(ab
161: .getAccountLineActualsBalanceAmount().add(
162: t.getTransactionLedgerEntryAmount()));
163: } else {
164: ab.setAccountLineActualsBalanceAmount(ab
165: .getAccountLineActualsBalanceAmount().subtract(
166: t.getTransactionLedgerEntryAmount()));
167: }
168: } else if (t.getFinancialBalanceTypeCode().equals(
169: t.getOption().getExtrnlEncumFinBalanceTypCd())
170: || t.getFinancialBalanceTypeCode().equals(
171: t.getOption().getIntrnlEncumFinBalanceTypCd())
172: || t.getFinancialBalanceTypeCode().equals(
173: t.getOption().getPreencumbranceFinBalTypeCd())
174: || t
175: .getFinancialBalanceTypeCode()
176: .equals(
177: t
178: .getOption()
179: .getCostShareEncumbranceBalanceTypeCd())) {
180: if (t.getObjectType().getFinObjectTypeDebitcreditCd()
181: .equals(t.getTransactionDebitCreditCode())
182: || ((!t.getBalanceType()
183: .isFinancialOffsetGenerationIndicator()) && KFSConstants.GL_BUDGET_CODE
184: .equals(t.getTransactionDebitCreditCode()))) {
185: ab.setAccountLineEncumbranceBalanceAmount(ab
186: .getAccountLineEncumbranceBalanceAmount().add(
187: t.getTransactionLedgerEntryAmount()));
188: } else {
189: ab.setAccountLineEncumbranceBalanceAmount(ab
190: .getAccountLineEncumbranceBalanceAmount()
191: .subtract(t.getTransactionLedgerEntryAmount()));
192: }
193: } else {
194: return false;
195: }
196: return true;
197: }
198:
199: /**
200: * @param t
201: * @param enc
202: */
203: public void updateAccountBalance(Transaction t, AccountBalance ab) {
204: updateAccountBalanceReturn(t, ab);
205: }
206:
207: public String getDestinationName() {
208: return MetadataManager.getInstance().getGlobalRepository()
209: .getDescriptorFor(AccountBalance.class)
210: .getFullTableName();
211: }
212: }
|