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