1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.businessobject.lookup;
17
18 import java.util.*;
19
20 import org.apache.commons.lang.ArrayUtils;
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.ole.coa.businessobject.Account;
23 import org.kuali.ole.coa.businessobject.ObjectCode;
24 import org.kuali.ole.gl.Constant;
25 import org.kuali.ole.gl.GeneralLedgerConstants;
26 import org.kuali.ole.gl.OJBUtility;
27 import org.kuali.ole.gl.batch.service.AccountBalanceCalculator;
28 import org.kuali.ole.gl.businessobject.AccountBalance;
29 import org.kuali.ole.gl.businessobject.TransientBalanceInquiryAttributes;
30 import org.kuali.ole.gl.businessobject.inquiry.AccountBalanceInquirableImpl;
31 import org.kuali.ole.gl.service.AccountBalanceService;
32 import org.kuali.ole.sys.OLEConstants;
33 import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
34 import org.kuali.ole.sys.businessobject.SystemOptions;
35 import org.kuali.ole.sys.context.SpringContext;
36 import org.kuali.ole.sys.service.OptionsService;
37 import org.kuali.rice.core.api.util.type.KualiDecimal;
38 import org.kuali.rice.kns.lookup.HtmlData;
39 import org.kuali.rice.krad.bo.BusinessObject;
40 import org.kuali.rice.krad.service.BusinessObjectService;
41 import org.kuali.rice.krad.util.ObjectUtils;
42
43
44
45
46 public class AccountBalanceLookupableHelperServiceImpl extends AbstractGeneralLedgerLookupableHelperServiceImpl {
47
48 private AccountBalanceCalculator postAccountBalance;
49 private AccountBalanceService accountBalanceService;
50 private OptionsService optionsService;
51
52
53
54
55
56
57
58
59 @Override
60 public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
61 return (new AccountBalanceInquirableImpl()).getInquiryUrl(bo, propertyName);
62 }
63
64
65
66
67
68
69
70
71
72
73 public List getSearchResults(Map fieldValues) {
74 setBackLocation((String) fieldValues.get(OLEConstants.BACK_LOCATION));
75 setDocFormKey((String) fieldValues.get(OLEConstants.DOC_FORM_KEY));
76
77 Collection searchResultsCollection = null;
78
79
80 String pendingEntryOption = this.getSelectedPendingEntryOption(fieldValues);
81
82
83 String consolidationOption = (String) fieldValues.get(GeneralLedgerConstants.DummyBusinessObject.CONSOLIDATION_OPTION);
84
85 boolean isConsolidated = isConsolidationSelected(fieldValues);
86
87
88
89 if (consolidationOption.equals(Constant.EXCLUDE_SUBACCOUNTS)){
90 fieldValues.put(Constant.SUB_ACCOUNT_OPTION, OLEConstants.getDashSubAccountNumber());
91 isConsolidated = false;
92 }
93
94 if (isConsolidated) {
95 Iterator availableBalanceIterator = accountBalanceService.findConsolidatedAvailableAccountBalance(fieldValues);
96 searchResultsCollection = buildConsolidedAvailableBalanceCollection(availableBalanceIterator);
97 }
98 else {
99 Iterator availableBalanceIterator = accountBalanceService.findAvailableAccountBalance(fieldValues);
100 searchResultsCollection = buildDetailedAvailableBalanceCollection(availableBalanceIterator);
101 }
102
103
104 updateByPendingLedgerEntry(searchResultsCollection, fieldValues, pendingEntryOption, isConsolidated, false);
105
106
107 for (Iterator iter = searchResultsCollection.iterator(); iter.hasNext();) {
108 AccountBalance ab = (AccountBalance) iter.next();
109 TransientBalanceInquiryAttributes dbo = ab.getDummyBusinessObject();
110 dbo.setConsolidationOption(consolidationOption);
111 dbo.setPendingEntryOption(pendingEntryOption);
112 }
113
114
115 Integer recordCount = accountBalanceService.getAvailableAccountBalanceCount(fieldValues, isConsolidated);
116 Long actualSize = OJBUtility.getResultActualSize(searchResultsCollection, recordCount, fieldValues, new AccountBalance());
117
118 SystemOptions option = getBusinessObjectService().findBySinglePrimaryKey(SystemOptions.class, Integer.parseInt((String)fieldValues.get(OLEConstants.FISCAL_YEAR)));
119 if(option != null){
120 if(searchResultsCollection.size() < 1) {
121 String accountNumber = fieldValues.get(OLEConstants.ACCOUNT_NUMBER).toString();
122 String chartCode = fieldValues.get(OLEConstants.CHART_CODE).toString();
123 if(!chartCode.equals("*") || !accountNumber.equals("*")){
124 List<Account> accountList = checkAccountEntry(accountNumber,chartCode);
125 for (Iterator<Account> accountIterator = accountList.iterator(); accountIterator.hasNext(); ) {
126 Account account = accountIterator.next();
127 AccountBalance balance = new AccountBalance();
128 balance.setChartOfAccountsCode(account.getChartOfAccountsCode());
129 balance.setAccountNumber(account.getAccountNumber());
130 String fiscalYear = fieldValues.get(OLEConstants.FISCAL_YEAR).toString();
131 balance.setUniversityFiscalYear(Integer.parseInt(fiscalYear));
132 balance.setObjectCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
133 balance.setSubAccountNumber(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
134 balance.setSubObjectCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
135 balance.setCurrentBudgetLineBalanceAmount(KualiDecimal.ZERO);
136 balance.setAccountLineActualsBalanceAmount(KualiDecimal.ZERO);
137 balance.setAccountLineEncumbranceBalanceAmount(KualiDecimal.ZERO);
138 searchResultsCollection.add(balance);
139 }
140 }
141 }
142 }
143 return this.buildSearchResultList(searchResultsCollection, actualSize);
144 }
145
146
147 public List<Account> checkAccountEntry(String accountNumber,String chartCode) {
148 boolean exists = false;
149 Map searchMap = new HashMap();
150 searchMap.put(OLEConstants.ACCOUNT_NUMBER, accountNumber);
151 searchMap.put(OLEConstants.CHART_CODE,chartCode);
152 List<Account> accountList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(Account.class, searchMap);
153 return accountList;
154 }
155
156
157
158
159
160
161 private Collection buildConsolidedAvailableBalanceCollection(Iterator iterator) {
162 Collection balanceCollection = new ArrayList();
163
164
165 while (iterator.hasNext()) {
166 Object avaiableAccountBalance = iterator.next();
167
168 if (avaiableAccountBalance.getClass().isArray()) {
169 int i = 0;
170 Object[] array = (Object[]) avaiableAccountBalance;
171 AccountBalance accountBalance = new AccountBalance();
172
173 accountBalance.setUniversityFiscalYear(new Integer(array[i++].toString()));
174 accountBalance.setChartOfAccountsCode(array[i++].toString());
175
176 accountBalance.setAccountNumber(array[i++].toString());
177 accountBalance.setSubAccountNumber(Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER);
178
179 accountBalance.setObjectCode(array[i++].toString());
180 accountBalance.setSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
181
182 String objectTypeCode = array[i++].toString();
183 accountBalance.getFinancialObject().setFinancialObjectTypeCode(objectTypeCode);
184
185 KualiDecimal budgetAmount = new KualiDecimal(array[i++].toString());
186 accountBalance.setCurrentBudgetLineBalanceAmount(budgetAmount);
187
188 KualiDecimal actualsAmount = new KualiDecimal(array[i++].toString());
189 accountBalance.setAccountLineActualsBalanceAmount(actualsAmount);
190
191 KualiDecimal encumbranceAmount = new KualiDecimal(array[i].toString());
192 accountBalance.setAccountLineEncumbranceBalanceAmount(encumbranceAmount);
193
194 KualiDecimal variance = calculateVariance(accountBalance);
195 accountBalance.getDummyBusinessObject().setGenericAmount(variance);
196
197 balanceCollection.add(accountBalance);
198 }
199 }
200 return balanceCollection;
201 }
202
203
204
205
206
207
208
209 private Collection buildDetailedAvailableBalanceCollection(Iterator iterator) {
210 Collection balanceCollection = new ArrayList();
211
212
213 while (iterator.hasNext()) {
214 AccountBalance accountBalance = (AccountBalance) iterator.next();
215
216 if (accountBalance.getDummyBusinessObject() == null) {
217 accountBalance.setDummyBusinessObject(new TransientBalanceInquiryAttributes());
218 }
219
220 KualiDecimal variance = calculateVariance(accountBalance);
221 accountBalance.getDummyBusinessObject().setGenericAmount(variance);
222
223 balanceCollection.add(accountBalance);
224 }
225 return balanceCollection;
226 }
227
228
229
230
231
232
233 private KualiDecimal calculateVariance(AccountBalance balance) {
234
235 KualiDecimal variance = new KualiDecimal(0.0);
236 KualiDecimal budgetAmount = balance.getCurrentBudgetLineBalanceAmount();
237 KualiDecimal actualsAmount = balance.getAccountLineActualsBalanceAmount();
238 KualiDecimal encumbranceAmount = balance.getAccountLineEncumbranceBalanceAmount();
239
240
241 if (ObjectUtils.isNull(balance.getFinancialObject()) || StringUtils.isBlank(balance.getFinancialObject().getFinancialObjectTypeCode())) {
242 balance.refreshReferenceObject("financialObject");
243 }
244 ObjectCode financialObject = balance.getFinancialObject();
245 String objectTypeCode = (financialObject == null) ? Constant.EMPTY_STRING : financialObject.getFinancialObjectTypeCode();
246
247 SystemOptions options = getOptionsService().getOptions(balance.getUniversityFiscalYear());
248 if (ObjectUtils.isNull(options)) {
249 options = getOptionsService().getCurrentYearOptions();
250 }
251 String[] objectTypeCodeList = new String[3];
252 objectTypeCodeList[0] = options.getFinObjTypeExpendNotExpCode();
253 objectTypeCodeList[1] = options.getFinObjTypeExpNotExpendCode();
254 objectTypeCodeList[2] = options.getFinObjTypeExpenditureexpCd();
255
256 boolean isObjectTypeCodeInList = ArrayUtils.contains(objectTypeCodeList, objectTypeCode);
257
258
259 if (isObjectTypeCodeInList) {
260 variance = budgetAmount.subtract(actualsAmount);
261 variance = variance.subtract(encumbranceAmount);
262 }
263 else {
264 variance = actualsAmount.subtract(budgetAmount);
265 }
266 return variance;
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280 @Override
281 protected void updateEntryCollection(Collection entryCollection, Map fieldValues, boolean isApproved, boolean isConsolidated, boolean isCostShareExcluded) {
282
283
284 Map pendingEntryFieldValues = BusinessObjectFieldConverter.convertToTransactionFieldValues(fieldValues);
285
286
287 Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService().findPendingLedgerEntriesForAccountBalance(pendingEntryFieldValues, isApproved);
288 while (pendingEntryIterator.hasNext()) {
289 GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator.next();
290
291 if (isCostShareExcluded) {
292 if (ObjectUtils.isNotNull(pendingEntry.getSubAccount()) && ObjectUtils.isNotNull(pendingEntry.getSubAccount().getA21SubAccount())) {
293 if (OLEConstants.SubAccountType.COST_SHARE.equals(pendingEntry.getSubAccount().getA21SubAccount().getSubAccountTypeCode())) {
294
295 continue;
296 }
297 }
298 }
299
300
301 if (isConsolidated) {
302 pendingEntry.setSubAccountNumber(Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER);
303 pendingEntry.setFinancialSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
304 pendingEntry.setFinancialObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
305 }
306
307 AccountBalance accountBalance = postAccountBalance.findAccountBalance(entryCollection, pendingEntry);
308 postAccountBalance.updateAccountBalance(pendingEntry, accountBalance);
309
310
311 if (accountBalance.getDummyBusinessObject() == null) {
312 accountBalance.setDummyBusinessObject(new TransientBalanceInquiryAttributes());
313 }
314 KualiDecimal variance = calculateVariance(accountBalance);
315 accountBalance.getDummyBusinessObject().setGenericAmount(variance);
316 }
317 }
318
319
320
321
322
323
324 public void setPostAccountBalance(AccountBalanceCalculator postAccountBalance) {
325 this.postAccountBalance = postAccountBalance;
326 }
327
328
329
330
331
332
333 public void setAccountBalanceService(AccountBalanceService accountBalanceService) {
334 this.accountBalanceService = accountBalanceService;
335 }
336
337
338
339
340
341
342 public void setOptionsService(OptionsService optionsService) {
343 this.optionsService = optionsService;
344 }
345
346
347
348
349
350 public OptionsService getOptionsService() {
351 return optionsService;
352 }
353 }