View Javadoc
1   /*
2    * Copyright 2013 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.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.select.businessobject.OleCreditMemoItem;
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 OleVendorCreditMemoItemExtendedPriceValidation extends GenericValidation {
30  
31      private DataDictionaryService dataDictionaryService;
32      private OleCreditMemoItem itemForValidation;
33  
34      @Override
35      public boolean validate(AttributedDocumentEvent event) {
36          boolean valid = true;
37          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
38  
39          String errorKeyPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
40          String errorKey = errorKeyPrefix + PurapPropertyConstants.EXTENDED_PRICE;
41  
42          if (itemForValidation.getExtendedPrice() != null) {
43              if (itemForValidation.getExtendedPrice().isNegative() && cmDocument.getInvoiceIdentifier() == null) {
44                  String label = dataDictionaryService.getAttributeErrorLabel(OleCreditMemoItem.class, PurapPropertyConstants.EXTENDED_PRICE);
45                  GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
46                  valid = false;
47              }
48              if (!cmDocument.isSourceVendor()) {
49                  // check cm extended price is not greater than total invoiced amount
50                  KualiDecimal invoicedAmount = null;
51                  if (cmDocument.isSourceDocumentPurchaseOrder()) {
52                      invoicedAmount = itemForValidation.getPoTotalAmount();
53                  } else {
54                      invoicedAmount = itemForValidation.getPreqTotalAmount();
55                  }
56  
57                  if (invoicedAmount == null) {
58                      invoicedAmount = KualiDecimal.ZERO;
59                  }
60  
61                  if (itemForValidation.getTotalAmount().isGreaterThan(invoicedAmount) && (itemForValidation.getItemSurcharge() == null)) {
62                      GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_EXTENDEDPRICE_TOOMUCH);
63                      valid = false;
64                  }
65              }
66  
67          }
68  
69          return valid;
70      }
71  
72      public DataDictionaryService getDataDictionaryService() {
73          return dataDictionaryService;
74      }
75  
76      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
77          this.dataDictionaryService = dataDictionaryService;
78      }
79  
80      public OleCreditMemoItem getItemForValidation() {
81          return itemForValidation;
82      }
83  
84      public void setItemForValidation(OleCreditMemoItem itemForValidation) {
85          this.itemForValidation = itemForValidation;
86      }
87  
88  
89  }