public ActionResult AddToCart(StoreItem storeItem)
        {
            try
            {
                var sg = new StoreGateway();
                var shoppingCartId = StoreGateway.GetShoppingCartId(HttpContext);
                StoreShoppingCart cart = null;

                if (shoppingCartId == null)
                {
                    cart = sg.CreateNewShoppingCart(storeItem.Store.MerchantId, RDN.Library.Classes.Account.User.GetUserId(), true, Request.UserHostAddress);
                    StoreGateway.SetShoppingCartSession(cart.ShoppingCartId, HttpContext);
                }
                else
                    cart = sg.GetShoppingCart(shoppingCartId.Value);

                int quantity = Convert.ToInt32(Request.Form["quantityToBuy"]);

                sg.AddItemToCart(cart.ShoppingCartId, storeItem.Store.MerchantId, storeItem.StoreItemId, quantity, storeItem.ItemSizeEnum, storeItem.ColorTempSelected);
                this.AddItemToCart(HttpContext.Session, quantity);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Redirect(Url.Content("~/roller-derby-item/" + RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(storeItem.Name) + "/" + storeItem.StoreItemId));

        }
        public ActionResult ViewListing(string name, string id)
        {
            StoreItem item = null;
            try
            {
                StoreGateway sg = new StoreGateway();
                item = sg.GetStoreItem(Convert.ToInt32(id), true);
                item.Note = RDN.Utilities.Strings.StringExt.ConvertTextAreaToHtml(item.Note);
                Dictionary<String, Int32> sizes = new Dictionary<string, int>();
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.X_Small))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.X_Small), Convert.ToInt32(StoreItemShirtSizesEnum.X_Small));
                }
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.Small))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.Small), Convert.ToInt32(StoreItemShirtSizesEnum.Small));
                }
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.Medium))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.Medium), Convert.ToInt32(StoreItemShirtSizesEnum.Medium));
                }
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.Large))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.Large), Convert.ToInt32(StoreItemShirtSizesEnum.Large));
                }
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.X_Large))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.X_Large), Convert.ToInt32(StoreItemShirtSizesEnum.X_Large));
                }
                if (item.ItemSize.HasFlag(StoreItemShirtSizesEnum.XX_Large))
                {
                    sizes.Add(RDN.Utilities.Enums.EnumExt.ToFreindlyName(StoreItemShirtSizesEnum.XX_Large), Convert.ToInt32(StoreItemShirtSizesEnum.XX_Large));
                }
                ViewBag.ItemSizes = new SelectList(sizes, "value", "key");
                ViewBag.Colors = new SelectList(item.Colors, "HexColor", "NameOfColor");
                var shoppingCartId = StoreGateway.GetShoppingCartId(HttpContext);
                if (shoppingCartId != null)
                {
                    StoreShoppingCart cart = sg.GetShoppingCart(shoppingCartId.Value);
                    item.CartItemsCount = cart.ItemsCount;
                }

            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return View(item);
        }
        protected void CartCount(HttpSessionStateBase session)
        {
            if (session["cartCount"] == null || string.IsNullOrEmpty(session["cartCount"].ToString()))
            {
                var shoppingCartId = StoreGateway.GetShoppingCartId(HttpContext);
                if (shoppingCartId != null)
                {
                    var sg = new StoreGateway();
                    var cart = sg.GetShoppingCart(shoppingCartId.Value);
                    if (cart != null)
                    {
                        session["cartCount"] = cart.ItemsCount;
                        return;
                    }
                }
                session["cartCount"] = 0;
            }

        }
        public ActionResult ToggleShipment(string storeItemCartId, string merchantId, string cartId)
        {
            try
            {
                StoreGateway sg = new StoreGateway();
                bool success = sg.ToggleShipment(Convert.ToInt32(storeItemCartId), new Guid(cartId));

                StoreShoppingCart cart = sg.GetShoppingCart(new Guid(cartId));

                var store = cart.Stores.Where(x => x.MerchantId == new Guid(merchantId)).FirstOrDefault();
                decimal itemTotals = 0.0M;
                decimal itemShipping = 0.0M;
                decimal totalAfterShipping = 0.0M;
                decimal price = 0.0M;
                decimal shippingTemp = 0.0M;
                if (store != null)
                {
                    itemTotals = store.TotalPrice;
                    itemShipping = store.TotalShipping;
                    totalAfterShipping = store.TotalAfterShipping;

                    var item = store.StoreItems.Where(x => x.ShoppingCartItemId == Convert.ToInt32(storeItemCartId)).FirstOrDefault();
                    price = item.Price;
                    shippingTemp = item.Shipping;
                }

                return Json(new { IsSuccessful = true, itemPrice = "$" + price.ToString("N2") + " + $" + shippingTemp.ToString("N2"), subtotal = "$" + itemTotals.ToString("N2"), shipping = "$" + itemShipping.ToString("N2"), afterShipping = "$" + totalAfterShipping.ToString("N2") }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            } return Json(new { IsSuccessful = false }, JsonRequestBehavior.AllowGet);

        }
        public ActionResult DeleteItemFromCart(string storeItemCartId, string merchantId, string cartId)
        {
            try
            {
                StoreGateway sg = new StoreGateway();
                bool success = sg.DeleteItemFromCart(Convert.ToInt32(storeItemCartId), new Guid(cartId));

                StoreShoppingCart cart = sg.GetShoppingCart(new Guid(cartId));
                var store = cart.Stores.Where(x => x.MerchantId == new Guid(merchantId)).FirstOrDefault();
                decimal itemTotals = 0.0M;
                decimal itemShipping = 0.0M;
                decimal totalAfterShipping = 0.0M;
                if (store != null)
                {
                    itemTotals = store.TotalPrice;
                    itemShipping = store.TotalShipping;
                    totalAfterShipping = store.TotalAfterShipping;
                }
                this.AddItemToCart(HttpContext.Session, -1);

                return Json(new { IsSuccessful = true, subtotal = "$" + itemTotals.ToString("N2"), shipping = "$" + itemShipping.ToString("N2"), afterShipping = "$" + totalAfterShipping.ToString("N2") }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            } return Json(new { IsSuccessful = false }, JsonRequestBehavior.AllowGet);

        }
        public ActionResult UpdateStoreQuantityItem(string storeCartItemId, string merchantId, string cartId, string quantity)
        {
            try
            {
                StoreGateway sg = new StoreGateway();
                int quan = 0;
                if (!String.IsNullOrEmpty(quantity))
                    quan = Convert.ToInt32(quantity);
                bool success = sg.UpdateStoreQuantityItem(Convert.ToInt32(storeCartItemId), new Guid(cartId), quan);
                StoreShoppingCart cart = sg.GetShoppingCart(new Guid(cartId));

                var store = cart.Stores.Where(x => x.MerchantId == new Guid(merchantId)).FirstOrDefault();
                decimal itemTotals = 0.0M;
                decimal itemShipping = 0.0M;
                decimal totalAfterShipping = 0.0M;
                decimal price = 0.0M;
                if (store != null)
                {
                    itemTotals = store.TotalPrice;
                    itemShipping = store.TotalShipping;
                    totalAfterShipping = store.TotalAfterShipping;

                    price = store.StoreItems.Where(x => x.ShoppingCartItemId == Convert.ToInt32(storeCartItemId)).FirstOrDefault().Price;
                }

                this.AddItemToCart(HttpContext.Session, Convert.ToInt32(quantity));
                return Json(new { IsSuccessful = true, itemPrice = "$" + price.ToString("N2"), subtotal = "$" + itemTotals.ToString("N2"), shipping = "$" + itemShipping.ToString("N2"), afterShipping = "$" + totalAfterShipping.ToString("N2") }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: quantity);
            } return Json(new { IsSuccessful = false }, JsonRequestBehavior.AllowGet);
        }
[RequireHttps] //apply to all actions in controller
#endif
        public ActionResult StoreCart()
        {
            try
            {
                StoreGateway sg = new StoreGateway();

                var shoppingCartId = StoreGateway.GetShoppingCartId(HttpContext);
                if (shoppingCartId != null)
                {
                    StoreShoppingCart cart = sg.GetShoppingCart(shoppingCartId.Value);
                    return View(cart);
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return View(new StoreShoppingCart());
        }
 private StoreShoppingCart GetCartForRdnStore()
 {
     var shoppingCartId = GetShoppingCartId();
     StoreShoppingCart cart = null;
     var sg = new StoreGateway();
     if (shoppingCartId == null)
     {
         cart = sg.CreateNewShoppingCart(null, RDN.Library.Classes.Account.User.GetUserId(), true, Request.UserHostAddress);
         Session.Add("ShoppingCartId", cart.ShoppingCartId);
     }
     else
         cart = sg.GetShoppingCart(shoppingCartId.Value);
     return cart;
 }