public ActionResult DatHang(FormCollection collection) { //them don hang DON_HANG ddh = new DON_HANG(); KHACHHANG kh = (KHACHHANG)Session["Taikhoan"]; List <Giohang> gh = Laygiohang(); ddh.MaKH = kh.MaKH; ddh.NgayDat = DateTime.Now; var ngaygiao = String.Format("{0:MM/dd/yyyy}", collection["Ngaygiao"]); ddh.Ngaygiao = DateTime.Parse(ngaygiao); ddh.Tinhtranggiaohang = false; ddh.Dathanhtoan = false; data.DON_HANGs.InsertOnSubmit(ddh); data.SubmitChanges(); //Them chi tiet don hang foreach (var item in gh) { CT_DONHANG ctdh = new CT_DONHANG(); ctdh.MaDH = ddh.MaDH; ctdh.MaGame = ddh.MaGame; ctdh.SoLuong = item.iSoluong; ctdh.Dongia = (decimal)item.dDongia; data.CT_DONHANGs.InsertOnSubmit(ctdh); } data.SubmitChanges(); Session["Giohang"] = null; return(RedirectToAction("Xacnhandonhang", "Giohang")); }
public ActionResult DatHangSubmit(string NgayGiao, string MaKM) { try { DONHANG dh = new DONHANG(); KHUYENMAI km = db.KHUYENMAIs.Find(MaKM); if (Session["TaiKhoan"] != null || Session["TaiKhoan"].ToString() == "") { KHACHHANG customer = (KHACHHANG)Session["TaiKhoan"]; if (ModelState.IsValid) { dh.MaKH = customer.MaKH; dh.NgayMuaHang = DateTime.Now; if (NgayGiao.Trim() != "") { dh.NgayGiao = Convert.ToDateTime(NgayGiao); } dh.TrangThaiGiao = 0; dh.TrangThaiThanhToan = "Chưa thanh toán"; if (!string.IsNullOrEmpty(MaKM)) { dh.MaKM = MaKM; dh.KHUYENMAI = db.KHUYENMAIs.Find(MaKM); } db.DONHANGs.Add(dh); db.SaveChanges(); if (!string.IsNullOrEmpty(MaKM)) { foreach (var item in db.KHUYENMAIs) { if (item.MaKM.ToLower() == MaKM.ToLower()) { item.SoLanConLai--; break; } } } db.SaveChanges(); } } if (Session["ShoppingCart"] != null) { List <CartItem> ls = (List <CartItem>)Session["ShoppingCart"]; foreach (CartItem item in ls) { CT_DONHANG CTDH = new CT_DONHANG(); CTDH.MaDH = dh.MaDH; CTDH.MaSach = item.productOrder.MaSach; CTDH.SoLuong = item.Quality; if (!string.IsNullOrEmpty(MaKM)) { CTDH.DonGia = item.productOrder.GiaTien - (item.productOrder.GiaTien * (km.GiaTriKM / 100)); } else { CTDH.DonGia = item.productOrder.GiaTien; } db.CT_DONHANG.Add(CTDH); DAUSACH sach = db.DAUSACHes.Find(item.productOrder.MaSach); sach.SoLuongTon -= item.Quality; db.SaveChanges(); } } Session["ShoppingCart"] = null; } catch (Exception) { return(Json(new { success = false, msg = "Đặt hàng thất bại, vui lòng thử lại!" })); } return(Json(new { success = true, msg = "Đặt hàng thành công!" })); }