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.select.document.OleVendorCreditMemoDocument;
22  import org.kuali.ole.sys.OLEPropertyConstants;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.core.api.util.type.KualiDecimal;
26  import org.kuali.rice.kns.service.DataDictionaryService;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  
29  public class VendorCreditMemoItemExtendedPriceValidation extends GenericValidation {
30  
31      private DataDictionaryService dataDictionaryService;
32      private CreditMemoItem itemForValidation;
33  
34      public boolean validate(AttributedDocumentEvent event) {
35          boolean valid = true;
36          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
37  
38          String errorKeyPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
39          String errorKey = errorKeyPrefix + PurapPropertyConstants.EXTENDED_PRICE;
40  
41          if (itemForValidation.getExtendedPrice() != null) {
42              if (itemForValidation.getExtendedPrice().isNegative() && cmDocument.getInvoiceIdentifier() == null) {
43                  String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.EXTENDED_PRICE);
44                  GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
45                  valid = false;
46              }
47              if (!cmDocument.isSourceVendor()) {
48                  // check cm extended price is not greater than total invoiced amount
49                  KualiDecimal invoicedAmount = null;
50                  if (cmDocument.isSourceDocumentPurchaseOrder()) {
51                      invoicedAmount = itemForValidation.getPoTotalAmount();
52                  } else {
53                      invoicedAmount = itemForValidation.getPreqTotalAmount();
54                  }
55  
56                  if (invoicedAmount == null) {
57                      invoicedAmount = KualiDecimal.ZERO;
58                  }
59  
60                  if (itemForValidation.getTotalAmount().isGreaterThan(invoicedAmount)) {
61                      GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_EXTENDEDPRICE_TOOMUCH);
62                      valid = false;
63                  }
64              }
65  
66          }
67  
68          return valid;
69      }
70  
71      public DataDictionaryService getDataDictionaryService() {
72          return dataDictionaryService;
73      }
74  
75      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
76          this.dataDictionaryService = dataDictionaryService;
77      }
78  
79      public CreditMemoItem getItemForValidation() {
80          return itemForValidation;
81      }
82  
83      public void setItemForValidation(CreditMemoItem itemForValidation) {
84          this.itemForValidation = itemForValidation;
85      }
86  
87  }