View Javadoc
1   /*
2    * Copyright 2009 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.document.PaymentRequestDocument;
23  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24  import org.kuali.ole.module.purap.document.service.PaymentRequestService;
25  import org.kuali.ole.module.purap.document.service.PurapService;
26  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
27  import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.ole.vnd.VendorUtils;
31  import org.kuali.ole.vnd.businessobject.VendorDetail;
32  import org.kuali.ole.vnd.document.service.VendorService;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.ObjectUtils;
35  
36  public class VendorCreditMemoInitTabReferenceNumberValidation extends GenericValidation {
37  
38      private PaymentRequestService paymentRequestService;
39      private PurapService purapService;
40      private PurchaseOrderService purchaseOrderService;
41      private VendorService vendorService;
42  
43      /**
44       * Validates only one of preq, po, or vendor number was given. Then validates the existence of that number.
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          boolean valid = true;
48          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
49          if (cmDocument.getInvoiceIdentifier() == null) {
50              if (!(ObjectUtils.isNotNull(cmDocument.getPaymentRequestIdentifier()) ^ StringUtils.isNotEmpty(cmDocument.getVendorNumber()) ^ ObjectUtils.isNotNull(cmDocument.getPurchaseOrderIdentifier())) || (ObjectUtils.isNotNull(cmDocument.getPaymentRequestIdentifier()) && StringUtils.isNotEmpty(cmDocument.getVendorNumber()) && ObjectUtils.isNotNull(cmDocument.getPurchaseOrderIdentifier()))) {
51                  GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_CREDIT_MEMO_REQUIRED_FIELDS);
52                  valid = false;
53              } else {
54                  // Make sure PREQ is valid if entered
55                  Integer preqNumber = cmDocument.getPaymentRequestIdentifier();
56                  if (ObjectUtils.isNotNull(preqNumber)) {
57                      PaymentRequestDocument preq = paymentRequestService.getPaymentRequestById(preqNumber);
58                      if (ObjectUtils.isNull(preq)) {
59                          GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(PurapPropertyConstants.PAYMENT_REQUEST_ID, PurapKeyConstants.ERROR_CREDIT_MEMO_PAYMENT_REQEUEST_INVALID, preqNumber.toString());
60                          valid = false;
61                      }
62  // RICE20 : !purapService.isFullDocumentEntryCompleted(preq) ||
63                      else if ((PurapConstants.PaymentRequestStatuses.APPDOC_IN_PROCESS.equals(preq.getApplicationDocumentStatus())) || (PurapConstants.PaymentRequestStatuses.CANCELLED_STATUSES.contains(preq.getApplicationDocumentStatus()))) {
64                          GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(PurapPropertyConstants.PAYMENT_REQUEST_ID, PurapKeyConstants.ERROR_CREDIT_MEMO_PAYMENT_REQEUEST_INVALID_SATATUS, preqNumber.toString());
65                          valid = false;
66                      }
67                  }
68  
69                  // Make sure PO # is valid if entered
70                  Integer purchaseOrderID = cmDocument.getPurchaseOrderIdentifier();
71                  if (ObjectUtils.isNotNull(purchaseOrderID)) {
72                      PurchaseOrderDocument purchaseOrder = purchaseOrderService.getCurrentPurchaseOrder(purchaseOrderID);
73                      if (ObjectUtils.isNull(purchaseOrder)) {
74                          GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_CREDIT_MEMO_PURCHASE_ORDER_INVALID, purchaseOrderID.toString());
75                          valid = false;
76                      } else if (purchaseOrder.isPendingActionIndicator()) {
77                          GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_PENDING_ACTION);
78                          valid &= false;
79                      } else if (!(StringUtils.equals(purchaseOrder.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN) || StringUtils.equals(purchaseOrder.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED))) {
80                          GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_CREDIT_MEMO_PURCAHSE_ORDER_INVALID_STATUS, purchaseOrderID.toString());
81                          valid = false;
82                      }
83                  }
84  
85                  // Make sure vendorNumber is valid if entered
86                  String vendorNumber = cmDocument.getVendorNumber();
87                  if (StringUtils.isNotEmpty(vendorNumber)) {
88                      VendorDetail vendor = vendorService.getVendorDetail(VendorUtils.getVendorHeaderId(vendorNumber), VendorUtils.getVendorDetailId(vendorNumber));
89                      if (ObjectUtils.isNull(vendor)) {
90                          GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(PurapPropertyConstants.VENDOR_NUMBER, PurapKeyConstants.ERROR_CREDIT_MEMO_VENDOR_NUMBER_INVALID, vendorNumber);
91                          valid = false;
92                      }
93                  }
94              }
95          }
96  
97          return valid;
98      }
99  
100     public PaymentRequestService getPaymentRequestService() {
101         return paymentRequestService;
102     }
103 
104     public void setPaymentRequestService(PaymentRequestService paymentRequestService) {
105         this.paymentRequestService = paymentRequestService;
106     }
107 
108     public PurchaseOrderService getPurchaseOrderService() {
109         return purchaseOrderService;
110     }
111 
112     public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
113         this.purchaseOrderService = purchaseOrderService;
114     }
115 
116     public VendorService getVendorService() {
117         return vendorService;
118     }
119 
120     public void setVendorService(VendorService vendorService) {
121         this.vendorService = vendorService;
122     }
123 
124     public PurapService getPurapService() {
125         return purapService;
126     }
127 
128     public void setPurapService(PurapService purapService) {
129         this.purapService = purapService;
130     }
131 
132 }