View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.document.validation.impl;
20  
21  import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
22  
23  import java.sql.Date;
24  import java.util.Calendar;
25  
26  import org.kuali.kfs.module.ar.ArKeyConstants;
27  import org.kuali.kfs.module.ar.ArPropertyConstants;
28  import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
29  import org.kuali.kfs.sys.document.validation.GenericValidation;
30  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.kfs.sys.util.KfsDateUtils;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  public class CustomerInvoiceBothEndDateAndTotalRecurrenceNumberValidation extends GenericValidation {
36      
37      private CustomerInvoiceDocument customerInvoiceDocument;
38  
39      public boolean validate(AttributedDocumentEvent event) {
40          
41          // short circuit if no recurrence object at all
42          if (ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails())) {
43              return true;
44          }
45          
46          if (ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceBeginDate()) || 
47              ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceIntervalCode()) ||
48              ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceEndDate()) ||
49              ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentTotalRecurrenceNumber())) {
50              return true;
51          }
52  
53          Calendar beginCalendar = Calendar.getInstance();
54          beginCalendar.setTime(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceBeginDate());
55          Date beginDate = customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceBeginDate();
56          Calendar endCalendar = Calendar.getInstance();
57          endCalendar.setTime(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceEndDate());
58          Date endDate = customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceEndDate();
59          Calendar nextCalendar = Calendar.getInstance();
60          Date nextDate = beginDate;
61                   
62          int totalRecurrences = 0;
63          int addCounter = 0;
64          String intervalCode = customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceIntervalCode();
65          if (intervalCode.equals("M")) {
66              addCounter = 1;
67          }
68          if (intervalCode.equals("Q")) {
69              addCounter = 3;
70          }
71          /* perform this loop while begin_date is less than or equal to end_date */
72          while (!(beginDate.after(endDate))){
73              beginCalendar.setTime(beginDate);
74              beginCalendar.add(Calendar.MONTH, addCounter);
75              beginDate = KfsDateUtils.convertToSqlDate(beginCalendar.getTime());
76              totalRecurrences++;
77              nextDate = beginDate;
78              nextCalendar.setTime(nextDate);
79              nextCalendar.add(Calendar.MONTH, addCounter);
80              nextDate = KfsDateUtils.convertToSqlDate(nextCalendar.getTime());
81              if (endDate.after(beginDate) && endDate.before(nextDate)) {
82                  totalRecurrences++;
83                  break;
84              }
85          }
86          if (totalRecurrences != customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentTotalRecurrenceNumber().intValue()) {
87              GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.INVOICE_DOCUMENT_RECURRENCE_END_DATE, ArKeyConstants.ERROR_END_DATE_AND_TOTAL_NUMBER_OF_RECURRENCES_NOT_VALID);
88              return false;
89          }
90          return true;
91      }
92  
93      public CustomerInvoiceDocument getCustomerInvoiceDocument() {
94          return customerInvoiceDocument;
95      }
96  
97      public void setCustomerInvoiceDocument(CustomerInvoiceDocument customerInvoiceDocument) {
98          this.customerInvoiceDocument = customerInvoiceDocument;
99      }
100 }