001: /*
002: * Copyright 2006-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.Date;
019:
020: import org.apache.ojb.broker.metadata.MetadataManager;
021: import org.kuali.core.util.KualiDecimal;
022: import org.kuali.kfs.KFSConstants;
023: import org.kuali.module.gl.GLConstants;
024: import org.kuali.module.gl.batch.poster.PostTransaction;
025: import org.kuali.module.gl.bo.SufficientFundBalances;
026: import org.kuali.module.gl.bo.Transaction;
027: import org.kuali.module.gl.dao.SufficientFundBalancesDao;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: /**
031: * An implementation of PostTransaction which posts a transaction to the appropriate sufficient funds record
032: */
033: @Transactional
034: public class PostSufficientFundBalances implements PostTransaction {
035: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(PostSufficientFundBalances.class);
037:
038: private SufficientFundBalancesDao sufficientFundBalancesDao;
039:
040: /**
041: * Constructs a PostSufficientFundBalances instance
042: */
043: public PostSufficientFundBalances() {
044: super ();
045: }
046:
047: public void setSufficientFundBalancesDao(
048: SufficientFundBalancesDao sfbd) {
049: sufficientFundBalancesDao = sfbd;
050: }
051:
052: /**
053: * Posts the transaction to the appropriate sufficient funds records
054: *
055: * @param t the transaction which is being posted
056: * @param mode the mode the poster is currently running in
057: * @param postDate the date this transaction should post to
058: * @return the accomplished post type
059: * @see org.kuali.module.gl.batch.poster.PostTransaction#post(org.kuali.module.gl.bo.Transaction, int, java.util.Date)
060: */
061: public String post(Transaction t, int mode, Date postDate) {
062: LOG.debug("post() started");
063:
064: String returnCode = GLConstants.UPDATE_CODE;
065:
066: if (KFSConstants.SF_TYPE_NO_CHECKING.equals(t.getAccount()
067: .getAccountSufficientFundsCode())) {
068: // Don't need to post
069: return GLConstants.EMPTY_CODE;
070: }
071:
072: // Get the Sufficient funds code
073: // Sufficient Funds Code
074: String sufficientFundsObjectCode = null;
075: if (KFSConstants.SF_TYPE_OBJECT.equals(t.getAccount()
076: .getAccountSufficientFundsCode())) {
077: sufficientFundsObjectCode = t.getFinancialObjectCode();
078: } else if (KFSConstants.SF_TYPE_LEVEL.equals(t.getAccount()
079: .getAccountSufficientFundsCode())) {
080: sufficientFundsObjectCode = t.getFinancialObject()
081: .getFinancialObjectLevelCode();
082: } else if (KFSConstants.SF_TYPE_CONSOLIDATION.equals(t
083: .getAccount().getAccountSufficientFundsCode())) {
084: sufficientFundsObjectCode = t.getFinancialObject()
085: .getFinancialObjectLevel()
086: .getFinancialConsolidationObjectCode();
087: } else if (KFSConstants.SF_TYPE_CASH_AT_ACCOUNT.equals(t
088: .getAccount().getAccountSufficientFundsCode())
089: || KFSConstants.SF_TYPE_ACCOUNT.equals(t.getAccount()
090: .getAccountSufficientFundsCode())) {
091: sufficientFundsObjectCode = GLConstants
092: .getSpaceFinancialObjectCode();
093: } else {
094: return "E:Invalid sufficient funds code ("
095: + t.getAccount().getAccountSufficientFundsCode()
096: + ")";
097: }
098:
099: // Look to see if there is a sufficient funds record for this
100: SufficientFundBalances sfBalance = sufficientFundBalancesDao
101: .getByPrimaryId(t.getUniversityFiscalYear(), t
102: .getChartOfAccountsCode(),
103: t.getAccountNumber(), sufficientFundsObjectCode);
104: if (sfBalance == null) {
105: returnCode = GLConstants.INSERT_CODE;
106: sfBalance = new SufficientFundBalances();
107: sfBalance.setUniversityFiscalYear(t
108: .getUniversityFiscalYear());
109: sfBalance
110: .setChartOfAccountsCode(t.getChartOfAccountsCode());
111: sfBalance.setAccountNumber(t.getAccountNumber());
112: sfBalance.setFinancialObjectCode(sufficientFundsObjectCode);
113: sfBalance.setAccountActualExpenditureAmt(KualiDecimal.ZERO);
114: sfBalance.setAccountEncumbranceAmount(KualiDecimal.ZERO);
115: sfBalance.setCurrentBudgetBalanceAmount(KualiDecimal.ZERO);
116: sfBalance.setAccountSufficientFundsCode(t.getAccount()
117: .getAccountSufficientFundsCode());
118: }
119:
120: if (KFSConstants.SF_TYPE_CASH_AT_ACCOUNT.equals(t.getAccount()
121: .getAccountSufficientFundsCode())) {
122: // 2640-PROCESS-CASH
123: if (t.getFinancialBalanceTypeCode().equals(
124: t.getOption().getActualFinancialBalanceTypeCd())) {
125: if (t.getFinancialObjectCode().equals(
126: t.getChart().getFinancialCashObjectCode())
127: || t
128: .getFinancialObjectCode()
129: .equals(
130: t
131: .getChart()
132: .getFinAccountsPayableObjectCode())) {
133: // 2641-PROCESS-CASH-ACTUAL
134: updateBudgetAmount(t
135: .getTransactionDebitCreditCode(),
136: sfBalance, t
137: .getTransactionLedgerEntryAmount());
138: } else {
139: // No need to post this
140: return GLConstants.EMPTY_CODE;
141: }
142: } else if (t.getFinancialBalanceTypeCode().equals(
143: t.getOption().getExtrnlEncumFinBalanceTypCd())
144: || t.getFinancialBalanceTypeCode().equals(
145: t.getOption()
146: .getIntrnlEncumFinBalanceTypCd())
147: || t.getFinancialBalanceTypeCode().equals(
148: t.getOption()
149: .getPreencumbranceFinBalTypeCd())
150: || t.getOption()
151: .getCostShareEncumbranceBalanceTypeCd()
152: .equals(t.getFinancialBalanceTypeCode())) {
153: if (t.getFinancialObjectTypeCode().equals(
154: t.getOption().getFinObjTypeExpenditureexpCd())
155: || t
156: .getFinancialObjectTypeCode()
157: .equals(
158: t
159: .getOption()
160: .getFinObjTypeExpendNotExpCode())
161: || t
162: .getOption()
163: .getFinancialObjectTypeTransferExpenseCd()
164: .equals(t.getFinancialObjectTypeCode())
165: || t.getOption()
166: .getFinObjTypeExpNotExpendCode()
167: .equals(t.getFinancialObjectTypeCode())) {
168: // 2462-PROCESS-CASH-ENCUMBRANCE
169: updateEncumbranceAmount(t
170: .getTransactionDebitCreditCode(),
171: sfBalance, t
172: .getTransactionLedgerEntryAmount());
173: } else {
174: // No need to post this
175: return GLConstants.EMPTY_CODE;
176: }
177: } else {
178: // No need to post this
179: return GLConstants.EMPTY_CODE;
180: }
181: } else {
182: // 2630-PROCESS-OBJECT-OR-ACCOUNT
183: if (t.getFinancialObjectTypeCode().equals(
184: t.getOption().getFinObjTypeExpenditureexpCd())
185: || t.getFinancialObjectTypeCode().equals(
186: t.getOption()
187: .getFinObjTypeExpendNotExpCode())
188: || t.getOption()
189: .getFinancialObjectTypeTransferExpenseCd()
190: .equals(t.getFinancialObjectTypeCode())
191: || t.getOption().getFinObjTypeExpNotExpendCode()
192: .equals(t.getFinancialObjectTypeCode())) {
193: if (t
194: .getFinancialBalanceTypeCode()
195: .equals(
196: t
197: .getOption()
198: .getActualFinancialBalanceTypeCd())) {
199: // 2631-PROCESS-OBJTACCT-ACTUAL
200: updateExpendedAmount(t
201: .getTransactionDebitCreditCode(),
202: sfBalance, t
203: .getTransactionLedgerEntryAmount());
204: } else if (t.getFinancialBalanceTypeCode().equals(
205: t.getOption().getExtrnlEncumFinBalanceTypCd())
206: || t
207: .getFinancialBalanceTypeCode()
208: .equals(
209: t
210: .getOption()
211: .getIntrnlEncumFinBalanceTypCd())
212: || t
213: .getFinancialBalanceTypeCode()
214: .equals(
215: t
216: .getOption()
217: .getPreencumbranceFinBalTypeCd())
218: || "CE".equals(t.getFinancialBalanceTypeCode())) {
219: // 2632-PROCESS-OBJTACCT-ENCMBRNC
220: updateEncumbranceAmount(t
221: .getTransactionDebitCreditCode(),
222: sfBalance, t
223: .getTransactionLedgerEntryAmount());
224: } else if (t.getFinancialBalanceTypeCode().equals(
225: t.getOption().getBudgetCheckingBalanceTypeCd())) {
226: sfBalance.setCurrentBudgetBalanceAmount(sfBalance
227: .getCurrentBudgetBalanceAmount()
228: .add(t.getTransactionLedgerEntryAmount()));
229: } else {
230: // No need to post this
231: return GLConstants.EMPTY_CODE;
232: }
233: } else {
234: // No need to post this
235: return GLConstants.EMPTY_CODE;
236: }
237: }
238:
239: // If we get here, we need to save the balance entry
240: sufficientFundBalancesDao.save(sfBalance);
241:
242: return returnCode;
243: }
244:
245: /**
246: * Updates the expenditure amount of a given sufficient funds balance record
247: *
248: * @param debitCreditCode whether the the amount should be debited or credited to the SF balance
249: * @param bal a sufficient funds balance to update
250: * @param amount the amount to debit or credit
251: */
252: private void updateExpendedAmount(String debitCreditCode,
253: SufficientFundBalances bal, KualiDecimal amount) {
254: if (KFSConstants.GL_CREDIT_CODE.equals(debitCreditCode)) {
255: bal.setAccountActualExpenditureAmt(bal
256: .getAccountActualExpenditureAmt().subtract(amount));
257: } else if (KFSConstants.GL_DEBIT_CODE.equals(debitCreditCode)
258: || KFSConstants.GL_BUDGET_CODE.equals(debitCreditCode)) {
259: bal.setAccountActualExpenditureAmt(bal
260: .getAccountActualExpenditureAmt().add(amount));
261: }
262: }
263:
264: /**
265: * Updates the encumbrance amount of a given sufficient funds balance record
266: *
267: * @param debitCreditCode whether the the amount should be debited or credited to the SF balance
268: * @param bal a sufficient funds balance to update
269: * @param amount the amount to debit or credit
270: */
271: private void updateEncumbranceAmount(String debitCreditCode,
272: SufficientFundBalances bal, KualiDecimal amount) {
273: if (KFSConstants.GL_CREDIT_CODE.equals(debitCreditCode)) {
274: bal.setAccountEncumbranceAmount(bal
275: .getAccountEncumbranceAmount().subtract(amount));
276: } else if (KFSConstants.GL_DEBIT_CODE.equals(debitCreditCode)
277: || KFSConstants.GL_BUDGET_CODE.equals(debitCreditCode)) {
278: bal.setAccountEncumbranceAmount(bal
279: .getAccountEncumbranceAmount().add(amount));
280: }
281: }
282:
283: /**
284: * Updates the budget amount of a given sufficient funds balance record
285: *
286: * @param debitCreditCode whether the the amount should be debited or credited to the SF balance
287: * @param bal a sufficient funds balance to update
288: * @param amount the amount to debit or credit
289: */
290: private void updateBudgetAmount(String debitCreditCode,
291: SufficientFundBalances bal, KualiDecimal amount) {
292: if (KFSConstants.GL_CREDIT_CODE.equals(debitCreditCode)) {
293: bal.setCurrentBudgetBalanceAmount(bal
294: .getCurrentBudgetBalanceAmount().subtract(amount));
295: } else if (KFSConstants.GL_DEBIT_CODE.equals(debitCreditCode)
296: || KFSConstants.GL_BUDGET_CODE.equals(debitCreditCode)) {
297: bal.setCurrentBudgetBalanceAmount(bal
298: .getCurrentBudgetBalanceAmount().add(amount));
299: }
300: }
301:
302: /**
303: * @see org.kuali.module.gl.batch.poster.PostTransaction#getDestinationName()
304: */
305: public String getDestinationName() {
306: return MetadataManager.getInstance().getGlobalRepository()
307: .getDescriptorFor(SufficientFundBalances.class)
308: .getFullTableName();
309: }
310: }
|