View Javadoc
1   /*
2    * Copyright 2008 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.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                  //PurchaseOrderDocument purchaseOrderDocument =document.getPurchaseOrderDocument(invoiceItem.getPurchaseOrderIdentifier());
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                  //OlePurchaseOrderDocument purchaseOrderDocument = (OlePurchaseOrderDocument) purchaseOrderDocument1;
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                      // if the PO is pending and it is not a Retransmit, we cannot generate a Invoice for it
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          //for (OlePurchaseOrderDocument purchaseOrderDocument : document.getPurchaseOrderDocuments()) {
93          //PurchaseOrderDocument purchaseOrderDocument = document.getPurchaseOrderDocument();
94              /*if (!lineItemtypeIndicator) {
95                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
96                  valid &= false;
97              }*/
98          return valid;
99      }
100 
101     /**
102      * Determines if there are items with encumbrances to be invoiced on passed in
103      * purchase order document.
104      *
105      * @param document - purchase order document
106      * @return
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             // Quantity-based items
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             // Service Items or Below-the-line Items
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         //GlobalVariables.getMessageMap().clearErrorPath();
134         return !zeroDollar;
135     }
136 
137 }