View Javadoc
1   /*
2    * Copyright 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.PurapConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
22  import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
23  import org.kuali.ole.module.purap.document.PurchaseOrderAmendmentDocument;
24  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
25  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
26  import org.kuali.ole.select.OleSelectConstant;
27  import org.kuali.ole.sys.OLEPropertyConstants;
28  import org.kuali.ole.sys.businessobject.AccountingLine;
29  import org.kuali.ole.sys.document.AccountingDocument;
30  import org.kuali.rice.core.api.util.type.KualiDecimal;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kim.api.identity.Person;
33  
34  import java.util.List;
35  import java.util.Set;
36  
37  /**
38   * Accounting line authorizer for Requisition document which allows adding accounting lines at specified nodes
39   */
40  public class PurchaseOrderAccountingLineAuthorizer extends PurapAccountingLineAuthorizer {
41      private static final String NEW_UNORDERED_ITEMS_NODE = "NewUnorderedItems";
42  
43      /**
44       * Allow new lines to be rendered at NewUnorderedItems node
45       *
46       * @see org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase#renderNewLine(org.kuali.ole.sys.document.AccountingDocument, java.lang.String)
47       */
48      @Override
49      public boolean renderNewLine(AccountingDocument accountingDocument, String accountingGroupProperty) {
50          WorkflowDocument workflowDocument = ((PurchasingAccountsPayableDocument) accountingDocument).getFinancialSystemDocumentHeader().getWorkflowDocument();
51  
52          Set<String> currentRouteNodeName = workflowDocument.getCurrentNodeNames();
53  
54          //  if its in the NEW_UNORDERED_ITEMS node, then allow the new line to be drawn
55          if (PurchaseOrderAccountingLineAuthorizer.NEW_UNORDERED_ITEMS_NODE.equals(currentRouteNodeName.toString())) {
56              return true;
57          }
58  
59          if (PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_AMENDMENT_DOCUMENT.equals(workflowDocument.getDocumentTypeName()) && StringUtils.isNotBlank(accountingGroupProperty) && accountingGroupProperty.contains(PurapPropertyConstants.ITEM)) {
60              int itemNumber = determineItemNumberFromGroupProperty(accountingGroupProperty);
61              PurchaseOrderAmendmentDocument poaDoc = (PurchaseOrderAmendmentDocument) accountingDocument;
62              List<PurchaseOrderItem> items = poaDoc.getItems();
63              PurchaseOrderItem item = items.get(itemNumber);
64              return item.isNewItemForAmendment() || item.getSourceAccountingLines().size() == 0;
65          }
66  
67          return super.renderNewLine(accountingDocument, accountingGroupProperty);
68      }
69  
70      private int determineItemNumberFromGroupProperty(String accountingGroupProperty) {
71          int openBracketPos = accountingGroupProperty.indexOf("[");
72          int closeBracketPos = accountingGroupProperty.indexOf("]");
73          String itemNumberString = accountingGroupProperty.substring(openBracketPos + 1, closeBracketPos);
74          int itemNumber = new Integer(itemNumberString).intValue();
75          return itemNumber;
76      }
77  
78      @Override
79      protected boolean allowAccountingLinesAreEditable(AccountingDocument accountingDocument,
80                                                        AccountingLine accountingLine) {
81          PurApAccountingLine purapAccount = (PurApAccountingLine) accountingLine;
82          PurchaseOrderItem poItem = (PurchaseOrderItem) purapAccount.getPurapItem();
83          PurchaseOrderDocument po = (PurchaseOrderDocument) accountingDocument;
84  
85  
86          if (poItem != null && !poItem.getItemType().isAdditionalChargeIndicator()) {
87              if (!poItem.isItemActiveIndicator()) {
88                  return false;
89              }
90  
91              // if total amount has a value and is non-zero
92              if (poItem.getItemInvoicedTotalAmount() != null && poItem.getItemInvoicedTotalAmount().compareTo(new KualiDecimal(0)) != 0) {
93                  return false;
94              }
95  
96              if (po.getContainsUnpaidPaymentRequestsOrCreditMemos() && !poItem.isNewItemForAmendment()) {
97                  return false;
98              }
99  
100         }
101         return super.allowAccountingLinesAreEditable(accountingDocument, accountingLine);
102     }
103 
104     @Override
105     public boolean determineEditPermissionOnLine(AccountingDocument accountingDocument, AccountingLine accountingLine, String accountingLineCollectionProperty, boolean currentUserIsDocumentInitiator, boolean pageIsEditable) {
106         // the fields in a new line should be always editable
107         if (accountingLine.getSequenceNumber() == null) {
108             return true;
109         }
110 
111         // check the initiation permission on the document if it is in the state of preroute, but only if
112         // the PO status is not In Process.
113         WorkflowDocument workflowDocument = ((PurchasingAccountsPayableDocument) accountingDocument).getFinancialSystemDocumentHeader().getWorkflowDocument();
114         PurchaseOrderDocument poDocument = ((PurchaseOrderDocument) accountingDocument);
115 
116         if (!poDocument.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_IN_PROCESS) && (workflowDocument.isInitiated() || workflowDocument.isSaved())) {
117             if (PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_AMENDMENT_DOCUMENT.equals(workflowDocument.getDocumentTypeName())) {
118                 PurApAccountingLine purapAccount = (PurApAccountingLine) accountingLine;
119                 PurchaseOrderItem item = (PurchaseOrderItem) purapAccount.getPurapItem();
120                 // changes for jira OLE-2354
121                 return item.isNewItemForAmendment() || item.getSourceAccountingLines().size() == OleSelectConstant.ONE;
122             } else {
123                 return currentUserIsDocumentInitiator;
124             }
125         } else {
126             return true;
127         }
128     }
129 
130     /**
131      * @see org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase#getUnviewableBlocks(org.kuali.ole.sys.document.AccountingDocument, org.kuali.ole.sys.businessobject.AccountingLine, boolean, org.kuali.rice.kim.api.identity.Person)
132      */
133     @Override
134     public Set<String> getUnviewableBlocks(AccountingDocument accountingDocument, AccountingLine accountingLine, boolean newLine, Person currentUser) {
135         Set<String> unviewableBlocks = super.getUnviewableBlocks(accountingDocument, accountingLine, newLine, currentUser);
136         unviewableBlocks.remove(OLEPropertyConstants.PERCENT);
137         unviewableBlocks.remove(OLEPropertyConstants.AMOUNT);
138 
139         return unviewableBlocks;
140     }
141 }