public ActionResult Request_Password_Change(String session)
 {
     Debug.WriteLine("Request change password from session " + session);
     KhachHangController ctr = new KhachHangController();
     KhachHang obj = ctr.timkiem("","","",null,0,0,"","","",session).FirstOrDefault();
     if(obj!=null)
     {
         ViewBag.khachhang_264 = obj;
         return View();
     }
     else
     {
         return RedirectToAction("Index","FrontHome");
     }
 }
 public ActionResult Request_Password_Change_Submit()
 {
     int obj_id = TextLibrary.ToInt(Request["khachhang_id"]);
     String session = TextLibrary.ToString(Request["khachhang_session"]);
     String new_pass = TextLibrary.ToString(Request["khachhang_matkhau"]);
     KhachHangController ctr = new KhachHangController();
     if (ctr.set_password_by_session(obj_id, session, new_pass))
     {
         ViewBag.Message = "Mật khẩu đã được khôi phục lại.";
     }
     else
     {
         ViewBag.Message = "Oops. Are you trying to hack my system ?";
     }
     return View();
 }
 public ActionResult Index(int page=1)
 {
     //check
     if (!this._nhanvien_permission.Contains("khachhang_view"))
     {
         return this._fail_permission("khachhang_view");
     }
     KhachHangController ctr = new KhachHangController();
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController();
     List<LoaiKhachHang> lkh_list = ctr_lkh.timkiem(timkiem_khachhang["loaikhachhang_id"]);
     //pagination
         int max_item_per_page = TextLibrary.ToInt(this.timkiem_khachhang["max_item_per_page"]);//get from setting
         Pagination pg = new Pagination();
         pg.set_current_page(page);
         pg.set_max_item_per_page(max_item_per_page);
         pg.set_total_item(
             ctr.timkiem_count(
                 timkiem_khachhang["id"],
                 timkiem_khachhang["tendangnhap"],
                 timkiem_khachhang["tendaydu"],
                 lkh_list,
                 TextLibrary.ToInt(timkiem_khachhang["diem_from"]),
                 TextLibrary.ToInt(timkiem_khachhang["diem_to"]),
                 "","","","",
                 timkiem_khachhang["active"]
                 )
             );
         pg.update();
     //Chọn danh sách nhân viên để hiển thị theo cookies tìm kiếm
     ViewBag.KhachHang_List = ctr.timkiem(
             timkiem_khachhang["id"],
             timkiem_khachhang["tendangnhap"],
             timkiem_khachhang["tendaydu"],
             lkh_list,
             TextLibrary.ToInt(timkiem_khachhang["diem_from"]),
             TextLibrary.ToInt(timkiem_khachhang["diem_to"]),
             "", "", "", "",
             timkiem_khachhang["active"], "id", true, pg.start_point, pg.max_item_per_page
         );
     //set search cookies
     ViewBag.timkiem_khachhang = this.timkiem_khachhang;
     ViewBag.Title += " - Quản lý";
     ViewBag.pagination = pg;
     ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
     return View();
 }
 public ActionResult Index(int id = 0)
 {
     if (!this._nhanvien_permission.Contains("khachhang_view"))
     {
         return _fail_permission("khachhang_view");
     }
     //get controller
     KhachHangController ctr = new KhachHangController();
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController();
     //kiểm tra tồn tại
         if (!ctr.is_exist(id)) return RedirectToAction("Index", "AdminKhachHangs");
     //get info
         ViewBag.KhachHang = ctr.get_by_id(id);
         ViewBag.Title += " - Xem chi tiết";
         ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
         return View();
 }
        public ActionResult Submit()
        {
            KhachHangController ctr = new KhachHangController();
            List<string> validate = new List<string>();
            //get post value
            int id = TextLibrary.ToInt(Request["khachhang_id"]);
            string tendaydu = TextLibrary.ToString(Request["khachhang_tendaydu"]);
            string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
            string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
            string diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
            string sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
            string email = TextLibrary.ToString(Request["khachhang_email"]);
            //load obj first
            KhachHang obj = ctr.get_by_id(id);
            if (obj == null)
            {
                return RedirectToAction("Index", "FrontKhachHang");
            }
            //pass to obj
            obj.diachi = diachi;
            obj.email = email;
            obj.sdt = sdt;
            obj.tendaydu = tendaydu;
            //validate
            validate.AddRange(ctr.validate(obj,matkhau,matkhau2));
            //update
            if (validate.Count == 0)
            {
                //call update for properties first
                ctr._db.SaveChanges();
                //call set password
                ctr.set_password(obj.id, matkhau2);
                obj = ctr.get_by_id(obj.id);
                this._khachhang = obj;
                //Session["khachhang"] = this._khachhang;//Không cần thiết vì sẽ refresh lại theo id lúc gọi action mới
                validate.Add("edit_ok");
            }

            ViewBag.khachhang = this._khachhang;
            //report back
            ViewBag.State = validate;
            return View("Index");
        }
 public ActionResult Submit()
 {
     //generate session for KH
     KhachHangController ctr=new KhachHangController();
     String email = TextLibrary.ToString(Request["khachhang_email"]);
     String session = "";
     Boolean valid_session = ctr.generate_forgot_password_session(email, out session);
     if (valid_session && ValidateLibrary.is_valid_email(email))
     {
         GMailLibrary gmail = new GMailLibrary();
         gmail.receive_email = email;
         gmail.Generate_ForgotPassword_Html(Url.Action("Request_Password_Change", "FrontForgotPassword", new { session = session }, this.Request.Url.Scheme));
         gmail.Send();
         ViewBag.Message = "Đường link khôi phục mật khẩu đã được gửi vào email.";
     }
     else
     {
         ViewBag.Message = "Không tìm thấy khách hàng nào có email đó cả.";
     }
     return View("Request_Password_Change_Submit");
 }
 public ActionResult Test_Login()
 {
     //get data from client
     KhachHangController ctr = new KhachHangController();
     String username = TextLibrary.ToString(Request["khachhang_username"]);
     String password = TextLibrary.ToString(Request["khachhang_password"]);
     Boolean remember = TextLibrary.ToBoolean(Request["khachhang_remember"]);
     //validate
     if (ctr.login(username, password))
     {
         Debug.WriteLine("Đăng nhập thành công");
         KhachHang obj = ctr.get_by_username(username);
         if (remember)
         {
             //set Cookies
             HttpCookie _tmp = new HttpCookie("khachhang");
             _tmp["khachhang_id"] = obj.id.ToString();
             _tmp["khachhang_password"] = obj.matkhau;
             _tmp.Expires = DateTime.Now.AddDays(1);
             Response.Cookies.Add(_tmp);
         }
         else
         {
             //set session
             Session["khachhang"] = obj;
         }
         //nếu được dẫn link từ FrontCart.CheckOut thì quay lại checkOut
         if (Session["link_after_login"] != null)
         {
             string url_to = (string)Session["link_after_login"];
             Session["link_after_login"] = null;
             return Redirect(url_to);
         }
         //redirect
         return RedirectToAction("Index", "FrontHome");
     }
     Debug.WriteLine("Đăng nhập thất bại");
     //load view
     return RedirectToAction("Index","FrontLogin");
 }
        public ActionResult Delete(int id = 0)
        {
            //check
            if (!this._nhanvien_permission.Contains("khachhang_delete"))
            {
                return this._fail_permission("khachhang_delete");
            }

            KhachHangController controller = new KhachHangController();
            if (!controller.is_exist(id))
            {
                return RedirectToAction("Index", "AdminKhachHangs");
            }
            try
            {
                controller.delete(id);
            }
            catch (Exception)
            {
                return _show_notification("Khách hàng này có dính khóa ngoại với đơn hàng hiện có nên không xóa được");
            }
            return RedirectToAction("Index", "AdminKhachHangs");
        }
 public ActionResult Submit()
 {
     //get obj id first
     int obj_id = TextLibrary.ToInt(Request["khachhang_id"]);
     KhachHangController ctr = new KhachHangController();
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController(ctr._db);
     //khoi tao moi doi tuong
     KhachHang obj;
     Boolean edit_mode = true;
     if (obj_id == 0)
     {
         //add mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_add"))
         {
             return this._fail_permission("khachhang_add");
         }
         obj = new KhachHang();
         edit_mode = false;
     }
     else
     {
         //update mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_edit"))
         {
             return _fail_permission("khachhang_edit");
         }
         //kiem tra ton tai
         if (ctr.is_exist(obj_id))
         {
             //get obj
             obj = ctr.get_by_id(obj_id);
         }
         else
         {
             //id khong ton tai
             return RedirectToAction("Index", "AdminKhachHangs");
         }
     }
     //assign data
     List<string> validate = new List<string>();
     string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
     string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
     obj.email = TextLibrary.ToString(Request["khachhang_email"]);
     obj.sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
     obj.diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
     obj.tendangnhap =  TextLibrary.ToString(Request["khachhang_tendangnhap"]);
     obj.tendaydu =  TextLibrary.ToString(Request["khachhang_tendaydu"]);
     obj.bad = TextLibrary.ToBoolean(Request["khachhang_bad"]);
     obj.active = TextLibrary.ToBoolean(Request["khachhang_active"]);
     //validate
     validate.AddRange(ctr.validate(obj,matkhau,matkhau2));
     //action
     if (validate.Count==0)
     {
         if (edit_mode)
         {
             //call update for properties
             ctr._db.SaveChanges();
             //call set password
             ctr.set_password(obj.id,matkhau2);
             validate.Add("edit_ok");
         }
         else
         {
             //add mode mới có chuyện set loại KH
                 obj.loaikhachhang = ctr_lkh.get_by_id(
                 TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
                 );
                 obj.diem = obj.loaikhachhang.mucdiem;
             //call update loaikh first
             obj._Update_LoaiKhachHang(ctr._db);
             //set raw password
             obj.matkhau = matkhau2;
             //call add
             int maxid = ctr.add(obj);
             //re assign id
             obj.id = maxid;
             validate.Add("add_ok");
         }
     }
     obj.loaikhachhang = ctr_lkh.get_by_id(
         TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
     );
     ViewBag.KhachHang = obj;
     ViewBag.Title += " - Submit";
     ViewBag.State = validate;
     ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
     ViewBag.add_mode = obj_id==0?"1":"0";
     return View("Index");
 }
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     KhachHangController ctr_kh = new KhachHangController();
     NhomSanPhamController ctr = new NhomSanPhamController();
     List<NhomSanPham2> list1 = ctr.timkiem("", "", "", "1");
     SanPhamController ctr2 = new SanPhamController(ctr._db);
     List<SanPham>list2=ctr2.get_bestseller(4);
     if (list1 != null && list2 != null)
     {
         ViewBag.NhomSanPham2_List_All = list1;
         ViewBag.SanPham_BestSeller = list2;
     }
     else
     {
         ViewBag.NhomSanPham2_List_All = new List<NhomSanPham2>();
         ViewBag.SanPham_BestSeller = new List<SanPham>();
     }
     //tim kiem
     //build timkiem_sanpham
     if (Request.Cookies.Get("front_timkiem_sanpham") == null)
     {
         //chưa set cookies trước => tiến hành set cookies
         this._khoitao_cookie();
         this._luu_cookie();
     }
     else
     {
         try
         {
             this.front_timkiem_sanpham = CookieLibrary.Base64Decode(Request.Cookies.Get("front_timkiem_sanpham"));
         }
         catch (Exception)
         {
             this._khoitao_cookie();
             this._luu_cookie();
         }
     }
     ViewBag.front_timkiem_sanpham = this.front_timkiem_sanpham;
     //Load thong tin KhachHang
     if (!this._is_logged_in())
     {
         //Chưa có ai đăng nhập hệ thống
         if (Session["khachhang"] != null)
         {
             //nếu như KH đã đăng nhập rồi
             this._khachhang = ctr_kh.get_by_id(((KhachHang)Session["khachhang"]).id);
         }
         else
         {
             //lấy từ cookies lên
             //lay thong tin tu cookies
             HttpCookie _tmp = Request.Cookies.Get("khachhang");
             if (_tmp != null)
             {
                 int uid = TextLibrary.ToInt(_tmp["khachhang_id"].ToString());
                 string password = TextLibrary.ToString(_tmp["khachhang_password"].ToString());
                 //lay thong tin user theo yeu cau dang nhap
                 this._khachhang = ctr_kh.get_by_id_hash_password(uid, password);
             }
         }
     }
     //
     //
     ViewBag.nhanvien = this._nhanvien;
     ViewBag.khachhang = this._khachhang;
     //get cart
         if (Session["giohang"] != null)
         {
             try
             {
                 this._giohang = (DonHang)Session["giohang"];
             }
             catch (Exception)
             {
                 this._giohang = new DonHang();
             }
         }
         else
         {
             this._giohang = new DonHang();
         }
         //gán khach hang va nhan vien
         this._giohang.khachhang = this._khachhang;
         this._giohang.khachhang_nhanvien = this._nhanvien;
         //save cart
         this._save_cart_to_session();
     ViewBag.giohang = this._giohang;
 }
        public ActionResult Submit()
        {
            KhachHangController ctr = new KhachHangController();
            //get post value
            string tendangnhap = TextLibrary.ToString(Request["khachhang_tendangnhap"]);
            string tendaydu = TextLibrary.ToString(Request["khachhang_tendaydu"]);
            string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
            string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
            string diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
            string sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
            string email = TextLibrary.ToString(Request["khachhang_email"]);
            string captcha = TextLibrary.ToString(Request["khachhang_captcha"]);
            //pass to obj
            KhachHang obj = new KhachHang();
            obj.diachi = diachi;
            obj.email = email;
            obj.matkhau = matkhau;
            obj.sdt = sdt;
            obj.tendangnhap = tendangnhap;
            obj.tendaydu = tendaydu;

            //validate
            List<string> validate = new List<string>();
            //xét captcha trước
            if (!this.get_captcha_string().ToLower().Equals(captcha.ToLower()))
            {
                validate.Add("captcha_fail");
            }
            //validate obj
            validate.AddRange(ctr.validate(obj, matkhau, matkhau2));
            //check
            if (validate.Count == 0)
            {
                //call update loaikh first
                obj._Update_LoaiKhachHang(ctr._db);
                //tiến hành thêm và gán session auto đăng nhập
                int max_id = ctr.add(obj);
                obj.id = max_id;
                //save to session
                Session["khachhang"] = ctr.get_by_id(max_id);
                //đăng ký thành công
                //nếu được dẫn link từ FrontCart.CheckOut thì quay lại checkOut
                if (Session["link_after_login"] != null)
                {
                    string url_to = (string)Session["link_after_login"];
                    Session["link_after_login"] = null;
                    return Redirect(url_to);
                }
                return RedirectToAction("Index", "FrontHome");
            }
            //add and redirect or return error

               //set tmp validate
            ViewBag.State = validate;
            ViewBag.khachhang_register = obj;
               return View("Index");
        }