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