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.Arrays;
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.KFSConstants;
028    import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kew.exception.WorkflowException;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.KualiDecimal;
034    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
035    
036    /**
037     * Validates that an accounting line does not have a capital object object code 
038     */
039    public class ProcurementCardAccountAccessibilityValidation extends GenericValidation {
040        private ProcurementCardDocument accountingDocumentForValidation;
041        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardAccountAccessibilityValidation.class);
042    
043        /**
044         * Validates that an accounting line does not have a capital object object code
045         * <strong>Expects an accounting line as the first a parameter</strong>
046         * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
047         */
048        public boolean validate(AttributedDocumentEvent event) {
049            boolean isValid = false;
050            ProcurementCardDocument pcDocument = (ProcurementCardDocument) getAccountingDocumentForValidation();
051    
052            KualiWorkflowDocument workflowDocument = pcDocument.getDocumentHeader().getWorkflowDocument();
053            List activeNodes = null;
054            try {
055                activeNodes = Arrays.asList(workflowDocument.getNodeNames());
056            }
057            catch (WorkflowException e) {
058                LOG.error("Error getting active nodes " + e.getMessage());
059                throw new RuntimeException("Error getting active nodes " + e.getMessage());
060            }
061    
062            if (workflowDocument.stateIsEnroute() && activeNodes.contains(KFSConstants.RouteLevelNames.ACCOUNT_REVIEW_FULL_EDIT)) {
063                isValid = true;
064            }
065            return isValid;
066        }
067    
068        /**
069         * This method validates the balance of the transaction given.  A procurement card transaction is in balance if 
070         * the total amount of the transaction equals the total of the target accounting lines corresponding to the transaction.
071         * 
072         * @param pcTransaction The transaction detail used to retrieve the procurement card transaction and target accounting 
073         *                      lines used to check for in balance.
074         * @return True if the amounts are equal and the transaction is in balance, false otherwise.
075         */
076        protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
077            boolean inBalance = true;
078            KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
079            List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
080    
081            KualiDecimal targetLineTotal = KualiDecimal.ZERO;
082    
083            for (TargetAccountingLine targetLine : targetAcctingLines) {
084                targetLineTotal = targetLineTotal.add(targetLine.getAmount());
085            }
086    
087            // perform absolute value check because current system has situations where amounts may be opposite in sign
088            // This will no longer be necessary following completion of KULFDBCK-1290
089            inBalance = transAmount.abs().equals(targetLineTotal.abs());
090    
091            if (!inBalance) {
092                GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
093            }
094    
095            return inBalance;
096        }
097    
098        /**
099         * Gets the accountingDocumentForValidation attribute. 
100         * @return Returns the accountingDocumentForValidation.
101         */
102        public ProcurementCardDocument getAccountingDocumentForValidation() {
103            return accountingDocumentForValidation;
104        }
105    
106        /**
107         * Sets the accountingDocumentForValidation attribute value.
108         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
109         */
110        public void setAccountingDocumentForValidation(ProcurementCardDocument accountingDocumentForValidation) {
111            this.accountingDocumentForValidation = accountingDocumentForValidation;
112        }
113    
114        
115    }