private void Get_Data(ref KT_CTuGoc obj, int position)
 {
     try
     {
         obj.MA_CTU = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_CTU"), "");
         obj.HD_SO = Utils.CStrDef(gridView1.GetRowCellValue(position, "HD_SO"), "");
         DateTime? temp = null;
         if(Utils.CDateDef(gridView1.GetRowCellValue(position, "HD_NGAY"), DateTime.MinValue) != DateTime.MinValue)
             temp =Utils.CDateDef(gridView1.GetRowCellValue(position, "HD_NGAY"), DateTime.MinValue);
         obj.HD_NGAY = temp;
         obj.SO_CTU = Utils.CStrDef(gridView1.GetRowCellValue(position, "SO_CTU"), "");
         temp = null;
         if (Utils.CDateDef(gridView1.GetRowCellValue(position, "NGAY_CTU"), DateTime.MinValue) != DateTime.MinValue)
             temp = Utils.CDateDef(gridView1.GetRowCellValue(position, "NGAY_CTU"), DateTime.MinValue);
         obj.NGAY_CTU = temp;
         obj.DIEN_GIAI = Utils.CStrDef(gridView1.GetRowCellValue(position, "DIEN_GIAI"), "");
         obj.TK_NO = Utils.CStrDef(gridView1.GetRowCellValue(position, "TK_NO"), "");
         obj.MA_DTPN_NO = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_DTPN_NO"), "");
         obj.MA_DM_NHAP = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_DM_NHAP"), "");
         obj.TK_CO = Utils.CStrDef(gridView1.GetRowCellValue(position, "TK_CO"), "");
         obj.MA_DTPN_CO = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_DTPN_CO"), "");
         obj.MA_DM_XUAT = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_DM_XUAT"), "");
         obj.TEN_DM = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_DM"), "");
         obj.DVT = Utils.CStrDef(gridView1.GetRowCellValue(position, "DVT"), "");
         obj.SO_LUONG = Utils.CDblDef(gridView1.GetRowCellValue(position, "SO_LUONG"), 0);
         obj.DON_GIA_VND = Utils.CDblDef(gridView1.GetRowCellValue(position, "DON_GIA_VND"), 0);
         obj.THANH_TIEN_VND = Utils.CDblDef(gridView1.GetRowCellValue(position, "THANH_TIEN_VND"), 0);
         obj.PT_CK = Utils.CDblDef(gridView1.GetRowCellValue(position, "PT_CK"), 0);
         obj.CHIET_KHAU_VND = Utils.CDblDef(gridView1.GetRowCellValue(position, "CHIET_KHAU_VND"), 0);
         obj.LOAI_THUE = Utils.CStrDef(gridView1.GetRowCellValue(position, "LOAI_THUE"), "");
         obj.TK_THUE = Utils.CStrDef(gridView1.GetRowCellValue(position, "TK_THUE"), "");
         obj.TS_GTGT = Utils.CStrDef(gridView1.GetRowCellValue(position, "TS_GTGT"), "");
         obj.TIEN_THUE_VND = Utils.CDblDef(gridView1.GetRowCellValue(position, "TIEN_THUE_VND"), 0);
         obj.TONG_TIEN_VND = Utils.CDblDef(gridView1.GetRowCellValue(position, "TONG_TIEN_VND"), 0);
         obj.MAT_HANG = Utils.CStrDef(gridView1.GetRowCellValue(position, "MAT_HANG"), "");
         obj.MA_KH = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_KH"), "");
         obj.TEN_KH = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_KH"), "");
         obj.MASO_THUE = Utils.CStrDef(gridView1.GetRowCellValue(position, "MASO_THUE"), "");
         obj.DIA_CHI = Utils.CStrDef(gridView1.GetRowCellValue(position, "DIA_CHI"), "");
         obj.DIA_CHI_NGD = Utils.CStrDef(gridView1.GetRowCellValue(position, "DIA_CHI_NGD"), "");
         obj.TEN_KH_GD = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_KH_GD"), "");
         obj.GHI_CHU = Utils.CStrDef(gridView1.GetRowCellValue(position, "GHI_CHU"), "");
         obj.TY_GIA = Utils.CDblDef(gridView1.GetRowCellValue(position, "TY_GIA"), 0);
         obj.THANH_TIEN_USD = Utils.CDblDef(gridView1.GetRowCellValue(position, "THANH_TIEN_USD"), 0);
         obj.MA_CTRINH = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_CTRINH"), "");
         obj.TEN_CTRINH = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_CTRINH"), "");
         obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(position, "TRANG_THAI"), 0);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (cboLoaiCTu.SelectedIndex < 0)
            {
                MessageBox.Show("Chưa chọn loại chứng từ!", "Thông báo");
                return;
            }
            if (txtSoCTu.Text.Trim().Length == 0)
            {
                MessageBox.Show("Chưa nhập số chứng từ!", "Thông báo");
                return;
            }
            if (dtpNgayCTu.Value == null || dtpNgayCTu.Value.Month != Utils.CIntDef(fTerm._month, 0) || dtpNgayCTu.Value.Year != Utils.CIntDef(fTerm._year, 0))
            {
                MessageBox.Show("Ngày chứng từ không hợp lệ!", "Thông báo");
                return;
            }
            if (cboTKNo.SelectedIndex < 0)
            {
                MessageBox.Show("Chưa chọn TK Nợ!", "Thông báo");
                return;
            }
            if (cboDTPNNo.SelectedIndex < 0)
            {
                MessageBox.Show("Chưa chọn ĐTPN Nợ!", "Thông báo");
                return;
            }
            if (cboTKCo.SelectedIndex < 0)
            {
                MessageBox.Show("Chưa chọn TK Có!", "Thông báo");
                return;
            }
            if (cboDTPNCo.SelectedIndex < 0)
            {
                MessageBox.Show("Chưa chọn ĐTPN Có!", "Thông báo");
                return;
            }
            KT_CTuGoc CTGoc = new KT_CTuGoc();
            CTGoc.MA_CTU = Utils.CStrDef(cboLoaiCTu.SelectedValue, "");
            CTGoc.MA_TT = Utils.CStrDef(cboMaTT.SelectedValue, "");
            CTGoc.MA_HD = Utils.CStrDef(cboMaHD.SelectedValue, "");
            CTGoc.KY_HIEU_MAU_HD = Utils.CStrDef(txtKHMHD.Text, "");
            CTGoc.HD_SR = Utils.CStrDef(txtSeriHD.Text, "");
            CTGoc.HD_SO = Utils.CStrDef(cboSoHD.SelectedValue, "");
            DateTime? temp = null;
            if (Utils.CDateDef(dtpNgayHD.Value, DateTime.MinValue) != DateTime.MinValue)
                temp = Utils.CDateDef(dtpNgayHD.Value, DateTime.MinValue);
            CTGoc.HD_NGAY = temp;
            CTGoc.LOAI_THUE = Utils.CStrDef(cboHDVat.SelectedValue, "");
            CTGoc.SO_CTU = Utils.CStrDef(txtSoCTu.Text, "");
            temp = null;
            if (Utils.CDateDef(dtpNgayCTu.Value, DateTime.MinValue) != DateTime.MinValue)
                temp = Utils.CDateDef(dtpNgayCTu.Value, DateTime.MinValue);
            CTGoc.NGAY_CTU = temp;
            var itemDiengiai = _KTDIENGIAIRepo.GetById(Utils.CIntDef(cboDiengiai.SelectedValue, 0));
            if (itemDiengiai != null)
            {
                CTGoc.DIEN_GIAI = Utils.CStrDef(itemDiengiai.DIEN_GIAI1, "");
                CTGoc.DIEN_GIAI2 = Utils.CStrDef(itemDiengiai.DIEN_GIAI2, "");
            }
            CTGoc.SO_HOPDONG = Utils.CStrDef(cboSoHopDong.SelectedValue, "");
            var itemCongtrinh = _KTDMCongTrinhRepo.GetById(Utils.CIntDef(cboCongtrinh.SelectedValue, 0));
            if (itemCongtrinh != null)
            {
                CTGoc.MA_CTRINH = Utils.CStrDef(itemCongtrinh.MA_CT, "");
                CTGoc.TEN_CTRINH = Utils.CStrDef(itemCongtrinh.TEN_CT, "");
            }
            CTGoc.TK_NO = Utils.CStrDef(cboTKNo.Text, "");
            CTGoc.TK_CO = Utils.CStrDef(cboTKCo.Text, "");
            CTGoc.MA_DTPN_NO = Utils.CStrDef(cboDTPNNo.Text, "");
            CTGoc.MA_DTPN_CO = Utils.CStrDef(cboDTPNCo.Text, "");
            CTGoc.MA_YTCP_NO = Utils.CStrDef(cboYTCPNo.Text, "");
            CTGoc.MA_YTCP_CO = Utils.CStrDef(cboYTCPCo.Text, "");
            //CTGoc.MA_VTHH_NO = Utils.CStrDef(cboYTCPNo.SelectedText, "");
            //CTGoc.MA_VTHH_CO = Utils.CStrDef(cboYTCPCo.SelectedText, "");
            CTGoc.SO_LUONG = Utils.CDblDef(txtSoluong.Text.Replace(",", ""), 0);
            CTGoc.DON_GIA_VND = Utils.CDblDef(txtDongia.Text.Replace(",", ""), 0);
            CTGoc.THANH_TIEN_VND = Utils.CDblDef(txtTienVnd.Text.Replace(",", ""), 0) > 0 ? Utils.CDblDef(txtTienVnd.Text.Replace(",", ""), 0) : CTGoc.SO_LUONG * CTGoc.DON_GIA_VND;
            CTGoc.TS_GTGT = Utils.CStrDef(txtTS.Text, "");
            CTGoc.TK_THUE = Utils.CStrDef(txtTKThue.Text, "");
            double tienthue = (Utils.CDblDef(CTGoc.THANH_TIEN_VND, 0)* Utils.CIntDef(CTGoc.TS_GTGT, 0))/100;
            CTGoc.TIEN_THUE_VND = Utils.CDblDef(txtThueVND.Text.Replace(",", ""), 0) > 0 ? Utils.CDblDef(txtThueVND.Text.Replace(",", ""), 0) : tienthue;
            //CTGoc.TONG_TIEN_VND = Utils.CDblDef(txtTongtien.Text, 0);
            CTGoc.TONG_TIEN_VND = CTGoc.THANH_TIEN_VND + CTGoc.TIEN_THUE_VND;
            CTGoc.TY_GIA = Utils.CDblDef(txtTygia.Text.Replace(",", ""), 0);
            CTGoc.THANH_TIEN_USD = Utils.CDblDef(txtTienUsd.Text.Replace(",", ""), 0);
            CTGoc.TS_NK = Utils.CDblDef(txtPTNK.Text, 0);
            CTGoc.THUE_NK_USD = Utils.CDblDef(txtThueNK.Text.Replace(",", ""), 0);

            //Thông tin khách hàng
            CTGoc.MA_KH = cboMakhachhang.Text;
            CTGoc.TEN_KH = txtTenkhachhang.Text;
            CTGoc.MASO_THUE = txtMasothue.Text;
            CTGoc.DIA_CHI = txtDiachi.Text;
            CTGoc.MAT_HANG = txtMathang.Text;
            //CTGoc.GanMaDTPN = txtDiachi.Text;
            CTGoc.TEN_KH_GD = txtTennguoigiaodich.Text;
            CTGoc.DIA_CHI_NGD = txtDiachiNGD.Text;
            CTGoc.MA_NV_BAN = cboManhanvienban.Text;
            CTGoc.TEN_NV_BAN = txtTennhanvienban.Text;
            CTGoc.GHI_CHU = txtGhichu.Text;

            _listCTuGoc.Add(CTGoc);

            Tinhtien();
            EnableCtu(false);
            LoadGrid2();
            LoadSTT();
        }
 partial void DeleteKT_CTuGoc(KT_CTuGoc instance);
 partial void UpdateKT_CTuGoc(KT_CTuGoc instance);
 partial void InsertKT_CTuGoc(KT_CTuGoc instance);
        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                _KT_CTuGocRepo = new KT_CTuGocRepo();
                //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
                if (view.IsNewItemRow(e.RowHandle))
                {
                    //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại
                    KT_CTuGoc obj = new KT_CTuGoc();
                    Get_Data(ref obj, e.RowHandle);
                    _KT_CTuGocRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(e.RowHandle, "ID").ToString(), 0);
                    KT_CTuGoc obj = _KT_CTuGocRepo.GetById(id);
                    if (obj != null)
                    {
                        Get_Data(ref obj, e.RowHandle);
                        _KT_CTuGocRepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void Save_Duplicate()
 {
     try
     {
         if (MessageBox.Show("Bạn có muốn copy dòng này thành dòng mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
         {
             _KT_CTuGocRepo = new KT_CTuGocRepo();
             KT_CTuGoc obj = new KT_CTuGoc();
             Get_Data(ref obj, gridView1.FocusedRowHandle);
             _KT_CTuGocRepo.Create(obj);
             MessageBox.Show("Đã copy dòng này vào cuối bảng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }