//Sửa phiếu xuất
 public bool Update(PhieuXuat inp)
 {
     try
     {
         IEnumerable <Model.PhieuXuat> dspx = from px in db.PhieuXuats
                                              where px.MaPhieuXuat == inp.MaPhieuXuat
                                              select px;
         Model.PhieuXuat phieuxuat = dspx.ElementAtOrDefault(0);
         if (phieuxuat != null)
         {
             phieuxuat.NhanVien   = inp.MaNhanVien;
             phieuxuat.KhachHang  = inp.MaKhachHang;
             phieuxuat.NgayXuat   = inp.NgayXuat;
             phieuxuat.DiaChiNhan = inp.DiaChiGiaoHang;
             phieuxuat.HoaDon     = inp.MaHoaDon;
             phieuxuat.TongTien   = inp.TongTien;
             db.SaveChanges();
             //Xoá chi tiết phiếu xuất cũ
             IEnumerable <Model.PhieuXuat_CT> dsctc = from ctc in db.PhieuXuat_CT
                                                      where ctc.MaPhieuXuat == inp.MaPhieuXuat
                                                      select ctc;
             if (dsctc.Count() > 0)
             {
                 foreach (Model.PhieuXuat_CT ctc in dsctc)
                 {
                     //cộng số lượng tồn
                     Model.QuanLiBanHangEntities db2  = new Model.QuanLiBanHangEntities();
                     IEnumerable <Model.SanPham> cnsp = from sp in db2.SanPhams
                                                        where sp.MaSanPham == ctc.MaSanPham
                                                        select sp;
                     Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                     sanpham.SoLuongTon = sanpham.SoLuongTon + ctc.SoLuongThucXuat;
                     db2.SaveChanges();
                     //xoá chi tiết phiếu
                     db.PhieuXuat_CT.Remove(ctc);
                 }
                 db.SaveChanges();
             }
             //Nhập lại chi tiết phiếu
             foreach (PhieuXuatCT ct in inp.DSChiTiet)
             {
                 Model.PhieuXuat_CT newct = new Model.PhieuXuat_CT();
                 newct.MaPhieuXuat     = ct.MaPhieuXuat;
                 newct.MaSanPham       = ct.MaSanPham;
                 newct.SoLuongYeuCau   = ct.SoLuongYeuCau;
                 newct.SoLuongThucXuat = ct.SoLuongThuc;
                 newct.DonGia          = ct.DonGia;
                 db.PhieuXuat_CT.Add(newct);
                 db.SaveChanges();
                 //Trừ vào số lượng tồn
                 IEnumerable <Model.SanPham> cnsp = from sp in db.SanPhams
                                                    where sp.MaSanPham == newct.MaSanPham
                                                    select sp;
                 Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                 sanpham.SoLuongTon = sanpham.SoLuongTon - ct.SoLuongThuc;
                 db.SaveChanges();
             }
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 //Sửa phiếu nhập
 public bool Update(PhieuNhap inp)
 {
     try
     {
         IEnumerable <Model.PhieuNhap> dspn = from pn in db.PhieuNhaps
                                              where pn.MaPhieuNhap == inp.MaPhieuNhap
                                              select pn;
         Model.PhieuNhap phieunhap = dspn.ElementAtOrDefault(0);
         if (phieunhap != null)
         {
             phieunhap.NhanVien = inp.MaNhanVien;
             phieunhap.NgayNhap = inp.NgayNhap;
             phieunhap.TongTien = inp.TongTien;
             db.SaveChanges();
             //Xoá chi tiết phiếu nhập cũ
             IEnumerable <Model.PhieuNhap_CT> dsctc = from ctc in db.PhieuNhap_CT
                                                      where ctc.MaPhieuNhap == inp.MaPhieuNhap
                                                      select ctc;
             if (dsctc.Count() > 0)
             {
                 foreach (Model.PhieuNhap_CT ctc in dsctc)
                 {
                     //trừ số lượng tồn
                     Model.QuanLiBanHangEntities db2  = new Model.QuanLiBanHangEntities();
                     IEnumerable <Model.SanPham> cnsp = from sp in db2.SanPhams
                                                        where sp.MaSanPham == ctc.MaSanPham
                                                        select sp;
                     Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                     sanpham.SoLuongTon = sanpham.SoLuongTon - ctc.SoLuong;
                     db2.SaveChanges();
                     //xoá chi tiết phiếu
                     db.PhieuNhap_CT.Remove(ctc);
                 }
                 db.SaveChanges();
             }
             //Nhập lại chi tiết phiếu
             foreach (PhieuNhapCT ct in inp.DSChiTiet)
             {
                 Model.PhieuNhap_CT newct = new Model.PhieuNhap_CT();
                 newct.MaPhieuNhap = ct.MaPhieuNhap;
                 newct.MaSanPham   = ct.MaSanPham;
                 newct.SoLuong     = ct.SoLuong;
                 newct.DonGia      = ct.DonGia;
                 db.PhieuNhap_CT.Add(newct);
                 db.SaveChanges();
                 //Cộng vào số lượng tồn
                 IEnumerable <Model.SanPham> cnsp = from sp in db.SanPhams
                                                    where sp.MaSanPham == newct.MaSanPham
                                                    select sp;
                 Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                 sanpham.SoLuongTon = sanpham.SoLuongTon + ct.SoLuong;
                 db.SaveChanges();
             }
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }