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.kuali.ole.module.purap.PurapKeyConstants;
19  import org.kuali.ole.module.purap.PurapPropertyConstants;
20  import org.kuali.ole.module.purap.businessobject.CreditMemoItem;
21  import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
22  import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.document.validation.GenericValidation;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.kns.service.DataDictionaryService;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  public class VendorCreditMemoItemQuantityValidation extends GenericValidation {
32  
33      private DataDictionaryService dataDictionaryService;
34      private CreditMemoItem itemForValidation;
35  
36      public boolean validate(AttributedDocumentEvent event) {
37          boolean valid = true;
38          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
39          String errorKeyPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
40          String errorKey = errorKeyPrefix + PurapPropertyConstants.QUANTITY;
41  
42          if (itemForValidation.getItemQuantity() != null) {
43              if (itemForValidation.getItemQuantity().isNegative() && cmDocument.getInvoiceIdentifier() == null) {
44                  String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
45                  GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
46                  valid = false;
47              }
48  
49              // check cm quantity is not greater than invoiced quantity
50              KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
51              if (itemForValidation.getItemQuantity().isGreaterThan(invoicedQuantity) && cmDocument.getInvoiceIdentifier() == null) {
52                  GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_QUANTITY_TOOMUCH);
53                  valid = false;
54              }
55          } else {
56              // check if quantity should be required
57              KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
58              if (cmDocument.getInvoiceIdentifier() == null && itemForValidation.getItemType().isQuantityBasedGeneralLedgerIndicator() && (invoicedQuantity != null && invoicedQuantity.isGreaterThan(KualiDecimal.ZERO)) && (itemForValidation.getExtendedPrice() != null && itemForValidation.getExtendedPrice().isGreaterThan(KualiDecimal.ZERO))) {
59                  String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
60                  GlobalVariables.getMessageMap().putError(errorKey, OLEKeyConstants.ERROR_REQUIRED, label);
61                  valid = false;
62              }
63          }
64  
65          return valid;
66      }
67  
68      /**
69       * Returns the total invoiced quantity for the item line based on the type of credit memo.
70       *
71       * @param cmDocument - credit memo document
72       * @param item       - credit memo item line to return total invoice quantity
73       * @return KualiDecimal - total invoiced quantity
74       */
75      protected KualiDecimal getSourceTotalInvoiceQuantity(VendorCreditMemoDocument cmDocument, CreditMemoItem item) {
76          KualiDecimal invoicedQuantity = null;
77  
78          if (cmDocument.isSourceDocumentPurchaseOrder()) {
79              invoicedQuantity = item.getPoInvoicedTotalQuantity();
80          } else {
81              invoicedQuantity = item.getPreqInvoicedTotalQuantity();
82          }
83  
84          return invoicedQuantity;
85      }
86  
87      public DataDictionaryService getDataDictionaryService() {
88          return dataDictionaryService;
89      }
90  
91      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
92          this.dataDictionaryService = dataDictionaryService;
93      }
94  
95      public CreditMemoItem getItemForValidation() {
96          return itemForValidation;
97      }
98  
99      public void setItemForValidation(CreditMemoItem itemForValidation) {
100         this.itemForValidation = itemForValidation;
101     }
102 
103 }