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.CMDocumentsStrings;
24 import org.kuali.ole.module.purap.PurapKeyConstants;
25 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
26 import org.kuali.ole.module.purap.businessobject.PurApItem;
27 import org.kuali.ole.module.purap.document.*;
28 import org.kuali.ole.module.purap.document.service.CreditMemoService;
29 import org.kuali.ole.module.purap.document.service.PaymentRequestService;
30 import org.kuali.ole.module.purap.document.service.PurapService;
31 import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
32 import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
33 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
34 import org.kuali.ole.module.purap.util.PurQuestionCallback;
35 import org.kuali.ole.sys.OLEConstants;
36 import org.kuali.ole.sys.context.SpringContext;
37 import org.kuali.rice.core.api.util.type.KualiDecimal;
38 import org.kuali.rice.kew.api.KewApiConstants;
39 import org.kuali.rice.kew.api.exception.WorkflowException;
40 import org.kuali.rice.kim.api.KimConstants;
41 import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
42 import org.kuali.rice.kns.question.ConfirmationQuestion;
43 import org.kuali.rice.kns.service.DocumentHelperService;
44 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
45 import org.kuali.rice.krad.document.Document;
46 import org.kuali.rice.krad.service.KualiRuleService;
47 import org.kuali.rice.krad.util.GlobalVariables;
48 import org.kuali.rice.krad.util.KRADConstants;
49 import org.kuali.rice.krad.util.ObjectUtils;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56
57
58
59
60 public class VendorCreditMemoAction extends AccountsPayableActionBase {
61 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorCreditMemoAction.class);
62
63
64
65
66
67
68 @Override
69 protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
70 super.createDocument(kualiDocumentFormBase);
71 ((VendorCreditMemoDocument) kualiDocumentFormBase.getDocument()).initiateDocument();
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 public ActionForward continueCreditMemo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
86 VendorCreditMemoForm cmForm = (VendorCreditMemoForm) form;
87 VendorCreditMemoDocument creditMemoDocument = (VendorCreditMemoDocument) cmForm.getDocument();
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedContinuePurapEvent(creditMemoDocument));
114 if (!rulePassed) {
115 return mapping.findForward(OLEConstants.MAPPING_BASIC);
116 }
117
118 if (creditMemoDocument.isSourceDocumentPaymentRequest()) {
119 PaymentRequestDocument preq = SpringContext.getBean(PaymentRequestService.class).getPaymentRequestById(creditMemoDocument.getPaymentRequestIdentifier());
120 if (ObjectUtils.isNotNull(preq)) {
121
122 creditMemoDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(preq.getAccountsPayablePurchasingDocumentLinkIdentifier());
123
124 if (!SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(creditMemoDocument).isAuthorizedByTemplate(creditMemoDocument, KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, GlobalVariables.getUserSession().getPrincipalId())) {
125 throw buildAuthorizationException("initiate document", creditMemoDocument);
126 }
127 }
128 } else if (creditMemoDocument.isSourceDocumentPurchaseOrder()) {
129 PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(creditMemoDocument.getPurchaseOrderIdentifier());
130 if (ObjectUtils.isNotNull(po)) {
131
132 creditMemoDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
133
134 if (!SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(creditMemoDocument).isAuthorizedByTemplate(creditMemoDocument, KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, GlobalVariables.getUserSession().getPrincipalId())) {
135 throw buildAuthorizationException("initiate document", creditMemoDocument);
136 }
137 }
138 } else {
139
140 }
141
142
143
144 ActionForward forward = performDuplicateCreditMemoCheck(mapping, form, request, response, creditMemoDocument);
145 if (forward != null) {
146 return forward;
147 }
148
149
150 SpringContext.getBean(CreditMemoService.class).populateAndSaveCreditMemo(creditMemoDocument);
151
152
153 SpringContext.getBean(PurapService.class).sortBelowTheLine(creditMemoDocument);
154
155
156 cmForm.updateItemCounts();
157
158
159 PurchaseOrderDocument po = creditMemoDocument.getPurchaseOrderDocument();
160 if ((creditMemoDocument.isSourceDocumentPaymentRequest() || creditMemoDocument.isSourceDocumentPurchaseOrder()) && PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED.equals(po.getApplicationDocumentStatus())) {
161 initiateReopenPurchaseOrder(po, cmForm.getAnnotation());
162 }
163
164
165 List<PurApItem> items = creditMemoDocument.getItems();
166
167 for (PurApItem item : items) {
168 for (PurApAccountingLine accountLine : item.getSourceAccountingLines()) {
169 accountLine.setAmount(KualiDecimal.ZERO);
170 }
171 }
172
173 return mapping.findForward(OLEConstants.MAPPING_BASIC);
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public ActionForward clearInitFields(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
237 VendorCreditMemoForm cmForm = (VendorCreditMemoForm) form;
238 VendorCreditMemoDocument creditMemoDocument = (VendorCreditMemoDocument) cmForm.getDocument();
239 creditMemoDocument.clearInitFields();
240
241 return super.refresh(mapping, form, request, response);
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 protected ActionForward performDuplicateCreditMemoCheck(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, VendorCreditMemoDocument creditMemoDocument) throws Exception {
259 ActionForward forward = null;
260 String duplicateMessage = SpringContext.getBean(CreditMemoService.class).creditMemoDuplicateMessages(creditMemoDocument);
261 if (StringUtils.isNotBlank(duplicateMessage)) {
262 Object question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
263 if (question == null) {
264
265 return this.performQuestionWithoutInput(mapping, form, request, response, PurapConstants.PREQDocumentsStrings.DUPLICATE_INVOICE_QUESTION, duplicateMessage, OLEConstants.CONFIRMATION_QUESTION, "continueCreditMemo", "");
266 }
267
268 Object buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
269 if ((PurapConstants.PREQDocumentsStrings.DUPLICATE_INVOICE_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
270 forward = mapping.findForward(OLEConstants.MAPPING_BASIC);
271 }
272 }
273
274 return forward;
275 }
276
277
278
279
280
281
282 @Override
283 protected void customCalculate(PurchasingAccountsPayableDocument apDoc) {
284 VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument) apDoc;
285
286
287 SpringContext.getBean(CreditMemoService.class).calculateCreditMemo(cmDocument);
288
289
290 SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(cmDocument));
291
292 }
293
294
295
296
297
298
299
300
301
302
303
304 public ActionForward addHoldOnCreditMemo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
305 String operation = "Hold ";
306
307 PurQuestionCallback callback = new PurQuestionCallback() {
308 @Override
309 public AccountsPayableDocument doPostQuestion(AccountsPayableDocument document, String noteText) throws Exception {
310 VendorCreditMemoDocument cmDocument = SpringContext.getBean(CreditMemoService.class).addHoldOnCreditMemo((VendorCreditMemoDocument) document, noteText);
311 return cmDocument;
312 }
313 };
314
315 return askQuestionWithInput(mapping, form, request, response, CMDocumentsStrings.HOLD_CM_QUESTION, operation, CMDocumentsStrings.HOLD_NOTE_PREFIX, PurapKeyConstants.CREDIT_MEMO_QUESTION_HOLD_DOCUMENT, callback);
316 }
317
318
319
320
321
322
323
324
325
326
327
328 public ActionForward removeHoldFromCreditMemo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
329 String operation = "Remove Hold ";
330
331 PurQuestionCallback callback = new PurQuestionCallback() {
332 @Override
333 public AccountsPayableDocument doPostQuestion(AccountsPayableDocument document, String noteText) throws Exception {
334 VendorCreditMemoDocument cmDocument = SpringContext.getBean(CreditMemoService.class).removeHoldOnCreditMemo((VendorCreditMemoDocument) document, noteText);
335 return cmDocument;
336 }
337 };
338
339 return askQuestionWithInput(mapping, form, request, response, CMDocumentsStrings.REMOVE_HOLD_CM_QUESTION, operation, CMDocumentsStrings.REMOVE_HOLD_NOTE_PREFIX, PurapKeyConstants.CREDIT_MEMO_QUESTION_REMOVE_HOLD_DOCUMENT, callback);
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 @Override
360 public String getActionName() {
361 return PurapConstants.CREDIT_MEMO_ACTION_NAME;
362 }
363
364 @Override
365 protected void populateAdHocActionRequestCodes(KualiDocumentFormBase formBase) {
366 Document document = formBase.getDocument();
367 DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
368 Map<String, String> adHocActionRequestCodes = new HashMap<String, String>();
369
370 if (documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_FYI_REQ, GlobalVariables.getUserSession().getPerson())) {
371 adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_FYI_REQ, KewApiConstants.ACTION_REQUEST_FYI_REQ_LABEL);
372 }
373 if ((document.getDocumentHeader().getWorkflowDocument().isInitiated()
374 || document.getDocumentHeader().getWorkflowDocument().isSaved()
375 || document.getDocumentHeader().getWorkflowDocument().isEnroute()
376 ) && documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, GlobalVariables.getUserSession().getPerson())) {
377 adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ_LABEL);
378 }
379 formBase.setAdHocActionRequestCodes(adHocActionRequestCodes);
380
381 }
382 }