Ejemplo n.º 1
0
        public static bool add_HoaDonNhap(HoaDonNhap_View hd, List <CT_HoaDonNhap_View> ct_hds)
        {
            using (var transaction = Context.getInstance().db.Database.BeginTransaction())
            {
                try
                {
                    decimal phanTramLoiNhuanBanLe   = decimal.Parse(HeThong_DAL.getHeThongByMa("HT002").GiaTri);
                    decimal phanTramLoiNhuanBanBuon = decimal.Parse(HeThong_DAL.getHeThongByMa("HT003").GiaTri);

                    Context.getInstance().db.Entry(hd.toHoaDonNhap()).State = System.Data.Entity.EntityState.Added;
                    Context.getInstance().db.SaveChanges();
                    LINHKIEN lk;
                    ct_hds.ForEach(x =>
                    {
                        foreach (var seri in x.SoSeri)
                        {
                            Context.getInstance().db.Entry(x.toCT_HoaDonNhap(seri)).State = System.Data.Entity.EntityState.Added;
                            //nhap kho
                            KHO myK        = new KHO();
                            myK.MaLinhKien = x.MaLinhKien;
                            myK.Seri       = seri;
                            myK.NgayNhap   = hd.NgayLap;
                            myK.TrangThai  = 1;
                            Context.getInstance().db.Entry(myK).State = System.Data.Entity.EntityState.Added;
                        }
                        lk = Context.getInstance().db.LINHKIENs.Where(key => key.MaLinhKien == x.MaLinhKien).FirstOrDefault();
                        if (lk.GiaNhap != x.GiaNhap)
                        {
                            lk.GiaNhap  = x.GiaNhap;
                            lk.GiaBanLe = x.GiaNhap + (x.GiaNhap * phanTramLoiNhuanBanLe / 100);
                            lk.GiaBanSi = x.GiaNhap + (x.GiaNhap * phanTramLoiNhuanBanBuon / 100);
                            Context.getInstance().db.Entry(lk).State = System.Data.Entity.EntityState.Modified;
                        }
                    });
                    //update so tien nhap hang cua nha cc
                    NHACUNGCAP ncc = Context.getInstance().db.NHACUNGCAPs.Where(key => key.MaNhaCungCap == hd.MaNhaCungCap).FirstOrDefault();
                    ncc.Tong += hd.TongTien;
                    Context.getInstance().db.Entry(ncc).State = System.Data.Entity.EntityState.Modified;

                    Context.getInstance().db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Context.Refresh();
                    Console.WriteLine("ERROR--------------------------------------" + ex.Message);
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        public static bool del_HoaDon(HoaDonNhap_View hd, DataUpdate <CT_HOADON_NHAPHANG> list_Change)
        {
            using (var transaction = Context.getInstance().db.Database.BeginTransaction())
            {
                try
                {
                    hd.TrangThai = 0;
                    Context.getInstance().db.Entry(hd.toHoaDonNhap()).State = System.Data.Entity.EntityState.Modified;

                    //list_Change.Inserts.ForEach(x =>
                    //{
                    //});

                    //list_Change.Updates.ForEach(x =>
                    //{
                    //    x.TinhTrang = 1;
                    //    Context.getInstance().db.Entry(getCTHD(x)).State = System.Data.Entity.EntityState.Modified;
                    //});

                    list_Change.Deletes.ForEach(x =>
                    {
                        //set trang thai = 0 o day
                        x.TinhTrang = 0;
                        Context.getInstance().db.Entry(getCTHD(x)).State = System.Data.Entity.EntityState.Modified;

                        //them trong kho
                        KHO kho       = Context.getInstance().db.KHOes.Where(key => key.MaLinhKien == x.MaLinhKien).Where(k => k.Seri == x.Seri).FirstOrDefault();
                        kho.TrangThai = 0;
                        Context.getInstance().db.Entry(kho).State = System.Data.Entity.EntityState.Modified;
                    });

                    //update so tien nhap hang cua nha cung cap
                    NHACUNGCAP kh = Context.getInstance().db.NHACUNGCAPs.Where(key => key.MaNhaCungCap == hd.MaNhaCungCap).FirstOrDefault();
                    kh.Tong -= hd.TongTien;
                    Context.getInstance().db.Entry(kh).State = System.Data.Entity.EntityState.Modified;

                    Context.getInstance().db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Context.Refresh();
                    Console.WriteLine("ERROR--------------------------------------" + ex.Message);
                    return(false);
                }
            }
            return(true);
        }