Not able to set SalesItemLineDetail tax code in Intuit quickbooks api C#

烂漫一生 提交于 2020-03-04 23:07:49

问题


I am trying to add Tax Code for SalesItemLineDetail inside Invoice of Quickbooks online api, but it is not setting tax code correctly when checking it in Online Quickbooks.

Here is my C# Code, which I am using to create Line Item

                            Line = new Intuit.Ipp.Data.Line();
                            InvoiceLine = new Intuit.Ipp.Data.SalesItemLineDetail();


                            InvoiceLine.ItemRef = new Intuit.Ipp.Data.ReferenceType
                            {
                                Value = GetItem.Id, // this is inventory Item Id
                                name = GetItem.Name // inventory item name
                            };





                            Line.DetailTypeSpecified = true;
                            Line.DetailType = Intuit.Ipp.Data.LineDetailTypeEnum.SalesItemLineDetail;
                            Line.Description = inv.Description;

                            Line.Amount = (inv.Price == null || inv.Price == 0.0) ? (decimal)0.00 : (decimal)inv.Price;
                            Line.AmountSpecified = true;



                            InvoiceLine.Qty = decimal.Parse(inv.Quantity.Value.ToString());
                            InvoiceLine.QtySpecified = true;

                            InvoiceLine.AnyIntuitObject = (inv.Price == null || inv.Price == 0.0) ? (decimal)0.00 : (decimal)(Math.Round(inv.Price.Value, 2) / inv.Quantity.Value);
                            InvoiceLine.ItemElementName = Intuit.Ipp.Data.ItemChoiceType.UnitPrice;




                            // this line is not settings tax code properly
                            InvoiceLine.TaxCodeRef = new Intuit.Ipp.Data.ReferenceType
                            {
                                name = taxName,
                                Value = TaxId

                            };


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

                            //Assign Sales Item Line Detail to Line Item

                            Line.AnyIntuitObject = InvoiceLine;


                            lines.Add(Line);

                    Intuit.Ipp.Data.Invoice invoice = new Intuit.Ipp.Data.Invoice();

                   // SalesOrder is a database table object, and OrderNumber is auto generated number

                    invoice.DocNumber = SalesOrder.OrderNumber.ToString();

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


                    invoice.CustomerRef = new Intuit.Ipp.Data.ReferenceType
                    {
                        Value =  CompanyId
                    };

                    //convert list to array for Intuit Line
                    invoice.Line = lines.ToArray();

                    //TxnTaxDetail
                    Intuit.Ipp.Data.Line taxLine = new Intuit.Ipp.Data.Line();
                    Intuit.Ipp.Data.TxnTaxDetail txnTaxDetail = new Intuit.Ipp.Data.TxnTaxDetail();
                    Intuit.Ipp.Data.TaxLineDetail taxLineDetail = new Intuit.Ipp.Data.TaxLineDetail(); ;
                    //txnTaxDetail.TotalTaxSpecified = true;
                    //txnTaxDetail.TotalTax = decimal.Parse("2");
                    var MainTaxValue = "";
                    txnTaxDetail.TxnTaxCodeRef = new Intuit.Ipp.Data.ReferenceType()
                    {

                        Value = TaxId,
                        name = SalesOrder.TaxCode.TaxCodeName
                    };
                    foreach (var TAXName in TaxObject.TaxRateDetail)
                    {
                        if(TAXName.TaxRateRef.name.Contains(SalesOrder.TaxCode.TaxCodeName))
                        {
                            MainTaxValue = TAXName.TaxRateRef.value;
                        }
                    }
                    taxLineDetail.TaxRateRef = new Intuit.Ipp.Data.ReferenceType
                    {
                        Value = MainTaxValue

                    };
                    taxLine.AnyIntuitObject = taxLineDetail;
                    txnTaxDetail.TaxLine = new Intuit.Ipp.Data.Line[] { taxLine };


                    //DueDate
                    invoice.DueDate = SalesOrder.InvoiceDueDate != null ? SalesOrder.InvoiceDueDate.Value : DateTime.Now.AddDays(30).Date;
                    invoice.DueDateSpecified = true;

                    invoice.TxnTaxDetail = txnTaxDetail;

I have tried these reference links, but it is not working for me

https://gist.github.com/IntuitDeveloperRelations/6500373

How to export Line items with Tax Code and Value in QBO Canada

https://developer.intuit.com/app/developer/qbo/docs/develop/tutorials/manage-sales-tax-for-non-us-locales

Using above links, I can see we can create Tax Code ref using this line of code, for Each Invoice Line item, but it is not setting value correctly.

                           InvoiceLine.TaxCodeRef = new Intuit.Ipp.Data.ReferenceType
                            {
                                name = taxName,
                                Value = TaxId

                            };

But it is not working. Note: this is non-US company, so I have to specify tax code ref for each Invoice Line.

Edit 1: Attaching Image of Postman API request, which I sent to Quickbooks for creating invoice.


回答1:


try by removing the name field I think it might not be required

InvoiceLine.TaxCodeRef = new Intuit.Ipp.Data.ReferenceType()
                        {

                            Value = TaxId

                        };



回答2:


The main difference between our versions is that I let QB calculate the tax. I commented out the Tax detail line, and told QB that tax amount wasn't included.

            //    //TxnTaxDetail
            //    TxnTaxDetail txnTaxDetail = new TxnTaxDetail();
            //    Line taxLine = new Line();
            //    taxLine.DetailType = LineDetailTypeEnum.TaxLineDetail;
            //    TaxLineDetail taxLineDetail = new TaxLineDetail();
            //    taxLineDetail.TaxRateRef = stateTaxCode.SalesTaxRateList.TaxRateDetail[0].TaxRateRef;
            //    txnTaxDetail.TxnTaxCodeRef = new ReferenceType
            //    {
            //        name = stateTaxCode.Name,
            //        Value = stateTaxCode.Id
            //    };
            //    if (customer.DefaultTaxCodeRef != null)
            //    {
            //        txnTaxDetail.TxnTaxCodeRef = customer.DefaultTaxCodeRef;
            //        taxLineDetail.TaxRateRef = customer.DefaultTaxCodeRef;
            //    }

            //    //Assigning the first Tax Rate in this Tax Code

            //    taxLine.AnyIntuitObject = taxLineDetail;
            //    txnTaxDetail.TaxLine = new[] { taxLine };
            //    invoice.TxnTaxDetail = txnTaxDetail;


            invoice.GlobalTaxCalculationSpecified = true;
            invoice.GlobalTaxCalculation = GlobalTaxCalculationEnum.TaxExcluded;

Here is my code for doing this, and it definitely works. I can't see a difference between the two though.This calculates VAT in Europe and puts in the Tax Code. Hope this helps.

            var invlines = new List<Line>();
            foreach (var lineitem in inv.Lines)
            {
                //Line
                Line invoiceLine = new Line();
                //Line Description
                invoiceLine.Description = (((lineitem.PublicationName == "N/A" || lineitem.PublicationName == "-") ? "" : lineitem.PublicationName) + " " + lineitem.Description).Trim();

                //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
                if (!string.IsNullOrEmpty(lineitem.ItemCode))
                {
                    lineSalesItemLineDetail.ItemRef = new ReferenceType()
                    {
                        Value = lineitem.ItemCode
                    };
                }
                else if (item != null)
                {
                    lineSalesItemLineDetail.ItemRef = new ReferenceType
                    {
                        name = item.Name,
                        Value = item.Id
                    };
                }

                //Line Sales Item Line Detail - UnitPrice

                //Line Sales Item Line Detail - Qty
                lineSalesItemLineDetail.Qty = 1;
                lineSalesItemLineDetail.QtySpecified = true;
                if (inv.DiscountPercent > 0)
                {
                    invoiceLine.Amount = (decimal)lineitem.PriceBeforeDiscount;
                    invoiceLine.AmountSpecified = true;
                    lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice;
                }
                else
                {
                    invoiceLine.Amount = (decimal)lineitem.Price;
                    invoiceLine.AmountSpecified = true;
                    lineSalesItemLineDetail.AnyIntuitObject = lineitem.Price;
                    lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice;
                }
                //Line Sales Item Line Detail - TaxCodeRef
                //For US companies, this can be 'TAX' or 'NON'
                var taxref = lineitem.TaxAmount == null || lineitem.TaxAmount == 0 ? nonvatid.ToString() : vatid.ToString();
                if (country == "US")
                {
                    taxref = lineitem.TaxAmount == null || lineitem.TaxAmount == 0 ? "NON" : "TAX";
                }
                lineSalesItemLineDetail.TaxCodeRef = new ReferenceType
                {
                    Value = taxref
                };

                //Line Sales Item Line Detail - ServiceDate 
                lineSalesItemLineDetail.ServiceDate = DateTimeService.Now.Date;
                lineSalesItemLineDetail.ServiceDateSpecified = true;
                //Assign Sales Item Line Detail to Line Item
                invoiceLine.AnyIntuitObject = lineSalesItemLineDetail;
                //Assign Line Item to Invoice
                invlines.Add(invoiceLine);
            }
            if (inv.DiscountPercent > 0)
            {
                Line invoiceLine = new Line();
                DiscountLineDetail discLine = new DiscountLineDetail();
                discLine.PercentBased = true;
                discLine.DiscountPercent = (decimal)inv.DiscountPercent;
                discLine.DiscountPercentSpecified = true;
                discLine.PercentBased = true;
                discLine.PercentBasedSpecified = true;
                invoiceLine.DetailType = LineDetailTypeEnum.DiscountLineDetail;
                invoiceLine.DetailTypeSpecified = true;
                invoiceLine.AnyIntuitObject = discLine;
                invlines.Add(invoiceLine);
                invoice.DiscountRate = (decimal) (inv.DiscountPercent);
                invoice.DiscountRateSpecified = true;
            }
            invoice.Line = invlines.ToArray();



回答3:


Finally was able to find the correct solution.

My Above code is right, there is no issue in it and @sheavens code is also right.

Actual problem was, I was assigning "default Tax code" to a selected company, which we cannot override while passing tax code reference in Invoice Line item.

To Check if there is any default code for company, navigate to Companies list in quickbooks online website , Select your desired Company from the list, click "Edit", then in the "Tax Info" tab, uncheck "Assign Default tax code" to pass tax code using Invoice Line item.

Hope this helps other developers, with same problem.



来源:https://stackoverflow.com/questions/60278540/not-able-to-set-salesitemlinedetail-tax-code-in-intuit-quickbooks-api-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!