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 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) document.getItems()) {
50 if (invoiceItem.getItemType().isLineItemIndicator()) {
51 lineItemtypeIndicator = true;
52 }
53
54 PurchaseOrderDocument purchaseOrderDocument = null ;
55 Map map = new HashMap();
56 map.put("purapDocumentIdentifier",invoiceItem.getPurchaseOrderIdentifier());
57 List<OlePurchaseOrderDocument> purchaseOrderDocumentList = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
58 if(purchaseOrderDocumentList!=null && purchaseOrderDocumentList.size()>0){
59 for (OlePurchaseOrderDocument poDoc : purchaseOrderDocumentList) {
60 if(poDoc.getPurchaseOrderCurrentIndicatorForSearching()){
61 purchaseOrderDocument = poDoc;
62 }
63 }
64 }
65 if (purchaseOrderDocument != null && purchaseOrderDocument.getDocumentHeader() == null) {
66 purchaseOrderDocument.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(purchaseOrderDocument.getDocumentNumber()));
67
68 }
69
70 if (purchaseOrderDocument != null && purchaseOrderDocument.isPendingActionIndicator()) {
71 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_PENDING_ACTION);
72 valid &= false;
73 } else if (purchaseOrderDocument != null && !StringUtils.equals(purchaseOrderDocument.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN)) {
74 closedVendorIds.add(purchaseOrderDocument.getPurapDocumentIdentifier());
75
76 }
77 }
78 if(closedVendorIds.size() > 0){
79 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_POS_NOT_OPEN,closedVendorIds.toString().replace("[" ,"").replace("]",""));
80 valid &= false;
81 }
82 } else {
83 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
84 valid &= false;
85 }
86
87
88
89
90
91
92
93
94 return valid;
95 }
96
97
98
99
100
101
102
103
104 protected boolean encumberedItemExistsForInvoicing(PurchaseOrderDocument document) {
105 boolean zeroDollar = true;
106 GlobalVariables.getMessageMap().clearErrorPath();
107 GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
108 for (PurchaseOrderItem poi : (List<PurchaseOrderItem>) document.getItems()) {
109
110 if (poi.getItemType().isLineItemIndicator() && poi.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
111 KualiDecimal encumberedQuantity = poi.getItemOutstandingEncumberedQuantity() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedQuantity();
112 if (encumberedQuantity.compareTo(KualiDecimal.ZERO) == 1) {
113 zeroDollar = false;
114 break;
115 }
116 }
117
118 else if (poi.getItemType().isAmountBasedGeneralLedgerIndicator() || poi.getItemType().isAdditionalChargeIndicator()) {
119 KualiDecimal encumberedAmount = poi.getItemOutstandingEncumberedAmount() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedAmount();
120 if (encumberedAmount.compareTo(KualiDecimal.ZERO) == 1) {
121 zeroDollar = false;
122 break;
123 }
124 }
125 }
126 if (zeroDollar) {
127 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_NO_ITEMS_TO_INVOICE);
128 }
129
130 return !zeroDollar;
131 }
132
133 }