1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch.service.impl;
17
18 import java.util.Collection;
19 import java.util.Date;
20 import java.util.Iterator;
21
22 import org.kuali.ole.gl.GeneralLedgerConstants;
23 import org.kuali.ole.gl.batch.service.AccountBalanceCalculator;
24 import org.kuali.ole.gl.batch.service.AccountingCycleCachingService;
25 import org.kuali.ole.gl.batch.service.PostTransaction;
26 import org.kuali.ole.gl.businessobject.AccountBalance;
27 import org.kuali.ole.gl.businessobject.Transaction;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.service.ReportWriterService;
30 import org.kuali.rice.krad.service.PersistenceStructureService;
31 import org.springframework.transaction.annotation.Transactional;
32
33 @Transactional
34 public class PostAccountBalance implements PostTransaction, AccountBalanceCalculator {
35 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostAccountBalance.class);
36
37 private AccountingCycleCachingService accountingCycleCachingService;
38 private PersistenceStructureService persistenceStructureService;
39
40
41
42
43 public PostAccountBalance() {
44 super();
45 }
46
47
48
49
50
51
52
53
54
55
56
57 public String post(Transaction t, int mode, Date postDate, ReportWriterService posterReportWriterService) {
58 LOG.debug("post() started");
59
60
61
62
63 if ((t.getFinancialBalanceTypeCode().equals(t.getOption().getActualFinancialBalanceTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getBudgetCheckingBalanceTypeCd())) || (t.getFinancialBalanceTypeCode().equals(t.getOption().getExtrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getIntrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getPreencumbranceFinBalTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getCostShareEncumbranceBalanceTypeCd())) && (!t.getFinancialObjectTypeCode().equals(t.getOption().getFinObjectTypeFundBalanceCd()))) {
64
65 String returnCode = GeneralLedgerConstants.UPDATE_CODE;
66
67
68 AccountBalance ab = accountingCycleCachingService.getAccountBalance(t);
69
70 if (ab == null) {
71 returnCode = GeneralLedgerConstants.INSERT_CODE;
72 ab = new AccountBalance(t);
73 }
74
75 ab.setTimestamp(new java.sql.Date(postDate.getTime()));
76
77 if (!updateAccountBalanceReturn(t, ab)) {
78 return GeneralLedgerConstants.EMPTY_CODE;
79 }
80
81 if (returnCode.equals(GeneralLedgerConstants.INSERT_CODE)) {
82 accountingCycleCachingService.insertAccountBalance(ab);
83 } else {
84 accountingCycleCachingService.updateAccountBalance(ab);
85 }
86
87 return returnCode;
88 }
89 else {
90 return GeneralLedgerConstants.EMPTY_CODE;
91 }
92 }
93
94 public AccountBalance findAccountBalance(Collection balanceList, Transaction t) {
95
96
97 for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
98 AccountBalance b = (AccountBalance) iter.next();
99
100 if (b.getUniversityFiscalYear().equals(t.getUniversityFiscalYear()) && b.getChartOfAccountsCode().equals(t.getChartOfAccountsCode()) && b.getAccountNumber().equals(t.getAccountNumber()) && b.getSubAccountNumber().equals(t.getSubAccountNumber()) && b.getObjectCode().equals(t.getFinancialObjectCode()) && b.getSubObjectCode().equals(t.getFinancialSubObjectCode())) {
101 return b;
102 }
103 }
104
105
106 AccountBalance b = new AccountBalance(t);
107 balanceList.add(b);
108
109 return b;
110 }
111
112 protected boolean updateAccountBalanceReturn(Transaction t, AccountBalance ab) {
113 if (t.getFinancialBalanceTypeCode().equals(t.getOption().getBudgetCheckingBalanceTypeCd())) {
114 ab.setCurrentBudgetLineBalanceAmount(ab.getCurrentBudgetLineBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
115 }
116 else if (t.getFinancialBalanceTypeCode().equals(t.getOption().getActualFinancialBalanceTypeCd())) {
117 if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && OLEConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
118 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
119 }
120 else {
121 ab.setAccountLineActualsBalanceAmount(ab.getAccountLineActualsBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
122 }
123 }
124 else if (t.getFinancialBalanceTypeCode().equals(t.getOption().getExtrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getIntrnlEncumFinBalanceTypCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getPreencumbranceFinBalTypeCd()) || t.getFinancialBalanceTypeCode().equals(t.getOption().getCostShareEncumbranceBalanceTypeCd())) {
125 if (t.getObjectType().getFinObjectTypeDebitcreditCd().equals(t.getTransactionDebitCreditCode()) || ((!t.getBalanceType().isFinancialOffsetGenerationIndicator()) && OLEConstants.GL_BUDGET_CODE.equals(t.getTransactionDebitCreditCode()))) {
126 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().add(t.getTransactionLedgerEntryAmount()));
127 }
128 else {
129 ab.setAccountLineEncumbranceBalanceAmount(ab.getAccountLineEncumbranceBalanceAmount().subtract(t.getTransactionLedgerEntryAmount()));
130 }
131 }
132 else {
133 return false;
134 }
135 return true;
136 }
137
138
139
140
141
142 public void updateAccountBalance(Transaction t, AccountBalance ab) {
143 updateAccountBalanceReturn(t, ab);
144 }
145
146 public String getDestinationName() {
147 return persistenceStructureService.getTableName(AccountBalance.class);
148 }
149
150 public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
151 this.accountingCycleCachingService = accountingCycleCachingService;
152 }
153
154 public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
155 this.persistenceStructureService = persistenceStructureService;
156 }
157 }