Example #1
0
        private static void Question3()
        {
            var product = new Product()
            {
                Id = Guid.NewGuid(), Price = 12.00M
            };
            Cart cart = new Cart();

            cart.AddItemToCart(product);
            var payment = new Payment("us");

            payment.Calculate(cart);
            CartTotal cartTotal = payment.GetReceipt();

            payment.PrintReceipt(cartTotal);


            var product1 = new Product()
            {
                Id = Guid.NewGuid(), Price = 12.00M
            };
            var cart1 = new Cart();

            cart1.AddItemToCart(product1);
            var payment1 = new Payment("ca");

            payment1.Calculate(cart);
            CartTotal receipt1 = payment1.GetReceipt();

            payment1.PrintReceipt(receipt1);
        }
Example #2
0
        public ActionResult GetCartTotalAsync()
        {
            var cartItems = GetAllCartItems();
            BlueGreenContext bgContext = new BlueGreenContext();
            var cartTotalResult        = new CartTotal()
            {
                AvailableRewards   = (bgContext.bxgOwner != null && bgContext.bxgOwner.EncoreBenefits != null) ? bgContext.bxgOwner.EncoreBenefits.DividendsBalance : 0,
                TotalRewardsinCart = (cartItems.Count > 0) ? cartItems.Select(t => t.SubTotal).Sum() : 0,
            };

            cartTotalResult.AvailablePoints = cartTotalResult.AvailableRewards - cartTotalResult.TotalRewardsinCart;

            return(PartialView("RewardsCartTotal", cartTotalResult));
        }
Example #3
0
        public static void Question3Solution()
        {
            List <Product> products = new List <Product>();

            Random random = new Random();

            for (int i = 0; i < 100; i++)
            {
                products.Add(new Product()
                {
                    Id    = Guid.NewGuid(),
                    Price = random.NextDouble() * random.Next(1, 100)
                });
            }

            ILocalizationInfo us = new Us();
            ILocalizationInfo ca = new Canada();

            ICartTotal cart = new CartTotal();

            Console.WriteLine("US: " + cart.Calculate(products, us));
            Console.WriteLine("CA: " + cart.Calculate(products, ca));
        }
Example #4
0
        ///<summary>
        ///Compares Validity of each item from itemsTobeValidated to sourceData List With Sitecore Values
        ///</summary>
        ///<param name="itemsTobeValidated"></param>
        ///<param name="sourceData"></param>
        /// <returns>JsonResponse</returns>
        public JsonResponse validateCartItems(List <CartItem> itemsTobeValidated, List <CartItem> sourceData, bool isUpdate = false)
        {
            JsonResponse jsonResponse = new JsonResponse();

            jsonResponse.errors  = new List <string>();
            jsonResponse.RetCode = "1";
            Sitecore.Data.Database dbContext = Sitecore.Context.Database;

            BlueGreenContext bgContext   = new BlueGreenContext();
            var       encoreAvailRewards = (bgContext != null && bgContext.bxgOwner != null && bgContext.bxgOwner.EncoreBenefits != null) ? bgContext.bxgOwner.EncoreBenefits.DividendsBalance : 0;
            CartTotal CartTotal          = new CartTotal()
            {
                AvailableRewards   = encoreAvailRewards,
                TotalRewardsinCart = (sourceData.Count > 0) ? sourceData.Select(t => t.SubTotal).Sum() : 0,
                AvailablePoints    = encoreAvailRewards - ((sourceData.Count > 0) ? sourceData.Select(t => t.SubTotal).Sum() : 0)
            };

            //Loop to check Whether each item has Actual price listed in Sitecore
            foreach (CartItem itemToBeAdded in itemsTobeValidated)
            {
                // If the item price/SubTotal is zero do not allow to add to reward cart
                if (!isUpdate && (itemToBeAdded.UnitPrice == 0 || itemToBeAdded.SubTotal == 0))
                {
                    jsonResponse.RetCode = "1";
                    jsonResponse.errors.Add("Please try to add/update valid cart item");
                }

                if (!itemToBeAdded.ItemID.Equals("MF"))
                {
                    // verify that the unit price matches the actual unit price defined in Sitecore and has not been altered
                    Sitecore.Data.Items.Item thisItem = dbContext.GetItem(itemToBeAdded.ItemID);
                    string itemUnitPrice = thisItem.Fields["Item Value"].Value.Replace(",", "");
                    if (!itemToBeAdded.UnitPrice.Equals(Convert.ToDecimal(itemUnitPrice)))
                    {
                        // somebody tampered with the data manipulating the unit price
                        jsonResponse.RetCode = "1";
                        jsonResponse.errors.Add("Validation error: Unit Price Invalid");
                    }
                }

                if (itemToBeAdded.SubTotal != itemToBeAdded.Quantity * itemToBeAdded.UnitPrice)
                {
                    // somebody tampered with the data manipulating the unit price
                    jsonResponse.RetCode = "1";
                    jsonResponse.errors.Add("Validation error: Sub Total Tampered");
                }

                // Check whether you already have one Error generated if so , break loop to avoid duplicate Error on Page
                if (jsonResponse.errors.Any())
                {
                    break;
                }
            }
            // Execute the Code Only if there are no Errors from previous checks
            // This Code is for Case where List of Items come in through Update Process and Available points go as negative
            if (!jsonResponse.errors.Any())
            {
                if (CartTotal.AvailablePoints >= 0 && !isUpdate)
                {
                    decimal subTotalOfSingleItem = 0;
                    var     itemInCartAlready    = sourceData.Where(x => itemsTobeValidated.Any(item => x.ItemID.Contains(item.ItemID)));
                    if (itemInCartAlready.Any() && itemsTobeValidated.Any() && !itemsTobeValidated.First().ItemID.Equals("MF"))
                    {
                        // Hotfix Issue : Not enuf points - subTotalOfSingleItem = (subtotalOfItemWhichIsbeingAdded -subTotalFromDatabaseForThatItem)
                        var sumOfTotalFromIncomingList = itemsTobeValidated.Select(t => t.SubTotal).Sum();
                        var sumOfTotalFromDb           = itemInCartAlready.Select(z => z.SubTotal).Sum();
                        subTotalOfSingleItem = sumOfTotalFromIncomingList > sumOfTotalFromDb ? sumOfTotalFromIncomingList - sumOfTotalFromDb : sumOfTotalFromDb - sumOfTotalFromIncomingList;
                    }
                    else
                    {
                        subTotalOfSingleItem = (itemsTobeValidated.Count > 0) ? itemsTobeValidated.Select(t => t.SubTotal).Sum() : 0;
                    }

                    if (subTotalOfSingleItem <= CartTotal.AvailablePoints)
                    {
                        jsonResponse.RetCode = "0";
                    }
                    else
                    {
                        jsonResponse.RetCode = "1";
                        string customError = isUpdate ? "Your cart cannot be updated because the total required rewards exceeds the amount of available rewards you have earned. The quantity in cart has been adjusted to the prior quantity submitted." : "The selected item cannot be added to your cart because the total required rewards exceeds the amount of available rewards you have earned.";
                        jsonResponse.errors.Add(customError);
                    }
                }
                else if (CartTotal.AvailablePoints >= 0 && isUpdate)
                {
                    jsonResponse.RetCode = "0";
                }
                else
                {
                    string customError = string.Empty;
                    jsonResponse.RetCode = "1";
                    // This is to match JS message
                    customError = isUpdate ? "Your cart cannot be updated because the total required rewards exceeds the amount of available rewards you have earned. The quantity in cart has been adjusted to the prior quantity submitted." : "The selected item cannot be added to your cart because the total required rewards exceeds the amount of available rewards you have earned.";
                    jsonResponse.errors.Add(customError);
                }
            }
            return(jsonResponse);
        }