示例#1
0
        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);
        }
示例#2
0
 static public bool HasFreeShippingComponents(CartItemCollection CartItems)
 {
     // CartItemCollection inherits from List, so .Count in C# doesn't directly
     // translate to LINQ in VB. Use .Where(Func predicate).Count() instead...
     return(CartItems.Where(ci => ci.FreeShipping).Count() > 0);
 }