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.PurchasingAccountsPayableDocumentBase;
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  import java.math.BigDecimal;
29  
30  public class PurchasingAccountsPayablesItemPreCalculateValidations extends GenericValidation {
31  
32      private PurApItem item;
33  
34      /**
35       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
36       */
37      @Override
38      public boolean validate(AttributedDocumentEvent event) {
39  
40          PurchasingAccountsPayableDocumentBase purApDocument = (PurchasingAccountsPayableDocumentBase) event.getDocument();
41          String accountDistributionMethod = purApDocument.getAccountDistributionMethod();
42  
43          // OLE-3405 : disabling the distribution method choice
44          // JHK : In this case, we are going to ensure that the amounts and percents add up
45          // since that matches the OLE behavior where they both should be populated after a calculate.
46          //if (PurapConstants.AccountDistributionMethodCodes.SEQUENTIAL_CODE.equalsIgnoreCase(accountDistributionMethod)) {
47          return this.checkTotalPercentAndTotalAmountsEqual(item);
48          //}
49  
50          //return this.checkTotalPercentOrTotalAmountsEqual(item);
51      }
52  
53      /**
54       * checks for both percent = 100% and item total = account amount total
55       *
56       * @param item
57       * @return true when percent = 100% AND total amount = item total
58       */
59      public boolean checkTotalPercentAndTotalAmountsEqual(PurApItem item) {
60          boolean valid = true;
61  
62          valid &= validateTotalPercent(item, true);
63  
64          if (valid) {
65              valid &= validateTotalAmount(item, true);
66          }
67  
68          return valid;
69      }
70  
71      /**
72       * checks for only either percent = 100% or item total = account amount total
73       *
74       * @param item
75       * @return true when either percent = 100% OR total amount = item total
76       */
77      public boolean checkTotalPercentOrTotalAmountsEqual(PurApItem item) {
78          boolean valid = false;
79  
80          boolean validPercent = validateTotalPercent(item, false);
81          if (validPercent) {
82              return true;
83          }
84  
85          boolean validAmount = validateTotalAmount(item, false);
86          if (validAmount) {
87              return true;
88          }
89  
90          KualiDecimal desiredAmount = (item.getTotalAmount() == null) ? new KualiDecimal(0) : item.getTotalAmount();
91  
92          if (!validPercent && !validAmount) {
93              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_PERCENT_OR_AMOUNT_INVALID, item.getItemIdentifierString(), desiredAmount.toString());
94          } else {
95              if (!validPercent) {
96                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
97              } else {
98                  if (!validAmount) {
99                      GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
100                 }
101             }
102         }
103 
104         return valid;
105     }
106 
107     /**
108      * Verifies account percent. If the total percent does not equal 100,
109      * the validation fails.
110      *
111      * @param item
112      * @param writeErrorMessage true if error message to be added to global error variables, else false
113      * @return true if percent sum = 100%
114      */
115     public boolean validateTotalPercent(PurApItem item, boolean writeErrorMessage) {
116         boolean valid = true;
117 
118         if (item.getSourceAccountingLines().size() == 0) {
119             return valid;
120         }
121 
122         // validate that the percents total 100 for each item
123         BigDecimal totalPercent = BigDecimal.ZERO;
124         BigDecimal desiredPercent = new BigDecimal("100");
125         for (PurApAccountingLine account : item.getSourceAccountingLines()) {
126             if (account.getAccountLinePercent() != null) {
127                 totalPercent = totalPercent.add(account.getAccountLinePercent());
128             } else {
129                 totalPercent = totalPercent.add(BigDecimal.ZERO);
130             }
131         }
132         if (desiredPercent.compareTo(totalPercent) != 0) {
133             if (writeErrorMessage) {
134                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
135             }
136 
137             valid = false;
138         }
139 
140         return valid;
141     }
142 
143     /**
144      * Verifies account amounts = item total. If does not equal then validation fails.
145      *
146      * @param item
147      * @param writeErrorMessage true if error message to be added to global error variables, else false
148      * @return true if account amounts sum = item total
149      */
150     public boolean validateTotalAmount(PurApItem item, boolean writeErrorMessage) {
151         boolean valid = true;
152 
153         if (item.getSourceAccountingLines().size() == 0) {
154             return valid;
155         }
156 
157         if (item.getItemQuantity() == null || item.getItemUnitPrice() == null || item.getTotalAmount().compareTo(KualiDecimal.ZERO) == 0) {
158             //extended cost is not available yet so do not run validations....
159             return valid;
160         }
161 
162         // validate that the amount total
163         KualiDecimal totalAmount = KualiDecimal.ZERO;
164 
165         KualiDecimal desiredAmount = (item.getTotalAmount() == null) ? new KualiDecimal(0) : item.getTotalAmount();
166         for (PurApAccountingLine account : item.getSourceAccountingLines()) {
167             if (account.getAmount() != null) {
168                 totalAmount = totalAmount.add(account.getAmount());
169             } else {
170                 totalAmount = totalAmount.add(KualiDecimal.ZERO);
171             }
172         }
173 
174         if (desiredAmount.compareTo(totalAmount) != 0) {
175             if (writeErrorMessage) {
176                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
177             }
178             valid = false;
179         }
180 
181         return valid;
182     }
183 
184     /**
185      * Sets the accountingDocumentForValidation attribute value.
186      *
187      * @param accountingDocumentForValidation
188      *         The accountingDocumentForValidation to set.
189      */
190     public void setItem(PurApItem item) {
191         this.item = item;
192     }
193 }