public ActionResult Submit(FormOrderSalePage model) { // lấy giá và tên sản phẩm từ barcode if (string.IsNullOrEmpty(model.Barcode)) { return(RedirectToAction("Index")); } else { int idsanpham = _menuOptionRepository.Get(o => o.Barcode.Equals(model.Barcode)).IdMenu; Menu sanpham = _menuRepository.Get(o => o.id_ == idsanpham); #region Insert DetailMenuComment DetailMenuComment order = new DetailMenuComment() { id_Menu = 9999, idBrand = 48611, // đẩy hết về Phú Nhuận Name = model.Name, Link = model.Phone.Trim().TrimEnd().TrimStart(), Code = "Đặt hàng từ Website", Content = model.Note, HuongDanSuDung = "", GiaoHang = model.Adress, sDate = DateTime.Now, sDateOk = DateTime.Now }; _detailMenuCommentRepository.Add(order); #endregion IList <string> str = new List <string>(); IList <KHLHProduct> khlhProducts = new List <KHLHProduct>(); DetailMenuCommentItem detail = new DetailMenuCommentItem(); //kiem tra khuyen mai string giakm = ""; PromotionDetail HasSalePage = _promotionDetailRepository.CheckKhuyenMaiSalePage(model.Barcode); if (HasSalePage != null) { // co khuyen mai giakm = HasSalePage.PriceDiscount.ToString(); #region Insert DetailMenuCommentItem // co khuyen mai detail = new DetailMenuCommentItem() { id_Menu = order.id_, Name = sanpham.NameProduct, Link = idsanpham + "", Price = sanpham.PricePro + "", PriceOf = giakm, Content = string.Format("Mã đơn hàng<#{0}#>", order.id_), Number = 1, BarCode = model.Barcode, sDate = DateTime.Now, sDateOk = DateTime.Now, Img = sanpham.Img }; khlhProducts.Add(new KHLHProduct() { Code = detail.BarCode, GiaWeb = int.Parse(detail.PriceOf), NgayTao = detail.sDate, SL = 1, //1 là số lượng sản phẩm đặt hàng LinkImage = "https://beautygarden.vn/Upload/Files/" + sanpham.Img }); } else { // không co khuyen mai detail = new DetailMenuCommentItem() { id_Menu = order.id_, Name = sanpham.NameProduct, Link = idsanpham + "", Price = sanpham.PricePro + "", PriceOf = sanpham.PricePro + "", Content = string.Format("Mã đơn hàng<#{0}#>", order.id_), Number = 1, BarCode = model.Barcode, sDate = DateTime.Now, sDateOk = DateTime.Now, Img = sanpham.Img }; khlhProducts.Add(new KHLHProduct() { Code = detail.BarCode, GiaWeb = int.Parse(detail.PriceOf), NgayTao = detail.sDate, SL = 1, //1 là số lượng sản phẩm đặt hàng LinkImage = "https://beautygarden.vn/Upload/Files/" + sanpham.Img }); } _detailMenuCommentItemRepository.Add(detail); str.Add(string.Format("{0}({1})", model.Barcode, 1)); //1 là số lượng sản phẩm đặt hàng #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, 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(); return(RedirectToAction("DatHangThanhCong", new { barcode = model.Barcode })); } }
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_ })); }