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.PaymentRequestDocument;
24  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.document.validation.GenericValidation;
27  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
28  import org.kuali.rice.core.api.util.type.KualiDecimal;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.ObjectUtils;
31  
32  import java.util.List;
33  
34  public class PaymentRequestPurchaseOrderIdValidation extends GenericValidation {
35  
36      public boolean validate(AttributedDocumentEvent event) {
37          boolean valid = true;
38          PaymentRequestDocument document = (PaymentRequestDocument) event.getDocument();
39          GlobalVariables.getMessageMap().clearErrorPath();
40          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
41  
42          Integer POID = document.getPurchaseOrderIdentifier();
43  
44          PurchaseOrderDocument purchaseOrderDocument = document.getPurchaseOrderDocument();
45          if (ObjectUtils.isNull(purchaseOrderDocument)) {
46              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
47              valid &= false;
48          } else if (purchaseOrderDocument.isPendingActionIndicator()) {
49              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_PENDING_ACTION);
50              valid &= false;
51          } else if (!StringUtils.equals(purchaseOrderDocument.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN)) {
52              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_OPEN);
53              valid &= false;
54              // if the PO is pending and it is not a Retransmit, we cannot generate a Payment Request for it
55          } else {
56              // Verify that there exists at least 1 item left to be invoiced
57              //valid &= encumberedItemExistsForInvoicing(purchaseOrderDocument);
58          }
59          GlobalVariables.getMessageMap().clearErrorPath();
60          return valid;
61      }
62  
63      /**
64       * Determines if there are items with encumbrances to be invoiced on passed in
65       * purchase order document.
66       *
67       * @param document - purchase order document
68       * @return
69       */
70      protected boolean encumberedItemExistsForInvoicing(PurchaseOrderDocument document) {
71          boolean zeroDollar = true;
72          GlobalVariables.getMessageMap().clearErrorPath();
73          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
74          for (PurchaseOrderItem poi : (List<PurchaseOrderItem>) document.getItems()) {
75              // Quantity-based items
76              if (poi.getItemType().isLineItemIndicator() && poi.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
77                  KualiDecimal encumberedQuantity = poi.getItemOutstandingEncumberedQuantity() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedQuantity();
78                  if (encumberedQuantity.compareTo(KualiDecimal.ZERO) == 1) {
79                      zeroDollar = false;
80                      break;
81                  }
82              }
83              // Service Items or Below-the-line Items
84              else if (poi.getItemType().isAmountBasedGeneralLedgerIndicator() || poi.getItemType().isAdditionalChargeIndicator()) {
85                  KualiDecimal encumberedAmount = poi.getItemOutstandingEncumberedAmount() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedAmount();
86                  if (encumberedAmount.compareTo(KualiDecimal.ZERO) == 1) {
87                      zeroDollar = false;
88                      break;
89                  }
90              }
91          }
92          if (zeroDollar) {
93              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_NO_ITEMS_TO_INVOICE);
94          }
95          GlobalVariables.getMessageMap().clearErrorPath();
96          return !zeroDollar;
97      }
98  
99  }