/**
   * @see
   *     org.kuali.kfs.module.ar.document.service.InvoiceRecurrenceService#isValidEndDateAndTotalRecurrenceNumber(Date,Date,int,String)
   */
  @Override
  public boolean isValidEndDateAndTotalRecurrenceNumber(
      Date recurrenceBeginDate,
      Date recurrenceEndDate,
      Integer totalRecurrenceNumber,
      String recurrenceIntervalCode) {

    if (ObjectUtils.isNull(recurrenceBeginDate)
        || ObjectUtils.isNull(recurrenceIntervalCode)
        || ObjectUtils.isNull(recurrenceEndDate)
        || ObjectUtils.isNull(totalRecurrenceNumber)) {
      return true;
    }

    Calendar beginCalendar = Calendar.getInstance();
    beginCalendar.setTime(recurrenceBeginDate);
    Date beginDate = recurrenceBeginDate;
    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(recurrenceEndDate);
    Date endDate = recurrenceEndDate;
    Calendar nextCalendar = Calendar.getInstance();
    Date nextDate = beginDate;

    int totalRecurrences = 0;
    int addCounter = 0;
    String intervalCode = recurrenceIntervalCode;
    if (intervalCode.equals("M")) {
      addCounter = 1;
    }
    if (intervalCode.equals("Q")) {
      addCounter = 3;
    }
    /* perform this loop while begin_date is less than or equal to end_date */
    while (!(beginDate.after(endDate))) {
      beginCalendar.setTime(beginDate);
      beginCalendar.add(Calendar.MONTH, addCounter);
      beginDate = KfsDateUtils.convertToSqlDate(beginCalendar.getTime());
      totalRecurrences++;

      nextDate = beginDate;
      nextCalendar.setTime(nextDate);
      nextCalendar.add(Calendar.MONTH, addCounter);
      nextDate = KfsDateUtils.convertToSqlDate(nextCalendar.getTime());
      if (endDate.after(beginDate) && endDate.before(nextDate)) {
        totalRecurrences++;
        break;
      }
    }
    if (totalRecurrences != totalRecurrenceNumber.intValue()) {
      return false;
    }

    return true;
  }
  /** get the date before the given amount of days */
  protected Date getPastDate(Integer amount) {
    Integer pastDateAmount = -1 * amount;

    java.util.Date today = this.getDateTimeService().getCurrentDate();
    java.util.Date pastDate = DateUtils.addDays(today, pastDateAmount);

    return KfsDateUtils.convertToSqlDate(pastDate);
  }
  /**
   * @see
   *     org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService#getAllAgingInvoiceDocumentsByCustomerTypes(java.util.List,
   *     java.lang.Integer, java.sql.Date)
   */
  @Override
  public Collection<CustomerInvoiceDocument> getAllAgingInvoiceDocumentsByCustomerTypes(
      List<String> customerTypes, Integer invoiceAge, Date invoiceDueDateFrom) {
    Date pastDate = this.getPastDate(invoiceAge - 1);
    Date invoiceDueDateTo = KfsDateUtils.convertToSqlDate(DateUtils.addDays(pastDate, 1));
    LOG.info("invoiceDueDateTo" + invoiceDueDateTo);

    return customerInvoiceDocumentDao.getAllAgingInvoiceDocumentsByCustomerTypes(
        customerTypes, invoiceDueDateFrom, invoiceDueDateTo);
  }