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.DOCUMENT_ERRORS;
019    import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_AV_INCORRECT_FISCAL_YEAR_AVRC;
020    import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC;
021    import static org.kuali.kfs.sys.KFSKeyConstants.AuxiliaryVoucher.ERROR_ACCOUNTING_PERIOD_OUT_OF_RANGE;
022    
023    import java.sql.Date;
024    import java.sql.Timestamp;
025    
026    import org.kuali.kfs.coa.businessobject.AccountingPeriod;
027    import org.kuali.kfs.coa.service.AccountingPeriodService;
028    import org.kuali.kfs.fp.document.AuxiliaryVoucherDocument;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.util.GlobalVariables;
032    
033    /**
034     * Validation that checks whether the accounting period of an AuxiliaryVoucher document is allowable,
035     * but only for recode-type AVs.
036     */
037    public class AuxiliaryVoucherRecodeAccountingPeriodValidation extends GenericValidation {
038        private AuxiliaryVoucherDocument auxiliaryVoucherDocumentForValidation;
039        private AccountingPeriodService accountingPeriodService;
040    
041        /**
042         * Validates the accounting period on an AV doc if the doc is a recode type, with the following rules:
043         * <ol>
044         *  <li>The accounting period does not occur in a previous fiscal year</li>
045         *  <li>The accounting period is between 1 and 12 (13 can't be recoded, nor can the non-numeric periods: AB, BB, and CB</li>
046         * </ol>
047         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
048         */
049        public boolean validate(AttributedDocumentEvent event) {
050            AccountingPeriod acctPeriod = getAccountingPeriodService().getByPeriod(auxiliaryVoucherDocumentForValidation.getPostingPeriodCode(), auxiliaryVoucherDocumentForValidation.getPostingYear());
051            
052            if (auxiliaryVoucherDocumentForValidation.isRecodeType()) {
053                boolean numericPeriod = true;
054                Integer period = null;
055                try {
056                    period = new Integer(auxiliaryVoucherDocumentForValidation.getPostingPeriodCode());
057                }
058                catch (NumberFormatException nfe) {
059                    numericPeriod = false;
060                }
061                Integer year = auxiliaryVoucherDocumentForValidation.getPostingYear();
062                Timestamp ts = new Timestamp(new java.util.Date().getTime());
063                AccountingPeriod currPeriod = getAccountingPeriodService().getByDate(new Date(ts.getTime()));
064                
065                // can't post into a previous fiscal year
066                Integer currFiscalYear = currPeriod.getUniversityFiscalYear();
067                if (currFiscalYear > year) {
068                    GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_FISCAL_YEAR_AVRC);
069                    return false;
070                }
071                if (numericPeriod) {
072                    // check the posting period, throw out if period 13
073                    if (period > 12) {
074                        GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC);
075                        return false;
076                    }
077                    else if (period < 1) {
078                        GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_ACCOUNTING_PERIOD_OUT_OF_RANGE);
079                        return false;
080                    }
081                }
082                else {
083                    // not a numeric period and this is a recode? Then we won't allow it; ref KULRNE-6001
084                    GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC);
085                    return false;
086                }
087            }
088            
089            return true;
090        }
091    
092        /**
093         * Gets the accountingPeriodService attribute. 
094         * @return Returns the accountingPeriodService.
095         */
096        public AccountingPeriodService getAccountingPeriodService() {
097            return accountingPeriodService;
098        }
099    
100        /**
101         * Sets the accountingPeriodService attribute value.
102         * @param accountingPeriodService The accountingPeriodService to set.
103         */
104        public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
105            this.accountingPeriodService = accountingPeriodService;
106        }
107    
108        /**
109         * Gets the auxiliaryVoucherDocumentForValidation attribute. 
110         * @return Returns the auxiliaryVoucherDocumentForValidation.
111         */
112        public AuxiliaryVoucherDocument getAuxiliaryVoucherDocumentForValidation() {
113            return auxiliaryVoucherDocumentForValidation;
114        }
115    
116        /**
117         * Sets the auxiliaryVoucherDocumentForValidation attribute value.
118         * @param auxiliaryVoucherDocumentForValidation The auxiliaryVoucherDocumentForValidation to set.
119         */
120        public void setAuxiliaryVoucherDocumentForValidation(AuxiliaryVoucherDocument auxiliaryVoucherDocumentForValidation) {
121            this.auxiliaryVoucherDocumentForValidation = auxiliaryVoucherDocumentForValidation;
122        }
123    }