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