コード例 #1
0
        public JsonResult DatHang(string PhanHoi)
        {
            List <GioHang> listGioHang = LayGioHang();
            //Thêm đơn hàng
            Table_DONHANG   ddh = new Table_DONHANG();
            Table_KHACHHANG kh  = (Table_KHACHHANG)Session["User"];
            Table_NHANVIEN  nv  = (Table_NHANVIEN)Session["User1"];

            List <GioHang> gh = LayGioHang();

            if (nv != null)
            {
                ddh.ID_NGUOIBAN = nv.ID_NHANVIEN;
            }

            ddh.ID_KHACHHANG = kh.ID_KHACHHANG;
            ddh.NgayGiaoDich = DateTime.Now;
            ddh.TONGTIEN     = TongTien();
            ddh.Damua        = false;
            ddh.TinhTrang    = true;
            ddh.PhanHoi      = PhanHoi;
            db.Table_DONHANGs.InsertOnSubmit(ddh);
            db.SubmitChanges();
            //Thêm chi tiết đơn hàng


            foreach (var item in gh)
            {
                Table_DONHANG_DM ctdh = new Table_DONHANG_DM();
                ctdh.ID_DONHANG = ddh.ID_DONHANG;
                ctdh.TuyChon    = item.TuyChon;
                ctdh.ID_SANPHAM = item.sID_SANPHAM;
                ctdh.SoLuong    = item.sSoLuong;
                ctdh.Giatien    = item.sDonGia;
                ctdh.TinhTrang  = false;
                ctdh.GHICHU     = item.GHICHU;
                db.Table_DONHANG_DMs.InsertOnSubmit(ctdh);
                var upslSP = db.Table_TUYCHONs.First(m => m.ID_TUYCHON == item.sID_TUYCHON);
                if (upslSP.SoLuong > 0 && upslSP.SoLuong < item.sSoLuong)
                {
                    ViewBag.Error = "Sản phẩm hiện chỉ còn " + upslSP.SoLuong.ToString();
                    break;
                }
                else if (upslSP.SoLuong == 0)
                {
                    ViewBag.Error = "Xin lỗi sản phẩm hiện hết hàng";
                    break;
                }
                else
                {
                    upslSP.SoLuong = upslSP.SoLuong - item.sSoLuong;
                    UpdateModel(upslSP);
                    db.SubmitChanges();
                }
            }
            Session["GioHang"] = null;


            return(Json(new
            {
            }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public ActionResult CheckLogin(FormCollection collection)
        {
            Table_KHACHHANG kh = new Table_KHACHHANG();
            Table_NHANVIEN  nv = (Table_NHANVIEN)Session["User1"];

            var    sdt    = collection["SODT"];
            string Sodt   = sdt;
            var    hoten  = collection["HOTEN"];
            var    diachi = collection["DIACHI"];

            if (Session["User1"] != null)
            {
                var dchi = collection["DIACHI"];
                if (String.IsNullOrEmpty(sdt))
                {
                    ViewData["Loi1"] = "*";
                    ViewBag.Mess     = string.Format("Nhập sô điện thoại");
                }
                else if (String.IsNullOrEmpty(hoten))
                {
                    ViewData["Loi2"] = "*";
                    ViewBag.Mess     = "Vui lòng nhập họ & tên";
                }
                else if (String.IsNullOrEmpty(dchi))
                {
                    ViewData["Loi3"] = "*";
                    ViewBag.Mess     = "Vui lòng nhập địa chỉ";
                }
                else
                {
                    kh.HoTen         = hoten;
                    kh.SDT           = sdt;
                    kh.DiaChi        = diachi;
                    kh.ID_GIOITHIEU  = nv.ID_NHANVIEN;
                    kh.NgayGioiThieu = DateTime.Now;
                    bool check = db.Table_KHACHHANGs.Any(m => m.SDT == sdt && m.ID_GIOITHIEU == nv.ID_NHANVIEN);
                    if (!check)
                    {
                        db.Table_KHACHHANGs.InsertOnSubmit(kh);
                        db.SubmitChanges();
                        Session["User"] = kh;
                    }
                    else
                    {
                        Table_KHACHHANG upKH = db.Table_KHACHHANGs.First(m => m.SDT == sdt && m.ID_GIOITHIEU == nv.ID_NHANVIEN);
                        upKH.HoTen         = hoten;
                        upKH.SDT           = sdt;
                        upKH.DiaChi        = diachi;
                        upKH.NgayGioiThieu = DateTime.Now;
                        UpdateModel(upKH);
                        db.SubmitChanges();
                        Session["User"] = upKH;
                    }


                    return(RedirectToAction("DatHang", "GioHang"));
                }
            }
            else
            {
                ViewBag.sdt    = sdt;
                ViewBag.hoten  = hoten;
                ViewBag.diachi = diachi;
                if (String.IsNullOrEmpty(sdt))
                {
                    ViewBag.Mess = string.Format("Nhập số điện thoại");
                }
                else if (String.IsNullOrEmpty(hoten))
                {
                    ViewBag.Mess = "Vui lòng nhập họ & tên";
                }
                else if (String.IsNullOrEmpty(diachi))
                {
                    ViewBag.Mess = "Vui lòng nhập địa chỉ";
                }
                else
                {
                    HttpCookie Phone_UserCookie   = new HttpCookie("phone_user", (sdt).ToString());
                    HttpCookie Name_UserCookie    = new HttpCookie("name_user", (hoten).ToString());
                    HttpCookie Address_UserCookie = new HttpCookie("address_user", (diachi).ToString());
                    Phone_UserCookie.HttpOnly   = true;
                    Name_UserCookie.HttpOnly    = true;
                    Address_UserCookie.HttpOnly = true;
                    Phone_UserCookie.Expires    = DateTime.Now.AddDays(365);
                    Name_UserCookie.Expires     = DateTime.Now.AddDays(365);
                    Address_UserCookie.Expires  = DateTime.Now.AddDays(365);
                    HttpContext.Response.SetCookie(Phone_UserCookie);
                    HttpContext.Response.SetCookie(Name_UserCookie);
                    HttpContext.Response.SetCookie(Address_UserCookie);

                    kh.HoTen  = hoten;
                    kh.SDT    = sdt;
                    kh.DiaChi = diachi;
                    bool check = db.Table_KHACHHANGs.Any(m => m.SDT == sdt);
                    if (!check)
                    {
                        db.Table_KHACHHANGs.InsertOnSubmit(kh);
                        db.SubmitChanges();
                        Session["User"] = kh;
                    }
                    else
                    {
                        Table_KHACHHANG upKH = db.Table_KHACHHANGs.First(m => m.SDT == sdt);
                        upKH.HoTen  = hoten;
                        upKH.SDT    = sdt;
                        upKH.DiaChi = diachi;
                        UpdateModel(upKH);
                        db.SubmitChanges();
                        Session["User"] = upKH;
                    }


                    return(RedirectToAction("DatHang", "GioHang"));
                }
            }
            return(View());
        }