1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.web.struts;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.kuali.ole.module.purap.PurapConstants;
23 import org.kuali.ole.module.purap.PurapConstants.*;
24 import org.kuali.ole.module.purap.PurapKeyConstants;
25 import org.kuali.ole.module.purap.PurapPropertyConstants;
26 import org.kuali.ole.module.purap.SingleConfirmationQuestion;
27 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
28 import org.kuali.ole.module.purap.businessobject.PurApItem;
29 import org.kuali.ole.module.purap.document.*;
30 import org.kuali.ole.module.purap.document.service.*;
31 import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
32 import org.kuali.ole.module.purap.document.validation.event.AttributedPreCalculateAccountsPayableEvent;
33 import org.kuali.ole.module.purap.service.PurapAccountingService;
34 import org.kuali.ole.module.purap.util.PurQuestionCallback;
35 import org.kuali.ole.select.document.service.OleSelectDocumentService;
36 import org.kuali.ole.sys.OLEConstants;
37 import org.kuali.ole.sys.OLEPropertyConstants;
38 import org.kuali.ole.sys.context.SpringContext;
39 import org.kuali.ole.vnd.VendorConstants;
40 import org.kuali.ole.vnd.businessobject.VendorAddress;
41 import org.kuali.rice.core.api.config.property.ConfigurationService;
42 import org.kuali.rice.core.api.util.RiceKeyConstants;
43 import org.kuali.rice.core.api.util.type.KualiDecimal;
44 import org.kuali.rice.kew.api.exception.WorkflowException;
45 import org.kuali.rice.kns.question.ConfirmationQuestion;
46 import org.kuali.rice.kns.service.DataDictionaryService;
47 import org.kuali.rice.kns.util.KNSGlobalVariables;
48 import org.kuali.rice.kns.util.MessageList;
49 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
50 import org.kuali.rice.krad.bo.Note;
51 import org.kuali.rice.krad.exception.ValidationException;
52 import org.kuali.rice.krad.service.BusinessObjectService;
53 import org.kuali.rice.krad.service.KualiRuleService;
54 import org.kuali.rice.krad.util.GlobalVariables;
55 import org.kuali.rice.krad.util.ObjectUtils;
56 import org.kuali.rice.krad.util.UrlFactory;
57
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpServletResponse;
60 import java.util.Iterator;
61 import java.util.List;
62 import java.util.Properties;
63 import java.util.TreeMap;
64 import java.util.Date;
65 import java.sql.Timestamp;
66
67
68
69
70 public class AccountsPayableActionBase extends PurchasingAccountsPayableActionBase {
71 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountsPayableActionBase.class);
72
73 private OleSelectDocumentService oleSelectDocumentService;
74
75 @Override
76 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
77 PurchasingAccountsPayableFormBase baseForm = (PurchasingAccountsPayableFormBase) form;
78
79 ActionForward fwd = super.execute(mapping, form, request, response);
80
81 AccountsPayableDocumentBase document = (AccountsPayableDocumentBase) baseForm.getDocument();
82 boolean foundAccountExpiredWarning = false;
83 for (int i = 0; i < KNSGlobalVariables.getMessageList().size(); i++) {
84 if (StringUtils.equals(KNSGlobalVariables.getMessageList().get(i).getErrorKey(), PurapKeyConstants.MESSAGE_CLOSED_OR_EXPIRED_ACCOUNTS_REPLACED)) {
85 foundAccountExpiredWarning = true;
86 }
87 }
88
89 if (!foundAccountExpiredWarning) {
90 SpringContext.getBean(AccountsPayableService.class).generateExpiredOrClosedAccountWarning(document);
91 }
92
93 return fwd;
94
95 }
96
97
98
99
100
101
102
103 @Override
104 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105 PurchasingAccountsPayableFormBase baseForm = (PurchasingAccountsPayableFormBase) form;
106
107 AccountsPayableDocumentBase document = (AccountsPayableDocumentBase) baseForm.getDocument();
108
109 if (StringUtils.equals(baseForm.getRefreshCaller(), VendorConstants.VENDOR_ADDRESS_LOOKUPABLE_IMPL)) {
110 if (StringUtils.isNotBlank(request.getParameter(OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.VENDOR_ADDRESS_ID))) {
111 Integer vendorAddressGeneratedId = document.getVendorAddressGeneratedIdentifier();
112 VendorAddress refreshVendorAddress = new VendorAddress();
113 refreshVendorAddress.setVendorAddressGeneratedIdentifier(vendorAddressGeneratedId);
114 refreshVendorAddress = (VendorAddress) SpringContext.getBean(BusinessObjectService.class).retrieve(refreshVendorAddress);
115 document.templateVendorAddress(refreshVendorAddress);
116 }
117 }
118
119 return super.refresh(mapping, form, request, response);
120 }
121
122
123
124
125
126
127
128 @Override
129 protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
130 super.loadDocument(kualiDocumentFormBase);
131 AccountsPayableDocument document = (AccountsPayableDocument) kualiDocumentFormBase.getDocument();
132
133 SpringContext.getBean(AccountsPayableService.class).generateExpiredOrClosedAccountWarning(document);
134
135 SpringContext.getBean(AccountsPayableService.class).updateItemList(document);
136 ((AccountsPayableFormBase) kualiDocumentFormBase).updateItemCounts();
137 }
138
139
140
141
142
143
144
145
146
147
148 @Override
149 public ActionForward calculate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
150 AccountsPayableFormBase apForm = (AccountsPayableFormBase) form;
151 AccountsPayableDocument apDoc = (AccountsPayableDocument) apForm.getDocument();
152
153
154 SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(apDoc);
155
156
157 if (SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedPreCalculateAccountsPayableEvent(apDoc))) {
158 customCalculate(apDoc);
159
160
161 if (apForm instanceof PaymentRequestForm && apDoc.getApplicationDocumentStatus().equals(PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
162
163 PaymentRequestForm preqForm = (PaymentRequestForm) apForm;
164 preqForm.setCalculatedTax(true);
165 } else {
166
167 apForm.setCalculated(true);
168 }
169 }
170
171 return super.calculate(mapping, form, request, response);
172 }
173
174 @Override
175 public ActionForward clearAllTaxes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
176 AccountsPayableFormBase payableForm = (AccountsPayableFormBase) form;
177 AccountsPayableDocument apDoc = (AccountsPayableDocument) payableForm.getDocument();
178
179 SpringContext.getBean(PurapService.class).clearAllTaxes(apDoc);
180
181 return super.clearAllTaxes(mapping, form, request, response);
182 }
183
184
185
186
187
188
189
190
191 protected boolean requiresCaculate(AccountsPayableFormBase apForm) {
192 boolean requiresCalculate = true;
193 PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) apForm.getDocument();
194 requiresCalculate = !apForm.isCalculated() && !SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(purapDocument);
195
196 return requiresCalculate;
197 }
198
199
200
201
202
203
204 public String getActionName() {
205 return null;
206 }
207
208
209
210
211
212 @Override
213 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
214 AccountsPayableFormBase apForm = (AccountsPayableFormBase) form;
215
216
217 AccountsPayableDocumentBase document = (AccountsPayableDocumentBase) apForm.getDocument();
218 document.setLastActionPerformedByPersonId(GlobalVariables.getUserSession().getPerson().getPrincipalId());
219
220
221 if (requiresCaculate(apForm)) {
222 GlobalVariables.getMessageMap().putError(OLEConstants.DOCUMENT_ERRORS, PurapKeyConstants.ERROR_APPROVE_REQUIRES_CALCULATE);
223 return mapping.findForward(OLEConstants.MAPPING_BASIC);
224 }
225
226
227 customCalculate((AccountsPayableDocument) apForm.getDocument());
228
229
230 ActionForward forward = super.route(mapping, form, request, response);
231
232
233 boolean successMessageFound = false;
234 MessageList messageList = KNSGlobalVariables.getMessageList();
235 for (int i = 0; i < messageList.size(); i++) {
236 if (StringUtils.equals(messageList.get(i).getErrorKey(), RiceKeyConstants.MESSAGE_ROUTE_SUCCESSFUL)) {
237 successMessageFound = true;
238 break;
239 }
240 }
241
242 if (successMessageFound) {
243 String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY);
244
245 Properties parameters = new Properties();
246 parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.DOC_HANDLER_METHOD);
247 parameters.put(OLEConstants.PARAMETER_COMMAND, "initiate");
248 parameters.put(OLEConstants.DOCUMENT_TYPE_NAME, apForm.getDocTypeName());
249
250 String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + "purap" + this.getActionName() + ".do", parameters);
251 forward = new ActionForward(lookupUrl, true);
252 }
253
254 return forward;
255 }
256
257
258
259
260
261 @Override
262 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
263 AccountsPayableFormBase apForm = (AccountsPayableFormBase) form;
264
265 if (!requiresCaculate(apForm)) {
266 return super.save(mapping, form, request, response);
267 }
268
269 GlobalVariables.getMessageMap().putError(OLEConstants.DOCUMENT_ERRORS, PurapKeyConstants.ERROR_SAVE_REQUIRES_CALCULATE);
270 return mapping.findForward(OLEConstants.MAPPING_BASIC);
271
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 protected ActionForward askQuestionWithInput(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String questionType, String notePrefix, String operation, String messageKey, PurQuestionCallback callback) throws Exception {
292 TreeMap<String, PurQuestionCallback> questionsAndCallbacks = new TreeMap<String, PurQuestionCallback>();
293 questionsAndCallbacks.put(questionType, callback);
294 return askQuestionWithInput(mapping, form, request, response, questionType, notePrefix, operation, messageKey, questionsAndCallbacks, "", mapping.findForward(OLEConstants.MAPPING_BASIC));
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 protected ActionForward askQuestionWithInput(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String questionType, String notePrefix, String operation, String messageKey, TreeMap<String, PurQuestionCallback> questionsAndCallbacks, String messagePrefix, ActionForward redirect) throws Exception {
319 KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
320 AccountsPayableDocumentBase apDocument = (AccountsPayableDocumentBase) kualiDocumentFormBase.getDocument();
321
322 String question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
323 String reason = request.getParameter(OLEConstants.QUESTION_REASON_ATTRIBUTE_NAME);
324 String noteText = "";
325
326 ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
327 String firstQuestion = questionsAndCallbacks.firstKey();
328 PurQuestionCallback callback = null;
329 Iterator questions = questionsAndCallbacks.keySet().iterator();
330 String mapQuestion = null;
331 String key = null;
332
333
334 if (question == null) {
335 key = getQuestionProperty(messageKey, messagePrefix, kualiConfiguration, firstQuestion);
336 String message = StringUtils.replace(key, "{0}", operation);
337
338
339 return this.performQuestionWithInput(mapping, form, request, response, firstQuestion, message, OLEConstants.CONFIRMATION_QUESTION, questionType, "");
340 } else {
341
342 while (questions.hasNext()) {
343 mapQuestion = (String) questions.next();
344
345 if (StringUtils.equals(mapQuestion, question)) {
346 callback = questionsAndCallbacks.get(mapQuestion);
347 break;
348 }
349 }
350 key = getQuestionProperty(messageKey, messagePrefix, kualiConfiguration, mapQuestion);
351
352 Object buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
353 if (question.equals(mapQuestion) && buttonClicked.equals(ConfirmationQuestion.NO)) {
354
355
356 String nextQuestion = null;
357
358 if (questions.hasNext()) {
359 nextQuestion = (String) questions.next();
360 key = getQuestionProperty(messageKey, messagePrefix, kualiConfiguration, nextQuestion);
361
362 return this.performQuestionWithInput(mapping, form, request, response, nextQuestion, key, OLEConstants.CONFIRMATION_QUESTION, questionType, "");
363 } else {
364
365 return mapping.findForward(OLEConstants.MAPPING_BASIC);
366 }
367 }
368
369 String introNoteMessage = notePrefix + OLEConstants.BLANK_SPACE;
370
371
372 noteText = introNoteMessage + reason;
373 int noteTextLength = noteText.length();
374
375
376 int noteTextMaxLength = SpringContext.getBean(DataDictionaryService.class).getAttributeMaxLength(Note.class, OLEConstants.NOTE_TEXT_PROPERTY_NAME).intValue();
377 if (StringUtils.isBlank(reason) || (noteTextLength > noteTextMaxLength)) {
378
379 int reasonLimit = noteTextMaxLength - noteTextLength;
380 if (reason == null) {
381
382 reason = "";
383 }
384
385 return this.performQuestionWithInputAgainBecauseOfErrors(mapping, form, request, response, mapQuestion, key, OLEConstants.CONFIRMATION_QUESTION, questionType, "", reason, PurapKeyConstants.ERROR_PAYMENT_REQUEST_REASON_REQUIRED, OLEConstants.QUESTION_REASON_ATTRIBUTE_NAME, new Integer(reasonLimit).toString());
386 }
387 }
388
389
390 if (ObjectUtils.isNotNull(callback)) {
391 AccountsPayableDocument refreshedApDocument = callback.doPostQuestion(apDocument, noteText);
392 kualiDocumentFormBase.setDocument(refreshedApDocument);
393 }
394 String nextQuestion = null;
395
396 if (questions.hasNext()) {
397 nextQuestion = (String) questions.next();
398 key = getQuestionProperty(messageKey, messagePrefix, kualiConfiguration, nextQuestion);
399
400 return this.performQuestionWithInput(mapping, form, request, response, nextQuestion, key, OLEConstants.CONFIRMATION_QUESTION, questionType, "");
401 }
402
403 return redirect;
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417 protected String getQuestionProperty(String messageKey, String messagePrefix, ConfigurationService kualiConfiguration, String question) {
418 return kualiConfiguration.getPropertyValueAsString((StringUtils.isEmpty(messagePrefix)) ? messageKey : messagePrefix + question);
419 }
420
421 public ActionForward reopenPo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
422 LOG.debug("Reopen PO started");
423 return askQuestionsAndPerformReopenPurchaseOrder(mapping, form, request, response);
424 }
425
426
427
428
429
430 @Override
431 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
432 return askCancelQuestion(mapping, form, request, response);
433 }
434
435
436
437
438
439
440
441
442
443
444
445 protected ActionForward askCancelQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
446 PurchasingAccountsPayableFormBase apForm = (PurchasingAccountsPayableFormBase) form;
447
448 String operation = "Cancel ";
449 PurQuestionCallback callback = cancelCallbackMethod();
450 TreeMap<String, PurQuestionCallback> questionsAndCallbacks = new TreeMap<String, PurQuestionCallback>();
451 questionsAndCallbacks.put("cancelAP", callback);
452
453 return askQuestionWithInput(mapping, form, request, response, CMDocumentsStrings.CANCEL_CM_QUESTION, AccountsPayableDocumentStrings.CANCEL_NOTE_PREFIX, operation, PurapKeyConstants.CREDIT_MEMO_QUESTION_CANCEL_DOCUMENT, questionsAndCallbacks, PurapKeyConstants.AP_QUESTION_PREFIX, mapping.findForward(OLEConstants.MAPPING_PORTAL));
454 }
455
456
457
458
459
460
461 protected PurQuestionCallback cancelPOActionCallbackMethod() {
462
463 return new PurQuestionCallback() {
464 @Override
465 public AccountsPayableDocument doPostQuestion(AccountsPayableDocument document, String noteText) throws Exception {
466
467 return document;
468 }
469 };
470 }
471
472
473
474
475
476
477 protected PurQuestionCallback cancelCallbackMethod() {
478 return new PurQuestionCallback() {
479 @Override
480 public AccountsPayableDocument doPostQuestion(AccountsPayableDocument document, String noteText) throws Exception {
481 SpringContext.getBean(AccountsPayableService.class).cancelAccountsPayableDocumentByCheckingDocumentStatus(document, noteText);
482 return document;
483 }
484 };
485 }
486
487 protected ActionForward askQuestionsAndPerformReopenPurchaseOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
488 LOG.debug("askQuestionsAndPerformDocumentAction started.");
489 KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
490 AccountsPayableDocumentBase apDoc = (AccountsPayableDocumentBase) kualiDocumentFormBase.getDocument();
491 Object question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
492 String questionType = PODocumentsStrings.REOPEN_PO_QUESTION;
493 String confirmType = PODocumentsStrings.CONFIRM_REOPEN_QUESTION;
494 String messageType = PurapKeyConstants.PURCHASE_ORDER_MESSAGE_REOPEN_DOCUMENT;
495 String operation = "Reopen ";
496
497 try {
498 ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
499
500
501 if (ObjectUtils.isNull(question)) {
502 String key = kualiConfiguration.getPropertyValueAsString(PurapKeyConstants.PURCHASE_ORDER_QUESTION_DOCUMENT);
503 String message = StringUtils.replace(key, "{0}", operation);
504 return this.performQuestionWithoutInput(mapping, form, request, response, questionType, message, OLEConstants.CONFIRMATION_QUESTION, questionType, "");
505 } else {
506 Object buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
507 if (question.equals(questionType) && buttonClicked.equals(ConfirmationQuestion.NO)) {
508
509 return mapping.findForward(OLEConstants.MAPPING_BASIC);
510 } else if (question.equals(confirmType) && buttonClicked.equals(SingleConfirmationQuestion.OK)) {
511
512 return mapping.findForward(OLEConstants.MAPPING_BASIC);
513 }
514 }
515
516 PurchaseOrderDocument po = apDoc.getPurchaseOrderDocument();
517 if (!po.isPendingActionIndicator() && PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED.equals(po.getApplicationDocumentStatus())) {
518
519
520
521
522
523
524
525 po = initiateReopenPurchaseOrder(po, kualiDocumentFormBase.getAnnotation());
526
527 if (!GlobalVariables.getMessageMap().hasNoErrors()) {
528 throw new ValidationException("errors occurred during new PO creation");
529 }
530
531 if (StringUtils.isNotEmpty(messageType)) {
532 KNSGlobalVariables.getMessageList().add(messageType);
533 }
534 return this.performQuestionWithoutInput(mapping, form, request, response, confirmType, kualiConfiguration.getPropertyValueAsString(messageType), PODocumentsStrings.SINGLE_CONFIRMATION_QUESTION, questionType, "");
535 } else {
536 return this.performQuestionWithoutInput(mapping, form, request, response, confirmType, "Unable to reopen the PO at this time due to the incorrect PO status or a pending PO change document.", PODocumentsStrings.SINGLE_CONFIRMATION_QUESTION, questionType, "");
537 }
538
539 } catch (ValidationException ve) {
540 throw ve;
541 }
542 }
543
544 public PurchaseOrderDocument initiateReopenPurchaseOrder(PurchaseOrderDocument po, String annotation) {
545 try {
546 LogicContainer logicToRun = new LogicContainer() {
547 @Override
548 public Object runLogic(Object[] objects) throws Exception {
549 PurchaseOrderDocument po = (PurchaseOrderDocument) objects[0];
550
551 Note cancelNote = new Note();
552 cancelNote.setNoteTypeCode(po.getNoteType().getCode());
553 cancelNote.setAuthorUniversalIdentifier(GlobalVariables.getUserSession().getPerson().getPrincipalId());
554 cancelNote.setNoteText(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(PurapKeyConstants.AP_REOPENS_PURCHASE_ORDER_NOTE));
555
556 Timestamp postTime = new Timestamp((new Date()).getTime());
557 cancelNote.setNotePostedTimestamp(postTime);
558 po.addNote(cancelNote);
559 SpringContext.getBean(PurapService.class).saveDocumentNoValidation(po);
560
561 return SpringContext.getBean(PurchaseOrderService.class).createAndRoutePotentialChangeDocument(po.getDocumentNumber(), PurchaseOrderDocTypes.PURCHASE_ORDER_REOPEN_DOCUMENT, (String) objects[1], null, PurchaseOrderStatuses.APPDOC_PENDING_REOPEN);
562 }
563 };
564 return (PurchaseOrderDocument) SpringContext.getBean(PurapService.class).performLogicWithFakedUserSession(getOleSelectDocumentService().getSelectParameterValue(OLEConstants.SYSTEM_USER), logicToRun, new Object[]{po, annotation});
565 } catch (WorkflowException e) {
566 String errorMsg = "Workflow Exception caught: " + e.getLocalizedMessage();
567 LOG.error(errorMsg, e);
568 throw new RuntimeException(errorMsg, e);
569 } catch (Exception e) {
570 throw new RuntimeException(e);
571 }
572 }
573
574
575
576
577
578
579
580
581
582
583
584
585 public ActionForward recalculateItemAccountsAmounts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
586 AccountsPayableFormBase payableForm = (AccountsPayableFormBase) form;
587 AccountsPayableDocument apDoc = (AccountsPayableDocument) payableForm.getDocument();
588
589 PurapAccountingService purapAccountingService = SpringContext.getBean(PurapAccountingService.class);
590
591 String[] indexes = getSelectedItemNumber(request);
592 int itemIndex = Integer.parseInt(indexes[0]);
593
594 PurApItem item = apDoc.getItem((itemIndex));
595
596
597 restoreItemAccountsAmounts(apDoc, item);
598
599 item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
600
601 final KualiDecimal itemExtendedPrice = (item.getExtendedPrice() == null) ? KualiDecimal.ZERO : item.getExtendedPrice();
602 ;
603 if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
604 KualiDecimal newExtendedPrice = item.calculateExtendedPrice();
605 item.setExtendedPrice(newExtendedPrice);
606 }
607
608 PaymentRequestDocument preqDoc = (PaymentRequestDocument) apDoc;
609
610
611 preqDoc.updateExtendedPriceOnItems();
612
613
614
615 if (preqDoc.getApplicationDocumentStatus().equals(PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
616 SpringContext.getBean(PaymentRequestService.class).calculateTaxArea(preqDoc);
617 }
618
619
620
621 SpringContext.getBean(PaymentRequestService.class).calculatePaymentRequest(preqDoc, true);
622 SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(preqDoc));
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640 return mapping.findForward(OLEConstants.MAPPING_BASIC);
641 }
642
643
644
645
646
647
648
649
650
651
652
653 public ActionForward restoreItemAccountsAmounts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
654 AccountsPayableFormBase payableForm = (AccountsPayableFormBase) form;
655 AccountsPayableDocument apDoc = (AccountsPayableDocument) payableForm.getDocument();
656
657 String[] indexes = getSelectedItemNumber(request);
658 int itemIndex = Integer.parseInt(indexes[0]);
659
660 PurApItem item = apDoc.getItem((itemIndex));
661
662
663 restoreItemAccountsAmounts(apDoc, item);
664
665 item.setItemQuantity(null);
666 item.setItemTaxAmount(null);
667
668 item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
669
670 final KualiDecimal itemExtendedPrice = (item.getExtendedPrice() == null) ? KualiDecimal.ZERO : item.getExtendedPrice();
671 ;
672 if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
673 KualiDecimal newExtendedPrice = item.calculateExtendedPrice();
674 item.setExtendedPrice(newExtendedPrice);
675 }
676
677 return mapping.findForward(OLEConstants.MAPPING_BASIC);
678 }
679
680
681
682
683
684
685
686
687
688 protected String[] getSelectedItemNumber(HttpServletRequest request) {
689 String itemString = new String();
690 String parameterName = (String) request.getAttribute(OLEConstants.METHOD_TO_CALL_ATTRIBUTE);
691 if (StringUtils.isNotBlank(parameterName)) {
692 itemString = StringUtils.substringBetween(parameterName, ".line", ".");
693 }
694 String[] result = StringUtils.split(itemString, ":");
695
696 return result;
697 }
698
699
700
701
702
703
704
705
706 protected void restoreItemAccountsAmounts(AccountsPayableDocument apDoc, PurApItem preqItem) {
707 List<PurApItem> pOItems = apDoc.getPurchaseOrderDocument().getItems();
708
709 PurApItem pOItem = getPOItem(pOItems, preqItem.getItemLineNumber());
710 if (ObjectUtils.isNotNull(pOItem)) {
711
712 List<PurApAccountingLine> preqAccountingLines = preqItem.getSourceAccountingLines();
713 for (PurApAccountingLine lineAcct : preqAccountingLines) {
714 updateItemAccountLine(pOItem, lineAcct);
715 }
716 }
717 }
718
719
720
721
722
723
724
725
726
727 protected PurApItem getPOItem(List<PurApItem> pOItems, Integer itemLineNumber) {
728 PurApItem pOItem = null;
729
730 for (PurApItem poItem : pOItems) {
731 if (poItem.getItemLineNumber().compareTo(itemLineNumber) == 0) {
732
733 return poItem;
734 }
735 }
736
737 return pOItem;
738 }
739
740
741
742
743
744
745
746
747 protected void updateItemAccountLine(PurApItem pOItem, PurApAccountingLine lineAcct) {
748 List<PurApAccountingLine> pOAccountingLines = pOItem.getSourceAccountingLines();
749 for (PurApAccountingLine pOLineAcct : pOAccountingLines) {
750 if (lineAcct.getChartOfAccountsCode().equalsIgnoreCase(pOLineAcct.getChartOfAccountsCode()) &&
751 lineAcct.getAccountNumber().equalsIgnoreCase(pOLineAcct.getAccountNumber()) &&
752 lineAcct.getFinancialObjectCode().equalsIgnoreCase(pOLineAcct.getFinancialObjectCode())) {
753 lineAcct.setAmount(pOLineAcct.getAmount());
754 lineAcct.setAccountLinePercent(pOLineAcct.getAccountLinePercent());
755 }
756 }
757 }
758
759 public OleSelectDocumentService getOleSelectDocumentService() {
760 if(oleSelectDocumentService == null){
761 oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
762 }
763 return oleSelectDocumentService;
764 }
765
766 public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
767 this.oleSelectDocumentService = oleSelectDocumentService;
768 }
769 }