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