Пример #1
0
 public SessionCart(SHOPPING_CART cart)
 {
     Id = cart.ID;
     ProductAttributeId      = cart.ProdAttrID;
     ProductAttributeVersion = cart.ProductAttributeVersion;
     Quantity  = cart.Quantity;
     DateAdded = cart.DateAdded;
     FullName  = cart.BrandName + " - " + cart.ProductNameWithSize;
     Price     = cart.UnitPrice.Value;
 }
        public void Update(SHOPPING_CART Entity, int PreviousQuantity)
        {
            PRODUCT_ATTRIBUTE prodAttr = _productDAO.GetProductAttributeById(Entity.ProdAttrID);
            Context.PRODUCT_ATTRIBUTE.Detach(prodAttr);

            if (PreviousQuantity != Entity.Quantity)
            {
                int difference = Entity.Quantity - PreviousQuantity;

                // using the version that was initially retrieved from db
                // if another user modified this record an OptimisticConcurrencyException will be raised only if the new quantity is greater than the old
                // so there is the risk of a negative availability

                // if difference is negative, products are being "returned to warehouse" so the 
                // concurrency check is not needed.
                if ((prodAttr.Availability - difference) < 0)
                {
                    //TODO think what to do with version
                    // set the version that was read when the product-attribute was first retrieved from user interface
                    // the entity must be detached in order to set it's concurrency-check attribute (version in this case)
                    prodAttr.Version = Entity.ProductAttributeVersion;
                }
                prodAttr.Availability -= difference;
                _productDAO.UpdateProductAttribute(prodAttr);
            }

            Entity.PRODUCT_ATTRIBUTE = null;
            _shoppingCartDAO.Update(Entity);
            try
            {
                if (prodAttr.Availability < 0)
                {
                    throw new System.Data.OptimisticConcurrencyException("Product availability negative.");
                }
                Context.SaveChanges();
            }
            catch (System.Data.OptimisticConcurrencyException)
            {
                // when an optimistic exception is raised, the old quantity is restored the old quantity
                Entity.Quantity = PreviousQuantity;
                throw;
            }
            finally
            {
                Context.Refresh(RefreshMode.StoreWins, prodAttr);
                Context.Refresh(RefreshMode.StoreWins, Entity);
                Entity.PRODUCT_ATTRIBUTE = prodAttr;

                // setting the new version for further modifications
                Entity.ProductAttributeVersion = prodAttr.Version;
            }
        }
        public void Insert(SHOPPING_CART Entity)
        {
            PRODUCT_ATTRIBUTE prodAttr = _productDAO.GetProductAttributeById(Entity.ProdAttrID);

            Context.PRODUCT_ATTRIBUTE.Detach(prodAttr);

            prodAttr.Availability -= Entity.Quantity;
            if (prodAttr.Availability < 0)
            {
                // partial verification, the exception is raised only if the resulting availability is less than 0
                // using the version that was initially retrieved from db
                // if another user modified this record an OptimisticConcurrencyException will be raised
                prodAttr.Version = Entity.ProductAttributeVersion;
            }

            _productDAO.UpdateProductAttribute(prodAttr);

            _shoppingCartDAO.Insert(Entity);
            try
            {
                if (prodAttr.Availability < 0)
                {
                    throw new System.Data.OptimisticConcurrencyException("Product availability negative.");
                }
                Context.SaveChanges();
            }
            catch (System.Data.OptimisticConcurrencyException ex)
            {
                throw;
            }
            finally
            {
                Context.Refresh(RefreshMode.StoreWins, prodAttr);
                Entity.PRODUCT_ATTRIBUTE = prodAttr;

                // setting the new version for further modifications
                Entity.ProductAttributeVersion = prodAttr.Version;
            }
        }
Пример #4
0
        protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            int selectedQty;
            if (Int32.TryParse(ddl.SelectedValue, out selectedQty))
            {
                RepeaterItem item = (RepeaterItem)ddl.NamingContainer;
                HiddenField field = (HiddenField)item.FindControl("ProdAttrID");
                int prodAttrId;
                if (Int32.TryParse(field.Value, out prodAttrId))
                {
                    SessionCart cart = CartSession.Find(c => c.ProductAttributeId == prodAttrId);
                    if (cart != null)
                    {
                        // we already have the right version in the session variable cartsession
                        int oldQuantity = cart.Quantity;
                        SHOPPING_CART shopping = new SHOPPING_CART() { ID = cart.Id, ProdAttrID = prodAttrId };
                        // session var
                        cart.Quantity = selectedQty;
                        cart.DateAdded = DateTime.Now;

                        shopping.Quantity = selectedQty;
                        shopping.DateAdded = DateTime.Now;
                        shopping.ProductAttributeVersion = cart.ProductAttributeVersion;

                        LinkButton lnk = (LinkButton)item.FindControl("lnkEdit");

                        try
                        {
                            ApplicationContext.Current.Carts.Update(shopping, oldQuantity);
                            lblMessage.Text = String.Empty;
                        }
                        catch (Exception ex)
                        {
                            BasePage.Log(ex, ex.Message, ex.StackTrace, "Cart Control");
                            updAvailListInOptimisticScenario(ddl, prodAttrId, oldQuantity, selectedQty, lnk);
                        }
                        finally
                        {
                            DataBind();
                            ddl.Visible = false;
                            lnk.Visible = true;
                        }
                    }
                }
            }
        }
Пример #5
0
        public List<SHOPPING_CART> Search(SHOPPING_CART Entity, int PageSize, int PageIndex, out int TotalRecords, string OrderExp, Util.SortDirection SortDirection)
        {
            var result = Context.SHOPPING_CART.AsQueryable();

            if (Entity != null)
            {
                if (!String.IsNullOrWhiteSpace(Entity.ProductName))
                {
                    result = result.Where(s => s.PRODUCT_ATTRIBUTE.PRODUCT.Name.Contains(Entity.ProductName));
                }

                if (!String.IsNullOrWhiteSpace(Entity.CampaignName))
                {
                    result = result.Where(s => s.CAMPAIGN.Name.Contains(Entity.CampaignName));
                }

                if (!String.IsNullOrWhiteSpace(Entity.CustomerName))
                {
                    result = result.Where(s => (s.CUSTOMER.Name + " " + s.CUSTOMER.Surname).Contains(Entity.CustomerName));
                }

                if (Entity.SearchStartDate.HasValue)
                {
                    result = result.Where(s => s.DateAdded >= Entity.SearchStartDate.Value);
                }

                if (Entity.CampaignID != 0)
                {
                    result = result.Where(s => s.CampaignID == Entity.CampaignID);
                }

                if (!String.IsNullOrWhiteSpace(Entity.ID))
                {
                    result = result.Where(s => s.ID == Entity.ID);
                }
            }

            TotalRecords = result.Count();

            if (!String.IsNullOrEmpty(OrderExp) && OrderExp.Equals("ProductName"))
            {
                if (SortDirection == SortDirection.Ascending)
                    result = result.OrderBy(s => s.PRODUCT_ATTRIBUTE.PRODUCT.Name);
                else
                    result = result.OrderByDescending(s => s.PRODUCT_ATTRIBUTE.PRODUCT.Name);
            }
            else if (!String.IsNullOrEmpty(OrderExp) && OrderExp.Equals("CustomerName"))
            {
                if (SortDirection == SortDirection.Ascending)
                    result = result.OrderBy(s => s.CUSTOMER.Name).ThenBy(s => s.CUSTOMER.Surname);
                else
                    result = result.OrderByDescending(s => s.CUSTOMER.Name).ThenBy(s => s.CUSTOMER.Surname);
            }
            else if (!String.IsNullOrEmpty(OrderExp) && OrderExp.Equals("CampaignName"))
            {
                if (SortDirection == SortDirection.Ascending)
                    result = result.OrderBy(s => s.CAMPAIGN.Name);
                else
                    result = result.OrderByDescending(s => s.CAMPAIGN.Name);
            }
            else
            {
                GenericSorterCaller<SHOPPING_CART> sorter = new GenericSorterCaller<SHOPPING_CART>();
                result = sorter.Sort(result, String.IsNullOrEmpty(OrderExp) ? "ID" : OrderExp, SortDirection);
            }
            // pagination
            return result.Skip(PageIndex * PageSize).Take(PageSize).ToList();
        }
Пример #6
0
 public void Delete(SHOPPING_CART Entity)
 {
     Delete(Entity, true);
 }
Пример #7
0
 public void Delete(SHOPPING_CART Entity, bool Attach = true)
 {
     if (Attach)
     {
         Context.SHOPPING_CART.Attach(Entity);
     }
     Context.SHOPPING_CART.DeleteObject(Entity);
 }
Пример #8
0
 public void Update(SHOPPING_CART Entity)
 {
     Context.SHOPPING_CART.Attach(Entity);
     var entry = Context.ObjectStateManager.GetObjectStateEntry(Entity);
     entry.SetModifiedProperty("Quantity");
     entry.SetModifiedProperty("DateAdded");
 }
Пример #9
0
        public void Insert(SHOPPING_CART Entity)
        {
            if (String.IsNullOrWhiteSpace(Entity.ID))
            {
                throw new Exception("Entity key null or empty or white space.");
            }

            Context.SHOPPING_CART.AddObject(Entity);
        }
        /// <summary>
        /// Adds a new product to this session cart
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="CampaignID"></param>
        /// <param name="ProductId"></param>
        /// <param name="ProdAttrId"></param>
        /// <param name="Quantity"></param>
        public void AddProductToCart(int CustomerID, int CampaignID, int ProductId, int ProdAttrId, int Quantity, string BrandName)
        {
            if (CartID == null)
            {
                Guid g = Guid.NewGuid();
                UniqueIdGenerator unique = UniqueIdGenerator.GetInstance();
                string cartId = unique.GetBase32UniqueId(g.ToByteArray(), 20).ToLower();
                CartID = cartId;
            }

            SHOPPING_CART cart = new SHOPPING_CART();
            cart.ID = CartID;
            cart.FrontEnd = false;
            cart.CampaignID = CampaignID;
            cart.CustomerID = CustomerID;
            cart.DateAdded = DateTime.Now;
            cart.ProductID = ProductId;
            cart.ProdAttrID = ProdAttrId;
            cart.Quantity = Quantity;
            cart.BrandName = BrandName;

            // the versions list of lists is created each time the product popup is shown, and destroyed each time it is closed
            if (Versions != null)
            {
                cart.ProductAttributeVersion = Versions.Where(x => x.Key == ProductId).FirstOrDefault().Value.Where(y => y.Key == ProdAttrId).FirstOrDefault().Value;
            }
            else
            {
                throw new ApplicationException("Session is compromised! Cannot proceed.");
            }

            if (CartSession == null)
            {
                CartSession = new List<SHOPPING_CART>();
            }

            List<SHOPPING_CART> carts = CartSession;
            SHOPPING_CART sc;

            // already in the cart
            if (carts.Count > 0 && (sc = carts.Where(c => c.ID == cart.ID && c.ProdAttrID == cart.ProdAttrID).FirstOrDefault()) != null)
            {
                cart.Quantity += sc.Quantity;
                ApplicationContext.Current.Carts.Update(cart, sc.Quantity);

                // updating session with last quantity and last prod-attr version
                sc.Quantity = cart.Quantity;
                sc.ProductAttributeVersion = cart.ProductAttributeVersion;
                //sc = cart;
            }
            else
            {
                ApplicationContext.Current.Carts.Insert(cart);

                // already has the last version set from the context saving
                CartSession.Add(cart);
            }

            // refreshing session, optional
            //ApplicationContext.Current.Carts.GetShoppingCartItems(CartID);
            DataBind(CartSession);
        }
Пример #11
0
 public SessionCart(SHOPPING_CART cart)
 {
     Id = cart.ID;
     ProductAttributeId = cart.ProdAttrID;
     ProductAttributeVersion = cart.ProductAttributeVersion;
     Quantity = cart.Quantity;
     DateAdded = cart.DateAdded;
     FullName = cart.BrandName + " - " + cart.ProductNameWithSize;
     Price = cart.UnitPrice.Value;
 }
Пример #12
0
        protected void lnkAddToBasket_Click(object sender, EventArgs e)
        {
            string CartID = String.Empty;

            Guid g = Guid.NewGuid();
            UniqueIdGenerator unique = UniqueIdGenerator.GetInstance();
            string cartId = unique.GetBase32UniqueId(g.ToByteArray(), 20).ToLower();
            if (CartSession == null || CartSession.Count == 0)
            {

                CartID = cartId;
                CartSession = new List<SessionCart>();
            }
            else
            {
                List<SessionCart> cSession = CartSession.OrderByDescending(c => c.DateAdded).ToList();
                SessionCart sessionCart = cSession.First();
                if (sessionCart.DateAdded.AddMinutes(Configuration.CartExpirationValue) < DateTime.Now)
                {
                    RefreshCart();
                    CartID = cartId;
                }
                else
                {
                    CartID = CartSession.First().Id;
                }
            }

            SHOPPING_CART cart = new SHOPPING_CART();
            cart.ID = CartID;
            cart.FrontEnd = true;
            cart.CampaignID = CampaignID;
            cart.CustomerID = CurrentCustomer.Id;
            cart.DateAdded = DateTime.Now;
            cart.ProductID = ProductID;
            int num = 0;

            if (!Int32.TryParse(ddlSize.SelectedValue, out num))
            {
                return;
            }
            cart.ProdAttrID = num;

            num = 0;
            if (!Int32.TryParse(ddlQuantity.SelectedValue, out num) || num == 0)
            {
                return;
            }
            cart.Quantity = num;

            // the versions list of lists is created each time the product popup is shown, and destroyed each time it is closed
            if (Version != null)
            {
                cart.ProductAttributeVersion = Version;
            }
            else
            {
                throw new ApplicationException("Session is compromised! Cannot proceed.");
            }

            SessionCart sC;
            try
            {
                // already in the cart
                if (CartSession != null && CartSession.Count > 0 && (sC = CartSession.Find(c => c.ProductAttributeId == cart.ProdAttrID)) != null)
                {
                    // sum with old quantity
                    cart.Quantity += sC.Quantity;
                    ApplicationContext.Current.Carts.Update(cart, sC.Quantity);

                    // updating session with last quantity and last prod-attr version
                    sC.Quantity = cart.Quantity;
                    sC.ProductAttributeVersion = cart.ProductAttributeVersion;
                }
                else
                {
                    ApplicationContext.Current.Carts.Insert(cart);
                    sC = new SessionCart(cart);
                    CartSession.Add(sC);
                }
                TotalAmount = ApplicationContext.Current.Carts.GetShoppingCartTotalAmount(CartID);
            }
            catch (Exception ex)
            {
                //TODO log error
                Log(ex, ex.Message, ex.StackTrace, "Product.AddToCart");

                List<int> qtyList;
                PRODUCT_ATTRIBUTE prodAttr = ApplicationContext.Current.Products.GetProductAvailability(cart.ProdAttrID, out qtyList);

                Version = prodAttr.Version;

                ddlQuantity.DataSource = qtyList;
                ddlQuantity.DataBind();
                if (!qtyList.Contains(cart.Quantity))
                {
                    lblMessage.Text = Resources.Lang.InsufficientAvailabilityMessage;
                }
                if (qtyList.Count == 0)
                {
                    ddlQuantity.Enabled = false;
                    lnkAddToBasket.Enabled = false;
                    loadProductAttributes();
                }
                //refreshing the size ddl
                loadProductAttributes();
                updPanelDDL.Update();
                return;
            }

            Version = null;
            Response.Redirect("/cart/mycart/");
        }
 public List<SHOPPING_CART> Search(SHOPPING_CART Entity, int PageSize, int PageIndex, out int TotalRecords, string OrderExp, Util.SortDirection SortDirection)
 {
     return _shoppingCartDAO.Search(Entity, PageSize, PageIndex, out TotalRecords, OrderExp, SortDirection);
 }
        public void Delete(SHOPPING_CART Entity)
        {
            // first "put back" the product that was in cart
            // version is not needed as the availability is incremented, so no risk
            PRODUCT_ATTRIBUTE prodAttr = _productDAO.GetProductAttributeById(Entity.ProdAttrID);
            prodAttr.Availability += Entity.Quantity;
            _productDAO.UpdateProductAttribute(prodAttr, false);

            // second remove item from cart
            _shoppingCartDAO.Delete(Entity, false);

            // then save if everything was successful
            Context.SaveChanges();
        }
 public void Update(SHOPPING_CART Entity)
 {
     Update(Entity, Entity.Quantity);
 }