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.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.select.businessobject.OleCreditMemoItem;
23  import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
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.krad.util.GlobalVariables;
28  
29  import java.math.BigDecimal;
30  
31  public class OleVendorCreditMemoAccountTotalValidation extends GenericValidation {
32  
33      private PurApItem itemForValidation;
34  
35      /**
36       * Verifies account percent. If the total percent does not equal 100, the validation fails.
37       */
38      @Override
39      public boolean validate(AttributedDocumentEvent event) {
40          boolean valid = true;
41  
42          // validate that the amount total
43          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
44          BigDecimal totalAmount = BigDecimal.ZERO;
45          BigDecimal desiredAmount = (itemForValidation.getTotalAmount() == null) ? new BigDecimal(0) : itemForValidation
46                  .getTotalAmount().bigDecimalValue();
47  
48          KualiDecimal prorateSurcharge = KualiDecimal.ZERO;
49  
50          OleCreditMemoItem crdtItem = (OleCreditMemoItem) itemForValidation;
51          if (crdtItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && crdtItem.getExtendedPrice() != null
52                  && crdtItem.getExtendedPrice().compareTo(KualiDecimal.ZERO) != 0) {
53              if (crdtItem.getItemSurcharge() != null
54                      && (crdtItem.getItemTypeCode().equals("ITEM") || crdtItem.getItemTypeCode()
55                      .equalsIgnoreCase("UNOR"))) {
56                  prorateSurcharge = new KualiDecimal(crdtItem.getItemSurcharge()).multiply(crdtItem.getItemQuantity());
57              }
58              if (prorateSurcharge.isNegative()) {
59                  prorateSurcharge = prorateSurcharge.negated();
60                  desiredAmount = desiredAmount.add(new BigDecimal(prorateSurcharge.toString()));
61              } else {
62                  desiredAmount = desiredAmount.subtract(new BigDecimal(prorateSurcharge.toString()));
63              }
64  
65          }
66          for (PurApAccountingLine account : itemForValidation.getSourceAccountingLines()) {
67              if (account.getAmount() != null) {
68                  totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
69              } else {
70                  totalAmount = totalAmount.add(BigDecimal.ZERO);
71              }
72          }
73          if (desiredAmount.compareTo(totalAmount) != 0 && cmDocument.getInvoiceIdentifier() == null) {
74              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
75                      PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, itemForValidation.getItemIdentifierString(),
76                      desiredAmount.toString());
77              valid = false;
78          }
79  
80  
81          return valid;
82      }
83  
84  
85      public PurApItem getItemForValidation() {
86          return itemForValidation;
87      }
88  
89  
90      public void setItemForValidation(PurApItem itemForValidation) {
91          this.itemForValidation = itemForValidation;
92      }
93  
94  }