public bool CapNhatGiaBanMoi(int MaSP, int MaMau, decimal GiaBan)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.Khoes.Where(p => p.MaSP == MaSP && p.MaMau == MaMau).ToList();
                    if (item != null)
                    {
                        for (int i = 0; i < item.Count; i++)
                            item[i].GiaBan = GiaBan;
                        db.SaveChanges();
                        return true;

                    }
                    else
                        return false;

                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public List<HoaDonNhap> LayTatCaHoaDon()
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var list = db.HDNhaps.Select(p => new HoaDonNhap
                    {
                        _MaHDN = p.MaHDN,
                        _MaNCC = (int)p.MaNCC,
                        _NCC = p.NhaCungCap.TenNCC,
                        _MaNV = (int)p.MaNV,
                        _NhanVien = p.NhanVien.HoTen,
                        _NgayLap = (DateTime)p.NgayNhap,
                        _TinhTrang = (int)p.TinhTrang,
                        _TongTien = (int)p.TongTien

                    }).ToList();
                    return list;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return null;
            }
        }
        public List<HoaDon> LayCTHDN(int MaHDN)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    List<HoaDon> list = new List<HoaDon>();
                    list = db.CTHDNs.Where(p => p.MaHDN == MaHDN).Select(p => new HoaDon
                    {
                        _MaSP = p.MaSP,
                        _TenSP = p.SanPham.TenSP,
                        _MaMau = p.MaMau,
                        _Mau = p.Mau.TenMau,
                        _MaSize = p.MaSize,
                        _Size = (int)p.Size.Size1,
                        _SoLuong = (int)p.SL,
                        _GiaSP = (decimal)p.GiaNhap,

                    }).ToList();
                    if (list != null)
                        return list;
                    return null;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return null;
            }
        }
 public List<SanPham_Price> DanhMuc(string th, int maloai)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var dm = db.Khoes.Where(n => n.SanPham.ThuongHieu == th && n.SanPham.MaLoai == maloai).Select(p => new SanPham_Price
             {
                 _MaSP = p.MaSP,
                 _MaMau = p.MaMau,
                 _TenSP = p.SanPham.TenSP,
                 _TenMau = p.Mau.TenMau,
                 _Size = (int)p.Size.Size1,
                 _GiaSP = (decimal)p.GiaBan,
                 _HinhAnh = p.SanPham.HinhAnh,
                 _NgayDang = (DateTime)p.SanPham.NgayDang,
             }).OrderBy(p => p._MaSP).ToList();
             int index = 0;
             while (index < dm.Count - 1)
             {
                 if (dm[index]._MaSP == dm[index + 1]._MaSP)
                     dm.RemoveAt(index);
                 else
                     index++;
             }
             return dm;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
        public bool SuaCTHDN(int MaHDN, int MaSP, int MaMau, int MaSize, int SoLuong, decimal DonGia)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.CTHDNs.Single(p => p.MaHDN == MaHDN && p.MaSP == MaSP);
                    if (item != null)
                    {
                        item.MaMau = MaMau;
                        item.MaSize = MaSize;
                        item.SL = SoLuong;
                        item.GiaNhap = (decimal)DonGia;
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public List<SanPham_Price> LayKhoTheoSP(int MaSP, int MaMau)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var list = db.Khoes.Where(n => n.MaSP == MaSP && n.MaMau == MaMau).Distinct().Select(p => new SanPham_Price
                    {
                        _MaSP = p.SanPham.MaSP,
                        _MaMau = p.MaMau,
                        _TenSP = p.SanPham.TenSP,
                        _TenMau = p.Mau.TenMau,
                        _Size = (int)p.Size.Size1,
                        _GiaSP = (decimal)p.GiaBan,
                        _HinhAnh = p.SanPham.HinhAnh,
                        _NgayDang = (DateTime)p.SanPham.NgayDang,
                    }).OrderBy(p => p._MaSP).ToList();
                    int index = 0;
                    while (index < list.Count - 1)
                    {
                        if (list[index]._MaSP == list[index + 1]._MaSP)
                            list.RemoveAt(index);
                        else
                            index++;
                    }
                    return list;

                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return null;
            }
        }
 public List<HoaDonXuat> LayTatCaDonHang()
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var list = db.DonHangs.Select(p => new HoaDonXuat
             {
                 _MaHDN = p.MaDH,
                 _MaKH = (int)p.MaKH,
                 _KhachHang = p.KhachHang.HoTen,
                 _NhanVien = p.NVDuyet,
                 _NgayLap = (DateTime)p.NgayMua,
                 _TinhTrang = (int)p.TinhTrang,
                 _TongTien = (int)p.TongTien
             }).OrderBy(p => p._NgayLap).ToList();
             return list;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public Size LaySizeTheoMa(int MaSize)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             Size tensize = db.Sizes.SingleOrDefault(n => n.MaSize == MaSize);
             return tensize;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public List<Size> LayTatCaSize()
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var list = db.Sizes.Select(p => p).ToList();
             return list;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public Mau LayMauTheoMa(int MaMau)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             Mau tenmau = db.Maus.SingleOrDefault(n => n.MaMau == MaMau);
             return tenmau;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public List<DonHang> LichSuDH(int MaKH)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var ls = db.DonHangs.Where(n => n.MaKH == MaKH).ToList();
             return ls;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public bool CheckSanPham(string TenSP)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var check = db.SanPhams.SingleOrDefault(p => p.TenSP.Equals(TenSP));
             if (check != null)
                 return true;
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
 public bool CheckNhaCungCap(string NCC)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var check = db.NhaCungCaps.SingleOrDefault(p => p.TenNCC.Equals(NCC));
             if (check != null)
                 return true;
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
 public bool CheckEmail(string Email)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var check = db.KhachHangs.SingleOrDefault(p => p.Email.Equals(Email));
             if (check != null)
                 return true;
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
        public int LayMotHoaDon()
        {
            int x = 0;
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var list = db.HDNhaps.Select(p => p.MaHDN).Max();
                    x = list;
                    return x;

                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return x;
            }
        }
 public List<SanPham> LayTatCaSanPham()
 {
     try
     {
         //Gọi tới Database
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             //Chọn tất cả trong bảng sản phẩm trong CSDL
             var list = (db.SanPhams.Where(p => p.TrangThai == 1).ToList());
             //Xuất ra dang sách sản phẩm
             return list;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
        public bool KiemTraNhanVien(string Email, string MatKhau)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var check = db.NhanViens.SingleOrDefault(c => c.Email == Email && c.MatKhau == MatKhau);
                    if (check != null)
                        return true;
                    return false;

                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public NhanVien LayMotNhanVien(string Email, string MatKhau)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var nhanvien = db.NhanViens.Where(c => c.Email.Contains(Email) && c.MatKhau.Contains(MatKhau)).SingleOrDefault();
                    if (nhanvien != null)
                        return nhanvien;

                    return null;

                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return null;
            }
        }
 public bool DuyetHD(int MaHDN)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var item = db.HDNhaps.SingleOrDefault(p => p.MaHDN == MaHDN);
             if (item != null)
             {
                 item.TinhTrang = 1;
                 db.SaveChanges();
                 return true;
             }
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
        public bool SuaNhaCungCap(int MaNCC, string TenNCC, string DiaChi, string SDT, string SoTK)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.NhaCungCaps.Single(p => p.MaNCC == MaNCC);
                    if (item != null)
                    {
                        if (!TenNCC.Equals(""))
                        {
                            item.TenNCC = TenNCC;
                        }
                        if (!DiaChi.Equals(""))
                        {
                            item.DiaChi = DiaChi;
                        }
                        if (!SDT.Equals(""))
                        {
                            item.SDT = SDT;
                        }
                        if (!SoTK.Equals(""))
                        {
                            item.SoTK = SoTK;
                        }
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public bool SuaLoai(int MaLoai, string TenLoai)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.Loais.Single(p => p.MaLoai == MaLoai);
                    if (item != null)
                    {
                        item.TenLoai = TenLoai;
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public bool SuaMau(int MaMau, string Mau)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.Maus.Single(p => p.MaMau == MaMau);
                    if (item != null)
                    {
                        item.TenMau = Mau;
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public bool SuaSize(int MaSize, int TenSize)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.Sizes.SingleOrDefault(p => p.MaSize == MaSize);
                    if (item != null)
                    {
                        item.Size1 = TenSize;
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
        public bool CapNhatBanHang(int MaSP, int Mau, int MaSize, int Soluong)
        {
            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    var item = db.Khoes.SingleOrDefault(p => p.MaSP == MaSP && p.MaMau == Mau && p.MaSize == MaSize);
                    if (item != null)
                    {
                        item.SL = item.SL - Soluong;
                        db.SaveChanges();
                        return true;

                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
 public bool XoaNhaCungCap(int MaNCC)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var SpDelete = db.NhaCungCaps.SingleOrDefault(p => p.MaNCC == MaNCC);
             if (SpDelete != null)
             {
                 db.NhaCungCaps.Remove(SpDelete);
                 db.SaveChanges();
                 return true;
             }
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
 public List<NhaCungCap> TimKiemNhaCungCap(string x)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var list = db.NhaCungCaps.Where(c => c.TenNCC.Contains(x) || c.DiaChi.Contains(x) || c.SDT.Contains(x) || c.SoTK.Contains(x)).ToList();
             return list;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return null;
     }
 }
 public bool ThemNhaCungCap(string TenNCC, string DiaChi, string SDT, string SoTK)
 {
     NhaCungCap ncc = new NhaCungCap();
     ncc.TenNCC = TenNCC;
     ncc.DiaChi = DiaChi;
     ncc.SDT = SDT;
     ncc.SoTK = SoTK;
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             db.NhaCungCaps.Add(ncc);
             db.SaveChanges();
             return true;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
        public bool ThemCTHDN(int MaHDN, int MaSP, int MaMau, int MaSize, int SoLuong, decimal DonGia)
        {
            CTHDN CT = new CTHDN();
            CT.MaHDN = MaHDN;
            CT.MaSP = MaSP;
            CT.MaMau = MaMau;
            CT.MaSize = MaSize;
            CT.SL = SoLuong;
            CT.GiaNhap = (decimal)DonGia;

            try
            {
                using (DBGiayDepEntities db = new DBGiayDepEntities())
                {
                    db.CTHDNs.Add(CT);
                    db.SaveChanges();
                    return true;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }
 public bool XoaCTHDN(int MaHDN, int MaSP, int MaMau, int MaSize)
 {
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             var SpDelete = db.CTHDNs.SingleOrDefault(p => p.MaHDN == MaHDN && p.MaSP == MaSP && p.MaSize == MaSize && p.MaMau == MaMau);
             if (SpDelete != null)
             {
                 db.CTHDNs.Remove(SpDelete);
                 db.SaveChanges();
                 return true;
             }
             return false;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }
 public bool ThemSize(int TenSize)
 {
     Size s = new Size();
     s.Size1 = TenSize;
     try
     {
         using (DBGiayDepEntities db = new DBGiayDepEntities())
         {
             db.Sizes.Add(s);
             db.SaveChanges();
             return true;
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         return false;
     }
 }