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.XtraReports.UI;
using ABC.File;
using DevExpress.XtraSplashScreen;

namespace ABC.BaoCao
{
    public partial class BaoCao_SoBaoCao_Le : DevExpress.XtraEditors.XtraUserControl
    {
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public BaoCao_SoBaoCao_Le()
        {
            InitializeComponent();
            // data();
            event_click();
            link_kh.Click += new EventHandler(link_kh_Click);
            link_tongluong.Click += new EventHandler(link_tongluong_Click);
            gv_daluu.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);

            txtngay.Text = Get_CauHinh.ngay_text;
            Look_tenlai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            lool_nhomLai.Properties.DataSource = DuLieu.List_Nhom_NCC.Tables[0];
            this.Load += new EventHandler(BaoCao_SoBaoCao_Le_Load);
            //khai bai report truoc

        }
        public static int _LoaiChon = -1;
        void link_tongluong_Click(object sender, EventArgs e)
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _LoaiChon = 1;               
                    #region
                    DataRow dtr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);

                    string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                    string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                    string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                    double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                    string sql_select = "select Tbl_GIAODICH_LE.*,Tbl_Hang.Name as Tbl_Hang_Name from  Tbl_GIAODICH_LE inner join Tbl_Hang  on Tbl_Hang.ID = Tbl_GIAODICH_LE.ID_Hang  where Tbl_GIAODICH_LE.Date_Num_Final_UpDate = " + ngaysua + " ";
                    sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                    if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                    {
                        sql_select += " and ID_KhachHang is null ";
                    }
                    else
                    {
                        sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                    }
                    sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                    sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";
                    DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                    //if (dt_update.Rows.Count > 0)
                    //{
                    //    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                    //    {
                    //        Logic_Sua_Le frm = new Logic_Sua_Le();
                    //        frm.ShowDialog();
                    //    }
                    //}
                if (_LoaiChon == 1 || _LoaiChon == 2)
                {
                    _Sua_tongLuong = new Sua_tongLuong();
                    _Sua_tongLuong.dtr_ds_chitiet = dt_update;
                    _Sua_tongLuong.idkh = ID_KhachHang_Truoc;
                    _Sua_tongLuong.tongluongtruoc = String.Format("{0:N1}", float.Parse(dtr["Sum_SL"].ToString()));
                    _Sua_tongLuong.Text = "Tổng Lượng : Trước " + String.Format("{0:N1}", float.Parse(dtr["Sum_SL"].ToString())) + " | Sau " + String.Format("{0:N1}", float.Parse(dtr["Sum_SL"].ToString()));
                    _Sua_tongLuong.FormClosed += new FormClosedEventHandler(_Sua_tongLuong_FormClosed);
                    _Sua_tongLuong.ShowDialog();
                }

                    #endregion
               
            }
        }

        void _Sua_tongLuong_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        Sua_tongLuong _Sua_tongLuong;
        void link_kh_Click(object sender, EventArgs e)
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                DataTable dt_sl = new DataTable();
                dt_sl.Columns.Add("ID");
                dt_sl.Columns.Add("Tbl_KhachHang_Name");
                dt_sl.Columns.Add("ID_KhachHang");
                dt_sl.Columns.Add("ID_KhachHang_Truoc");
                dt_sl.Columns.Add("Date_Num_Final_UpDate");

                dt_sl.Columns.Add("ToaLe");
                dt_sl.Columns.Add("TongTien", typeof(double));
                dt_sl.Columns.Add("TienMat", typeof(double));
                dt_sl.Columns.Add("_ID_HD_LE");
                string tenkh = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString().ToUpper();
                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                string sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                {
                    sql_select += " and ID_KhachHang is null ";
                }
                else
                {
                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                }
                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";
                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];




                for (int i = 0; i < dt_update.Rows.Count; i++)
                {
                    DataView dtv_1hoadon = new DataView(DuLieu.ToaLe_Full.Tables[0]);
                    dtv_1hoadon.RowFilter = "_ID_HD_LE = '" + dt_update.Rows[i]["_ID_HD_LE"].ToString().ToUpper() + "'";
                    DataTable dt = dtv_1hoadon.ToTable();
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["ID_KhachHang"].ToString().ToUpper() == ID_KhachHang_Truoc.ToUpper())
                        {

                            #region
                            DataRow[] dr_check = dt_sl.Select("ID_KhachHang = '" + ID_KhachHang_Truoc.ToUpper() + "' and _ID_HD_LE = '" + dt.Rows[0]["_ID_HD_LE"].ToString().ToUpper() + "'");
                            if (dr_check.Length == 0)
                            {
                                DataRow dr = dt_sl.NewRow();
                                dr["Tbl_KhachHang_Name"] = dt.Rows[0]["Tbl_KhachHang_Name"].ToString();
                                dr["ID_KhachHang"] = ID_KhachHang_Truoc;
                                dr["ID_KhachHang_Truoc"] = ID_KhachHang_Truoc;
                                dr["Date_Num_Final_UpDate"] = dt.Rows[0]["Date_Num_Final_UpDate"].ToString().ToUpper();
                                dr["ToaLe"] = dt.Rows[0]["ToaLe"].ToString().ToUpper();
                                dr["TongTien"] = double.Parse(dt.Rows[0]["TongTien"].ToString());
                                dr["TienMat"] = double.Parse(dt.Rows[0]["TienMat"].ToString());
                                dr["_ID_HD_LE"] = dt.Rows[0]["_ID_HD_LE"].ToString().ToUpper();
                                dr["ID"]= dt_update.Rows[i]["ID"].ToString().ToUpper();
                                dt_sl.Rows.Add(dr);
                            }
                            #endregion
                        }
                    }
                }





                _Sua_KH = new Sua_KH();
                _Sua_KH.dtr_ds_toa = dt_sl;
                _Sua_KH.Text = tenkh;
                _Sua_KH.FormClosed += new FormClosedEventHandler(_Sua_KH_FormClosed);
                _Sua_KH.ShowDialog();


                #endregion
            }
        }
        void _Sua_KH_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        Sua_KH _Sua_KH;
        void BaoCao_SoBaoCao_Le_Load(object sender, EventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length == 0)
            {
                DataRow dr_new = DuLieu.List_Hang.Tables[0].NewRow();
                dr_new["Name"] = "        ''        ";
                DuLieu.List_Hang.Tables[0].Rows.Add(dr_new);
            }
            // this.FormClosed += new FormClosedEventHandler(BaoCao_SoBaoCao_Le_FormClosed);
            tailai();

        }

        void BaoCao_SoBaoCao_Le_FormClosed(object sender, FormClosedEventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length > 0)
            {
                for (int d = 0; d < dr.Length; d++)
                {
                    DuLieu.List_Hang.Tables[0].Rows.Remove(dr[d]);
                }
                DuLieu.List_Hang.Tables[0].AcceptChanges();
            }
        }


        #region gv_daluu
        public int ngaysua;
        Class_KetNoi db = new Class_KetNoi();
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            string _toaID = "";
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (e.Column.FieldName != "Select")
                {
                    clsConnect cls = new clsConnect();
                    _LoaiChon = 1;
                    string _IDToa_Le = "";
                    switch (e.Column.FieldName)
                    {
                        case "Gia_1":
                            {
                                #region
                                //select *,ID_NCC AS ID_NCC_Truoc,ID_KhachHang as ID_KhachHang_Truoc,ID_Hang as ID_Hang_Truoc,Gia_1 as Gia_1_Truoc from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend 
                                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                                string sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                {
                                    sql_select += " and ID_KhachHang is null ";
                                }
                                else
                                {
                                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                }
                                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";

                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                        Logic_Sua_Le frm = new Logic_Sua_Le();
                                        frm.ShowDialog();
                                    }
                                }
                                double TienCu = 0;
                                if (dt_update.Rows.Count > 0)
                                    TienCu = double.Parse(dt_update.Rows[0]["SL"].ToString()) * double.Parse(dt_update.Rows[0]["Gia_1"].ToString());
                                double _sl = (gv_daluu.GetFocusedRowCellValue("Sum_SL") == null) ? 0 : double.Parse(gv_daluu.GetFocusedRowCellValue("Sum_SL").ToString());
                                double _gia1 = (gv_daluu.GetFocusedRowCellValue(e.Column.FieldName) == null) ? 0 : double.Parse(gv_daluu.GetFocusedRowCellValue(e.Column.FieldName).ToString());
                                string sql = "update Tbl_GIAODICH_LE set Gia_1='" + _gia1 + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                //
                                sql = "update Tbl_GIAODICH_LE set _Num1="+ _sl* _gia1+",_Num2="+_sl*_gia1+" where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                if (_LoaiChon == 2)
                                {
                                    sql = "update Tbl_GIAODICH_LE set Toa_Gia='" + _gia1 + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                    cls.UpdateTable(sql);
                                    //
                                    double TtienHang = _sl* _gia1;
                                    sql = "update TOA set TTHANG='" + TtienHang + "',THANH_TOAN=TKHIEN+TIEN+TNUOC+TUNG+TXE+TKY+" + TtienHang + " where ID='" + _toaID + "'";
                                    cls.UpdateTable(sql);
                                }
                                double _TienMoi = 0;

                                string sqlTien = "select * from Tbl_GIAODICH_LE where ID ='" + gv_daluu.GetFocusedRowCellValue("ID").ToString() + "'";

                                DataTable dtNo = cls.LoadTable(sqlTien);
                                for (int i = 0; i < dtNo.Rows.Count; i++)
                                {
                                    _TienMoi = double.Parse(dtNo.Rows[i]["SL"].ToString()) * double.Parse(dtNo.Rows[i]["Gia_1"].ToString());
                                    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);

                                    string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                    sql3 += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                    sql3 += "'" + _IDToa_Le + "',";
                                    sql3 += "'LE',";
                                    sql3 += ngaysua + ",";
                                    sql3 += _TienMoi - TienCu + "";
                                    sql3 += ")";
                                    cls.UpdateTable(sql3);

                                }

                                #endregion
                                break;
                            }
                        case "Tbl_NCC_Name":
                            {
                                #region
                                DataRowView row_ncc = look_thuonglai_daluu.GetDataSourceRowByKeyValue(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_NCC_Name"].ToString()) as DataRowView;
                                string id_ncc_new = row_ncc["ID"].ToString().ToUpper();
                                //select *,ID_NCC AS ID_NCC_Truoc,ID_KhachHang as ID_KhachHang_Truoc,ID_Hang as ID_Hang_Truoc,Gia_1 as Gia_1_Truoc from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend 
                                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                                string sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                {
                                    sql_select += " and ID_KhachHang is null ";
                                }
                                else
                                {
                                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                }
                                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";
                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                        Logic_Sua_Le frm = new Logic_Sua_Le();
                                        frm.ShowDialog();
                                    }
                                }
                                string sql = "update Tbl_GIAODICH_LE set ID_NCC='" + id_ncc_new + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                if ( _LoaiChon == 2)
                                {
                                   
                                    string sql2 = "update TOA set NCC_ID='" + id_ncc_new + "' where ID='" + _toaID + "'";
                                    cls.UpdateTable(sql2);
                                }
                                double _TienNo = 0;
                                //update Tbl_CongNo
                                string sqlTien = "select * from ToaLe_HoaDon where _ID_HD_LE ='" + _IDToa_Le + "' and Date_Num_Final_UpDate =" + ngaysua;
                                DataTable dtNo = cls.LoadTable(sqlTien);
                                for (int i = 0; i < dtNo.Rows.Count; i++)
                                {
                                    _TienNo = double.Parse(dtNo.Rows[i]["TongTien"].ToString());
                                    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    if (dtCheck.Rows.Count > 0)
                                        cls.UpdateTable("update Tbl_CONGNO set Tien=" + _TienNo + " where ID_Auto=" + dtCheck.Rows[0]["ID_Auto"].ToString().Trim());
                                    else
                                    {
                                        string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                        sql3 += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                        sql3 += "'" + _IDToa_Le + "',";
                                        sql3 += "'LE',";
                                        sql3 += ngaysua + ",";
                                        sql3 += _TienNo + "";
                                        sql3 += ")";
                                        cls.UpdateTable(sql3);
                                    }
                                }

                                //if (_LoaiChon == 1 || _LoaiChon == 2)
                                //{
                                //    for (int i = 0; i < dt_update.Rows.Count; i++)
                                //    {
                                //        #region
                                //        string sql = (_LoaiChon == 1) ? "Tbl_GIAODICH_LE_insert2019 " : "Tbl_GIAODICH_LE_insert2019_TOA ";
                                //        // @STT INT,
                                //        sql += "N'" + dt_update.Rows[i]["STT"].ToString() + "',";
                                //        //@ID uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["ID"].ToString().ToUpper() + "',";
                                //        //@ID_Hang uniqueidentifier,
                                //        sql += "N'" + ID_Hang_Truoc + "',";
                                //        //@ID_KhachHang uniqueidentifier,
                                //        if (ID_KhachHang_Truoc.Length > 10)
                                //        {
                                //            sql += "N'" + ID_KhachHang_Truoc.ToUpper() + "',";
                                //        }
                                //        else
                                //        {
                                //            sql += "Null,";
                                //        }
                                //        //@ID_NCC uniqueidentifier,
                                //        sql += "N'" + id_ncc_new + "',";

                                //        //@Date_Num_Final_UpDate int,
                                //        sql += "N'" + ngaysua + "',";
                                //        //@User_ID_Final_UpDate uniqueidentifier,
                                //        sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                                //        //@IS_User  bit,
                                //        sql += "N'1',";
                                //        //@NguonGoc nvarchar(100),
                                //        sql += "N'LE',";


                                //        //@SL decimal,
                                //        sql += "N'" + dt_update.Rows[i]["SL"].ToString() + "',";
                                //        //@Gia_0 decimal,
                                //        sql += "Null,";
                                //        //@Gia_1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["Gia_1"].ToString() + "',";
                                //        //@Gia_2 decimal,
                                //        sql += "Null,";
                                //        //@Sot int,
                                //        sql += "Null,";
                                //        //@Ket int,
                                //        sql += "Null,";
                                //        //@TienMat decimal,
                                //        sql += "Null,";

                                //        //@Toa_ID uniqueidentifier,
                                //        sql += "Null,";
                                //        //@Toa_SL decimal,
                                //        sql += "Null,";
                                //        //@Toa_Gia decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHSL decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHPC decimal,
                                //        sql += "Null,";
                                //        //@Toa_Xoa bit,
                                //        sql += "Null,";

                                //        //@_Text1 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text2 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text3 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text4 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text5 nvarchar(100),
                                //        sql += "Null,";


                                //        //@_Num1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["_Num1"].ToString() + "',";
                                //        //@_Num2 decimal,

                                //        sql += "N'" + dt_update.Rows[i]["_Num2"].ToString() + "',";


                                //        //@_Num3 decimal,
                                //        sql += "Null,";
                                //        //@_ID1 uniqueidentifier,
                                //        sql += "Null,";

                                //        //@_SoHD_LE int,
                                //        sql += "N'" + dt_update.Rows[i]["_SoHD_LE"].ToString() + "',";
                                //        //@_ID_HD_LE uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["_ID_HD_LE"].ToString() + "',";
                                //        //@_Node_LE nvarchar(100),
                                //        sql += "N'" + dt_update.Rows[i]["_Node_LE"].ToString() + "',";
                                //        //@_ID_Lai_TienMat uniqueidentifier
                                //        sql += "Null,"; //kiem tran duoi sql
                                //        //Lu
                                //        sql += "Null,";
                                //        //Cat
                                //        sql += "Null,";
                                //        //Bom
                                //        sql += "Null"; 
                                //        //toa la lay 2 cot cong lai
                                //        db.T_exc(sql);

                                //        #endregion
                                //    }
                                //}
                                #endregion
                                break;
                            }
                        case "Tbl_KhachHang_Name":
                            {
                                #region
                                string _So_HD = gv_daluu.GetFocusedRowCellValue("_SoHD_LE").ToString();
                                string _ID= gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID"].ToString().ToUpper();
                                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                                string idCu = ID_KhachHang_Truoc;
                                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                                string sql_select = "select * from Tbl_GIAODICH_LE where ID = '" + gv_daluu.GetFocusedRowCellValue("ID").ToString() + "' ";
                                //sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                //if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                //{
                                //    sql_select += " and ID_KhachHang is null ";
                                //}
                                //else
                                //{
                                //    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                //}
                                //sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                //sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";

                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                    }
                                }
                                DataRowView row_kh = look_khachhang_daluu.GetDataSourceRowByKeyValue(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString()) as DataRowView;
                                string id_kh = row_kh["ID"].ToString().ToUpper();

                                string sql = "update Tbl_GIAODICH_LE set ID_KhachHang='" + id_kh + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                double _TienNo = 0;
                                //update Tbl_CongNo

                                #endregion

                                //cập nhật công nợ kh mới
                                double TienCu = 0;
                                DataTable dt4 = db.T_opendataset("select SL*Gia_1 from Tbl_GIAODICH_LE where ID='"+_ID+"'").Tables[0];
                                if (dt4.Rows.Count > 0)
                                    TienCu = double.Parse(dt4.Rows[0][0].ToString());
                                    #region khach moi

                                ID_KhachHang_Truoc = id_kh;

                                sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                {
                                    sql_select += " and ID_KhachHang is null ";
                                }
                                else
                                {
                                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                }
                                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";

                                dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                    }
                                }



                                _TienNo = 0;
                                //update Tbl_CongNo
                                string sqlTien = "select * from ToaLe_HoaDon where _ID_HD_LE ='" + _IDToa_Le + "' and Date_Num_Final_UpDate =" + ngaysua;
                                DataTable dtNo = cls.LoadTable(sqlTien);
                                for (int i = 0; i < dtNo.Rows.Count; i++)
                                {
                                    _TienNo = double.Parse(dtNo.Rows[i]["TongTien"].ToString());
                                    //
                                    // cls.UpdateTable("update ToaLe_HoaDon TienMat=0,Tien_No=TongTien where _ID_HD_LE='"+ _IDToa_Le+"' and Date_Num_Final_UpDate = " + ngaysua);
                                    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    if (dtCheck.Rows.Count > 0)
                                        cls.UpdateTable("update Tbl_CONGNO set Tien=" + _TienNo + " where ID_Auto=" + dtCheck.Rows[0]["ID_Auto"].ToString().Trim());
                                    else
                                    {
                                        string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                        sql3 += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                        sql3 += "'" + _IDToa_Le + "',";
                                        sql3 += "'LE',";
                                        sql3 += ngaysua + ",";
                                        sql3 += _TienNo + "";
                                        sql3 += ")";
                                        cls.UpdateTable(sql3);
                                    }
                                    //string sqlCN = "Tbl_CONGNO_insert ";
                                    //sqlCN += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                    //sqlCN += "'" + _IDToa_Le + "',";
                                    //sqlCN += "'LE',";
                                    //sqlCN += ngaysua + ",";
                                    //sqlCN += "'" + _TienNo + "'";
                                    //db.T_exc(sqlCN);
                                }
                                //if (dtNo.Rows.Count > 0)
                                //{

                                //    string sqlCN = "Tbl_CONGNO_insert ";
                                //    sqlCN += "'" + id_kh.ToUpper() + "',";
                                //    sqlCN += "'" + _IDToa_Le + "',";
                                //    sqlCN += "'LE',";
                                //    sqlCN += ngaysua + ",";
                                //    sqlCN += "'" + _TienNo + "'";
                                //    db.T_exc(sqlCN);
                                //}
                                #endregion
                                #region khach cu
                               
                                   
                                  
                                    DataTable dtCheck2 = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + idCu.ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    if (dtCheck2.Rows.Count > 0)
                                        cls.UpdateTable("delete Tbl_CONGNO  where ID_Auto=" + dtCheck2.Rows[0]["ID_Auto"].ToString().Trim());
                                    
                                    
                               
                            
                               
                                #endregion
                                break;
                            }
                        case "Sum_SL":
                            {
                                #region
                                DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["CA_View"].ToString()) as DataRowView;
                                string id_ca = row_hang["ID"].ToString().ToUpper();
                                if (id_ca == null || id_ca == "")
                                {

                                    for (int i = gv_daluu.FocusedRowHandle; i >= 0; i--)
                                    {
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                                        if (gv_daluu.GetDataRow(i)["CA_View"] != "        ''        ")
                                        {
                                            id_ca = gv_daluu.GetDataRow(i)["ID_Hang"].ToString();
                                            break;
                                        }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
                                    }
                                }

                                //select *,ID_NCC AS ID_NCC_Truoc,ID_KhachHang as ID_KhachHang_Truoc,ID_Hang as ID_Hang_Truoc,Gia_1 as Gia_1_Truoc from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend 
                                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                                string sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                {
                                    sql_select += " and ID_KhachHang is null ";
                                }
                                else
                                {
                                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                }
                                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";
                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                        Logic_Sua_Le frm = new Logic_Sua_Le();
                                        frm.ShowDialog();
                                    }
                                }
                                double TienCu = 0;
                                if (dt_update.Rows.Count > 0)
                                    TienCu = double.Parse(dt_update.Rows[0]["SL"].ToString())* double.Parse(dt_update.Rows[0]["Gia_1"].ToString());
                                double _sl = (gv_daluu.GetFocusedRowCellValue(e.Column.FieldName) == null) ? 0 : double.Parse(gv_daluu.GetFocusedRowCellValue(e.Column.FieldName).ToString());
                                string sql = "update Tbl_GIAODICH_LE set SL='" + _sl + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                //
                                sql = "update Tbl_GIAODICH_LE set _Num1=SL*Gia_1,_Num2=SL*Gia_1 where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                if ( _LoaiChon == 2)
                                {
                                    sql = "update Tbl_GIAODICH_LE set Toa_SL='"+ _sl+"' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                    cls.UpdateTable(sql);
                                    //
                                    double TtienHang = 0;
                                    //DataTable dtH = cls.LoadTable("select SL*Gia_1 from Tbl_GIAODICH_LE where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim()+"'");
                                    DataTable dtH = cls.LoadTable("select sum(SL*Gia_1) from Tbl_GIAODICH_LE where Toa_ID in(select Toa_ID from Tbl_GIAODICH_LE where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim()+"')");
                                    if (dtH.Rows.Count > 0)
                                        TtienHang = double.Parse(dtH.Rows[0][0].ToString());
                                    sql = "update TOA set TTHANG='" + TtienHang + "',THANH_TOAN=TKHIEN+TIEN+TNUOC+TUNG+TXE+TKY+"+ TtienHang+" where ID='" + _toaID + "'";
                                    cls.UpdateTable(sql);
                                }
                                double _TienMoi = 0;
                                
                                string sqlTien = "select * from Tbl_GIAODICH_LE where ID ='" + gv_daluu.GetFocusedRowCellValue("ID").ToString() + "'";
                                
                                DataTable dtNo = cls.LoadTable(sqlTien);
                                for (int i = 0; i < dtNo.Rows.Count; i++)
                                {
                                    _TienMoi = double.Parse(dtNo.Rows[i]["SL"].ToString()) * double.Parse(dtNo.Rows[i]["Gia_1"].ToString());
                                    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                  
                                        string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                        sql3 += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                        sql3 += "'" + _IDToa_Le + "',";
                                        sql3 += "'LE',";
                                        sql3 += ngaysua + ",";
                                        sql3 += _TienMoi - TienCu + "";
                                        sql3 += ")";
                                        cls.UpdateTable(sql3);
                                    
                                }
                                //if (_LoaiChon == 1 || _LoaiChon == 2)
                                //{
                                //    for (int i = 0; i < dt_update.Rows.Count; i++)
                                //    {
                                //        #region
                                //        string sql = (_LoaiChon == 1) ? "Tbl_GIAODICH_LE_insert2019 " : "Tbl_GIAODICH_LE_insert2019_TOA ";
                                //        // @STT INT,
                                //        sql += "N'" + dt_update.Rows[i]["STT"].ToString() + "',";
                                //        //@ID uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["ID"].ToString().ToUpper() + "',";
                                //        //@ID_Hang uniqueidentifier,
                                //        sql += "N'" + id_ca + "',";
                                //        //@ID_KhachHang uniqueidentifier,
                                //        if (ID_KhachHang_Truoc.Length > 10)
                                //        {
                                //            sql += "N'" + ID_KhachHang_Truoc.ToUpper() + "',";
                                //        }
                                //        else
                                //        {
                                //            sql += "Null,";
                                //        }
                                //        //@ID_NCC uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["ID_NCC"].ToString().ToUpper() + "',";

                                //        //@Date_Num_Final_UpDate int,
                                //        sql += "N'" + ngaysua + "',";
                                //        //@User_ID_Final_UpDate uniqueidentifier,
                                //        sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                                //        //@IS_User  bit,
                                //        sql += "N'1',";
                                //        //@NguonGoc nvarchar(100),
                                //        sql += "N'LE',";


                                //        //@SL decimal,
                                //        sql += "N'" + dt_update.Rows[i]["SL"].ToString() + "',";
                                //        //@Gia_0 decimal,
                                //        sql += "Null,";
                                //        //@Gia_1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["Gia_1"].ToString() + "',";
                                //        //@Gia_2 decimal,
                                //        sql += "Null,";
                                //        //@Sot int,
                                //        sql += "Null,";
                                //        //@Ket int,
                                //        sql += "Null,";
                                //        //@TienMat decimal,
                                //        sql += "Null,";

                                //        //@Toa_ID uniqueidentifier,
                                //        sql += "Null,";
                                //        //@Toa_SL decimal,
                                //        sql += "Null,";
                                //        //@Toa_Gia decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHSL decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHPC decimal,
                                //        sql += "Null,";
                                //        //@Toa_Xoa bit,
                                //        sql += "Null,";

                                //        //@_Text1 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text2 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text3 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text4 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text5 nvarchar(100),
                                //        sql += "Null,";


                                //        //@_Num1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["_Num1"].ToString() + "',";
                                //        //@_Num2 decimal,

                                //        sql += "N'" + dt_update.Rows[i]["_Num2"].ToString() + "',";


                                //        //@_Num3 decimal,
                                //        sql += "Null,";
                                //        //@_ID1 uniqueidentifier,
                                //        sql += "Null,";

                                //        //@_SoHD_LE int,
                                //        sql += "N'" + dt_update.Rows[i]["_SoHD_LE"].ToString() + "',";
                                //        //@_ID_HD_LE uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["_ID_HD_LE"].ToString() + "',";
                                //        //@_Node_LE nvarchar(100),
                                //        sql += "N'" + dt_update.Rows[i]["_Node_LE"].ToString() + "',";
                                //        //@_ID_Lai_TienMat uniqueidentifier
                                //        sql += "Null,"; //kiem tran duoi sql
                                //        //Lu
                                //        sql += "Null,";
                                //        //Cat
                                //        sql += "Null,";
                                //        //Bom
                                //        sql += "Null"; 
                                //        //toa la lay 2 cot cong lai
                                //        db.T_exc(sql);

                                //        #endregion
                                //    }
                                //}
                                #endregion
                                break;
                            }
                        case "CA_View":
                            {
                                #region
                                DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["CA_View"].ToString()) as DataRowView;
                                string id_ca = row_hang["ID"].ToString().ToUpper();
                                if (id_ca == null || id_ca == "")
                                {

                                    for (int i = gv_daluu.FocusedRowHandle; i >= 0; i--)
                                    {
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                                        if (gv_daluu.GetDataRow(i)["CA_View"] != "        ''        ")
                                        {
                                            id_ca = gv_daluu.GetDataRow(i)["ID_Hang"].ToString();
                                            break;
                                        }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
                                    }
                                }

                                //select *,ID_NCC AS ID_NCC_Truoc,ID_KhachHang as ID_KhachHang_Truoc,ID_Hang as ID_Hang_Truoc,Gia_1 as Gia_1_Truoc from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend 
                                string ID_NCC_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_NCC_Truoc"].ToString().ToUpper();
                                string ID_KhachHang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper();
                                string ID_Hang_Truoc = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_Hang_Truoc"].ToString().ToUpper();
                                double Gia_1_Truoc = double.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Gia_1_Truoc"].ToString().ToUpper());
                                string sql_select = "select * from Tbl_GIAODICH_LE where Date_Num_Final_UpDate = " + ngaysua + " ";
                                sql_select += " and ID_NCC= '" + ID_NCC_Truoc + "'";
                                if (ID_KhachHang_Truoc == "" || ID_KhachHang_Truoc == null)
                                {
                                    sql_select += " and ID_KhachHang is null ";
                                }
                                else
                                {
                                    sql_select += " and ID_KhachHang= '" + ID_KhachHang_Truoc + "'";
                                }
                                sql_select += " and ID_Hang= '" + ID_Hang_Truoc + "'";
                                sql_select += " and Gia_1= '" + Gia_1_Truoc + "'";
                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];                             
                                if (dt_update.Rows.Count > 0)
                                {
                                    _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    if (dt_update.Rows[0]["Toa_ID"].ToString().Trim().Length > 5)
                                    {
                                        Logic_Sua_Le frm = new Logic_Sua_Le();
                                        frm.ShowDialog();
                                    }
                                }
                                string sql = "update Tbl_GIAODICH_LE set ID_Hang='" + id_ca + "' where ID='" + gv_daluu.GetFocusedRowCellValue("ID").ToString().Trim() + "'";
                                cls.UpdateTable(sql);
                                double _TienNo = 0;
                            //update Tbl_CongNo
                            var idHoaDonLeToken = string.IsNullOrEmpty(_IDToa_Le) ?
                                " is null " : $" = '{_IDToa_Le}'";
                                string sqlTien = "select * from ToaLe_HoaDon where _ID_HD_LE " + idHoaDonLeToken + " and Date_Num_Final_UpDate =" + ngaysua;
                                DataTable dtNo = cls.LoadTable(sqlTien);
                                for (int i = 0; i < dtNo.Rows.Count; i++)
                                {
                                var idKhachHang = dtNo.Rows[i]["ID_KhachHang"]?.ToString()?.ToUpper();
                                var idKhachHangToken = string.IsNullOrEmpty(idKhachHang) ?
                                    " is null " : $" = '{idKhachHang}' ";
                                var idkhachhangParam = string.IsNullOrEmpty(idKhachHang) ?" null ": $" '{idKhachHang}' ";
                                    _TienNo = double.Parse(dtNo.Rows[i]["TongTien"].ToString());
                                    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang" + idKhachHangToken + " and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    if (dtCheck.Rows.Count > 0)
                                        cls.UpdateTable("update Tbl_CONGNO set Tien=" + _TienNo + " where ID_Auto=" + dtCheck.Rows[0]["ID_Auto"].ToString().Trim());
                                    else
                                    {
                                    var idToaLeParam = string.IsNullOrEmpty(_IDToa_Le) ?
                                        " null " : $"'{_IDToa_Le}'";
                                        string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                        sql3 += idkhachhangParam + ",";
                                        sql3 += idToaLeParam + ",";
                                        sql3 += "'LE',";
                                        sql3 += ngaysua + ",";
                                        sql3 += _TienNo + "";
                                        sql3 += ")";
                                        cls.UpdateTable(sql3);
                                    }
                                }
                                //if (_LoaiChon == 1 || _LoaiChon == 2)
                                //{
                                //    for (int i = 0; i < dt_update.Rows.Count; i++)
                                //    {
                                //        #region
                                //        string sql = (_LoaiChon == 1) ? "Tbl_GIAODICH_LE_insert2019 " : "Tbl_GIAODICH_LE_insert2019_TOA ";
                                //        // @STT INT,
                                //        sql += "N'" + dt_update.Rows[i]["STT"].ToString() + "',";
                                //        //@ID uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["ID"].ToString().ToUpper() + "',";
                                //        //@ID_Hang uniqueidentifier,
                                //        sql += "N'" + id_ca + "',";
                                //        //@ID_KhachHang uniqueidentifier,
                                //        if (ID_KhachHang_Truoc.Length > 10)
                                //        {
                                //            sql += "N'" + ID_KhachHang_Truoc.ToUpper() + "',";
                                //        }
                                //        else
                                //        {
                                //            sql += "Null,";
                                //        }
                                //        //@ID_NCC uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["ID_NCC"].ToString().ToUpper() + "',";

                                //        //@Date_Num_Final_UpDate int,
                                //        sql += "N'" + ngaysua + "',";
                                //        //@User_ID_Final_UpDate uniqueidentifier,
                                //        sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                                //        //@IS_User  bit,
                                //        sql += "N'1',";
                                //        //@NguonGoc nvarchar(100),
                                //        sql += "N'LE',";


                                //        //@SL decimal,
                                //        sql += "N'" + dt_update.Rows[i]["SL"].ToString() + "',";
                                //        //@Gia_0 decimal,
                                //        sql += "Null,";
                                //        //@Gia_1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["Gia_1"].ToString() + "',";
                                //        //@Gia_2 decimal,
                                //        sql += "Null,";
                                //        //@Sot int,
                                //        sql += "Null,";
                                //        //@Ket int,
                                //        sql += "Null,";
                                //        //@TienMat decimal,
                                //        sql += "Null,";

                                //        //@Toa_ID uniqueidentifier,
                                //        sql += "Null,";
                                //        //@Toa_SL decimal,
                                //        sql += "Null,";
                                //        //@Toa_Gia decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHSL decimal,
                                //        sql += "Null,";
                                //        //@Toa_HHPC decimal,
                                //        sql += "Null,";
                                //        //@Toa_Xoa bit,
                                //        sql += "Null,";

                                //        //@_Text1 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text2 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text3 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text4 nvarchar(100),
                                //        sql += "Null,";
                                //        //@_Text5 nvarchar(100),
                                //        sql += "Null,";


                                //        //@_Num1 decimal,
                                //        sql += "N'" + dt_update.Rows[i]["_Num1"].ToString() + "',";
                                //        //@_Num2 decimal,

                                //        sql += "N'" + dt_update.Rows[i]["_Num2"].ToString() + "',";


                                //        //@_Num3 decimal,
                                //        sql += "Null,";
                                //        //@_ID1 uniqueidentifier,
                                //        sql += "Null,";

                                //        //@_SoHD_LE int,
                                //        sql += "N'" + dt_update.Rows[i]["_SoHD_LE"].ToString() + "',";
                                //        //@_ID_HD_LE uniqueidentifier,
                                //        sql += "N'" + dt_update.Rows[i]["_ID_HD_LE"].ToString() + "',";
                                //        //@_Node_LE nvarchar(100),
                                //        sql += "N'" + dt_update.Rows[i]["_Node_LE"].ToString() + "',";
                                //        //@_ID_Lai_TienMat uniqueidentifier
                                //        sql += "Null,"; //kiem tran duoi sql
                                //        //Lu
                                //        sql += "Null,";
                                //        //Cat
                                //        sql += "Null,";
                                //        //Bom
                                //        sql += "Null"; 
                                //        //toa la lay 2 cot cong lai
                                //        db.T_exc(sql);

                                //        #endregion
                                //    }
                                //}
                                #endregion
                                break;
                            }
                           
                    }
                    tailai();

                }
                else
                {

                }
            }
            else
            {
                gv_daluu.CancelUpdateCurrentRow();
            }

        }
        #endregion

        #region lệnh dùng chung
        public void excel()
        {
        }
        public void xoa()
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                ///if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
                //{
                try
                {
                    for (int i = 0; i < gv_daluu.RowCount; i++)
                    {
                        if (gv_daluu.GetRowCellValue(i, "Select") != null)
                        {
                            if (gv_daluu.GetRowCellValue(i, "Select").ToString() == "True")
                            {


                                string sql_select = "select * from Tbl_GIAODICH_LE where ID = '" + gv_daluu.GetRowCellDisplayText(i, "ID").ToString() + "'";
                                string _IDToa_Le = "";
                                string _toaID = "";
                                DataTable dt_update = db.T_opendataset(sql_select).Tables[0];
                                if (dt_update.Rows.Count > 0)
                                {

                                    if (dt_update.Columns.Contains("_ID_HD_LE"))
                                    {
                                        _IDToa_Le = dt_update.Rows[0]["_ID_HD_LE"].ToString().Trim();
                                    }
                                    //else
                                    //{
                                    //    // Handle the case where the column does not exist
                                    //    //throw new Exception("Column '_IDToa_Le' does not exist in the result set.");
                                    //}
                                    // _IDToa_Le= dt_update.Rows[0]["_IDToa_Le"].ToString().Trim();
                                    _toaID = dt_update.Rows[0]["Toa_ID"].ToString().Trim();
                                    clsConnect cls = new clsConnect();
                                    if (_toaID != "")
                                    {
                                        if (MessageBox.Show("Bạn có muốn xoá Toa theo mặt hàng này không (Y/N)?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                        {
                                            cls.UpdateTable("Delete from TOA where ID='" + _toaID.Trim() + "'");

                                        }
                                    }
                                    cls.UpdateTable("delete from Tbl_GIAODICH_LE where ID='" + gv_daluu.GetRowCellValue(i, "ID").ToString().Trim() + "'");
                                    //update Tbl_CongNo

                                    string id_kh = dt_update.Rows[0]["ID_KhachHang"].ToString().Trim();
                                    //_IDToa_Le = gv_daluu.GetFocusedRowCellValue("_ID_HD_LE").ToString().Trim();
                                    cls.UpdateTable("delete from Tbl_CONGNO where ID_KhachHang='" + id_kh.ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    //string sqlTien = "select * from ToaLe_HoaDon where ID_KhachHang='" + id_kh + "' and _ID_HD_LE ='" + _IDToa_Le + "' and Date_Num_Final_UpDate =" + ngaysua;
                                    //DataTable dtNo = cls.LoadTable(sqlTien);
                                    //double _TienNo = 0;
                                    //for (int k = 0; k < dtNo.Rows.Count; k++)
                                    //{
                                    //    _TienNo = double.Parse(dtNo.Rows[k]["TongTien"].ToString());
                                    //    DataTable dtCheck = cls.LoadTable("select * from Tbl_CONGNO where ID_KhachHang='" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "' and ID_Nguon='" + _IDToa_Le + "' and NgayPhatSinhNo=" + ngaysua);
                                    //    if (dtCheck.Rows.Count > 0)
                                    //        cls.UpdateTable("update Tbl_CONGNO set Tien=" + _TienNo + " where ID_Auto=" + dtCheck.Rows[0]["ID_Auto"].ToString().Trim());
                                    //    else
                                    //    {
                                    //        string sql3 = "insert into Tbl_CONGNO(ID_KhachHang,ID_Nguon,NguonPhatSinh,NgayPhatSinhNo,Tien)values(";
                                    //        sql3 += "'" + dtNo.Rows[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                                    //        sql3 += "'" + _IDToa_Le + "',";
                                    //        sql3 += "'LE',";
                                    //        sql3 += ngaysua + ",";
                                    //        sql3 += _TienNo + "";
                                    //        sql3 += ")";
                                    //        cls.UpdateTable(sql3);
                                    //    }
                                    //}
                                    //if (dtNo.Rows.Count > 0)
                                    //{

                                    //    string sqlCN = "Tbl_CONGNO_insert ";
                                    //    sqlCN += "'" + id_kh.ToUpper() + "',";
                                    //    sqlCN += "'" + _IDToa_Le + "',";
                                    //    sqlCN += "'LE',";
                                    //    sqlCN += ngaysua + ",";
                                    //    sqlCN += "'" + _TienNo + "'";
                                    //    db.T_exc(sqlCN);
                                    //}
                                }

                            }
                        }

                    }
                }
                catch (Exception)
                {
                }
                
                
                //DataRow[] dr = DuLieu.BaoCaoLe.Tables[0].Select("Select = " + true + "");
                //for (int i = 0; i < dr.Length; i++)
                //{
                //    //old truoc gay 18/07/2024
                //    // string sql = "NhapLe_Xoa_Toa '" + dr[i]["ID"].ToString().ToUpper() + "'";
                //    // _NhapSi_Class.set(sql);
                //    // DuLieu.BaoCaoLe.Tables[0].Rows.Remove(dr[i]);
                //    //fix 18/07/2024, thay doi noi dung store pro
                //    string sql = "NhapLe_Xoa_Toa1807 '" + dr[i]["ID"].ToString().ToUpper() + "',";
                //    if (dr[i]["ID_KhachHang"].ToString().Length > 10)
                //    {
                //        sql += "N'" + dr[i]["ID_KhachHang"].ToString().ToUpper() + "',";

                //    }
                //    else
                //    {
                //        sql += "Null,";
                //    }
                //    sql += "'" + dr[i]["Date_Num_Final_UpDate"].ToString() + "'";
                //    db.T_exc(sql);
                //
                //  }
                gv_daluu.RefreshData();
                Thongkesoluong.Text = clum_sl.SummaryText;
                thongketien.Text = clum_thanhtien.SummaryText;
                ThongkeGiaBQ.Text = clum_Gia.SummaryText;
                tailai();

               // }
            }
        }
        //    BaoCao_SoBaoCao_Si_Bien_Edit _BaoCao_SoBaoCao_Si_Bien_Edit;
        public void sua()
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                //_BaoCao_SoBaoCao_Si_Bien_Edit = new BaoCao_SoBaoCao_Si_Bien_Edit();
                //_BaoCao_SoBaoCao_Si_Bien_Edit.FormClosed += new FormClosedEventHandler(_BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed);
                //_BaoCao_SoBaoCao_Si_Bien_Edit.ShowDialog();
                #endregion
            }
        }

        void _BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
            //for (int i = 0; i < DuLieu.BaoCaoLe.Tables[0].Rows.Count; i++)
            //{
            //    DuLieu.BaoCaoLe.Tables[0].Rows[i]["Select"] = true;
            //}
            //DuLieu.BaoCaoLe.Tables[0].AcceptChanges();
            //gv_daluu.RefreshData();
        }
        public void bochon()
        {
            //for (int i = 0; i < DuLieu.BaoCaoLe.Tables[0].Rows.Count; i++)
            //{
            //    DuLieu.BaoCaoLe.Tables[0].Rows[i]["Select"] = false;
            //}
            //DuLieu.BaoCaoLe.Tables[0].AcceptChanges();
            //gv_daluu.RefreshData();
        }
        public void load_combo()
        {
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;

                string load_tl = "select ID_NCC as ID,Tbl_NCC_Name as Name,Date_Num_Final_UpDate from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate having  Date_Num_Final_UpDate = " + ng + " ORDER BY Tbl_NCC_Name asc";
                DuLieu.List_NCC_Tra = _NhapSi_Class.get_dataset(load_tl);
                Look_tenlai.Properties.DataSource = DuLieu.List_NCC_Tra.Tables[0];
            }
            else
            {
                string load_tl = "select ID_NCC as ID,Tbl_NCC_Name as Name,Date_Num_Final_UpDate from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate having Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + " ORDER BY Tbl_NCC_Name asc";
                DuLieu.List_NCC_Tra = _NhapSi_Class.get_dataset(load_tl);
                Look_tenlai.Properties.DataSource = DuLieu.List_NCC_Tra.Tables[0];
            }
        }
        public void tailai()
        {
            look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
            string filter_string = "select *,ID_NCC AS ID_NCC_Truoc,ID_KhachHang as ID_KhachHang_Truoc,ID_Hang as ID_Hang_Truoc,Gia_1 as Gia_1_Truoc,_SoHD_LE from Nhaple_BaoCao_Theo_Lai_ca_gia_KH_extend where Date_Num_Final_UpDate >0 ";
            string sql_hoadon = "";
            if (check_ngay.Checked == true)
            {

                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                sql_hoadon = "select * from Nhaple_HOADON where Date_Num_Final_UpDate = " + ng + "";
                filter_string += "AND Date_Num_Final_UpDate = " + ng + "";
                ngaysua = ng;
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null && Look_tenlai.EditValue != "")
                {
                    string nhomchon = "'" + Look_tenlai.Properties.GetCheckedItems().ToString().Replace(", ", "','") + "'";

                    filter_string += " And ID_NCC IN (" + nhomchon + ")";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            else
            {
                filter_string += "AND Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
                ngaysua = Get_CauHinh.Ngay_DN;
                sql_hoadon = "select * from Nhaple_HOADON where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null && Look_tenlai.EditValue != "")
                {
                    string nhomchon = "'" + Look_tenlai.Properties.GetCheckedItems().ToString().Replace(", ", "','") + "'";

                    filter_string += " And ID_NCC IN (" + nhomchon + ")";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            filter_string += " order by Tbl_NCC_Name asc,Tbl_Hang_Name asc,Tbl_KhachHang_Name asc,Gia_1 desc";
            DuLieu.BaoCaoLe = _NhapSi_Class.get_dataset(filter_string);
            //lay thong tin no cua 1 ngay
            DataSet dts_hoadon = _NhapSi_Class.get_dataset(sql_hoadon);
            //tinh tong no va tm 1 ngay
            for (int i = 0; i < dts_hoadon.Tables[0].Rows.Count; i++)
            {
                dts_hoadon.Tables[0].Rows[i]["SUM_TM"] = float.Parse(dts_hoadon.Tables[0].Rows[i]["Sum_TienHang"].ToString()) - float.Parse(dts_hoadon.Tables[0].Rows[i]["SUM_TNO"].ToString());
            }
            object TIENMAT = dts_hoadon.Tables[0].Compute("Sum(SUM_TM)", "");
            object TIENNO = dts_hoadon.Tables[0].Compute("Sum(SUM_TNO)", "");
            try
            {
                DuLieu.tongtienmat_nhaple_baoca = decimal.Parse(TIENMAT.ToString().Replace(",", ""));
            }
            catch
            {
                DuLieu.tongtienmat_nhaple_baoca = 0;
            }
            try
            {
                DuLieu.tongtienno_nhaple_baoca = decimal.Parse(TIENNO.ToString().Replace(",", ""));
            }
            catch
            {
                DuLieu.tongtienno_nhaple_baoca = 0;
            }
           
            //tinh tien mat cua hoa don & tien no
            DuLieu.BaoCaoLe.Tables[0].Columns.Add("Sum_TongTienMat", typeof(float));
            DuLieu.BaoCaoLe.Tables[0].Columns.Add("Sum_TongTienNo", typeof(float));
            //gan tien mat va no vao cac dong cua chi tiet
            for (int i = 0; i < DuLieu.BaoCaoLe.Tables[0].Rows.Count; i++)
            {
                DataRow[] dr = dts_hoadon.Tables[0].Select("ID_NCC = '" + DuLieu.BaoCaoLe.Tables[0].Rows[i]["ID_NCC"].ToString() + "'");
                DuLieu.BaoCaoLe.Tables[0].Rows[i]["Sum_TongTienMat"] = float.Parse(dr[0]["SUM_TM"].ToString());
                DuLieu.BaoCaoLe.Tables[0].Rows[i]["Sum_TongTienNo"] = float.Parse(dr[0]["SUM_TNO"].ToString());
                DuLieu.BaoCaoLe.Tables[0].AcceptChanges();

            }



            DuLieu.BaoCaoLe.Tables[0].Columns.Add("Select", typeof(bool));
            DuLieu.BaoCaoLe.Tables[0].Columns.Add("CA_View");
            // lever1("Tbl_Hang_Name", "CA_View");

            // Duyệt qua từng hàng và gán giá trị từ cột "Tbl_Hang_Name" vào cột "CA_View"
            foreach (DataRow row in DuLieu.BaoCaoLe.Tables[0].Rows)
            {
                row["CA_View"] = row["Tbl_Hang_Name"];
            }

            data();
            tai_hoadon();
        }
        public void tai_hoadon()
        {
            //load hoa don
            #region
            string filter_string = "Hoadon_le_Full " + ngaysua + ",";
            filter_string += "Null,";
            filter_string += "Null";


            #endregion

            DuLieu.ToaLe_Full = db.T_opendataset(filter_string);
        }
        public void lever1(string filename, string colum)
        {
            if (DuLieu.BaoCaoLe.Tables[0].Rows.Count > 0)
            {
                string lever1 = DuLieu.BaoCaoLe.Tables[0].Rows[0][filename].ToString();
                DuLieu.BaoCaoLe.Tables[0].Rows[0][colum] = lever1;
                DuLieu.BaoCaoLe.Tables[0].AcceptChanges();
                for (int i = 1; i < DuLieu.BaoCaoLe.Tables[0].Rows.Count; i++)
                {
                    if (lever1 == DuLieu.BaoCaoLe.Tables[0].Rows[i][filename].ToString())
                    {
                        //xoa trang    
                        DuLieu.BaoCaoLe.Tables[0].Rows[i][colum] = "        ''        ";
                        DuLieu.BaoCaoLe.Tables[0].AcceptChanges();

                    }
                    else
                    {
                        lever1 = DuLieu.BaoCaoLe.Tables[0].Rows[i][filename].ToString();
                        DuLieu.BaoCaoLe.Tables[0].Rows[i][colum] = lever1;
                        DuLieu.BaoCaoLe.Tables[0].AcceptChanges();
                    }
                }
            }
        }
        public void print()
        {

            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region

                //------------------



                Get_CauHinh.sobaocao_nhaple_text = "SỔ BÁO CÁO BÁN LẼ NGÀY " + txtngay.Value.ToShortDateString();
                T_Baocaotonghop_1 rapport = new T_Baocaotonghop_1();
                rapport.DataSource = DuLieu.BaoCaoLe.Tables[0];
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowRibbonPreview();

                #endregion
            }


        }
        public void print_bill()// in tong hop
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                Get_CauHinh.baocaotonghopnhaple_text = "BÁO CÁO TỔNG HỢP NHẬP LẺ NGÀY " + txtngay.Value.ToShortDateString();
                DataSet dts = _NhapSi_Class.get_dataset("select * from Nhaple_TongHop where  Date_Num_Final_UpDate = " + ng + "");
                T_baocaotonghople rapport = new T_baocaotonghople();
                rapport.DataSource = DuLieu.BaoCaoLe.Tables[0];
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowRibbonPreview();
                #endregion

                #region
                //Get_CauHinh.baocaotonghopnhaple_text = "BÁO CÁO TỔNG HỢP NHẬP LẺ NGÀY " + txtngay.Value.ToShortDateString();
                ////T_baocao_bien rapport = new T_baocao_bien();
                //Get_CauHinh.sobaocao_nhaple_text = "SỔ BÁO CÁO BÁN LẼ NGÀY " + txtngay.Value.ToShortDateString();
                //T_baocao_le rapport = new T_baocao_le();
                //rapport.DataSource = DuLieu.BaoCaoLe.Tables[0];
                //rapport.ShowPreview();
                #endregion
            }
        }
        public void print_chitiet()// in tong hop
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Le", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                //  #region
                ////  int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                // Get_CauHinh.baocaotonghopnhaple_text = "BÁO CÁO TỔNG HỢP NHẬP LẺ NGÀY " + txtngay.Value.ToShortDateString();
                ////  DataSet dts= _NhapSi_Class.get_dataset("select * from Nhaple_TongHop where  Date_Num_Final_UpDate = " + ng + "");
                //  T_baocaotonghople rapport = new T_baocaotonghople();
                //  rapport.DataSource = DuLieu.BaoCaoLe.Tables[0];
                //  rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                //  rapport.ShowPreview();
                //  #endregion

                #region
                //Get_CauHinh.baocaotonghopnhaple_text = "BÁO CÁO TỔNG HỢP NHẬP LẺ NGÀY " + txtngay.Value.ToShortDateString();
                //T_baocao_bien rapport = new T_baocao_bien();
                Get_CauHinh.sobaocao_nhaple_text = "SỔ BÁO CÁO BÁN LẼ NGÀY " + txtngay.Value.ToShortDateString();
                T_baocao_le rapport = new T_baocao_le();
                rapport.DataSource = DuLieu.BaoCaoLe.Tables[0];
                rapport.ShowRibbonPreview();
                #endregion
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {

            btn_del.Click += new EventHandler(btn_del_Click);          
            //btn_all.Click += new EventHandler(btn_all_Click);
            //btn_none.Click += new EventHandler(btn_none_Click);
            //btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
            btn_in.Click += new EventHandler(btn_in_Click);
            btn_sua.Click += new EventHandler(btn_sua_Click);
            btn_inso1.Click += new EventHandler(btn_inso1_Click);
        }

        void btn_inso1_Click(object sender, EventArgs e)
        {
            print_chitiet();
        }

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



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

        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)
        {
            bochon();
        }

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

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




        #endregion
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label ThongkeGiaBQ = new Label();
        #region data
        public void data()
        {
            load_combo();
            gc_daluu.DataSource = DuLieu.BaoCaoLe.Tables[0];
            gv_daluu.ExpandAllGroups();
            gv_daluu.ExpandAllGroups();
            look_thuonglai_daluu.DataSource = DuLieu.List_NCC.Tables[0];
            look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
            look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
            Thongkesoluong.Text = clum_sl.SummaryText;
            thongketien.Text = clum_thanhtien.SummaryText;
            ThongkeGiaBQ.Text = clum_Gia.SummaryText;


        }
        #endregion






    }
}

