public int XyLyHoanNhap(DatabaseHelper help, string mp)
        {
            try
            {
                var pxk = (from d in help.ent.Phieu_Xuat_Tam_Vat_Tu
                           where d.Ma_phieu_xuat_tam == mp
                           select d).FirstOrDefault();
                if (pxk == null)
                    return 0;

                var entryPointCTPN = (from d in help.ent.Chi_Tiet_Phieu_Xuat_Tam
                                      where d.Ma_phieu_xuat_tam == pxk.Ma_phieu_xuat_tam
                                      select d).ToList();
                if (entryPointCTPN.Count == 0)
                    return 1;

                for (int i = 0; i < entryPointCTPN.Count; i++)
                {
                    string mavattu = entryPointCTPN[i].Ma_vat_tu;
                    int idcl = (int)entryPointCTPN[i].Id_chat_luong;
                    string maphieu = entryPointCTPN[i].Ma_phieu_xuat_tam;
                    decimal sl = (decimal)entryPointCTPN[i].So_luong_thuc_xuat;
                    DateTime ngay_xuat = (DateTime)pxk.Ngay_xuat;
                    string dien_giai = pxk.Ly_do;

                        var entryPoint = (from d in help.ent.Ton_kho

                                          where d.ID_kho == pxk.ID_kho && d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                          select d).FirstOrDefault();

                        if (entryPoint == null)
                        {
                            return 0;
                        }
                        else
                        {

                            {
                                entryPoint.So_luong = entryPoint.So_luong + sl;
                                help.ent.Ton_kho.Attach(entryPoint);
                                help.ent.Entry(entryPoint).State = EntityState.Modified;
                                help.ent.SaveChanges();
                            }
                        }
                        var entryPointTK = (from d in help.ent.The_kho

                                            where d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                            select d).FirstOrDefault();

                        if (entryPointTK == null)
                        {
                            return 0;

                        }
                        Chi_tiet_the_kho cttk = new Chi_tiet_the_kho();
                        cttk.ID_The_Kho = entryPointTK.ID_The_Kho;
                        cttk.Ma_phieu = maphieu;
                        cttk.Ngay_xuat_chung_tu = ngay_xuat;
                        cttk.Dien_giai = dien_giai;
                        cttk.SL_Nhap = sl;

                        cttk.ID_loai_phieu_nhap = 0;

                        help.ent.Chi_tiet_the_kho.Add(cttk);
                        help.ent.SaveChanges();
                }
                pxk.Da_duyet = true;
                pxk.isHoanNhap = true;
                help.ent.Phieu_Xuat_Tam_Vat_Tu.Attach(pxk);
                help.ent.Entry(pxk).State = EntityState.Modified;
                help.ent.SaveChanges();

                return 1;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public override int Insert(int id, DatabaseHelper help)
        {
            try
               {
               {

                   var pnk = (from d in help.ent.Phieu_Nhap_Kho
                              where d.ID_phieu_nhap == id
                              select d).FirstOrDefault();
                   if (pnk == null)
                       return 0;

                   var entryPointCTPN = (from d in help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu
                                         where d.Ma_phieu_nhap == pnk.Ma_phieu_nhap
                                         select d).ToList();
                   if (entryPointCTPN.Count == 0)
                       return 1;
                   for (int i = 0; i < entryPointCTPN.Count; i++)
                   {
                       string mavattu = entryPointCTPN[i].Ma_vat_tu;
                       int idcl = (int)entryPointCTPN[i].Id_chat_luong;
                       string maphieu = entryPointCTPN[i].Ma_phieu_nhap;
                       decimal sl = (decimal)entryPointCTPN[i].So_luong_thuc_lanh;
                       DateTime ngay_xuat = (DateTime)pnk.Ngay_lap;
                        DateTime ngay_nhap_xuat = DateTime.Now;
                        string dien_giai = pnk.Ly_do;
                       var entryPoint = (from d in help.ent.Ton_kho

                                         where d.ID_kho == pnk.ID_kho && d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                         select d).FirstOrDefault();
                       if (entryPoint == null)
                       {
                           Ton_kho entTonKho = new Ton_kho();
                           entTonKho.ID_kho = pnk.ID_kho;
                           entTonKho.Ma_vat_tu = mavattu;
                           entTonKho.So_luong = sl;
                           entTonKho.Id_chat_luong = idcl;
                           help.ent.Ton_kho.Add(entTonKho);
                           help.ent.SaveChanges();
                       }
                       else
                       {
                           entryPoint.So_luong = entryPoint.So_luong + sl;
                           help.ent.Ton_kho.Attach(entryPoint);
                           help.ent.Entry(entryPoint).State = EntityState.Modified;
                           help.ent.SaveChanges();
                       }
                       var entryPointTK = (from d in help.ent.The_kho

                                           where d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                           select d).FirstOrDefault();

                        The_kho tk = new The_kho();
                        if (entryPointTK == null)
                       {

                           tk.Ma_vat_tu = mavattu;
                           tk.Dia_diem = "";
                           tk.Don_vi = "";
                           tk.Id_chat_luong = idcl;

                           help.ent.The_kho.Add(tk);
                           help.ent.SaveChanges();

                       }
                       Chi_tiet_the_kho cttk = new Chi_tiet_the_kho();
                       cttk.ID_The_Kho = tk.ID_The_Kho;
                       cttk.Ma_phieu = maphieu;
                        cttk.Ngay_nhap_xuat = ngay_nhap_xuat;
                       cttk.Ngay_xuat_chung_tu = ngay_xuat;
                       cttk.Dien_giai = dien_giai;
                       cttk.SL_Nhap = sl;

                       cttk.ID_loai_phieu_nhap = pnk.ID_Loai_Phieu_Nhap;

                       help.ent.Chi_tiet_the_kho.Add(cttk);
                       help.ent.SaveChanges();
                   }
                   pnk.Da_phan_kho = true;

                   help.ent.Phieu_Nhap_Kho.Attach(pnk);
                   help.ent.Entry(pnk).State = EntityState.Modified;
                   help.ent.SaveChanges();

                   return 1;
               }
               }
               catch (Exception ex)
               {
               return 0;
               }
        }
        public int TraNo(int id_phieu_xuat)
        {
            try
            {
                DatabaseHelper help = new DatabaseHelper();
                help.ConnectDatabase();
                using (var dbcxtransaction = help.ent.Database.BeginTransaction())
                {
                    var pxk = (from d in help.ent.Phieu_Xuat_Tam_Vat_Tu
                               where d.ID_phieu_xuat_tam == id_phieu_xuat
                               select d).FirstOrDefault();
                    if (pxk == null)
                        return 0;

                    var entryPointCTPN = (from d in help.ent.Chi_Tiet_Phieu_Xuat_Tam
                                          where d.Ma_phieu_xuat_tam == pxk.Ma_phieu_xuat_tam
                                          select d).ToList();

                    if (entryPointCTPN.Count == 0)
                        return 1;
                     var DMK = (from d in help.ent.DM_Kho
                                        where d.ID_kho == pxk.ID_kho
                                        select d).FirstOrDefault();

                     for (int i = 0; i < entryPointCTPN.Count; i++)
                     {
                         string mavattu = entryPointCTPN[i].Ma_vat_tu;
                         int idcl = (int)entryPointCTPN[i].Id_chat_luong;
                         string maphieu = entryPointCTPN[i].Ma_phieu_xuat_tam;
                         decimal sl = (decimal)entryPointCTPN[i].So_luong_giu_lai;
                         DateTime ngay_xuat = (DateTime)pxk.Ngay_xuat;

                         string dien_giai = pxk.Ly_do;
                         if (sl > 0)
                         {
                             var entryPoint = (from d in help.ent.Ton_kho

                                               where d.ID_kho == pxk.ID_kho && d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                               select d).FirstOrDefault();
                             if (entryPoint == null)
                                 return 0;
                             entryPoint.So_luong = entryPoint.So_luong + sl;
                             help.ent.Ton_kho.Attach(entryPoint);
                             help.ent.Entry(entryPoint).State = EntityState.Modified;
                             help.ent.SaveChanges();
                             if (DMK.isKhoNgoai == false|| DMK.isKhoNgoai ==null)
                             {

                                 Chi_tiet_the_kho ChitietTK = new Chi_tiet_the_kho();
                                 ChitietTK.ID_The_Kho = pxk.ID_kho;
                                 ChitietTK.Ma_phieu = maphieu;
                                 ChitietTK.Ngay_xuat_chung_tu = ngay_xuat;
                                 ChitietTK.Dien_giai = dien_giai;
                                 ChitietTK.SL_Nhap = sl;
                                 ChitietTK.Ghi_chu = "Nhân viên trả nợ vật tư";
                                 ChitietTK.ID_loai_phieu_nhap = 0;
                                 help.ent.Chi_tiet_the_kho.Add(ChitietTK);
                                 help.ent.SaveChanges();

                             }
                             entryPointCTPN[i].So_luong_giu_lai = 0;
                             help.ent.Chi_Tiet_Phieu_Xuat_Tam.Attach(entryPointCTPN[i]);
                             help.ent.Entry(entryPointCTPN[i]).State = EntityState.Modified;
                             help.ent.SaveChanges();
                         }

                         pxk.isGiuLai = false;
                         help.ent.Phieu_Xuat_Tam_Vat_Tu.Attach(pxk);
                         help.ent.Entry(pxk).State = EntityState.Modified;
                         help.ent.SaveChanges();
                         dbcxtransaction.Commit();
                         return 1;

                     }
                }
            }
            catch (Exception ex) {
                return 0;
            }
            return 0;
        }
        public int Insert(int id_phieu_xuat)
        {
            try
            {
                DatabaseHelper help = new DatabaseHelper();
                help.ConnectDatabase();
                using (var dbcxtransaction = help.ent.Database.BeginTransaction())
                {

                    var pxk = (from d in help.ent.Phieu_Xuat_Tam_Vat_Tu
                               where d.ID_phieu_xuat_tam == id_phieu_xuat
                               select d).FirstOrDefault();
                    if (pxk == null)
                        return 0;

                    var entryPointCTPN = (from d in help.ent.Chi_Tiet_Phieu_Xuat_Tam
                                          where d.Ma_phieu_xuat_tam == pxk.Ma_phieu_xuat_tam
                                          select d).ToList();
                    if (entryPointCTPN.Count == 0)
                        return 1;

                    var DMK = (from d in help.ent.DM_Kho
                                        where d.ID_kho == pxk.ID_kho
                                        select d).FirstOrDefault();

                    for (int i = 0; i < entryPointCTPN.Count; i++)
                    {
                        string mavattu = entryPointCTPN[i].Ma_vat_tu;
                        int idcl = (int)entryPointCTPN[i].Id_chat_luong;
                        string maphieu = entryPointCTPN[i].Ma_phieu_xuat_tam;
                        decimal sl = (decimal)entryPointCTPN[i].So_luong_thuc_xuat;
                        DateTime ngay_xuat = (DateTime)pxk.Ngay_xuat;
                        string dien_giai = pxk.Ly_do;
                        int id_kho_chinh = (int)pxk.ID_kho;
                        int id_kho_muon = (int)entryPointCTPN[i].ID_kho;
                        var entryPoint = (from d in help.ent.Ton_kho

                                          where d.ID_kho == pxk.ID_kho && d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                          select d).FirstOrDefault();
                        if (DMK.isKhoNgoai == true)
                        {
                            entryPoint.So_luong = entryPoint.So_luong - sl;
                            help.ent.Ton_kho.Attach(entryPoint);
                            help.ent.Entry(entryPoint).State = EntityState.Modified;
                            help.ent.SaveChanges();

                        }
                        else
                        {
                            var entryPointTK = (from d in help.ent.The_kho

                                                where d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                                select d).FirstOrDefault();
                            if (id_kho_chinh != id_kho_muon)
                            {
                                Kho_muon_vat_tu khomuon = new Kho_muon_vat_tu();
                                khomuon.ID_Kho = id_kho_chinh;
                                khomuon.ID_Kho_muon = id_kho_muon;
                                khomuon.Ma_vat_tu = mavattu;
                                khomuon.Id_chat_luong = idcl;
                                khomuon.So_luong = sl;
                                khomuon.Ma_phieu_xuat_tam = maphieu;
                                khomuon.Da_tra = false;
                                help.ent.Kho_muon_vat_tu.Add(khomuon);
                                help.ent.SaveChanges();
                                entryPoint.So_luong = entryPoint.So_luong + sl;
                                help.ent.Ton_kho.Attach(entryPoint);
                                help.ent.Entry(entryPoint).State = EntityState.Modified;
                                help.ent.SaveChanges();
                                Chi_tiet_the_kho cttk = new Chi_tiet_the_kho();
                                cttk.ID_The_Kho = entryPointTK.ID_The_Kho;
                                cttk.Ma_phieu = maphieu;
                                cttk.Ngay_xuat_chung_tu = ngay_xuat;
                                cttk.Dien_giai = "Mượn vật tư kho khác ";
                                cttk.SL_Nhap = sl;

                                cttk.ID_loai_phieu_nhap = 0;

                                help.ent.Chi_tiet_the_kho.Add(cttk);
                                help.ent.SaveChanges();
                            }

                            if (entryPoint == null)
                            {
                                return 0;
                            }
                            else
                            {
                                entryPoint.So_luong = entryPoint.So_luong - sl;
                                help.ent.Ton_kho.Attach(entryPoint);
                                help.ent.Entry(entryPoint).State = EntityState.Modified;
                                help.ent.SaveChanges();
                            }

                            if (entryPointTK == null)
                            {
                                return 0;

                            }

                            Chi_tiet_the_kho ChitietTK = new Chi_tiet_the_kho();
                            ChitietTK.ID_The_Kho = entryPointTK.ID_The_Kho;
                            ChitietTK.Ma_phieu = maphieu;
                            ChitietTK.Ngay_xuat_chung_tu = ngay_xuat;
                            ChitietTK.Dien_giai = dien_giai;
                            ChitietTK.SL_Xuat = sl;
                            ChitietTK.ID_loai_phieu_nhap = 0;
                            help.ent.Chi_tiet_the_kho.Add(ChitietTK);
                            help.ent.SaveChanges();
                        }
                    }
                    pxk.Da_duyet = true;

                    help.ent.Phieu_Xuat_Tam_Vat_Tu.Attach(pxk);
                    help.ent.Entry(pxk).State = EntityState.Modified;
                    help.ent.SaveChanges();
                    dbcxtransaction.Commit();
                    return 1;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
示例#5
0
        public override int Insert(int id)
        {
            DatabaseHelper help = new DatabaseHelper();
              help.ConnectDatabase();
              using (var dbcxtransaction = help.ent.Database.BeginTransaction())
              {

              var pnk = (from d in help.ent.Phieu_Nhap_Kho
                         where d.ID_phieu_nhap == id
                         select d).FirstOrDefault();
              if (pnk == null)
                  return 0;

              var entryPointCTPN = (from d in help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu
                                    where d.Ma_phieu_nhap == pnk.Ma_phieu_nhap
                                    select d).ToList();
              if (entryPointCTPN.Count == 0)
                  return 0;
              for (int i = 0; i < entryPointCTPN.Count; i++)
              {
                  string mavattu = entryPointCTPN[i].Ma_vat_tu;
                  int idcl = (int)entryPointCTPN[i].Id_chat_luong;
                  string maphieu = entryPointCTPN[i].Ma_phieu_nhap;
                  decimal sl = (decimal)entryPointCTPN[i].So_luong_thuc_lanh;
                  DateTime ngay_xuat = (DateTime)pnk.Ngay_lap;
                    DateTime ngay_nhap_xuat = DateTime.Now;
                    string dien_giai = pnk.Ly_do;
                  var entryPoint = (from d in help.ent.Ton_kho

                                    where d.ID_kho == pnk.ID_kho && d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                    select d).FirstOrDefault();
                  if (entryPoint == null || entryPoint.So_luong < sl)
                  {
                      return 0;
                  }
                  entryPoint.So_luong = entryPoint.So_luong - sl;
                  help.ent.Ton_kho.Attach(entryPoint);
                  help.ent.Entry(entryPoint).State = EntityState.Modified;
                  help.ent.SaveChanges();
                  var entryPointTK = (from d in help.ent.The_kho

                                      where d.Ma_vat_tu == mavattu && d.Id_chat_luong == idcl
                                      select d).FirstOrDefault();

                  if (entryPointTK == null)
                  {
                      return 0;
                  }

                  Chi_tiet_the_kho cttk = new Chi_tiet_the_kho();
                  cttk.ID_The_Kho = entryPointTK.ID_The_Kho;
                  cttk.Ma_phieu = maphieu;
                  cttk.Ngay_xuat_chung_tu = ngay_xuat;
                  cttk.Dien_giai = dien_giai;
                  cttk.SL_Xuat = sl;
                    cttk.Ngay_nhap_xuat = ngay_nhap_xuat;
                  cttk.ID_loai_phieu_nhap = pnk.ID_Loai_Phieu_Nhap;

                  help.ent.Chi_tiet_the_kho.Add(cttk);
                  help.ent.SaveChanges();
                  pnk.Da_phan_kho = true;
                  help.ent.Phieu_Nhap_Kho.Attach(pnk);
                  help.ent.Entry(pnk).State = EntityState.Modified;
                  help.ent.SaveChanges();
              }
              dbcxtransaction.Commit();
              }
              return base.Insert(id);
        }
        /// <summary>
        /// hàm insert tồn kho sử dụng transaction bên ngoài 
        /// hàm này dùng +,- số lượng với mã phiếu X
        /// sử dụng chung cho phiếu nhập và cả xuất liên quan đến các bảng ton kho
        /// chi tiet ton kho
        /// phieu nhap 
        /// chi tiet phieu nhap
        /// the kho
        /// chi tiet the kho
        /// </summary>
        /// <param name="mavt"></param>
        /// <param name="idkho"></param>
        /// <param name="soluong"></param>
        /// <param name="maphieu"></param>
        /// <param name="NgayNhap"></param>
        /// <param name="ID_chat_luong"></param>
        /// <returns></returns>
        public int InsertTonKho(DatabaseHelper help, string mavt, int idkho, decimal soluong, string maphieu, DateTime NgayNhap, int ID_chat_luong, bool LNP)
        {
            {
                try
                {
                    var entryPoint = (from d in help.ent.Ton_kho

                                      where d.ID_kho == idkho && d.Ma_vat_tu == mavt &&d.Id_chat_luong == ID_chat_luong
                                      select d).ToList();
                    #region "Thêm bảng tồn kho "
                    if (entryPoint.Count == 0)
                    {
                        //nếu không có trong bảng tồn kho mà có phiếu hoàn nhập thì thông báo không cho hoàn nhập
                        if (LNP == false)
                        {
                            return 0;
                        }
                        //neu chưa có tiền hành lưu vật tư này vào kho với số lượng bằng số lượng nhập
                        Ton_kho entTonKho = new Ton_kho();
                        entTonKho.ID_kho = idkho;
                        entTonKho.Ma_vat_tu = mavt;
                        entTonKho.So_luong = soluong;
                        entTonKho.Id_chat_luong = ID_chat_luong;
                        help.ent.Ton_kho.Add(entTonKho);
                        help.ent.SaveChanges();

                        ////buoc 2: them 1 dong vao ban chi tiet ton kho voi ma, so luong, kho tuong tung
                        //Chi_Tiet_Ton_Kho cttk = new Chi_Tiet_Ton_Kho();
                        //cttk.ID_Ton_kho = entTonKho.ID_ton_kho;
                        //cttk.Ma_phieu = maphieu;
                        //cttk.So_luong = soluong;
                        //cttk.Ngay_thay_doi = DateTime.Now;
                        //if(LNP == true)
                        //cttk.Tang_Giam = true;//<-- set tang
                        //else
                        //    cttk.Tang_Giam = false;//<-- set tang

                        //help.ent.Chi_Tiet_Ton_Kho.Add(cttk);
                        //help.ent.SaveChanges();
                        //buoc 3 : sau khi them vao bang chi tiet ton kho thi cap nhat lai trang thai phieu nhap
                        //Chi_Tiet_Phieu_Nhap_Vat_Tu ctpn = new Chi_Tiet_Phieu_Nhap_Vat_Tu();
                        //cập nhật trạng thái phiếu nhập
                        ////sau đó thêm 1 dòng vào trong thẻ kho
                        ////thêm vào chi tiết thẻ kho
                        //// tìm kiếm thẻ kho xem đã có thẻ kho có mã vật tư hay chưa ?
                        //// nếu có tiến hành thêm 1 dòng vào chi tiết thẻ kho
                        ////nếu không có thì tiến hành insert và trong bảng thẻ kho và chi tiết thẻ kho
                        //var entryPointTK = (from d in help.ent.The_kho

                        //                    where d.Ma_vat_tu == mavt&& d.Id_chat_luong == ID_chat_luong
                        //                    select d).ToList();
                        ////nếu chưa có trong thẻ kho
                        //if (entryPointTK.Count == 0)
                        //{
                        //    //tiến hành thêm 1 dong vao the kho và chi tiet the kho
                        //    The_kho tk = new The_kho();
                        //    tk.Ma_vat_tu = mavt;
                        //    tk.Dia_diem = idkho.ToString();
                        //    tk.Don_vi = idkho.ToString();
                        //    tk.Id_chat_luong = ID_chat_luong;
                        //    help.ent.The_kho.Add(tk);
                        //    help.ent.SaveChanges();

                        //    Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                        //    cttks.ID_The_Kho = tk.ID_The_Kho;
                        //    cttks.Ma_phieu = maphieu;
                        //    cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                        //    cttks.Dien_giai = entryPointPN[0].Ly_do;
                        //    cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                        //    cttks.Loai_phieu = true;
                        //    cttks.Ngay_nhap_xuat = NgayNhap;

                        //    help.ent.Chi_tiet_the_kho.Add(cttks);
                        //    help.ent.SaveChanges();

                        //}
                        //else
                        //{

                        //    //nếu đã có trong bảng thẻ kho thì tiến hành thêm vào bảng chi tiết thẻ kho 1 dòng mới
                        //    Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                        //    cttks.ID_The_Kho = entryPointTK[0].ID_The_Kho;
                        //    cttks.Ma_phieu = maphieu;
                        //    cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                        //    cttks.Dien_giai = entryPointPN[0].Ly_do;
                        //    cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                        //    cttks.Loai_phieu = true;
                        //    cttks.Ngay_nhap_xuat = NgayNhap;
                        //    help.ent.Chi_tiet_the_kho.Add(cttks);
                        //    help.ent.SaveChanges();

                        //}

                      //  return 1;
                    }
                    else
                    {
                        //neu kho đó đã có vật tư thì tiến hành cộng, hoac tru vật tư hiện tại vào kho

                        // entTonKho.So_luong = soluong;
                        //lay 1 dong ra
                        var stud = help.ent.Ton_kho.Where(s => s.ID_kho == idkho && s.Ma_vat_tu == mavt && s.Id_chat_luong == ID_chat_luong).FirstOrDefault<Ton_kho>();
                        if (LNP == true)
                        {
                            stud.So_luong = stud.So_luong + soluong;
                        }
                        else
                            stud.So_luong = stud.So_luong - soluong;
                        help.ent.Ton_kho.Attach(stud);
                        help.ent.Entry(stud).State = EntityState.Modified;//chinh sua so luong

                        //help.ent.Ton_kho.Attach(entTonKho);
                        //help.ent.Entry(entTonKho).State = EntityState.Modified;
                        help.ent.SaveChanges();

                        //buoc 2: them 1 dong vao ban chi tiet ton kho voi ma, so luong, kho tuong tung
                        //Chi_Tiet_Ton_Kho cttk = new Chi_Tiet_Ton_Kho();
                        //cttk.ID_Ton_kho = stud.ID_ton_kho;
                        //cttk.Ma_phieu = maphieu;
                        //cttk.So_luong = stud.So_luong;

                        //cttk.Ngay_thay_doi = DateTime.Now;
                        //if(LNP == true)
                        //    cttk.Tang_Giam = true;//<-- set tang
                        //else
                        //    cttk.Tang_Giam = false;
                        //help.ent.Chi_Tiet_Ton_Kho.Add(cttk);
                        //help.ent.SaveChanges();
                        //cập nhật lại trạng thái phiếu nhập
                        //var entryPointPN = (from d in help.ent.Phieu_Nhap_Kho

                        //                    where d.Ma_phieu_nhap == maphieu
                        //                    select d).ToList();
                        //if (entryPointPN[0].Da_phan_kho == false)
                        //{
                        //    entryPointPN[0].Da_phan_kho = true;
                        //    help.ent.Phieu_Nhap_Kho.Attach(entryPointPN[0]);
                        //    help.ent.Entry(entryPointPN[0]).State = EntityState.Modified;
                        //}

                        //help.ent.SaveChanges();
                        //buoc 3 : sau khi them vao bang chi tiet ton kho thi cap nhat lai trang thai phieu nhap

                        //Chi_Tiet_Phieu_Nhap_Vat_Tu ctpn = new Chi_Tiet_Phieu_Nhap_Vat_Tu();

                        //sau đó thêm 1 dòng vào trong thẻ kho
                        //thêm vào chi tiết thẻ kho
                        // tìm kiếm thẻ kho xem đã có thẻ kho có mã vật tư hay chưa ?
                        // nếu có tiến hành thêm 1 dòng vào chi tiết thẻ kho
                        //nếu không có thì tiến hành insert và trong bảng thẻ kho và chi tiết thẻ kho
                     //   dbcxtransaction.Commit();//hoan thanh thao tac

                    }
                    #endregion
                    #region "Cập nhật bảng chi tiết pn,ctpn"
                    var entryPointCT = (from d in help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu

                                        where d.Ma_vat_tu == mavt && d.Ma_phieu_nhap == maphieu && d.Id_chat_luong == ID_chat_luong
                                        select d).ToList();
                    foreach (var temp in entryPointCT)
                    {
                        temp.Da_duyet = true;

                        help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu.Attach(temp);
                        help.ent.Entry(temp).State = EntityState.Modified;
                    }
                    help.ent.SaveChanges();
                    var entryPointPN = (from d in help.ent.Phieu_Nhap_Kho

                                        where d.Ma_phieu_nhap == maphieu
                                        select d).ToList();
                    if (entryPointPN[0].Da_phan_kho == false)
                    {
                        entryPointPN[0].Da_phan_kho = true;
                        help.ent.Phieu_Nhap_Kho.Attach(entryPointPN[0]);
                        help.ent.Entry(entryPointPN[0]).State = EntityState.Modified;
                    }

                    help.ent.SaveChanges();
                    #endregion
                    #region "Xữ lý kho , thẻ kho"
                    var entryPointTK = (from d in help.ent.The_kho

                                        where d.Ma_vat_tu == mavt && d.Id_chat_luong == ID_chat_luong
                                        select d).ToList();
                    //nếu chưa có trong thẻ kho
                    if (entryPointTK.Count == 0)
                    {
                        //tiến hành thêm 1 dong vao the kho và chi tiet the kho
                        The_kho tk = new The_kho();
                        tk.Ma_vat_tu = mavt;
                        tk.Dia_diem = idkho.ToString();
                        tk.Don_vi = idkho.ToString();
                        tk.Id_chat_luong = ID_chat_luong;
                        help.ent.The_kho.Add(tk);
                        help.ent.SaveChanges();

                        Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                        cttks.ID_The_Kho = tk.ID_The_Kho;
                        cttks.Ma_phieu = maphieu;
                        cttks.Ngay_xuat_chung_tu = entryPointPN.First().Ngay_lap;
                        cttks.Dien_giai = entryPointPN.First().Ly_do;
                        if (LNP == true)
                        {
                            cttks.SL_Nhap = soluong;
                            cttks.Loai_phieu = true;
                        }
                        else
                        {
                            cttks.SL_Xuat = soluong; cttks.Loai_phieu = false;
                        }
                       // cttks.SL_Ton = soluong;// nếu đây là dòng đầu tiên trong danh sách thì số lượng tồn = sl thực lãnh

                        cttks.Ngay_nhap_xuat = NgayNhap;
                        help.ent.Chi_tiet_the_kho.Add(cttks);
                        help.ent.SaveChanges();

                    }
                    else
                    {

                        //nếu đã có trong bảng thẻ kho thì tiến hành thêm vào bảng chi tiết thẻ kho 1 dòng mới
                        Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                        cttks.ID_The_Kho = entryPointTK[0].ID_The_Kho;
                        cttks.Ma_phieu = maphieu;
                        cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                        cttks.Dien_giai = entryPointPN[0].Ly_do;
                        if(LNP ==true)
                            cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                        else
                            cttks.SL_Xuat = soluong;
                        //tìm kiếm số lượng tồn trước đó

                        //sln danh sách các thẻ kho có
                        //int id = entryPointTK.First().ID_The_Kho;
                        //var slt = (from d in help.ent.Chi_tiet_the_kho

                        //           where d.ID_The_Kho == id
                        //           orderby d.ID_chi_tiet_the_kho

                        //           select d).ToList().Last();

                        if (LNP == true)
                        {
                            //nếu phiếu nhập X
                            cttks.Loai_phieu = true;
                            //thì cộng vào t0ồn
                         //   cttks.SL_Ton = slt.SL_Ton + soluong;

                        }
                        else
                        {
                            cttks.Loai_phieu = false;
                        //    cttks.SL_Ton = slt.SL_Ton - soluong;

                        }
                        cttks.Ngay_nhap_xuat = NgayNhap;
                        help.ent.Chi_tiet_the_kho.Add(cttks);
                        help.ent.SaveChanges();

                    }
                    #endregion
                }
                catch (Exception ex)
                {
                 //   dbcxtransaction.Rollback();

                    return 0;

                }
            }
            return 1;
            //return 0;
        }
        /// <summary>
        /// hàm này bị sai tạm thời không dùng mà dùng hàm trên 
        /// hàm xữ lý thêm vật tư vào kho liên quan đến nhiều lớp, tính toán phức tạp
        /// </summary>
        /// <returns></returns>
        public int InsertTonKho(string mavt, int idkho, decimal soluong, string maphieu, DateTime NgayNhap, int ID_chat_luong)
        {
            DatabaseHelper help = new DatabaseHelper();
            help.ConnectDatabase();
            using (var dbcxtransaction = help.ent.Database.BeginTransaction())
            {
                try
                {
                    var entryPoint = (from d in help.ent.Ton_kho

                                      where d.ID_kho == idkho && d.Ma_vat_tu == mavt
                                      select d).ToList();
                    if (entryPoint.Count == 0)
                    {
                        //neu chưa có tiền hành lưu vật tư này vào kho với số lượng bằng số lượng nhập
                        Ton_kho entTonKho = new Ton_kho();
                        entTonKho.ID_kho = idkho;
                        entTonKho.Ma_vat_tu = mavt;
                        entTonKho.So_luong = soluong;
                        entTonKho.Id_chat_luong = ID_chat_luong;
                        help.ent.Ton_kho.Add(entTonKho);
                        help.ent.SaveChanges();

                        //buoc 2: them 1 dong vao ban chi tiet ton kho voi ma, so luong, kho tuong tung
                        //Chi_Tiet_Ton_Kho cttk = new Chi_Tiet_Ton_Kho();
                        //cttk.ID_Ton_kho = entTonKho.ID_ton_kho;
                        //cttk.Ma_phieu = maphieu;
                        //cttk.So_luong = soluong;
                        //cttk.Ngay_thay_doi = DateTime.Now;
                        //cttk.Tang_Giam = true;//<-- set tang
                        //help.ent.Chi_Tiet_Ton_Kho.Add(cttk);
                        //help.ent.SaveChanges();
                        //buoc 3 : sau khi them vao bang chi tiet ton kho thi cap nhat lai trang thai phieu nhap
                        //Chi_Tiet_Phieu_Nhap_Vat_Tu ctpn = new Chi_Tiet_Phieu_Nhap_Vat_Tu();
                        var entryPointCT = (from d in help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu

                                            where d.Ma_vat_tu == mavt && d.Ma_phieu_nhap == maphieu && d.Id_chat_luong == ID_chat_luong
                                            select d).ToList();
                        foreach (var temp in entryPointCT)
                        {
                            temp.Da_duyet = true;
                            help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu.Attach(temp);
                            help.ent.Entry(temp).State = EntityState.Modified;
                        }
                         help.ent.SaveChanges();
                        //cập nhật trạng thái phiếu nhập
                        var entryPointPN = (from d in help.ent.Phieu_Nhap_Kho

                                            where d.Ma_phieu_nhap == maphieu
                                            select d).ToList();
                        if (entryPointPN[0].Da_phan_kho == false)
                        {
                            entryPointPN[0].Da_phan_kho = true;
                            help.ent.Phieu_Nhap_Kho.Attach(entryPointPN[0]);
                            help.ent.Entry(entryPointPN[0]).State = EntityState.Modified;
                        }
                        help.ent.SaveChanges();

                        //sau đó thêm 1 dòng vào trong thẻ kho
                        //thêm vào chi tiết thẻ kho
                        // tìm kiếm thẻ kho xem đã có thẻ kho có mã vật tư hay chưa ?
                        // nếu có tiến hành thêm 1 dòng vào chi tiết thẻ kho
                        //nếu không có thì tiến hành insert và trong bảng thẻ kho và chi tiết thẻ kho
                        var entryPointTK = (from d in help.ent.The_kho

                                            where d.Ma_vat_tu == mavt
                                            select d).ToList();
                        //nếu chưa có trong thẻ kho
                        if (entryPointTK.Count == 0)
                        {
                            //tiến hành thêm 1 dong vao the kho và chi tiet the kho
                            The_kho tk = new The_kho();
                            tk.Ma_vat_tu = mavt;
                            tk.Dia_diem = idkho.ToString();
                            tk.Don_vi = idkho.ToString();
                            tk.Id_chat_luong = ID_chat_luong;
                            help.ent.The_kho.Add(tk);
                            help.ent.SaveChanges();

                            Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                            cttks.ID_The_Kho = tk.ID_The_Kho;
                            cttks.Ma_phieu = maphieu;
                            cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                            cttks.Dien_giai = entryPointPN[0].Ly_do;
                            cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                            cttks.Loai_phieu = true;
                            cttks.Ngay_nhap_xuat = NgayNhap;

                            help.ent.Chi_tiet_the_kho.Add(cttks);
                            help.ent.SaveChanges();

                        }
                        else
                        {

                            //nếu đã có trong bảng thẻ kho thì tiến hành thêm vào bảng chi tiết thẻ kho 1 dòng mới
                            Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                            cttks.ID_The_Kho = entryPointTK[0].ID_The_Kho;
                            cttks.Ma_phieu = maphieu;
                            cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                            cttks.Dien_giai = entryPointPN[0].Ly_do;
                            cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                            cttks.Loai_phieu = true;
                            cttks.Ngay_nhap_xuat = NgayNhap;
                            help.ent.Chi_tiet_the_kho.Add(cttks);
                            help.ent.SaveChanges();

                        }

                        dbcxtransaction.Commit();//hoan thanh thao tac
                        return 1;
                    }
                    else
                    {
                        //neu kho đó đã có vật tư thì tiến hành cộng vật tư hiện tại vào kho

                       // entTonKho.So_luong = soluong;
                        //lay 1 dong ra
                      var   stud =  help.ent.Ton_kho.Where(s => s.ID_kho == idkho &&s.Ma_vat_tu ==mavt).FirstOrDefault<Ton_kho>();

                      stud.So_luong = stud.So_luong + soluong;

                      help.ent.Ton_kho.Attach(stud);
                      help.ent.Entry(stud).State = EntityState.Modified;//chinh sua so luong

                        //help.ent.Ton_kho.Attach(entTonKho);
                        //help.ent.Entry(entTonKho).State = EntityState.Modified;
                        help.ent.SaveChanges();

                        //buoc 2: them 1 dong vao ban chi tiet ton kho voi ma, so luong, kho tuong tung
                        //Chi_Tiet_Ton_Kho cttk = new Chi_Tiet_Ton_Kho();
                        //cttk.ID_Ton_kho = stud.ID_ton_kho;
                        //cttk.Ma_phieu = maphieu;
                        //cttk.So_luong = stud.So_luong;

                        //cttk.Ngay_thay_doi = DateTime.Now;
                        //cttk.Tang_Giam = true;//<-- set tang
                        //help.ent.Chi_Tiet_Ton_Kho.Add(cttk);
                        //help.ent.SaveChanges();
                        //cập nhật lại trạng thái phiếu nhập
                        var entryPointPN = (from d in help.ent.Phieu_Nhap_Kho

                                            where d.Ma_phieu_nhap == maphieu
                                            select d).ToList();
                        if(entryPointPN[0].Da_phan_kho ==false)
                        {
                            entryPointPN[0].Da_phan_kho = true;
                            help.ent.Phieu_Nhap_Kho.Attach(entryPointPN[0]);
                            help.ent.Entry(entryPointPN[0]).State = EntityState.Modified;
                        }

                        help.ent.SaveChanges();
                        //buoc 3 : sau khi them vao bang chi tiet ton kho thi cap nhat lai trang thai phieu nhap

                        //Chi_Tiet_Phieu_Nhap_Vat_Tu ctpn = new Chi_Tiet_Phieu_Nhap_Vat_Tu();
                        var entryPointCT = (from d in help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu

                                            where d.Ma_vat_tu == mavt && d.Ma_phieu_nhap == maphieu && d.Id_chat_luong == ID_chat_luong
                                            select d).ToList();
                        foreach (var temp in entryPointCT)
                        {
                            temp.Da_duyet = true;

                            help.ent.Chi_Tiet_Phieu_Nhap_Vat_Tu.Attach(temp);
                            help.ent.Entry(temp).State = EntityState.Modified;
                        }
                        help.ent.SaveChanges();

                        //sau đó thêm 1 dòng vào trong thẻ kho
                        //thêm vào chi tiết thẻ kho
                        // tìm kiếm thẻ kho xem đã có thẻ kho có mã vật tư hay chưa ?
                        // nếu có tiến hành thêm 1 dòng vào chi tiết thẻ kho
                        //nếu không có thì tiến hành insert và trong bảng thẻ kho và chi tiết thẻ kho
                        var entryPointTK = (from d in help.ent.The_kho

                                            where d.Ma_vat_tu == mavt
                                            select d).ToList();
                        //nếu chưa có trong thẻ kho
                        if (entryPointTK.Count == 0)
                        {
                            //tiến hành thêm 1 dong vao the kho và chi tiet the kho
                            The_kho tk = new The_kho();
                            tk.Ma_vat_tu = mavt;
                            tk.Dia_diem = idkho.ToString();
                            tk.Don_vi = idkho.ToString();
                            tk.Id_chat_luong = ID_chat_luong;
                            help.ent.The_kho.Add(tk);
                            help.ent.SaveChanges();

                            Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                            cttks.ID_The_Kho = entryPointTK[0].ID_The_Kho;
                            cttks.Ma_phieu = maphieu;
                            cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                            cttks.Dien_giai = entryPointPN[0].Ly_do;
                            cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                            cttks.Loai_phieu = true;
                            cttks.Ngay_nhap_xuat = NgayNhap;
                            help.ent.Chi_tiet_the_kho.Add(cttks);
                            help.ent.SaveChanges();

                        }
                        else
                        {

                            //nếu đã có trong bảng thẻ kho thì tiến hành thêm vào bảng chi tiết thẻ kho 1 dòng mới
                            Chi_tiet_the_kho cttks = new Chi_tiet_the_kho();
                            cttks.ID_The_Kho = entryPointTK[0].ID_The_Kho;
                            cttks.Ma_phieu = maphieu;
                            cttks.Ngay_xuat_chung_tu = entryPointPN[0].Ngay_lap;
                            cttks.Dien_giai = entryPointPN[0].Ly_do;
                            cttks.SL_Nhap = entryPointCT[0].So_luong_thuc_lanh;
                            cttks.Loai_phieu = true;
                            cttks.Ngay_nhap_xuat = NgayNhap;
                            help.ent.Chi_tiet_the_kho.Add(cttks);
                            help.ent.SaveChanges();

                        }

                        dbcxtransaction.Commit();//hoan thanh thao tac
                        return 1;
                    }
                }
                catch (Exception ex)
                {
                    dbcxtransaction.Rollback();
                    return 0;

                }
            }
            return 1;
            //return 0;
        }