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_AV_INCORRECT_FISCAL_YEAR_AVRC;
20  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC;
21  import static org.kuali.ole.sys.OLEKeyConstants.AuxiliaryVoucher.ERROR_ACCOUNTING_PERIOD_OUT_OF_RANGE;
22  
23  import java.sql.Date;
24  import java.sql.Timestamp;
25  
26  import org.kuali.ole.coa.businessobject.AccountingPeriod;
27  import org.kuali.ole.coa.service.AccountingPeriodService;
28  import org.kuali.ole.fp.document.AuxiliaryVoucherDocument;
29  import org.kuali.ole.sys.document.validation.GenericValidation;
30  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  /**
34   * Validation that checks whether the accounting period of an AuxiliaryVoucher document is allowable,
35   * but only for recode-type AVs.
36   */
37  public class AuxiliaryVoucherRecodeAccountingPeriodValidation extends GenericValidation {
38      private AuxiliaryVoucherDocument auxiliaryVoucherDocumentForValidation;
39      private AccountingPeriodService accountingPeriodService;
40  
41      /**
42       * Validates the accounting period on an AV doc if the doc is a recode type, with the following rules:
43       * <ol>
44       *  <li>The accounting period does not occur in a previous fiscal year</li>
45       *  <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>
46       * </ol>
47       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
48       */
49      public boolean validate(AttributedDocumentEvent event) {
50          AccountingPeriod acctPeriod = getAccountingPeriodService().getByPeriod(auxiliaryVoucherDocumentForValidation.getPostingPeriodCode(), auxiliaryVoucherDocumentForValidation.getPostingYear());
51          
52          if (auxiliaryVoucherDocumentForValidation.isRecodeType()) {
53              boolean numericPeriod = true;
54              Integer period = null;
55              try {
56                  period = new Integer(auxiliaryVoucherDocumentForValidation.getPostingPeriodCode());
57              }
58              catch (NumberFormatException nfe) {
59                  numericPeriod = false;
60              }
61              Integer year = auxiliaryVoucherDocumentForValidation.getPostingYear();
62              Timestamp ts = new Timestamp(new java.util.Date().getTime());
63              AccountingPeriod currPeriod = getAccountingPeriodService().getByDate(new Date(ts.getTime()));
64              
65              // can't post into a previous fiscal year
66              Integer currFiscalYear = currPeriod.getUniversityFiscalYear();
67              if (currFiscalYear > year) {
68                  GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_FISCAL_YEAR_AVRC);
69                  return false;
70              }
71              if (numericPeriod) {
72                  // check the posting period, throw out if period 13
73                  if (period > 12) {
74                      GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC);
75                      return false;
76                  }
77                  else if (period < 1) {
78                      GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_ACCOUNTING_PERIOD_OUT_OF_RANGE);
79                      return false;
80                  }
81              }
82              else {
83                  // not a numeric period and this is a recode? Then we won't allow it; ref KULRNE-6001
84                  GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_AV_INCORRECT_POST_PERIOD_AVRC);
85                  return false;
86              }
87          }
88          
89          return true;
90      }
91  
92      /**
93       * Gets the accountingPeriodService attribute. 
94       * @return Returns the accountingPeriodService.
95       */
96      public AccountingPeriodService getAccountingPeriodService() {
97          return accountingPeriodService;
98      }
99  
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 }