001: /*
002: * Copyright 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.kfs.service.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.kuali.core.service.KualiRuleService;
025: import org.kuali.core.util.GeneralLedgerPendingEntrySequenceHelper;
026: import org.kuali.core.util.KualiDecimal;
027: import org.kuali.kfs.bo.AccountingLine;
028: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
029: import org.kuali.kfs.bo.Options;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.kfs.dao.GeneralLedgerPendingEntryDao;
032: import org.kuali.kfs.document.AccountingDocument;
033: import org.kuali.kfs.document.GeneralLedgerPostingDocument;
034: import org.kuali.kfs.rule.event.GenerateGeneralLedgerDocumentPendingEntriesEvent;
035: import org.kuali.kfs.rule.event.GenerateGeneralLedgerPendingEntriesEvent;
036: import org.kuali.kfs.service.GeneralLedgerPendingEntryService;
037: import org.kuali.kfs.service.OptionsService;
038: import org.kuali.kfs.service.ParameterService;
039: import org.kuali.module.chart.bo.Account;
040: import org.kuali.module.chart.bo.Chart;
041: import org.kuali.module.chart.bo.codes.BalanceTyp;
042: import org.kuali.module.chart.service.BalanceTypService;
043: import org.kuali.module.chart.service.ChartService;
044: import org.kuali.module.chart.service.ObjectTypeService;
045: import org.kuali.module.financial.service.UniversityDateService;
046: import org.kuali.module.gl.bo.Balance;
047: import org.kuali.module.gl.bo.Encumbrance;
048: import org.kuali.module.gl.bo.UniversityDate;
049: import org.kuali.module.gl.service.SufficientFundsServiceConstants;
050: import org.springframework.transaction.annotation.Transactional;
051:
052: /**
053: * This class is the service implementation for the GeneralLedgerPendingEntry structure. This is the default implementation, that is
054: * delivered with Kuali.
055: */
056: @Transactional
057: public class GeneralLedgerPendingEntryServiceImpl implements
058: GeneralLedgerPendingEntryService {
059: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
060: .getLogger(GeneralLedgerPendingEntryServiceImpl.class);
061:
062: private GeneralLedgerPendingEntryDao generalLedgerPendingEntryDao;
063: private KualiRuleService kualiRuleService;
064: private ChartService chartService;
065: private OptionsService optionsService;
066: private ParameterService parameterService;
067: private BalanceTypService balanceTypeService;
068:
069: /**
070: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getExpenseSummary(java.util.List, java.lang.String,
071: * java.lang.String, boolean, boolean)
072: */
073: public KualiDecimal getExpenseSummary(Integer universityFiscalYear,
074: String chartOfAccountsCode, String accountNumber,
075: String sufficientFundsObjectCode, boolean isDebit,
076: boolean isYearEnd) {
077: LOG.debug("getExpenseSummary() started");
078:
079: ObjectTypeService objectTypeService = (ObjectTypeService) SpringContext
080: .getBean(ObjectTypeService.class);
081: List<String> objectTypes = objectTypeService
082: .getExpenseObjectTypes(universityFiscalYear);
083:
084: Options options = optionsService
085: .getOptions(universityFiscalYear);
086:
087: Collection balanceTypeCodes = new ArrayList();
088: balanceTypeCodes.add(options.getActualFinancialBalanceTypeCd());
089:
090: return generalLedgerPendingEntryDao.getTransactionSummary(
091: universityFiscalYear, chartOfAccountsCode,
092: accountNumber, objectTypes, balanceTypeCodes,
093: sufficientFundsObjectCode, isDebit, isYearEnd);
094: }
095:
096: /**
097: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getEncumbranceSummary(java.lang.Integer, java.lang.String,
098: * java.lang.String, java.lang.String, boolean, boolean)
099: */
100: public KualiDecimal getEncumbranceSummary(
101: Integer universityFiscalYear, String chartOfAccountsCode,
102: String accountNumber, String sufficientFundsObjectCode,
103: boolean isDebit, boolean isYearEnd) {
104: LOG.debug("getEncumbranceSummary() started");
105:
106: ObjectTypeService objectTypeService = (ObjectTypeService) SpringContext
107: .getBean(ObjectTypeService.class);
108: List<String> objectTypes = objectTypeService
109: .getExpenseObjectTypes(universityFiscalYear);
110:
111: Options options = optionsService
112: .getOptions(universityFiscalYear);
113:
114: List<String> balanceTypeCodes = new ArrayList();
115: for (BalanceTyp balanceTyp : (Collection<BalanceTyp>) balanceTypeService
116: .getEncumbranceBalanceTypes()) {
117: balanceTypeCodes.add(balanceTyp.getCode());
118: }
119:
120: return generalLedgerPendingEntryDao.getTransactionSummary(
121: universityFiscalYear, chartOfAccountsCode,
122: accountNumber, objectTypes, balanceTypeCodes,
123: sufficientFundsObjectCode, isDebit, isYearEnd);
124: }
125:
126: /**
127: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getBudgetSummary(java.lang.Integer, java.lang.String,
128: * java.lang.String, java.lang.String, boolean)
129: */
130: public KualiDecimal getBudgetSummary(Integer universityFiscalYear,
131: String chartOfAccountsCode, String accountNumber,
132: String sufficientFundsObjectCode, boolean isYearEnd) {
133: LOG.debug("getBudgetSummary() started");
134:
135: ObjectTypeService objectTypeService = (ObjectTypeService) SpringContext
136: .getBean(ObjectTypeService.class);
137: List<String> objectTypes = objectTypeService
138: .getExpenseObjectTypes(universityFiscalYear);
139:
140: Options options = optionsService
141: .getOptions(universityFiscalYear);
142:
143: Collection balanceTypeCodes = new ArrayList();
144: balanceTypeCodes.add(options.getBudgetCheckingBalanceTypeCd());
145:
146: return generalLedgerPendingEntryDao.getTransactionSummary(
147: universityFiscalYear, chartOfAccountsCode,
148: accountNumber, objectTypes, balanceTypeCodes,
149: sufficientFundsObjectCode, isYearEnd);
150: }
151:
152: /**
153: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getCashSummary(java.util.Collection, java.lang.String,
154: * java.lang.String, boolean)
155: */
156: public KualiDecimal getCashSummary(List universityFiscalYears,
157: String chartOfAccountsCode, String accountNumber,
158: boolean isDebit) {
159: LOG.debug("getCashSummary() started");
160:
161: Chart c = chartService.getByPrimaryId(chartOfAccountsCode);
162:
163: // Note, we are getting the options from the first fiscal year in the list. We are assuming that the
164: // balance type code for actual is the same in all the years in the list.
165: Options options = optionsService
166: .getOptions((Integer) universityFiscalYears.get(0));
167:
168: Collection objectCodes = new ArrayList();
169: objectCodes.add(c.getFinancialCashObjectCode());
170:
171: Collection balanceTypeCodes = new ArrayList();
172: balanceTypeCodes.add(options.getActualFinancialBalanceTypeCd());
173:
174: return generalLedgerPendingEntryDao.getTransactionSummary(
175: universityFiscalYears, chartOfAccountsCode,
176: accountNumber, objectCodes, balanceTypeCodes, isDebit);
177: }
178:
179: /**
180: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getActualSummary(java.util.List, java.lang.String,
181: * java.lang.String, boolean)
182: */
183: public KualiDecimal getActualSummary(List universityFiscalYears,
184: String chartOfAccountsCode, String accountNumber,
185: boolean isDebit) {
186: LOG.debug("getActualSummary() started");
187:
188: List<String> codes = parameterService
189: .getParameterValues(
190: ParameterConstants.FINANCIAL_SYSTEM_ALL.class,
191: SufficientFundsServiceConstants.SUFFICIENT_FUNDS_OBJECT_CODE_SPECIALS);
192:
193: // Note, we are getting the options from the first fiscal year in the list. We are assuming that the
194: // balance type code for actual is the same in all the years in the list.
195: Options options = optionsService
196: .getOptions((Integer) universityFiscalYears.get(0));
197:
198: Collection balanceTypeCodes = new ArrayList();
199: balanceTypeCodes.add(options.getActualFinancialBalanceTypeCd());
200:
201: return generalLedgerPendingEntryDao.getTransactionSummary(
202: universityFiscalYears, chartOfAccountsCode,
203: accountNumber, codes, balanceTypeCodes, isDebit);
204: }
205:
206: /**
207: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#getByPrimaryId(java.lang.Integer, java.lang.String)
208: */
209: public GeneralLedgerPendingEntry getByPrimaryId(
210: Integer transactionEntrySequenceId, String documentHeaderId) {
211: LOG.debug("getByPrimaryId() started");
212:
213: return generalLedgerPendingEntryDao.getByPrimaryId(
214: documentHeaderId, transactionEntrySequenceId);
215: }
216:
217: public void fillInFiscalPeriodYear(GeneralLedgerPendingEntry glpe) {
218: LOG.debug("fillInFiscalPeriodYear() started");
219:
220: // TODO Handle year end documents
221:
222: if ((glpe.getUniversityFiscalPeriodCode() == null)
223: || (glpe.getUniversityFiscalYear() == null)) {
224: UniversityDate ud = SpringContext.getBean(
225: UniversityDateService.class)
226: .getCurrentUniversityDate();
227:
228: glpe.setUniversityFiscalYear(ud.getUniversityFiscalYear());
229: glpe.setUniversityFiscalPeriodCode(ud
230: .getUniversityFiscalAccountingPeriod());
231: }
232: }
233:
234: /**
235: * Invokes generateEntries method on the financial document.
236: *
237: * @param document - document whose pending entries need generated
238: * @return whether the business rules succeeded
239: */
240: public boolean generateGeneralLedgerPendingEntries(
241: GeneralLedgerPostingDocument document) {
242: boolean success = true;
243:
244: // we must clear them first before creating new ones
245: document.getGeneralLedgerPendingEntries().clear();
246:
247: LOG
248: .info("deleting existing gl pending ledger entries for document "
249: + document.getDocumentNumber());
250: delete(document.getDocumentNumber());
251:
252: LOG.info("generating gl pending ledger entries for document "
253: + document.getDocumentNumber());
254: GeneralLedgerPendingEntrySequenceHelper sequenceHelper = new GeneralLedgerPendingEntrySequenceHelper();
255: if (document instanceof AccountingDocument) {
256: AccountingDocument transactionalDocument = (AccountingDocument) document;
257: List sourceAccountingLines = transactionalDocument
258: .getSourceAccountingLines();
259: if (sourceAccountingLines != null) {
260: for (Iterator iter = sourceAccountingLines.iterator(); iter
261: .hasNext();) {
262: success &= processGeneralLedgerPendingEntryForAccountingLine(
263: transactionalDocument, sequenceHelper, iter);
264: }
265: }
266:
267: List targetAccountingLines = transactionalDocument
268: .getTargetAccountingLines();
269: if (targetAccountingLines != null) {
270: for (Iterator iter = targetAccountingLines.iterator(); iter
271: .hasNext();) {
272: success &= processGeneralLedgerPendingEntryForAccountingLine(
273: transactionalDocument, sequenceHelper, iter);
274: }
275: }
276: }
277:
278: // doc specific pending entries generation
279: GenerateGeneralLedgerDocumentPendingEntriesEvent event = new GenerateGeneralLedgerDocumentPendingEntriesEvent(
280: document, sequenceHelper);
281: success &= kualiRuleService.applyRules(event);
282: return success;
283: }
284:
285: /**
286: * This method handles generically taking an accounting line, doing a deep copy on it so that we have a new instance without
287: * reference to the original (won't affect the tran doc's acct lines), performing a retrieveNonKeyFields on the line to make
288: * sure it's populated properly, and then calling the rule framework driven GLPE generation code.
289: *
290: * @param document
291: * @param sequenceHelper
292: * @param iter
293: * @return whether the business rules succeeded
294: */
295: private boolean processGeneralLedgerPendingEntryForAccountingLine(
296: AccountingDocument document,
297: GeneralLedgerPendingEntrySequenceHelper sequenceHelper,
298: Iterator iter) {
299: LOG
300: .debug("processGeneralLedgerPendingEntryForAccountingLine() started");
301: boolean success = true;
302:
303: AccountingLine accountingLine = (AccountingLine) iter.next();
304:
305: GenerateGeneralLedgerPendingEntriesEvent event = new GenerateGeneralLedgerPendingEntriesEvent(
306: document, accountingLine, sequenceHelper);
307: success &= kualiRuleService.applyRules(event);
308: sequenceHelper.increment(); // increment for the next line
309: return success;
310: }
311:
312: /**
313: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#save(org.kuali.module.gl.bo.GeneralLedgerPendingEntry)
314: */
315: public void save(GeneralLedgerPendingEntry generalLedgerPendingEntry) {
316: LOG.debug("save() started");
317:
318: generalLedgerPendingEntryDao.save(generalLedgerPendingEntry);
319: }
320:
321: public void delete(String documentHeaderId) {
322: LOG.debug("delete() started");
323:
324: this .generalLedgerPendingEntryDao.delete(documentHeaderId);
325: }
326:
327: public void deleteByFinancialDocumentApprovedCode(
328: String financialDocumentApprovedCode) {
329: LOG.debug("deleteByFinancialDocumentApprovedCode() started");
330:
331: this .generalLedgerPendingEntryDao
332: .deleteByFinancialDocumentApprovedCode(financialDocumentApprovedCode);
333: }
334:
335: /**
336: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findApprovedPendingLedgerEntries()
337: */
338: public Iterator findApprovedPendingLedgerEntries() {
339: LOG.debug("findApprovedPendingLedgerEntries() started");
340:
341: return generalLedgerPendingEntryDao
342: .findApprovedPendingLedgerEntries();
343: }
344:
345: /**
346: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntries(org.kuali.module.gl.bo.Encumbrance,
347: * boolean)
348: */
349: public Iterator findPendingLedgerEntries(Encumbrance encumbrance,
350: boolean isApproved) {
351: LOG.debug("findPendingLedgerEntries() started");
352:
353: return generalLedgerPendingEntryDao.findPendingLedgerEntries(
354: encumbrance, isApproved);
355: }
356:
357: /**
358: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#hasPendingGeneralLedgerEntry(org.kuali.module.chart.bo.Account)
359: */
360: public boolean hasPendingGeneralLedgerEntry(Account account) {
361: LOG.debug("hasPendingGeneralLedgerEntry() started");
362:
363: return generalLedgerPendingEntryDao
364: .countPendingLedgerEntries(account) > 0;
365: }
366:
367: /**
368: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntries(Balance, boolean, boolean)
369: */
370: public Iterator findPendingLedgerEntries(Balance balance,
371: boolean isApproved, boolean isConsolidated) {
372: LOG.debug("findPendingLedgerEntries() started");
373:
374: return generalLedgerPendingEntryDao.findPendingLedgerEntries(
375: balance, isApproved, isConsolidated);
376: }
377:
378: /**
379: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForEntry(java.util.Map, boolean)
380: */
381: public Iterator findPendingLedgerEntriesForEntry(Map fieldValues,
382: boolean isApproved) {
383: LOG.debug("findPendingLedgerEntriesForEntry() started");
384:
385: return generalLedgerPendingEntryDao
386: .findPendingLedgerEntriesForEntry(fieldValues,
387: isApproved);
388: }
389:
390: /**
391: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForEncumbrance(Map, boolean)
392: */
393: public Iterator findPendingLedgerEntriesForEncumbrance(
394: Map fieldValues, boolean isApproved) {
395: LOG.debug("findPendingLedgerEntriesForEncumbrance() started");
396:
397: return generalLedgerPendingEntryDao
398: .findPendingLedgerEntriesForEncumbrance(fieldValues,
399: isApproved);
400: }
401:
402: /**
403: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForCashBalance(java.util.Map,
404: * boolean)
405: */
406: public Iterator findPendingLedgerEntriesForCashBalance(
407: Map fieldValues, boolean isApproved) {
408: LOG.debug("findPendingLedgerEntriesForCashBalance() started");
409:
410: return generalLedgerPendingEntryDao
411: .findPendingLedgerEntriesForCashBalance(fieldValues,
412: isApproved);
413: }
414:
415: /**
416: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForBalance(java.util.Map, boolean)
417: */
418: public Iterator findPendingLedgerEntriesForBalance(Map fieldValues,
419: boolean isApproved) {
420: LOG.debug("findPendingLedgerEntriesForBalance() started");
421:
422: return generalLedgerPendingEntryDao
423: .findPendingLedgerEntriesForBalance(fieldValues,
424: isApproved);
425: }
426:
427: /**
428: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForAccountBalance(java.util.Map,
429: * boolean, boolean)
430: */
431: public Iterator findPendingLedgerEntriesForAccountBalance(
432: Map fieldValues, boolean isApproved) {
433: LOG
434: .debug("findPendingLedgerEntriesForAccountBalance() started");
435:
436: return generalLedgerPendingEntryDao
437: .findPendingLedgerEntriesForAccountBalance(fieldValues,
438: isApproved);
439: }
440:
441: /**
442: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntrySummaryForAccountBalance(java.util.Map,
443: * boolean, boolean)
444: */
445: public Iterator findPendingLedgerEntrySummaryForAccountBalance(
446: Map fieldValues, boolean isApproved) {
447: LOG
448: .debug("findPendingLedgerEntrySummaryForAccountBalance() started");
449:
450: return generalLedgerPendingEntryDao
451: .findPendingLedgerEntrySummaryForAccountBalance(
452: fieldValues, isApproved);
453: }
454:
455: public Collection findPendingEntries(Map fieldValues,
456: boolean isApproved) {
457: LOG.debug("findPendingEntries() started");
458:
459: return generalLedgerPendingEntryDao.findPendingEntries(
460: fieldValues, isApproved);
461: }
462:
463: public void setBalanceTypeService(
464: BalanceTypService balanceTypeService) {
465: this .balanceTypeService = balanceTypeService;
466: }
467:
468: public void setChartService(ChartService chartService) {
469: this .chartService = chartService;
470: }
471:
472: public void setGeneralLedgerPendingEntryDao(
473: GeneralLedgerPendingEntryDao generalLedgerPendingEntryDao) {
474: this .generalLedgerPendingEntryDao = generalLedgerPendingEntryDao;
475: }
476:
477: public void setParameterService(ParameterService parameterService) {
478: this .parameterService = parameterService;
479: }
480:
481: public void setKualiRuleService(KualiRuleService kualiRuleService) {
482: this .kualiRuleService = kualiRuleService;
483: }
484:
485: public void setOptionsService(OptionsService optionsService) {
486: this.optionsService = optionsService;
487: }
488: }
|