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.kuali.core.util.KualiDecimal;
023: import org.kuali.kfs.context.SpringContext;
024: import org.kuali.module.financial.service.UniversityDateService;
025: import org.kuali.module.gl.batch.poster.BalanceCalculator;
026: import org.kuali.module.gl.batch.poster.PostTransaction;
027: import org.kuali.module.gl.bo.Balance;
028: import org.kuali.module.gl.bo.Transaction;
029: import org.kuali.module.gl.bo.UniversityDate;
030: import org.kuali.module.gl.dao.BalanceDao;
031: import org.springframework.transaction.annotation.Transactional;
032:
033: /**
034: * This implementation of PostTransaction updates the appropriate Balance
035: */
036: @Transactional
037: public class PostBalance implements PostTransaction, BalanceCalculator {
038: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(PostBalance.class);
040:
041: private BalanceDao balanceDao;
042:
043: /**
044: * Constructs a PostBalance instance
045: */
046: public PostBalance() {
047: super ();
048: }
049:
050: /**
051: * This posts the effect of the transaction upon the appropriate balance record.
052: *
053: * @param t the transaction which is being posted
054: * @param mode the mode the poster is currently running in
055: * @param postDate the date this transaction should post to
056: * @return the accomplished post type
057: * @see org.kuali.module.gl.batch.poster.PostTransaction#post(org.kuali.module.gl.bo.Transaction, int, java.util.Date)
058: */
059: public String post(Transaction t, int mode, Date postDate) {
060: LOG.debug("post() started");
061:
062: String postType = "U";
063:
064: KualiDecimal amount = t.getTransactionLedgerEntryAmount();
065:
066: // Subtract the amount if offset generation indicator & the debit/credit code isn't the same
067: // as the one in the object type code table
068: if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
069: if (!t.getTransactionDebitCreditCode().equals(
070: t.getObjectType().getFinObjectTypeDebitcreditCd())) {
071: amount = amount.multiply(new KualiDecimal(-1));
072: }
073: }
074:
075: Balance b = balanceDao.getBalanceByTransaction(t);
076: if (b == null) {
077: postType = "I";
078: b = new Balance(t);
079: }
080: b.setTimestamp(new java.sql.Date(postDate.getTime()));
081:
082: String period = t.getUniversityFiscalPeriodCode();
083: b.addAmount(period, amount);
084:
085: balanceDao.save(b);
086:
087: return postType;
088: }
089:
090: /**
091: * Given a list of balances, determines which one the given trsnaction should post to
092: *
093: * @param balanceList a Collection of balances
094: * @param t the transaction that is being posted
095: * @return the balance, either found from the list, or, if not present in the list, newly created
096: * @see org.kuali.module.gl.batch.poster.BalanceCalculator#findBalance(java.util.Collection, org.kuali.module.gl.bo.Transaction)
097: */
098: public Balance findBalance(Collection balanceList, Transaction t) {
099: // Try to find one that already exists
100: for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
101: Balance b = (Balance) iter.next();
102:
103: if (b.getUniversityFiscalYear().equals(
104: t.getUniversityFiscalYear())
105: && b.getChartOfAccountsCode().equals(
106: t.getChartOfAccountsCode())
107: && b.getAccountNumber()
108: .equals(t.getAccountNumber())
109: && b.getSubAccountNumber().equals(
110: t.getSubAccountNumber())
111: && b.getObjectCode().equals(
112: t.getFinancialObjectCode())
113: && b.getSubObjectCode().equals(
114: t.getFinancialSubObjectCode())
115: && b.getBalanceTypeCode().equals(
116: t.getFinancialBalanceTypeCode())
117: && b.getObjectTypeCode().equals(
118: t.getFinancialObjectTypeCode())) {
119: return b;
120: }
121: }
122:
123: // If we couldn't find one that exists, create a new one
124: Balance b = new Balance(t);
125: balanceList.add(b);
126:
127: return b;
128: }
129:
130: /**
131: * @param t
132: * @param enc
133: */
134: public void updateBalance(Transaction t, Balance b) {
135:
136: // The pending entries haven't been scrubbed so there could be
137: // bad data. This won't update a balance if the data it needs
138: // is invalid
139: KualiDecimal amount = t.getTransactionLedgerEntryAmount();
140: if (amount == null) {
141: amount = KualiDecimal.ZERO;
142: }
143:
144: if (t.getObjectType() == null) {
145: LOG.error("updateBalance() Invalid object type ("
146: + t.getFinancialObjectTypeCode()
147: + ") in pending table");
148: return;
149: }
150:
151: if (t.getBalanceType() == null) {
152: LOG.error("updateBalance() Invalid balance type ("
153: + t.getFinancialBalanceTypeCode()
154: + ") in pending table");
155: return;
156: }
157:
158: // Subtract the amount if offset generation indicator & the debit/credit code isn't the same
159: // as the one in the object type code table
160: if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
161: if (!t.getTransactionDebitCreditCode().equals(
162: t.getObjectType().getFinObjectTypeDebitcreditCd())) {
163: amount = amount.multiply(new KualiDecimal(-1));
164: }
165: }
166:
167: // update the balance amount of the cooresponding period
168: String period = t.getUniversityFiscalPeriodCode();
169: if (period == null) {
170: UniversityDate currentUniversityDate = SpringContext
171: .getBean(UniversityDateService.class)
172: .getCurrentUniversityDate();
173: period = currentUniversityDate
174: .getUniversityFiscalAccountingPeriod();
175: }
176:
177: b.addAmount(period, amount);
178: }
179:
180: /**
181: * @see org.kuali.module.gl.batch.poster.PostTransaction#getDestinationName()
182: */
183: public String getDestinationName() {
184: return "GL_BALANCE_T";
185: }
186:
187: public void setBalanceDao(BalanceDao bd) {
188: balanceDao = bd;
189: }
190: }
|