public decimal GetTaxRate(Customer customer, CartItemCollection cartItems, IEnumerable <OrderOption> orderOptions) { if (!customer.HasAtLeastOneAddress() || !cartItems.Any()) { return(0m); } // Return the cached value if it's present and still valid string lastCartHash = HttpContext.Current.Items["TaxCloud.CartHash"] as string; decimal?lastTaxAmount = HttpContext.Current.Items["TaxCloud.TaxAmount"] as decimal?; string currentCartHash = GetCartHash(cartItems, customer, orderOptions); if (lastTaxAmount != null && currentCartHash == lastCartHash) { return(lastTaxAmount.Value); } // Create line items for all cart items and shipping selections decimal taxAmount = 0M; List <CouponObject> CouponList = cartItems.CouponList; List <QDObject> QuantityDiscountList = cartItems.QuantityDiscountList; if (Shipping.GetDistinctShippingAddressIDs(cartItems).Count == 1) { IEnumerable <net.taxcloud.api.CartItem> refCartitems = ConvertCartItems(cartItems, customer); net.taxcloud.api.Address destAddress = ConvertAddress(customer.PrimaryShippingAddress); refCartitems = refCartitems.Concat(CreateCartShippingLineItem(customer, cartItems, orderOptions)).Concat(CreateOrderOptionLineItems(orderOptions)); taxAmount = lookupTaxRate(customer.CustomerID.ToString(), refCartitems.ToArray(), refOrigin, destAddress); } else { List <int> shipAddresses = Shipping.GetDistinctShippingAddressIDs(cartItems); foreach (int _addressID in shipAddresses) { net.taxcloud.api.Address destAddress = ConvertAddress(_addressID); IEnumerable <CartItem> tmpcic = cartItems.Where(r => r.ShippingAddressID == _addressID); IEnumerable <net.taxcloud.api.CartItem> refCartitems = ConvertCartItems(tmpcic, customer, CouponList, QuantityDiscountList); refCartitems = refCartitems.Concat(CreateCartShippingLineItem(customer, tmpcic, orderOptions)); if (_addressID == customer.PrimaryShippingAddressID) { refCartitems = refCartitems.Concat(CreateOrderOptionLineItems(orderOptions)); } taxAmount += lookupTaxRate(customer.CustomerID.ToString(), refCartitems.ToArray(), refOrigin, destAddress); } } //Cache the tax amount HttpContext.Current.Items["TaxCloud.CartHash"] = currentCartHash; HttpContext.Current.Items["TaxCloud.TaxAmount"] = taxAmount; return(taxAmount); }
public decimal GetTaxRate(Customer customer, CartItemCollection cartItems, IEnumerable <OrderOption> orderOptions) { if (!Enabled) { throw new InvalidOperationException("AvalaraInactiveException"); } if (!customer.HasAtLeastOneAddress() || !cartItems.Any()) { return(0m); } // Create line items for all cart items and shipping selections var cartItemAddressGroups = GroupCartItemsByShippingAddress(cartItems, customer.PrimaryShippingAddressID); var lineItems = CreateItemAndShippingLineItems(cartItemAddressGroups, (shipmentAddressId, shipmentAddress, shipmentCartItems) => CreateCartShippingLineItem(shipmentAddress, customer, shipmentCartItems, orderOptions)); // 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)); var discountAmount = -cartItems.DiscountResults.Sum(dr => dr.OrderTotal); // This value is returned as negative, but Avalara expectes a positive // Build and submit the tax request var taxRequest = BuildTaxRequest(customer, GetOriginAddress(), DocumentType.SalesOrder); taxRequest.Discount = discountAmount; // Add each line to the request, setting the line number sequentially var lineItemIndex = 1; foreach (var line in lineItems) { line.No = (lineItemIndex++).ToString(); taxRequest.Lines.Add(line); } var taxService = CreateTaxService(); var taxResult = taxService.GetTax(taxRequest); foreach (Message message in taxResult.Messages) { LogErrorMessage(message); } return(taxResult.TotalTax); }
public decimal GetTaxRate(Customer customer, CartItemCollection cartItems, IEnumerable <OrderOption> orderOptions) { if (!Enabled) { throw new InvalidOperationException("AvalaraInactiveException"); } if (!customer.HasAtLeastOneAddress() || !cartItems.Any()) { return(0m); } // Return the cached value if it's present and still valid string lastCartHash = HttpContext.Current.Items["Avalara.CartHash"] as string; decimal?lastTaxAmount = HttpContext.Current.Items["Avalara.TaxAmount"] as decimal?; string currentCartHash = GetCartHash(cartItems, customer, orderOptions); if (lastTaxAmount != null && currentCartHash == lastCartHash) { return(lastTaxAmount.Value); } // Create line items for all cart items and shipping selections var cartItemAddressGroups = GroupCartItemsByShippingAddress(cartItems, customer.PrimaryShippingAddressID); var lineItems = CreateItemAndShippingLineItems(cartItemAddressGroups, (shipmentAddressId, shipmentAddress, shipmentCartItems) => CreateCartShippingLineItem(shipmentAddress, customer, shipmentCartItems, orderOptions)); // 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)); decimal discountAmount = -cartItems.DiscountResults.Sum(dr => dr.OrderTotal); // This value is returned as negative, but Avalara expectes a positive // Build and submit the tax request GetTaxRequest taxRequest = BuildTaxRequest(customer, GetOriginAddress(), DocumentType.SalesOrder); taxRequest.Discount = discountAmount; // Add each line to the request, setting the line number sequentially int lineItemIndex = 1; foreach (var line in lineItems) { line.No = (lineItemIndex++).ToString(); taxRequest.Lines.Add(line); } TaxSvc taxService = CreateTaxService(); GetTaxResult taxResult = taxService.GetTax(taxRequest); foreach (Message message in taxResult.Messages) { LogErrorMessage(message); } decimal taxAmount = taxResult.TotalTax; // Cache the tax amount HttpContext.Current.Items["Avalara.CartHash"] = currentCartHash; HttpContext.Current.Items["Avalara.TaxAmount"] = taxAmount; return(taxAmount); }