View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import static org.kuali.ole.sys.OLEConstants.DOCUMENT_ERRORS;
19  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS;
20  
21  import java.sql.Date;
22  import java.sql.Timestamp;
23  
24  import org.kuali.ole.coa.businessobject.AccountingPeriod;
25  import org.kuali.ole.coa.service.AccountingPeriodService;
26  import org.kuali.ole.fp.document.AuxiliaryVoucherDocument;
27  import org.kuali.ole.sys.document.validation.GenericValidation;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.ole.sys.service.UniversityDateService;
30  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  /**
34   * Validation for Auxiliary Voucher documents that tests whether the accounting period for the document is within the defined grace period.
35   */
36  public class AuxiliaryVoucherAccountingPeriodWithinGracePeriodValidation extends GenericValidation {
37      private AuxiliaryVoucherDocument auxiliaryVoucherDocumentForValidation;
38      private AccountingPeriodService accountingPeriodService;
39      private UniversityDateService universityDateService;
40      private ParameterService parameterService;
41  
42      /**
43       * A validation to check if the given accounting period is within the "grace period" of the AV doc, defined in JIRA KULRNE-4634.
44       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          /*
48           * Nota bene: a full summarization of these rules can be found in the comments to KULRNE-4634
49           */
50          // first we need to get the period itself to check these things
51          boolean valid = true;
52          AccountingPeriod acctPeriod = getAccountingPeriodService().getByPeriod(getAuxiliaryVoucherDocumentForValidation().getPostingPeriodCode(), getAuxiliaryVoucherDocumentForValidation().getPostingYear());        
53  
54          Timestamp ts = new Timestamp(new java.util.Date().getTime());
55          AccountingPeriod currPeriod = getAccountingPeriodService().getByDate(new Date(ts.getTime()));
56  
57          if (acctPeriod.getUniversityFiscalYear().equals(getUniversityDateService().getCurrentFiscalYear())) {
58              if (getAccountingPeriodService().compareAccountingPeriodsByDate(acctPeriod, currPeriod) < 0) {
59                  // we've only got problems if the av's accounting period is earlier than now
60  
61                  // are we in the grace period for this accounting period?
62                  if (!getAuxiliaryVoucherDocumentForValidation().calculateIfWithinGracePeriod(new Date(ts.getTime()), acctPeriod)) {
63                      GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS);
64                      return false;
65                  }
66              }
67          }
68          else {
69              // it's not the same fiscal year, so we need to test whether we are currently
70              // in the grace period of the acctPeriod
71              if (!getAuxiliaryVoucherDocumentForValidation().calculateIfWithinGracePeriod(new Date(ts.getTime()), acctPeriod) && getAuxiliaryVoucherDocumentForValidation().isEndOfPreviousFiscalYear(acctPeriod)) {
72                  GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_ACCOUNTING_TWO_PERIODS);
73                  return false;
74              }
75          }
76  
77          return valid;
78      }
79  
80      /**
81       * Gets the auxiliaryVoucherDocumentForValidation attribute. 
82       * @return Returns the auxiliaryVoucherDocumentForValidation.
83       */
84      public AuxiliaryVoucherDocument getAuxiliaryVoucherDocumentForValidation() {
85          return auxiliaryVoucherDocumentForValidation;
86      }
87  
88      /**
89       * Sets the auxiliaryVoucherDocumentForValidation attribute value.
90       * @param auxiliaryVoucherDocumentForValidation The auxiliaryVoucherDocumentForValidation to set.
91       */
92      public void setAuxiliaryVoucherDocumentForValidation(AuxiliaryVoucherDocument accountingDocumentForValidation) {
93          this.auxiliaryVoucherDocumentForValidation = accountingDocumentForValidation;
94      }
95  
96      /**
97       * Gets the accountingPeriodService attribute. 
98       * @return Returns the accountingPeriodService.
99       */
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 }