/**
   * This method prepare the report model object to display on jsp page.
   *
   * @param invoices
   * @param results
   */
  protected void populateReportDetails(
      List<ContractsGrantsInvoiceDocument> invoices, List results) {
    for (ContractsGrantsInvoiceDocument invoice : invoices) {
      ContractsGrantsAgingOpenInvoicesReport detail = new ContractsGrantsAgingOpenInvoicesReport();
      // Document Type
      detail.setDocumentType(
          invoice.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
      // Document Number
      detail.setDocumentNumber(invoice.getDocumentNumber());
      // Document Description
      String documentDescription = invoice.getDocumentHeader().getDocumentDescription();
      if (ObjectUtils.isNotNull(documentDescription)) {
        detail.setDocumentDescription(documentDescription);
      } else {
        detail.setDocumentDescription("");
      }
      // Billing Date
      detail.setBillingDate(invoice.getBillingDate());
      // Due Date
      detail.setDueApprovedDate(invoice.getInvoiceDueDate());
      // Document Payment Amount
      detail.setDocumentPaymentAmount(
          invoice.getFinancialSystemDocumentHeader().getFinancialDocumentTotalAmount());
      // Unpaid/Unapplied Amount
      detail.setUnpaidUnappliedAmount(
          customerInvoiceDocumentService.getOpenAmountForCustomerInvoiceDocument(invoice));
      detail.setFinalInvoice(
          !ObjectUtils.isNull(invoice.getInvoiceGeneralDetail())
                  && invoice.getInvoiceGeneralDetail().isFinalBillIndicator()
              ? KFSConstants.ParameterValues.STRING_YES
              : KFSConstants.ParameterValues.STRING_NO);
      // set agency number, proposal number, account number
      if (!ObjectUtils.isNull(invoice.getInvoiceGeneralDetail())
          && !ObjectUtils.isNull(invoice.getInvoiceGeneralDetail().getProposalNumber())) {
        detail.setProposalNumber(invoice.getInvoiceGeneralDetail().getProposalNumber().toString());
      }

      // Set Agency Number
      ContractsAndGrantsBillingAgency cgAgency =
          this.getAgencyByCustomer(
              invoice.getAccountsReceivableDocumentHeader().getCustomerNumber());
      if (ObjectUtils.isNotNull(cgAgency)) {
        detail.setAgencyNumber(cgAgency.getAgencyNumber());
      }

      // Set Account number
      List<CustomerInvoiceDetail> details = invoice.getSourceAccountingLines();
      String accountNum =
          (CollectionUtils.isNotEmpty(details) && ObjectUtils.isNotNull(details.get(0)))
              ? details.get(0).getAccountNumber()
              : "";
      detail.setAccountNumber(accountNum);
      results.add(detail);
    }
  }
  /**
   * Get the search results that meet the input search criteria.
   *
   * @param fieldValues - Map containing prop name keys and search values
   * @return a List of found business objects
   */
  @Override
  public List getSearchResults(Map fieldValues) {
    List<ContractsAndGrantsAgingReport> results = new ArrayList<ContractsAndGrantsAgingReport>();
    setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
    setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));

    total0to30 = KualiDecimal.ZERO;
    total31to60 = KualiDecimal.ZERO;
    total61to90 = KualiDecimal.ZERO;
    total91toSYSPR = KualiDecimal.ZERO;
    totalSYSPRplus1orMore = KualiDecimal.ZERO;
    totalOpenInvoices = KualiDecimal.ZERO;
    totalWriteOffs = KualiDecimal.ZERO;
    totalCredits = KualiDecimal.ZERO;

    Collection<CustomerInvoiceDocument> invoices = new ArrayList<CustomerInvoiceDocument>();
    Map<String, ContractsAndGrantsAgingReport> knownCustomers =
        new HashMap<String, ContractsAndGrantsAgingReport>();
    ContractsAndGrantsAgingReport custDetail;

    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    dateFormat.setLenient(false);

    Date today = getDateTimeService().getCurrentDate();
    String reportRunDateStr =
        (String) fieldValues.get(ArPropertyConstants.CustomerAgingReportFields.REPORT_RUN_DATE);
    try {

      if (ObjectUtils.isNull(reportRunDateStr) || reportRunDateStr.isEmpty()) {
        reportRunDate = today;
      } else {
        reportRunDate = dateFormat.parse(reportRunDateStr);
      }
      // set dates for buckets
      Date cutoffdate30 = DateUtils.addDays(reportRunDate, -30);
      Date cutoffdate31 = DateUtils.addDays(reportRunDate, -31);
      Date cutoffdate60 = DateUtils.addDays(reportRunDate, -60);
      Date cutoffdate61 = DateUtils.addDays(reportRunDate, -61);
      Date cutoffdate90 = DateUtils.addDays(reportRunDate, -90);
      Date cutoffdate91 = DateUtils.addDays(reportRunDate, -91);
      Date cutoffdate120 =
          DateUtils.addDays(reportRunDate, -1 * Integer.parseInt(nbrDaysForLastBucket));
      Date cutoffdate121 = DateUtils.addDays(cutoffdate120, -1);

      // retrieve filtered data according to the lookup
      Map<String, List<ContractsGrantsInvoiceDocument>> cgMapByCustomer =
          contractsGrantsAgingReportService.filterContractsGrantsAgingReport(
              fieldValues, null, new java.sql.Date(reportRunDate.getTime()));
      if (ObjectUtils.isNotNull(cgMapByCustomer) && !cgMapByCustomer.isEmpty()) {
        // 30 days
        computeFor0To30DaysByBillingChartAndOrg(
            cgMapByCustomer,
            new java.sql.Date(cutoffdate30.getTime()),
            new java.sql.Date(reportRunDate.getTime()),
            knownCustomers);
        // 60 days
        computeFor31To60DaysByBillingChartAndOrg(
            cgMapByCustomer,
            new java.sql.Date(cutoffdate60.getTime()),
            new java.sql.Date(cutoffdate31.getTime()),
            knownCustomers);
        // 90 days
        computeFor61To90DaysByBillingChartAndOrg(
            cgMapByCustomer,
            new java.sql.Date(cutoffdate90.getTime()),
            new java.sql.Date(cutoffdate61.getTime()),
            knownCustomers);
        // 120 days
        computeFor91ToSYSPRDaysByBillingChartAndOrg(
            cgMapByCustomer,
            new java.sql.Date(cutoffdate120.getTime()),
            new java.sql.Date(cutoffdate91.getTime()),
            knownCustomers);
        // 120 + older
        computeForSYSPRplus1orMoreDaysByBillingChartAndOrg(
            cgMapByCustomer, null, new java.sql.Date(cutoffdate121.getTime()), knownCustomers);
        // credits
        calculateTotalCreditsForCustomers(cgMapByCustomer, knownCustomers);
      }

      CustomerAgingReportDao agingReportDao = SpringContext.getBean(CustomerAgingReportDao.class);
      // prepare customer map.

      for (ContractsAndGrantsAgingReport detail : knownCustomers.values()) {

        // get agency name for customer
        ContractsAndGrantsBillingAgency agencyObj = getAgencyByCustomer(detail.getCustomerNumber());
        if (ObjectUtils.isNotNull(agencyObj)) {
          detail.setReportingName(agencyObj.getReportingName());
          detail.setAgencyNumber(agencyObj.getAgencyNumber());
        }

        // set total open invoices
        KualiDecimal amount =
            detail
                .getUnpaidBalance0to30()
                .add(detail.getUnpaidBalance31to60())
                .add(detail.getUnpaidBalance61to90())
                .add(
                    detail
                        .getUnpaidBalance91toSYSPR()
                        .add(detail.getUnpaidBalanceSYSPRplus1orMore()));
        detail.setTotalOpenInvoices(amount);
        totalOpenInvoices = totalOpenInvoices.add(amount);

        // find total writeoff
        KualiDecimal writeOffAmt =
            agingReportDao.findWriteOffAmountByCustomerNumber(detail.getCustomerNumber());
        if (ObjectUtils.isNotNull(writeOffAmt)) {
          totalWriteOffs = totalWriteOffs.add(writeOffAmt);
        } else {
          writeOffAmt = KualiDecimal.ZERO;
        }
        detail.setTotalWriteOff(writeOffAmt);

        // calculate total credits
        results.add(detail);
      }
    } catch (Exception ex) {
      LOG.error(
          "problem during ContractsGrantsAgingReportLookupableHelperServiceImpl.getSearchResults()",
          ex);
    }
    return new CollectionIncomplete<ContractsAndGrantsAgingReport>(results, (long) results.size());
  }