public ActionResult ThanhCong() { try { //var dh = ctx.QC_DonHang.OrderByDescending(x => x.id).FirstOrDefault(); //TempData["donhang"] = dh; if (TempData["donhang"] != null) { QC_DonHang data = (QC_DonHang)TempData["donhang"]; var dh = ctx.QC_DonHang.Where(x => x.id == data.id).FirstOrDefault(); string ten_mgg = ""; if (dh.idmagiamgia != null && dh.idmagiamgia != 0) { var mgg = ctx.QC_MaGiamGia.Find(dh.idmagiamgia); ten_mgg = mgg == null ? "" : mgg.tenma; } ViewBag.MGG = ten_mgg; return(View(dh)); } else { return(RedirectToAction("", "Home")); } } catch (Exception ex) { SetAlert("Lỗi" + ex.Message.ToString(), AlertType.Error); Logs.WriteLog(ex); return(View()); } }
public JsonResult ThanhToan(string magiamgia, string ghichu, int ptthanhtoan, decimal sotiengiam, string madonhang, string diachigiaohang) { try { string username = HttpContext.User.Identity.Name; var kh = ctx.QC_KhachHang.Where(x => x.tendangnhap == username).FirstOrDefault(); var mgg = ctx.QC_MaGiamGia.Where(x => x.tenma.ToLower().Trim() == magiamgia.ToLower().Trim() && x.kichhoat == true).FirstOrDefault(); List <ShoppingCart> lCart = new List <ShoppingCart>(); if (Session["cart"] != null && kh != null) { lCart = (List <ShoppingCart>)Session["cart"]; QC_DonHang dh = new QC_DonHang(); dh.sotiengiam = 0; if (mgg != null) { dh.idmagiamgia = mgg.id; dh.sotiengiam = sotiengiam; } dh.madonhang = madonhang; dh.ngaydathang = DateTime.Now; dh.idkhachhang = kh.id; dh.ghichu = ghichu; dh.trangthai = false; dh.diachinhanhang = diachigiaohang; dh.idphuongthucthanhtoan = ptthanhtoan; ctx.QC_DonHang.Add(dh); ctx.SaveChanges(); decimal tongthanhtien = 0; if (dh.id > 0) { foreach (var item in lCart) { QC_Thuoc_DonHang t_dh = new QC_Thuoc_DonHang(); t_dh.iddonhang = dh.id; t_dh.idthuoc = item.idsanpham; t_dh.giatien = item.giatien; t_dh.soluong = item.soluong; t_dh.thanhtien = item.soluong * item.giatien; ctx.QC_Thuoc_DonHang.Add(t_dh); tongthanhtien += item.soluong * item.giatien; } ctx.SaveChanges(); } dh.thanhtientruocgiam = tongthanhtien; dh.thanhtiensaugiam = tongthanhtien - dh.sotiengiam; ctx.Entry(dh).State = EntityState.Modified; ctx.SaveChanges(); sendEmail("quản trị viên", HttpContext.Application["email"].ToString(), dh.madonhang, dh.QC_KhachHang.tendaydu); TempData["donhang"] = dh; Session["cart"] = null; return(Json(new { status = true }, JsonRequestBehavior.AllowGet)); } return(Json(new { status = false, message = "Có lỗi xảy ra!" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { Logs.WriteLog(ex); return(Json(new { status = false, message = "Lỗi: " + ex }, JsonRequestBehavior.AllowGet)); } }