Last active
September 13, 2021 21:44
-
-
Save IntuitDeveloperRelations/6500373 to your computer and use it in GitHub Desktop.
IPP .NET SDK v3 - QBO - Create Invoice with Sales Line Item Details #IppDotNetSdkV3 #Invoice #QBO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void CreateQBOInvoice(DataService dataService, ServiceContext context) | |
{ | |
//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); | |
#region " Logged Request and Response " | |
//Request | |
//POST https://<baseUrl>/v3/company/<realmId>/invoice HTTP/1.1 | |
//Content-Type: application/xml | |
//Host: <baseUrl> | |
//Authorization: OAuth oauth_token="lvprdTJzbZqooMvYCvU35HwpwYsL1ZaJsvJwRYAIvZcL906W",oauth_nonce="b93219b6-430d-46c8-a217-a0918ed5aff8",oauth_consumer_key="qyprdcAQ7lV9AbDAdMA5WEkHEkvcj4",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1378833234",oauth_version="1.0",oauth_signature="5kWNe6uXOEGHT%2BIAbJHHwxSaV%2BQ%3D" | |
//User-Agent: V3DotNetSDK1.0.7 | |
//Content-Length: 1706 | |
//Expect: 100-continue | |
//<?xml version="1.0"?> | |
//<Invoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schema.intuit.com/finance/v3"> | |
// <TxnDate>2013-09-10-04:00</TxnDate> | |
// <PrivateNote>This is a private note</PrivateNote> | |
// <Line> | |
// <Description>Invoice line description.</Description> | |
// <Amount>330</Amount> | |
// <DetailType>SalesItemLineDetail</DetailType> | |
// <SalesItemLineDetail> | |
// <ItemRef name="Sales">1</ItemRef> | |
// <UnitPrice>33</UnitPrice> | |
// <Qty>10</Qty> | |
// <TaxCodeRef>TAX</TaxCodeRef> | |
// <ServiceDate>2013-09-10-04:00</ServiceDate> | |
// </SalesItemLineDetail> | |
// </Line> | |
// <TxnTaxDetail> | |
// <TxnTaxCodeRef name="StateSalesTax">2</TxnTaxCodeRef> | |
// <TaxLine> | |
// <TaxLineDetail> | |
// <TaxRateRef name="StateSalesTax">1</TaxRateRef> | |
// </TaxLineDetail> | |
// </TaxLine> | |
// </TxnTaxDetail> | |
// <AutoDocNumber>true</AutoDocNumber> | |
// <CustomerRef name="Test Customer">1</CustomerRef> | |
// <BillAddr> | |
// <Line1>123 Main St.</Line1> | |
// <Line2>Unit 506</Line2> | |
// <City>Brockton</City> | |
// <Country>United States</Country> | |
// <CountrySubDivisionCode>MA</CountrySubDivisionCode> | |
// <PostalCode>02301</PostalCode> | |
// <Note>Billing Address Note</Note> | |
// </BillAddr> | |
// <ShipAddr> | |
// <Line1>100 Fifth Ave.</Line1> | |
// <City>Waltham</City> | |
// <Country>United States</Country> | |
// <CountrySubDivisionCode>MA</CountrySubDivisionCode> | |
// <PostalCode>02452</PostalCode> | |
// <Note>Shipping Address Note</Note> | |
// </ShipAddr> | |
// <SalesTermRef name="Due on receipt">1</SalesTermRef> | |
// <DueDate>2013-10-10-04:00</DueDate> | |
// <ARAccountRef name="Accounts Receivable (A/R)">48</ARAccountRef> | |
//</Invoice> | |
#endregion | |
#region " Screenshot of result in QBO UI " | |
//http://i.imgur.com/LlrP5ca.png | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment