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.businessobject.DisbursementVoucherPreConferenceDetail;
019    import org.kuali.kfs.fp.document.DisbursementVoucherConstants;
020    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
021    import org.kuali.kfs.fp.document.service.DisbursementVoucherTaxService;
022    import org.kuali.kfs.sys.KFSConstants;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.sys.document.AccountingDocument;
027    import org.kuali.kfs.sys.document.validation.GenericValidation;
028    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
029    import org.kuali.rice.kns.service.DictionaryValidationService;
030    import org.kuali.rice.kns.service.ParameterEvaluator;
031    import org.kuali.rice.kns.service.ParameterService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.KualiDecimal;
034    import org.kuali.rice.kns.util.MessageMap;
035    
036    public class DisbursementVoucherPrePaidTravelValidation extends GenericValidation {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPrePaidTravelValidation.class);
038    
039        private ParameterService parameterService;
040        private AccountingDocument accountingDocumentForValidation;
041    
042        /**
043         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
044         */
045        public boolean validate(AttributedDocumentEvent event) {
046            LOG.debug("validate start");
047            boolean isValid = true;
048            
049            DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
050            DisbursementVoucherPreConferenceDetail preConferenceDetail = document.getDvPreConferenceDetail();
051            
052            if (!isTravelPrepaidPaymentReason(document)) {
053                return true;
054            }
055            
056            MessageMap errors = GlobalVariables.getMessageMap();
057            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
058            errors.addToErrorPath(KFSPropertyConstants.DV_PRE_CONFERENCE_DETAIL);
059            
060            SpringContext.getBean(DictionaryValidationService.class).validateBusinessObjectsRecursively(preConferenceDetail, 1);
061            if (!errors.isEmpty()) {
062                errors.removeFromErrorPath(KFSPropertyConstants.DV_PRE_CONFERENCE_DETAIL);
063                errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
064                return false;
065            }
066    
067            /* check conference end date is not before conference start date */
068            if (preConferenceDetail.getDisbVchrConferenceEndDate().compareTo(preConferenceDetail.getDisbVchrConferenceStartDate()) < 0) {
069                errors.putError(KFSPropertyConstants.DISB_VCHR_CONFERENCE_END_DATE, KFSKeyConstants.ERROR_DV_CONF_END_DATE);
070                isValid = false;
071            }
072    
073            /* total on prepaid travel must equal Check Total */
074            /* if tax has been taken out, need to add back in the tax amount for the check */
075            KualiDecimal paidAmount = document.getDisbVchrCheckTotalAmount();
076            paidAmount = paidAmount.add(SpringContext.getBean(DisbursementVoucherTaxService.class).getNonResidentAlienTaxAmount(document));
077            if (paidAmount.compareTo(preConferenceDetail.getDisbVchrConferenceTotalAmt()) != 0) {
078                errors.putErrorWithoutFullErrorPath(KFSConstants.GENERAL_PREPAID_TAB_ERRORS, KFSKeyConstants.ERROR_DV_PREPAID_CHECK_TOTAL);
079                isValid = false;
080            }
081    
082            errors.removeFromErrorPath(KFSPropertyConstants.DV_PRE_CONFERENCE_DETAIL);
083            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
084    
085            return isValid;
086        }
087        
088        /**
089         * Returns whether the document's payment reason is for prepaid travel
090         * 
091         * @param disbursementVoucherDocument
092         * @return true if payment reason is for pre-paid travel reason
093         */
094        protected boolean isTravelPrepaidPaymentReason(DisbursementVoucherDocument disbursementVoucherDocument) {
095            ParameterEvaluator travelPrepaidPaymentReasonEvaluator = parameterService.getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.PREPAID_TRAVEL_PAYMENT_REASONS_PARM_NM, disbursementVoucherDocument.getDvPayeeDetail().getDisbVchrPaymentReasonCode());
096            return travelPrepaidPaymentReasonEvaluator.evaluationSucceeds();
097        }
098    
099        /**
100         * Sets the accountingDocumentForValidation attribute value.
101         * 
102         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
103         */
104        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
105            this.accountingDocumentForValidation = accountingDocumentForValidation;
106        }
107    
108        /**
109         * Sets the parameterService attribute value.
110         * @param parameterService The parameterService to set.
111         */
112        public void setParameterService(ParameterService parameterService) {
113            this.parameterService = parameterService;
114        }
115    
116        /**
117         * Gets the accountingDocumentForValidation attribute. 
118         * @return Returns the accountingDocumentForValidation.
119         */
120        public AccountingDocument getAccountingDocumentForValidation() {
121            return accountingDocumentForValidation;
122        }
123    }