Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

c# - IPP .NET SDK for QuickBooks v3.0 Create Invoice Error - Bad Request

I am having a hard time figuring out what exactly is going wrong here - I do not get alot of insight form the error Bad Request - here is my code:

OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
                ServiceContext context = new ServiceContext(appToken, companyID, IntuitServicesType.QBO, oauthValidator);

                DataService service = new DataService(context);
                Customer customer = new Customer();
                customer.GivenName = "Mary " + DateTime.Now.Second;
                customer.Title = "Ms.";
                customer.MiddleName = "Jayne";
                customer.FamilyName = "Cooper";
                customer.CompanyName = "Mary " + DateTime.Now.Second;
                Customer resultCustomer = service.Add(customer) as Customer;

                Invoice invoice = new Invoice();
                //Mandatory fields
                invoice.DocNumber = Guid.NewGuid().ToString("N").Substring(0, 10);
                invoice.TxnDate = DateTime.Today.Date;
                invoice.TxnDateSpecified = true;
                invoice.CustomerRef = new ReferenceType()
                {
                     Value = resultCustomer.Id
                };

                Line invLine = new Line();

                invLine.Amount = 10000;
                invLine.DetailType = LineDetailTypeEnum.SalesItemLineDetail;
                invLine.Description = "Test Product";

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

               Invoice resutlInvoice = service.Add(invoice) as Invoice;

                var invId = resutlInvoice.Id;

Basically I am generating a new customer (which work fine) and then I am trying to create an invoice for them with a single item on it.

Looking at what XML the documentation states here: http://ippdocs.intuit.com/0025_QuickBooksAPI/0050_Data_Services/V3/030_Entity_Services_Reference/Invoice

The NuGet package is missing a few things, which I know cant be true - form the documentation:

<Invoice xmlns="http://schema.intuit.com/finance/v3">
  <Line>
    <Amount>15</Amount>
    <DetailType>SalesItemLineDetail</DetailType>
    <SalesItemLineDetail>
      <ItemRef>1</ItemRef>
    </SalesItemLineDetail>
  </Line>
  <CustomerRef>67</CustomerRef>
</Invoice>

The Line object I get from this SDK has no properties for SalesItemLineDetail or ItemRef on it.

Anyone have a working example of this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
//Find Customer
QueryService<Customer> customerQueryService = new QueryService<Customer>(context);
Customer customer = customerQueryService.ExecuteIdsQuery("Select * From Customer StartPosition 1 MaxResults 1").FirstOrDefault<Customer>();

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

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

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

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


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";

//Line
Line invoiceLine = new Line();
//Line Description
invoiceLine.Description = "Invoice line description.";
//Line Amount
invoiceLine.Amount = 330m;
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 = item.Name,
    Value = item.Id
};
//Line Sales Item Line Detail - UnitPrice
lineSalesItemLineDetail.AnyIntuitObject = 33m;
lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice;
//Line Sales Item Line Detail - Qty
lineSalesItemLineDetail.Qty = 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 };

//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;

//Customer (Client)
invoice.CustomerRef = new ReferenceType()
{
    name = customer.DisplayName,
    Value = customer.Id
};

//Billing Address
PhysicalAddress billAddr = new PhysicalAddress();
billAddr.Line1 = "123 Main St.";
billAddr.Line2 = "Unit 506";
billAddr.City = "Brockton";
billAddr.CountrySubDivisionCode = "MA";
billAddr.Country = "United States";
billAddr.PostalCode = "02301";
billAddr.Note = "Billing Address Note";
invoice.BillAddr = billAddr;

//Shipping Address
PhysicalAddress shipAddr = new PhysicalAddress();
shipAddr.Line1 = "100 Fifth Ave.";
shipAddr.City = "Waltham";
shipAddr.CountrySubDivisionCode = "MA";
shipAddr.Country = "United States";
shipAddr.PostalCode = "02452";
shipAddr.Note = "Shipping Address Note";
invoice.ShipAddr = shipAddr;

//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 = dataService.Add<Invoice>(invoice);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...