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_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL;
020    
021    import java.util.List;
022    
023    import org.kuali.kfs.fp.businessobject.ProcurementCardTargetAccountingLine;
024    import org.kuali.kfs.fp.businessobject.ProcurementCardTransactionDetail;
025    import org.kuali.kfs.fp.document.ProcurementCardDocument;
026    import org.kuali.kfs.sys.businessobject.AccountingLine;
027    import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
028    import org.kuali.kfs.sys.document.AccountingDocument;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.util.GlobalVariables;
032    import org.kuali.rice.kns.util.KualiDecimal;
033    
034    /**
035     * Validates that an accounting line does not have a capital object object code 
036     */
037    public class ProcurementCardFixErrorPathValidation extends GenericValidation {
038        private AccountingLine accountingLineForValidation;
039        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardFixErrorPathValidation.class);
040    
041        /**
042         * Validates that an accounting line does not have a capital object object code
043         * <strong>Expects an accounting line as the first a parameter</strong>
044         * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
045         */
046        public boolean validate(AttributedDocumentEvent event) {
047           ProcurementCardErrorPathUtil.fixErrorPath((AccountingDocument)event.getDocument(), accountingLineForValidation);
048            return true;
049        }
050    
051        /**
052         * This method validates the balance of the transaction given.  A procurement card transaction is in balance if 
053         * the total amount of the transaction equals the total of the target accounting lines corresponding to the transaction.
054         * 
055         * @param pcTransaction The transaction detail used to retrieve the procurement card transaction and target accounting 
056         *                      lines used to check for in balance.
057         * @return True if the amounts are equal and the transaction is in balance, false otherwise.
058         */
059        protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
060            boolean inBalance = true;
061            KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
062            List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
063    
064            KualiDecimal targetLineTotal = KualiDecimal.ZERO;
065    
066            for (TargetAccountingLine targetLine : targetAcctingLines) {
067                targetLineTotal = targetLineTotal.add(targetLine.getAmount());
068            }
069    
070            // perform absolute value check because current system has situations where amounts may be opposite in sign
071            // This will no longer be necessary following completion of KULFDBCK-1290
072            inBalance = transAmount.abs().equals(targetLineTotal.abs());
073    
074            if (!inBalance) {
075                GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
076            }
077    
078            return inBalance;
079        }
080    
081        /**
082         * Gets the accountingLineForValidation attribute. 
083         * @return Returns the accountingLineForValidation.
084         */
085        public AccountingLine getAccountingLineForValidation() {
086            return accountingLineForValidation;
087        }
088    
089        /**
090         * Sets the accountingLineForValidation attribute value.
091         * @param accountingLineForValidation The accountingLineForValidation to set.
092         */
093        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
094            this.accountingLineForValidation = accountingLineForValidation;
095        }
096    
097       
098    }