如何使用 QBFC13 和 C# 获取一个月的所有发票并将它们全部保存为 PDF?

How can I get all invoices for one month and save them all as PDFs using QBFC13 and C#?

我正在制作一个获取一个月所有发票的应用程序,然后将每张发票保存为 PDF,我从未使用 QuickBooks 开发过,所以我对此知之甚少。有人能给我任何源代码来做这个或类似的事情吗?我发现了几个链接,我认为这些链接为我指明了正确的方向,但我不是 100% 确定:

How can I get the invoices that has been paid on an specific date from QuickBooks to .NET app?

https://intuitdeveloper.lc.intuit.com/questions/1382278-sample-net-code-for-quickbooks-api-oauth-call-to-create-an-invoice-and-pdf-or-any-api-call-if-no-invoice

https://intuitdeveloper.lc.intuit.com/questions/1060982-quickbooks-online-api-invoice-pdf-download

我正在使用 C# .NET 和 QBFC13Lib。

编辑:

我已使用此代码从链接问题中获取所有发票。

bool sessionBegun = false;
        bool connectionOpen = false;
        QBSessionManager sessionManager = null;

        try
        {
            //Create the session Manager object
            sessionManager = new QBSessionManager();

            //Create the message set request object to hold our request
            IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
            requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

            //Connect to QuickBooks and begin a session
            sessionManager.OpenConnection("", "GenerateInvoicePDFs");
            connectionOpen = true;
            sessionManager.BeginSession("", ENOpenMode.omDontCare);
            sessionBegun = true;

            IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq();

            invoiceQueryRq.IncludeLineItems.SetValue(true);

            //Send the request and get the response from QuickBooks
            IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);
            IResponse response = responseMsgSet.ResponseList.GetAt(0);
            IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail;

            var invoices = new List<Invoice>();

            if (invoiceRetList != null)
            {
                for (int i = 0; i < invoiceRetList.Count; i++)
                {
                    IInvoiceRet invoiceRet = invoiceRetList.GetAt(i);

                    var invoice = new Invoice
                    {
                        QuickBooksID = invoiceRet.TxnID.GetValue(),
                        EditSequence = invoiceRet.EditSequence.GetValue()
                    };
                }
            }
        }
        catch
        {

        }

我收到一条错误消息,指出发票不是一种类型。

SDK 在查询对象上提供过滤器,使您能够查询数据的子集。您可以根据交易日期筛选发票:

// get all invoices for the month of august 2016
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.FromTxnDate.SetValue(new DateTime(2016, 8, 1));
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.ToTxnDate.SetValue(new DateTime(2016, 8, 31));

或者,您可以查询在给定日期范围内修改过的发票:

// all invoices modified in the month of August 2016
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(new DateTime(2016, 8, 1));
invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(new DateTime(2016, 8, 31));

在您使用的代码中,当您调用 BeginSession 时,第一个参数是空白。虽然这是通过获取当前打开的公司文件来实现的,但建议明确提供要查询的公司文件的文件路径。

请务必在完成查询后进行清理:

if (requestMsgSet != null)
{
    Marshal.FinalReleaseComObject(requestMsgSet);
}
if (invoiceQueryRq != null)
{
    Marshal.FinalReleaseComObject(invoiceQueryRq);
}
sessionMgr.EndSession();
sessionMgr.CloseConnection();

无论如何,我建议您通读 SDK 开发人员指南以了解您的代码实际在做什么。

更新:

您问题的第二部分已得到解答 here