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