public void saveNhatKiNhapSach()
        {
            //NHATKINHAPSACH nhatkinhapsach = repository.getNhatKiNhapSach(view.selectedNhatKiNhapSach);
            NHATKINHAPSACH nhatkinhapsach = ViewToModel();


            if (valid(nhatkinhapsach) && repository.checkRepeat(nhatkinhapsach) == null)
            {
                NHATKINHAPSACH kq = repository.saveNhatKiNhapSach(nhatkinhapsach);

                //view.Log("Đã lưu thành công");
                getListNhatKiNhapSach();
            }
            else if (repository.checkRepeat(nhatkinhapsach) != null)
            {
                NHATKINHAPSACH temp = repository.checkRepeat(nhatkinhapsach);

                if (view.Log("Mã sách và thời gian trùng lặp,bạn có muốn cộng dồn vào số lượng cũ") == DialogResult.Yes)
                {
                    temp.SOLUONG = temp.SOLUONG + nhatkinhapsach.SOLUONG;
                    repository.editNhatKiNhapSach(temp, temp.STT);
                }
                getListNhatKiNhapSach();
            }
        }
        public void editNhatKiNhapSach()
        {
            NHATKINHAPSACH nhatkinhapsach_moi = ViewToModel();

            if (view.selectedNhatKiNhapSach != -1)
            {
                int manhatkinhapsach_cu = Convert.ToInt32(view.selectedNhatKiNhapSach);
                nhatkinhapsach_moi.STT = manhatkinhapsach_cu;
                if (valid(nhatkinhapsach_moi) && repository.checkRepeat(nhatkinhapsach_moi) == null)
                {
                    nhatkinhapsach_moi.SOLUONG = nhatkinhapsach_moi.SOLUONG;
                    NHATKINHAPSACH kq = repository.editNhatKiNhapSach(nhatkinhapsach_moi, manhatkinhapsach_cu);


                    getListNhatKiNhapSach();
                }
                else
                if (valid(nhatkinhapsach_moi) && repository.checkRepeat(nhatkinhapsach_moi) != null)
                {
                    NHATKINHAPSACH temp = repository.checkRepeat(nhatkinhapsach_moi);

                    if (view.Log("Mã sách và thời gian trùng lặp,bạn có muốn cộng dồn vào số lượng cũ") == DialogResult.Yes)
                    {
                        temp.SOLUONG = temp.SOLUONG + nhatkinhapsach_moi.SOLUONG;
                        repository.editNhatKiNhapSach(temp, temp.STT);
                        repository.deleteNhatKiNhapSach(manhatkinhapsach_cu);
                    }
                    getListNhatKiNhapSach();
                }
            }
        }
 private void ModelToView(NHATKINHAPSACH nhatkinhapsach)
 {
     //view.STT=nhatkinhapsach.STT;
     view.MASACH   = nhatkinhapsach.MASACH;
     view.NGAYNHAP = nhatkinhapsach.NGAYNHAP;
     view.SOLUONG  = nhatkinhapsach.SOLUONG;
 }
        public NHATKINHAPSACH editNhatKiNhapSach(NHATKINHAPSACH nkns, int oldMaNhatKiNhapSach)
        {
            NHATKINHAPSACH nhatkinhapsach_re = (from c in entity.NHATKINHAPSACHes where c.STT == oldMaNhatKiNhapSach select c).FirstOrDefault();
            int            change;

            if (nhatkinhapsach_re.SOLUONG > nkns.SOLUONG)
            {
                change = (int)(nhatkinhapsach_re.SOLUONG - nkns.SOLUONG);
                KHO kho = (from c in entity.KHOes
                           where c.MASACH == nhatkinhapsach_re.MASACH
                           select c).FirstOrDefault();
                kho.TONGSOLUONG = kho.TONGSOLUONG - change;
            }
            else
            {
                change = (int)(nkns.SOLUONG - nhatkinhapsach_re.SOLUONG);
                KHO kho = (from c in entity.KHOes
                           where c.MASACH == nhatkinhapsach_re.MASACH
                           select c).FirstOrDefault();
                kho.TONGSOLUONG = kho.TONGSOLUONG + change;
            }


            nhatkinhapsach_re.MASACH = nkns.MASACH;
            //saveNhatKiNhapSach(nkns);
            //deleteNhatKiNhapSach(nhatkinhapsach_re.STT);
            nhatkinhapsach_re.NGAYNHAP = nkns.NGAYNHAP;
            nhatkinhapsach_re.SOLUONG  = nkns.SOLUONG;

            entity.SaveChanges();

            return(nhatkinhapsach_re); //nhatkinhapsach cu
        }
        private NHATKINHAPSACH ViewToModel()
        {
            NHATKINHAPSACH nhatkinhapsach = new NHATKINHAPSACH();

            nhatkinhapsach.MASACH   = view.MASACH;
            nhatkinhapsach.NGAYNHAP = view.NGAYNHAP;
            nhatkinhapsach.SOLUONG  = view.SOLUONG;
            return(nhatkinhapsach);
        }
 public void showSelected()
 {
     if (view.selectedNhatKiNhapSach != -1)
     {
         NHATKINHAPSACH nhatkinhapsach = repository.getNhatKiNhapSach(view.selectedNhatKiNhapSach);
         ModelToView(nhatkinhapsach);
         //view.NhatKiNhapSach = nhatkinhapsach;
     }
 }
        public void deleteNhatKiNhapSach()
        {
            //NHATKINHAPSACH nhatkinhapsach = repository.getNhatKiNhapSach(view.selectedNhatKiNhapSach);
            if (view.selectedNhatKiNhapSach != -1)
            {
                int            manhatkinhapsach = Convert.ToInt32(view.selectedNhatKiNhapSach);
                NHATKINHAPSACH nhatkinhapsach   = repository.getNhatKiNhapSach(manhatkinhapsach);
                if (valid(nhatkinhapsach))
                {
                    NHATKINHAPSACH kq = repository.deleteNhatKiNhapSach(manhatkinhapsach);

                    //view.Log("Đã lưu thành công");
                    getListNhatKiNhapSach();
                }
            }
        }
 public NHATKINHAPSACH saveNhatKiNhapSach(NHATKINHAPSACH nkns)
 {
     try
     {
         entity.NHATKINHAPSACHes.Add(nkns); entity.SaveChanges();
         KHO kho = (from c in entity.KHOes
                    where c.MASACH == nkns.MASACH
                    select c).FirstOrDefault();
         kho.TONGSOLUONG = kho.TONGSOLUONG + nkns.SOLUONG;
         entity.SaveChanges();
         return(nkns);
     }
     catch (DbUpdateException) // tranh loi update
     {
         return(null);
     }
 }
        public NHATKINHAPSACH checkRepeat(NHATKINHAPSACH nkns)
        {
            NHATKINHAPSACH repeat_item = (from c in entity.NHATKINHAPSACHes
                                          where nkns.MASACH == c.MASACH &&
                                          nkns.NGAYNHAP == c.NGAYNHAP &&
                                          nkns.STT != c.STT
                                          select c).FirstOrDefault();

            if (repeat_item == null)
            {
                return(null);
            }
            else
            {
                return(repeat_item);
            }
        }
 public bool valid(NHATKINHAPSACH nhatkinhapsach)
 {
     //xet null
     state.Clear();
     //if (nhatkinhapsach.SOLUONGCON <0)
     //    state.addError("soluongcon", "Số lượng còn không được nhỏ hơn 0");
     //if (nhatkinhapsach.TONGSOLUONG < 0)
     //    state.addError("tongsoluong", "Tổng số lượng không được nhỏ hơn 0");
     //if (nhatkinhapsach.SOLUONGCON > nhatkinhapsach.TONGSOLUONG)
     //    state.addError("lonhon", "Số lượng còn không được lớn hơn tổng số lượng");
     //if (nhatkinhapsach.MASACH == "")
     //    state.addError("sach", "Không còn sách để thêm thông tin");
     if (nhatkinhapsach.MASACH == "a")
     {
         state.addError("sach", "Chọn sách để thêm thông tin");
     }
     return(state.isValid());
 }
        public NHATKINHAPSACH deleteNhatKiNhapSach(int MaNhatKiNhapSach)
        {
            NHATKINHAPSACH nhatkinhapsach = (from c in entity.NHATKINHAPSACHes where c.STT == MaNhatKiNhapSach select c).FirstOrDefault();

            //entity.SaveChanges();
            KHO kho = (from c in entity.KHOes
                       where c.MASACH == nhatkinhapsach.MASACH
                       select c).FirstOrDefault();

            kho.TONGSOLUONG = kho.TONGSOLUONG - nhatkinhapsach.SOLUONG;
            if (kho.SOLUONGCON > kho.TONGSOLUONG)
            {
                kho.SOLUONGCON = kho.TONGSOLUONG;
            }
            entity.NHATKINHAPSACHes.Remove(nhatkinhapsach);
            entity.SaveChanges();



            return(nhatkinhapsach);
        }