/// <summary> /// Calculate taxes due on order items. Determined by a combination of /// TaxCode used, address information, and nexus/jurisdiction. /// </summary> /// <param name="order"></param> /// <returns></returns> public static Order Calculate(Order order) { var taxDetail = new OrderTaxDetail(); if (Ping()) { var taxRequest = new GetTaxRequest { CustomerCode = order.DistId.ToString(), DocDate = DateTime.Now, CompanyCode = ConfigService.CompanyCode(), DocCode = order.Id.ToString(), DetailLevel = DetailLevel.Tax, Commit = false, DocType = (DocumentType) order.DocType, OriginAddress = ConvertAddress(order, AddressType.ShipFrom), DestinationAddress = ConvertAddress(order, AddressType.ShipTo), SalespersonCode = order.SalesPersonCode, }; if (order.TaxOverride != null) { taxRequest.TaxOverride.TaxOverrideType = (Avalara.AvaTax.Adapter.TaxService.TaxOverrideType) order.TaxOverride.TaxOverrideType; taxRequest.TaxOverride.Reason = order.TaxOverride.Reason; taxRequest.TaxOverride.TaxAmount = order.TaxOverride.TaxAmount; taxRequest.TaxOverride.TaxDate = order.TaxOverride.TaxDate; } // Ship from address var address1 = new Address { Line1 = order.ShipFromAddress.Address1, Line2 = order.ShipFromAddress.Address2, City = order.ShipFromAddress.City, Region = order.ShipFromAddress.State, PostalCode = order.ShipFromAddress.PostalCode }; // Ship to address var address2 = new Address { Line1 = order.ShipToAddress.Address1, Line2 = order.ShipToAddress.Address2, City = order.ShipToAddress.City, Region = order.ShipToAddress.State, PostalCode = order.ShipToAddress.PostalCode }; int lineCount = 1; foreach (var item in order.Items) { // Line data var line = new Line { No = lineCount.ToString("00000") + "-" + item.Id, ItemCode = item.Sku, Qty = item.Qty, Amount = item.Price, OriginAddress = address1, DestinationAddress = address2, Description = item.Description, TaxCode = item.TaxCode }; taxRequest.Lines.Add(line); lineCount += 1; } GetTaxResult taxResult = null; try { taxResult = SetupTaxSvc.Result().GetTax(taxRequest); } catch(Exception ex) { order.Error = true; order.ErrorException = ex; } if (!order.Error) { if (taxResult != null) { taxDetail.DocCode = taxResult.DocCode; taxDetail.TotalAmount = taxResult.TotalAmount; taxDetail.TotalTax = taxResult.TotalTax; taxDetail.TotalTaxable = taxResult.TotalTaxable; taxDetail.TransactionId = taxResult.TransactionId; var lines = new List<OrderTaxLine>(); foreach (TaxLine taxLine in taxResult.TaxLines) { var line = new OrderTaxLine { LineNo = taxLine.No, Rate = taxLine.Rate, Tax = taxLine.Tax, TaxCalculated = taxLine.TaxCalculated, AccountingMethod = taxLine.AccountingMethod.ToString(), BoundaryLevel = taxLine.BoundaryLevel.ToString(), TaxCode = taxLine.TaxCode }; lines.Add(line); } taxDetail.OrderTaxLines = lines; taxDetail.OrderTotal = taxResult.TotalAmount + taxResult.TotalTax; } order.OrderTaxDetail = taxDetail; } } return order; }
private static Order TestOrder() { var order = new Order {DocType = DocType.SalesOrder, SalesPersonCode = "TestApp"}; // create addresses for shipping and handling var addressOrigin = new OrderAddress { Address1 = "1313 N Boulevard", City = "Anaheim", State = "CA", PostalCode = "92802" }; order.ShipFromAddress = addressOrigin; var addressDestination = new OrderAddress { Address1 = "800 Occidental Ave South", Address2 = "#100", City = "Seattle", State = "WA", PostalCode = "98134" }; order.ShipToAddress = addressDestination; // create sample items var items = new List<Item>(); var item1 = new Item { Id = 1787, Sku = "TK421", Description = "Security Detail", Price = Convert.ToDecimal(25.00), Qty = 1, TaxCode = TaxCodeItem }; items.Add(item1); var item2 = new Item { Id = 1931, Sku = "TK422", Description = "Security Response Product", Price = Convert.ToDecimal(75.00), // Item Total - must be summed before assigning value Qty = 3, TaxCode = TaxCodeItem }; items.Add(item2); var item3 = new Item { Id = 325, Sku = "TK500", Description = "24-hour Security Drive By", Price = Convert.ToDecimal(40.00), Qty = 2, TaxCode = TaxCodeItem }; items.Add(item3); var itemShipping = new Item { Id = 0, Sku = "SHIPPING", Description = "Shipping Charge", Price = Convert.ToDecimal(5.00), Qty = 1, TaxCode = TaxCodeShipping }; items.Add(itemShipping); var itemHandling = new Item { Id = 0, Sku = "HANDLING", Description = "Handling Charge", Price = Convert.ToDecimal(2.00), Qty = 1, TaxCode = TaxCodeHandling }; items.Add(itemHandling); order.Items = items; // populate order fields order.Id = RandomOrderId(); order.DistId = RandomDistId(); return order; }
public static Address ConvertAddress(Order order, AddressType type) { var address = new Address(); switch (type) { case AddressType.ShipFrom: address.Line1 = order.ShipFromAddress.Address1; address.Line2 = order.ShipFromAddress.Address2; address.City = order.ShipFromAddress.City; address.Region = order.ShipFromAddress.State; address.Country = order.ShipFromAddress.Country; address.PostalCode = order.ShipFromAddress.PostalCode; break; case AddressType.ShipTo: address.Line1 = order.ShipToAddress.Address1; address.Line2 = order.ShipToAddress.Address2; address.City = order.ShipToAddress.City; address.Region = order.ShipToAddress.State; address.Country = order.ShipToAddress.Country; address.PostalCode = order.ShipToAddress.PostalCode; break; } return address; }