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 org.kuali.kfs.fp.document.DisbursementVoucherDocument;
019    import org.kuali.kfs.sys.KFSConstants;
020    import org.kuali.kfs.sys.KFSKeyConstants;
021    import org.kuali.kfs.sys.KFSPropertyConstants;
022    import org.kuali.kfs.sys.document.AccountingDocument;
023    import org.kuali.kfs.sys.document.validation.GenericValidation;
024    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    import org.kuali.rice.kns.util.MessageMap;
028    
029    public class DisbursementVoucherDocumentAmountValidation extends GenericValidation {
030        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentAmountValidation.class);
031    
032        private AccountingDocument accountingDocumentForValidation;
033    
034        /**
035         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
036         */
037        public boolean validate(AttributedDocumentEvent event) {
038            LOG.debug("validate start");
039            
040            boolean isValid = true;
041            DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument) accountingDocumentForValidation;
042            
043            MessageMap errors = GlobalVariables.getMessageMap();
044            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
045    
046            /* check total cannot be negative or zero */
047            if (!disbursementVoucherDocument.getDisbVchrCheckTotalAmount().isPositive()) {
048                errors.putError(KFSPropertyConstants.DISB_VCHR_CHECK_TOTAL_AMOUNT, KFSKeyConstants.ERROR_NEGATIVE_OR_ZERO_CHECK_TOTAL);
049                isValid = false;
050            }
051    
052            /* total accounting lines cannot be negative */
053            if (KualiDecimal.ZERO.compareTo(disbursementVoucherDocument.getSourceTotal()) == 1) {
054                errors.putErrorWithoutFullErrorPath(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_NEGATIVE_ACCOUNTING_TOTAL);
055                isValid = false;
056            }
057    
058            /* total of accounting lines must match check total */
059            if (disbursementVoucherDocument.getDisbVchrCheckTotalAmount().compareTo(disbursementVoucherDocument.getSourceTotal()) != 0) {
060                errors.putErrorWithoutFullErrorPath(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_CHECK_ACCOUNTING_TOTAL);
061                isValid = false;
062            }
063            
064            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
065    
066            return isValid;
067        }
068    
069        /**
070         * Sets the accountingDocumentForValidation attribute value.
071         * 
072         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
073         */
074        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
075            this.accountingDocumentForValidation = accountingDocumentForValidation;
076        }
077    
078        /**
079         * Gets the accountingDocumentForValidation attribute. 
080         * @return Returns the accountingDocumentForValidation.
081         */
082        public AccountingDocument getAccountingDocumentForValidation() {
083            return accountingDocumentForValidation;
084        }
085    
086    }