Ejemplo n.º 1
0
        private CartViewModel CreateViewModelFromCart(Cart cart)
        {
            var viewModel = new CartViewModel();

            viewModel.Id      = cart.Id;
            viewModel.BuyerId = cart.BuyerId;
            viewModel.Items   = cart.Items.Select(i =>
            {
                var itemModel = new CartViewModel.CartItem()
                {
                    Id            = i.Id,
                    UnitPrice     = i.UnitPrice,
                    Quantity      = i.Quantity,
                    CatalogItemId = i.CatalogItemId
                };
                var item              = _context.CatalogItems.Find(i.CatalogItemId);
                itemModel.ImageUrl    = _urlComposer.ComposeImgUrl(item.ImageUrl);
                itemModel.ProductName = item.Name;
                return(itemModel);
            }).ToList();

            return(viewModel);
        }
        public ActionResult Index()
        {
            IList <CartViewModel.ProductCart> productCarts = new List <CartViewModel.ProductCart>();
            CookieHelper cookieHelper = new CookieHelper("Cart");

            if (cookieHelper.GetCookie() != null)
            {
                string json = HttpUtility.UrlDecode(cookieHelper.GetCookie().Values["Cart"]);
                productCarts = JsonConvert.DeserializeObject <List <CartViewModel.ProductCart> >(json);
            }
            IList <PromotionMapping.PromotionCheckProduct> promotions = _promotionRepository.GetActives();

            IList <ProductFrontPageMapping.ProductShowCart> productShowCarts =
                _menuRepository.GetProductShowCartByBarcode(productCarts.Select(o => o.Barcode).ToList());

            IList <CartViewModel.CartItem> cartItems = new List <CartViewModel.CartItem>();
            int         gia       = 0;
            IList <int> productId = new List <int>();

            if (productCarts.Any())
            {
                foreach (var o in productCarts)
                {
                    IList <ProductStockSyncMapping.TonKho> tonKhos = _productStockSyncRepository.GetTonKhoCuaSanPham(o.ProductId);
                    var tonkho = tonKhos.FirstOrDefault(t => t.Barcode.Equals(o.Barcode));
                    var item   = productShowCarts.FirstOrDefault(p => p.Barcode.Equals(o.Barcode));
                    if (item != null)
                    {
                        #region ghép combo < 80K
                        if (item.Price < 80000 && o.Quantity >= 2)
                        {
                            //nếu số lượng > 2  và giá < 80000 bắt đầu tính giá combo
                            int giatru = (item.Price * 10) / 100;
                            gia = item.Price - giatru;
                        }
                        else
                        {
                            gia = item.Price;
                        }
                        #endregion

                        var cartItem = new CartViewModel.CartItem()
                        {
                            ProductId      = o.ProductId,
                            Name           = item.NameProduct,
                            Image          = item.Img,
                            Link           = item.Link,
                            Price          = item.Price,
                            PricePromotion = gia,
                            Barcode        = o.Barcode,
                            Quantity       = o.Quantity <= 0 ? 1 : o.Quantity,
                            AttributeImg   = item.AttributeImg,
                            AttributeName  = item.AttributeName,
                            AttributeFlag  = item.AttributeFlag,
                            Available      = tonkho != null && tonkho.OnHand >= (o.Quantity <= 0 ? 1 : o.Quantity)
                        };

                        #region check promotion
                        if (promotions.Any())
                        {
                            foreach (var pr in promotions)
                            {
                                var promotionDetail = pr.PromotionDetails.FirstOrDefault(dt => dt.ProductId == cartItem.ProductId);
                                if (promotionDetail != null)
                                {
                                    cartItem.HasPromotion   = true;
                                    cartItem.PricePromotion = (int)promotionDetail.PriceDiscount;
                                    cartItem.Discount       = (short)promotionDetail.Percent;
                                    break;
                                }
                            }
                        }
                        #endregion
                        cartItems.Add(cartItem);
                        productId.Add(cartItem.ProductId); // lấy Id để remarketing facebook
                    }
                }
            }
            CartViewModel.CartModel cartModel = new CartViewModel.CartModel()
            {
                CartItems     = cartItems,
                Total         = cartItems.Sum(o => o.Quantity * o.PricePromotion),
                Available     = cartItems.All(o => o.Available),
                ListProductId = productId
            };
            return(View("Index", cartModel));
        }
        public JsonResult AddToCart(CartViewModel.ProductCart model)
        {
            IList <CartViewModel.ProductCart> productCarts = new List <CartViewModel.ProductCart>();

            #region cart
            CookieHelper cookieHelper = new CookieHelper("Cart");

            if (cookieHelper.GetCookie() != null)
            {
                string json = HttpUtility.UrlDecode(cookieHelper.GetCookie().Values["Cart"]);
                productCarts = JsonConvert.DeserializeObject <List <CartViewModel.ProductCart> >(json);
            }
            //neu trung id va ma vach thi cong thêm số lượng, ngược lại thì mới add cookie
            bool exist = productCarts.Any(o => o.ProductId == model.ProductId && o.Barcode.Equals(model.Barcode));
            if (exist)
            {
                foreach (var p in productCarts)
                {
                    if (p.ProductId == model.ProductId && p.Barcode.Equals(model.Barcode))
                    {
                        p.Quantity += model.Quantity;
                        break;
                    }
                }
            }
            else
            {
                productCarts.Add(model);
            }
            string jsonString = JsonConvert.SerializeObject(productCarts);
            cookieHelper.SetCartCookie(jsonString);
            #endregion

            int gia = 0;
            IList <ProductFrontPageMapping.ProductShowCart> productShowCarts =
                _menuRepository.GetProductShowCartByBarcode(productCarts.Select(o => o.Barcode).ToList());
            IList <PromotionMapping.PromotionCheckProduct> promotions = _promotionRepository.GetActives();
            IList <CartViewModel.CartItem> cartItems = new List <CartViewModel.CartItem>();
            if (productCarts.Any())
            {
                foreach (var o in productCarts)
                {
                    var item = productShowCarts.FirstOrDefault(p => p.Barcode.Equals(o.Barcode));
                    #region ghép combo < 80K
                    if (item.Price < 80000 && o.Quantity >= 2)
                    {
                        //nếu số lượng > 2  và giá < 80000 bắt đầu tính giá combo
                        int giatru = (item.Price * 10) / 100;
                        gia = item.Price - giatru;
                    }
                    else
                    {
                        gia = item.Price;
                    }
                    #endregion
                    var cartItem = new CartViewModel.CartItem()
                    {
                        ProductId      = o.ProductId,
                        Name           = item.NameProduct,
                        Image          = item.Img,
                        Price          = item.Price,
                        PricePromotion = gia,
                        Barcode        = o.Barcode,
                        Quantity       = o.Quantity <= 0 ? 1 : o.Quantity,
                        AttributeImg   = item.AttributeImg,
                        AttributeName  = item.AttributeName,
                        AttributeFlag  = item.AttributeFlag,
                        Link           = item.Link
                    };

                    #region check promotion
                    if (promotions.Any())
                    {
                        foreach (var pr in promotions)
                        {
                            var promotionDetail = pr.PromotionDetails.FirstOrDefault(dt => dt.ProductId == cartItem.ProductId);
                            if (promotionDetail != null)
                            {
                                cartItem.HasPromotion   = true;
                                cartItem.PricePromotion = (int)promotionDetail.PriceDiscount;
                                cartItem.Discount       = (short)promotionDetail.Percent;
                                break;
                            }
                        }
                    }

                    #endregion
                    cartItems.Add(cartItem);
                }
            }

            CartViewModel.CartModel cartModel = new CartViewModel.CartModel()
            {
                CartItems = cartItems,
                Total     = cartItems.Sum(o => o.Quantity * o.PricePromotion)
            };

            string html = RenderPartialViewToString("ViewRender", cartModel);

            return(Json(new { html = html, countCart = cartModel.CartItems.Count() }, JsonRequestBehavior.AllowGet));
        }
        public PartialViewResult CartView()
        {
            IList <CartViewModel.ProductCart> productCarts = new List <CartViewModel.ProductCart>();
            CookieHelper cookieHelper = new CookieHelper("Cart");

            if (cookieHelper.GetCookie() != null)
            {
                string json = HttpUtility.UrlDecode(cookieHelper.GetCookie().Values["Cart"]);
                productCarts = JsonConvert.DeserializeObject <List <CartViewModel.ProductCart> >(json);
            }
            IList <PromotionMapping.PromotionCheckProduct>  promotions       = _promotionRepository.GetActives();
            IList <ProductFrontPageMapping.ProductShowCart> productShowCarts =
                _menuRepository.GetProductShowCartByBarcode(productCarts.Select(o => o.Barcode).ToList());

            IList <CartViewModel.CartItem> cartItems = new List <CartViewModel.CartItem>();

            if (productCarts.Any())
            {
                foreach (var o in productCarts)
                {
                    var item = productShowCarts.FirstOrDefault(p => p.Barcode.Equals(o.Barcode));
                    if (item != null)
                    {
                        var cartItem = new CartViewModel.CartItem()
                        {
                            ProductId      = o.ProductId,
                            Name           = item.NameProduct,
                            Image          = item.Img,
                            Price          = item.Price,
                            PricePromotion = item.Price,
                            Barcode        = o.Barcode,
                            Quantity       = o.Quantity <= 0 ? 1 : o.Quantity,
                            AttributeImg   = item.AttributeImg,
                            AttributeName  = item.AttributeName,
                            AttributeFlag  = item.AttributeFlag,
                            Link           = item.Link
                        };

                        #region check promotion
                        if (promotions.Any())
                        {
                            foreach (var pr in promotions)
                            {
                                var promotionDetail = pr.PromotionDetails.FirstOrDefault(dt => dt.ProductId == cartItem.ProductId);
                                if (promotionDetail != null)
                                {
                                    cartItem.HasPromotion   = true;
                                    cartItem.PricePromotion = (int)promotionDetail.PriceDiscount;
                                    cartItem.Discount       = (short)promotionDetail.Percent;
                                    break;
                                }
                            }
                        }

                        #endregion
                        cartItems.Add(cartItem);
                    }
                }
            }

            CartViewModel.CartModel cartModel = new CartViewModel.CartModel()
            {
                CartItems = cartItems,
                Total     = cartItems.Sum(o => o.Quantity * o.PricePromotion)
            };
            return(PartialView("ViewPartial", cartModel));
        }
Ejemplo n.º 5
0
        public ActionResult SaveQuick(CartViewModel.CartForm form)
        {
            if (!ModelState.IsValid)
            {
                Menu menu = _menuRepository.Get(o => o.id_ == form.ProductId);
                return(RedirectToRoute("ChiTietSanPham", new { splink = menu.Link }));
            }
            IList <CartViewModel.ProductCart> productCarts = new List <CartViewModel.ProductCart>();

            #region cart
            CookieHelper cookieHelper = new CookieHelper("Cart");

            if (cookieHelper.GetCookie() != null)
            {
                string json = HttpUtility.UrlDecode(cookieHelper.GetCookie().Values["Cart"]);
                productCarts = JsonConvert.DeserializeObject <List <CartViewModel.ProductCart> >(json);
            }
            //neu trung id va ma vach thi cong thêm số lượng, ngược lại thì mới add cookie
            bool exist = productCarts.Any(o => o.ProductId == form.ProductId && o.Barcode.Equals(form.Barcode));
            if (exist)
            {
                foreach (var p in productCarts)
                {
                    if (p.ProductId == form.ProductId && p.Barcode.Equals(form.Barcode))
                    {
                        p.Quantity += 1;
                        break;
                    }
                }
            }
            else
            {
                productCarts.Add(new CartViewModel.ProductCart()
                {
                    ProductId = form.ProductId,
                    Barcode   = form.Barcode,
                    Quantity  = 1
                });
            }
            string jsonString = JsonConvert.SerializeObject(productCarts);
            cookieHelper.SetCartCookie(jsonString);
            #endregion

            #region items
            // PromotionMapping.PromotionCheck promotion = _promotionRepository.GetCurrentPromotion();
            IList <PromotionMapping.PromotionCheckProduct>  promotions       = _promotionRepository.GetActives();
            IList <ProductFrontPageMapping.ProductShowCart> productShowCarts =
                _menuRepository.GetProductShowCartByBarcode(productCarts.Select(o => o.Barcode).ToList());

            if (!productShowCarts.Any())
            {
                return(RedirectToRoute("Cart"));
            }
            IList <string> ghiChuQT = new List <string>();
            IList <CartViewModel.CartItem> cartItems = new List <CartViewModel.CartItem>();
            if (productCarts.Any())
            {
                foreach (var o in productCarts)
                {
                    var item = productShowCarts.FirstOrDefault(p => p.Barcode.Equals(o.Barcode));
                    if (item == null)
                    {
                        return(RedirectToRoute("Cart"));
                    }

                    #region insert sản phẩm quà tặng

                    var quatang = _khoQuaTangRepository.Get(p => p.IdMenu == o.ProductId);
                    if (quatang != null)
                    {
                        int idQT      = Convert.ToInt32(quatang.IdSanPhamTang);
                        var barcodeqt = _menuOptionRepository.Get(p => p.IdMenu == idQT);

                        var productquatang  = _menuRepository.Get(p => p.id_ == idQT);
                        var cartItemquatang = new CartViewModel.CartItem()
                        {
                            ProductId      = productquatang.id_,
                            Name           = productquatang.NameProduct,
                            Image          = productquatang.Img,
                            Price          = productquatang.PricePro.HasValue ? productquatang.PricePro.Value : 0,
                            PricePromotion = productquatang.PricePro.HasValue ? productquatang.PricePro.Value : 0,
                            Barcode        = barcodeqt != null ? barcodeqt.Barcode : "",
                            Quantity       = 1,
                            AttributeImg   = "",
                            AttributeName  = "",
                            AttributeFlag  = 1,
                            Iquatang       = true
                        };
                        ghiChuQT.Add(productquatang.NameProduct);
                        cartItems.Add(cartItemquatang);
                    }

                    #endregion
                    var cartItem = new CartViewModel.CartItem()
                    {
                        ProductId      = o.ProductId,
                        Name           = item.NameProduct,
                        Image          = item.Img,
                        Price          = item.Price,
                        PricePromotion = item.Price,
                        Barcode        = o.Barcode,
                        Quantity       = o.Quantity,
                        AttributeImg   = item.AttributeImg,
                        AttributeName  = item.AttributeName,
                        AttributeFlag  = item.AttributeFlag
                    };

                    #region check promotion
                    if (promotions.Any())
                    {
                        foreach (var pr in promotions)
                        {
                            var promotionDetail = pr.PromotionDetails.FirstOrDefault(dt => dt.ProductId == cartItem.ProductId);
                            if (promotionDetail != null)
                            {
                                cartItem.HasPromotion   = true;
                                cartItem.PricePromotion = (int)promotionDetail.PriceDiscount;
                                cartItem.Discount       = (short)promotionDetail.Percent;
                                break;
                            }
                        }
                    }

                    #endregion
                    cartItems.Add(cartItem);
                }
            }
            #endregion

            #region Insert DetailMenuComment
            DetailMenuComment order = new DetailMenuComment()
            {
                id_Menu        = 9999,
                idBrand        = form.BrandId,
                Name           = form.Name,
                Link           = form.Phone.Trim().TrimEnd().TrimStart(),
                Code           = "Đặt hàng từ Website",
                Content        = form.Note + (ghiChuQT.Any() ? "<span style=\"color: red;font-weight: bold;\"> - Quà tặng</span> " + string.Join(",", ghiChuQT) : ""),
                HuongDanSuDung = form.Email,
                GiaoHang       = form.Address,
                sDate          = DateTime.Now,
                sDateOk        = DateTime.Now
            };
            _detailMenuCommentRepository.Add(order);

            #endregion

            IList <string> str = new List <string>();

            IList <KHLHProduct> khlhProducts = new List <KHLHProduct>();

            #region Insert DetailMenuCommentItem

            foreach (var dt in cartItems)
            {
                DetailMenuCommentItem detail = new DetailMenuCommentItem()
                {
                    id_Menu = order.id_,
                    Name    = dt.Name,
                    Link    = dt.ProductId + "",
                    Price   = dt.Price + "",
                    PriceOf = dt.PricePromotion + "",
                    Content = string.Format("Mã đơn hàng<#{0}#>", order.id_),
                    Number  = dt.Quantity,
                    BarCode = dt.Barcode,
                    sDate   = DateTime.Now,
                    sDateOk = DateTime.Now,
                    Img     = dt.Image
                };
                _detailMenuCommentItemRepository.Add(detail);

                if (!dt.Iquatang)
                {
                    str.Add(string.Format("{0}({1})", dt.Barcode, dt.Quantity));
                }

                khlhProducts.Add(new KHLHProduct()
                {
                    Code      = detail.BarCode,
                    GiaWeb    = int.Parse(detail.PriceOf),
                    NgayTao   = detail.sDate,
                    SL        = dt.Quantity,
                    LinkImage = "https://beautygarden.vn/Upload/Files/" + dt.Image,
                    Quatang   = dt.Iquatang
                });
            }
            #endregion

            #region insert KH_LH
            string dh = string.Format("{0}#,{1}", order.id_, string.Join(",", str));

            string sql =
                string.Format(
                    "INSERT INTO [bg.hvnet.vn].dbo.KH_LH (NguoiNhap,Nguon,Ten,Phone,DiaChi,GhiChu,idTinh,TrangThai,DonHang,idShop) values (" +
                    " '{0}',{1},N'{2}','{3}',N'{4}',N'{5}',{6},{7},'{8}',{9} " +
                    ")", "system", 1,
                    order.Name,
                    order.Link,
                    order.GiaoHang,
                    //string.Format("Mã đơn hàng<#{0}#>", order.id_),
                    order.Content,
                    1,
                    0,
                    dh,
                    order.idBrand
                    );

            int id = _detailMenuCommentRepository.InsertIntoKHLH(sql, order.Link);


            #endregion

            #region Insert KH_LH_product
            if (khlhProducts.Any() && id != 0)
            {
                foreach (var khlhProduct in khlhProducts)
                {
                    khlhProduct.IdKH = id;
                }
                _detailMenuCommentRepository.InsertKHLHProduct(khlhProducts);
            }

            #endregion

            _unitOfWork.Commit();
            //sau khi thanh toán thành công thì clear cookie
            cookieHelper.ClearCookie();
            //cookieHelperform.ClearCookie();

            CookieHelper cookieInfo = new CookieHelper("User_Infor");

            string jsonStringinfo = JsonConvert.SerializeObject(new LienHeModel()
            {
                GiaoHang       = form.Address,
                HuongDanSuDung = form.Email,
                Link           = form.Phone,
                Name           = form.Name
            });
            cookieInfo.SetUserInforCookie(jsonStringinfo);

            return(RedirectToRoute("OrderSuccess", new { id = order.id_ }));
        }