c# - 如何为 QBO IPP .NET SDK V3 的发票行项目指定产品/服务?

标签 c# intuit-partner-platform quickbooks-online

我尝试为导入到 QuickBooks Online (QBO) 公司文件的发票的发票行项目指定产品/服务列表项目,但收到错误消息。

我收到的错误是:

Intuit.Ipp.Exception.IdsException: InternalServerError ---> Intuit.Ipp.Exception.EndpointNotFoundException: Ids service endpoint was not found.

该异常不会进一步详细说明我所做的事情是否有效。

我的单元测试方法:

    [TestMethod()]
    public void CreateTest()
    {
        Entities.Invoice invoice = new Entities.Invoice();
        invoice.ReferenceId = Guid.NewGuid().ToString("N").Substring(0, 10);
        invoice.CreatedDate = DateTime.Now;
        invoice.CustomerId = 1;
        invoice.LineItems.Add(new InvoiceLine() { ItemName = "Initial Funding", Description = "Initial Funding", Amount = 5500 });
        invoice.LineItems.Add(new InvoiceLine() { ItemName = "Lien Fee", Description = "Lien Fee", Amount = 100 });


        IPPRestProfile restProfile = new IPPRestProfile(realmId, accessToken, accessTokenSecret, Intuit.Ipp.Core.IntuitServicesType.QBO, consumerKey, consumerSecret);
        IPP.Invoices target = new IPP.Invoices(restProfile);
        Intuit.Ipp.Data.Invoice actual = target.Create(invoice);

        if (actual != null)
        {
            Console.WriteLine("QB Invoice ID: {0}", actual.Id);
            Console.WriteLine("QB Sync Token: {0}", actual.SyncToken);
            Console.WriteLine("================================================");
            ObjectDumper.Write(actual, 4);
        }
    }

单元测试调用的方法:

        public Intuit.Ipp.Data.Invoice Create(Entities.Invoice invoice)
    {
        // Check pre-conditions
        if (invoice == null) { throw new ArgumentException("Invoice object is required.", "invoice"); }

        var qboInvoice = new Intuit.Ipp.Data.Invoice();
        BuildInvoiceEntity(qboInvoice, invoice);

        return _Service.Add(qboInvoice) as Intuit.Ipp.Data.Invoice;
    }

最后是构建发票方法:

    private void BuildInvoiceEntity(Intuit.Ipp.Data.Invoice qboInvoice, Entities.Invoice invoice)
    {
        if (qboInvoice != null && invoice != null)
        {
            IQuickBooksHeader header = invoice as IQuickBooksHeader;

            if (String.IsNullOrEmpty(header.Id))
            {
                qboInvoice.DocNumber = invoice.ReferenceId;
                qboInvoice.TxnDate = invoice.CreatedDate;
                qboInvoice.TxnDateSpecified = true;

                // Customer
                qboInvoice.CustomerRef = new ReferenceType()
                {
                    type = objectNameEnumType.Customer.ToString(),
                    Value = invoice.CustomerId.ToString()
                };

                // AR Account
                qboInvoice.ARAccountRef = new ReferenceType()
                {
                    type = objectNameEnumType.Account.ToString(),
                    name = "Accounts Receivable"
                };
            }

            if (invoice.LineItems.Count > 0)
            {
                Intuit.Ipp.Data.Line[] invoiceLineCollection = new Intuit.Ipp.Data.Line[invoice.LineItems.Count];
                for (int i = 0; i < invoice.LineItems.Count; i++)
                {
                    var line = invoice.LineItems[i];
                    var qboInvoiceLine = new Intuit.Ipp.Data.Line()
                    {
                        Amount = line.Amount,
                        AmountSpecified = true,
                        Description = line.Description,
                        DetailType = LineDetailTypeEnum.SalesItemLineDetail,
                        DetailTypeSpecified = true,
                        AnyIntuitObject = new SalesItemLineDetail()
                        {
                            ItemRef = new ReferenceType()
                            {
                                name = line.ItemName,
                            },
                            ItemElementName = ItemChoiceType.UnitPrice,
                            AnyIntuitObject = line.Amount
                        }
                    };
                    invoiceLineCollection[i] = qboInvoiceLine;
                }
                qboInvoice.Line = invoiceLineCollection;
            }
        }
    }

如果我从构建方法中删除这段代码:

      ItemRef = new ReferenceType()
      {
          name = line.ItemName,
      },

发票已成功添加,发票行项目的产品/服务具有默认的“服务”列表项。

IPP .NET SDK V3 的在线文档对于为 ReferenceType 指定的内容含糊其辞。只指定列表项的名称有什么问题?如果我错误地尝试为发票行项目指定产品/服务列表项目,那么正确的方法是什么?

最佳答案

经过几天的研究,我从未找到为什么我不能像我想要的那样使用名称的答案,尽管在指定 AccountRef 时它是这样工作的。但我离题了,这是我的解决方案:

// Hold a collection of QBO items
private ReadOnlyCollection<Item> _Items;

// I set the collection in the constructor only once
public Invoices(Entities.IPPRestProfile restProfile)
{
    if (restProfile == null)
        throw new ArgumentException("IPPRestProfile object is required.", "restProfile");

    OAuthRequestValidator oAuthValidator = new OAuthRequestValidator(restProfile.OAuthAccessToken, restProfile.OAuthAccessTokenSecret,
            restProfile.ConsumerKey, restProfile.ConsumerSecret);
    ServiceContext context = new ServiceContext(restProfile.RealmId, restProfile.DataSource, oAuthValidator);
    _Service = new DataService(context);
    _Items = (new QueryService<Item>(context)).ExecuteIdsQuery("SELECT * FROM Item", QueryOperationType.query);
}

每当我生成发票时,我都会按名称查询集合中的项目 ID:

private void BuildInvoiceEntity(Intuit.Ipp.Data.Invoice qboInvoice, Entities.Invoice invoice)
{
    ...
    // Get the Id value of the item by name
    string itemTypeId = _Items.Where(o => o.Name == line.ItemName).FirstOrDefault().Id;

    // Specify the Id value in the item reference of the SalesItemLineDetail
    var qboInvoiceLine = new Intuit.Ipp.Data.Line()
    {
        Amount = (decimal)amount,
        AmountSpecified = true,
        Description = line.Description,
        DetailType = LineDetailTypeEnum.SalesItemLineDetail,
        DetailTypeSpecified = true,
        AnyIntuitObject = new SalesItemLineDetail()
        {
            ItemRef = new ReferenceType() { Value = itemTypeId },
            AnyIntuitObject = (decimal)line.Rate,
            ItemElementName = ItemChoiceType.UnitPrice
        }
    };
    ...
}

希望这有助于为某人指明正确的方向,找到可能更好的解决方案。

关于c# - 如何为 QBO IPP .NET SDK V3 的发票行项目指定产品/服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19694093/

相关文章:

java - 如何进行身份验证以访问我们自己的数据?

c# - ToolTip 的 BackColor 在不使用任何 BackColor 属性的情况下因两个不同的样本而异

google-app-engine - 来自 Intuit IPP 的请求 token 请求被拒绝

php - 如何使用 Keith Palmer 的 QBO 框架设置自定义字段的值?

QuickBooks API - 检索所有帐户的交易

c# - 有没有使用 OAuth2 访问 Quickbooks API 的简单方法?

c# - 如何从 header 和详细信息类创建 Json

C#Openfiledialog

c# - 如何在javascript中定义公共(public)和私有(private)属性

intuit-partner-platform - 您可以通过 OAuth 检索 Intuit 用户 ID