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              for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) document.getItems()) {
50                  if (invoiceItem.getItemType().isLineItemIndicator()) {
51                      lineItemtypeIndicator = true;
52                  }
53                  //PurchaseOrderDocument purchaseOrderDocument =document.getPurchaseOrderDocument(invoiceItem.getPurchaseOrderIdentifier());
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                  //OlePurchaseOrderDocument purchaseOrderDocument = (OlePurchaseOrderDocument) purchaseOrderDocument1;
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                      // if the PO is pending and it is not a Retransmit, we cannot generate a Invoice for it
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          //for (OlePurchaseOrderDocument purchaseOrderDocument : document.getPurchaseOrderDocuments()) {
89          //PurchaseOrderDocument purchaseOrderDocument = document.getPurchaseOrderDocument();
90              /*if (!lineItemtypeIndicator) {
91                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
92                  valid &= false;
93              }*/
94          return valid;
95      }
96  
97      /**
98       * Determines if there are items with encumbrances to be invoiced on passed in
99       * purchase order document.
100      *
101      * @param document - purchase order document
102      * @return
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             // Quantity-based items
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             // Service Items or Below-the-line Items
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         //GlobalVariables.getMessageMap().clearErrorPath();
130         return !zeroDollar;
131     }
132 
133 }