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();
        }
        private void insert_data()
        {
            if (!check_validate_data_is_ok()) return;
            decimal v_dc_id_san_pham = CIPConvert.ToDecimal(m_lsb_san_pham.SelectedValue);
            GD_SO_DU v_gd = new GD_SO_DU();
            v_gd.ID_SAN_PHAM = v_dc_id_san_pham;
            v_gd.NGAY_THANG = DateTime.Now;
            v_gd.SO_LUONG = Convert.ToInt16(m_txt_so_luong.Text);
            db.GD_SO_DU.Add(v_gd);
            db.SaveChanges();

            //Update don vi tinh
            DM_SAN_PHAM v_dm_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == v_dc_id_san_pham);
            if (v_dm_san_pham != null)
            {
                v_dm_san_pham.DON_VI_TINH = m_txt_don_vi_tinh.Text;
                db.SaveChanges();
            }
        }
        public static void CapNhatSoDu(
            decimal ip_dc_id_san_pham
            , DateTime ip_dat_tu_ngay
            , DateTime ip_dat_den_ngay
            , decimal ip_dc_so_luong)
        {
            BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
            var v_lst_so_du = db.GD_SO_DU
                .Where(x => x.ID_SAN_PHAM == ip_dc_id_san_pham
                        && x.NGAY_THANG >= ip_dat_den_ngay
                        && x.NGAY_THANG <= ip_dat_den_ngay)
                .ToList();

            //Cập nhật số dư nếu đã có
            if (v_lst_so_du.Count > 0)
            {
                for (int i = 0; i < v_lst_so_du.Count; i++)
                {
                    v_lst_so_du[i].SO_LUONG += (int)ip_dc_so_luong;
                    if (v_lst_so_du[i].SO_LUONG <= 0)
                    {
                        db.GD_SO_DU.Remove(v_lst_so_du[i]);
                    }
                    db.SaveChanges();
                }
            }
            else
            {
                //Nếu chưa có số dư thì thêm mới dữ liệu số dư
                GD_SO_DU v_gd_so_du = new GD_SO_DU();
                v_gd_so_du.ID_SAN_PHAM = ip_dc_id_san_pham;
                v_gd_so_du.NGAY_THANG = ip_dat_den_ngay;
                v_gd_so_du.SO_LUONG = (int)ip_dc_so_luong;
                db.GD_SO_DU.Add(v_gd_so_du);
                db.SaveChanges();

            }
            db.Dispose();
        }
        private void m_cmd_luu_thong_tin_Click(object sender, EventArgs e)
        {
            try
            {
                decimal v_dc_id_hoa_don = CIPConvert.ToDecimal(m_lsb_hoa_don.SelectedValue);
                var v_gd_hoa_don = db.GD_HOA_DON_BAN.FirstOrDefault(x => x.ID == v_dc_id_hoa_don);
                if (v_gd_hoa_don != null)
                {
                    //v_gd_hoa_don.THANH_TIEN = (double)CIPConvert.ToDecimal(m_txt_thanh_tien.Text);
                    //v_gd_hoa_don.KHACH_HANG_NO = (double)CIPConvert.ToDecimal(m_txt_khach_hang_no.Text);
                    //v_gd_hoa_don.DA_THANH_TOAN = (double)CIPConvert.ToDecimal(m_txt_da_thanh_toan.Text);
                    v_gd_hoa_don.THANH_TIEN = Convert.ToDouble(m_txt_thanh_tien.Text);
                    v_gd_hoa_don.KHACH_HANG_NO = Convert.ToDouble(m_txt_khach_hang_no.Text);
                    v_gd_hoa_don.DA_THANH_TOAN = Convert.ToDouble(m_txt_da_thanh_toan.Text);

                    var v_gd_hoa_don_ban = db.GD_CT_HOA_DON_BAN.FirstOrDefault(x => x.ID_HOA_DON_BAN == v_dc_id_hoa_don);
                    GD_SO_DU v_gd_so_du = new GD_SO_DU();
                    if (v_gd_hoa_don_ban != null)
                    {
                        v_gd_so_du.SO_LUONG = v_gd_so_du.SO_LUONG - v_gd_hoa_don_ban.SO_LUONG;
                        if (v_gd_so_du.SO_LUONG <=0)
                        {
                            MessageBox.Show("Sản phẩm này đã hết!", "Cảnh báo!");
                            return;
                        }
                    }

                }
                db.SaveChanges();
                //m_cmd_huy_thao_tac_Click(null, null);
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }