using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ABC.GiaoDich;
using ABC.Report;
using ABC.KetNoi;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;
using ABC.BaoCao;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraReports.UI;
using DevExpress.XtraGrid.Views.Base;
using ABC.File;

namespace ABC.CongNo
{
    public partial class CongNo_SoThuTien : DevExpress.XtraEditors.XtraUserControl
    {
        
        public CongNo_SoThuTien()
        {
            InitializeComponent();
            Get_CauHinh.NgayTinhNo = Get_CauHinh.Ngay_DN;
           
            event_click();
            dtKH = DuLieu.List_KhachHang.Tables[0];
            Look_khachhang.Properties.DataSource = dtKH;
            
            gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);
            gv_daluu.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(gv_daluu_RowCellClick);
            txtngay.Text = Get_CauHinh.ngay_text;

            this.Load += new EventHandler(CongNo_SoThuTien_Load);
            //khai bai report truoc
           
        }
        DataTable dtKH;
        void CongNo_SoThuTien_Load(object sender, EventArgs e)
        {
            tailai();
            simpleButton1_Click_2(sender, e);
        }

        void gv_daluu_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            #region
            string col = e.Column.Caption;
            string colname = e.Column.FieldName;
            if (colname == "NoCu")
            {
                 kh_ten_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString();
                 kh_id_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang"].ToString();
               // ngay =int.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["NgayPhatSinhNo"].ToString());                
                xemnocu();
            }            
            if (colname == "NoMoi")
            {
                 kh_ten_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString();
                 kh_id_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang"].ToString();
               //  ngay = int.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["NgayPhatSinhNo"].ToString());                
                xemnomoi();
            }
            #endregion
        }
        string kh_ten_choos = "";
        string kh_id_choos = "";        
        int ngay = 0;
        public void xemnomoi()
        {
            ngay = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;

            frmBaoCao_SoBanHang_ChiTiet_KH ct = new frmBaoCao_SoBanHang_ChiTiet_KH();
            ct.xemtuformcongno = true;
            ct.hinhthucxem = "NoMoi";
            ct.ngayxemcongno = txtngay.Value;
            ct.kh_ten_congno = kh_ten_choos;
            ct.kh_id_congno = kh_id_choos; ;            
            ct.ShowDialog();
        }
      
        public void xemnocu()
        {
            #region
            frmBaoCao_SoBanHang_ChiTiet_KH ct = new frmBaoCao_SoBanHang_ChiTiet_KH();
            ct.xemtuformcongno = true;
            ngay = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;            
            ct.ngayxemcongno = new DateTime(1900, 1, 1).AddDays(ngay-1);
            ct.hinhthucxem = "NoCu";
            // get ngay TRA
            string sql = "sp_TongTra_moi_ngay "+ngay+",'" + kh_id_choos + "'";
            DataTable DT_NGAYTRA = db.T_opendataset(sql).Tables[0];
            if (DT_NGAYTRA.Rows.Count > 0)
            {
                #region kh co ngay tra 
                int ngay_tra =int.Parse(DT_NGAYTRA.Rows[0]["NGAYTRASAUCUNG"].ToString());
                ct.ngaytrasaucung = new DateTime(1900, 1, 1).AddDays(ngay_tra);
                ct.kh_ten_congno = kh_ten_choos;
                ct.kh_id_congno = kh_id_choos; ;
                ct.ShowDialog();
                #endregion
            }
            else
            {
                #region kh khong co ngay tra
                //lay ngay mua dau tien
                ct.ngaytrasaucung = new DateTime(1900, 1, 1);
                ct.kh_ten_congno = kh_ten_choos;
                ct.kh_id_congno = kh_id_choos; ;
                ct.ShowDialog();
                #endregion
            }
            #endregion
        }
      
        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                if (Pow.kiemtra("CongNo_SoThuTien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
                {
                    DataRow dt_update = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                    decimal Tong_No = decimal.Parse(dt_update["TongNo"].ToString());
                    decimal TogTra = decimal.Parse(e.Value.ToString());
                    TogTra = TogTra * 1000;
                    if (TogTra > Tong_No)
                    {
                        msgbox.dialog_OK_warning("Tiền trả vược mức tiền nợ.");
                        gv_daluu.CancelUpdateCurrentRow();
                    }
                    else
                    {
                        #region

                        // xoa chi tiet
                        DataRow[] dtv = DuLieu.ChiTiet_TraNo.Tables[0].Select("ID_KhachHang = '" + dt_update["ID_KhachHang"].ToString().ToUpper() + "' and SuDung = 1 and NgayPhatSinhNo = " + Get_CauHinh.NgayTinhNo + "");
                        //dtv.RowFilter = "ID_KhachHang = '" + dt_update["ID_KhachHang"].ToString().ToUpper() + "' and SuDung = 1 and NgayPhatSinhNo = " + Get_CauHinh.NgayTinhNo + "";
                        int i = 0;
                        for (i = 0; i < dtv.Length; i++)
                        {
                            string sql1 = "Tbl_ChiTietTraNo_XoaChiTiet ";
                            sql1 += "N'" + dtv[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                            sql1 += "N'" + dtv[i]["Luc"].ToString() + "',";
                            sql1 += "N'" + dtv[i]["NgayPhatSinhNo"].ToString() + "'";
                            db.T_exc(sql1);
                        }
                        if (i == dtv.Length)
                        {
                            // them chi tiet moi
                            string sql = "Tbl_ChiTietTraNo_insert ";
                            //@ID_KhachHang uniqueidentifier,
                            sql += "N'" + dt_update["ID_KhachHang"].ToString().ToUpper() + "',";
                            //@Luc nvarchar(200),
                            sql += "N'" + DateTime.Now.Hour.ToString() + " giờ " + DateTime.Now.Minute.ToString() + " phút " + DateTime.Now.Second.ToString() + " giây " + DateTime.Now.Date.ToShortDateString() + "',";
                            //@NgayPhatSinhNo int,
                            if (check_ngay.Checked == false)
                            {
                                sql += "N'" + Get_CauHinh.NgayTinhNo + "',";
                            }
                            else
                            {
                                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                                sql += "N'" + ng + "',";
                            }
                            //@Tien decimal
                            sql += "N'" + TogTra + "'";
                            db.T_exc(sql);
                        }
                        //
                        //tailai();
                        //DataRow[] dt_r = DuLieu.CongNo_1Ngay.Tables[0].Select("ID_KhachHang = '" + dt_update["ID_KhachHang"].ToString().ToUpper());
                        DuLieu.CongNo_1Ngay.Tables[0].AcceptChanges();
                        //gv_daluu.RefreshData();

                        #region
                        string sql_tinhlai = "sp_TongTra " + Get_CauHinh.NgayTinhNo + ",'"+  dt_update["ID_KhachHang"].ToString().ToUpper() + "'";
                        DataSet dts = db.T_opendataset(sql_tinhlai);
                        if (dts.Tables[0].Rows.Count > 0)
                        {
                            double tongtra_laytu_sql;
                            try
                            {
                                tongtra_laytu_sql = double.Parse(dts.Tables[0].Rows[0]["tongtra"].ToString());
                            }
                            catch
                            {
                                tongtra_laytu_sql = 0;
                            }

                            if (tongtra_laytu_sql != 0)
                            {
                                // DataRow[] dr = DuLieu.CongNo_1Ngay.Tables[0].Select();
                                DataRow[] dt_r = DuLieu.CongNo_1Ngay.Tables[0].Select("ID_KhachHang = '" + dt_update["ID_KhachHang"].ToString().ToUpper() + "'");
                                double nommoi = double.Parse(dt_r[0]["NoMoi"].ToString());
                                double nocu = double.Parse(dt_r[0]["NoCu"].ToString());

                                double ton = (nommoi + nocu) - tongtra_laytu_sql;
                                dt_r[0]["Ton"] = ton;
                                dt_r[0]["MoiTra"] = tongtra_laytu_sql;
                                dt_r[0]["GhiChu"] = "";
                                DuLieu.CongNo_1Ngay.Tables[0].AcceptChanges();


                            }
                        }
                        #endregion
                        data();
                        #endregion
                    }
                }
                else
                {
                    gv_daluu.CancelUpdateCurrentRow();
                }
            }
            catch (Exception)
            {
              
            }
            

        }
        #endregion

        #region lệnh dùng chung
        public void excel()
        {
        }
       

        #region cap nhat tien tra
        TienTra _TienTra;
        public void btn_capnhat()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _TienTra = new TienTra();
               
                _TienTra.FormClosed += new FormClosedEventHandler(_TienTra_FormClosed);
                _TienTra.TextChanged += new EventHandler(_TienTra_TextChanged);
                _TienTra.ShowDialog();
            }
            
        }
        string select_khachhang_name;
        
        void _TienTra_TextChanged(object sender, EventArgs e)
        {
            select_khachhang_name = _TienTra.select_khachhang_name;
           // msgbox.dialog_OK_info(select_khachhang_name);
           // gv_daluu.Columns["gridColumn1"].FilterInfo = new ColumnFilterInfo("[gridColumn1] LIKE '" + select_khachhang_name + "'");
           gv_daluu.ActiveFilterString = "[Tbl_KhachHang_Name] = '" + select_khachhang_name + "'";

      

            
        }

        void _TienTra_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_TienTra.DialogResult == DialogResult.OK)
            {

                data();

               
            }
            gv_daluu.ClearColumnsFilter();
            //CriteriaOperator expr0 = null;
            //gv_daluu.ActiveFilterCriteria = GroupOperator.And(new CriteriaOperator[] { expr0 });
            //_TienTra = new TienTra();
        }
#endregion

        #region
        public void khaibao_nodau()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                _T_KhaiBaoCongNo = new T_KhaiBaoCongNo();
                _T_KhaiBaoCongNo.FormClosed += new FormClosedEventHandler(_T_KhaiBaoCongNo_FormClosed);
                _T_KhaiBaoCongNo.ShowDialog();
                #endregion
            }
            else
            {
                msgbox.dialog_OK_warning("Bạn không có quyền cập nhật");
            }
        }
        T_KhaiBaoCongNo _T_KhaiBaoCongNo;
        void _T_KhaiBaoCongNo_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_T_KhaiBaoCongNo.DialogResult == DialogResult.OK)
            {
                tailai();
            }
        }
        #endregion

        public void sua()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
               
            }
        }

        
        public void print1()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.sono1 = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
                // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                Form frm = new frmChonIn();
                frm.ShowDialog();
                T_Tien1 rapport = new T_Tien1();

                rapport.DataSource = DuLieu.CongNo_1Ngay;              
                rapport.ShowRibbonPreview();
            }
        }
        public void print2()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.sono1 = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
                // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                Form frm = new frmChonIn();
                frm.ShowDialog();
                T_Tien rapport = new T_Tien();

                rapport.DataSource = DuLieu.CongNo_1Ngay;
               
                rapport.ShowRibbonPreview();
            }
        }
        Class_KetNoi db = new Class_KetNoi();
        int ngaydetru;
        public void tailai()
        {
            int ng2 = 0;
           
           // DataTable dtCongNo_old = db.T_opendataset("sp_CongNo_Chot").Tables[0];
            string filter_string = "";     
            if (check_ngay.Checked == true)
            {
              
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                ngaydetru = ng;
              
                Get_CauHinh.NgayTinhNo = ng;
                Get_CauHinh.NgayTinhNo_text = txtngay.Text;
                filter_string = "XemNo_1Ngay " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text!="")
                {
                    filter_string += txtsongay.Text+",";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'"+txtkhachhang.Text+"',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue!="")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }                
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            else
            {
                Get_CauHinh.NgayTinhNo = Get_CauHinh.Ngay_DN;
                Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                ngaydetru = Get_CauHinh.Ngay_DN;
                

                filter_string = "XemNo_1Ngay " + Get_CauHinh.NgayTinhNo + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'"+txtsongay.Text+"',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }            
            DuLieu.CongNo_1Ngay = db.T_opendataset(filter_string);
         //   DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("VuotHanMuc");
         if(DuLieu.CongNo_1Ngay!=null)
            DuLieu.CongNo_1Ngay.Tables.Remove("Table");
            //bool isTrung = false;
            //for (int i = 0; i < DuLieu.CongNo_1Ngay.Tables[0].Columns.Count; i++)
            //{
            //    if(DuLieu.CongNo_1Ngay.Tables[0].Columns[i].ColumnName.ToLower()=="nhomkh")
            //    {
            //        isTrung = true;
            //        break;
            //    }
            //}
            //if(!isTrung)
            //  DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("NhomKH");
           //lay ds khach
           //DataTable dtKhach = Main_Form._dtKH;
           // if (DuLieu.CongNo_1Ngay.Tables[0].Rows.Count == 0)
           // {
           //     for (int i = 0; i < dtKhach.Rows.Count; i++)
           //     {
           //         DataRow row = DuLieu.CongNo_1Ngay.Tables[0].NewRow();
           //         DataView viewKH = DuLieu.CongNo_1Ngay.Tables[0].Copy().DefaultView;
           //         viewKH.RowFilter = "ID_KhachHang='" + dtKhach.Rows[i]["ID_KhachHang"].ToString() + "'";

           //         if (viewKH.ToTable().Rows.Count == 0)
           //         {
           //             row["ID_KhachHang"] = dtKhach.Rows[i]["ID_KhachHang"].ToString();
           //             row["Tbl_KhachHang_Name"] = dtKhach.Rows[i]["Tbl_KhachHang_Name"].ToString();
           //             row["NoCu"] = 0;
           //             row["NoMoi"] = 0;
           //             row["MoiTra"] = 0;
           //             row["TongNo"] = 0;
           //             row["Ton"] = 0;
           //             row["So_ngayno"] = 0;
           //             row["NhomKH"] = dtKhach.Rows[i]["_ID1_Name"].ToString();
           //             DuLieu.CongNo_1Ngay.Tables[0].Rows.Add(row);
           //         }
           //         else
           //         {

           //             DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["NhomKH"] = dtKhach.Rows[i]["_ID1_Name"].ToString();
           //         }
           //     }
           // }
           
            #region doc du lieu da ket chuyen gan nhat
            //if (DuLieu.CongNo_1Ngay.Tables[0].Rows.Count > 0)
            //{
            //    DataView view = dtCongNo_old.DefaultView;
            //    DateTime ngayChot_old;
            //    if (view.ToTable().Rows.Count > 0)
            //    {
            //        ngayChot_old = DateTime.Parse(view.ToTable().Rows[0]["NgayChot"].ToString());
            //        ng2 = (int)(Convert.ToDateTime(txtngay.Text) - ngayChot_old).TotalDays;
            //    }
            //    else
            //        ng2= (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
            //    for (int i = 0; i < DuLieu.CongNo_1Ngay.Tables[0].Rows.Count; i++)
            //    {
            //        string _IDKhachHang = DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().Trim();
            //        double soNgayChot_old = 0;
                  
            //        double _So_ngayno = (DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["So_ngayno"].ToString().Trim() == "") ? 0 : double.Parse(DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["So_ngayno"].ToString().Trim());
                  
            //        view.RowFilter = "ID_KhachHang='" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString() + "'";
            //        double _Ton = double.Parse(DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["Ton"].ToString());
            //        double _NoCu = double.Parse(DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["NoCu"].ToString());
            //        double _TongNo = double.Parse(DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TongNo"].ToString());
            //        if (view.ToTable().Rows.Count > 0)
            //        {
            //            soNgayChot_old = int.Parse("0" + view.ToTable().Rows[0]["SoNgayNo"].ToString());
            //            DateTime _NgayKetChuyen = DateTime.Parse(view.ToTable().Rows[0]["NgayChot"].ToString());
            //            DataTable dtCheckNo = db.T_opendataset("sp_Union_NoTraMoiNgay '" + _IDKhachHang + "'").Tables[0];
            //            double sogay = 0;
            //            if (dtCheckNo.Rows.Count == 0)
            //                sogay =  soNgayChot_old + ng2;//code ok
            //            else
            //                sogay = _So_ngayno;
            //            int ng3 = (int)(Convert.ToDateTime(txtngay.Text) - _NgayKetChuyen).TotalDays;
            //            int _son = int.Parse(DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["So_ngayno"].ToString());
            //            DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["Ton"] = _Ton + double.Parse(view.ToTable().Rows[0]["ChotTien"].ToString());
            //            DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["NoCu"] = _NoCu + double.Parse(view.ToTable().Rows[0]["ChotTien"].ToString());
            //            DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TongNo"] = _TongNo + double.Parse(view.ToTable().Rows[0]["ChotTien"].ToString());
            //            DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["So_ngayno"] = sogay;
            //            DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["GhiChu"] = (sogay).ToString() + " ngày";
            //        }

            //    }
            //}
            //else
            //{
            //    DataView view = dtCongNo_old.DefaultView;
            //    DataTable dt = view.ToTable();
            //    DateTime _NgayKetChuyen = DateTime.Parse(view.ToTable().Rows[0]["NgayChot"].ToString());
            //    int ng3 = (int)(Convert.ToDateTime(txtngay.Text) - _NgayKetChuyen).TotalDays;
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        DataRow row = DuLieu.CongNo_1Ngay.Tables[0].NewRow();
            //        row["ID_KhachHang"] = dt.Rows[i]["ID_KhachHang"].ToString();
            //        row["Tbl_KhachHang_Name"] = dt.Rows[i]["Tbl_KhachHang_Name"].ToString();
            //        row["Ton"] = double.Parse(dt.Rows[i]["ChotTien"].ToString());
            //        row["NoCu"] = double.Parse(dt.Rows[i]["ChotTien"].ToString());
            //        row["TongNo"] = double.Parse(dt.Rows[i]["ChotTien"].ToString());
            //        row["So_ngayno"] = int.Parse(dt.Rows[i]["SoNgayNo"].ToString()) + ng3;
            //        row["GhiChu"] = (int.Parse(dt.Rows[i]["SoNgayNo"].ToString()) + ng3) + " ngày";
            //        DuLieu.CongNo_1Ngay.Tables[0].Rows.Add(row);
            //    }
            //}
            #endregion
            
            DataView viewkq = DuLieu.CongNo_1Ngay.Tables[0].Copy().DefaultView;
            viewkq.RowFilter = "Ton>0 or TongNo>0";
            DuLieu.CongNo_1Ngay.Tables[0].Rows.Clear();
            DuLieu.CongNo_1Ngay.Tables[0].Merge(viewkq.ToTable());
            data();
            gv_daluu.ExpandAllGroups();
        }
        private string  LoadTenNhoKH(string IDKH)
        {
            Class_KetNoi db = new Class_KetNoi();
            string sql = "select [Name] from Tbl_NhomKhachHang where ID in(select top 1 _ID1 from Tbl_KhachHang where ID='"+IDKH+"')";
            DataTable dt = db.T_opendataset(sql).Tables[0];
            if (dt.Rows.Count > 0)
                return dt.Rows[0][0].ToString();
            else
                return "";
        }
        private bool KiemTraTonTaiKH_CongNo(string ID_KhachHang, DataTable dtCongNo_old)
        {
            DataView view = dtCongNo_old.DefaultView;
            view.RowFilter = "ID_KhachHang='" + ID_KhachHang + "'";
            if (view.ToTable().Rows.Count > 0)
                return true;
            else
                return false;
        }
#pragma warning disable CS0169 // The field 'CongNo_SoThuTien.dts' is never used
        DataSet dts;
#pragma warning restore CS0169 // The field 'CongNo_SoThuTien.dts' is never used
        public void print1111()
        {
           
            if (Pow.kiemtra("CongNo_SoThuTien", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                DataSet dsSelectData = new DataSet();

                dsSelectData = DuLieu.BaoCao_SoBaoCao_Si_Bien;
                Get_CauHinh.report_ngay = "BÁO CÁO NHẬP SĨ (BIỂN) NGÀY " + txtngay.Value.ToShortDateString();
                T_BaocaonhapsiBien rapport = new T_BaocaonhapsiBien();
                rapport.DataSource = dsSelectData;
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowRibbonPreview();

                #endregion
            }
        }
        public void print_bill()
        {
            if (Pow.kiemtra("CongNo_SoThuTien", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                Get_CauHinh.bill_no = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
               // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                //get cong no ket
               
                tailai_ket();
                tailai_sot();
                tailai_lu();
                tailai_cat();
                tailai_bom();
                T_bill_tien rapport = new T_bill_tien();
                try { 
                    DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("TonS", typeof(int)); 
                    DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("TonK", typeof(int));
                    DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("TonL", typeof(int));
                    DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("TonC", typeof(int));
                    DuLieu.CongNo_1Ngay.Tables[0].Columns.Add("TonB", typeof(int)); 
                }
                catch { };
              //  string sql_SP_ketChuyen = "select * from Tbl_CongNo_SP_chot";
              //  DataTable dt_SP_ketChuyen = db.T_open(sql_SP_ketChuyen).Tables[0];
              //  DataView view = dt_SP_ketChuyen.Copy().DefaultView;
                for (int i = 0; i < DuLieu.CongNo_1Ngay.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_no = DuLieu.CongNo_1Ngay.Tables[0].Rows[i];
                    DataRow[] dr_sot = DuLieu.CongNo_1Ngay_Sot.Tables[0].Select("ID_KhachHang = '" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().ToUpper() + "'");
                    DataRow[] dr_ket = DuLieu.CongNo_1Ngay_Ket.Tables[0].Select("ID_KhachHang = '" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().ToUpper() + "'");
                    DataRow[] dr_lu = DuLieu.CongNo_1Ngay_Lu.Tables[0].Select("ID_KhachHang = '" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().ToUpper() + "'");
                    DataRow[] dr_cat = DuLieu.CongNo_1Ngay_Cat.Tables[0].Select("ID_KhachHang = '" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().ToUpper() + "'");
                    DataRow[] dr_bom = DuLieu.CongNo_1Ngay_Bom.Tables[0].Select("ID_KhachHang = '" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString().ToUpper() + "'");
                    if (dr_sot.Length > 0)
                    {
                        dr_no["TonS"] = dr_sot[0]["Ton"];
                    }
                    else
                    {
                        dr_no["TonS"] = 0;
                    }
                    if (dr_ket.Length > 0)
                    {
                        dr_no["TonK"] = dr_ket[0]["Ton"];
                    }
                    else
                    {
                        dr_no["TonK"] = 0;
                    }
                    if (dr_lu.Length > 0)
                    {
                        dr_no["TonL"] = dr_lu[0]["Ton"];
                    }
                    else
                    {
                        dr_no["TonL"] = 0;
                    }
                    if (dr_cat.Length > 0)
                    {
                        dr_no["TonC"] = dr_cat[0]["Ton"];
                    }
                    else
                    {
                        dr_no["TonC"] = 0;
                    }
                    if (dr_bom.Length > 0)
                    {
                        dr_no["TonB"] = dr_bom[0]["Ton"];
                    }
                    else
                    {
                        dr_no["TonB"] = 0;
                    }


                    //view.RowFilter = "ID_KhachHang='" + DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["ID_KhachHang"].ToString() + "'";
                    //double TonS = 0, TonK = 0, TonL = 0, TonC = 0, TonB = 0;
                    //if (view.ToTable().Rows.Count > 0)
                    //{
                    //    TonS = double.Parse(view.ToTable().Rows[0]["Ton_sot"].ToString());
                    //    TonK = double.Parse(view.ToTable().Rows[0]["Ton_ket"].ToString());
                    //    TonL = double.Parse(view.ToTable().Rows[0]["Ton_lu"].ToString());
                    //    TonC = double.Parse(view.ToTable().Rows[0]["Ton_cat"].ToString());
                    //    TonB = double.Parse(view.ToTable().Rows[0]["Ton_bom"].ToString());
                    //}
                    //DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TonS"] = double.Parse(dr_no["TonS"].ToString()) + TonS;
                    //DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TonK"] = double.Parse(dr_no["TonK"].ToString()) + TonK;
                    //DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TonL"] = double.Parse(dr_no["TonL"].ToString()) + TonL;
                    //DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TonC"] = double.Parse(dr_no["TonC"].ToString()) + TonC;
                    //DuLieu.CongNo_1Ngay.Tables[0].Rows[i]["TonB"] = double.Parse(dr_no["TonB"].ToString()) + TonB;

                    DuLieu.CongNo_1Ngay.Tables[0].AcceptChanges();
                }
                
                rapport.DataSource = DuLieu.CongNo_1Ngay;
                rapport.ShowRibbonPreview();
                #endregion
            }
        }
        #endregion

        #region tailai ket
        public void tailai_ket()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_ket = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Ket " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_ket = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Ket " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }

            DuLieu.CongNo_1Ngay_Ket = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Ket.Tables.Remove("Table");



            data();

        }
        #endregion

        #region tai lai sot
        public void tailai_sot()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_sot = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Sot " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_sot = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Sot " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }

            DuLieu.CongNo_1Ngay_Sot = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Sot.Tables.Remove("Table");



            data();

        }
        public void tailai_lu()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_sot = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Lu " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_sot = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Lu " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }

            DuLieu.CongNo_1Ngay_Lu = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Lu.Tables.Remove("Table");



           // data();

        }
        public void tailai_cat()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_sot = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Cat " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_sot = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Cat " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }

            DuLieu.CongNo_1Ngay_Cat = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Cat.Tables.Remove("Table");



            // data();

        }
        public void tailai_bom()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_sot = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Bom " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_sot = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
                // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Bom " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }

            DuLieu.CongNo_1Ngay_Bom = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Bom.Tables.Remove("Table");



            // data();

        }
        #endregion

        #region menu click
        public void event_click()
        {

            //btn_tratien.Click += new EventHandler(btn_del_Click);
            //btn_print1.Click += new EventHandler(btn_all_Click);
            //btn_print2.Click += new EventHandler(btn_none_Click);

            btn_load.Click += new EventHandler(btn_load_Click);
            //btn_nodau.Click += new EventHandler(btn_in_Click);
            //btn_bill.Click += new EventHandler(btn_sua_Click);

        }

        void btn_ghichu_Click(object sender, EventArgs e)
        {
            xemghichu();
            data();
        }
        public void xemghichu()
        {
           
        }

        void btn_sua_Click(object sender, EventArgs e)
        {
            print_bill();
        }

   

        void btn_in_Click(object sender, EventArgs e)
        {
            khaibao_nodau();
        }

        void btn_load_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(SplashScreen1));
            tailai();
            SplashScreenManager.CloseForm();

        }

        void btn_close_Click(object sender, EventArgs e)
        {
           // this.Close();
        }

        void btn_none_Click(object sender, EventArgs e)
        {
            print2();
        }

        void btn_all_Click(object sender, EventArgs e)
        {
            print1();
        } 

        void btn_del_Click(object sender, EventArgs e)
        {
            btn_capnhat();
        }

       


        #endregion

        #region data       
#pragma warning disable CS0169 // The field 'CongNo_SoThuTien.dtv_songay' is never used
        DataView dtv_songay;
#pragma warning restore CS0169 // The field 'CongNo_SoThuTien.dtv_songay' is never used
        DataTable dtGrid;
        public void data()
        {
            string sql = "";
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                sql = "sp_TienTraTruoc_Ngay " + ng;
            }
            else
            {
                //int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                sql = "sp_TienTraTruoc_Ngay " + Get_CauHinh.Ngay_DN;
            }
            DuLieu.ChiTiet_TraNo = db.T_opendataset(sql);
            gv_daluu.ClearColumnsFilter();
            if(DuLieu.CongNo_1Ngay!=null)
               dtGrid = DuLieu.CongNo_1Ngay.Tables[0];
            gc_daluu.DataSource = dtGrid;
                gv_daluu.ExpandAllGroups();
                gv_daluu.ExpandAllGroups();
                gv_daluu.RefreshData();

            //lbnocu.Text = cnocu.SummaryText + "____Nợ củ";
            //lbnomoi.Text = cnomoi.SummaryText + "____Nợ Mới";
            //lbtongno.Text = ctongno.SummaryText + "____Tổng nợ";
            //lbtongtra.Text = ctongtra.SummaryText + "____Tổng trả";
            //lbton.Text = cton.SummaryText + "____Tồn";
        }
        #endregion

        private void BaoCao_ToaSi_Load(object sender, EventArgs e)
        {
            
            data();
        }

        private void gv_daluu_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //try
            //{
            //    string _vuot = gv_daluu.GetRowCellValue(e.RowHandle, "VuotHanMuc").ToString().Trim();
            //    if (_vuot == "x")
            //    {
            //        e.Appearance.BackColor = Color.Red;
            //        e.Appearance.ForeColor = Color.White;
            //    }
            //}
            //catch (Exception)
            //{
                
                
            //}
        }

        private void gv_daluu_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            //try
            //{
            //    double _hanmuc = double.Parse(gv_daluu.GetRowCellValue(e.RowHandle, "HanMuc").ToString().Trim());
            //    double _ton = double.Parse(gv_daluu.GetRowCellValue(e.RowHandle, "Ton").ToString().Trim());
            //    if (_hanmuc < _ton&&_hanmuc>0)
            //    {
            //        e.Appearance.BackColor = Color.LightGray;
            //        e.Appearance.ForeColor = Color.Black;
            //        //gv_daluu.SetRowCellValue(e.RowHandle, "VuotHanMuc", "x");
            //    }
            //}
            //catch (Exception)
            //{
                
               
            //}
          
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DataView dv = dtGrid.Copy().DefaultView;
            dv.RowFilter = "VuotHanMuc='x'";
            frmVuot_DM_CongNo_SoThuTien frm = new frmVuot_DM_CongNo_SoThuTien(dv.ToTable());
            frm.ShowDialog();
            //ColumnView view = gv_daluu;
            //view.ActiveFilter.Clear();
            //ViewColumnFilterInfo viewFilterInfo = new ViewColumnFilterInfo(view.Columns["VuotHanMuc"],
            //  new ColumnFilterInfo("[VuotHanMuc] = 'x'", ""));
            //view.ActiveFilter.Add(viewFilterInfo);
        }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            //ColumnView view = gv_daluu;
            //view.ActiveFilter.Clear();
            //ViewColumnFilterInfo viewFilterInfo = new ViewColumnFilterInfo(view.Columns["VuotHanMuc"],
            //  new ColumnFilterInfo("[VuotHanMuc] == 'k'", ""));
            //view.ActiveFilter.Add(viewFilterInfo);
            
        }

        private void simpleButton1_Click_2(object sender, EventArgs e)
        {
            if(simpleButton1.Text.ToLower().Trim()=="mở điều kiện lọc")
            {
                panelControl2.Visible = true;
                simpleButton1.Text = "Tắt điều kiện lọc";
            }
            else
            {
                panelControl2.Visible = false;
                simpleButton1.Text = "Mở điều kiện lọc";
            }    
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_all_Click(sender, e);
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_in_Click(sender, e);
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_sua_Click(sender, e);
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_none_Click(sender, e);
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_del_Click(sender, e);
        }

        private void gv_daluu_DoubleClick(object sender, EventArgs e)
        {
           
        }
    }
}
