001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.fp.document.validation.impl;
017
018 import static org.kuali.kfs.sys.KFSConstants.ACCOUNTING_LINE_ERRORS;
019 import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS;
020 import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL;
021
022 import java.util.List;
023
024 import org.kuali.kfs.fp.businessobject.ProcurementCardTargetAccountingLine;
025 import org.kuali.kfs.fp.businessobject.ProcurementCardTransactionDetail;
026 import org.kuali.kfs.fp.document.ProcurementCardDocument;
027 import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
028 import org.kuali.kfs.sys.document.validation.GenericValidation;
029 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030 import org.kuali.rice.kns.util.GlobalVariables;
031 import org.kuali.rice.kns.util.KualiDecimal;
032
033 /**
034 * Validates that an accounting line does not have a capital object object code
035 */
036 public class ProcurementCardBalanceValidation extends GenericValidation {
037 private ProcurementCardDocument accountingDocumentForValidation;
038
039 /**
040 * Validates that an accounting line does not have a capital object object code
041 * <strong>Expects an accounting line as the first a parameter</strong>
042 * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
043 */
044 public boolean validate(AttributedDocumentEvent event) {
045 ProcurementCardDocument pcDocument = (ProcurementCardDocument) getAccountingDocumentForValidation();
046
047 KualiDecimal targetTotal = pcDocument.getTargetTotal();
048 KualiDecimal sourceTotal = pcDocument.getSourceTotal();
049
050 boolean isValid = targetTotal.compareTo(sourceTotal) == 0;
051
052 if (!isValid) {
053 GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS, new String[] { sourceTotal.toString(), targetTotal.toString() });
054 }
055
056 List<ProcurementCardTransactionDetail> pcTransactionEntries = pcDocument.getTransactionEntries();
057
058 for (ProcurementCardTransactionDetail pcTransactionDetail : pcTransactionEntries) {
059 isValid &= isTransactionBalanceValid(pcTransactionDetail);
060 }
061
062 return isValid;
063 }
064
065 /**
066 * This method validates the balance of the transaction given. A procurement card transaction is in balance if
067 * the total amount of the transaction equals the total of the target accounting lines corresponding to the transaction.
068 *
069 * @param pcTransaction The transaction detail used to retrieve the procurement card transaction and target accounting
070 * lines used to check for in balance.
071 * @return True if the amounts are equal and the transaction is in balance, false otherwise.
072 */
073 protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
074 boolean inBalance = true;
075 KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
076 List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
077
078 KualiDecimal targetLineTotal = KualiDecimal.ZERO;
079
080 for (TargetAccountingLine targetLine : targetAcctingLines) {
081 targetLineTotal = targetLineTotal.add(targetLine.getAmount());
082 }
083
084 // perform absolute value check because current system has situations where amounts may be opposite in sign
085 // This will no longer be necessary following completion of KULFDBCK-1290
086 inBalance = transAmount.abs().equals(targetLineTotal.abs());
087
088 if (!inBalance) {
089 GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
090 }
091
092 return inBalance;
093 }
094
095 /**
096 * Gets the accountingDocumentForValidation attribute.
097 * @return Returns the accountingDocumentForValidation.
098 */
099 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 }