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.fp.document.validation.impl;
17  
18  import static org.kuali.ole.sys.OLEConstants.ACCOUNTING_LINE_ERRORS;
19  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS;
20  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL;
21  
22  import java.util.List;
23  
24  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
25  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
26  import org.kuali.ole.fp.document.ProcurementCardDocument;
27  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.rice.core.api.util.type.KualiDecimal;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  /**
34   * Validates that an accounting line does not have a capital object object code 
35   */
36  public class ProcurementCardBalanceValidation extends GenericValidation {
37      private ProcurementCardDocument accountingDocumentForValidation;
38  
39      /**
40       * Validates that an accounting line does not have a capital object object code
41       * <strong>Expects an accounting line as the first a parameter</strong>
42       * @see org.kuali.ole.sys.document.validation.Validation#validate(java.lang.Object[])
43       */
44      public boolean validate(AttributedDocumentEvent event) {
45          ProcurementCardDocument pcDocument = (ProcurementCardDocument) getAccountingDocumentForValidation();
46  
47          KualiDecimal targetTotal = pcDocument.getTargetTotal();
48          KualiDecimal sourceTotal = pcDocument.getSourceTotal();
49  
50          boolean isValid = targetTotal.compareTo(sourceTotal) == 0;
51  
52          if (!isValid) {
53              GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS, new String[] { sourceTotal.toString(), targetTotal.toString() });
54          }
55  
56          List<ProcurementCardTransactionDetail> pcTransactionEntries = pcDocument.getTransactionEntries();
57  
58          for (ProcurementCardTransactionDetail pcTransactionDetail : pcTransactionEntries) {
59              isValid &= isTransactionBalanceValid(pcTransactionDetail);
60          }
61  
62          return isValid;
63      }
64  
65      /**
66       * This method validates the balance of the transaction given.  A procurement card transaction is in balance if 
67       * the total amount of the transaction equals the total of the target accounting lines corresponding to the transaction.
68       * 
69       * @param pcTransaction The transaction detail used to retrieve the procurement card transaction and target accounting 
70       *                      lines used to check for in balance.
71       * @return True if the amounts are equal and the transaction is in balance, false otherwise.
72       */
73      protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
74          boolean inBalance = true;
75          KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
76          List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
77  
78          KualiDecimal targetLineTotal = KualiDecimal.ZERO;
79  
80          for (TargetAccountingLine targetLine : targetAcctingLines) {
81              targetLineTotal = targetLineTotal.add(targetLine.getAmount());
82          }
83  
84          // perform absolute value check because current system has situations where amounts may be opposite in sign
85          // This will no longer be necessary following completion of KULFDBCK-1290
86          inBalance = transAmount.abs().equals(targetLineTotal.abs());
87  
88          if (!inBalance) {
89              GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
90          }
91  
92          return inBalance;
93      }
94  
95      /**
96       * Gets the accountingDocumentForValidation attribute. 
97       * @return Returns the accountingDocumentForValidation.
98       */
99      public ProcurementCardDocument getAccountingDocumentForValidation() {
100         return accountingDocumentForValidation;
101     }
102 
103     /**
104      * Sets the accountingDocumentForValidation attribute value.
105      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
106      */
107     public void setAccountingDocumentForValidation(ProcurementCardDocument accountingDocumentForValidation) {
108         this.accountingDocumentForValidation = accountingDocumentForValidation;
109     }
110 
111     
112 }