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.PurApAccountingLine;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.document.validation.PurchasingAccountsPayableItemPreCalculationRule;
23  import org.kuali.rice.kns.rules.DocumentRuleBase;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  
26  import java.math.BigDecimal;
27  
28  public class PurchasingAccountsPayablesItemPreCalculateDocumentRule extends DocumentRuleBase implements PurchasingAccountsPayableItemPreCalculationRule {
29  
30      public boolean checkPercentOrTotalAmountsEqual(PurApItem item) {
31          boolean valid = true;
32  
33          valid &= validatePercent(item);
34  
35          if (valid) {
36              valid &= validateTotalAmount(item);
37          }
38  
39          return valid;
40      }
41  
42      /**
43       * Verifies account percent. If the total percent does not equal 100, the validation fails.
44       */
45      public boolean validatePercent(PurApItem item) {
46          boolean valid = true;
47  
48          // validate that the percents total 100 for each item
49          BigDecimal totalPercent = BigDecimal.ZERO;
50          BigDecimal desiredPercent = new BigDecimal("100");
51          for (PurApAccountingLine account : item.getSourceAccountingLines()) {
52              if (account.getAccountLinePercent() != null) {
53                  totalPercent = totalPercent.add(account.getAccountLinePercent());
54              } else {
55                  totalPercent = totalPercent.add(BigDecimal.ZERO);
56              }
57          }
58          if (desiredPercent.compareTo(totalPercent) != 0) {
59              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
60              valid = false;
61          }
62  
63          return valid;
64      }
65  
66      /**
67       * Verifies account total. If the total does not equal item total,
68       * the validation fails.
69       */
70      public boolean validateTotalAmount(PurApItem item) {
71          boolean valid = true;
72  
73          // validate that the amount total
74          BigDecimal totalAmount = BigDecimal.ZERO;
75          BigDecimal desiredAmount =
76                  (item.getTotalAmount() == null) ? new BigDecimal(0) : item.getTotalAmount().bigDecimalValue();
77          for (PurApAccountingLine account : item.getSourceAccountingLines()) {
78              if (account.getAmount() != null) {
79                  totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
80              } else {
81                  totalAmount = totalAmount.add(BigDecimal.ZERO);
82              }
83          }
84          if (desiredAmount.compareTo(totalAmount) != 0) {
85              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
86              valid = false;
87          }
88  
89          return valid;
90      }
91  }