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

时间:2017-07-26 09:22:48

标签: c# asp.net quickbooks

如果可以为该客户提供信用额度,那么当我尝试将Quickbooks的“应用信用”功能实施到现有发票时,我就会陷入困境。我可以通过Quickbooks Desktop UI来实现它,但是没有通过用C#编写的.Net集成应用程序来实现它。可以请任何人指导。

我用来通过C#代码向Quickbooks实施“申请信用”的代码。 我收到错误:“QuickBooks在解析提供的XML文本流时发现错误。”使用以下代码。

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
}

提前致谢!!

1 个答案:

答案 0 :(得分:0)

我通过使用以下功能获得解决方案: -

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);
      }
    }
  }
}