/**
   * @see
   *     org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService#setupDefaultValuesForCopiedCustomerInvoiceDocument(org.kuali.kfs.module.ar.document.CustomerInvoiceDocument)
   */
  @Override
  public void setupDefaultValuesForCopiedCustomerInvoiceDocument(CustomerInvoiceDocument document) {

    setupBasicDefaultValuesForCustomerInvoiceDocument(document);

    // Save customer number since it will get overwritten when we retrieve the accounts receivable
    // document header from service
    String customerNumber = document.getAccountsReceivableDocumentHeader().getCustomerNumber();

    // Set up the default values for the AR DOC Header
    AccountsReceivableDocumentHeader accountsReceivableDocumentHeader =
        accountsReceivableDocumentHeaderService
            .getNewAccountsReceivableDocumentHeaderForCurrentUser();
    accountsReceivableDocumentHeader.setDocumentNumber(document.getDocumentNumber());
    accountsReceivableDocumentHeader.setCustomerNumber(customerNumber);
    document.setAccountsReceivableDocumentHeader(accountsReceivableDocumentHeader);

    // set up the primary key for AR_INV_RCURRNC_DTL_T
    CustomerInvoiceRecurrenceDetails recurrenceDetails = new CustomerInvoiceRecurrenceDetails();
    recurrenceDetails.setInvoiceNumber(document.getDocumentNumber());
    // recurrenceDetails.setCustomerNumber(document.getAccountsReceivableDocumentHeader().getCustomerNumber());
    document.setCustomerInvoiceRecurrenceDetails(recurrenceDetails);

    // make open invoice indicator to true
    document.setOpenInvoiceIndicator(true);
    document.setPrintDate(null);
    document.setBillingDate(dateTimeService.getCurrentSqlDateMidnight());
  }
  /**
   * @see
   *     org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService#getInvoicesByCustomerNumber(java.lang.String)
   */
  @Override
  public Collection<CustomerInvoiceDocument> getCustomerInvoiceDocumentsByCustomerNumber(
      String customerNumber) {

    Collection<CustomerInvoiceDocument> invoices = new ArrayList<CustomerInvoiceDocument>();

    Map<String, String> fieldValues = new HashMap<String, String>();
    fieldValues.put("customerNumber", customerNumber);

    Collection<AccountsReceivableDocumentHeader> documentHeaders =
        businessObjectService.findMatching(AccountsReceivableDocumentHeader.class, fieldValues);

    List<String> documentHeaderIds = new ArrayList<String>();
    for (AccountsReceivableDocumentHeader header : documentHeaders) {
      documentHeaderIds.add(header.getDocumentHeader().getDocumentNumber());
    }

    if (0 < documentHeaderIds.size()) {
      try {
        for (Document doc :
            documentService.getDocumentsByListOfDocumentHeaderIds(
                CustomerInvoiceDocument.class, documentHeaderIds)) {
          invoices.add((CustomerInvoiceDocument) doc);
        }
      } catch (WorkflowException e) {
        LOG.error("getCustomerInvoiceDocumentsByCustomerNumber " + customerNumber + " failed", e);
      }
    }
    return invoices;
  }
 /**
  * Adding the role qualifications for the processing chart and organization
  *
  * @see
  *     org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase#addRoleQualification(org.kuali.rice.kns.bo.BusinessObject,
  *     java.util.Map)
  */
 @Override
 protected void addRoleQualification(Object businessObject, Map<String, String> attributes) {
   super.addRoleQualification(businessObject, attributes);
   if (businessObject != null && businessObject instanceof PaymentApplicationDocument) {
     final PaymentApplicationDocument document = (PaymentApplicationDocument) businessObject;
     final WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
     if (workflowDocument.isInitiated()
         || workflowDocument.isSaved()
         || workflowDocument
             .isCompletionRequested()) { // only add processing chart and org if we're PreRoute
       final AccountsReceivableDocumentHeader arDocumentHeader =
           document.getAccountsReceivableDocumentHeader();
       if (!ObjectUtils.isNull(arDocumentHeader)) {
         if (!StringUtils.isBlank(arDocumentHeader.getProcessingChartOfAccCodeAndOrgCode())) {
           attributes.put(
               KfsKimAttributes.CHART_OF_ACCOUNTS_CODE,
               arDocumentHeader.getProcessingChartOfAccountCode());
         }
         if (!StringUtils.isBlank(arDocumentHeader.getProcessingOrganizationCode())) {
           attributes.put(
               KfsKimAttributes.ORGANIZATION_CODE,
               arDocumentHeader.getProcessingOrganizationCode());
         }
       }
     }
   }
 }
  /**
   * Refactor to have all the setters in here.
   *
   * @see
   *     org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService#setupDefaultValuesForNewCustomerInvoiceDocument(org.kuali.kfs.module.ar.document.CustomerInvoiceDocument)
   */
  @Override
  public void setupDefaultValuesForNewCustomerInvoiceDocument(CustomerInvoiceDocument document) {

    setupBasicDefaultValuesForCustomerInvoiceDocument(document);

    // set up the default values for the AR DOC Header

    AccountsReceivableDocumentHeader accountsReceivableDocumentHeader =
        accountsReceivableDocumentHeaderService
            .getNewAccountsReceivableDocumentHeaderForCurrentUser();
    accountsReceivableDocumentHeader.setDocumentNumber(document.getDocumentNumber());
    document.setAccountsReceivableDocumentHeader(accountsReceivableDocumentHeader);

    // set up the primary key for AR_INV_RCURRNC_DTL_T
    CustomerInvoiceRecurrenceDetails recurrenceDetails = new CustomerInvoiceRecurrenceDetails();
    recurrenceDetails.setInvoiceNumber(document.getDocumentNumber());
    // recurrenceDetails.setCustomerNumber(document.getCustomer().getCustomerNumber());
    document.setCustomerInvoiceRecurrenceDetails(recurrenceDetails);

    Map<String, String> criteria = new HashMap<String, String>();
    criteria.put(
        KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, document.getBillByChartOfAccountCode());
    criteria.put(KFSPropertyConstants.ORGANIZATION_CODE, document.getBilledByOrganizationCode());
    OrganizationOptions organizationOptions =
        businessObjectService.findByPrimaryKey(OrganizationOptions.class, criteria);

    if (ObjectUtils.isNotNull(organizationOptions)) {
      document.setPrintInvoiceIndicator(organizationOptions.getPrintInvoiceIndicator());
      document.setInvoiceTermsText(organizationOptions.getOrganizationPaymentTermsText());
    }

    // If document is using receivable option, set receivable accounting line for customer invoice
    // document
    String receivableOffsetOption =
        parameterService.getParameterValueAsString(
            CustomerInvoiceDocument.class, ArConstants.GLPE_RECEIVABLE_OFFSET_GENERATION_METHOD);
    boolean isUsingReceivableFAU =
        ArConstants.GLPE_RECEIVABLE_OFFSET_GENERATION_METHOD_FAU.equals(receivableOffsetOption);
    if (isUsingReceivableFAU) {
      receivableAccountingLineService.setReceivableAccountingLineForCustomerInvoiceDocument(
          document);
    }
  }
  /**
   * This method creates a payment application document based on the passed in fixture array
   *
   * @param paymentFixture
   * @param customerInvoiceDetailFixtures
   * @return
   */
  public PaymentApplicationDocument createPaymentApplicationDocument() {

    PaymentApplicationDocument paymentApplicationDocument = null;
    try {
      paymentApplicationDocument =
          (PaymentApplicationDocument)
              DocumentTestUtils.createDocument(
                  SpringContext.getBean(DocumentService.class), PaymentApplicationDocument.class);
    } catch (WorkflowException e) {
      throw new RuntimeException("Document creation failed.");
    }

    // set AR doc Header
    AccountsReceivableDocumentHeader arDocHeader = new AccountsReceivableDocumentHeader();
    AccountsReceivableDocumentHeaderService accountsReceivableDocumentHeaderService =
        SpringContext.getBean(AccountsReceivableDocumentHeaderService.class);
    arDocHeader.setDocumentNumber(paymentApplicationDocument.getDocumentNumber());
    arDocHeader.setCustomerNumber(customerNumber);
    arDocHeader.setDocumentNumber(paymentApplicationDocument.getDocumentNumber());
    paymentApplicationDocument.setAccountsReceivableDocumentHeader(arDocHeader);

    return paymentApplicationDocument;
  }