1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.document.web.struts;
17
18 import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME;
19 import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME;
20
21 import java.sql.Date;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.kuali.ole.coa.businessobject.AccountingPeriod;
30 import org.kuali.ole.coa.businessobject.ObjectCode;
31 import org.kuali.ole.coa.businessobject.SubObjectCode;
32 import org.kuali.ole.coa.service.AccountingPeriodService;
33 import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelper;
34 import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelperBase;
35 import org.kuali.ole.fp.document.VoucherDocument;
36 import org.kuali.ole.sys.OLEConstants;
37 import org.kuali.ole.sys.OLEKeyConstants;
38 import org.kuali.ole.sys.OLEPropertyConstants;
39 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
40 import org.kuali.ole.sys.context.SpringContext;
41 import org.kuali.ole.sys.document.AmountTotaling;
42 import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
43 import org.kuali.rice.core.api.datetime.DateTimeService;
44 import org.kuali.rice.core.api.util.type.KualiDecimal;
45 import org.kuali.rice.core.web.format.CurrencyFormatter;
46 import org.kuali.rice.krad.util.GlobalVariables;
47 import org.kuali.rice.krad.util.ObjectUtils;
48
49
50
51
52
53
54
55
56
57 public class VoucherForm extends KualiAccountingDocumentFormBase {
58 protected List accountingPeriods;
59 protected KualiDecimal newSourceLineDebit;
60 protected KualiDecimal newSourceLineCredit;
61 protected List voucherLineHelpers;
62 protected String selectedAccountingPeriod;
63
64
65
66
67 public VoucherForm() {
68 populateDefaultSelectedAccountingPeriod();
69 setNewSourceLineCredit(KualiDecimal.ZERO);
70 setNewSourceLineDebit(KualiDecimal.ZERO);
71 setVoucherLineHelpers(new ArrayList());
72 }
73
74
75
76
77 public void populateDefaultSelectedAccountingPeriod() {
78 Date date = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
79 AccountingPeriod accountingPeriod = SpringContext.getBean(AccountingPeriodService.class).getByDate(date);
80
81 StringBuffer sb = new StringBuffer();
82 sb.append(accountingPeriod.getUniversityFiscalPeriodCode());
83 sb.append(accountingPeriod.getUniversityFiscalYear());
84
85 setSelectedAccountingPeriod(sb.toString());
86 }
87
88
89
90
91
92
93
94
95 @Override
96 public void populate(HttpServletRequest request) {
97 super.populate(request);
98
99
100 if (OLEConstants.RETURN_METHOD_TO_CALL.equals(getMethodToCall())) {
101 String selectedPeriod = (StringUtils.defaultString(request.getParameter(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE)) + StringUtils.defaultString(request.getParameter(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)));
102 if (StringUtils.isNotBlank(selectedPeriod)) {
103 setSelectedAccountingPeriod(selectedPeriod);
104 }
105 }
106 populateAccountingPeriodListForRendering();
107
108
109 if (StringUtils.isBlank(getMethodToCall()) || !getMethodToCall().equals(OLEConstants.RELOAD_METHOD_TO_CALL)) {
110
111 populateCreditAndDebitAmounts();
112 }
113 }
114
115
116
117
118
119
120
121 protected Integer getSelectedPostingYear() {
122 Integer postingYear = null;
123 if (StringUtils.isNotBlank(getSelectedAccountingPeriod())) {
124 postingYear = new Integer(StringUtils.right(getSelectedAccountingPeriod(), 4));
125 }
126 return postingYear;
127 }
128
129
130
131
132
133
134 protected String getSelectedPostingPeriodCode() {
135 String periodCode = null;
136 String selectedPeriod = getSelectedAccountingPeriod();
137 if (StringUtils.isNotBlank(selectedPeriod)) {
138 periodCode = StringUtils.left(selectedPeriod, 2);
139 }
140 return periodCode;
141 }
142
143
144
145
146
147
148 public VoucherDocument getVoucherDocument() {
149 return (VoucherDocument) getDocument();
150 }
151
152
153
154
155
156
157
158
159 @Override
160 public void populateSourceAccountingLine(SourceAccountingLine sourceLine, String accountingLinePropertyName, Map parameterMap) {
161 super.populateSourceAccountingLine(sourceLine, accountingLinePropertyName, parameterMap);
162
163
164 String selectedAccountingPeriod = getSelectedAccountingPeriod();
165
166 if (StringUtils.isNotBlank(selectedAccountingPeriod)) {
167 Integer postingYear = getSelectedPostingYear();
168 sourceLine.setPostingYear(postingYear);
169
170 if (ObjectUtils.isNull(sourceLine.getObjectCode())) {
171 sourceLine.setObjectCode(new ObjectCode());
172 }
173 sourceLine.getObjectCode().setUniversityFiscalYear(postingYear);
174
175 if (ObjectUtils.isNull(sourceLine.getSubObjectCode())) {
176 sourceLine.setSubObjectCode(new SubObjectCode());
177 }
178 sourceLine.getSubObjectCode().setUniversityFiscalYear(postingYear);
179 }
180
181 }
182
183
184
185
186
187
188 public List getAccountingPeriods() {
189 return accountingPeriods;
190 }
191
192
193
194
195
196
197 public void setAccountingPeriods(List accountingPeriods) {
198 this.accountingPeriods = accountingPeriods;
199 }
200
201
202
203
204
205
206 public String getFormattedReversalDate() {
207 return formatReversalDate(getVoucherDocument().getReversalDate());
208 }
209
210
211
212
213
214
215 public String getSelectedAccountingPeriod() {
216 return selectedAccountingPeriod;
217 }
218
219
220
221
222 public AccountingPeriod getAccountingPeriod() {
223 AccountingPeriod period = null;
224
225 if (!StringUtils.isBlank(getSelectedAccountingPeriod())) {
226 period = SpringContext.getBean(AccountingPeriodService.class).getByPeriod(getSelectedPostingPeriodCode(), getSelectedPostingYear());
227 }
228
229 return period;
230 }
231
232
233
234
235
236
237 public void setSelectedAccountingPeriod(String selectedAccountingPeriod) {
238 this.selectedAccountingPeriod = selectedAccountingPeriod;
239 }
240
241
242
243
244
245
246
247 public List getVoucherLineHelpers() {
248 return voucherLineHelpers;
249 }
250
251
252
253
254
255
256
257
258 public VoucherAccountingLineHelper getVoucherLineHelper(int index) {
259 while (getVoucherLineHelpers().size() <= index) {
260 getVoucherLineHelpers().add(new VoucherAccountingLineHelperBase());
261 }
262 return (VoucherAccountingLineHelper) getVoucherLineHelpers().get(index);
263 }
264
265
266
267
268
269
270 public void setVoucherLineHelpers(List voucherLineHelpers) {
271 this.voucherLineHelpers = voucherLineHelpers;
272 }
273
274
275
276
277
278
279 public KualiDecimal getNewSourceLineCredit() {
280 return newSourceLineCredit;
281 }
282
283
284
285
286
287
288 public void setNewSourceLineCredit(KualiDecimal newSourceLineCredit) {
289 this.newSourceLineCredit = newSourceLineCredit;
290 }
291
292
293
294
295
296
297 public KualiDecimal getNewSourceLineDebit() {
298 return newSourceLineDebit;
299 }
300
301
302
303
304
305
306 public void setNewSourceLineDebit(KualiDecimal newSourceLineDebit) {
307 this.newSourceLineDebit = newSourceLineDebit;
308 }
309
310
311
312
313
314
315 public String getCurrencyFormattedDebitTotal() {
316 return (String) new CurrencyFormatter().format(getVoucherDocument().getDebitTotal());
317 }
318
319
320
321
322
323
324 public String getCurrencyFormattedCreditTotal() {
325 return (String) new CurrencyFormatter().format(getVoucherDocument().getCreditTotal());
326 }
327
328
329
330
331
332
333 public String getCurrencyFormattedTotal() {
334 return (String) new CurrencyFormatter().format(((AmountTotaling) getVoucherDocument()).getTotalDollarAmount());
335 }
336
337
338
339
340
341 public void populateAccountingPeriodListForRendering() {
342
343 ArrayList accountingPeriods = new ArrayList(SpringContext.getBean(AccountingPeriodService.class).getOpenAccountingPeriods());
344
345 setAccountingPeriods(accountingPeriods);
346
347 populateSelectedVoucherAccountingPeriod();
348 }
349
350
351
352
353
354
355 protected void populateSelectedVoucherAccountingPeriod() {
356 if (StringUtils.isNotBlank(getSelectedAccountingPeriod())) {
357 AccountingPeriod ap = new AccountingPeriod();
358 ap.setUniversityFiscalPeriodCode(getSelectedPostingPeriodCode());
359 ap.setUniversityFiscalYear(getSelectedPostingYear());
360 getFinancialDocument().setAccountingPeriod(ap);
361 }
362 }
363
364
365
366
367
368
369 protected void populateCreditAndDebitAmounts() {
370 processDebitAndCreditForNewSourceLine();
371 processDebitAndCreditForAllSourceLines();
372 }
373
374
375
376
377
378
379
380 protected boolean processDebitAndCreditForNewSourceLine() {
381
382 boolean passed = processDebitAndCreditForSourceLine(getNewSourceLine(), newSourceLineDebit, newSourceLineCredit, OLEConstants.NEGATIVE_ONE);
383
384 return passed;
385 }
386
387
388
389
390
391
392
393
394
395 protected boolean processDebitAndCreditForAllSourceLines() {
396 VoucherDocument vDoc = getVoucherDocument();
397
398
399 boolean validProcessing = true;
400 for (int i = 0; i < vDoc.getSourceAccountingLines().size(); i++) {
401
402 SourceAccountingLine sourceLine = vDoc.getSourceAccountingLine(i);
403 VoucherAccountingLineHelper voucherLineHelper = getVoucherLineHelper(i);
404
405
406
407
408
409 validProcessing &= processDebitAndCreditForSourceLine(sourceLine, voucherLineHelper.getDebit(), voucherLineHelper.getCredit(), i);
410 }
411 return validProcessing;
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 protected boolean processDebitAndCreditForSourceLine(SourceAccountingLine sourceLine, KualiDecimal debitAmount, KualiDecimal creditAmount, int index) {
428
429 if (!validateCreditAndDebitAmounts(debitAmount, creditAmount, index)) {
430 return false;
431 }
432
433
434
435 if (debitAmount != null && debitAmount.isNonZero()) {
436
437 KualiDecimal tmpDebitAmount = new KualiDecimal(debitAmount.toString());
438 sourceLine.setDebitCreditCode(OLEConstants.GL_DEBIT_CODE);
439 sourceLine.setAmount(tmpDebitAmount);
440 }
441 else if (creditAmount != null && creditAmount.isNonZero()) {
442
443 KualiDecimal tmpCreditAmount = new KualiDecimal(creditAmount.toString());
444 sourceLine.setDebitCreditCode(OLEConstants.GL_CREDIT_CODE);
445 sourceLine.setAmount(tmpCreditAmount);
446 }
447 else {
448 sourceLine.setDebitCreditCode(OLEConstants.GL_DEBIT_CODE);
449 sourceLine.setAmount(KualiDecimal.ZERO);
450 }
451
452 return true;
453 }
454
455
456
457
458
459
460
461
462
463 protected boolean validateCreditAndDebitAmounts(KualiDecimal debitAmount, KualiDecimal creditAmount, int index) {
464 boolean valid = false;
465 if (null != creditAmount && null != debitAmount) {
466 if (creditAmount.isNonZero() && debitAmount.isNonZero()) {
467
468 if (OLEConstants.NEGATIVE_ONE == index) {
469 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(OLEConstants.DEBIT_AMOUNT_PROPERTY_NAME, OLEKeyConstants.ERROR_DOCUMENT_JV_AMOUNTS_IN_CREDIT_AND_DEBIT_FIELDS);
470 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(OLEConstants.CREDIT_AMOUNT_PROPERTY_NAME, OLEKeyConstants.ERROR_DOCUMENT_JV_AMOUNTS_IN_CREDIT_AND_DEBIT_FIELDS);
471 }
472 else {
473 String errorKeyPath = OLEConstants.JOURNAL_LINE_HELPER_PROPERTY_NAME + OLEConstants.SQUARE_BRACKET_LEFT + Integer.toString(index) + OLEConstants.SQUARE_BRACKET_RIGHT;
474 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(errorKeyPath + VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME, OLEKeyConstants.ERROR_DOCUMENT_JV_AMOUNTS_IN_CREDIT_AND_DEBIT_FIELDS);
475 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(errorKeyPath + VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME, OLEKeyConstants.ERROR_DOCUMENT_JV_AMOUNTS_IN_CREDIT_AND_DEBIT_FIELDS);
476 }
477 }
478 else {
479 valid = true;
480 }
481 }
482 else {
483 valid = true;
484 }
485 return valid;
486 }
487 }