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.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.businessobject.InvoiceItem;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.document.InvoiceDocument;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.ole.sys.document.validation.GenericValidation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.core.api.util.type.KualiDecimal;
27  import org.kuali.rice.kns.util.KNSGlobalVariables;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  import java.util.List;
32  
33  public class InvoiceTotalsValidation extends GenericValidation {
34  
35      @Override
36      public boolean validate(AttributedDocumentEvent event) {
37          InvoiceDocument document = (InvoiceDocument) event.getDocument();
38          GlobalVariables.getMessageMap().clearErrorPath();
39          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
40  
41          String[] excludeArray = {PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE};
42  
43          // if NO invoice amount
44          if (ObjectUtils.isNull(document.getVendorInvoiceAmount())) {
45              if (!KNSGlobalVariables.getMessageList().contains(PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID)) {
46                  KNSGlobalVariables.getMessageList().add(PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID);
47              }
48          }
49          // if UseTax is included, then the invoiceInitialAmount should be compared against the
50          // total amount NOT INCLUDING tax
51          else if (document.isUseTaxIndicator()) {
52              if (document.getTotalPreTaxDollarAmountAllItems(excludeArray).compareTo(document.getVendorInvoiceAmount()) != 0
53                      && !document.isUnmatchedOverride()) {
54                  if (!KNSGlobalVariables.getMessageList().contains(
55                          PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID)) {
56                      KNSGlobalVariables.getMessageList().add(
57                              PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID);
58                  }
59              }
60          }
61          // if NO UseTax, then the invoiceInitialAmount should be compared against the
62          // total amount INCLUDING sales tax (since if the vendor invoices with sales tax, then we pay it)
63          else {
64              if (document.getTotalDollarAmountAllItems(excludeArray).compareTo(document.getVendorInvoiceAmount()) != 0
65                      && !document.isUnmatchedOverride()) {
66                  if (!KNSGlobalVariables.getMessageList().contains(
67                          PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID)) {
68                      KNSGlobalVariables.getMessageList().add(
69                              PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID);
70                  }
71              }
72          }
73  
74          flagLineItemTotals(document.getItems());
75  
76          GlobalVariables.getMessageMap().clearErrorPath();
77  
78          //always returns true, as this is a warning, not an error
79          return true;
80      }
81  
82      /**
83       * Calculates a total but excludes passed in item types from the totalling.
84       *
85       * @param itemList          - list of purap items
86       * @param excludedItemTypes - list of item types to exclude from totalling
87       * @return
88       */
89      protected KualiDecimal getTotalExcludingItemTypes(List<PurApItem> itemList, List<String> excludedItemTypes) {
90          KualiDecimal total = KualiDecimal.ZERO;
91          for (PurApItem item : itemList) {
92              if (item.getTotalAmount() != null && item.getTotalAmount().isNonZero()) {
93                  boolean skipThisItem = false;
94                  if (excludedItemTypes.contains(item.getItemTypeCode())) {
95                      // this item type is excluded
96                      skipThisItem = true;
97                      break;
98                  }
99                  if (skipThisItem) {
100                     continue;
101                 }
102                 total = total.add(item.getTotalAmount());
103             }
104         }
105         return total;
106     }
107 
108     /**
109      * Flags with an erorr the item totals whos calculated extended price does not equal its extended price.
110      *
111      * @param itemList - list of purap items
112      */
113     protected void flagLineItemTotals(List<PurApItem> itemList) {
114         for (PurApItem purApItem : itemList) {
115             InvoiceItem item = (InvoiceItem) purApItem;
116             if (item.getItemQuantity() != null && item.getExtendedPrice() != null) {
117                 if (item.calculateExtendedPrice().compareTo(item.getExtendedPrice()) != 0) {
118                     GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_PAYMENT_REQUEST_ITEM_TOTAL_NOT_EQUAL, item.getItemIdentifierString());
119                 }
120             }
121         }
122     }
123 
124 }