View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.web.struts;
17  
18  import org.kuali.ole.module.purap.PurapAuthorizationConstants.InvoiceEditMode;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapConstants.InvoiceStatuses;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.document.InvoiceDocument;
23  import org.kuali.ole.module.purap.document.service.PurapService;
24  import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.core.api.config.property.ConfigurationService;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
30  import org.kuali.rice.kns.service.DocumentHelperService;
31  import org.kuali.rice.kns.web.ui.ExtraButton;
32  import org.kuali.rice.kns.web.ui.HeaderField;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import java.util.List;
39  
40  /**
41   * Struts Action Form for Invoice document.
42   */
43  public class InvoiceForm extends AccountsPayableFormBase {
44  
45      /**
46       * Indicates whether tax has been calculated based on the tax area data.
47       * Tax calculation is enforced before preq can be routed for tax approval.
48       */
49      protected boolean calculatedTax;
50  
51      /**
52       * Constructs a InvoiceForm instance and sets up the appropriately casted document.
53       */
54      public InvoiceForm() {
55          super();
56          this.setNewPurchasingItemLine(setupNewPurchasingItemLine());
57          //on PO, account distribution should be read only
58          setReadOnlyAccountDistributionMethod(true);
59      }
60  
61      @Override
62      protected String getDefaultDocumentTypeName() {
63          return "OLE_PRQS";
64      }
65  
66      public boolean isCalculatedTax() {
67          return calculatedTax;
68      }
69  
70      public void setCalculatedTax(boolean calculatedTax) {
71          this.calculatedTax = calculatedTax;
72      }
73  
74      public InvoiceDocument getInvoiceDocument() {
75          return (InvoiceDocument) getDocument();
76      }
77  
78      public void setInvoiceDocument(InvoiceDocument purchaseOrderDocument) {
79          setDocument(purchaseOrderDocument);
80      }
81  
82      @Override
83      public void populateHeaderFields(WorkflowDocument workflowDocument) {
84          super.populateHeaderFields(workflowDocument);
85  
86          if (ObjectUtils.isNotNull(this.getInvoiceDocument().getPurapDocumentIdentifier())) {
87              getDocInfo().add(new HeaderField("DataDictionary.InvoiceDocument.attributes.purapDocumentIdentifier", ((InvoiceDocument) this.getDocument()).getPurapDocumentIdentifier().toString()));
88          } else {
89              getDocInfo().add(new HeaderField("DataDictionary.InvoiceDocument.attributes.purapDocumentIdentifier", "Not Available"));
90          }
91  
92          String applicationDocumentStatus = "Not Available";
93  
94          if (ObjectUtils.isNotNull(this.getInvoiceDocument().getApplicationDocumentStatus())) {
95              applicationDocumentStatus = workflowDocument.getApplicationDocumentStatus();
96          }
97  
98          getDocInfo().add(new HeaderField("DataDictionary.InvoiceDocument.attributes.applicationDocumentStatus", applicationDocumentStatus));
99      }
100 
101     /**
102      * @see PurchasingFormBase#setupNewPurchasingItemLine()
103      */
104     @Override
105     public PurApItem setupNewPurchasingItemLine() {
106         return new OlePurchaseOrderItem();
107     }
108 
109     /**
110      * Build additional Invoice specific buttons and set extraButtons list.
111      *
112      * @return - list of extra buttons to be displayed to the user
113      *         <p/>
114      *         KRAD Conversion: Performs customization of an extra button.
115      *         <p/>
116      *         No data dictionary is involved.
117      */
118     @Override
119     public List<ExtraButton> getExtraButtons() {
120         extraButtons.clear(); // clear out the extra buttons array
121         InvoiceDocument invoiceDocument = this.getInvoiceDocument();
122         String externalImageURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY);
123         String appExternalImageURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
124 
125         if (canContinue()) {
126             addExtraButton("methodToCall.continuePRQS", externalImageURL + "buttonsmall_continue.gif", "Continue");
127             addExtraButton("methodToCall.clearInitFields", externalImageURL + "buttonsmall_clear.gif", "Clear");
128         } else {
129             if (getEditingMode().containsKey(InvoiceEditMode.HOLD)) {
130                 addExtraButton("methodToCall.addHoldOnPayment", appExternalImageURL + "buttonsmall_hold.gif", "Hold");
131             }
132 
133             if (getEditingMode().containsKey(InvoiceEditMode.REMOVE_HOLD) && invoiceDocument.isHoldIndicator()) {
134                 addExtraButton("methodToCall.removeHoldFromPayment", appExternalImageURL + "buttonsmall_removehold.gif", "Remove");
135             }
136 
137             if (getEditingMode().containsKey(InvoiceEditMode.REQUEST_CANCEL)) {
138                 addExtraButton("methodToCall.requestCancelOnPayment", appExternalImageURL + "buttonsmall_requestcancel.gif", "Cancel");
139             }
140 
141             if (getEditingMode().containsKey(InvoiceEditMode.REMOVE_REQUEST_CANCEL) && invoiceDocument.isInvoiceCancelIndicator()) {
142                 addExtraButton("methodToCall.removeCancelRequestFromPayment", appExternalImageURL + "buttonsmall_remreqcanc.gif", "Remove");
143             }
144 
145             if (canCalculate()) {
146                 addExtraButton("methodToCall.calculate", appExternalImageURL + "buttonsmall_calculate.gif", "Calculate");
147             }
148 
149             if (getEditingMode().containsKey(InvoiceEditMode.ACCOUNTS_PAYABLE_PROCESSOR_CANCEL) ||
150                     getEditingMode().containsKey(InvoiceEditMode.ACCOUNTS_PAYABLE_MANAGER_CANCEL)) {
151                 if (PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED.equals(invoiceDocument.getPurchaseOrderDocument().getApplicationDocumentStatus())) {
152                     //if the PO is CLOSED, show the 'open order' button; but only if there are no pending actions on the PO
153                     if (!invoiceDocument.getPurchaseOrderDocument().isPendingActionIndicator()) {
154                         addExtraButton("methodToCall.reopenPo", appExternalImageURL + "buttonsmall_openorder.gif", "Reopen PO");
155                     }
156                 } else {
157                     if (!invoiceDocument.getFinancialSystemDocumentHeader().getWorkflowDocument().isDisapproved()) {
158                         addExtraButton("methodToCall.cancel", externalImageURL + "buttonsmall_cancel.gif", "Cancel");
159                     }
160                 }
161             }
162         }
163 
164         return extraButtons;
165     }
166 
167     /**
168      * Determines whether the current user can continue creating or clear fields of the Invoice in initial status. Conditions:
169      * - the Invoice must be in the INITIATE state; and
170      * - the user must have the authorization to initiate a Invoice.
171      *
172      * @return True if the current user can continue creating or clear fields of the initiated Invoice.
173      */
174     public boolean canContinue() {
175         // preq must be in initiated status
176         boolean can = InvoiceStatuses.APPDOC_INITIATE.equals(getInvoiceDocument().getApplicationDocumentStatus());
177 
178         // check user authorization
179         if (can) {
180             DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(getInvoiceDocument());
181             can = documentAuthorizer.canInitiate(OLEConstants.FinancialDocumentTypeCodes.INVOICE, GlobalVariables.getUserSession().getPerson());
182         }
183 
184         return can;
185     }
186 
187     /**
188      * Determine whether the current user can calculate the invoice. Conditions:
189      * - Invoice is not FullDocumentEntryCompleted, and
190      * - current user has the permission to edit the document.
191      *
192      * @return True if the current user can calculate the Invoice.
193      */
194     public boolean canCalculate() {
195         // preq must not be FullDocumentEntryCompleted
196         boolean can = !SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(getInvoiceDocument());
197 
198         // check user authorization: whoever can edit can calculate
199         can = can && documentActions.containsKey(KRADConstants.KUALI_ACTION_CAN_EDIT);
200 
201         //FIXME this is temporary so that calculate will show up at tax
202         can = can || editingMode.containsKey(InvoiceEditMode.TAX_AREA_EDITABLE);
203 
204         return can;
205     }
206 
207     /**
208      * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#shouldMethodToCallParameterBeUsed(String, String, javax.servlet.http.HttpServletRequest)
209      */
210     @Override
211     public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
212         if (KRADConstants.DISPATCH_REQUEST_PARAMETER.equals(methodToCallParameterName) &&
213                 ("changeUseTaxIndicator".equals(methodToCallParameterValue))) {
214             return true;
215         }
216         return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
217     }
218 
219 }
220