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 java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.util.ArrayList;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.kuali.ole.coa.businessobject.BalanceType;
30 import org.kuali.ole.coa.service.BalanceTypeService;
31 import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelper;
32 import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelperBase;
33 import org.kuali.ole.fp.businessobject.VoucherSourceAccountingLine;
34 import org.kuali.ole.fp.document.JournalVoucherDocument;
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.rice.core.api.config.property.ConfigurationService;
42 import org.kuali.rice.core.api.util.type.KualiDecimal;
43 import org.kuali.rice.core.web.format.CurrencyFormatter;
44 import org.kuali.rice.kew.api.exception.WorkflowException;
45 import org.kuali.rice.kns.question.ConfirmationQuestion;
46 import org.kuali.rice.kns.util.KNSGlobalVariables;
47 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
48
49
50
51
52
53
54 public class JournalVoucherAction extends VoucherAction {
55
56
57
58 protected static final int CREDIT_DEBIT_TO_SINGLE_AMT_MODE = 0;
59 protected static final int SINGLE_AMT_TO_CREDIT_DEBIT_MODE = 1;
60 protected static final int EXT_ENCUMB_TO_NON_EXT_ENCUMB = 0;
61 protected static final int NON_EXT_ENCUMB_TO_EXT_ENCUMB = 1;
62 protected static final int NO_MODE_CHANGE = -1;
63
64
65
66
67
68
69
70
71 @Override
72 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
73 JournalVoucherForm journalVoucherForm = (JournalVoucherForm) form;
74
75 populateBalanceTypeOneDocument(journalVoucherForm);
76
77
78
79
80
81 ActionForward returnForward;
82 if (StringUtils.isNotBlank(journalVoucherForm.getOriginalBalanceType()) && !journalVoucherForm.getSelectedBalanceType().getCode().equals(journalVoucherForm.getOriginalBalanceType())) {
83 returnForward = super.dispatchMethod(mapping, form, request, response, OLEConstants.CHANGE_JOURNAL_VOUCHER_BALANCE_TYPE_METHOD);
84
85
86 this.populateAuthorizationFields(journalVoucherForm);
87 }
88 else {
89 returnForward = super.execute(mapping, journalVoucherForm, request, response);
90 }
91 return returnForward;
92 }
93
94
95
96
97
98
99
100
101 @Override
102 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
103
104 VoucherDocument vDoc = ((VoucherForm) form).getVoucherDocument();
105
106 KualiDecimal balance = vDoc.getCreditTotal().subtract(vDoc.getDebitTotal());
107 if (vDoc.getSourceAccountingLines().size() > 0 && balance.compareTo(KualiDecimal.ZERO) != 0) {
108
109 ActionForward returnForward = processRouteOutOfBalanceDocumentConfirmationQuestion(mapping, form, request, response);
110
111
112
113
114 if (returnForward != null) {
115 return returnForward;
116 }
117 }
118
119 return super.route(mapping, form, request, response);
120 }
121
122
123
124
125
126
127 protected void populateBalanceTypeOneDocument(JournalVoucherForm journalVoucherForm) {
128 String selectedBalanceTypeCode = journalVoucherForm.getSelectedBalanceType().getCode();
129 BalanceType selectedBalanceType = getPopulatedBalanceTypeInstance(selectedBalanceTypeCode);
130 journalVoucherForm.getJournalVoucherDocument().setBalanceTypeCode(selectedBalanceTypeCode);
131 journalVoucherForm.getJournalVoucherDocument().setBalanceType(selectedBalanceType);
132
133
134 journalVoucherForm.setSelectedBalanceType(selectedBalanceType);
135 }
136
137
138
139
140
141
142
143
144
145 @Override
146 public ActionForward correct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
147 ActionForward actionForward = super.correct(mapping, form, request, response);
148
149 JournalVoucherDocument jvDoc = (JournalVoucherDocument) ((JournalVoucherForm) form).getDocument();
150
151 jvDoc.refreshReferenceObject(OLEPropertyConstants.BALANCE_TYPE);
152
153 if (jvDoc.getBalanceType().isFinancialOffsetGenerationIndicator()) {
154
155 populateAllVoucherAccountingLineHelpers((JournalVoucherForm) form);
156 }
157
158 return actionForward;
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 public ActionForward changeBalanceType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
173 JournalVoucherForm journalVoucherForm = (JournalVoucherForm) form;
174
175
176 int balanceTypeAmountChangeMode = determineBalanceTypeAmountChangeMode(journalVoucherForm);
177 int balanceTypeExternalEncumbranceChangeMode = determineBalanceTypeEncumbranceChangeMode(journalVoucherForm);
178
179
180 if (balanceTypeAmountChangeMode != NO_MODE_CHANGE || balanceTypeExternalEncumbranceChangeMode != NO_MODE_CHANGE) {
181 ActionForward returnForward = processChangeBalanceTypeConfirmationQuestion(mapping, form, request, response);
182
183
184
185 if (returnForward != null) {
186 return returnForward;
187 }
188 else {
189
190 if (balanceTypeAmountChangeMode == CREDIT_DEBIT_TO_SINGLE_AMT_MODE) {
191 switchFromCreditDebitModeToSingleAmountMode(journalVoucherForm);
192 }
193 else if (balanceTypeAmountChangeMode == SINGLE_AMT_TO_CREDIT_DEBIT_MODE) {
194 switchFromSingleAmountModeToCreditDebitMode(journalVoucherForm);
195 }
196
197
198 if (balanceTypeExternalEncumbranceChangeMode == EXT_ENCUMB_TO_NON_EXT_ENCUMB) {
199 switchFromExternalEncumbranceModeToNonExternalEncumbrance(journalVoucherForm);
200 }
201 }
202 }
203
204 return mapping.findForward(OLEConstants.MAPPING_BASIC);
205 }
206
207
208
209
210
211
212
213
214
215 protected int determineBalanceTypeAmountChangeMode(JournalVoucherForm journalVoucherForm) throws Exception {
216 int balanceTypeAmountChangeMode = NO_MODE_CHANGE;
217
218
219 BalanceType origBalType = getPopulatedBalanceTypeInstance(journalVoucherForm.getOriginalBalanceType());
220 BalanceType newBalType = getPopulatedBalanceTypeInstance(journalVoucherForm.getSelectedBalanceType().getCode());
221
222
223 if (origBalType.isFinancialOffsetGenerationIndicator() && !newBalType.isFinancialOffsetGenerationIndicator()) {
224 balanceTypeAmountChangeMode = CREDIT_DEBIT_TO_SINGLE_AMT_MODE;
225 }
226 else if (!origBalType.isFinancialOffsetGenerationIndicator() && newBalType.isFinancialOffsetGenerationIndicator()) {
227 balanceTypeAmountChangeMode = SINGLE_AMT_TO_CREDIT_DEBIT_MODE;
228 }
229
230 return balanceTypeAmountChangeMode;
231 }
232
233
234
235
236
237
238
239
240
241 protected int determineBalanceTypeEncumbranceChangeMode(JournalVoucherForm journalVoucherForm) throws Exception {
242 int balanceTypeExternalEncumbranceChangeMode = NO_MODE_CHANGE;
243
244
245 BalanceType origBalType = getPopulatedBalanceTypeInstance(journalVoucherForm.getOriginalBalanceType());
246 BalanceType newBalType = getPopulatedBalanceTypeInstance(journalVoucherForm.getSelectedBalanceType().getCode());
247
248
249 if (origBalType.getCode().equals(OLEConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE) && !newBalType.getCode().equals(OLEConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE)) {
250 balanceTypeExternalEncumbranceChangeMode = EXT_ENCUMB_TO_NON_EXT_ENCUMB;
251 }
252 else if (!origBalType.getCode().equals(OLEConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE) && newBalType.getCode().equals(OLEConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE)) {
253 balanceTypeExternalEncumbranceChangeMode = NON_EXT_ENCUMB_TO_EXT_ENCUMB;
254 }
255
256 return balanceTypeExternalEncumbranceChangeMode;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270 protected ActionForward processChangeBalanceTypeConfirmationQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
271 JournalVoucherForm jvForm = (JournalVoucherForm) form;
272 JournalVoucherDocument jvDoc = jvForm.getJournalVoucherDocument();
273
274
275
276 if (jvDoc.getSourceAccountingLines().size() != 0) {
277 String question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
278 ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
279
280 if (question == null) {
281 String message = buildBalanceTypeChangeConfirmationMessage(jvForm, kualiConfiguration);
282
283
284 return this.performQuestionWithoutInput(mapping, form, request, response, OLEConstants.JOURNAL_VOUCHER_CHANGE_BALANCE_TYPE_QUESTION, message, OLEConstants.CONFIRMATION_QUESTION, OLEConstants.CHANGE_JOURNAL_VOUCHER_BALANCE_TYPE_METHOD, "");
285 }
286 else {
287 String buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
288 if ((OLEConstants.JOURNAL_VOUCHER_CHANGE_BALANCE_TYPE_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
289
290 BalanceType origBalType = getPopulatedBalanceTypeInstance(jvForm.getOriginalBalanceType());
291 jvForm.setSelectedBalanceType(origBalType);
292 jvDoc.setBalanceType(origBalType);
293 jvDoc.setBalanceTypeCode(origBalType.getCode());
294 return mapping.findForward(OLEConstants.MAPPING_BASIC);
295 }
296 }
297 }
298 return null;
299 }
300
301
302
303
304
305
306
307
308
309
310
311 protected String buildBalanceTypeChangeConfirmationMessage(JournalVoucherForm jvForm, ConfigurationService kualiConfiguration) throws Exception {
312 String message = new String("");
313
314
315 int balanceTypeAmountChangeMode = determineBalanceTypeAmountChangeMode(jvForm);
316 int balanceTypeExternalEncumbranceChangeMode = determineBalanceTypeEncumbranceChangeMode(jvForm);
317
318
319 if (balanceTypeAmountChangeMode == SINGLE_AMT_TO_CREDIT_DEBIT_MODE) {
320 message = kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_SINGLE_AMT_TO_CREDIT_DEBIT_MODE);
321
322 String newMessage = new String("");
323 if (balanceTypeExternalEncumbranceChangeMode == NON_EXT_ENCUMB_TO_EXT_ENCUMB) {
324 newMessage = StringUtils.replace(message, "{3}", kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_SINGLE_AMT_TO_EXT_ENCUMB_CREDIT_DEBIT_MODE));
325 }
326 else {
327 newMessage = StringUtils.replace(message, "{3}", "");
328 }
329 message = new String(newMessage);
330 }
331 else if (balanceTypeAmountChangeMode == CREDIT_DEBIT_TO_SINGLE_AMT_MODE) {
332 message = kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_CREDIT_DEBIT_TO_SINGLE_AMT_MODE);
333
334 String newMessage = new String("");
335 if (balanceTypeExternalEncumbranceChangeMode == EXT_ENCUMB_TO_NON_EXT_ENCUMB) {
336 newMessage = StringUtils.replace(message, "{3}", kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_EXT_ENCUMB_CREDIT_DEBIT_TO_SINGLE_AMT_MODE));
337 }
338 else {
339 newMessage = StringUtils.replace(message, "{3}", "");
340 }
341 message = new String(newMessage);
342 }
343 else if (balanceTypeExternalEncumbranceChangeMode == EXT_ENCUMB_TO_NON_EXT_ENCUMB) {
344 message = kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_EXT_ENCUMB_TO_NON_EXT_ENCUMB);
345 }
346 else if (balanceTypeExternalEncumbranceChangeMode == NON_EXT_ENCUMB_TO_EXT_ENCUMB) {
347 message = kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_CHANGE_JV_BAL_TYPE_FROM_NON_EXT_ENCUMB_TO_EXT_ENCUMB);
348 }
349
350
351 BalanceType origBalType = getPopulatedBalanceTypeInstance(jvForm.getOriginalBalanceType());
352 BalanceType newBalType = getPopulatedBalanceTypeInstance(jvForm.getSelectedBalanceType().getCode());
353
354
355 String replacement = "\"" + origBalType.getCode() + "-" + origBalType.getName() + "\"";
356 String newMessage = StringUtils.replace(message, "{0}", replacement);
357
358 replacement = "\"" + newBalType.getCode() + "-" + newBalType.getName() + "\"";
359 String finalMessage = StringUtils.replace(newMessage, "{1}", replacement);
360
361 return finalMessage;
362 }
363
364
365
366
367
368
369
370
371 protected BalanceType getPopulatedBalanceTypeInstance(String balanceTypeCode) {
372
373 return SpringContext.getBean(BalanceTypeService.class).getBalanceTypeByCode(balanceTypeCode);
374 }
375
376
377
378
379
380
381 protected void switchFromSingleAmountModeToCreditDebitMode(JournalVoucherForm journalVoucherForm) {
382
383
384 JournalVoucherDocument jvDoc = (JournalVoucherDocument) journalVoucherForm.getTransactionalDocument();
385 ArrayList sourceLines = (ArrayList) jvDoc.getSourceAccountingLines();
386 ArrayList helperLines = (ArrayList) journalVoucherForm.getVoucherLineHelpers();
387 helperLines.clear();
388
389
390 helperLines.ensureCapacity(sourceLines.size());
391
392 for (int i = 0; i < sourceLines.size(); i++) {
393 VoucherSourceAccountingLine sourceLine = (VoucherSourceAccountingLine) sourceLines.get(i);
394 sourceLine.setAmount(KualiDecimal.ZERO);
395 sourceLine.setDebitCreditCode(OLEConstants.GL_DEBIT_CODE);
396
397 helperLines.add(new VoucherAccountingLineHelperBase());
398 }
399 }
400
401
402
403
404
405
406
407 protected void switchFromExternalEncumbranceModeToNonExternalEncumbrance(JournalVoucherForm journalVoucherForm) {
408
409
410 JournalVoucherDocument jvDoc = (JournalVoucherDocument) journalVoucherForm.getTransactionalDocument();
411 ArrayList sourceLines = (ArrayList) jvDoc.getSourceAccountingLines();
412
413 for (int i = 0; i < sourceLines.size(); i++) {
414 VoucherSourceAccountingLine sourceLine = (VoucherSourceAccountingLine) sourceLines.get(i);
415 sourceLine.setReferenceOriginCode(null);
416 sourceLine.setReferenceNumber(null);
417 sourceLine.setReferenceTypeCode(null);
418 }
419 }
420
421
422
423
424
425
426 protected void switchFromCreditDebitModeToSingleAmountMode(JournalVoucherForm journalVoucherForm) {
427
428
429 JournalVoucherDocument jvDoc = journalVoucherForm.getJournalVoucherDocument();
430 ArrayList sourceLines = (ArrayList) jvDoc.getSourceAccountingLines();
431 ArrayList helperLines = (ArrayList) journalVoucherForm.getVoucherLineHelpers();
432
433 KualiDecimal ZERO = new KualiDecimal("0.00");
434 for (int i = 0; i < sourceLines.size(); i++) {
435 VoucherAccountingLineHelper helperLine = (VoucherAccountingLineHelper) helperLines.get(i);
436 SourceAccountingLine sourceLine = (SourceAccountingLine) sourceLines.get(i);
437 sourceLine.setAmount(ZERO);
438 sourceLine.setDebitCreditCode(OLEConstants.GL_DEBIT_CODE);
439
440 helperLine.setCredit(null);
441 helperLine.setDebit(null);
442 }
443 }
444
445
446
447
448
449
450
451 @Override
452 protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
453 super.loadDocument(kualiDocumentFormBase);
454 JournalVoucherForm journalVoucherForm = (JournalVoucherForm) kualiDocumentFormBase;
455
456
457
458 JournalVoucherDocument journalVoucherDocument = (JournalVoucherDocument) journalVoucherForm.getTransactionalDocument();
459 if (journalVoucherDocument.getBalanceType().isFinancialOffsetGenerationIndicator()) {
460 populateAllVoucherAccountingLineHelpers(journalVoucherForm);
461 KualiDecimal ZERO = new KualiDecimal("0.00");
462 journalVoucherForm.setNewSourceLineCredit(ZERO);
463 journalVoucherForm.setNewSourceLineDebit(ZERO);
464 }
465
466
467 journalVoucherForm.setNewSourceLine(null);
468
469
470 populateSelectedJournalBalanceType(journalVoucherDocument, journalVoucherForm);
471 populateSelectedAccountingPeriod(journalVoucherDocument, journalVoucherForm);
472 }
473
474
475
476
477
478
479
480 protected void populateSelectedJournalBalanceType(JournalVoucherDocument journalVoucherDocument, JournalVoucherForm journalVoucherForm) {
481 journalVoucherForm.setSelectedBalanceType(journalVoucherDocument.getBalanceType());
482 if (StringUtils.isNotBlank(journalVoucherDocument.getBalanceTypeCode())) {
483 journalVoucherForm.setOriginalBalanceType(journalVoucherDocument.getBalanceTypeCode());
484 }
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 @Override
504 protected ActionForward processRouteOutOfBalanceDocumentConfirmationQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
505 JournalVoucherForm jvForm = (JournalVoucherForm) form;
506 JournalVoucherDocument jvDoc = jvForm.getJournalVoucherDocument();
507
508 String question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
509 ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
510
511 if (question == null) {
512 String currencyFormattedDebitTotal = (String) new CurrencyFormatter().format(jvDoc.getDebitTotal());
513 String currencyFormattedCreditTotal = (String) new CurrencyFormatter().format(jvDoc.getCreditTotal());
514 String currencyFormattedTotal = (String) new CurrencyFormatter().format(jvDoc.getTotalDollarAmount());
515 String message = "";
516 jvDoc.refreshReferenceObject(OLEPropertyConstants.BALANCE_TYPE);
517 if (jvDoc.getBalanceType().isFinancialOffsetGenerationIndicator()) {
518 message = StringUtils.replace(kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_ROUTE_OUT_OF_BALANCE_JV_DOC), "{0}", currencyFormattedDebitTotal);
519 message = StringUtils.replace(message, "{1}", currencyFormattedCreditTotal);
520 }
521 else {
522 message = StringUtils.replace(kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_ROUTE_OUT_OF_BALANCE_JV_DOC_SINGLE_AMT_MODE), "{0}", currencyFormattedTotal);
523 }
524
525
526 return this.performQuestionWithoutInput(mapping, form, request, response, OLEConstants.JOURNAL_VOUCHER_ROUTE_OUT_OF_BALANCE_DOCUMENT_QUESTION, message, OLEConstants.CONFIRMATION_QUESTION, OLEConstants.ROUTE_METHOD, "");
527 }
528 else {
529 String buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
530 if ((OLEConstants.JOURNAL_VOUCHER_ROUTE_OUT_OF_BALANCE_DOCUMENT_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
531 KNSGlobalVariables.getMessageList().add(OLEKeyConstants.MESSAGE_JV_CANCELLED_ROUTE);
532 return mapping.findForward(OLEConstants.MAPPING_BASIC);
533 }
534 }
535 return null;
536 }
537
538
539
540
541
542
543
544
545
546
547
548
549
550 @Override
551 public ActionForward uploadSourceLines(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
552
553 uploadAccountingLines(true, form);
554
555 return mapping.findForward(OLEConstants.MAPPING_BASIC);
556 }
557
558
559
560
561
562
563
564
565
566
567 @Override
568 protected void uploadAccountingLines(boolean isSource, ActionForm form) throws FileNotFoundException, IOException {
569 JournalVoucherForm jvForm = (JournalVoucherForm) form;
570
571 jvForm.getJournalVoucherDocument().refreshReferenceObject(OLEPropertyConstants.BALANCE_TYPE);
572 super.uploadAccountingLines(isSource, jvForm);
573 }
574
575 }