1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.collections.IteratorUtils;
27 import org.kuali.ole.coa.businessobject.Account;
28 import org.kuali.ole.coa.businessobject.OrganizationReversion;
29 import org.kuali.ole.coa.service.BalanceTypeService;
30 import org.kuali.ole.coa.service.ObjectTypeService;
31 import org.kuali.ole.coa.service.SubFundGroupService;
32 import org.kuali.ole.gl.GeneralLedgerConstants;
33 import org.kuali.ole.gl.OJBUtility;
34 import org.kuali.ole.gl.batch.BalanceForwardStep;
35 import org.kuali.ole.gl.batch.service.FilteringBalanceIterator;
36 import org.kuali.ole.gl.businessobject.Balance;
37 import org.kuali.ole.gl.businessobject.GlSummary;
38 import org.kuali.ole.gl.dataaccess.BalanceDao;
39 import org.kuali.ole.gl.service.BalanceService;
40 import org.kuali.ole.sys.OLEConstants;
41 import org.kuali.ole.sys.OLEPropertyConstants;
42 import org.kuali.ole.sys.businessobject.SystemOptions;
43 import org.kuali.ole.sys.context.SpringContext;
44 import org.kuali.ole.sys.service.OptionsService;
45 import org.kuali.ole.sys.service.UniversityDateService;
46 import org.kuali.rice.core.api.parameter.ParameterEvaluator;
47 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
48 import org.kuali.rice.core.api.util.type.KualiDecimal;
49 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
50 import org.springframework.transaction.annotation.Transactional;
51
52
53
54
55 @Transactional
56 public class BalanceServiceImpl implements BalanceService {
57 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BalanceServiceImpl.class);
58
59 protected static final String PARAMETER_PREFIX = "SELECTION_";
60
61 protected BalanceDao balanceDao;
62 protected OptionsService optionsService;
63 protected ObjectTypeService objectTypeService;
64 protected SubFundGroupService subFundGroupService;
65 protected ParameterService parameterService;
66 protected BalanceTypeService balanceTypService;
67
68
69 Collection<String> assetLiabilityFundBalanceObjectTypeCodes = null;
70 Collection<String> encumbranceBaseBudgetBalanceTypeCodes = null;
71 Collection<String> actualBalanceCodes = null;
72 Collection<String> incomeObjectTypeCodes = null;
73 Collection<String> expenseObjectTypeCodes = null;
74
75
76
77
78
79
80
81 public List<GlSummary> getGlSummary(int universityFiscalYear, List<String> balanceTypeCodes) {
82 LOG.debug("getGlSummary() started");
83
84 List<GlSummary> sum = new ArrayList<GlSummary>();
85
86 Iterator<Object[]> i = balanceDao.getGlSummary(universityFiscalYear, balanceTypeCodes);
87 while (i.hasNext()) {
88 Object[] data = i.next();
89 sum.add(new GlSummary(data));
90 }
91 return sum;
92 }
93
94
95
96
97
98
99
100
101 public Iterator<Balance> findBalancesForFiscalYear(Integer fiscalYear) {
102 return (Iterator<Balance>) balanceDao.findBalancesForFiscalYear(fiscalYear);
103 }
104
105
106
107
108
109
110
111
112 public boolean hasAssetLiabilityFundBalanceBalances(Account account) {
113
114
115
116
117
118
119
120
121
122
123 Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
124 ArrayList fundBalanceObjectCodes = new ArrayList();
125 fundBalanceObjectCodes.add(null == account.getChartOfAccounts() ? null : account.getChartOfAccounts().getFundBalanceObjectCode());
126 Iterator balances = balanceDao.findBalances(account, fiscalYear, null, fundBalanceObjectCodes, getAssetLiabilityFundBalanceBalanceTypeCodes(), getActualBalanceCodes());
127
128 KualiDecimal begin;
129 KualiDecimal annual;
130
131
132
133
134 Map groups = new HashMap();
135
136 while (balances.hasNext()) {
137 Balance balance = (Balance) balances.next();
138 begin = balance.getBeginningBalanceLineAmount();
139 annual = balance.getAccountLineAnnualBalanceAmount();
140
141 String objectCode = balance.getObjectCode();
142
143 KualiDecimal runningTotal = (KualiDecimal) groups.get(objectCode);
144
145 if (runningTotal == null) {
146 runningTotal = KualiDecimal.ZERO;
147 }
148
149 runningTotal = runningTotal.add(begin);
150 runningTotal = runningTotal.add(annual);
151
152 groups.put(objectCode, runningTotal);
153
154
155 }
156
157 boolean success = false;
158
159 Iterator iter = groups.keySet().iterator();
160 while (iter.hasNext()) {
161 success |= ((KualiDecimal) groups.get(iter.next())).isNonZero();
162 }
163
164 return success;
165
166 }
167
168
169
170
171
172
173
174
175 protected KualiDecimal sumBalances(Iterator balances) {
176 KualiDecimal runningTotal = KualiDecimal.ZERO;
177
178 KualiDecimal begin;
179 KualiDecimal annual;
180
181 while (balances.hasNext()) {
182 Balance balance = (Balance) balances.next();
183 begin = balance.getBeginningBalanceLineAmount();
184 annual = balance.getAccountLineAnnualBalanceAmount();
185
186 runningTotal = runningTotal.add(begin);
187 runningTotal = runningTotal.add(annual);
188 }
189
190 return runningTotal;
191
192 }
193
194
195
196
197
198
199
200 protected KualiDecimal incomeBalances(Account account) {
201
202
203
204
205
206
207
208
209
210 Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
211
212 ArrayList fundBalanceObjectCodes = new ArrayList();
213 fundBalanceObjectCodes.add(account.getChartOfAccounts().getFundBalanceObjectCode());
214 Iterator balances = balanceDao.findBalances(account, fiscalYear, fundBalanceObjectCodes, null, getIncomeObjectTypeCodes(), getActualBalanceCodes());
215
216 return sumBalances(balances);
217
218 }
219
220
221
222
223
224
225
226 protected KualiDecimal expenseBalances(Account account) {
227
228
229
230
231
232
233
234 Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
235 Iterator balances = balanceDao.findBalances(account, fiscalYear, null, null, getExpenseObjectTypeCodes(), getActualBalanceCodes());
236
237 return sumBalances(balances);
238
239 }
240
241
242
243
244
245
246
247
248 public boolean fundBalanceWillNetToZero(Account account) {
249 KualiDecimal income = incomeBalances(account);
250 KualiDecimal expense = expenseBalances(account);
251
252 return income.equals(expense);
253 }
254
255
256
257
258
259
260
261
262
263
264 public boolean hasEncumbrancesOrBaseBudgets(Account account) {
265
266
267
268
269
270
271
272
273
274 Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
275 Iterator balances = balanceDao.findBalances(account, fiscalYear, null, null, null, getEncumbranceBaseBudgetBalanceTypeCodes());
276
277 return sumBalances(balances).isNonZero();
278 }
279
280
281
282
283
284
285
286
287
288 public boolean beginningBalanceLoaded(Account account) {
289 return optionsService.getCurrentYearOptions().isFinancialBeginBalanceLoadInd();
290 }
291
292
293
294
295
296
297
298
299 public boolean hasAssetLiabilityOrFundBalance(Account account) {
300 return hasAssetLiabilityFundBalanceBalances(account) || !fundBalanceWillNetToZero(account) || hasEncumbrancesOrBaseBudgets(account);
301 }
302
303 public void setBalanceDao(BalanceDao balanceDao) {
304 this.balanceDao = balanceDao;
305 }
306
307 public void setOptionsService(OptionsService optionsService) {
308 this.optionsService = optionsService;
309 }
310
311
312
313
314
315
316
317
318
319
320 public Iterator lookupCashBalance(Map fieldValues, boolean isConsolidated) {
321 LOG.debug("findCashBalance() started");
322
323 return balanceDao.lookupCashBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues));
324 }
325
326
327
328
329
330
331
332
333
334 public Integer getCashBalanceRecordCount(Map fieldValues, boolean isConsolidated) {
335 LOG.debug("getCashBalanceRecordCount() started");
336
337 Integer recordCount = new Integer(0);
338 if (!isConsolidated) {
339 recordCount = balanceDao.getDetailedCashBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
340 }
341 else {
342 recordCount = balanceDao.getConsolidatedCashBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
343 }
344 return recordCount;
345 }
346
347
348
349
350
351
352
353
354
355 public Iterator findBalance(Map fieldValues, boolean isConsolidated) {
356 LOG.debug("findBalance() started");
357 return balanceDao.findBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues));
358 }
359
360
361
362
363
364
365
366
367
368 public Integer getBalanceRecordCount(Map fieldValues, boolean isConsolidated) {
369 LOG.debug("getBalanceRecordCount() started");
370
371 Integer recordCount = null;
372 if (!isConsolidated) {
373 recordCount = OJBUtility.getResultSizeFromMap(fieldValues, new Balance()).intValue();
374 }
375 else {
376 Iterator recordCountIterator = balanceDao.getConsolidatedBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
377
378 List recordCountList = IteratorUtils.toList(recordCountIterator);
379 recordCount = recordCountList.size();
380 }
381 return recordCount;
382 }
383
384
385
386
387
388
389
390 public void purgeYearByChart(String chart, int year) {
391 LOG.debug("purgeYearByChart() started");
392
393 balanceDao.purgeYearByChart(chart, year);
394 }
395
396
397
398
399 protected void loadConstantsFromOptions() {
400 LOG.debug("loadConstantsFromOptions() started");
401 SystemOptions options = optionsService.getCurrentYearOptions();
402
403 actualBalanceCodes = Arrays.asList( options.getActualFinancialBalanceTypeCd() );
404
405 incomeObjectTypeCodes = Arrays.asList( options.getFinObjTypeIncomeNotCashCd(),
406 options.getFinObjectTypeIncomecashCode(),
407 options.getFinObjTypeCshNotIncomeCd(),
408 options.getFinancialObjectTypeTransferIncomeCd()
409 );
410
411 expenseObjectTypeCodes = Arrays.asList( options.getFinObjTypeExpendNotExpCode(),
412 options.getFinObjTypeExpenditureexpCd(),
413 options.getFinObjTypeExpNotExpendCode(),
414 options.getFinancialObjectTypeTransferExpenseCd()
415 );
416
417 assetLiabilityFundBalanceObjectTypeCodes = Arrays.asList( options.getFinancialObjectTypeAssetsCd(),
418 options.getFinObjectTypeLiabilitiesCode(),
419 options.getFinObjectTypeFundBalanceCd()
420 );
421
422 encumbranceBaseBudgetBalanceTypeCodes = Arrays.asList( options.getExtrnlEncumFinBalanceTypCd(),
423 options.getIntrnlEncumFinBalanceTypCd(),
424 options.getPreencumbranceFinBalTypeCd(),
425 options.getBaseBudgetFinancialBalanceTypeCd()
426 );
427 }
428
429
430
431
432
433
434 protected Collection<String> getActualBalanceCodes() {
435 if (actualBalanceCodes == null) {
436 loadConstantsFromOptions();
437 }
438 return actualBalanceCodes;
439 }
440
441
442
443
444
445
446 protected Collection<String> getIncomeObjectTypeCodes() {
447 if (incomeObjectTypeCodes == null) {
448 loadConstantsFromOptions();
449 }
450 return incomeObjectTypeCodes;
451 }
452
453
454
455
456
457
458 protected Collection<String> getExpenseObjectTypeCodes() {
459 if (expenseObjectTypeCodes == null) {
460 loadConstantsFromOptions();
461 }
462 return expenseObjectTypeCodes;
463 }
464
465
466
467
468
469
470 protected Collection<String> getAssetLiabilityFundBalanceBalanceTypeCodes() {
471 if (assetLiabilityFundBalanceObjectTypeCodes == null) {
472 loadConstantsFromOptions();
473 }
474 return assetLiabilityFundBalanceObjectTypeCodes;
475 }
476
477
478
479
480
481
482 protected Collection<String> getEncumbranceBaseBudgetBalanceTypeCodes() {
483 if (encumbranceBaseBudgetBalanceTypeCodes == null) {
484 loadConstantsFromOptions();
485 }
486 return encumbranceBaseBudgetBalanceTypeCodes;
487 }
488
489
490
491
492
493
494
495
496 public int countBalancesForFiscalYear(Integer year) {
497 return balanceDao.countBalancesForFiscalYear(year);
498 }
499
500
501
502
503
504
505
506
507 public Iterator<Balance> findNominalActivityBalancesForFiscalYear(Integer year) {
508
509 List<String> nominalActivityObjectTypeCodes = objectTypeService.getNominalActivityClosingAllowedObjectTypes(year);
510 SystemOptions currentYearOptions = optionsService.getCurrentYearOptions();
511 return balanceDao.findNominalActivityBalancesForFiscalYear(year, nominalActivityObjectTypeCodes, currentYearOptions);
512 }
513
514
515
516
517
518
519
520
521 public Iterator<Balance> findCumulativeBalancesToForwardForFiscalYear(Integer year) {
522 List<String> cumulativeForwardBalanceObjectTypes = objectTypeService.getCumulativeForwardBalanceObjectTypes(year);
523 Collection<String> contractsAndGrantsDenotingValues = subFundGroupService.getContractsAndGrantsDenotingValues();
524 Collection<String> subFundGroupsForCumulativeBalanceForwardingArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.SUB_FUND_GROUPS_FOR_INCEPTION_TO_DATE_REPORTING);
525 Collection<String> cumulativeBalanceForwardBalanceTypesArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.BALANCE_TYPES_TO_ROLL_FORWARD_FOR_INCOME_EXPENSE);
526 boolean fundGroupDenotesCGInd = parameterService.getParameterValueAsBoolean(Account.class, OLEConstants.ChartApcParms.ACCOUNT_FUND_GROUP_DENOTES_CG);
527 Iterator<Balance> balances = balanceDao.findCumulativeBalancesToForwardForFiscalYear(year, cumulativeForwardBalanceObjectTypes, contractsAndGrantsDenotingValues, subFundGroupsForCumulativeBalanceForwardingArray, cumulativeBalanceForwardBalanceTypesArray, fundGroupDenotesCGInd);
528
529 FilteringBalanceIterator filteredBalances = SpringContext.getBean(FilteringBalanceIterator.class, "glBalanceAnnualAndCGTotalNotZeroIterator");
530 filteredBalances.setBalancesSource(balances);
531
532 return filteredBalances;
533 }
534
535
536
537
538
539
540
541
542 public Iterator<Balance> findGeneralBalancesToForwardForFiscalYear(Integer year) {
543 List<String> generalForwardBalanceObjectTypes = objectTypeService.getGeneralForwardBalanceObjectTypes(year);
544 Collection<String> generalBalanceForwardBalanceTypesArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.BALANCE_TYPES_TO_ROLL_FORWARD_FOR_BALANCE_SHEET);
545 Iterator<Balance> balances = balanceDao.findGeneralBalancesToForwardForFiscalYear(year, generalForwardBalanceObjectTypes, generalBalanceForwardBalanceTypesArray);
546
547 Map<String, FilteringBalanceIterator> balanceIterators = SpringContext.getBeansOfType(FilteringBalanceIterator.class);
548 FilteringBalanceIterator filteredBalances = balanceIterators.get("glBalanceTotalNotZeroIterator");
549 filteredBalances.setBalancesSource(balances);
550
551 return filteredBalances;
552 }
553
554
555
556
557
558
559
560
561
562
563 public Iterator<Balance> findOrganizationReversionBalancesForFiscalYear(Integer year, boolean endOfYear) {
564 SystemOptions options = SpringContext.getBean(OptionsService.class).getOptions(year);
565 List<ParameterEvaluator> parameterEvaluators = new ArrayList<ParameterEvaluator>();
566
567 int i = 1;
568 boolean moreParams = true;
569 while (moreParams) {
570 if (parameterService.parameterExists(OrganizationReversion.class, PARAMETER_PREFIX + i)) {
571 ParameterEvaluator parameterEvaluator =
572 parameterEvaluators.add(parameterEvaluator);
573 }
574 else {
575 moreParams = false;
576 }
577 i++;
578 }
579 return balanceDao.findOrganizationReversionBalancesForFiscalYear(year, endOfYear, options, parameterEvaluators);
580 }
581
582
583
584
585
586
587
588 protected List<String> getEncumbranceBalanceTypes(Map fieldValues) {
589
590 List<String> encumbranceBalanceTypes = null;
591 if (fieldValues.containsKey(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
592
593 String universityFiscalYearStr = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
594 Integer universityFiscalYear = new Integer(universityFiscalYearStr);
595 encumbranceBalanceTypes = balanceTypService.getEncumbranceBalanceTypes(universityFiscalYear);
596 }
597 return encumbranceBalanceTypes;
598 }
599
600
601
602
603
604
605 public void setObjectTypeService(ObjectTypeService objectTypeService) {
606 this.objectTypeService = objectTypeService;
607 }
608
609
610
611
612
613
614 public void setSubFundGroupService(SubFundGroupService subFundGroupService) {
615 this.subFundGroupService = subFundGroupService;
616 }
617
618
619
620
621
622
623 public void setParameterService(ParameterService parameterService) {
624 this.parameterService = parameterService;
625 }
626
627
628
629
630
631
632 public void setBalanceTypService(BalanceTypeService balanceTypService) {
633 this.balanceTypService = balanceTypService;
634 }
635
636 }