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.batch.service.AccountingCycleCachingService;
23 import org.kuali.ole.gl.batch.service.BalanceCalculator;
24 import org.kuali.ole.gl.batch.service.PostTransaction;
25 import org.kuali.ole.gl.businessobject.Balance;
26 import org.kuali.ole.gl.businessobject.Transaction;
27 import org.kuali.ole.sys.businessobject.UniversityDate;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.service.ReportWriterService;
30 import org.kuali.ole.sys.service.UniversityDateService;
31 import org.kuali.rice.core.api.util.type.KualiDecimal;
32 import org.springframework.transaction.annotation.Transactional;
33
34
35
36
37 @Transactional
38 public class PostBalance implements PostTransaction, BalanceCalculator {
39 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostBalance.class);
40
41 private AccountingCycleCachingService accountingCycleCachingService;
42 private static final KualiDecimal NEGATIVE_ONE = new KualiDecimal(-1);
43
44
45
46 public PostBalance() {
47 super();
48 }
49
50
51
52
53
54
55
56
57
58
59
60 public String post(Transaction t, int mode, Date postDate, ReportWriterService posterReportWriterService) {
61 LOG.debug("post() started");
62
63 String postType = "U";
64
65 KualiDecimal amount = t.getTransactionLedgerEntryAmount();
66
67
68
69 if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
70 if (!t.getTransactionDebitCreditCode().equals(t.getObjectType().getFinObjectTypeDebitcreditCd())) {
71 amount = amount.multiply(NEGATIVE_ONE);
72 }
73 }
74
75 Balance b = accountingCycleCachingService.getBalance(t);
76 if (b == null) {
77 postType = "I";
78 b = new Balance(t);
79 }
80 String period = t.getUniversityFiscalPeriodCode();
81 b.addAmount(period, amount);
82
83 if (postType.equals("I")) {
84 accountingCycleCachingService.insertBalance(b);
85 } else {
86 accountingCycleCachingService.updateBalance(b);
87 }
88
89 return postType;
90 }
91
92
93
94
95
96
97
98
99
100 public Balance findBalance(Collection balanceList, Transaction t) {
101
102 for (Iterator iter = balanceList.iterator(); iter.hasNext();) {
103 Balance b = (Balance) iter.next();
104
105 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()) && b.getBalanceTypeCode().equals(t.getFinancialBalanceTypeCode()) && b.getObjectTypeCode().equals(t.getFinancialObjectTypeCode())) {
106 return b;
107 }
108 }
109
110
111 Balance b = new Balance(t);
112 balanceList.add(b);
113
114 return b;
115 }
116
117
118
119
120
121 public void updateBalance(Transaction t, Balance b) {
122
123
124
125
126 KualiDecimal amount = t.getTransactionLedgerEntryAmount();
127 if (amount == null) {
128 amount = KualiDecimal.ZERO;
129 }
130
131 if (t.getObjectType() == null) {
132 LOG.error("updateBalance() Invalid object type (" + t.getFinancialObjectTypeCode() + ") in pending table");
133 return;
134 }
135
136 if (t.getBalanceType() == null) {
137 LOG.error("updateBalance() Invalid balance type (" + t.getFinancialBalanceTypeCode() + ") in pending table");
138 return;
139 }
140
141
142
143 if (t.getBalanceType().isFinancialOffsetGenerationIndicator()) {
144 if (!t.getTransactionDebitCreditCode().equals(t.getObjectType().getFinObjectTypeDebitcreditCd())) {
145 amount = amount.multiply(new KualiDecimal(-1));
146 }
147 }
148
149
150 String period = t.getUniversityFiscalPeriodCode();
151 if (period == null) {
152 UniversityDate currentUniversityDate = SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate();
153 period = currentUniversityDate.getUniversityFiscalAccountingPeriod();
154 }
155
156 b.addAmount(period, amount);
157 }
158
159
160
161
162 public String getDestinationName() {
163 return "GL_BALANCE_T";
164 }
165
166 public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
167 this.accountingCycleCachingService = accountingCycleCachingService;
168 }
169 }