//WORK OK
 public ActionResult Add()
 {
     if (!this._nhanvien_permission.Contains("sanpham_add"))
     {
         return _fail_permission("sanpham_add");
     }
     SanPhamController ctr = new SanPhamController();
     SanPham obj = new SanPham();
     //Save obj to session
     Session["sanpham_new_tmp"] = obj;
     Session["sanpham_new_dbcontext"] = ctr._db;
     return RedirectToAction("Index", "AdminSanPham", new { id = 0 });
 }
 public List<SanPham> spnew()
 {
     SanPhamController ctrl = new SanPhamController();
     List<SanPham> listnew = ctrl.timkiem("", "", "", "", -1, -1, null, null, "1", "id", true, 0, 6);
     if (listnew != null)
     {
         //foreach (var item in listnew)
         //{
         //    item.mota = sub(item.mota);
         //}
         return listnew;
     }
     return new List<SanPham>();
 }
 public ActionResult Active(int id=0)
 {
     //controller
     SanPhamController ctr= new SanPhamController();
     //get obj
     SanPham obj = ctr.get_by_id(id);
     //check
     if (obj == null)
     {
         return RedirectToAction("Index", "AdminSanPhams");
     }
     //đảo ngược active
     obj.active = !obj.active;
     //lưu
     ctr._db.SaveChanges();
     //redirect
     return RedirectToAction("Index","AdminSanPhams");
 }
 //
 // GET: /FrontSanPhamDetail/
 public ActionResult Index(int id=0)
 {
     ViewBag.id = 2;
     SanPhamController ctr = new SanPhamController();
     SanPham sp = new SanPham();
     sp=ctr.get_by_id(id);
     if (sp != null&&sp.active==true)
     {
         ViewBag.Title = sp.ten;
         ViewBag.SanPhamDetail = sp;
         List<NhomSanPham> a = new List<NhomSanPham>();
         a.Add(sp.nhomsanpham);
         List<SanPham> SanPhamRelate = ctr.timkiem("", "", "", "", -1, -1, null, a, "1", "id", true, 0, 3);
         if (SanPhamRelate != null) ViewBag.SanPhamRelate = SanPhamRelate;
         else ViewBag.SanPhamRelate = new List<SanPham>();
         return View();
     }
     else return View("Error");
 }
        public ActionResult ChiTietSP_Delete(int id)
        {
            if (!this._nhanvien_permission.Contains("chitietsp_delete"))
            {
                return _fail_permission("chitietsp_delete");
            }
            SanPhamController ctr = new SanPhamController();
            ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db);
            ChiTietSP ctsp = new ChiTietSP();
            SanPham sanpham = (SanPham)Session["sanpham_new_tmp"];
            if (sanpham != null)
            {
                //vẫn còn đang ở un_save mode thì xóa từ session
                ctsp = sanpham.ds_chitietsp.Where(x => x.id == id).FirstOrDefault();
                sanpham.ds_chitietsp.Remove(ctsp);

                return Redirect(Url.Action("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id });
            }
            //đã save rồi
            else
            {
                ctsp = ctr_chitietsp.get_by_id(id);
                sanpham = ctr.get_by_id(ctsp.sanpham.id);
                if (ctsp == null)
                {
                    return RedirectToAction("Index", "AdminSanPhams");
                }
                //call xóa ctsp
                try
                {
                    ctr_chitietsp.delete(ctsp.id);
                }
                catch (Exception)
                {
                    return _show_notification("Chi tiết sản phẩm 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 Redirect(Url.Action("Index", "AdminSanPham", new { id = sanpham.id, chitietsp_id = id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id });
            }
        }
        public ActionResult Delete(int id = 0)
        {
            //check
            if (!this._nhanvien_permission.Contains("sanpham_delete"))
            {
                return this._fail_permission("sanpham_delete");
            }

            SanPhamController controller = new SanPhamController();
            if (!controller.is_exist(id))
            {
                return RedirectToAction("Index", "AdminSanPhams");
            }
            try
            {
                controller.delete(id);
            }
            catch (Exception)
            {
                return _show_notification("Sản phẩm này có dính khóa ngoại với chi tiết sản phẩm, hình ảnh hoặc đơn hàng hiện có nên không xóa được");
            }
            return RedirectToAction("Index", "AdminSanPhams");
        }
 public ActionResult ChiTietSP_Edit(int id)
 {
     if (!this._nhanvien_permission.Contains("chitietsp_edit"))
     {
         return _fail_permission("chitietsp_edit");
     }
     SanPhamController ctr = new SanPhamController();
     ChiTietSPController ctr_chitietsp = new ChiTietSPController();
     ChiTietSP ctsp = new ChiTietSP();
     SanPham sanpham =(SanPham) Session["sanpham_new_tmp"];
     if (sanpham != null)
     {
         //vẫn còn đang ở un_save mode thì lấy từ session ra
         ctsp = sanpham.ds_chitietsp.Where(x => x.id == id).FirstOrDefault();
         return Redirect(Url.Action("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = 0, chitietsp_id = ctsp.id });
     }
     //đã save rồi
     else
     {
         ctsp = ctr_chitietsp.get_by_id(id);
         sanpham = ctr.get_by_id(ctsp.sanpham.id);
         if (ctsp == null)
         {
             return RedirectToAction("Index", "AdminSanPhams");
         }
         return Redirect(Url.Action("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id }) + "#qd_chitietsp");//return RedirectToAction("Index", "AdminSanPham", new { id = ctsp.sanpham.id, chitietsp_id = ctsp.id });
     }
 }
        public ActionResult Submit()
        {
            //get obj id first
            int obj_id = TextLibrary.ToInt(Request["sanpham_id"]);
            //must use same dbcontext neu khong se bao loi (chi can thiet khi co cap nhat thao tac tren doi tuong sanpham trong session)
            SanPhamController ctr = new SanPhamController(this._get_current_dbcontext());

            SanPham obj = (SanPham)Session["sanpham_new_tmp"];
            Boolean un_save = false;
            if (obj != null)
            {
                //un save mode
                un_save = true;
                //add mode
                if (!this._nhanvien_permission.Contains("sanpham_add"))
                {
                    return _fail_permission("sanpham_add");
                }
            }
            else
            {
                //get obj from csdl
                obj = ctr.get_by_id(obj_id);
                //edit mode
                if (!this._nhanvien_permission.Contains("sanpham_edit"))
                {
                    return _fail_permission("sanpham_edit");
                }
                if (obj == null)
                {
                    return RedirectToAction("Index", "AdminSanPhams");
                }
            }

            //assign value
                obj.active = TextLibrary.ToBoolean(Request["sanpham_active"]);
                obj.gia = TextLibrary.ToInt(Request["sanpham_gia"]);
                obj.masp = TextLibrary.ToString(Request["sanpham_masp"]);
                obj.mota = TextLibrary.ToString(Request.Unvalidated["sanpham_mota"]);
                obj.ten = TextLibrary.ToString(Request["sanpham_ten"]);
                //get external object
                HangSXController ctr_hangsx = new HangSXController(ctr._db);
                obj.hangsx = ctr_hangsx.get_by_id(TextLibrary.ToInt(Request["sanpham_hangsx_id"]));

                NhomSanPhamController ctr_nhomsanpham = new NhomSanPhamController(ctr._db);
                obj.nhomsanpham = ctr_nhomsanpham.get_by_id(TextLibrary.ToInt(Request["sanpham_nhomsanpham_id"]));

            //validate properties
            List<String> validate = ctr.validate(obj);

            //action
            if (validate.Count == 0)
            {
                if (!un_save)
                {
                    //update properties first
                    ctr._db.SaveChanges();
                    this._state.Add("edit_ok");
                    this._add_state_tempdata("edit_ok");
                    //get image
                    if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                    {
                        HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                        List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                        obj.ds_hinhanh.AddRange(hinhanh_list);
                        ctr._db.SaveChanges();
                    }

                    //successfull redirect
                    return RedirectToAction("Index", "AdminSanPham", new { id = obj.id });
                }
                else
                {
                    //người dùng bấm nút upload hình ảnh chứ không phải nút lưu
                    if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                    {
                        //khoan hãy lưu vào csdl
                        HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                        List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                        obj.ds_hinhanh.AddRange(hinhanh_list);
                        //re assign to session
                        Session["sanpham_new_tmp"] = obj;
                        //successfull redirect
                        return RedirectToAction("Index", "AdminSanPham", new { id = 0 });
                    }
                    else
                    {
                        //call add
                        int maxid = ctr.add(obj);
                        //re assign id
                        obj.id = maxid;
                        this._state.Add("add_ok");
                        this._add_state_tempdata("add_ok");
                        //successfull redirect
                        return RedirectToAction("Index", "AdminSanPham", new { id = obj.id });
                    }

                }
            }
            else
            {
                //người dùng bấm nút upload hình ảnh chứ không phải nút lưu
                if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                {
                    //khoan hãy lưu vào csdl
                    HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                    List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                    obj.ds_hinhanh.AddRange(hinhanh_list);
                    //re assign to session
                    Session["sanpham_new_tmp"] = obj;
                    //successfull redirect
                    this._add_state_tempdata(validate);
                    return RedirectToAction("Index", "AdminSanPham", new { id = 0 });
                }
            }

            //fail or redirect
            this._state.AddRange(validate);
            ViewBag.State = this._state;
            ViewBag.SanPham = obj;
            ViewBag.Title += " - Submit";
            return View("Index");
        }
 //WORK OK
 public ActionResult Index(int id = 0, int chitietsp_id = 0)
 {
     if (!this._nhanvien_permission.Contains("sanpham_view"))
     {
         return _fail_permission("sanpham_view");
     }
     SanPhamController ctr = new SanPhamController();
     SanPham obj = ctr.get_by_id(id);
     Boolean un_save = false;
     if (obj == null)//tuwsc laf id=0, ddang owr chees dooj SANPHAM chuwa luuw
     {
         //lay tu session ra
         if (Session != null && Session["sanpham_new_tmp"] != null)
         {
             obj = (SanPham)Session["sanpham_new_tmp"];
         }
         else
         {
             //Chưa gọi set session trước
             return RedirectToAction("Index", "AdminSanPhams");
         }
         un_save = true;
     }
     else
     {
         //một khi đã hiển thị sản phẩm đã được save thì xóa ngay và luôn session
         if (Session != null && Session["sanpham_new_tmp"] != null)
         {
             Session["sanpham_new_tmp"] = null;
         }
     }
     ViewBag.SanPham = obj;
     //ON ActionEx...
     ViewBag.Title += " - View";
     ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db);
     ChiTietSP ctsp;
     if (un_save)
     {
         if (obj.ds_chitietsp.Where(x => x.id == chitietsp_id).FirstOrDefault()!=null)
         {
             //lay tư trong session ra
             ctsp = obj.ds_chitietsp.Where(x => x.id == chitietsp_id).FirstOrDefault();
         }
         else
         {
             ctsp = new ChiTietSP();
         }
     }
     else
     {
         ctsp = ctr_chitietsp.get_by_id(chitietsp_id);
     }
     ViewBag.ChiTietSP = ctsp == null ? new ChiTietSP() : ctsp;
     //từ bên controller trước đưa sang
     this._state.AddRange(this._get_state_tempdata());
     ViewBag.State = this._state;
     return View();
 }
        public ActionResult ChiTietSP_Submit()
        {
            int sanpham_id = TextLibrary.ToInt(Request["sanpham_id"]);
            //must use same dbcontext neu khong se bao loi (chi can thiet khi co cap nhat thao tac tren doi tuong sanpham trong session)
            SanPhamController ctr = new SanPhamController(this._get_current_dbcontext());

            ChiTietSPController ctr_chitietsp = new ChiTietSPController(ctr._db);
            KichThuocController ctr_kichthuoc = new KichThuocController(ctr._db);
            MauSacController ctr_mausac = new MauSacController(ctr._db);
            SanPham sanpham_obj = ctr.get_by_id(sanpham_id);
            Boolean un_save_mode = false;
            //neu sanpham_id=0, dang o mode chua save
            if (sanpham_obj == null)
            {
                sanpham_obj = (SanPham)Session["sanpham_new_tmp"];
                un_save_mode = true;
            }
            //create new
            ChiTietSP obj;
            int chitietsp_id = TextLibrary.ToInt(Request["chitietsp_id"]);
            Boolean edit_mode;
            if (un_save_mode)
            {
                edit_mode  = sanpham_obj.ds_chitietsp.Where(x=>x.id==chitietsp_id).FirstOrDefault()!=null?true:false;
                if (edit_mode)
                {
                    if (!this._nhanvien_permission.Contains("chitietsp_edit"))
                    {
                        return _fail_permission("chitietsp_edit");
                    }
                    obj = sanpham_obj.ds_chitietsp.Where(x => x.id==chitietsp_id).FirstOrDefault();
                }
                else
                {
                    if (!this._nhanvien_permission.Contains("chitietsp_add"))
                    {
                        return _fail_permission("chitietsp_add");
                    }
                    obj = new ChiTietSP();
                    //kiểm tra phải có ít nhất 1 kích thước
                    if (ctr_kichthuoc.timkiem_count("", "", "", "1") <= 0)
                    {
                        return _show_notification("Yêu cầu phải có ít nhất 1 kích thước active mới thêm được chi tiết sản phẩm");
                    }
                    //kiểm tra phải có ít nhất 1 màu sắc
                    if (ctr_mausac.timkiem_count("", "", "", "1") <= 0)
                    {
                        return _show_notification("Yêu cầu phải có ít nhất 1 màu sắc active mới thêm được chi tiết sản phẩm");
                    }
                }
            }
            //chế dộ đã save rồi
            else
            {
                edit_mode = ctr_chitietsp.get_by_id(chitietsp_id) != null;
                if (edit_mode)
                {
                    if (!this._nhanvien_permission.Contains("chitietsp_edit"))
                    {
                        return _fail_permission("chitietsp_edit");
                    }
                    obj = ctr_chitietsp.get_by_id(chitietsp_id);
                }
                else
                {
                    if (!this._nhanvien_permission.Contains("chitietsp_add"))
                    {
                        return _fail_permission("chitietsp_add");
                    }
                    obj = new ChiTietSP();
                    //kiểm tra phải có ít nhất 1 kích thước
                    if (ctr_kichthuoc.timkiem_count("", "", "", "1") <= 0)
                    {
                        return _show_notification("Yêu cầu phải có ít nhất 1 kích thước active mới thêm được chi tiết sản phẩm");
                    }
                    //kiểm tra phải có ít nhất 1 màu sắc
                    if (ctr_mausac.timkiem_count("", "", "", "1") <= 0)
                    {
                        return _show_notification("Yêu cầu phải có ít nhất 1 màu sắc active mới thêm được chi tiết sản phẩm");
                    }
                }
            }

            //assign data
                obj.soluong = TextLibrary.ToInt(Request["chitietsp_soluong"]);
                //ctr_kichthuoc = new KichThuocController(ctr._db);
                obj.kichthuoc = ctr_kichthuoc.get_by_id(TextLibrary.ToInt(Request["chitietsp_kichthuoc_id"]));

                //ctr_mausac = new MauSacController(ctr._db);
                obj.mausac = ctr_mausac.get_by_id(TextLibrary.ToInt(Request["chitietsp_mausac_id"]));

                obj.active = TextLibrary.ToBoolean(Request["chitietsp_active"]);
            if (un_save_mode)
            {
                if (edit_mode)
                {
                    //do nothing
                }
                else
                {
                    //add to sanpham tmp
                    //must set id
                    try
                    {
                        obj.id = sanpham_obj.ds_chitietsp.Max(x => x.id) + 1;
                    }
                    catch (Exception)
                    {
                        obj.id = 1;
                    }
                    sanpham_obj.ds_chitietsp.Add(obj);
                    //re assign to session
                    Session["sanpham_new_tmp"] = sanpham_obj;
                }
            }
            //chế độ đã save rồi
            else
            {
                if (edit_mode)
                {
                    //nothing
                }
                else
                {
                    sanpham_obj.ds_chitietsp.Add(obj);
                }
                //finally call update
                ctr._db.SaveChanges();
            }
            return Redirect(Url.Action("Index", "AdminSanPham", new { id = sanpham_id }) + "#qd_ds_chitietsp"); //return RedirectToAction("Index", "AdminSanPham", new { id = sanpham_id });
        }
 public Boolean set_default(int id)
 {
     //get hinhanh obj
     HinhAnh obj = this.get_by_id(id);
     if (obj == null || obj.sanpham == null)
     {
         return false;
     }
     //get sanpham obj
     SanPhamController ctr_sanpham=new SanPhamController(this.db);
     SanPham sanpham = ctr_sanpham.get_by_id(obj.sanpham.id);
     //get all hinhanh belong to this sanpham
     List<HinhAnh> hinhanh_list = this.db.ds_hinhanh.Where(x => x.sanpham.id == sanpham.id).ToList();
     //set all to non-default
     foreach (HinhAnh item in hinhanh_list)
     {
         item.macdinh = false;
         //set to default
         if (item.id == obj.id)
         {
             item.macdinh = true;
         }
     }
     this.db.SaveChanges();
     return true;
 }
 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 Index(int page=1)
        {
            //check
            if (!this._nhanvien_permission.Contains("sanpham_view"))
            {
                return this._fail_permission("sanpham_view");
            }
            SanPhamController ctr = new SanPhamController();
            NhomSanPhamController ctr_nhom = new NhomSanPhamController();
            HangSXController ctr_hangsx = new HangSXController();
            List<HangSX> hangsx_list = ctr_hangsx.timkiem(timkiem_sanpham["hangsx_id"]);
            NhomSanPham nhom_obj = ctr_nhom.get_by_id(TextLibrary.ToInt(timkiem_sanpham["nhomsanpham_id"]));
            List<NhomSanPham> nhomsanpham_list = ctr_nhom.get_tree2(nhom_obj);
            //pagination
                int max_item_per_page = TextLibrary.ToInt(this.timkiem_sanpham["max_item_per_page"]);
                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_sanpham["id"],
                        timkiem_sanpham["masp"],
                        timkiem_sanpham["ten"],
                        "",
                        TextLibrary.ToInt(timkiem_sanpham["gia_from"]),
                        TextLibrary.ToInt(timkiem_sanpham["gia_to"]),
                        hangsx_list, nhomsanpham_list, timkiem_sanpham["active"]
                        )
                    );
                pg.update();
            //Chọn danh sách nhân viên để hiển thị theo cookies tìm kiếm

            ViewBag.SanPham_List = ctr.timkiem(
                timkiem_sanpham["id"],
                timkiem_sanpham["masp"],
                timkiem_sanpham["ten"],
                "",
                TextLibrary.ToInt( timkiem_sanpham["gia_from"]),
                TextLibrary.ToInt(timkiem_sanpham["gia_to"]),
                hangsx_list, nhomsanpham_list, timkiem_sanpham["active"], timkiem_sanpham["order_by"], TextLibrary.ToBoolean(timkiem_sanpham["order_desc"]), pg.start_point, max_item_per_page
                );
            //set search cookies
            ViewBag.timkiem_sanpham = this.timkiem_sanpham;
            ViewBag.Title += " - Quản lý";
            ViewBag.pagination = pg;
            ViewBag.NhomSanPham2_List = ctr_nhom.timkiem();
            ViewBag.HangSX_List = ctr_hangsx.timkiem();
            return View();
        }