@Override
  public void recalculateCustomerCreditMemoDocument(
      CustomerCreditMemoDocument customerCreditMemoDocument,
      boolean blanketApproveDocumentEventFlag) {
    KualiDecimal customerCreditMemoDetailItemAmount;
    BigDecimal itemQuantity;

    String invDocumentNumber =
        customerCreditMemoDocument.getFinancialDocumentReferenceInvoiceNumber();
    List<CustomerCreditMemoDetail> customerCreditMemoDetails =
        customerCreditMemoDocument.getCreditMemoDetails();

    if (!blanketApproveDocumentEventFlag) {
      customerCreditMemoDocument.resetTotals();
    }

    for (CustomerCreditMemoDetail customerCreditMemoDetail : customerCreditMemoDetails) {
      // no data entered for the current credit memo detail -> no processing needed
      itemQuantity = customerCreditMemoDetail.getCreditMemoItemQuantity();
      customerCreditMemoDetailItemAmount = customerCreditMemoDetail.getCreditMemoItemTotalAmount();
      if (ObjectUtils.isNull(itemQuantity)
          && ObjectUtils.isNull(customerCreditMemoDetailItemAmount)) {
        if (!blanketApproveDocumentEventFlag) {
          customerCreditMemoDetail.setDuplicateCreditMemoItemTotalAmount(null);
        }
        continue;
      }

      // if item amount was entered, it takes precedence, if not, use the item quantity to re-calc
      // amount
      if (ObjectUtils.isNotNull(customerCreditMemoDetailItemAmount)) {
        customerCreditMemoDetail.recalculateBasedOnEnteredItemAmount(customerCreditMemoDocument);
      } // if item quantity was entered
      else {
        customerCreditMemoDetail.recalculateBasedOnEnteredItemQty(customerCreditMemoDocument);
        if (!blanketApproveDocumentEventFlag) {
          customerCreditMemoDetailItemAmount =
              customerCreditMemoDetail.getCreditMemoItemTotalAmount();
        }
      }

      if (!blanketApproveDocumentEventFlag) {
        customerCreditMemoDetail.setDuplicateCreditMemoItemTotalAmount(
            customerCreditMemoDetailItemAmount);
        boolean isCustomerInvoiceDetailTaxable =
            accountsReceivableTaxService.isCustomerInvoiceDetailTaxable(
                customerCreditMemoDocument.getInvoice(),
                customerCreditMemoDetail.getCustomerInvoiceDetail());
        customerCreditMemoDocument.recalculateTotals(
            customerCreditMemoDetailItemAmount, isCustomerInvoiceDetailTaxable);
      }
    }

    //  force the docHeader docTotal
    customerCreditMemoDocument
        .getFinancialSystemDocumentHeader()
        .setFinancialDocumentTotalAmount(customerCreditMemoDocument.getCrmTotalAmount());
  }
 /**
  * This method calculates the total credits for the customers.
  *
  * @param cgMapByCustomer
  * @param knownCustomers
  */
 private void calculateTotalCreditsForCustomers(
     Map<String, List<ContractsGrantsInvoiceDocument>> cgMapByCustomer,
     Map<String, ContractsAndGrantsAgingReport> knownCustomers) {
   Set<String> customerIds = cgMapByCustomer.keySet();
   KualiDecimal credits = KualiDecimal.ZERO;
   for (String customer : customerIds) {
     ContractsAndGrantsAgingReport agingReportDetail =
         pickContractsGrantsAgingReportDetail(knownCustomers, customer);
     List<ContractsGrantsInvoiceDocument> cgDocs = cgMapByCustomer.get(customer);
     if (ObjectUtils.isNotNull(cgDocs) && !cgDocs.isEmpty()) {
       credits = KualiDecimal.ZERO;
       for (ContractsGrantsInvoiceDocument cgDoc : cgDocs) {
         Collection<CustomerCreditMemoDocument> creditDocs =
             customerCreditMemoDocumentService.getCustomerCreditMemoDocumentByInvoiceDocument(
                 cgDoc.getDocumentNumber());
         if (ObjectUtils.isNotNull(creditDocs) && !creditDocs.isEmpty()) {
           for (CustomerCreditMemoDocument cm : creditDocs) {
             for (CustomerCreditMemoDetail cmDetail : cm.getCreditMemoDetails()) {
               credits = credits.add(cmDetail.getCreditMemoItemTotalAmount());
             }
           }
         }
       }
     }
     agingReportDetail.setTotalCredits(credits);
     totalCredits = totalCredits.add(credits);
   }
 }
  @Override
  public boolean isThereNoDataToSubmit(CustomerCreditMemoDocument customerCreditMemoDocument) {
    boolean isSuccess = true;
    KualiDecimal customerCreditMemoDetailItemAmount;
    BigDecimal itemQuantity;
    List<CustomerCreditMemoDetail> customerCreditMemoDetails =
        customerCreditMemoDocument.getCreditMemoDetails();

    for (CustomerCreditMemoDetail customerCreditMemoDetail : customerCreditMemoDetails) {
      // no data entered for the current credit memo detail -> no processing needed
      itemQuantity = customerCreditMemoDetail.getCreditMemoItemQuantity();
      customerCreditMemoDetailItemAmount = customerCreditMemoDetail.getCreditMemoItemTotalAmount();
      if (ObjectUtils.isNotNull(itemQuantity)
          || ObjectUtils.isNotNull(customerCreditMemoDetailItemAmount)) {
        isSuccess = false;
        break;
      }
    }
    return isSuccess;
  }