public ActionResult GoToOrder() { var currentUser = Session["nguoidung"] as NGUOI_DUNG; NGUOI_DUNG nguoidungHienTai = db.NGUOI_DUNG.FirstOrDefault(x => x.USERNAME == currentUser.USERNAME); HoaDonVM temp = new HoaDonVM(); temp.HO_TEN = nguoidungHienTai.HO_TEN; temp.DIEN_THOAI = 0907625361; temp.DIA_CHI = nguoidungHienTai.DIA_CHI; return(View(temp)); }
public ActionResult Order(HoaDonVM model) { try { //lấy giỏ hàng List <Item> orders = ((GioHang)(Session["cart"])).List; //lấy người dùng NGUOI_DUNG user = (NGUOI_DUNG)(Session["nguoidung"]); //tạo hóa đơn HOA_DON hd = new HOA_DON { USERNAME = user.USERNAME, IDTT = 0, NGAYDATHANG = DateTime.Now.Date, DIA_CHI = model.DIA_CHI, DIEN_THOAI = model.DIEN_THOAI, GHI_CHU = model.GHI_CHU, HO_TEN = model.HO_TEN, TONG_TIEN = 0 }; db.HOA_DON.Add(hd); db.SaveChanges(); //tạo chi tiết hóa đơn long MaHD = hd.MA_HD; long tongtien = 0; foreach (Item item in orders) { CT_HOA_DON cthd = new CT_HOA_DON { MAHD = MaHD, DON_GIA = item.GiaBan, SO_LUONG = item.SoLuongTrongGio, TENSACH = item.TenSach }; tongtien += (item.SoLuongTrongGio * item.GiaBan); db.CT_HOA_DON.Add(cthd); } var updatedHd = db.HOA_DON.FirstOrDefault(x => x.MA_HD == hd.MA_HD); updatedHd.TONG_TIEN = tongtien; db.SaveChanges(); Session.Remove("cart"); return(RedirectToAction("ThankYou", "Cart")); } catch (Exception) { ModelState.AddModelError("", "Đã xảy ra lỗi"); return(View(model)); } }