public static bool edit(CongNoNXB congno)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             CONGNONXB cn;
             cn = (from d in db.CONGNONXBs
                   where d.masonxb.Equals(congno.MaSoNXB)
                   && d.masosach.Equals(congno.MaSoSach)
                   && d.thang.Month.Equals(congno.Thang.Month)
                   && d.thang.Year.Equals(congno.Thang.Year)
                   select d).SingleOrDefault();
             if (cn == null) return false; //Nếu đại lý không tồn tại
             cn.soluong = congno.SoLuong;
             cn.dongia = congno.DonGia;
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool edit(NhaXuatBan nhaxuatban)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             NXB nxb;
             nxb = (from n in db.NXBs
                    where n.masonxb.Equals(nhaxuatban.MaSoNXB)
                    select n).SingleOrDefault();
             if (nxb == null) return false;
             nxb.ten = nhaxuatban.TenNXB;
             nxb.diachi = nhaxuatban.DiaChi;
             nxb.sodienthoai = nhaxuatban.SoDienThoai;
             nxb.sotaikhoan = nhaxuatban.SoTaiKhoan;
             nxb.trangthai = nhaxuatban.TrangThai;
             nxb.nganhang = nhaxuatban.NganHang;
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool edit(DaiLy daily)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             DAILY dl;
             dl = (from d in db.DAILies
                   where d.masodaily.Equals(daily.MaSoDaiLy)
                   select d).SingleOrDefault();
             if (dl == null) return false; //Nếu đại lý không tồn tại
             dl.ten = daily.TenDaiLy;
             dl.diachi = daily.DiaChi;
             dl.sodienthoai = daily.SoDienThoai;
             dl.sotaikhoan = daily.SoTaiKhoan;
             dl.trangthai = daily.TrangThai;
             dl.nganhang = daily.NganHang;
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static int add(Sach sach)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var s = new SACH();
             s.tensach = sach.TenSach;
             s.masolinhvuc = sach.LinhVucSach.MaSoLinhVuc;
             s.masonxb = sach.NXB.MaSoNXB;
             s.tacgia = sach.TenTacGia;
             s.soluong = sach.Soluong;
             s.giaban = sach.GiaBan;
             s.gianhap = sach.GiaNhap;
             s.hinhanh = sach.HinhAnh;
             s.mota = sach.MoTa;
             db.SACHes.InsertOnSubmit(s);
             db.SubmitChanges();
             return s.masosach;
         }
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static int add(DaiLy daily)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             DAILY dl;
             dl = (from d in db.DAILies
                   where d.masodaily.Equals(daily.MaSoDaiLy)
                   select d).SingleOrDefault();
             if (dl != null) return 0; //Nếu đại lý đã tồn tại
             dl = new DAILY()
             {
                 ten = daily.TenDaiLy,
                 diachi = daily.DiaChi,
                 sodienthoai = daily.SoDienThoai,
                 sotaikhoan = daily.SoTaiKhoan,
                 nganhang = daily.NganHang,
                 masonguoidung = daily.MaSoNguoiDung
             };
             db.DAILies.InsertOnSubmit(dl);
             db.SubmitChanges();
             return dl.masodaily;
         }
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static int add(PhieuNhap phieunhap)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var phieu = new PHIEUNHAP()
             {
                 masonxb = phieunhap.NXB.MaSoNXB,
                 ngaylap = phieunhap.NgayLap,
                 nguoigiaosach = phieunhap.NguoiGiao,
                 tongtien = phieunhap.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia),
                 trangthai = 0
             };
             db.PHIEUNHAPs.InsertOnSubmit(phieu);
             db.SubmitChanges();
             ChiTiet.add(phieunhap.ChiTiet, phieu.masophieunhap);
             return phieu.masophieunhap;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static int add(PhieuXuat phieuxuat)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var phieu = new PHIEUXUAT
             {
                 masodaily = phieuxuat.MaSoDaiLy,
                 ngaylap = phieuxuat.NgayLap,
                 nguoinhasach = phieuxuat.NguoiNhan,
                 tongtien = phieuxuat.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia),
                 trangthai = 0
             };
             db.PHIEUXUATs.InsertOnSubmit(phieu);
             db.SubmitChanges();
             foreach(ChiTietPhieuXuat ct in phieuxuat.ChiTiet)
             {
                 Chitiet.add(ct, phieu.masophieuxuat);
             }
             return phieu.masophieuxuat;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
        public static bool edit(HoaDonDaiLy hoadon)
        {
            try
            {
                using (EntitiesDataContext db = new EntitiesDataContext())
                {
                    HOADONDAILY hd;
                    hd = (from p in db.HOADONDAILies
                          where p.masohoadon.Equals(hoadon.MaSoHoaDon)
                          select p).SingleOrDefault();
                    if (hd == null) return false;
                    hd.masodaily = hoadon.MaSoDaiLy;
                    hd.ngaylap = hoadon.NgayLap;
                    hd.trangthai = hoadon.TrangThai;
                    hd.tongtien = hoadon.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia); // tính tổng tiền các chi tiết
                    db.CHITIETHOADONDAILies.DeleteAllOnSubmit(hd.CHITIETHOADONDAILies);
                    db.SubmitChanges();
                    foreach (ChiTietHoaDonDaiLy ct in hoadon.ChiTiet)
                    {
                        ChiTiet.add(ct, hoadon.MaSoHoaDon);
                    }

                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
 public static int add(HoaDonDaiLy hoadon)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var hd = new HOADONDAILY
             {
                 masodaily = hoadon.MaSoDaiLy,
                 ngaylap = hoadon.NgayLap,
                 tongtien = hoadon.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia),
                 trangthai = 0
             };
             db.HOADONDAILies.InsertOnSubmit(hd);
             db.SubmitChanges();
             foreach (ChiTietHoaDonDaiLy ct in hoadon.ChiTiet)
             {
                 ChiTiet.add(ct, hd.masohoadon);
             }
             return hd.masohoadon;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static bool delete(CongNoDaiLy congno)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             CONGNODAILY cn;
             cn = (from d in db.CONGNODAILies
                   where d.masodaily.Equals(congno.MaSoDaiLy)
                   && d.masosach.Equals(congno.MaSoSach)
                   && d.thang.Month.Equals(congno.Thang.Month)
                   && d.thang.Year.Equals(congno.Thang.Year)
                   select d).SingleOrDefault();
             if (cn == null) return false; //Nếu đại lý không tồn tại
             db.CONGNODAILies.DeleteOnSubmit(cn);
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
        public static bool edit(PhieuXuat phieu)
        {
            try
            {
                using(EntitiesDataContext db = new EntitiesDataContext())
                {
                    PHIEUXUAT px;
                    px = (from p in db.PHIEUXUATs
                          where p.masophieuxuat.Equals(phieu.MaSoPhieuXuat)
                          select p).SingleOrDefault();
                    if (px == null) return false;
                    px.masodaily = phieu.MaSoDaiLy;
                    px.ngaylap = phieu.NgayLap;
                    px.nguoinhasach = phieu.NguoiNhan;
                    px.trangthai = phieu.TrangThai;
                    px.tongtien = phieu.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia); // tính tổng tiền các chi tiết
                    db.CHITIETPHIEUXUATs.DeleteAllOnSubmit(px.CHITIETPHIEUXUATs);
                    db.SubmitChanges();
                    foreach (ChiTietPhieuXuat ct in phieu.ChiTiet)
                    {
                        ct.MaSoPhieuXuat = phieu.MaSoPhieuXuat;
                        Chitiet.add(ct);
                    }

                    return true;
                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
 public static int add(LinhVuc linhvuc)
 {
     using (EntitiesDataContext db = new EntitiesDataContext())
     {
         var lv = new LINHVUC();
         lv.ten = linhvuc.TenLinhVuc;
         db.LINHVUCs.InsertOnSubmit(lv);
         db.SubmitChanges();
         return lv.masolinhvuc;
     }
 }
 public static bool edit(LinhVuc linhvuc)
 {
     using (EntitiesDataContext db = new EntitiesDataContext())
     {
         //Kiểm tra lĩnh vực có tồn tại không
         LINHVUC lv;
         lv = (from b in db.LINHVUCs
               where b.masolinhvuc.Equals(linhvuc.MaSoLinhVuc)
               select b).SingleOrDefault();
         if (lv == null) return false;
         lv.ten = linhvuc.TenLinhVuc;
         lv.trangthai = linhvuc.TrangThai;
         db.SubmitChanges();
         return true;
     }
 }
 public static bool edit(NguoiDung nguoidung)
 {
     using (EntitiesDataContext db = new EntitiesDataContext())
     {
         var s = (from b in db.NGUOIDUNGs
                  where b.masonguoidung == nguoidung.MaSoNguoiDung
                  select b).SingleOrDefault();
         if (s == null) return false;
         s.matkhau = nguoidung.MatKhau;
         s.tendaydu = nguoidung.TenDayDu;
         s.email = nguoidung.Email;
         s.phanquyen = nguoidung.PhanQuyen;
         s.trangthai = nguoidung.TrangThai;
         db.SubmitChanges();
         return true;
     }
 }
 public static bool delete(int masosach)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             SACH sach;
             sach = (from s in db.SACHes
                     where s.masosach.Equals(masosach)
                     select s).SingleOrDefault();
             if (sach == null) return false;
             db.SACHes.DeleteOnSubmit(sach);
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool edit(Sach sach)
 {
     using (EntitiesDataContext db = new EntitiesDataContext())
     {
         var s = (from b in db.SACHes
                  where b.masosach == sach.MaSoSach
                  select b).SingleOrDefault();
         if (s == null) return false;
         s.tensach = sach.TenSach;
         s.masolinhvuc = sach.LinhVucSach.MaSoLinhVuc;
         s.masonxb = sach.NXB.MaSoNXB;
         s.tacgia = sach.TenTacGia;
         s.soluong = sach.Soluong;
         s.giaban = sach.GiaBan;
         s.gianhap = sach.GiaNhap;
         s.hinhanh = sach.HinhAnh;
         s.mota = sach.MoTa;
         s.trangthai = sach.TrangThai;
         db.SubmitChanges();
         return true;
     }
 }
 public static bool delete(int masonguoidung)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             NGUOIDUNG nguoidung;
             nguoidung = (from s in db.NGUOIDUNGs
                     where s.masonguoidung.Equals(masonguoidung)
                     select s).SingleOrDefault();
             if (nguoidung == null) return false;
             db.NGUOIDUNGs.DeleteOnSubmit(nguoidung);
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool delete(int masophieuxuat)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             PHIEUXUAT phieu;
             phieu = (from p in db.PHIEUXUATs
                      where p.masophieuxuat.Equals(masophieuxuat)
                      select p).SingleOrDefault();
             if (phieu == null) return false;
             db.CHITIETPHIEUXUATs.DeleteAllOnSubmit(phieu.CHITIETPHIEUXUATs);
             db.PHIEUXUATs.DeleteOnSubmit(phieu);
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool delete(TheKho thekho)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             THEKHO tk;
             tk = (from t in db.THEKHOs
                   where t.masosach.Equals(thekho.MaSoSach)
                   && t.ngayghi.Equals(thekho.NgayGhi)
                   select t).SingleOrDefault();
             if (tk == null) return false;
             db.THEKHOs.DeleteOnSubmit(tk);
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool delete(int masodaily)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             DAILY dl;
             dl = (from d in db.DAILies
                   where d.masodaily.Equals(masodaily)
                   select d).SingleOrDefault();
             if (dl == null) return false; //Nếu đại lý không tồn tại
             db.DAILies.DeleteOnSubmit(dl);
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static int add(CongNoNXB congno)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             CONGNONXB cn;
             cn = (from c in db.CONGNONXBs
                   where c.masonxb.Equals(congno.MaSoNXB)
                   && c.masosach.Equals(congno.MaSoSach)
                   && c.thang.Month.Equals(congno.Thang.Month)
                   && c.thang.Year.Equals(congno.Thang.Year)
                   select c).SingleOrDefault();
             if (cn != null)
             {
                 cn.soluong += congno.SoLuong;
                 db.SubmitChanges();
                 return 1;
             }
             else
             {
                 cn = new CONGNONXB();
                 cn.masonxb = congno.MaSoNXB;
                 cn.masosach = congno.MaSoSach;
                 cn.soluong = congno.SoLuong;
                 cn.dongia = congno.DonGia;
                 cn.thang = congno.Thang;
                 db.CONGNONXBs.InsertOnSubmit(cn);
                 db.SubmitChanges();
                 return 1;
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static bool delete(int masolinhvuc)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             //Kiểm tra lĩnh vực có tồn tại không
             LINHVUC lv;
             lv = (from b in db.LINHVUCs
                   where b.masolinhvuc.Equals(masolinhvuc)
                   select b).SingleOrDefault();
             if (lv == null) return false;
             db.LINHVUCs.DeleteOnSubmit(lv);
             db.SubmitChanges();
             return true;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool delete(int masonxb)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             NXB nxb;
             nxb = (from n in db.NXBs
                    where n.masonxb.Equals(masonxb)
                    select n).SingleOrDefault();
             if (nxb == null) return false;
             db.NXBs.DeleteOnSubmit(nxb);
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool delete(int masohoadon)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             HOADONDAILY hd;
             hd = (from p in db.HOADONDAILies
                   where p.masohoadon.Equals(masohoadon)
                   select p).SingleOrDefault();
             if (hd == null) return false;
             db.CHITIETHOADONDAILies.DeleteAllOnSubmit(hd.CHITIETHOADONDAILies);
             db.HOADONDAILies.DeleteOnSubmit(hd);
             db.SubmitChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static bool edit(TheKho thekho)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             THEKHO tk;
             tk = (from s in db.THEKHOs
                   where s.masosach.Equals(thekho.MaSoSach)
                         && s.ngayghi.Equals(thekho.NgayGhi)
                   select s).SingleOrDefault();
             if (tk == null) return false; //Nếu không tồn tại thẻ kho
             tk.soluong = thekho.SoLuong;
             db.SubmitChanges();
             return true;
         }
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return false;
     }
 }
 public static int add(NguoiDung nguoidung)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var s = new NGUOIDUNG();
             s.tennguoidung = nguoidung.TenNguoiDung;
             s.matkhau = nguoidung.MatKhau;
             s.tendaydu = nguoidung.TenDayDu;
             s.email = nguoidung.Email;
             s.phanquyen = nguoidung.PhanQuyen;
             s.trangthai = nguoidung.TrangThai;
             db.NGUOIDUNGs.InsertOnSubmit(s);
             db.SubmitChanges();
             return s.masonguoidung;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static int add(TheKho thekho)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             THEKHO tk;
             tk = (from s in db.THEKHOs
                   where s.masosach.Equals(thekho.MaSoSach)
                     && s.ngayghi.Equals(thekho.NgayGhi)
                   select s).SingleOrDefault();
             if(tk != null)
             {
                 tk.soluong = thekho.SoLuong;
                 db.SubmitChanges();
                 return 1;
             }
             else
             {
                 tk = new THEKHO()
                 {
                     masosach = thekho.MaSoSach,
                     soluong = thekho.SoLuong,
                     ngayghi = thekho.NgayGhi
                 };
                 db.THEKHOs.InsertOnSubmit(tk);
                 db.SubmitChanges();
                 return 1;
             }
         }
     }
     catch
     {
         return 0;
     }
 }
 public static int add(NhaXuatBan nhaxuatban)
 {
     try
     {
         using(EntitiesDataContext db = new EntitiesDataContext())
         {
             var nxb = new NXB
             {
                 ten = nhaxuatban.TenNXB,
                 diachi = nhaxuatban.DiaChi,
                 sodienthoai = nhaxuatban.SoDienThoai,
                 sotaikhoan = nhaxuatban.SoTaiKhoan,
                 nganhang = nhaxuatban.NganHang
             };
             db.NXBs.InsertOnSubmit(nxb);
             db.SubmitChanges();
             return nxb.masonxb;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public static List<NhaXuatBan> getAllAlive()
 {
     using (EntitiesDataContext db = new EntitiesDataContext())
     {
         var linqQuery = from nxb in db.NXBs
                         where nxb.trangthai == null
                         select new NhaXuatBan(nxb);
         return linqQuery.ToList();
     }
 }
 public static List<NhaXuatBan> getAll()
 {
     using(EntitiesDataContext db = new EntitiesDataContext())
     {
         var linqQuery = from nxb in db.NXBs
                         select new NhaXuatBan(nxb);
         return linqQuery.ToList();
     }
 }