001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.purap.web.struts.action;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.struts.action.ActionForm;
023: import org.apache.struts.action.ActionForward;
024: import org.apache.struts.action.ActionMapping;
025: import org.kuali.core.question.ConfirmationQuestion;
026: import org.kuali.core.service.KualiRuleService;
027: import org.kuali.core.web.struts.form.KualiDocumentFormBase;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.purap.PurapConstants;
031: import org.kuali.module.purap.PurapKeyConstants;
032: import org.kuali.module.purap.PurapConstants.CMDocumentsStrings;
033: import org.kuali.module.purap.document.AccountsPayableDocument;
034: import org.kuali.module.purap.document.CreditMemoDocument;
035: import org.kuali.module.purap.rule.event.CalculateAccountsPayableEvent;
036: import org.kuali.module.purap.service.CreditMemoService;
037: import org.kuali.module.purap.service.PurapService;
038: import org.kuali.module.purap.util.PurQuestionCallback;
039: import org.kuali.module.purap.web.struts.form.CreditMemoForm;
040:
041: import edu.iu.uis.eden.exception.WorkflowException;
042:
043: /**
044: * Struts Action for Credit Memo document.
045: */
046: public class CreditMemoAction extends AccountsPayableActionBase {
047: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
048: .getLogger(CreditMemoAction.class);
049:
050: /**
051: * Do initialization for a new credit memo.
052: *
053: * @see org.kuali.core.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.core.web.struts.form.KualiDocumentFormBase)
054: */
055: @Override
056: protected void createDocument(
057: KualiDocumentFormBase kualiDocumentFormBase)
058: throws WorkflowException {
059: super .createDocument(kualiDocumentFormBase);
060: ((CreditMemoDocument) kualiDocumentFormBase.getDocument())
061: .initiateDocument();
062: }
063:
064: /**
065: * Handles continue request. This request comes from the initial screen which gives indicates whether the type is payment
066: * request, purchase order, or vendor. Based on that, the credit memo is initially populated and the remaining tabs shown.
067: *
068: * @param mapping An ActionMapping
069: * @param form An ActionForm
070: * @param request The HttpServletRequest
071: * @param response The HttpServletResponse
072: * @throws Exception
073: * @return An ActionForward
074: */
075: public ActionForward continueCreditMemo(ActionMapping mapping,
076: ActionForm form, HttpServletRequest request,
077: HttpServletResponse response) throws Exception {
078: CreditMemoForm cmForm = (CreditMemoForm) form;
079: CreditMemoDocument creditMemoDocument = (CreditMemoDocument) cmForm
080: .getDocument();
081:
082: // preform duplicate check which will forward to a question prompt if one is found
083: ActionForward forward = performDuplicateCreditMemoCheck(
084: mapping, form, request, response, creditMemoDocument);
085: if (forward != null) {
086:
087: return forward;
088: }
089:
090: // perform validation of init tab
091: SpringContext.getBean(CreditMemoService.class)
092: .populateAndSaveCreditMemo(creditMemoDocument);
093:
094: // sort below the line (doesn't really need to be done on CM, but will help if we ever bring btl from other docs)
095: SpringContext.getBean(PurapService.class).sortBelowTheLine(
096: creditMemoDocument);
097:
098: // update the counts on the form
099: cmForm.updateItemCounts();
100:
101: return mapping.findForward(KFSConstants.MAPPING_BASIC);
102: }
103:
104: /**
105: * Clears out fields of the init tab.
106: *
107: * @param mapping An ActionMapping
108: * @param form An ActionForm
109: * @param request The HttpServletRequest
110: * @param response The HttpServletResponse
111: * @throws Exception
112: * @return An ActionForward
113: */
114: public ActionForward clearInitFields(ActionMapping mapping,
115: ActionForm form, HttpServletRequest request,
116: HttpServletResponse response) throws Exception {
117: CreditMemoForm cmForm = (CreditMemoForm) form;
118: CreditMemoDocument creditMemoDocument = (CreditMemoDocument) cmForm
119: .getDocument();
120: creditMemoDocument.clearInitFields();
121:
122: return super .refresh(mapping, form, request, response);
123: }
124:
125: /**
126: * Calls <code>CreditMemoService</code> to perform the duplicate credit memo check. If one is found, a question is setup and
127: * control is forwarded to the question action method. Coming back from the question prompt, the button that was clicked is
128: * checked, and if 'no' was selected, they are forward back to the page still in init mode.
129: *
130: * @param mapping An ActionMapping
131: * @param form An ActionForm
132: * @param request The HttpServletRequest
133: * @param response The HttpServletResponse
134: * @param creditMemoDocument The CreditMemoDocument
135: * @throws Exception
136: * @return An ActionForward
137: * @see org.kuali.module.purap.service.CreditMemoService
138: */
139: private ActionForward performDuplicateCreditMemoCheck(
140: ActionMapping mapping, ActionForm form,
141: HttpServletRequest request, HttpServletResponse response,
142: CreditMemoDocument creditMemoDocument) throws Exception {
143: ActionForward forward = null;
144: String duplicateMessage = SpringContext.getBean(
145: CreditMemoService.class).creditMemoDuplicateMessages(
146: creditMemoDocument);
147: if (StringUtils.isNotBlank(duplicateMessage)) {
148: Object question = request
149: .getParameter(KFSConstants.QUESTION_INST_ATTRIBUTE_NAME);
150: if (question == null) {
151:
152: return this
153: .performQuestionWithoutInput(
154: mapping,
155: form,
156: request,
157: response,
158: PurapConstants.PREQDocumentsStrings.DUPLICATE_INVOICE_QUESTION,
159: duplicateMessage,
160: KFSConstants.CONFIRMATION_QUESTION,
161: "continueCreditMemo", "");
162: }
163:
164: Object buttonClicked = request
165: .getParameter(KFSConstants.QUESTION_CLICKED_BUTTON);
166: if ((PurapConstants.PREQDocumentsStrings.DUPLICATE_INVOICE_QUESTION
167: .equals(question))
168: && ConfirmationQuestion.NO.equals(buttonClicked)) {
169: forward = mapping
170: .findForward(KFSConstants.MAPPING_BASIC);
171: }
172: }
173:
174: return forward;
175: }
176:
177: /**
178: * Calls methods to perform credit allowed calculation and total credit memo amount.
179: *
180: * @param apDoc An AccountsPayableDocument
181: */
182: @Override
183: protected void customCalculate(AccountsPayableDocument apDoc) {
184: CreditMemoDocument cmDocument = (CreditMemoDocument) apDoc;
185:
186: // call service method to finish up calculation
187: SpringContext.getBean(CreditMemoService.class)
188: .calculateCreditMemo(cmDocument);
189:
190: // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
191: SpringContext.getBean(KualiRuleService.class).applyRules(
192: new CalculateAccountsPayableEvent(cmDocument));
193: // }
194: }
195:
196: /**
197: * Puts a credit memo on hold, prompting for a reason before hand. This stops further approvals or routing.
198: *
199: * @param mapping An ActionMapping
200: * @param form An ActionForm
201: * @param request The HttpServletRequest
202: * @param response The HttpServletResponse
203: * @throws Exception
204: * @return An ActionForward
205: */
206: public ActionForward addHoldOnCreditMemo(ActionMapping mapping,
207: ActionForm form, HttpServletRequest request,
208: HttpServletResponse response) throws Exception {
209: String operation = "Hold ";
210:
211: PurQuestionCallback callback = new PurQuestionCallback() {
212: public void doPostQuestion(
213: AccountsPayableDocument document, String noteText)
214: throws Exception {
215: SpringContext
216: .getBean(CreditMemoService.class)
217: .addHoldOnCreditMemo(
218: (CreditMemoDocument) document, noteText);
219: }
220: };
221:
222: return askQuestionWithInput(mapping, form, request, response,
223: CMDocumentsStrings.HOLD_CM_QUESTION, operation,
224: CMDocumentsStrings.HOLD_NOTE_PREFIX,
225: PurapKeyConstants.CREDIT_MEMO_QUESTION_HOLD_DOCUMENT,
226: callback);
227: }
228:
229: /**
230: * Removes a hold on the credit memo.
231: *
232: * @param mapping An ActionMapping
233: * @param form An ActionForm
234: * @param request The HttpServletRequest
235: * @param response The HttpServletResponse
236: * @throws Exception
237: * @return An ActionForward
238: */
239: public ActionForward removeHoldFromCreditMemo(
240: ActionMapping mapping, ActionForm form,
241: HttpServletRequest request, HttpServletResponse response)
242: throws Exception {
243: String operation = "Remove Hold ";
244:
245: PurQuestionCallback callback = new PurQuestionCallback() {
246: public void doPostQuestion(
247: AccountsPayableDocument document, String noteText)
248: throws Exception {
249: SpringContext
250: .getBean(CreditMemoService.class)
251: .removeHoldOnCreditMemo(
252: (CreditMemoDocument) document, noteText);
253: }
254: };
255:
256: return askQuestionWithInput(
257: mapping,
258: form,
259: request,
260: response,
261: CMDocumentsStrings.REMOVE_HOLD_CM_QUESTION,
262: operation,
263: CMDocumentsStrings.REMOVE_HOLD_NOTE_PREFIX,
264: PurapKeyConstants.CREDIT_MEMO_QUESTION_REMOVE_HOLD_DOCUMENT,
265: callback);
266: }
267:
268: /**
269: * @see org.kuali.module.purap.web.struts.action.AccountsPayableActionBase#cancelPOActionCallbackMethod()
270: */
271: @Override
272: protected PurQuestionCallback cancelPOActionCallbackMethod() {
273: return new PurQuestionCallback() {
274: public void doPostQuestion(
275: AccountsPayableDocument document, String noteText)
276: throws Exception {
277: CreditMemoDocument cmDocument = (CreditMemoDocument) document;
278: cmDocument.setClosePurchaseOrderIndicator(true);
279: }
280: };
281: }
282:
283: /**
284: * @see org.kuali.module.purap.web.struts.action.AccountsPayableActionBase#getActionName()
285: */
286: @Override
287: public String getActionName() {
288: return PurapConstants.CREDIT_MEMO_ACTION_NAME;
289: }
290: }
|