//This loads the invoice (or return) located at the specified path and returns a GetTaxRequest object for tax calculation. public static GetTaxRequest Load() { GetTaxRequest req = new GetTaxRequest(); //loads the invoice file string[] txtInv = File.ReadAllLines("INV0001.txt"); //Parses header-level data from the invoice file req.DocCode = txtInv[0].Split(':')[1] + DateTime.Now.ToString(); req.CustomerCode = txtInv[1].Split(':')[1]; req.DocDate = "2012-07-07";//txtInv[3].Split(':')[1]; req.DocType = DocType.SalesInvoice; string[] shipto = txtInv[10].Split(':')[1].Split(','); req.Addresses = new Address[2]; //We will need to pass in two addresses, our origin and destination. //Parse our destiniation address Address dest = new Address(); dest.AddressCode = "01"; dest.Line1 = shipto[0]; dest.City = shipto[1]; dest.Region = shipto[2]; dest.PostalCode = shipto[3]; //Add the address to our request object. req.Addresses[0] = new Address(); req.Addresses[0] = dest; //Hardcodes the origin address for the GetTaxRequest. This should be your warehouse or company address, and should not be hardcoded. req.Addresses[1] = new Address(); req.Addresses[1].AddressCode = "02"; req.Addresses[1].Line1 = "PO Box 123"; req.Addresses[1].City = "Bainbridge Island"; req.Addresses[1].Region = "WA"; req.Addresses[1].PostalCode = "98110"; //create array of line items req.Lines = new Line[txtInv.Length - 12]; //Iterate through line items on transaction and add them to the request for (int i = 1; txtInv.Length > 12 + i; i++) { string[] item = txtInv[12 + i].Split(','); req.Lines[i] = new Line(); req.Lines[i].LineNo = item[0]; req.Lines[i].ItemCode = item[1]; req.Lines[i].Qty = Convert.ToDecimal(item[3]); req.Lines[i].Amount = Convert.ToDecimal(item[4]) * req.Lines[i].Qty; req.Lines[i].OriginCode = "02"; req.Lines[i].DestinationCode = "01"; } //Pull the freight line from the header information and add to the request as an additional line item req.Lines[0] = new Line(); req.Lines[0].ItemCode = "Shipping"; req.Lines[0].Qty = 1; req.Lines[0].LineNo = "FR"; req.Lines[0].Amount = Convert.ToDecimal(txtInv[7].Split(':')[1]); req.Lines[0].OriginCode = "02"; req.Lines[0].DestinationCode = "01"; return req; }
static void Main() { GetTaxRequest calcReq = DocumentLoader.Load(); //Loads document from file to generate request //Run address validation test (address/validate) try { ValidateResult addressResult = ValidateAddress.Validate(calcReq.Addresses[0], ACCTNUM, KEY, WEBADDR); //Validates a given address. Console.Write("ValidateAddress test result: " + addressResult.ResultCode.ToString() + " >> "); if (addressResult.ResultCode.Equals(SeverityLevel.Success)) { Console.WriteLine("Address=" + addressResult.Address.Line1 + " " + addressResult.Address.City + " " + addressResult.Address.Region + " " + addressResult.Address.PostalCode);//At this point, you would display the validated result to the user for approval, and write it to the customer record. } else { Console.WriteLine(addressResult.Messages[0].Summary); } } catch (Exception ex) { Console.WriteLine("ValidateAddress Exception: " + ex.Message); } //Run tax calculation test (tax/get POST) try { GetTaxResult calcresult = GetTax.Get(calcReq, ACCTNUM, KEY, COMPANYCODE, WEBADDR); //Calculates tax on document Console.Write("GetTax test result: " + calcresult.ResultCode.ToString() + " >> "); if (calcresult.ResultCode.Equals(SeverityLevel.Success)) { Console.WriteLine("TotalTax=" + calcresult.TotalTax.ToString()); //At this point, you would write the tax calculated to your database and display to the user. } else { Console.WriteLine(calcresult.Messages[0].Summary); } } catch (Exception ex) { Console.WriteLine("GetTax Exception: " + ex.Message); } //Run cancel tax test (tax/cancel) try { CancelTaxResult cancelresult = CancelTax.Cancel(calcReq, ACCTNUM, KEY, COMPANYCODE, WEBADDR); //Let's void this document to demonstrate tax/cancel //You would normally initiate a tax/cancel call upon voiding or deleting the document in your system. Console.Write("CancelTax test result: " + cancelresult.ResultCode.ToString() + " >> "); //Let's display the result of the cancellation. At this point, you would allow your system to complete the delete/void. if (cancelresult.ResultCode.Equals(SeverityLevel.Success)) { Console.WriteLine("Document Cancelled"); } else { Console.WriteLine(cancelresult.Messages[0].Summary); } } catch (Exception ex) { Console.WriteLine("CancelTax Exception: " + ex.Message); } Console.WriteLine("Done"); Console.ReadLine(); }
//This calls CancelTax to void a transaction specified in taxreq public static CancelTaxResult Cancel(GetTaxRequest taxReq, string acctNum, string licKey, string companyCode, string webAddr) { CancelTaxRequest req = new CancelTaxRequest(); req.CompanyCode = taxReq.CompanyCode; req.DocCode = taxReq.DocCode; req.DocType = taxReq.DocType; req.CancelCode = CancelCode.DocVoided; //Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); //doc.Save(@"cancel_tax_request.xml"); //Call the service Uri address = new Uri(webAddr + "tax/cancel"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); CancelTaxResponse cancelResponse = new CancelTaxResponse(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(cancelResponse.GetType()); cancelResponse = (CancelTaxResponse)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(cancelResponse.GetType()); cancelResponse = (CancelTaxResponse)r.Deserialize(((HttpWebResponse)ex.Response).GetResponseStream()); if (cancelResponse.ResultCode.Equals(SeverityLevel.Error)) //If the error is returned at the cancelResponse level, translate it to the cancelResult. { cancelResponse.CancelTaxResult = new CancelTaxResult(); cancelResponse.CancelTaxResult.ResultCode = cancelResponse.ResultCode; cancelResponse.CancelTaxResult.Messages = cancelResponse.Messages; } } return(cancelResponse.CancelTaxResult); }
// This actually calls the service to perform the tax calculation, and returns the calculation result. public GetTaxResult GetTax(GetTaxRequest req) { var jsonRequest = JsonConvert.SerializeObject(req); // Call the service var address = new Uri(svcURL + "tax/get"); var request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license))); request.Method = "POST"; request.ContentType = "text/json"; request.ContentLength = jsonRequest.Length; var newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(jsonRequest), 0, jsonRequest.Length); var result = new GetTaxResult(); try { using (var response = (HttpWebResponse)request.GetResponse()) { // Get the stream containing content returned by the server. newStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. using (var reader = new StreamReader(newStream)) { result = JsonConvert.DeserializeObject <GetTaxResult>(reader.ReadToEnd()); } } } catch (WebException ex) { if (ex.Response == null) { result.ResultCode = SeverityLevel.Error; result.Messages = new[] { new Message { Severity = SeverityLevel.Error, Summary = ex.Message } }; return(result); } using (var response = ex.Response) { using (var data = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. using (var reader = new StreamReader(data)) { result = JsonConvert.DeserializeObject <GetTaxResult>(reader.ReadToEnd()); } } } } return(result); }
// This actually calls the service to perform the tax calculation, and returns the calculation result. public GetTaxResult GetTax(GetTaxRequest req) { var jsonRequest = JsonConvert.SerializeObject(req); // Call the service var address = new Uri(svcURL + "tax/get"); var request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license))); request.Method = "POST"; request.ContentType = "text/json"; request.ContentLength = jsonRequest.Length; var newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(jsonRequest), 0, jsonRequest.Length); var result = new GetTaxResult(); try { using (var response = (HttpWebResponse)request.GetResponse()) { // Get the stream containing content returned by the server. newStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. using (var reader = new StreamReader(newStream)) { result = JsonConvert.DeserializeObject<GetTaxResult>(reader.ReadToEnd()); } } } catch (WebException ex) { if (ex.Response == null) { result.ResultCode = SeverityLevel.Error; result.Messages = new[] { new Message { Severity = SeverityLevel.Error, Summary = ex.Message } }; return result; } using (var response = ex.Response) { using (var data = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. using (var reader = new StreamReader(data)) { result = JsonConvert.DeserializeObject<GetTaxResult>(reader.ReadToEnd()); } } } } return result; }
//This calls CancelTax to void a transaction specified in taxreq public static CancelTaxResult Cancel(GetTaxRequest taxReq, string acctNum, string licKey, string companyCode, string webAddr) { CancelTaxRequest req = new CancelTaxRequest(); req.CompanyCode = taxReq.CompanyCode; req.DocCode = taxReq.DocCode; req.DocType = taxReq.DocType; req.CancelCode = CancelCode.DocVoided; //Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); //doc.Save(@"cancel_tax_request.xml"); //Call the service Uri address = new Uri(webAddr + "tax/cancel"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); CancelTaxResponse cancelResponse = new CancelTaxResponse(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(cancelResponse.GetType()); cancelResponse = (CancelTaxResponse)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(cancelResponse.GetType()); cancelResponse = (CancelTaxResponse)r.Deserialize(((HttpWebResponse)ex.Response).GetResponseStream()); if(cancelResponse.ResultCode.Equals(SeverityLevel.Error)) //If the error is returned at the cancelResponse level, translate it to the cancelResult. { cancelResponse.CancelTaxResult = new CancelTaxResult(); cancelResponse.CancelTaxResult.ResultCode = cancelResponse.ResultCode; cancelResponse.CancelTaxResult.Messages = cancelResponse.Messages; } } return cancelResponse.CancelTaxResult; }
//This actually calls the service to perform the tax calculation, and returns the calculation result. public static GetTaxResult Get(GetTaxRequest req, string acctNum, string licKey, string companyCode, string webAddr) { //Company Code is ususally maintiained with the account credentials, so it's passed in to this function even though it's included in the body of the GetTaxRequest. req.CompanyCode = companyCode; //Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); //doc.Save(@"get_tax_request.xml"); //Call the service Uri address = new Uri(webAddr + "tax/get"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); GetTaxResult result = new GetTaxResult(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(((HttpWebResponse)ex.Response).GetResponseStream()); } return(result); }
// This actually calls the service to perform the tax calculation, and returns the calculation result. public GetTaxResult GetTax(GetTaxRequest req) { // Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add(string.Empty, string.Empty); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); // Call the service Uri address = new Uri(svcURL + "tax/get"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); GetTaxResult result = new GetTaxResult(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize((ex.Response).GetResponseStream()); } return(result); }
//This actually calls the service to perform the tax calculation, and returns the calculation result. public static GetTaxResult Get(GetTaxRequest req, string acctNum, string licKey, string companyCode, string webAddr) { //Company Code is ususally maintiained with the account credentials, so it's passed in to this function even though it's included in the body of the GetTaxRequest. req.CompanyCode = companyCode; //Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); //doc.Save(@"get_tax_request.xml"); //Call the service Uri address = new Uri(webAddr + "tax/get"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); GetTaxResult result = new GetTaxResult(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(((HttpWebResponse)ex.Response).GetResponseStream()); } return result; }
// This actually calls the service to perform the tax calculation, and returns the calculation result. public GetTaxResult GetTax(GetTaxRequest req) { // Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add(string.Empty, string.Empty); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); // Call the service Uri address = new Uri(svcURL + "tax/get"); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license))); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sb.Length; Stream newStream = request.GetRequestStream(); newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); GetTaxResult result = new GetTaxResult(); try { WebResponse response = request.GetResponse(); XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult)r.Deserialize((ex.Response).GetResponseStream()); } return result; }
//This loads some test data and returns a GetTaxRequest object for tax calculation. //TODO: These values should not be hardcoded in your integration. You will need to map to the values appropriate to your system. public static GetTaxRequest Load() { GetTaxRequest getTaxRequest = new GetTaxRequest(); //Document Level Setup // R: indicates Required Element // O: Indicates Optional Element // // Set the tax document properties - Required unless noted as Optional //getTaxRequest.CompanyCode = "SDK"; // R: Company Code from the Admin Console. This is passed in to the GetTax.Get function, since it is information typically maintained with the credentials. getTaxRequest.Client = "AvaTaxCalcRESTCsharp Sample"; DateTime docDate = DateTime.Today; getTaxRequest.DocCode = "SampleDoc: " + docDate.ToString(); // R: Invoice or document tracking number - Must be unique getTaxRequest.DocType = DocType.SalesInvoice; // R: Typically SalesOrder,SalesInvoice, ReturnInvoice getTaxRequest.DocDate = docDate.ToString("yyyy-MM-dd"); // R: sets reporting date and default tax date getTaxRequest.CustomerCode = "TaxSvcTest"; // R: String - Customer Tracking number or Exemption Customer Code getTaxRequest.DetailLevel = DetailLevel.Tax; // R: Chose Summary, Document, Line or Tax - varying levels of results detail getTaxRequest.Commit = false; // O: Default is "false" - Set to "true" to commit the Document //getTaxRequest.CustomerUsageType="G"; // O: Send for tax exempt transactions only. //getTaxRequest.ExemptionNo="12334"; // O: Send for tax exempt transactions only. //getTaxRequest.Discount=0; // O: Send for document-level discounts only. getTaxRequest.PurchaseOrderNo = "PO 23423"; // O: Specifies the purchase order number associated with the transaction. This value can be used to track single-use exemption certficates. getTaxRequest.ReferenceCode = ""; // O: This is a reportable value that does not affect tax calculation. getTaxRequest.PosLaneCode = ""; // O: This is a reportable value that does not affect tax calculation. //getTaxRequest.TaxOverride=new TaxOverrideDef(); // O: Allows the TaxDate =or other values) to be overridden for tax calculation. Situational only. //getTaxRequest.BusinessIdentificationNo=""; // O: Specified VAT ID of customer for international/VAT calculations and reporting. // Begin Address Section // Add the origin and destination addresses referred to by the // "setOriginCode" and "setDestinationCode" properties above. Address origin = new Address(); origin.AddressCode = "Origin"; origin.Line1 = "Avalara"; origin.Line2 = "100 Ravine Lane NE"; origin.Line3 = "Suite 220"; origin.City = "Bainbridge Island"; origin.Region = "WA"; origin.PostalCode = "98110"; origin.Country = "USA"; Address destination = new Address(); destination.AddressCode = "Dest"; destination.Line1 = "7462 Kearny Street"; destination.City = "Commerce City"; destination.Region = "CO"; destination.PostalCode = "80022"; destination.Country = "USA"; Address[] addresses = { origin, destination }; // // Alternate: Latitude / Longitude addressing // // // Address origin = new BaseAddress=; // origin.AddressCode="Origin"; // origin.Latitude="47.6253"; // origin.Longitude="-122.515114"; // // Address destination = new BaseAddress=; // destination.AddressCode="Destination"; // destination.Latitude="39.833597"; // destination.Longitude="-104.917220"; // Address[] addresses = {origin, destination}; // End Address Section getTaxRequest.Addresses = addresses; // Add invoice lines Line line1 = new Line(); // New instance of a line line1.LineNo = "101"; // R: string - line Number of invoice - must be unique. line1.ItemCode = "Item001"; // R: string - SKU or short name of Item line1.Qty = 1; // R: decimal - The number of items -- Qty of product sold. Does not function as a mulitplier for Amount line1.Amount = 1000; // R: decimal - the "NET" amount -- Amount should be the 'extended' or 'net' amount line1.CustomerUsageType = ""; // O: string - AKA Entity Use Code - Typically A - L =G = Reseller) line1.Description = "ITEM1"; // O: string - Description or category of item sold. line1.TaxCode = ""; // O: string - Pass standard, custom or Pro-Tax code // Can be NULL to default to tangible personal property =P0000000) line1.OriginCode = "Origin"; // R: Value representing the Origin Address line1.DestinationCode = "Dest"; // R: Value representing the Destination Address //Line 2 - Shipping/Freight line - See property descriptions above Line line2 = new Line(); // New instance of a line line2.LineNo = "102"; // R: string - SKU or short name of Item line2.ItemCode = "Shipping"; // R: string - SKU or short name of Item line2.Description = "Shipping- Freight Charges"; // O: string - Description or category of item sold. line2.Qty = 1; // R: decimal - The number of items -- Qty of product sold. Does not function as a mulitplier for Amount line2.Amount = 10; // R: decimal - the "NET" amount -- Amount should be the 'extended' or 'net' amount line2.TaxCode = "FR"; // O: string - Pass standard, custom or Pro-Tax code FR020100 line2.OriginCode = "Origin"; // R: Value representing the Origin Address line2.DestinationCode = "Dest"; // R: Value representing the Destination Address Line[] lines = { line1, line2 }; getTaxRequest.Lines = lines; // Sets array of lines return getTaxRequest; }
public static GetTaxRequest ToAvaTaxRequest(this TaxEvaluationContext evalContext, string companyCode, bool commit = false) { if (evalContext.Address != null && evalContext.Lines != null && evalContext.Lines.Any()) { // Document Level Elements // Required Request Parameters var getTaxRequest = new GetTaxRequest { CustomerCode = evalContext.Customer.Id, DocDate = DateTime.UtcNow.ToString("yyyy-MM-dd"), CompanyCode = companyCode, Client = "VirtoCommerce,2.x,VirtoCommerce", DetailLevel = DetailLevel.Tax, Commit = commit, DocType = DocType.SalesInvoice, DocCode = evalContext.Id, CurrencyCode = evalContext.Currency.ToString() }; // Best Practice Request Parameters // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters //getTaxRequest.PurchaseOrderNo = order.Number; //getTaxRequest.ReferenceCode = "ref123456"; //getTaxRequest.PosLaneCode = "09"; //add customer tax exemption code to cart if exists getTaxRequest.ExemptionNo = evalContext.Customer.GetDynamicPropertyValue("Tax exempt", string.Empty); string destinationAddressIndex = "0"; // Address Data var addresses = new List<Address>{ new Address { AddressCode = evalContext.Address.AddressType.ToString(), Line1 = evalContext.Address.Line1, City = evalContext.Address.City, Region = evalContext.Address.RegionName ?? evalContext.Address.RegionId, PostalCode = evalContext.Address.PostalCode, Country = evalContext.Address.CountryName } }; getTaxRequest.Addresses = addresses.ToArray(); // Line Data // Required Parameters getTaxRequest.Lines = evalContext.Lines.Select(li => new Line { LineNo = li.Id, ItemCode = li.Code, Qty = li.Amount, Amount = li.Price * li.Amount, OriginCode = destinationAddressIndex, //TODO set origin address (fulfillment?) DestinationCode = destinationAddressIndex, Description = li.Name, TaxCode = li.TaxType } ).ToList(); return getTaxRequest; } return null; }
public static GetTaxRequest ToAvaTaxRequest( this VirtoCommerce.Domain.Cart.Model.ShoppingCart cart, string companyCode, bool commit = false) { if (cart.Addresses != null && cart.Addresses.Any() && cart.Items != null && cart.Items.Any()) { var getTaxRequest = new GetTaxRequest { CustomerCode = cart.CustomerId, DocDate = cart.CreatedDate.ToShortDateString(), CompanyCode = companyCode, Client = "VirtoCommerce,2.x,VirtoCommerce", DocCode = cart.Id, DetailLevel = DetailLevel.Tax, Commit = false, DocType = DocType.SalesOrder }; // Document Level Elements // Required Request Parameters // Best Practice Request Parameters // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters //getTaxRequest.PurchaseOrderNo = order.Id; //getTaxRequest.ReferenceCode = "ref123456"; //getTaxRequest.PosLaneCode = "09"; //getTaxRequest.CurrencyCode = order.Currency.ToString(); // Address Data string destinationAddressIndex = "0"; // Address Data var addresses = new List<Address>(); foreach (var address in cart.Addresses.Select((x, i) => new { Value = x, Index = i })) { addresses.Add( new Address { AddressCode = address.Index.ToString(), Line1 = address.Value.Line1, City = address.Value.City, Region = address.Value.RegionName ?? address.Value.RegionId, PostalCode = address.Value.PostalCode, Country = address.Value.CountryName }); if (address.Value.AddressType == AddressType.Shipping || address.Value.AddressType == AddressType.Shipping) destinationAddressIndex = address.Index.ToString(CultureInfo.InvariantCulture); } getTaxRequest.Addresses = addresses; // Line Data // Required Parameters getTaxRequest.Lines = cart.Items.Select((x, i) => new { Value = x, Index = i }).Select(li => new Line { LineNo = li.Index.ToString(CultureInfo.InvariantCulture), ItemCode = li.Value.ProductId, Qty = li.Value.Quantity, Amount = li.Value.PlacedPrice, OriginCode = destinationAddressIndex, //TODO set origin address (fulfillment?) DestinationCode = destinationAddressIndex, Description = li.Value.Name, TaxCode = li.Value.Product.TaxType } ).ToArray(); return getTaxRequest; } return null; }
public static GetTaxRequest ToAvaTaxAdjustmentRequest(this VirtoCommerce.Domain.Order.Model.CustomerOrder modifiedOrder, string companyCode, Contact contact, VirtoCommerce.Domain.Order.Model.CustomerOrder originalOrder, bool commit = false) { if (modifiedOrder.Addresses != null && modifiedOrder.Addresses.Any() && originalOrder.Items != null && originalOrder.Items.Any()) { // Document Level Elements // Required Request Parameters var getTaxRequest = new GetTaxRequest { CustomerCode = modifiedOrder.CustomerId, DocDate = DateTime.UtcNow.ToString("yyyy-MM-dd"), CompanyCode = companyCode, Client = "VirtoCommerce,2.x,VirtoCommerce", DocCode = string.Format("{0}.{1}", originalOrder.Number, DateTime.UtcNow.ToString("yy-MM-dd-hh-mm")), DetailLevel = DetailLevel.Tax, Commit = commit, DocType = DocType.ReturnInvoice, TaxOverride = new TaxOverrideDef { TaxOverrideType = "TaxDate", Reason = "Adjustment for return", TaxDate = originalOrder.CreatedDate == DateTime.MinValue ? DateTime.UtcNow.ToString("yyyy-MM-dd") : originalOrder.CreatedDate.ToString("yyyy-MM-dd"), TaxAmount = "0" } }; // Best Practice Request Parameters // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; //for VAT tax calculations // getTaxRequest.Discount = 50; // Optional Request Parameters //getTaxRequest.PurchaseOrderNo = order.Number; getTaxRequest.ReferenceCode = originalOrder.Number; //getTaxRequest.PosLaneCode = "09"; getTaxRequest.CurrencyCode = modifiedOrder.Currency.ToString(); //add customer tax exemption code to cart if exists getTaxRequest.ExemptionNo = contact.GetDynamicPropertyValue("Tax exempt", string.Empty); string destinationAddressIndex = "0"; // Address Data var addresses = new List<Address>(); foreach (var address in modifiedOrder.Addresses.Select((x, i) => new { Value = x, Index = i })) { addresses.Add(new Address { AddressCode = address.Index.ToString(CultureInfo.InvariantCulture), Line1 = address.Value.Line1, City = address.Value.City, Region = address.Value.RegionName ?? address.Value.RegionId, PostalCode = address.Value.PostalCode, Country = address.Value.CountryName }); if (address.Value.AddressType == AddressType.Shipping || address.Value.AddressType == AddressType.Shipping) destinationAddressIndex = address.Index.ToString(CultureInfo.InvariantCulture); } getTaxRequest.Addresses = addresses.ToArray(); // Line Data // Required Parameters getTaxRequest.Lines = originalOrder.Items.Where(i => !modifiedOrder.Items.Any(mli => mli.Id.Equals(i.Id)) || i.Quantity > (modifiedOrder.Items.Single(oi => oi.Id.Equals(i.Id)).Quantity)).Select(li => new Line { LineNo = li.Id, ItemCode = li.ProductId, Qty = modifiedOrder.Items.Any(mli => mli.Id.Equals(li.Id)) ? Math.Abs(li.Quantity - modifiedOrder.Items.Single(oi => oi.Id.Equals(li.Id)).Quantity) : li.Quantity, Amount = modifiedOrder.Items.Any(mli => mli.Id.Equals(li.Id)) ? -(li.Price * li.Quantity - modifiedOrder.Items.Single(oi => oi.Id.Equals(li.Id)).Price * modifiedOrder.Items.Single(mli => mli.Id.Equals(li.Id)).Quantity) : -li.Price * li.Quantity, OriginCode = destinationAddressIndex, //TODO set origin address (fulfillment?) DestinationCode = destinationAddressIndex, Description = li.Name, TaxCode = li.TaxType } ).ToList(); return getTaxRequest; } return null; }
public static GetTaxRequest ToAvaTaxRequest(this VirtoCommerce.Domain.Order.Model.CustomerOrder order, string companyCode, Contact contact, bool commit = false) { if (order.Addresses != null && order.Addresses.Any() && order.Items != null && order.Items.Any()) { // Document Level Elements // Required Request Parameters var getTaxRequest = new GetTaxRequest { CustomerCode = order.CustomerId, DocDate = order.CreatedDate == DateTime.MinValue ? DateTime.UtcNow.ToString("yyyy-MM-dd") : order.CreatedDate.ToString("yyyy-MM-dd"), CompanyCode = companyCode, Client = "VirtoCommerce,2.x,VirtoCommerce", DetailLevel = DetailLevel.Tax, Commit = commit, DocType = DocType.SalesInvoice, DocCode = order.Number, CurrencyCode = order.Currency.ToString() }; // Best Practice Request Parameters // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters //getTaxRequest.PurchaseOrderNo = order.Number; //getTaxRequest.ReferenceCode = "ref123456"; //getTaxRequest.PosLaneCode = "09"; //add customer tax exemption code to cart if exists getTaxRequest.ExemptionNo = contact.GetDynamicPropertyValue("Tax exempt", string.Empty); string destinationAddressIndex = "0"; // Address Data var addresses = new List<Address>(); foreach (var address in order.Addresses.Select((x, i) => new { Value = x, Index = i })) { addresses.Add(new Address { AddressCode = address.Index.ToString(CultureInfo.InvariantCulture), Line1 = address.Value.Line1, City = address.Value.City, Region = address.Value.RegionName ?? address.Value.RegionId, PostalCode = address.Value.PostalCode, Country = address.Value.CountryName }); if (address.Value.AddressType == AddressType.Shipping || address.Value.AddressType == AddressType.Shipping) destinationAddressIndex = address.Index.ToString(CultureInfo.InvariantCulture); } getTaxRequest.Addresses = addresses.ToArray(); // Line Data // Required Parameters getTaxRequest.Lines = order.Items.Select(li => new Line { LineNo = li.Id, ItemCode = li.ProductId, Qty = li.Quantity, Amount = li.Price * li.Quantity, OriginCode = destinationAddressIndex, //TODO set origin address (fulfillment?) DestinationCode = destinationAddressIndex, Description = li.Name, TaxCode = li.TaxType } ).ToList(); //Add shipments as lines if (order.Shipments != null && order.Shipments.Any()) { order.Shipments.ForEach(sh => getTaxRequest.Lines.Add(new Line { LineNo = sh.Id ?? sh.ShipmentMethodCode, ItemCode = sh.ShipmentMethodCode, Qty = 1, Amount = sh.Sum, OriginCode = destinationAddressIndex, //TODO set origin address (fulfillment?) DestinationCode = destinationAddressIndex, Description = sh.ShipmentMethodCode, TaxCode = sh.TaxType ?? "FR" }) ); } return getTaxRequest; } return null; }
public static void Test() { // Header Level Elements // Required Header Level Elements string accountNumber = "1234567890"; string licenseKey = "A1B2C3D4E5F6G7H8"; string serviceURL = "https://development.avalara.net"; TaxSvc taxSvc = new TaxSvc(accountNumber, licenseKey, serviceURL); GetTaxRequest getTaxRequest = new GetTaxRequest(); // Document Level Elements // Required Request Parameters getTaxRequest.CustomerCode = "ABC4335"; getTaxRequest.DocDate = "2014-01-01"; // Best Practice Request Parameters getTaxRequest.CompanyCode = "APITrialCompany"; getTaxRequest.Client = "AvaTaxSample"; getTaxRequest.DocCode = "INV001"; getTaxRequest.DetailLevel = DetailLevel.Tax; getTaxRequest.Commit = false; getTaxRequest.DocType = DocType.SalesInvoice; // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters getTaxRequest.PurchaseOrderNo = "PO123456"; getTaxRequest.ReferenceCode = "ref123456"; getTaxRequest.PosLaneCode = "09"; getTaxRequest.CurrencyCode = "USD"; // Address Data Address address1 = new Address(); address1.AddressCode = "01"; address1.Line1 = "45 Fremont Street"; address1.City = "San Francisco"; address1.Region = "CA"; Address address2 = new Address(); address2.AddressCode = "02"; address2.Line1 = "118 N Clark St"; address2.Line2 = "Suite 100"; address2.Line3 = "ATTN Accounts Payable"; address2.City = "Chicago"; address2.Region = "IL"; address2.Country = "US"; address2.PostalCode = "60602"; Address address3 = new Address(); address3.AddressCode = "03"; address3.Latitude = (decimal)47.627935; address3.Longitude = (decimal)-122.51702; Address[] addresses = { address1, address2, address3 }; getTaxRequest.Addresses = addresses; // Line Data // Required Parameters Line line1 = new Line(); line1.LineNo = "01"; line1.ItemCode = "N543"; line1.Qty = 1; line1.Amount = 10; line1.OriginCode = "01"; line1.DestinationCode = "02"; // Best Practice Request Parameters line1.Description = "Red Size 7 Widget"; line1.TaxCode = "NT"; // Situational Request Parameters // line1.CustomerUsageType = "L"; // line1.Discounted = true; // line1.TaxIncluded = true; // line1.TaxOverride = new TaxOverrideDef(); // line1.TaxOverride.TaxOverrideType = "TaxDate"; // line1.TaxOverride.Reason = "Adjustment for return"; // line1.TaxOverride.TaxDate = "2013-07-01"; // line1.TaxOverride.TaxAmount = "0"; // Optional Request Parameters line1.Ref1 = "ref123"; line1.Ref2 = "ref456"; Line line2 = new Line(); line2.LineNo = "02"; line2.ItemCode = "T345"; line2.Qty = 3; line2.Amount = 150; line2.OriginCode = "01"; line2.DestinationCode = "03"; line2.Description = "Size 10 Green Running Shoe"; line2.TaxCode = "PC030147"; Line line3 = new Line(); line3.LineNo = "02-FR"; line3.ItemCode = "FREIGHT"; line3.Qty = 1; line3.Amount = 15; line3.OriginCode = "01"; line3.DestinationCode = "03"; line3.Description = "Shipping Charge"; line3.TaxCode = "FR"; Line[] lines = { line1, line2, line3 }; getTaxRequest.Lines = lines; GetTaxResult getTaxResult = taxSvc.GetTax(getTaxRequest); // Print results Console.WriteLine("GetTaxTest Result: " + getTaxResult.ResultCode.ToString()); if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success)) { foreach (Message message in getTaxResult.Messages) { Console.WriteLine(message.Summary); } } else { Console.WriteLine("Document Code: " + getTaxResult.DocCode + " Total Tax: " + getTaxResult.TotalTax); foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty<TaxLine>()) { Console.WriteLine(" " + "Line Number: " + taxLine.LineNo + " Line Tax: " + taxLine.Tax.ToString()); foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty<TaxDetail>()) { Console.WriteLine(" " + "Jurisdiction: " + taxDetail.JurisName + "Tax: " + taxDetail.Tax.ToString()); } } } }
public static void Test() { // Header Level Elements // Required Header Level Elements string accountNumber = ConfigurationManager.AppSettings["AvaTax:AccountNumber"]; string licenseKey = ConfigurationManager.AppSettings["AvaTax:LicenseKey"]; string serviceURL = ConfigurationManager.AppSettings["AvaTax:ServiceUrl"]; TaxSvc taxSvc = new TaxSvc(accountNumber, licenseKey, serviceURL); GetTaxRequest getTaxRequest = new GetTaxRequest(); // Document Level Elements // Required Request Parameters getTaxRequest.CustomerCode = "ABC4335"; getTaxRequest.DocDate = "2014-01-01"; // Best Practice Request Parameters getTaxRequest.CompanyCode = "APITrialCompany"; getTaxRequest.Client = "AvaTaxSample"; getTaxRequest.DocCode = "INV001"; getTaxRequest.DetailLevel = DetailLevel.Tax; getTaxRequest.Commit = false; getTaxRequest.DocType = DocType.SalesInvoice; // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters getTaxRequest.PurchaseOrderNo = "PO123456"; getTaxRequest.ReferenceCode = "ref123456"; getTaxRequest.PosLaneCode = "09"; getTaxRequest.CurrencyCode = "USD"; // Address Data Address address1 = new Address(); address1.AddressCode = "01"; address1.Line1 = "45 Fremont Street"; address1.City = "San Francisco"; address1.Region = "CA"; Address address2 = new Address(); address2.AddressCode = "02"; address2.Line1 = "118 N Clark St"; address2.Line2 = "Suite 100"; address2.Line3 = "ATTN Accounts Payable"; address2.City = "Chicago"; address2.Region = "IL"; address2.Country = "US"; address2.PostalCode = "60602"; Address address3 = new Address(); address3.AddressCode = "03"; address3.Latitude = (decimal)47.627935; address3.Longitude = (decimal) - 122.51702; Address[] addresses = { address1, address2, address3 }; getTaxRequest.Addresses = addresses; // Line Data // Required Parameters Line line1 = new Line(); line1.LineNo = "01"; line1.ItemCode = "N543"; line1.Qty = 1; line1.Amount = 10; line1.OriginCode = "01"; line1.DestinationCode = "02"; // Best Practice Request Parameters line1.Description = "Red Size 7 Widget"; line1.TaxCode = "NT"; // Situational Request Parameters // line1.CustomerUsageType = "L"; // line1.Discounted = true; // line1.TaxIncluded = true; // line1.BusinessIdentificationNo = "234243"; // line1.TaxOverride = new TaxOverrideDef(); // line1.TaxOverride.TaxOverrideType = "TaxDate"; // line1.TaxOverride.Reason = "Adjustment for return"; // line1.TaxOverride.TaxDate = "2013-07-01"; // line1.TaxOverride.TaxAmount = "0"; // Optional Request Parameters line1.Ref1 = "ref123"; line1.Ref2 = "ref456"; Line line2 = new Line(); line2.LineNo = "02"; line2.ItemCode = "T345"; line2.Qty = 3; line2.Amount = 150; line2.OriginCode = "01"; line2.DestinationCode = "03"; line2.Description = "Size 10 Green Running Shoe"; line2.TaxCode = "PC030147"; Line line3 = new Line(); line3.LineNo = "02-FR"; line3.ItemCode = "FREIGHT"; line3.Qty = 1; line3.Amount = 15; line3.OriginCode = "01"; line3.DestinationCode = "03"; line3.Description = "Shipping Charge"; line3.TaxCode = "FR"; Line[] lines = { line1, line2, line3 }; getTaxRequest.Lines = lines; GetTaxResult getTaxResult = taxSvc.GetTax(getTaxRequest); // Print results Console.WriteLine("GetTaxTest Result: " + getTaxResult.ResultCode.ToString()); if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success)) { foreach (Message message in getTaxResult.Messages) { Console.WriteLine(message.Summary); } } else { Console.WriteLine("Document Code: " + getTaxResult.DocCode + " Total Tax: " + getTaxResult.TotalTax); foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty <TaxLine>()) { Console.WriteLine(" " + "Line Number: " + taxLine.LineNo + " Line Tax: " + taxLine.Tax.ToString()); foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty <TaxDetail>()) { Console.WriteLine(" " + "Jurisdiction: " + taxDetail.JurisName + "Tax: " + taxDetail.Tax.ToString()); } } } }
//This loads some test data and returns a GetTaxRequest object for tax calculation. //TODO: These values should not be hardcoded in your integration. You will need to map to the values appropriate to your system. public static GetTaxRequest Load() { GetTaxRequest getTaxRequest = new GetTaxRequest(); //Document Level Setup // R: indicates Required Element // O: Indicates Optional Element // // Set the tax document properties - Required unless noted as Optional //getTaxRequest.CompanyCode = "SDK"; // R: Company Code from the Admin Console. This is passed in to the GetTax.Get function, since it is information typically maintained with the credentials. getTaxRequest.Client = "AvaTaxCalcRESTCsharp Sample"; DateTime docDate = DateTime.Today; getTaxRequest.DocCode = "SampleDoc: " + docDate.ToString(); // R: Invoice or document tracking number - Must be unique getTaxRequest.DocType = DocType.SalesInvoice; // R: Typically SalesOrder,SalesInvoice, ReturnInvoice getTaxRequest.DocDate = docDate.ToString("yyyy-MM-dd"); // R: sets reporting date and default tax date getTaxRequest.CustomerCode = "TaxSvcTest"; // R: String - Customer Tracking number or Exemption Customer Code getTaxRequest.DetailLevel = DetailLevel.Tax; // R: Chose Summary, Document, Line or Tax - varying levels of results detail getTaxRequest.Commit = false; // O: Default is "false" - Set to "true" to commit the Document //getTaxRequest.CustomerUsageType="G"; // O: Send for tax exempt transactions only. //getTaxRequest.ExemptionNo="12334"; // O: Send for tax exempt transactions only. //getTaxRequest.Discount=0; // O: Send for document-level discounts only. getTaxRequest.PurchaseOrderNo = "PO 23423"; // O: Specifies the purchase order number associated with the transaction. This value can be used to track single-use exemption certficates. getTaxRequest.ReferenceCode = ""; // O: This is a reportable value that does not affect tax calculation. getTaxRequest.PosLaneCode = ""; // O: This is a reportable value that does not affect tax calculation. //getTaxRequest.TaxOverride=new TaxOverrideDef(); // O: Allows the TaxDate =or other values) to be overridden for tax calculation. Situational only. //getTaxRequest.BusinessIdentificationNo=""; // O: Specified VAT ID of customer for international/VAT calculations and reporting. // Begin Address Section // Add the origin and destination addresses referred to by the // "setOriginCode" and "setDestinationCode" properties above. Address origin = new Address(); origin.AddressCode = "Origin"; origin.Line1 = "Avalara"; origin.Line2 = "100 Ravine Lane NE"; origin.Line3 = "Suite 220"; origin.City = "Bainbridge Island"; origin.Region = "WA"; origin.PostalCode = "98110"; origin.Country = "USA"; Address destination = new Address(); destination.AddressCode = "Dest"; destination.Line1 = "7462 Kearny Street"; destination.City = "Commerce City"; destination.Region = "CO"; destination.PostalCode = "80022"; destination.Country = "USA"; Address[] addresses = { origin, destination }; // // Alternate: Latitude / Longitude addressing // // // Address origin = new BaseAddress=; // origin.AddressCode="Origin"; // origin.Latitude="47.6253"; // origin.Longitude="-122.515114"; // // Address destination = new BaseAddress=; // destination.AddressCode="Destination"; // destination.Latitude="39.833597"; // destination.Longitude="-104.917220"; // Address[] addresses = {origin, destination}; // End Address Section getTaxRequest.Addresses = addresses; // Add invoice lines Line line1 = new Line(); // New instance of a line line1.LineNo = "101"; // R: string - line Number of invoice - must be unique. line1.ItemCode = "Item001"; // R: string - SKU or short name of Item line1.Qty = 1; // R: decimal - The number of items -- Qty of product sold. Does not function as a mulitplier for Amount line1.Amount = 1000; // R: decimal - the "NET" amount -- Amount should be the 'extended' or 'net' amount line1.CustomerUsageType = ""; // O: string - AKA Entity Use Code - Typically A - L =G = Reseller) line1.Description = "ITEM1"; // O: string - Description or category of item sold. line1.TaxCode = ""; // O: string - Pass standard, custom or Pro-Tax code // Can be NULL to default to tangible personal property =P0000000) line1.OriginCode = "Origin"; // R: Value representing the Origin Address line1.DestinationCode = "Dest"; // R: Value representing the Destination Address //Line 2 - Shipping/Freight line - See property descriptions above Line line2 = new Line(); // New instance of a line line2.LineNo = "102"; // R: string - SKU or short name of Item line2.ItemCode = "Shipping"; // R: string - SKU or short name of Item line2.Description = "Shipping- Freight Charges"; // O: string - Description or category of item sold. line2.Qty = 1; // R: decimal - The number of items -- Qty of product sold. Does not function as a mulitplier for Amount line2.Amount = 10; // R: decimal - the "NET" amount -- Amount should be the 'extended' or 'net' amount line2.TaxCode = "FR"; // O: string - Pass standard, custom or Pro-Tax code FR020100 line2.OriginCode = "Origin"; // R: Value representing the Origin Address line2.DestinationCode = "Dest"; // R: Value representing the Destination Address Line[] lines = { line1, line2 }; getTaxRequest.Lines = lines; // Sets array of lines return(getTaxRequest); }
//This loads the invoice (or return) located at the specified path and returns a GetTaxRequest object for tax calculation. public static GetTaxRequest Load() { GetTaxRequest req = new GetTaxRequest(); //loads the invoice file string[] txtInv = File.ReadAllLines("INV0001.txt"); //Parses header-level data from the invoice file req.DocCode = txtInv[0].Split(':')[1] + DateTime.Now.ToString(); req.CustomerCode = txtInv[1].Split(':')[1]; req.DocDate = "2012-07-07";//txtInv[3].Split(':')[1]; req.DocType = DocType.SalesInvoice; string[] shipto = txtInv[10].Split(':')[1].Split(','); req.Addresses = new Address[2]; //We will need to pass in two addresses, our origin and destination. //Parse our destiniation address Address dest = new Address(); dest.AddressCode = "01"; dest.Line1 = shipto[0]; dest.City = shipto[1]; dest.Region = shipto[2]; dest.PostalCode = shipto[3]; //Add the address to our request object. req.Addresses[0] = new Address(); req.Addresses[0] = dest; //Hardcodes the origin address for the GetTaxRequest. This should be your warehouse or company address, and should not be hardcoded. req.Addresses[1] = new Address(); req.Addresses[1].AddressCode = "02"; req.Addresses[1].Line1 = "PO Box 123"; req.Addresses[1].City = "Bainbridge Island"; req.Addresses[1].Region = "WA"; req.Addresses[1].PostalCode = "98110"; //create array of line items req.Lines = new Line[txtInv.Length - 12]; //Iterate through line items on transaction and add them to the request for (int i = 1; txtInv.Length > 12 + i; i++) { string[] item = txtInv[12 + i].Split(','); req.Lines[i] = new Line(); req.Lines[i].LineNo = item[0]; req.Lines[i].ItemCode = item[1]; req.Lines[i].Qty = Convert.ToDecimal(item[3]); req.Lines[i].Amount = Convert.ToDecimal(item[4]) * req.Lines[i].Qty; req.Lines[i].OriginCode = "02"; req.Lines[i].DestinationCode = "01"; } //Pull the freight line from the header information and add to the request as an additional line item req.Lines[0] = new Line(); req.Lines[0].ItemCode = "Shipping"; req.Lines[0].Qty = 1; req.Lines[0].LineNo = "FR"; req.Lines[0].Amount = Convert.ToDecimal(txtInv[7].Split(':')[1]); req.Lines[0].OriginCode = "02"; req.Lines[0].DestinationCode = "01"; return(req); }