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