Quickbooks在线会计 - 如何在发票中添加多个行项目?

时间:2017-07-18 18:24:18

标签: c# quickbooks-online

使用以下代码,我想创建多个订单项以显示在新发票中。

如何在我的发票中创建多个订单项,与我的自定义应用程序中的多个订单商品相对应?

使用OrderItems按顺序运行以下代码,只显示发票中的一个项目。价格和名称是正确的,但产品数量不是。只有一个出现。

我相信这一行是添加订单项的正确行。但它实际上是否添加了订单项?

  

invoice.Line = new Line [] {invoiceLine};

代码

var orderItems = order.OrderItems;

foreach (var orderItem in orderItems)
{
    //Line
    Line invoiceLine = new Line();

    //Line Description
    invoiceLine.Description = itemRepository.Get(i=>i.ItemID == orderItem.ItemID).First().FullDescription;

    //Line Amount
    invoiceLine.Amount = orderItem.Price * orderItem.Quantity;
    invoiceLine.AmountSpecified = true;

    //Line Detail Type
    invoiceLine.DetailType = LineDetailTypeEnum.SalesItemLineDetail;
    invoiceLine.DetailTypeSpecified = true;

    //Line Sales Item Line Detail
    SalesItemLineDetail lineSalesItemLineDetail = new SalesItemLineDetail();

    //Line Sales Item Line Detail - ItemRef
    lineSalesItemLineDetail.ItemRef = new ReferenceType()
    {
        name = itemRepository.Get(
            i=>i.ItemID == orderItem.ItemID).First().FullDescription,
            Value = item.Id
    };

    //Line Sales Item Line Detail - UnitPrice
    lineSalesItemLineDetail.AnyIntuitObject = orderItem.Price;//33m;
    lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice;

    //Line Sales Item Line Detail - Qty
    lineSalesItemLineDetail.Qty = orderItem.Quantity;//10;
    lineSalesItemLineDetail.QtySpecified = true;

    //Line Sales Item Line Detail - TaxCodeRef
    //For US companies, this can be 'TAX' or 'NON
    lineSalesItemLineDetail.TaxCodeRef = new ReferenceType()
    {
        Value = "TAX"
    };

    //Line Sales Item Line Detail - ServiceDate 
    lineSalesItemLineDetail.ServiceDate = DateTime.Now.Date;
    lineSalesItemLineDetail.ServiceDateSpecified = true;

    //Assign Sales Item Line Detail to Line Item
    invoiceLine.AnyIntuitObject = lineSalesItemLineDetail;

    //Assign Line Item to Invoice
    invoice.Line = new Line[] { invoiceLine };

}

编辑:

OAuthRequestValidator reqValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerKeySecret);
            ServiceContext qboContextoAuth = new ServiceContext(realmId, IntuitServicesType.QBO, reqValidator);
            var service = new DataService(qboContextoAuth);

            //Find Customer
            var customerQueryService = new QueryService<Customer>(qboContextoAuth);
            Customer customer = customerQueryService.ExecuteIdsQuery("Select * From Customer Where DisplayName = 'John Doe' StartPosition 1 MaxResults 1").FirstOrDefault();

            //Find Tax Code for Invoice - Searching for a tax code named 'StateSalesTax' in this example
            var stateTaxCodeQueryService = new QueryService<TaxCode>(qboContextoAuth);
            TaxCode stateTaxCode = stateTaxCodeQueryService.ExecuteIdsQuery("Select * From TaxCode Where Name='Los Angeles' StartPosition 1 MaxResults 1").FirstOrDefault();

            //Find Item
            var itemQueryService = new QueryService<Item>(qboContextoAuth);
            Item item = itemQueryService.ExecuteIdsQuery("Select * From Item StartPosition 1").FirstOrDefault();


            //Find Account - Accounts Receivable account required
            var accountQueryService = new QueryService<Account>(qboContextoAuth);
            Account account = accountQueryService.ExecuteIdsQuery("Select * From Account Where AccountType='Accounts Receivable' StartPosition 1 MaxResults 1").FirstOrDefault();

            //Find Term
            var termQueryService = new QueryService<Term>(qboContextoAuth);
            Term term = termQueryService.ExecuteIdsQuery("Select * From Term StartPosition 1 MaxResults 1").FirstOrDefault();

            Invoice invoice = new Invoice();

            //DocNumber - QBO Only, otherwise use DocNumber
            invoice.AutoDocNumber = true;
            invoice.AutoDocNumberSpecified = true;

            //TxnDate
            invoice.TxnDate = DateTime.Now.Date;
            invoice.TxnDateSpecified = true;

            //PrivateNote
            invoice.PrivateNote = "This is a private note";


            var orderItems = order.OrderItems;

            int idx = 0;

            var lines = new List<Line>();

            foreach (var orderItem in orderItems)
            {
                //Line
                Line invoiceLine = new Line();
                //Line Description
                invoiceLine.Description = itemRepository.Get(i => i.ItemID == orderItem.ItemID).First().FullDescription;
                //Line Amount
                invoiceLine.Amount = orderItem.Price * orderItem.Quantity;
                invoiceLine.AmountSpecified = true;
                //Line Detail Type
                invoiceLine.DetailType = LineDetailTypeEnum.SalesItemLineDetail;
                invoiceLine.DetailTypeSpecified = true;
                //Line Sales Item Line Detail


                SalesItemLineDetail lineSalesItemLineDetail = new SalesItemLineDetail();


                //Line Sales Item Line Detail - ItemRef
                lineSalesItemLineDetail.ItemRef = new ReferenceType()
                {
                    name = itemRepository.Get(i => i.ItemID == orderItem.ItemID).First().FullDescription,
                    Value = (idx + 1).ToString()
                };
                //Line Sales Item Line Detail - UnitPrice
                lineSalesItemLineDetail.AnyIntuitObject = orderItem.Price;//33m;
                lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice;
                //Line Sales Item Line Detail - Qty
                lineSalesItemLineDetail.Qty = orderItem.Quantity;//10;
                lineSalesItemLineDetail.QtySpecified = true;
                //Line Sales Item Line Detail - TaxCodeRef
                //For US companies, this can be 'TAX' or 'NON

                lineSalesItemLineDetail.TaxCodeRef = new ReferenceType()
                {
                    Value = "TAX"
                };
                //Line Sales Item Line Detail - ServiceDate 
                lineSalesItemLineDetail.ServiceDate = DateTime.Now.Date;
                lineSalesItemLineDetail.ServiceDateSpecified = true;
                //Assign Sales Item Line Detail to Line Item
                invoiceLine.AnyIntuitObject = lineSalesItemLineDetail;
                //Assign Line Item to Invoice
                //invoice.Line = new Line[] { invoiceLine };
                lines.Add(invoiceLine);

                //TxnTaxDetail
                TxnTaxDetail txnTaxDetail = new TxnTaxDetail();
                txnTaxDetail.TxnTaxCodeRef = new ReferenceType()
                {
                    name = stateTaxCode.Name,
                    Value = stateTaxCode.Id
                };
                Line taxLine = new Line();
                taxLine.DetailType = LineDetailTypeEnum.TaxLineDetail;
                TaxLineDetail taxLineDetail = new TaxLineDetail();
                //Assigning the fist Tax Rate in this Tax Code
                taxLineDetail.TaxRateRef = stateTaxCode.SalesTaxRateList.TaxRateDetail[0].TaxRateRef;
                taxLine.AnyIntuitObject = taxLineDetail;
                txnTaxDetail.TaxLine = new Line[] { taxLine };
                invoice.TxnTaxDetail = txnTaxDetail;

                idx++;
            }
            //Customer (Client)
            invoice.CustomerRef = new ReferenceType()
            {
                name = customer.DisplayName,
                Value = customer.Id
            };
            var _user = userRepository.Get(u => u.UserID == order.CreatedUserID, includeProperties: "ContactMethod, ContactMethod.Addresses, Location, Location.Organization").FirstOrDefault();

            if (_user != null)
            {
                var addresses = _user.ContactMethod.Addresses.Where(a => a.StatusCode == Convert.ToByte(StatusCodes.Active) && a.IsPrimary).ToList();


                var _billingAddress = addresses.Where(a => a.AddressTypeId == Convert.ToByte(AddressTypes.Billing)).FirstOrDefault();
                var _shippingAddress = addresses.Where(a => a.AddressTypeId == Convert.ToByte(AddressTypes.Shipping)).FirstOrDefault();


                //Billing Address
                PhysicalAddress billAddr = new PhysicalAddress();
                billAddr.Line1 = _billingAddress.Address1;
                billAddr.Line2 = _billingAddress.Address2;
                billAddr.City = _billingAddress.City;
                billAddr.CountrySubDivisionCode = _billingAddress.State;
                billAddr.Country = _billingAddress.Country;
                billAddr.PostalCode = _billingAddress.ZipCode;
                billAddr.Note = "Billing Address Note";
                invoice.BillAddr = billAddr;

                //Shipping Address
                PhysicalAddress shipAddr = new PhysicalAddress();
                shipAddr.Line1 = _shippingAddress.Address1;
                shipAddr.City = _shippingAddress.City;
                shipAddr.CountrySubDivisionCode = _shippingAddress.City;
                shipAddr.Country = _shippingAddress.Country;
                shipAddr.PostalCode = _shippingAddress.ZipCode;
                shipAddr.Note = "Shipping Address Note";
                invoice.ShipAddr = shipAddr;

            }

            invoice.Line = lines.ToArray();

            //SalesTermRef
            invoice.SalesTermRef = new ReferenceType()
            {
                name = term.Name,
                Value = term.Id
            };

            //DueDate
            invoice.DueDate = DateTime.Now.AddDays(30).Date;
            invoice.DueDateSpecified = true;

            //ARAccountRef
            invoice.ARAccountRef = new ReferenceType()
            {
                name = account.Name,
                Value = account.Id
            };

            Invoice invoiceAdded = service.Add(invoice);

            return invoiceAdded;

1 个答案:

答案 0 :(得分:2)

目前每次循环执行此行

invoice.Line = new Line[] { invoiceLine };

它通过分配新数组来覆盖先前的值。根据您当前的代码,一旦有订单项,数组中总会有一个项目。

将行存储在列表中,然后在处理完所有订单项后转换为数组。

var orderItems = order.OrderItems;
var lines = new List<Line>();
foreach (var orderItem in orderItems) {
    //Line
    Line invoiceLine = new Line();

    //...code removed for brevity

    lines.Add(invoiceLine);
}    
//Assign Line Items to Invoice
invoice.Line = lines.ToArray();

或者另一种选择是先创建数组,然后在创建订单项时填充它。

var orderItems = order.OrderItems;
var lineCount = orderItems.Count();
//Creating Line array before
invoice.Line = new Line[lineCount];   
for (int index = 0; index < lineCount; index++) {
    //Order item
    var orderItem = orderItems[index];
    //Line
    Line invoiceLine = new Line();

    //...code removed for brevity

    //Assign Line Item to Invoice
    invoice.Line[index] = invoiceLine;
}   

更新:

获取项目参考并设置必要的值

var itemRef = itemRepository.Get(i => i.ItemID == orderItem.ItemID).FirstOrDefault();
if (itemRef != null) {
    //Line Sales Item Line Detail - ItemRef
    lineSalesItemLineDetail.ItemRef = new ReferenceType() {
        Name = itemRef.FullDescription,
        Value = itemRef.ItemID 
    };
}