如果使用 Interop 通过 .Net C# 代码为该客户提供信用,如何对现有发票实施 "apply credit"。QBFC13Lib.dll?

How to implement "apply credit" to existing invoices if credits are available for that customer via .Net C# code by using Interop.QBFC13Lib.dll?

我在尝试对现有发票实施 Quickbooks 的 "apply credit" 功能时遇到了困难(如果该客户有信用可用的话)。我可以通过 Quickbooks Desktop UI 来完成它,但无法通过用 C# 编写的 .Net 集成应用程序来实现它。任何人都可以指导一下吗?

我用来通过 C# 代码实现 "apply credit" 到 Quickbooks 的代码。 我收到错误:"QuickBooks found an error when parsing the provided XML text stream.",代码如下。

public void ApplyCreditsOnPrePayInvoices()
    {
      // open connection and begin session before data fetch - intentionally skipped this code
      IMsgSetRequest msgset = null;
      ICreditMemoQuery creditMemoQuery = null;
      string sCustomerName = string.Empty;
      if (this.GetConnectedToQB()) //this is the method call to login to quickbooks desktop
      {
        try
        {
          // during data fetch
          msgset = m_sessionManager.CreateMsgSetRequest("US", 8, 0);
          creditMemoQuery = msgset.AppendCreditMemoQueryRq();
          creditMemoQuery.ORTxnQuery.TxnFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(new DateTime(2012, 3, 31), false);
      IMsgSetResponse msgRes = m_sessionManager.DoRequests(msgset);
      IResponseList responseList = msgRes.ResponseList;
      if (responseList.Count > 0)
      {
        IResponse response = responseList.GetAt(0);
        ICreditMemoRetList creditMemoList = response.Detail as ICreditMemoRetList;
        if (creditMemoList == null)
        {
          return;
        }
        for (int i = 0; i <= creditMemoList.Count - 1; i++)
        {
          ICreditMemoRet qbCreditMemo = creditMemoList.GetAt(i);
          if (this.GetQBCustomerListId(qbCreditMemo.CustomerRef.FullName.GetValue()) != string.Empty)
          {
            m_requestMsgSet.ClearRequests();
            m_requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
            IInvoiceAdd invoiceAddRq = m_requestMsgSet.AppendInvoiceAddRq();
            invoiceAddRq.CustomerRef.FullName.SetValue(qbCreditMemo.CustomerRef.FullName.GetValue());
            ISetCredit SetCredit1 = invoiceAddRq.SetCreditList.Append();
            SetCredit1.CreditTxnID.SetValue(qbCreditMemo.TxnID.GetValue());
            SetCredit1.AppliedAmount.SetValue(qbCreditMemo.TotalAmount.GetValue());
            IMsgSetResponse responseSetInvoice = m_sessionManager.DoRequests(m_requestMsgSet);
            DataSet dsInvoice = this.GetExtractResponseFromQB(responseSetInvoice);
            string sQueryResponse = Stringcl.GetValue(dsInvoice.Tables["InvoiceAddRs"].Rows[0]["statusMessage"]);
            if (sQueryResponse == "Status OK")
            {
              Console.WriteLine("Credit no.:" + qbCreditMemo.TxnID.GetValue() + " Customer:" + qbCreditMemo.CustomerRef.FullName.GetValue() + " Total:" + qbCreditMemo.TotalAmount.GetValue());
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      string ss = ex.Message;
      //handle exception here
    }
    finally
    {
      if (msgset != null)
      {
        Marshal.FinalReleaseComObject(msgset);
      }
      if (creditMemoQuery != null)
      {
        Marshal.FinalReleaseComObject(creditMemoQuery);
      }
    }
  }

  // end session and close connection after data fetch - intentionally skipped this code
}

提前致谢!!

我使用以下函数得到了解决方案:-

public void ApplyCreditsOnPrePayInvoices(string sMonth, string sYear)
{
  IMsgSetRequest IMsgSetRequestToQB = null;
  ICreditMemoQuery ICreditMemoQueryToQB = null;
  string sInvoiceTxnId = string.Empty;
  string sInvoiceNumber = string.Empty;
  if (this.GetConnectedToQB())
  {
    try
    {
      IMsgSetRequestToQB = m_sessionManager.CreateMsgSetRequest("US", 8, 0);
      ICreditMemoQueryToQB = IMsgSetRequestToQB.AppendCreditMemoQueryRq();
      ICreditMemoQueryToQB.ORTxnQuery.TxnFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(DateTimecl.GetValue("1." + sMonth + sYear), true);
      IMsgSetResponse IMsgSetResponseFromQB = m_sessionManager.DoRequests(IMsgSetRequestToQB);
      IResponseList ICreditListMemoAvailable = IMsgSetResponseFromQB.ResponseList;
      if (ICreditListMemoAvailable.Count > 0)
      {
        string sCustomerListIdQB = string.Empty;
        string sCustomerAccountNumber = string.Empty;
        string sQBImportPrepayAccounts = Path.Combine(Environment.CurrentDirectory, sMonth + sYear, "Step11_QBImport_PrepayAccounts.csv");
        DataTable dtQBImportPrepayAccounts = Utilcl.GetDataTableFromCSVFile(sQBImportPrepayAccounts);
        IResponse ICreditMemoAvailable = ICreditListMemoAvailable.GetAt(0);
        ICreditMemoRetList iCreditMemoList = ICreditMemoAvailable.Detail as ICreditMemoRetList;
        if (iCreditMemoList != null)
        {
          for (int iCtr = 0; iCtr <= iCreditMemoList.Count - 1; iCtr++)
          {
            ICreditMemoRet ICreditMemo = iCreditMemoList.GetAt(iCtr);
            DataRow[] drInvoiceNos = dtQBImportPrepayAccounts.Select("RefNumber = '" + ICreditMemo.RefNumber.GetValue() + "'");
            if (drInvoiceNos.Length > 0)
            {
              sInvoiceNumber = Stringcl.GetValue(drInvoiceNos[0]["RefNumber"]);
              m_requestMsgSet.ClearRequests();
              m_requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
              IReceivePaymentAdd IReceivePayment = m_requestMsgSet.AppendReceivePaymentAddRq();
              sInvoiceTxnId = this.GetQBInvoiceTxnIDList(ICreditMemo.CustomerRef.FullName.GetValue()); //To get the Transaction ID of Invoice
              IReceivePayment.CustomerRef.FullName.SetValue(Stringcl.GetValue(ICreditMemo.CustomerRef.FullName.GetValue()));
              IAppliedToTxnAdd IAppliedToTxnAddress = IReceivePayment.ORApplyPayment.AppliedToTxnAddList.Append();
              IAppliedToTxnAddress.TxnID.SetValue(sInvoiceTxnId);
              ISetCredit ISetCreditToInvoice = IAppliedToTxnAddress.SetCreditList.Append();
              ISetCreditToInvoice.CreditTxnID.SetValue(ICreditMemo.TxnID.GetValue());
              ISetCreditToInvoice.AppliedAmount.SetValue(ICreditMemo.TotalAmount.GetValue());
              IMsgSetResponse responseApplyCredit = m_sessionManager.DoRequests(m_requestMsgSet);
              DataSet dsInvoice = this.GetExtractResponseFromQB(responseApplyCredit);
              string sQueryResponse = Stringcl.GetValue(dsInvoice.Tables["ReceivePaymentAddRs"].Rows[0]["statusMessage"]);
              if (sQueryResponse != "Status OK")
              {
                Utilcl.LogMessage("QB Credit Memo Query Response: " + sQueryResponse);
              }
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      Utilcl.LogMessage(ex);
    }
    finally
    {
      if (IMsgSetRequestToQB != null)
      {
        Marshal.FinalReleaseComObject(IMsgSetRequestToQB);
      }
      if (ICreditMemoQueryToQB != null)
      {
        Marshal.FinalReleaseComObject(ICreditMemoQueryToQB);
      }
    }
  }
}