private void reload_grid()
 {
     using (QuanLyKhoEntities db = new QuanLyKhoEntities())
     {
         m_bs_ht_quyen.DataSource = db.HT_QUYEN.OrderBy(x => x.RIBBON_PAGE_TEXT).ToList();
     }
 }
Пример #2
0
        public static void TinhLaiGiaThanh(QuanLyKhoEntities db, GD_SO_DU ip_sd, GD_CHI_TIET_GIAO_DICH ip_ctgd, bool isThemMoiSoDu, bool isXoaBanGhi)
        {
            //Luu y: Function nay chi tinh gia thanh cho giao dich: NHAP KHO hoac XUAT KHO
            GD_SO_DU v_gd_so_su_tai_thoi_diem = db.GD_SO_DU
                .Where(x => (x.ID_KHO == ip_ctgd.ID_KHO)
                                && x.ID_VAT_TU_HANG_HOA == ip_ctgd.ID_VAT_TU_HANG_HOA
                                && x.THOI_DIEM_PHAT_SINH <= ip_ctgd.GD_GIAO_DICH.NGAY_GIAO_DICH)
                .GroupBy(x => new { x.ID_VAT_TU_HANG_HOA, x.ID_KHO })
                .Select(x => x.OrderByDescending(y => y.THOI_DIEM_PHAT_SINH).FirstOrDefault())
                .FirstOrDefault();
            decimal v_dc_so_luong = SoDu.quy_doi_so_luong(ip_ctgd.ID_VAT_TU_HANG_HOA ?? -1, ip_ctgd.ID_DON_VI_TINH ?? -1, ip_ctgd.DM_VAT_TU_HANG_HOA.ID_DON_VI_TINH_CO_SO ?? -1, ip_ctgd.SO_LUONG ?? 0);
            if (isThemMoiSoDu)
            {
                ip_sd.ID_CHI_TIET_GIAO_DICH = ip_ctgd.ID;
                ip_sd.ID_DON_VI_TINH = ip_ctgd.DM_VAT_TU_HANG_HOA.ID_DON_VI_TINH_CO_SO ?? -1;
                ip_sd.ID_KHO = ip_ctgd.ID_KHO ?? -1;
                ip_sd.ID_VAT_TU_HANG_HOA = ip_ctgd.ID_VAT_TU_HANG_HOA ?? -1;
                ip_sd.THOI_DIEM_PHAT_SINH = ip_ctgd.GD_GIAO_DICH.NGAY_GIAO_DICH;
                ip_sd.SO_LUONG = SoDu.quy_doi_so_luong(ip_ctgd.ID_VAT_TU_HANG_HOA??-1, ip_ctgd.ID_DON_VI_TINH??-1, ip_ctgd.DM_VAT_TU_HANG_HOA.ID_DON_VI_TINH_CO_SO??-1, ip_ctgd.SO_LUONG??0);
            }

            //Cap nhat gia thanh
            if (v_gd_so_su_tai_thoi_diem == null)
            {

                ip_sd.GIA_THANH = ip_ctgd.GIA_THANH ?? 0;
            }
            else
            {
                if (ip_ctgd.GD_GIAO_DICH.ID_LOAI_GIAO_DICH == LOAI_GIAO_DICH.NHAP_KHO)
                {
                    ip_sd.SO_LUONG = v_gd_so_su_tai_thoi_diem.SO_LUONG + v_dc_so_luong;
                    ip_sd.GIA_THANH = (Int32)((v_gd_so_su_tai_thoi_diem.SO_LUONG * v_gd_so_su_tai_thoi_diem.GIA_THANH + (ip_ctgd.GIA_THANH ?? 0) * ip_sd.SO_LUONG) / (v_gd_so_su_tai_thoi_diem.SO_LUONG + ip_sd.SO_LUONG));
                }
                else if (ip_ctgd.GD_GIAO_DICH.ID_LOAI_GIAO_DICH == LOAI_GIAO_DICH.XUAT_KHO)
                {
                    ip_sd.SO_LUONG = v_gd_so_su_tai_thoi_diem.SO_LUONG - v_dc_so_luong;
                    ip_sd.GIA_THANH = v_gd_so_su_tai_thoi_diem.GIA_THANH;
                }

            }
            if (isThemMoiSoDu)
            {
                db.GD_SO_DU.Add(ip_sd);
            }
            if (isXoaBanGhi)
            {
                db.GD_SO_DU.Remove(ip_sd);
            }
            db.SaveChanges();
        }
Пример #3
0
        public static GD_SO_DU getRecordTonKho(QuanLyKhoEntities db
            , decimal ip_dc_id_vat_tu_hang_hoa
            , decimal ip_dc_id_kho
            , DateTime ip_dat_ngay_giao_dich)
        {
            GD_SO_DU v_gd_so_su_tai_thoi_diem = db.GD_SO_DU
                .Where(x => (x.ID_KHO == ip_dc_id_kho || ip_dc_id_kho == -1)
                                && x.ID_VAT_TU_HANG_HOA == ip_dc_id_vat_tu_hang_hoa
                                && x.THOI_DIEM_PHAT_SINH <= ip_dat_ngay_giao_dich)
                .GroupBy(x => new { x.ID_VAT_TU_HANG_HOA, x.ID_KHO })
                .Select(x => x.OrderByDescending(y => y.THOI_DIEM_PHAT_SINH).FirstOrDefault())
                .FirstOrDefault();

            return v_gd_so_su_tai_thoi_diem;
        }
Пример #4
0
 public static void InitialUser(decimal ip_dc_id )
 {
     using(QuanLyKhoEntities db=new QuanLyKhoEntities())
     {
         var ht_nsd=db.HT_NGUOI_SU_DUNG.Where(x=>x.ID==ip_dc_id).FirstOrDefault();
         m_ht_nguoi_su_dung=new HT_NGUOI_SU_DUNG()
         {
             ID=ht_nsd.ID
             , ID_CO_CAU_CO_CHUC=ht_nsd.ID_CO_CAU_CO_CHUC
             , DANG_SU_DUNG_YN=ht_nsd.DANG_SU_DUNG_YN
             , MAT_KHAU=ht_nsd.MAT_KHAU
             , TEN_TRUY_CAP=ht_nsd.TEN_TRUY_CAP
         };
     }
 }
Пример #5
0
 public static decimal TinhGiaThanhXuatKho(QuanLyKhoEntities db, DateTime ip_dat_thoi_diem_phat_sinh, decimal ip_dc_id_vat_tu_hang_hoa, decimal ip_dc_id_kho)
 {
     GD_SO_DU v_gd_so_su_tai_thoi_diem = db.GD_SO_DU
         .Where(x => (x.ID_KHO == ip_dc_id_kho)
                         && x.ID_VAT_TU_HANG_HOA == ip_dc_id_vat_tu_hang_hoa
                         && x.THOI_DIEM_PHAT_SINH <= ip_dat_thoi_diem_phat_sinh)
         .GroupBy(x => new { x.ID_VAT_TU_HANG_HOA, x.ID_KHO })
         .Select(x => x.OrderByDescending(y => y.THOI_DIEM_PHAT_SINH).FirstOrDefault())
         .FirstOrDefault();
     if (v_gd_so_su_tai_thoi_diem == null)
     {
         return 0;
     }
     return v_gd_so_su_tai_thoi_diem.GIA_THANH;
 }
Пример #6
0
 public static decimal getGiaTriXuat(QuanLyKhoEntities db
     , decimal ip_dc_id_vat_tu_hang_hoa
     , decimal ip_dc_id_kho
     , DateTime ip_dat_tu_ngay
     , DateTime ip_dat_den_ngay)
 {
     var v_lst_gd = db.GD_CHI_TIET_GIAO_DICH
         .Where(x => (x.ID_KHO == ip_dc_id_kho || ip_dc_id_kho == -1)
                         && x.ID_VAT_TU_HANG_HOA == ip_dc_id_vat_tu_hang_hoa
                         && x.GD_GIAO_DICH.NGAY_GIAO_DICH >= ip_dat_tu_ngay
                         && x.GD_GIAO_DICH.NGAY_GIAO_DICH <= ip_dat_den_ngay
                         && x.GD_GIAO_DICH.ID_LOAI_GIAO_DICH == LOAI_GIAO_DICH.XUAT_KHO)
         .ToList();
     decimal v_dc_gia_tri = 0;
     if (v_lst_gd != null)
     {
         foreach (var ctgd in v_lst_gd)
         {
             v_dc_gia_tri += quy_doi_so_luong(ctgd.ID_VAT_TU_HANG_HOA ?? -1, ctgd.ID_DON_VI_TINH ?? -1, ctgd.DM_VAT_TU_HANG_HOA.ID_DON_VI_TINH_CO_SO ?? -1, ctgd.SO_LUONG ?? 0) * ctgd.GIA_THANH ?? 0;
         }
     }
     return v_dc_gia_tri;
 }
        private void update_menu()
        {
            F001_Main v_frm = new F001_Main();
            DevExpress.XtraBars.Ribbon.RibbonControl m_rc_menu = v_frm.m_rc_menu;
            List<HT_QUYEN> v_lst_menu = new List<HT_QUYEN>();
            //1. Get list menu
            //Level RibbonPage
            foreach (DevExpress.XtraBars.Ribbon.RibbonPage rp in m_rc_menu.Pages)
            {
                //Level RibbonPageGroup
                foreach (DevExpress.XtraBars.Ribbon.RibbonPageGroup rpg in rp.Groups)
                {
                    //Level BarButtonItem
                    foreach (DevExpress.XtraBars.BarItemLink bbl in rpg.ItemLinks)
                    {
                        HT_QUYEN v_q = new HT_QUYEN()
                        {
                            RIBBON_PAGE_NAME = rp.Name
                            ,
                            RIBBON_PAGE_TEXT = rp.Text
                            ,
                            RIBBON_PAGE_GROUP_NAME = rpg.Name
                            ,
                            RIBBON_PAGE_GROUP_TEXT = rpg.Text
                            ,
                            BAR_BUTTON_ITEM_NAME = bbl.Item.Name
                            ,
                            BAR_BUTTON_ITEM_CAPTION = bbl.Item.Caption
                        };
                        v_lst_menu.Add(v_q);
                    }
                }
            }
            //2. Update database
            using (QuanLyKhoEntities db = new QuanLyKhoEntities())
            {

                List<HT_QUYEN> v_lst_ht_quyen = db.HT_QUYEN.ToList();
                //2.1 Xoa bo di menu trong database ma khong co trong form
                foreach (var q in v_lst_ht_quyen)
                {
                    HT_QUYEN v_q = v_lst_menu.Where(x => x.RIBBON_PAGE_GROUP_NAME == q.RIBBON_PAGE_GROUP_NAME
                        && x.RIBBON_PAGE_NAME == q.RIBBON_PAGE_NAME
                        && x.BAR_BUTTON_ITEM_NAME == q.BAR_BUTTON_ITEM_NAME)
                        .FirstOrDefault();
                    //Neu khong tim thay menu trong form thi xoa menu trong database
                    if (v_q == null)
                    {
                        //Remove ht_phan_quyen
                        List<HT_PHAN_QUYEN> v_lst_phan_quyen = db.HT_PHAN_QUYEN
                            .Where(x => x.ID_QUYEN == q.ID)
                            .ToList();
                        foreach (var pq in v_lst_phan_quyen)
                        {
                            db.HT_PHAN_QUYEN.Remove(pq);
                            db.SaveChanges();
                        }
                        //Remove ht_quyen
                        db.HT_QUYEN.Remove(q);
                        db.SaveChanges();
                        v_lst_ht_quyen = db.HT_QUYEN.ToList();
                    }
                    else
                    {
                        //Neu tim thay thi cap nhat lai caption, text
                        q.RIBBON_PAGE_TEXT = v_q.RIBBON_PAGE_TEXT;
                        q.RIBBON_PAGE_GROUP_TEXT = v_q.RIBBON_PAGE_GROUP_TEXT;
                        q.BAR_BUTTON_ITEM_CAPTION = v_q.BAR_BUTTON_ITEM_CAPTION;
                        db.SaveChanges();
                    }
                }
                v_lst_ht_quyen = db.HT_QUYEN.ToList();
                //2.2 Them vao database nhung menu co trong form nhung chua co trong database
                foreach (var q in v_lst_menu)
                {
                    if (!v_lst_ht_quyen.Exists(x => x.RIBBON_PAGE_GROUP_NAME == q.RIBBON_PAGE_GROUP_NAME
                        && x.RIBBON_PAGE_NAME == q.RIBBON_PAGE_NAME
                        && x.BAR_BUTTON_ITEM_NAME == q.BAR_BUTTON_ITEM_NAME))
                    {
                        db.HT_QUYEN.Add(q);
                        db.SaveChanges();
                        v_lst_ht_quyen = db.HT_QUYEN.ToList();
                    }
                }
            }
        }
Пример #8
0
        public static decimal quy_doi_so_luong(
            decimal ip_dc_id_vat_tu_hang_hoa
            , decimal? ip_dc_id_don_vi_dan_xuat
            , decimal? ip_dc_id_don_vi_co_so
            , decimal ip_dc_so_luong)
        {
            QuanLyKhoEntities db = new QuanLyKhoEntities();
            decimal v_dc_output = ip_dc_so_luong;
            if (ip_dc_id_don_vi_dan_xuat == ip_dc_id_don_vi_co_so) return ip_dc_so_luong;
            var v_lst_don_vi_quy_doi = db.GD_DON_VI_QUY_DOI
                .Where(x => x.ID_VAT_TU_HANG_HOA == ip_dc_id_vat_tu_hang_hoa)
                .ToList();
            GD_DON_VI_QUY_DOI dan_xuat = v_lst_don_vi_quy_doi
                .Where(x => x.ID_DON_VI_TINH_DAN_XUAT == ip_dc_id_don_vi_dan_xuat
                    && x.ID_DON_VI_TINH_DAN_XUAT != x.ID_DON_VI_CO_SO)
                .FirstOrDefault();
            if (dan_xuat == null)
            {
                dan_xuat = v_lst_don_vi_quy_doi
                .Where(x => x.ID_DON_VI_CO_SO == ip_dc_id_don_vi_dan_xuat
                    && x.ID_DON_VI_TINH_DAN_XUAT != x.ID_DON_VI_CO_SO)
                .FirstOrDefault();
                if (dan_xuat == null) return v_dc_output;
                else
                {

                    v_dc_output /= dan_xuat.HE_SO_QUY_DOI ?? 1;
                    return v_dc_output;
                }
            }
            else
            {
                v_dc_output *= dan_xuat.HE_SO_QUY_DOI ?? 1;
            }
            while (dan_xuat.ID_DON_VI_CO_SO != ip_dc_id_don_vi_co_so
                && dan_xuat.ID_DON_VI_TINH_DAN_XUAT != ip_dc_id_don_vi_co_so)
            {
                //Truong hop tim duoc don vi quy doi thuan: 1m=10dm, 1dm=10cm, 1cm=10mm => 1m=10*10*10 mm
                if (v_lst_don_vi_quy_doi.Count(x => x.ID_DON_VI_TINH_DAN_XUAT == dan_xuat.ID_DON_VI_CO_SO
                    && x.ID != dan_xuat.ID) > 0)
                {
                    dan_xuat = v_lst_don_vi_quy_doi
                        .Where(x => x.ID_DON_VI_TINH_DAN_XUAT == dan_xuat.ID_DON_VI_CO_SO
                            && x.ID != dan_xuat.ID)
                        .FirstOrDefault();
                    if (dan_xuat == null) break;
                    v_dc_output *= dan_xuat.HE_SO_QUY_DOI ?? 1;

                }
                else
                {
                    dan_xuat = v_lst_don_vi_quy_doi
                        .Where(x => x.ID_DON_VI_CO_SO == dan_xuat.ID_DON_VI_CO_SO
                            && x.ID != dan_xuat.ID)
                        .FirstOrDefault();
                    if (dan_xuat == null) break;
                    v_dc_output /= dan_xuat.HE_SO_QUY_DOI ?? 1;
                }
            }

            if (dan_xuat.ID_DON_VI_CO_SO != ip_dc_id_don_vi_co_so
                && dan_xuat.ID_DON_VI_TINH_DAN_XUAT != ip_dc_id_don_vi_co_so)
            {
                MessageBox.Show("Bộ đơn vị quy đổi bị lỗi, bạn không thể thực hiện được thao tác này, bạn cần phải cấu hình lại bộ quản đơn vị quy đổi của Vật tư, hàng hoá trước khi thực hiện giao dịch", "Lỗi");
                return -1;
            }

            return v_dc_output;
        }
Пример #9
0
        public Test()
        {
            InitializeComponent();
            //define_events();
            // This line of code is generated by Data Source Configuration Wizard
            // Instantiate a new DBContext
            SqlDataAccess.QuanLyKhoEntities db = new SqlDataAccess.QuanLyKhoEntities();
            m_bs_gd_chi_tiet_giao_dich.DataSource = db.GD_CHI_TIET_GIAO_DICH.ToList();

            var v_lst_giao_dich = db.GD_GIAO_DICH
                .Select(x => new
                {
                    x.ID
                    ,
                    x.MA_GIAO_DICH
                    ,
                    x.GHI_CHU
                }).ToList();
            var v_lst_kho = db.DM_KHO
                .Select(x => new
                {
                    x.ID
                    ,
                    x.MA_KHO
                    ,
                    x.TEN_KHO
                    ,
                    x.DIA_CHI
                    ,
                    x.GHI_CHU
                }).ToList();
            var v_lst_loai_tien = db.DM_DON_VI_TINH.Where(x => x.ID_LOAI_DON_VI_TINH == LOAI_DON_VI_TINH.DON_VI_TIEN_TE)
                .Select(x => new
                {
                    x.ID
                    ,
                    x.MA_DON_VI_TINH
                    ,
                    x.TEN
                    ,
                    x.GHI_CHU
                }).ToList();
            var v_lst_vat_tu_hang_hoa = db.DM_VAT_TU_HANG_HOA
                .Select(x => new
                {
                    x.ID
                    ,
                    x.MA_VAT_TU_HANG_HOA
                    ,
                    x.TEN_VAT_TU_HANG_HOA
                }).ToList();
            //Giao dich
            col_slue_giao_dich.DataSource = v_lst_giao_dich;
            col_slue_giao_dich.DisplayMember = "MA_GIAO_DICH";
            col_slue_giao_dich.ValueMember = "ID";
            col_slue_giao_dich.NullText = "Chọn giao dịch";
            col_slue_giao_dich.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            col_slue_giao_dich.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;
            col_sluev_giao_dich.ShowFindPanel();
            //Kho
            col_slue_kho.DataSource = v_lst_kho;
            col_slue_kho.DisplayMember = "TEN_KHO";
            col_slue_kho.ValueMember = "ID";
            col_slue_kho.NullText = "Chọn kho";
            col_slue_kho.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            col_sluev_kho.ShowFindPanel();
            //Vat tu, hang hoa
            col_slue_vat_tu_hang_hoa.DataSource = v_lst_vat_tu_hang_hoa;
            col_slue_vat_tu_hang_hoa.DisplayMember = "TEN_VAT_TU_HANG_HOA";
            col_slue_vat_tu_hang_hoa.ValueMember = "ID";
            col_slue_vat_tu_hang_hoa.NullText = "Chọn vật tư, hàng hoá";
            col_slue_vat_tu_hang_hoa.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            col_sluev_vat_tu_hang_hoa.ShowFindPanel();
            //Loai tien
            col_slue_loai_tien.DataSource = v_lst_loai_tien;
            col_slue_loai_tien.DisplayMember = "TEN";
            col_slue_loai_tien.ValueMember = "ID";
            col_slue_loai_tien.NullText = "Chọn loại tiền";
            col_slue_loai_tien.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            col_sluev_loai_tien.ShowFindPanel();

            m_gv.OptionsView.ColumnAutoWidth = false;
            m_gv.BestFitColumns();

            //colGridViewGiaoDich.Columns["ID"].Visible = false;
            //colGridViewKho.Columns["ID"].Visible = false;
            //colGridViewLoaiTien.Columns["ID"].Visible = false;
            //colGridViewVatTuHangHoa.Columns["ID"].Visible = false;
        }
Пример #10
0
 private void m_cmd_dang_nhap_Click(object sender, EventArgs e)
 {
     using(QuanLyKhoEntities db=new QuanLyKhoEntities())
     {
         var nsd = db.HT_NGUOI_SU_DUNG
             .Where(x => x.TEN_TRUY_CAP == m_txt_ten_dang_nhap.Text
                 && x.MAT_KHAU == m_txt_mat_khau.Text)
             .FirstOrDefault();
         if (nsd!=null)
         {
             Current_User.InitialUser(nsd.ID);
             QuanLyKho.F001_Main v_frm = new F001_Main();
             this.Hide();
             v_frm.display_main_form(this);
         }
         else
         {
             MessageBox.Show("Tên truy cập hoặc mật khẩu không đúng!", "Cảnh báo", MessageBoxButtons.OK);
             m_txt_ten_dang_nhap.Focus();
         }
     }
 }
Пример #11
0
        private void lap_phieu_xu_ly_kiem_ke(decimal ip_dc_id_kiem_ke_kho, LOAI_XU_LY_CHENH_LECH ip_loai_xu_ly_chenh_lech)
        {
            using (var trans = db.Database.BeginTransaction())
            {
                try
                {
                    var kk = db.GD_KIEM_KE_KHO.Where(x => x.ID == ip_dc_id_kiem_ke_kho).FirstOrDefault();
                    //Hien tai he thong chi xu ly duoc 1 loai tien te la VND;
                    var v_tien_te = db.DM_DON_VI_TINH.Where(x => x.ID_LOAI_DON_VI_TINH == LOAI_DON_VI_TINH.DON_VI_TIEN_TE).FirstOrDefault();

                    //Them moi Phieu nhap kho Nguyen vat lieu do dang
                    GD_GIAO_DICH v_gd = new GD_GIAO_DICH();
                    decimal v_dc_id_loai_giao_dich = QuanLyKho.LOAI_GIAO_DICH.NHAP_KHO;
                    decimal v_dc_id_loai_chung_tu = CHUNG_TU_NHAP_KHO.KHAC;
                    if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.XUAT_KHO)
                    {
                        v_dc_id_loai_chung_tu = CHUNG_TU_XUAT_KHO.KHAC;
                        v_dc_id_loai_giao_dich = QuanLyKho.LOAI_GIAO_DICH.XUAT_KHO;
                    }

                    v_gd.ID_LOAI_GIAO_DICH = v_dc_id_loai_giao_dich;
                    v_gd.ID_LOAI_CHUNG_TU = v_dc_id_loai_chung_tu;
                    DateTime v_dat_tu_ngay = (kk.NGAY_THANG ?? DateTime.Now).Date;
                    DateTime v_dat_den_ngay = v_dat_tu_ngay.AddHours(23);
                    var v_lst_giao_dich = db.GD_GIAO_DICH.Where(x => x.ID_LOAI_GIAO_DICH == v_dc_id_loai_giao_dich
                                                && x.ID_LOAI_CHUNG_TU == v_dc_id_loai_chung_tu
                                                && x.NGAY_GIAO_DICH > v_dat_tu_ngay
                                                && x.NGAY_GIAO_DICH < v_dat_den_ngay)
                                            .ToList();
                    v_gd.MA_GIAO_DICH = F301_Giao_dich.gen_ma_giao_dich(v_dc_id_loai_giao_dich, v_dc_id_loai_chung_tu, (kk.NGAY_THANG ?? DateTime.Now).Date, v_lst_giao_dich);
                    v_gd.NGAY_GIAO_DICH = kk.NGAY_THANG ?? DateTime.Now;
                    v_gd.GHI_CHU = "Xử lý chênh lệnh biên bản kiểm kê " + kk.MA_SO;
                    db.GD_GIAO_DICH.Add(v_gd);
                    db.SaveChanges();
                    //Tao list vat lieu cua Phieu nhap kho Nguyen vat lieu do dang
                    List<GD_CHI_TIET_KIEM_KE_KHO> v_lst_ctkk = new List<GD_CHI_TIET_KIEM_KE_KHO>();
                    if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.XUAT_KHO)
                    {
                        v_lst_ctkk = db.GD_CHI_TIET_KIEM_KE_KHO
                        .Where(x => x.ID_KIEM_KE_KHO == ip_dc_id_kiem_ke_kho
                        && x.SO_LUONG_THEO_KIEM_KE < x.SO_LUONG_THEO_SO_KE_TOAN).
                        ToList();
                    }
                    else if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.NHAP_KHO)
                    {
                        v_lst_ctkk = db.GD_CHI_TIET_KIEM_KE_KHO
                        .Where(x => x.ID_KIEM_KE_KHO == ip_dc_id_kiem_ke_kho
                        && x.SO_LUONG_THEO_KIEM_KE > x.SO_LUONG_THEO_SO_KE_TOAN).
                        ToList();
                    }
                    List<QuanLyKho.NghiepVu.U301_CHI_TIET_NGHIEP_VU.GridU301> v_lst_item = new List<QuanLyKho.NghiepVu.U301_CHI_TIET_NGHIEP_VU.GridU301>();
                    foreach (var ctkk in v_lst_ctkk)
                    {
                        QuanLyKho.NghiepVu.U301_CHI_TIET_NGHIEP_VU.GridU301 v_item = new QuanLyKho.NghiepVu.U301_CHI_TIET_NGHIEP_VU.GridU301();
                        v_item.ID_DON_VI_TINH = ctkk.ID_DON_VI_TINH;
                        v_item.ID_GIAO_DICH = v_gd.ID;
                        v_item.ID_KHO = ctkk.ID_KHO;
                        var v_so_du = NghiepVu.SoDu.getRecordTonKho(db, ctkk.ID_VAT_TU_HANG_HOA ?? -1, ctkk.ID_KHO ?? -1, v_gd.NGAY_GIAO_DICH);

                        v_item.ID_LOAI_TIEN_TE = v_tien_te.ID;

                        v_item.ID_VAT_TU_HANG_HOA = ctkk.ID_VAT_TU_HANG_HOA ?? -1;
                        if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.NHAP_KHO)
                        {
                            v_item.SO_LUONG = (ctkk.SO_LUONG_THEO_KIEM_KE - ctkk.SO_LUONG_THEO_SO_KE_TOAN) ?? 0;
                        }
                        else if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.XUAT_KHO)
                        {
                            v_item.SO_LUONG = (-ctkk.SO_LUONG_THEO_KIEM_KE + ctkk.SO_LUONG_THEO_SO_KE_TOAN) ?? 0;
                        }
                        v_item.GIA_THANH = v_so_du.GIA_THANH;
                        v_item.ID_GIAO_DICH = v_gd.ID;
                        v_lst_item.Add(v_item);
                        ctkk.DA_XU_LY_CHENH_LECH_YN = true;
                        db.SaveChanges();
                    }
                    var v_lst_ctgd = db.GD_CHI_TIET_GIAO_DICH
                        .Where(x => x.ID_GIAO_DICH == v_gd.ID).ToList();
                    U301_CHI_TIET_NGHIEP_VU.LOAI_GIAO_DICH v_loai_giao_dich = U301_CHI_TIET_NGHIEP_VU.LOAI_GIAO_DICH.NHAP_KHO_KHAC;
                    if (ip_loai_xu_ly_chenh_lech == LOAI_XU_LY_CHENH_LECH.XUAT_KHO)
                    {
                        v_loai_giao_dich = U301_CHI_TIET_NGHIEP_VU.LOAI_GIAO_DICH.XUAT_KHO_KHAC;
                    }
                    U301_CHI_TIET_NGHIEP_VU.cap_nhat_chi_tiet_giao_dich_va_so_du(db, v_lst_ctgd, v_lst_item, v_gd.ID, v_gd, v_loai_giao_dich);

                    trans.Commit();
                }
                catch (Exception v_e)
                {
                    db.Dispose();
                    db = new QuanLyKhoEntities();
                    trans.Rollback();
                }

            }
        }