예제 #1
0
        public static bool update_HoaDon(HoaDon_View hd, List <CT_HoaDon_View> ct_hds)
        {
            using (var transaction = Context.getInstance().db.Database.BeginTransaction())
            {
                try
                {
                    Context.getInstance().db.Entry(hd.toHoaDon()).State = System.Data.Entity.EntityState.Modified;

                    ct_hds.ForEach(x =>
                    {
                        Context.getInstance().db.CT_HOADON.Add(x.toCT_HoaDon());
                    });

                    Context.getInstance().db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Context.getInstance().db.HOADONs.Local.Clear();
                    Context.getInstance().db.CT_HOADON.Local.Clear();
                    transaction.Rollback();
                    return(false);
                }
            }
            return(true);
        }
예제 #2
0
        private void InnitVal(string maHD)
        {
            //gridThaoTac = new GridHelper<CT_HoaDon_View>(gridControl1);
            HoaDon_View hd_view = HoaDon_DAL.get_HoaDon_By_MaHD(maHD);

            tong_tien_old = hd_view.TongTien;
            dt            = new DataUpdate <CT_HOADON>();

            kh_vanglai = KhachHang_DAL.get_KhachHang_VangLai();
            ls_cthd    = new List <CT_HoaDon_View>();

            if (maHD.Equals(""))
            {
                isNew             = true;
                hoadon            = new HoaDon_View();
                hoadon.NhanVien   = Context.getInstance().nv.TenNhanVien;
                hoadon.MaNhanVien = Context.getInstance().nv.MaNhanVien;
                hoadon.MaHoaDon   = HoaDon_DAL.get_HoaDonMax();
                hoadon.NgayLap    = DateTime.Now;
                hoadon.GhiChu     = "";
            }
            else
            {
                isNew  = false;
                hoadon = HoaDon_DAL.get_HoaDon_By_MaHD(maHD);
            }
            setDataLoad();
        }
예제 #3
0
        private void btn_Update_Click(object sender, EventArgs e)
        {
            HoaDon_View hd = gridView1.GetFocusedRow() as HoaDon_View;

            if (hd != null)
            {
                if (hd.Mode != TT.DELETE)
                {
                    var result = MessageBox.Show("Bạn có muốn xóa hóa đơn bán hàng " + hd.MaHoaDon + "?", "Lưu thông tin", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        DataUpdate <CT_HOADON> dt = new DataUpdate <CT_HOADON>();
                        foreach (var item in hd.ChiTietHoaDon)
                        {
                            dt.Deletes.AddRange(item.toList_CT_HoaDon());
                        }
                        hd.MaNhanVienSua = Context.getInstance().nv.MaNhanVien;
                        hd.NgaySua       = DateTime.Now;
                        if (HoaDon_DAL.del_HoaDon(hd, dt))
                        {
                            MessageBox.Show("Lưu thông tin thành công!");
                            gridThaoTac.Delete();
                            gridControl1.RefreshDataSource();
                        }
                        else
                        {
                            MessageBox.Show("Đã có lỗi xảy ra, vui lòng kiểm tra dữ liệu!");
                        }
                    }
                }
            }
        }
예제 #4
0
        // methods
        public List <HoaDon_View> To_View(List <HoaDon> list)
        {
            List <HoaDon_View> list_view = new List <HoaDon_View>();

            foreach (HoaDon hd in list)
            {
                HoaDon_View hd_view = new HoaDon_View();
                hd_view.ID      = hd.id;
                hd_view.Date    = hd.MakeDate;
                hd_view.Partner = hd.TenKhachHang;
                hd_view.Phone   = hd.Phone;
                hd_view.Staff   = hd.TenNhanVien;
                hd_view.CMND    = hd.CMND;
                if (hd.Type == 0)
                {
                    hd_view.Type = "Phiếu nhập hàng";
                }
                else
                {
                    hd_view.Type = "Hóa đơn bán hàng";
                }
                hd_view.Total = hd.TongTien;
                list_view.Add(hd_view);
            }
            return(list_view);
        }
예제 #5
0
        private void InnitVal(string maHD)
        {
            kh_vanglai         = KhachHang_DAL.get_KhachHang_VangLai();
            ls_cthd            = new List <CT_HoaDon_View>();
            dt                 = new DataUpdate <CT_HOADON>();
            txt_ChietKhau.Text = "0";

            if (maHD.Equals(""))
            {
                isNew                = true;
                hoadon               = new HoaDon_View();
                hoadon.NhanVien      = Context.getInstance().nv.TenNhanVien;
                hoadon.MaNhanVien    = Context.getInstance().nv.MaNhanVien;
                hoadon.NgaySua       = hoadon.NgayLap;
                hoadon.MaNhanVienSua = Context.getInstance().nv.MaNhanVien;
                hoadon.MaHoaDon      = HoaDon_DAL.get_HoaDonMax();
                hoadon.NgayLap       = DateTime.Now;
                hoadon.GhiChu        = "";
            }
            else
            {
                isNew  = false;
                hoadon = HoaDon_DAL.get_HoaDon_By_MaHD(maHD);
            }
            setDataLoad();
        }
예제 #6
0
        public static HoaDon_View get_HoaDon_By_MaHD(string maHD)
        {
            var hd = from hoadon in Context.getInstance().db.HOADONs
                     where hoadon.MaHoaDon == maHD
                     select new HoaDon_View
            {
                MaHoaDon       = hoadon.MaHoaDon,
                NgayLap        = hoadon.NgayLap,
                NhanVien       = hoadon.NHANVIEN1.TenNhanVien,
                MaNhanVien     = hoadon.MaNguoiLap,
                TongTien       = hoadon.TongTien,
                TongLoiNhuan   = hoadon.TongLoiNhuan,
                TrangThai      = hoadon.TrangThai,
                KhachHang      = hoadon.KHACHHANG.TenKhachHang,
                MaKhachHang    = hoadon.MaKhachHang,
                SoDienThoai    = hoadon.KHACHHANG.SoDienThoai,
                MaNhanVienSua  = hoadon.MaNguoiSua,
                TenNhanVienSua = hoadon.NHANVIEN.TenNhanVien,
                NgaySua        = (DateTime)hoadon.NgaySua,
                GhiChu         = hoadon.GhiChu
            };
            HoaDon_View hdV = hd.ToList()[0];

            hdV.ChiTietHoaDon = CT_HoaDon_DAL.get_CTHoaDon_By_MaHD_TT01(hdV.MaHoaDon);
            hdV.InitOldData();
            if (hdV.TrangThai == 0)
            {
                hdV.Mode = TT.DELETE;
            }
            return(hdV);
        }
예제 #7
0
        public static bool del_HoaDon(HoaDon_View hd, DataUpdate <CT_HOADON> list_Change)
        {
            using (var transaction = Context.getInstance().db.Database.BeginTransaction())
            {
                try
                {
                    hd.TrangThai = 0;
                    Context.getInstance().db.Entry(hd.toHoaDon()).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 = 1;
                        Context.getInstance().db.Entry(kho).State = System.Data.Entity.EntityState.Modified;
                    });


                    //update so tien mua hang cua khach hang
                    KHACHHANG kh = Context.getInstance().db.KHACHHANGs.Where(key => key.MaKhachHang == hd.MaKhachHang).FirstOrDefault();
                    kh.Tong -= hd.TongTien;
                    Context.getInstance().db.Entry(kh).State = System.Data.Entity.EntityState.Modified;

                    //update so luong ban hang cua nhan vien
                    NHANVIEN mNV = Context.getInstance().db.NHANVIENs.Where(key => key.MaNhanVien == hd.MaNhanVien).FirstOrDefault();
                    mNV.TongTien -= hd.TongTien;
                    Context.getInstance().db.Entry(mNV).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);
        }
예제 #8
0
        public static bool add_HoaDon(HoaDon_View hd, DataUpdate <CT_HOADON> list_Change)
        {
            using (var transaction = Context.getInstance().db.Database.BeginTransaction())
            {
                try
                {
                    hd.TrangThai = 1;
                    Context.getInstance().db.Entry(hd.toHoaDon()).State = System.Data.Entity.EntityState.Added;
                    list_Change.Inserts.ForEach(x =>
                    {
                        //set trang thai = 1 o day
                        x.TinhTrang = 1;
                        Context.getInstance().db.Entry(x).State = System.Data.Entity.EntityState.Added;

                        //xoa 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;

                        //chuyen trang thai cho hoa don nhap sang 2 la ko duoc xoa
                        List <HoaDonNhap_View> listHDN = HoaDonNhap_DAL.searchSeri(x.Seri);
                        if (listHDN != null)
                        {
                            foreach (var item in listHDN)
                            {
                                HOADON_NHAPHANG hdn = item.toHoaDonNhap();
                                if (hdn.TrangThai == 1)
                                {
                                    hdn.TrangThai = 2;
                                    Context.getInstance().db.Entry(hdn).State = System.Data.Entity.EntityState.Modified;
                                }
                            }
                        }
                    });

                    //update so tien mua hang cua khach hang
                    KHACHHANG kh = Context.getInstance().db.KHACHHANGs.Where(key => key.MaKhachHang == hd.MaKhachHang).FirstOrDefault();
                    kh.Tong += hd.TongTien;
                    Context.getInstance().db.Entry(kh).State = System.Data.Entity.EntityState.Modified;

                    //update so luong ban hang cua nhan vien
                    NHANVIEN mNV = Context.getInstance().db.NHANVIENs.Where(key => key.MaNhanVien == hd.MaNhanVien).FirstOrDefault();
                    mNV.TongTien += hd.TongTien;
                    Context.getInstance().db.Entry(mNV).State = System.Data.Entity.EntityState.Modified;

                    Context.getInstance().db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    //Context.getInstance().db.KHOes.Local.Clear();
                    //Context.getInstance().db.CT_HOADON.Local.Clear();
                    //Context.getInstance().db.KHACHHANGs.Local.Clear();
                    //Context.getInstance().db.NHANVIENs.Local.Clear();
                    Context.Refresh();
                    Console.WriteLine("ERROR--------------------------------------" + ex.Message);
                    return(false);
                }
            }
            return(true);
        }