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 static org.kuali.ole.sys.OLEConstants.DOCUMENT_ERRORS;
019import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS;
020
021import java.sql.Date;
022import java.sql.Timestamp;
023
024import org.kuali.ole.coa.businessobject.AccountingPeriod;
025import org.kuali.ole.coa.service.AccountingPeriodService;
026import org.kuali.ole.fp.document.AuxiliaryVoucherDocument;
027import org.kuali.ole.sys.document.validation.GenericValidation;
028import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
029import org.kuali.ole.sys.service.UniversityDateService;
030import org.kuali.rice.coreservice.framework.parameter.ParameterService;
031import org.kuali.rice.krad.util.GlobalVariables;
032
033/**
034 * Validation for Auxiliary Voucher documents that tests whether the accounting period for the document is within the defined grace period.
035 */
036public class AuxiliaryVoucherAccountingPeriodWithinGracePeriodValidation extends GenericValidation {
037    private AuxiliaryVoucherDocument auxiliaryVoucherDocumentForValidation;
038    private AccountingPeriodService accountingPeriodService;
039    private UniversityDateService universityDateService;
040    private ParameterService parameterService;
041
042    /**
043     * A validation to check if the given accounting period is within the "grace period" of the AV doc, defined in JIRA KULRNE-4634.
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        /*
048         * Nota bene: a full summarization of these rules can be found in the comments to KULRNE-4634
049         */
050        // first we need to get the period itself to check these things
051        boolean valid = true;
052        AccountingPeriod acctPeriod = getAccountingPeriodService().getByPeriod(getAuxiliaryVoucherDocumentForValidation().getPostingPeriodCode(), getAuxiliaryVoucherDocumentForValidation().getPostingYear());        
053
054        Timestamp ts = new Timestamp(new java.util.Date().getTime());
055        AccountingPeriod currPeriod = getAccountingPeriodService().getByDate(new Date(ts.getTime()));
056
057        if (acctPeriod.getUniversityFiscalYear().equals(getUniversityDateService().getCurrentFiscalYear())) {
058            if (getAccountingPeriodService().compareAccountingPeriodsByDate(acctPeriod, currPeriod) < 0) {
059                // we've only got problems if the av's accounting period is earlier than now
060
061                // are we in the grace period for this accounting period?
062                if (!getAuxiliaryVoucherDocumentForValidation().calculateIfWithinGracePeriod(new Date(ts.getTime()), acctPeriod)) {
063                    GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS);
064                    return false;
065                }
066            }
067        }
068        else {
069            // it's not the same fiscal year, so we need to test whether we are currently
070            // in the grace period of the acctPeriod
071            if (!getAuxiliaryVoucherDocumentForValidation().calculateIfWithinGracePeriod(new Date(ts.getTime()), acctPeriod) && getAuxiliaryVoucherDocumentForValidation().isEndOfPreviousFiscalYear(acctPeriod)) {
072                GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS);
073                return false;
074            }
075        }
076
077        return valid;
078    }
079
080    /**
081     * Gets the auxiliaryVoucherDocumentForValidation attribute. 
082     * @return Returns the auxiliaryVoucherDocumentForValidation.
083     */
084    public AuxiliaryVoucherDocument getAuxiliaryVoucherDocumentForValidation() {
085        return auxiliaryVoucherDocumentForValidation;
086    }
087
088    /**
089     * Sets the auxiliaryVoucherDocumentForValidation attribute value.
090     * @param auxiliaryVoucherDocumentForValidation The auxiliaryVoucherDocumentForValidation to set.
091     */
092    public void setAuxiliaryVoucherDocumentForValidation(AuxiliaryVoucherDocument accountingDocumentForValidation) {
093        this.auxiliaryVoucherDocumentForValidation = accountingDocumentForValidation;
094    }
095
096    /**
097     * Gets the accountingPeriodService attribute. 
098     * @return Returns the accountingPeriodService.
099     */
100    public AccountingPeriodService getAccountingPeriodService() {
101        return accountingPeriodService;
102    }
103
104    /**
105     * Sets the accountingPeriodService attribute value.
106     * @param accountingPeriodService The accountingPeriodService to set.
107     */
108    public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
109        this.accountingPeriodService = accountingPeriodService;
110    }
111
112    /**
113     * Gets the universityDateService attribute. 
114     * @return Returns the universityDateService.
115     */
116    public UniversityDateService getUniversityDateService() {
117        return universityDateService;
118    }
119
120    /**
121     * Sets the universityDateService attribute value.
122     * @param universityDateService The universityDateService to set.
123     */
124    public void setUniversityDateService(UniversityDateService universityDateService) {
125        this.universityDateService = universityDateService;
126    }
127
128    /**
129     * Gets the parameterService attribute. 
130     * @return Returns the parameterService.
131     */
132    public ParameterService getParameterService() {
133        return parameterService;
134    }
135
136    /**
137     * Sets the parameterService attribute value.
138     * @param parameterService The parameterService to set.
139     */
140    public void setParameterService(ParameterService parameterService) {
141        this.parameterService = parameterService;
142    }
143}