Example #1
0
 public CartItemModel()
 {
     if (this.cartItem == null)
     {
         this.cartItem = new CartItem();
     }
 }
Example #2
0
        /// <summary>
        /// Create new instance
        /// </summary>
        public ItemData(CartItem cartItem, Product product, Guid renewalPeriodId)
        {
            if (cartItem == null)
            {
                throw new ArgumentNullException("cartItem");
            }

            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            this.cartItem = cartItem;
            this.product = product;
            this.renewalPeriodId = renewalPeriodId;
        }
        /// <summary>
        /// Get Atomia Billing renewal period id for <see cref="CartItem"/>
        /// </summary
        public Guid GetRenewalPeriodId(CartItem cartItem)
        {
            if (cartItem.RenewalPeriod == null)
            {
                return Guid.Empty;
            }

            var period = cartItem.RenewalPeriod.Period;
            var unit = cartItem.RenewalPeriod.Unit;
            var product = apiProductsProvider.GetProduct(cartItem.ArticleNumber);

            var apiRenewalPeriod = product.RenewalPeriods
                .FirstOrDefault(r => r.RenewalPeriodUnit.ToUpper() == unit && r.RenewalPeriodValue == period);

            if (apiRenewalPeriod == null)
            {
                throw new InvalidOperationException(String.Format("No renewal period {0} {1} found", period, unit));
            }

            return apiRenewalPeriod.Id;
        }
Example #4
0
 public CartItemModel(CartItem cartItem) : this()
 {
     this.cartItem = cartItem;
 }
Example #5
0
        /// <summary>
        /// Add a single cart item to cart.
        /// </summary>
        /// <param name="cartItem">The cart item to add.</param>
        /// <returns>The id assigned to the cart item.</returns>
        public Guid AddItem(CartItem cartItem)
        {
            if (cartItem == null)
            {
                throw new ArgumentNullException("cartItem");
            }
            
            cartItem.Id = Guid.NewGuid();
            this.cartItems.Add(cartItem);


            try
            {
                RecalculatePricingAndSave();
            }
            catch(Exception)
            {
                this.cartItems.Remove(cartItem);
                throw;
            }
            

            return cartItem.Id;
        }
        /// <summary>
        /// Add or remove any required setup fees before calculating pricing for cart. 
        /// Setup fee items are marked with "NotRemovable" custom attributes as help to e.g. GUI code.
        /// </summary>
        public Cart CalculatePricing(Cart cart)
        {
            var setupFee = apiProductsProvider.GetProductsByCategories(new List<string>() { SetupFeeCategory }).FirstOrDefault();
            
            if (setupFee != null)
            {
                var allSetupFeeProducts = apiProductsProvider.GetProductsByCategories(ProductCategoriesWithSetupFee.ToList());
                var freePackageProducts = apiProductsProvider.GetProductsByCategories(new List<string> { FreePackageCategory });
                var setupFeeProducts = allSetupFeeProducts.Where(s => !freePackageProducts.Any(f => s.ArticleNumber == f.ArticleNumber));

                var shouldHaveSetupFee = setupFeeProducts.Any(p => cart.CartItems.Any(ci => ci.ArticleNumber == p.ArticleNumber));
                var hasSetupFee = cart.CartItems.Any(ci => ci.ArticleNumber == setupFee.ArticleNumber);

                if (shouldHaveSetupFee && !hasSetupFee)
                {
                    var setupFeeItem = new CartItem
                    {
                        ArticleNumber = setupFee.ArticleNumber,
                        Quantity = 1,
                        RenewalPeriod = null,
                        CustomAttributes = new List<Core.CustomAttribute>()
                        {
                            new Core.CustomAttribute{ Name = "NotRemovable", Value = "true" }
                        }
                    };

                    cart.AddItem(setupFeeItem);
                }
                else if (!shouldHaveSetupFee && hasSetupFee)
                {
                    var setupFeeItem = cart.CartItems.First(ci => ci.ArticleNumber == setupFee.ArticleNumber);
                    
                    cart.RemoveItem(setupFeeItem.Id);
                }
            }
            
            var calculatedCart = baseCartPricingService.CalculatePricing(cart);

            return calculatedCart;
        }