1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45 public class PurchaseOrderAccountingLineAuthorizer extends PurapAccountingLineAuthorizer {
46 private static final String NEW_UNORDERED_ITEMS_NODE = "NewUnorderedItems";
47
48
49
50
51
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
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
97 if (poItem.getItemInvoicedTotalAmount() != null && poItem.getItemInvoicedTotalAmount().isGreaterThan(KualiDecimal.ZERO) && getOrderType(po.getPurchaseOrderTypeId())) {
98 return false;
99 }
100
101
102
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
112 if (accountingLine.getSequenceNumber() == null) {
113 return true;
114 }
115
116
117
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
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
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 }