View Javadoc

1   /*
2    * Copyright 2008-2009 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.authorization;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapAuthorizationConstants.InvoiceEditMode;
20  import org.kuali.ole.module.purap.PurapConstants.InvoiceStatuses;
21  import org.kuali.ole.module.purap.PurapConstants.PurchaseOrderStatuses;
22  import org.kuali.ole.module.purap.PurapParameterConstants;
23  import org.kuali.ole.module.purap.businessobject.InvoiceItem;
24  import org.kuali.ole.module.purap.document.InvoiceDocument;
25  import org.kuali.ole.module.purap.document.service.PurapService;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.OleAuthorizationConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.service.FinancialSystemWorkflowHelperService;
30  import org.kuali.ole.sys.service.impl.OleParameterConstants;
31  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
32  import org.kuali.rice.kew.api.WorkflowDocument;
33  import org.kuali.rice.kew.api.action.ActionType;
34  import org.kuali.rice.kew.api.action.ValidActions;
35  import org.kuali.rice.krad.document.Document;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  import java.util.ArrayList;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Set;
43  
44  
45  public class InvoiceDocumentPresentationController extends PurchasingAccountsPayableDocumentPresentationController {
46  
47      Boolean canHold;
48      Boolean canRequestCancel;
49      Boolean canEditPreExtraction;
50  
51      @Override
52      public boolean canSave(Document document) {
53          InvoiceDocument invoiceDocument = (InvoiceDocument) document;
54  
55          if (!StringUtils.equalsIgnoreCase(invoiceDocument.getDocumentHeader().getWorkflowDocument().getStatus().name(), InvoiceStatuses.APPDOC_INITIATE)
56                 && !StringUtils.equalsIgnoreCase(invoiceDocument.getDocumentHeader().getWorkflowDocument().getStatus().name(), OLEConstants.OleInvoice.INVOICE_SAVED)) {
57              return false;
58          }
59  
60          if (canEditPreExtraction(invoiceDocument)) {
61              return true;
62          }
63  
64          return super.canSave(document);
65      }
66  
67      @Override
68      public boolean canReload(Document document) {
69          InvoiceDocument invoiceDocument = (InvoiceDocument) document;
70  
71          if (StringUtils.equals(invoiceDocument.getApplicationDocumentStatus(), InvoiceStatuses.APPDOC_INITIATE)) {
72              return false;
73          }
74  
75          if (canEditPreExtraction(invoiceDocument)) {
76              return true;
77          }
78  
79          return super.canReload(document);
80      }
81  
82      @Override
83      public boolean canCancel(Document document) {
84          //controlling the cancel button through getExtraButtons in InvoiceForm
85          return false;
86      }
87  
88      @Override
89      public boolean canApprove(Document document) {
90          InvoiceDocument invoiceDocument = (InvoiceDocument) document;
91  
92          if (invoiceDocument.isInvoiceCancelIndicator() || invoiceDocument.isHoldIndicator()) {
93              return false;
94          }
95  
96          return super.canApprove(document);
97      }
98  
99      @Override
100     public boolean canDisapprove(Document document) {
101 
102         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
103         if (workflowDocument.isEnroute()) {
104             ValidActions validActions = workflowDocument.getValidActions();
105             return validActions.getValidActions().contains(ActionType.DISAPPROVE);
106         }
107 
108         return super.canDisapprove(document);
109     }
110 
111     /**
112      * @see org.kuali.rice.krad.document.DocumentPresentationControllerBase#canEdit(org.kuali.rice.krad.document.Document)
113      */
114     @Override
115     public boolean canEdit(Document document) {
116         InvoiceDocument invoiceDocument = (InvoiceDocument) document;
117         boolean fullDocEntryCompleted = SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(invoiceDocument);
118 
119         // if the hold or cancel indicator is true, don't allow editing
120         if (invoiceDocument.isHoldIndicator() || invoiceDocument.isInvoiceCancelIndicator()) {
121             return false;
122         }
123         if (fullDocEntryCompleted) {
124             //  after fullDocEntry is completed, only fiscal officer reviewers can edit
125             if (invoiceDocument.isDocumentStoppedInRouteNode(InvoiceStatuses.NODE_ACCOUNT_REVIEW)) {
126                 return true;
127             }
128             return false;
129         } else {
130             //before fullDocEntry is completed, document can be edited (could be preroute or enroute)
131             return true;
132         }
133     }
134 
135     /**
136      * @see org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.krad.document.Document)
137      */
138     @Override
139     public Set<String> getEditModes(Document document) {
140         Set<String> editModes = super.getEditModes(document);
141 
142         InvoiceDocument invoiceDocument = (InvoiceDocument) document;
143 
144         if (canProcessorCancel(invoiceDocument)) {
145             editModes.add(InvoiceEditMode.ACCOUNTS_PAYABLE_PROCESSOR_CANCEL);
146         }
147 
148         if (canManagerCancel(invoiceDocument)) {
149             editModes.add(InvoiceEditMode.ACCOUNTS_PAYABLE_MANAGER_CANCEL);
150         }
151 
152         if (canHold(invoiceDocument)) {
153             editModes.add(InvoiceEditMode.HOLD);
154         }
155 
156         if (canRequestCancel(invoiceDocument)) {
157             editModes.add(InvoiceEditMode.REQUEST_CANCEL);
158         }
159 
160         if (canRemoveHold(invoiceDocument)) {
161             editModes.add(InvoiceEditMode.REMOVE_HOLD);
162         }
163 
164         if (canRemoveRequestCancel(invoiceDocument)) {
165             editModes.add(InvoiceEditMode.REMOVE_REQUEST_CANCEL);
166         }
167 
168         if (canProcessorInit(invoiceDocument)) {
169             editModes.add(InvoiceEditMode.DISPLAY_INIT_TAB);
170         }
171 
172         if (ObjectUtils.isNotNull(invoiceDocument.getVendorHeaderGeneratedIdentifier())) {
173             editModes.add(InvoiceEditMode.LOCK_VENDOR_ENTRY);
174         }
175 
176         if (SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(invoiceDocument)) {
177             editModes.add(InvoiceEditMode.FULL_DOCUMENT_ENTRY_COMPLETED);
178         }
179         //else if (ObjectUtils.isNotNull(invoiceDocument.getPurchaseOrderDocument()) && PurchaseOrderStatuses.APPDOC_OPEN.equals(invoiceDocument.getPurchaseOrderDocument().getApplicationDocumentStatus())) {
180           /*
181             String documentTypeName = OLEConstants.FinancialDocumentTypeCodes.PAYMENT_REQUEST;
182             String nameSpaceCode = OLEConstants.CoreModuleNamespaces.SELECT;
183 
184             AttributeSet permissionDetails = new AttributeSet();
185             permissionDetails.put(KimAttributes.DOCUMENT_TYPE_NAME,documentTypeName);
186 
187             boolean canClosePO = KIMServiceLocator.getIdentityManagementService().hasPermission(GlobalVariables.getUserSession().getPerson().getPrincipalId(), nameSpaceCode,
188                     OLEConstants.OleInvoice.CAN_CLOSE_PO, permissionDetails);
189             if(canClosePO) {
190             editModes.add(InvoiceEditMode.ALLOW_CLOSE_PURCHASE_ORDER);
191             }*/
192         //  editModes.add(InvoiceEditMode.ALLOW_CLOSE_PURCHASE_ORDER);
193         // }
194 
195         //FIXME hjs: alter to restrict what AP shouldn't be allowed to edit
196         if (canEditPreExtraction(invoiceDocument)) {
197             editModes.add(InvoiceEditMode.EDIT_PRE_EXTRACT);
198         }
199 
200         // See if purap tax is enabled
201         boolean salesTaxInd = SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.ENABLE_SALES_TAX_IND);
202         if (salesTaxInd) {
203             editModes.add(InvoiceEditMode.PURAP_TAX_ENABLED);
204 
205             if (invoiceDocument.isUseTaxIndicator()) {
206                 // if use tax, don't allow editing of tax fields
207                 editModes.add(InvoiceEditMode.LOCK_TAX_AMOUNT_ENTRY);
208             } else {
209                 // display the "clear all taxes" button if doc is not using use tax
210                 editModes.add(InvoiceEditMode.CLEAR_ALL_TAXES);
211 
212             }
213         }
214 
215         // tax area tab is editable while waiting for tax review
216         if (invoiceDocument.isDocumentStoppedInRouteNode(InvoiceStatuses.NODE_VENDOR_TAX_REVIEW)) {
217             editModes.add(InvoiceEditMode.TAX_AREA_EDITABLE);
218         }
219 
220         if (PurchaseOrderStatuses.APPDOC_AWAIT_TAX_REVIEW
221                 .equals(invoiceDocument.getApplicationDocumentStatus())) {
222             editModes.add(InvoiceEditMode.TAX_AREA_EDITABLE);
223         }
224 
225 
226         // the tax tab is viewable to everyone after tax is approved
227         if (InvoiceStatuses.APPDOC_DEPARTMENT_APPROVED.equals(invoiceDocument.getApplicationDocumentStatus()) &&
228                 // if and only if the invoice has gone through tax review would TaxClassificationCode be non-empty
229                 !StringUtils.isEmpty(invoiceDocument.getTaxClassificationCode())) {
230             editModes.add(InvoiceEditMode.TAX_INFO_VIEWABLE);
231         }
232 
233         if (invoiceDocument.isDocumentStoppedInRouteNode(InvoiceStatuses.NODE_ACCOUNT_REVIEW)) {
234             // remove FULL_ENTRY because FO cannot edit rest of doc; only their own acct lines
235             editModes.add(InvoiceEditMode.RESTRICT_FISCAL_ENTRY);
236 
237             // only do line item check if the hold/cancel indicator is false, otherwise document editing should be turned off.
238             if (!invoiceDocument.isHoldIndicator() && !invoiceDocument.isInvoiceCancelIndicator()) {
239                 List lineList = new ArrayList();
240                 for (Iterator iter = invoiceDocument.getItems().iterator(); iter.hasNext(); ) {
241                     InvoiceItem item = (InvoiceItem) iter.next();
242                     lineList.addAll(item.getSourceAccountingLines());
243                     // If FO has deleted the last accounting line for an item, set entry mode to full so they can add another one
244                     if (item.getItemType().isLineItemIndicator() && item.getSourceAccountingLines().size() == 0) {
245                         editModes.add(OleAuthorizationConstants.TransactionalEditMode.EXPENSE_ENTRY);
246                     }
247                 }
248             }
249         }
250 
251         // Remove editBank edit mode if the document has been extracted
252         if (invoiceDocument.isExtracted()) {
253             editModes.remove(OLEConstants.BANK_ENTRY_EDITABLE_EDITING_MODE);
254         }
255 
256         return editModes;
257     }
258 
259     protected boolean canProcessorInit(InvoiceDocument invoiceDocument) {
260         // if Payment Request is in INITIATE status or NULL returned from getAppDocStatus
261         String status = invoiceDocument.getApplicationDocumentStatus();
262         if (StringUtils.equals(status, InvoiceStatuses.APPDOC_INITIATE)) {
263             return true;
264         }
265         return false;
266     }
267 
268 
269     protected boolean canProcessorCancel(InvoiceDocument invoiceDocument) {
270         // if Payment Request is in INITIATE status, user cannot cancel doc
271         if (canProcessorInit(invoiceDocument)) {
272             return false;
273         }
274 
275         String docStatus = invoiceDocument.getApplicationDocumentStatus();
276         boolean requestCancelIndicator = invoiceDocument.getInvoiceCancelIndicator();
277         boolean holdIndicator = invoiceDocument.isHoldIndicator();
278         boolean extracted = invoiceDocument.isExtracted();
279 
280         boolean preroute =
281                 InvoiceStatuses.APPDOC_IN_PROCESS.equals(docStatus) ||
282                         InvoiceStatuses.APPDOC_AWAITING_ACCOUNTS_PAYABLE_REVIEW.equals(docStatus);
283         boolean enroute =
284                 InvoiceStatuses.APPDOC_AWAITING_SUB_ACCT_MGR_REVIEW.equals(docStatus) ||
285                         InvoiceStatuses.APPDOC_AWAITING_FISCAL_REVIEW.equals(docStatus) ||
286                         InvoiceStatuses.APPDOC_AWAITING_ORG_REVIEW.equals(docStatus) ||
287                         InvoiceStatuses.APPDOC_AWAITING_PAYMENT_REVIEW.equals(docStatus)
288                         ||
289                         InvoiceStatuses.APPDOC_AWAITING_TAX_REVIEW.equals(docStatus);
290         boolean postroute =
291                 InvoiceStatuses.APPDOC_DEPARTMENT_APPROVED.equals(docStatus) ||
292                         InvoiceStatuses.APPDOC_AUTO_APPROVED.equals(docStatus);
293 
294         boolean can = false;
295         if (InvoiceStatuses.STATUSES_PREROUTE.contains(docStatus)) {
296             can = true;
297         } else if (InvoiceStatuses.STATUSES_ENROUTE.contains(docStatus)) {
298             can = requestCancelIndicator;
299         } else if (InvoiceStatuses.STATUSES_POSTROUTE.contains(docStatus)) {
300             can = !requestCancelIndicator && !holdIndicator && !extracted;
301         }
302 
303         return can;
304     }
305 
306     protected boolean canManagerCancel(InvoiceDocument invoiceDocument) {
307         // if Payment Request is in INITIATE status, user cannot cancel doc
308         if (canProcessorInit(invoiceDocument)) {
309             return false;
310         }
311 
312         String docStatus = invoiceDocument.getApplicationDocumentStatus();
313         boolean requestCancelIndicator = invoiceDocument.getInvoiceCancelIndicator();
314         boolean holdIndicator = invoiceDocument.isHoldIndicator();
315         boolean extracted = invoiceDocument.isExtracted();
316 
317         boolean preroute =
318                 InvoiceStatuses.APPDOC_IN_PROCESS.equals(docStatus) ||
319                         InvoiceStatuses.APPDOC_AWAITING_ACCOUNTS_PAYABLE_REVIEW.equals(docStatus);
320         boolean enroute =
321                 InvoiceStatuses.APPDOC_AWAITING_SUB_ACCT_MGR_REVIEW.equals(docStatus) ||
322                         InvoiceStatuses.APPDOC_AWAITING_FISCAL_REVIEW.equals(docStatus) ||
323                         InvoiceStatuses.APPDOC_AWAITING_ORG_REVIEW.equals(docStatus) ||
324                         InvoiceStatuses.APPDOC_AWAITING_PAYMENT_REVIEW.equals(docStatus)
325                         ||
326                         InvoiceStatuses.APPDOC_AWAITING_TAX_REVIEW.equals(docStatus);
327         boolean postroute =
328                 InvoiceStatuses.APPDOC_DEPARTMENT_APPROVED.equals(docStatus) ||
329                         InvoiceStatuses.APPDOC_AUTO_APPROVED.equals(docStatus);
330 
331         boolean can = false;
332         if (InvoiceStatuses.STATUSES_PREROUTE.contains(docStatus) ||
333                 InvoiceStatuses.STATUSES_ENROUTE.contains(docStatus)) {
334             can = true;
335         } else if (InvoiceStatuses.STATUSES_POSTROUTE.contains(docStatus)) {
336             can = !requestCancelIndicator && !holdIndicator && !extracted;
337         }
338 
339         return can;
340     }
341 
342     /**
343      * Determines whether the Invoice Hold button shall be available. Conditions:
344      * - Payment Request is not already on hold, and
345      * - Payment Request is not already being requested to be canceled, and
346      * - Payment Request has not already been extracted to PDP, and
347      * - Payment Request status is not in the list of "STATUSES_DISALLOWING_HOLD" or document is being adhoc routed; and
348      *
349      * @return True if the document state allows placing the Payment Request on hold.
350      */
351     protected boolean canHold(InvoiceDocument invoiceDocument) {
352         if (canHold == null) {
353 
354             boolean can = !invoiceDocument.isHoldIndicator()
355                     && !invoiceDocument.isInvoiceCancelIndicator()
356                     && !invoiceDocument.isExtracted();
357             if (can) {
358                 can = SpringContext.getBean(FinancialSystemWorkflowHelperService.class)
359                         .isAdhocApprovalRequestedForPrincipal(
360                                 invoiceDocument.getFinancialSystemDocumentHeader().getWorkflowDocument(),
361                                 GlobalVariables.getUserSession().getPrincipalId());
362                 can = can
363                         || !InvoiceStatuses.STATUSES_DISALLOWING_HOLD.contains(invoiceDocument
364                         .getApplicationDocumentStatus());
365             }
366             canHold = can;
367         }
368 
369         return canHold;
370     }
371 
372     /**
373      * Determines whether the Request Cancel Invoice button shall be available. Conditions:
374      * - Payment Request is not already on hold, and
375      * - Payment Request is not already being requested to be canceled, and
376      * - Payment Request has not already been extracted to PDP, and
377      * - Payment Request status is not in the list of "STATUSES_DISALLOWING_REQUEST_CANCEL" or document is being adhoc routed; and
378      *
379      * @return True if the document state allows placing the request that the Payment Request be canceled.
380      */
381     protected boolean canRequestCancel(InvoiceDocument invoiceDocument) {
382         if (canRequestCancel == null) {
383             boolean can = !invoiceDocument.isInvoiceCancelIndicator()
384                     && !invoiceDocument.isHoldIndicator() && !invoiceDocument.isExtracted();
385             if (can) {
386                 can = SpringContext.getBean(FinancialSystemWorkflowHelperService.class)
387                         .isAdhocApprovalRequestedForPrincipal(
388                                 invoiceDocument.getFinancialSystemDocumentHeader().getWorkflowDocument(),
389                                 GlobalVariables.getUserSession().getPrincipalId());
390                 can = can
391                         || !InvoiceStatuses.STATUSES_DISALLOWING_REQUEST_CANCEL.contains(invoiceDocument
392                         .getApplicationDocumentStatus());
393             }
394             canRequestCancel = can;
395         }
396         return canRequestCancel;
397     }
398 
399     /**
400      * Determines whether the Remove Hold button shall be available. Conditions:
401      * - the hold indicator is set to true
402      * <p/>
403      * Because the state of the Payment Request cannot be changed while the document is on hold,
404      * we should not have to check the state of the document to remove the hold.
405      * For example, the document should not be allowed to be approved or extracted while on hold.
406      *
407      * @return True if the document state allows removing the Payment Request from hold.
408      */
409     protected boolean canRemoveHold(InvoiceDocument invoiceDocument) {
410         return invoiceDocument.isHoldIndicator();
411     }
412 
413     /**
414      * Determines whether the Remove Request Cancel button shall be available. Conditions:
415      * - the request cancel indicator is set to true;  and
416      * <p/>
417      * Because the state of the Payment Request cannot be changed while the document is set to request cancel,
418      * we should not have to check the state of the document to remove the request cancel.
419      * For example, the document should not be allowed to be approved or extracted while set to request cancel.
420      *
421      * @return True if the document state allows removing a request that the Payment Request be canceled.
422      */
423     protected boolean canRemoveRequestCancel(InvoiceDocument invoiceDocument) {
424         return invoiceDocument.isInvoiceCancelIndicator();
425     }
426 
427     protected boolean canEditPreExtraction(InvoiceDocument invoiceDocument) {
428         if (canEditPreExtraction == null) {
429             boolean can = (!invoiceDocument.isExtracted()
430                     && !SpringContext.getBean(FinancialSystemWorkflowHelperService.class)
431                     .isAdhocApprovalRequestedForPrincipal(
432                             invoiceDocument.getFinancialSystemDocumentHeader().getWorkflowDocument(),
433                             GlobalVariables.getUserSession().getPrincipalId()) && !InvoiceStatuses.CANCELLED_STATUSES
434                     .contains(invoiceDocument.getApplicationDocumentStatus()));
435             canEditPreExtraction = can;
436         }
437         return canEditPreExtraction;
438     }
439 
440 }