1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.validation.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.module.purap.PurapConstants;
20 import org.kuali.ole.module.purap.PurapKeyConstants;
21 import org.kuali.ole.module.purap.PurapPropertyConstants;
22 import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
23 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24 import org.kuali.ole.select.businessobject.OleInvoiceItem;
25 import org.kuali.ole.select.document.OleInvoiceDocument;
26 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
27 import org.kuali.ole.sys.OLEPropertyConstants;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.document.validation.GenericValidation;
30 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
31 import org.kuali.rice.core.api.util.type.KualiDecimal;
32 import org.kuali.rice.krad.service.DocumentHeaderService;
33 import org.kuali.rice.krad.service.KRADServiceLocator;
34 import org.kuali.rice.krad.util.GlobalVariables;
35
36 import java.util.*;
37
38 public class InvoicePurchaseOrderIdValidation extends GenericValidation {
39
40 public boolean validate(AttributedDocumentEvent event) {
41 boolean valid = true;
42 boolean lineItemtypeIndicator = false;
43 OleInvoiceDocument document = (OleInvoiceDocument) event.getDocument();
44 GlobalVariables.getMessageMap().clearErrorPath();
45 GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
46 Integer POID = document.getPurchaseOrderIdentifier();
47 if (document.getItems().size() > 0) {
48 Set closedVendorIds = new TreeSet();
49 Set pendingActionPoIds = new TreeSet();
50 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) document.getItems()) {
51 if (invoiceItem.getItemType().isLineItemIndicator()) {
52 lineItemtypeIndicator = true;
53 }
54
55 PurchaseOrderDocument purchaseOrderDocument = null ;
56 Map map = new HashMap();
57 map.put("purapDocumentIdentifier",invoiceItem.getPurchaseOrderIdentifier());
58 List<OlePurchaseOrderDocument> purchaseOrderDocumentList = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
59 if(purchaseOrderDocumentList!=null && purchaseOrderDocumentList.size()>0){
60 for (OlePurchaseOrderDocument poDoc : purchaseOrderDocumentList) {
61 if(poDoc.getPurchaseOrderCurrentIndicatorForSearching()){
62 purchaseOrderDocument = poDoc;
63 }
64 }
65 }
66 if (purchaseOrderDocument != null && purchaseOrderDocument.getDocumentHeader() == null) {
67 purchaseOrderDocument.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(purchaseOrderDocument.getDocumentNumber()));
68
69 }
70
71 if (purchaseOrderDocument != null && purchaseOrderDocument.isPendingActionIndicator()) {
72 pendingActionPoIds.add(purchaseOrderDocument.getPurapDocumentIdentifier());
73 } else if (purchaseOrderDocument != null && !StringUtils.equals(purchaseOrderDocument.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN)) {
74 closedVendorIds.add(purchaseOrderDocument.getPurapDocumentIdentifier());
75
76 }
77 }
78 if(pendingActionPoIds.size() > 0) {
79 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_PENDING_ACTION,pendingActionPoIds.toString().replace("[","").replace("]",""));
80 valid &= false;
81 }
82 if(closedVendorIds.size() > 0){
83 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_POS_NOT_OPEN,closedVendorIds.toString().replace("[" ,"").replace("]",""));
84 valid &= false;
85 }
86 } else {
87 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
88 valid &= false;
89 }
90
91
92
93
94
95
96
97
98 return valid;
99 }
100
101
102
103
104
105
106
107
108 protected boolean encumberedItemExistsForInvoicing(PurchaseOrderDocument document) {
109 boolean zeroDollar = true;
110 GlobalVariables.getMessageMap().clearErrorPath();
111 GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
112 for (PurchaseOrderItem poi : (List<PurchaseOrderItem>) document.getItems()) {
113
114 if (poi.getItemType().isLineItemIndicator() && poi.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
115 KualiDecimal encumberedQuantity = poi.getItemOutstandingEncumberedQuantity() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedQuantity();
116 if (encumberedQuantity.compareTo(KualiDecimal.ZERO) == 1) {
117 zeroDollar = false;
118 break;
119 }
120 }
121
122 else if (poi.getItemType().isAmountBasedGeneralLedgerIndicator() || poi.getItemType().isAdditionalChargeIndicator()) {
123 KualiDecimal encumberedAmount = poi.getItemOutstandingEncumberedAmount() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedAmount();
124 if (encumberedAmount.compareTo(KualiDecimal.ZERO) == 1) {
125 zeroDollar = false;
126 break;
127 }
128 }
129 }
130 if (zeroDollar) {
131 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_NO_ITEMS_TO_INVOICE);
132 }
133
134 return !zeroDollar;
135 }
136
137 }