示例#1
0
        public async Task <Cart> CalculateCartAsync(
            TaxType taxType, ICatalogGateway catalogGateway,
            IPromoGateway promoGateway, IShippingGateway shippingGateway)
        {
            if (CartItems != null && CartItems?.Count() > 0)
            {
                CartItemTotal = 0.0D;
                foreach (var cartItem in CartItems)
                {
                    var product = await catalogGateway.GetProductByIdAsync(cartItem.Product.ProductId);

                    if (product == null)
                    {
                        throw new Exception("Could not find product.");
                    }

                    cartItem
                    .FillUpProductInfo(product.Name, product.Price, product.Desc)
                    .ChangePrice(product.Price);

                    CartItemPromoSavings = CartItemPromoSavings + cartItem.PromoSavings * cartItem.Quantity;
                    CartItemTotal        = CartItemTotal + cartItem.Product.Price * cartItem.Quantity;
                }

                shippingGateway.CalculateShipping(this);
            }

            promoGateway.ApplyShippingPromotions(this);

            switch (taxType)
            {
            case TaxType.NoTax:
                CartTotal = CartItemTotal + ShippingTotal;
                break;

            case TaxType.TenPercentage:
                var cartTotal = CartItemTotal + ShippingTotal;
                CartTotal = cartTotal * 10 / 100 + cartTotal;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(taxType), taxType, null);
            }

            return(this);
        }
示例#2
0
        public Domain.Cart Execute(Domain.Cart cart)
        {
            if (cart == null)
            {
                throw new Exception("Cart is null.");
            }

            if (cart.CartItems != null && cart.CartItems?.Count() > 0)
            {
                cart.CartItemTotal = 0;
                foreach (CartItem item in cart.CartItems)
                {
                    cart.CartItemPromoSavings = cart.CartItemPromoSavings + (item.PromoSavings * item.Quantity);
                    cart.CartItemTotal        = cart.CartItemTotal + (item.Product.Price * item.Quantity);
                }
                cart = _shippingGateway.CalculateShipping(cart);
            }

            cart           = _promoGateway.ApplyShippingPromotions(cart);
            cart.CartTotal = AddTaxCost(cart.CartItemTotal + cart.ShippingTotal);

            return(cart);
        }