public void OrderPlaced(Order order) { if (!Enabled) { throw new InvalidOperationException("AvalaraInactiveException"); } var customer = new Customer(order.CustomerID); var cartItems = order.CartItems; var orderOptions = GetOrderOptions(order); // Create line items for all cart items and shipping selections var cartItemAddressGroups = GroupCartItemsByShippingAddress(cartItems, customer.PrimaryShippingAddressID); var lineItems = CreateItemAndShippingLineItems(cartItemAddressGroups, (shipmentAddressId, shipmentAddress, shipmentCartItems) => CreateOrderShippingLineItem(shipmentAddress, shipmentCartItems, order, shipmentAddressId)); // Create line items for order options using the first shipping address as the destination var firstShippingAddress = LoadAvalaraAddress(cartItemAddressGroups.First().Key); lineItems = lineItems.Concat(CreateOrderOptionLineItems(orderOptions, firstShippingAddress)); // Calculate the discount from the promotion usages for this order decimal discountAmount; using (var promotionsDataContext = new AspDotNetStorefront.Promotions.Data.EntityContextDataContext()) { discountAmount = promotionsDataContext.PromotionUsages .Where(pu => pu.OrderId == order.OrderNumber) .Where(pu => pu.Complete) .Sum(pu => - pu.OrderDiscountAmount) // Avalara expects a positive number .GetValueOrDefault(0); } // Build the tax request var taxRequest = BuildTaxRequest(customer, GetOriginAddress(), DocumentType.SalesInvoice); taxRequest.Discount = discountAmount; taxRequest.DocCode = order.OrderNumber.ToString(); taxRequest.TaxOverride.TaxDate = order.OrderDate; taxRequest.DocDate = DateTime.Now; taxRequest.Commit = CommitTaxes; // Add each line to the request, setting the line number sequentially var lineItemIndex = 0; foreach (var line in lineItems) { line.No = (++lineItemIndex).ToString(); taxRequest.Lines.Add(line); } //Submit the request var taxService = CreateTaxService(); var taxResult = taxService.GetTax(taxRequest); foreach (Message message in taxResult.Messages) { LogErrorMessage(message); //this throws an exception } }
private Line CreateLineItem(CartItem cartItem, Avalara.AvaTax.Adapter.AddressService.Address destinationAddress) { decimal extendedPrice; if (cartItem.ThisShoppingCart == null) { // Order line items using (var promotionsDataContext = new AspDotNetStorefront.Promotions.Data.EntityContextDataContext()) { // Sum the discount for every PromotionLineItem that applies to the current cart item. // A gift product's line item price is already discounted, so don't include the discount when IsAGift is true. var lineItemDiscountAmount = promotionsDataContext.PromotionLineItems .Where(pli => !pli.isAGift) .Where(pli => pli.shoppingCartRecordId == cartItem.ShoppingCartRecordID) .Sum(pli => (decimal?)pli.discountAmount); extendedPrice = cartItem.Price + (lineItemDiscountAmount ?? 0); } } else { // Shopping cart items CartItemCollection cartItems = cartItem.ThisShoppingCart.CartItems; extendedPrice = Prices.LineItemPrice(cartItem, cartItems.CouponList, cartItems.QuantityDiscountList, cartItem.ThisCustomer); } Line lineItem = new Line { ItemCode = cartItem.SKU, Description = cartItem.ProductName, Amount = extendedPrice, Qty = (double)cartItem.Quantity, Discounted = true, DestinationAddress = destinationAddress, }; if (cartItem.IsTaxable) { var lineItemTaxClass = new TaxClass(cartItem.TaxClassID); lineItem.TaxCode = lineItemTaxClass.TaxCode; } else { lineItem.TaxCode = "NT"; } lineItem.TaxOverride.TaxDate = System.DateTime.Today; return(lineItem); }
private IEnumerable <net.taxcloud.api.CartItem> ConvertCartItems(IEnumerable <CartItem> cartItems, Customer customer, List <CouponObject> CouponList, List <QDObject> QuantityDiscountList) { IList <net.taxcloud.api.CartItem> refCartItems = new List <net.taxcloud.api.CartItem>(); foreach (CartItem i in cartItems) { decimal extendedPrice = Decimal.Zero; if (i.ThisShoppingCart == null) { // Order line items using (var promotionsDataContext = new AspDotNetStorefront.Promotions.Data.EntityContextDataContext()) { // Sum the discount for every PromotionLineItem that applies to the current cart item. // A gift product's line item price is already discounted, so don't include the discount when IsAGift is true. var lineItemDiscountAmount = promotionsDataContext.PromotionLineItems .Where(pli => !pli.isAGift) .Where(pli => pli.shoppingCartRecordId == i.ShoppingCartRecordID) .Sum(pli => (decimal?)pli.discountAmount); extendedPrice = i.Price + (lineItemDiscountAmount ?? 0); } } else { // Shopping cart items extendedPrice = Prices.LineItemPrice(i, CouponList, QuantityDiscountList, customer); } net.taxcloud.api.CartItem refCartItem = new net.taxcloud.api.CartItem() { Index = i.ShoppingCartRecordID, ItemID = i.SKU, TIC = Localization.ParseNativeInt(new TaxClass(i.TaxClassID).TaxCode), Price = (double)extendedPrice / i.Quantity, Qty = (float)i.Quantity, }; refCartItems.Add(refCartItem); } return(refCartItems); }
public void IssueRefund(Order order, Address originAddress, decimal refundTotal) { if (!Enabled) { throw new InvalidOperationException("AvalaraInactiveException"); } var customer = new Customer(order.CustomerID); var cartItems = order.CartItems; var orderOptions = GetOrderOptions(order); var numberOfRefundTransactions = (order.ChildOrderNumbers ?? string.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length; decimal discountAmount; using (var promotionsDataContext = new AspDotNetStorefront.Promotions.Data.EntityContextDataContext()) { discountAmount = promotionsDataContext.PromotionUsages .Where(pu => pu.OrderId == order.OrderNumber) .Where(pu => pu.Complete) .Sum(pu => pu.OrderDiscountAmount) // Avalara expects a positive discount amount everywhere else, but it should be negative on refunds .GetValueOrDefault(0); } // Build the tax request var taxRequest = BuildTaxRequest(customer, GetOriginAddress(), DocumentType.SalesInvoice); taxRequest.Discount = discountAmount; taxRequest.DocCode = order.OrderNumber.ToString() + "." + (numberOfRefundTransactions + 1).ToString(); taxRequest.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate; taxRequest.TaxOverride.TaxDate = order.OrderDate; taxRequest.TaxOverride.Reason = "Refund"; taxRequest.DocDate = DateTime.Now; taxRequest.Commit = CommitRefunds; if (refundTotal > 0) { // Partial refund // Add in the refund line object var refundLineItem = new Line { Amount = -refundTotal, Qty = 1, No = "1", TaxCode = TaxRefunds ? null : "NT" // Make this refund non-taxable as recommended by Avalara }; taxRequest.Lines.Add(refundLineItem); } else { // Refund the full order // Void any partial refunds VoidRefunds(order); // Add order items to the refund transaction // Create line items for all cart items and shipping selections var cartItemAddressGroups = GroupCartItemsByShippingAddress(cartItems, customer.PrimaryShippingAddressID); var lineItems = CreateItemAndShippingLineItems(cartItemAddressGroups, (shipmentAddressId, shipmentAddress, shipmentCartItems) => CreateOrderShippingLineItem(shipmentAddress, shipmentCartItems, order, shipmentAddressId)); // Create line items for order options using the first shipping address as the destination var firstShippingAddress = LoadAvalaraAddress(cartItemAddressGroups.First().Key); lineItems = lineItems.Concat(CreateOrderOptionLineItems(orderOptions, firstShippingAddress)); // Add each line to the request, setting the line number sequentially var lineItemIndex = 0; foreach (var line in lineItems) { line.Amount = -line.Amount; line.No = (++lineItemIndex).ToString(); taxRequest.Lines.Add(line); } } //Submit the request var taxService = CreateTaxService(); var taxResult = taxService.GetTax(taxRequest); foreach (Message message in taxResult.Messages) { LogErrorMessage(message); } }