using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ABC.KetNoi;
using System.IO;
using System.Windows.Forms;

using System.Xml;
using ABC.File;
using System.Xml.Linq;

namespace ABC.BanLe
{
    class NhapLe_Class
    {
        public static string _xmlfile, __xmlfile_chitiet;
        public static string _nhom;
        Class_KetNoi db = new Class_KetNoi();
        public  void get_store(string sql)
        {
            #region           
            DuLieu.List_NhapSi_DaLuu= db.T_open_store(sql);
            DuLieu.List_NhapSi_DaLuu.Tables[0].Columns.Add("Select", typeof(bool));
             
            #endregion
        }
        public void get_NCC_store(string sql)
        {
            #region
            DuLieu.NhapLe_List_NCC = db.T_open_store(sql);
            #endregion
        }

        #region NCC
        public bool them_ncc(string id)
        {

            DataRow[] dr = DuLieu.NhapLe_List_NCC.Tables[0].Select("NCC_ID = '" + id.ToUpper() + "'");
            if (dr.Length == 0)
            {
                string sql_kt_sd = "select * from BanLe where NCC_ID = '" + id.ToUpper() + "' and SD = 1";
                DataTable dt = db.T_opendataset(sql_kt_sd).Tables[0];
                if (dt.Rows.Count == 0)
                {
                    string sql = "insert into BanLe (NCC_ID) values ('" + id + "')";
                    set(sql);
                }
                else
                {
                    string sql = "update BanLe set SD = null where NCC_ID = '" + id.ToUpper() + "'";
                    set(sql);
                }
               
              
                return true;
            }
            else
            {
                msgbox.dialog_OK_info("Thương lái " + dr[0]["Name"].ToString() + " đã có trong danh sách bán lẽ");
                return false;
            }
        }
        #endregion

        public void load_conno()
        {
            string filter_string = "XemNo_1Ngay " + Get_CauHinh.Ngay_DN + ",Null,Null,Null";
            DuLieu.CongNo_1Ngay = db.T_opendataset(filter_string);
            DuLieu.CongNo_1Ngay.Tables.Remove("Table"); 
        }
        public bool them_ca(string idca,string id_ncc,string ncc_name)
        {
            DataRow[] dr = DuLieu.NhapLe_List_Ca.Tables[0].Select("NCC_ID = '" + id_ncc.ToUpper() + "' and Hang_ID = '"+idca.ToUpper()+"'");
            if (dr.Length == 0)
            {
                string sql = "NhapLe_insert_Ca '"+id_ncc+"','"+idca+"'";
                set(sql);
                //set so thu tu
                return true;
            }
            else
            {
                msgbox.dialog_OK_info("Loại cá " + dr[0]["Name"].ToString() + " đã có trong danh sách bán lẽ của lái "+ncc_name+"");
                return false;
            }
        }
        public void set_stt(string id_ncc, string id_ca, string stt)
        {
            string sql = "Update BanLe set Note = " + stt + " where NCC_ID = '" + id_ncc.ToUpper() + "' and Hang_ID ='" + id_ca.ToUpper() + "'";
            db.T_exc(sql);
            
        }
        public void get_ca_store(string sql)
        {
            #region
            DuLieu.NhapLe_List_Ca = db.T_open_store(sql);
            DuLieu.NhapLe_List_Ca.Tables[0].Columns.Add("SL", typeof(decimal));
            DuLieu.NhapLe_List_Ca.Tables[0].Columns.Add("Gia", typeof(decimal));

            #endregion
        }
        public DataSet get_dataset(string sql)
        {
            #region
            //DuLieu.List_NhapSi_DaLuu = db.T_open_store(sql);
            //DuLieu.List_NhapSi_DaLuu.Tables[0].Columns.Add("Select", typeof(bool));
            return db.T_opendataset(sql); 
            
            #endregion
        }
        public  void set(string sql)
        {
            db.T_exc(sql);
        }
        Xml_control _Xml_control = new Xml_control();

        #region XML
        public void load_xml_hoadon_chitiet()
        {
            string fileTemp = "";
            int thamso = 1;
            string[] arr_name = new string[thamso];
            object[] arr_value = new object[thamso];
            arr_name[0] = "@ID";
            arr_value[0] = Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper();
            DataTable dt = db.T_opendataset_store("sp_CauHinh_TheoID", arr_name, arr_value, thamso).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                fileTemp = row["ThuMucFileTemp"].ToString();
            }
            string _xml_temp = Path.GetDirectoryName(Application.ExecutablePath) + @"\BanLe\TempDataLe\ChiTietHD\TempData_NhapLe.xml";
            __xmlfile_chitiet = fileTemp + @"\BanLe\ChiTietHD\" + Get_CauHinh.Ngay_DN + ".xml";
            if (!System.IO.File.Exists(__xmlfile_chitiet))
            {
                System.IO.File.Copy(_xml_temp, __xmlfile_chitiet);
                DuLieu.List_NhapLe_ChiTiet = _Xml_control.read_xml(__xmlfile_chitiet);
                if (DuLieu.List_NhapLe_ChiTiet.Tables[0].Rows.Count > 0 && DuLieu.List_NhapLe_ChiTiet.Tables[0].Rows[0]["ID"].ToString() == "Null")
                {
                    DataRow dr = DuLieu.List_NhapLe_ChiTiet.Tables[0].Rows[0];
                    _Xml_control.delete_xml_node(__xmlfile_chitiet, dr["ID"].ToString());
                    DuLieu.List_NhapLe_ChiTiet.Tables[0].Rows.Remove(dr);
                    DuLieu.List_NhapLe_ChiTiet.Tables[0].AcceptChanges();
                }
                DuLieu.List_NhapLe_ChiTiet.Tables[0].Columns.Add("Select", typeof(bool));
                
            }
            else
            {
                DuLieu.List_NhapLe_ChiTiet = _Xml_control.read_xml(__xmlfile_chitiet);
                DuLieu.List_NhapLe_ChiTiet.Tables[0].Columns.Add("Select", typeof(bool));
            }

        }
        public void load_xml_hoadon()
        {
            try
            {
                string fileTemp = "";
                int thamso = 1;
                string[] arr_name = new string[thamso];
                object[] arr_value = new object[thamso];
                arr_name[0] = "@ID";
                arr_value[0] = Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper();
                DataTable dt = db.T_opendataset_store("sp_CauHinh_TheoID", arr_name, arr_value, thamso).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    fileTemp = row["ThuMucFileTemp"].ToString();
                }
                string _xml_temp = Path.GetDirectoryName(Application.ExecutablePath) + @"\BanLe\TempDataLe\HD\Temp_DataHoaDon_NhapLe.xml";
                _xmlfile = fileTemp + @"\BanLe\HD\" + Get_CauHinh.Ngay_DN + ".xml";
                if (!System.IO.File.Exists(_xmlfile))
                {
                    System.IO.File.Copy(_xml_temp, _xmlfile);
                    DuLieu.List_NhapLe_HoaDon = _Xml_control.read_xml(_xmlfile);
                    if (DuLieu.List_NhapLe_HoaDon.Tables[0].Rows.Count > 0 && DuLieu.List_NhapLe_HoaDon.Tables[0].Rows[0]["ID"].ToString() == "Null")
                    {
                        DataRow dr = DuLieu.List_NhapLe_HoaDon.Tables[0].Rows[0];
                        _Xml_control.delete_xml_node(_xmlfile, dr["ID"].ToString());
                        DuLieu.List_NhapLe_HoaDon.Tables[0].Rows.Remove(dr);
                        DuLieu.List_NhapLe_HoaDon.Tables[0].AcceptChanges();
                    }
                    DuLieu.List_NhapLe_HoaDon.Tables[0].Columns.Add("Select", typeof(bool));

                }
                else
                {
                    DuLieu.List_NhapLe_HoaDon = _Xml_control.read_xml(_xmlfile);
                    DuLieu.List_NhapLe_HoaDon.Tables[0].Columns.Add("Select", typeof(bool));
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void load_xml_kytu()
        {
            string _xml_temp_kytu = Path.GetDirectoryName(Application.ExecutablePath) + @"\BanLe\TempDataLe\KyTu.xml";            
            DataSet ds = new DataSet();
            ds = _Xml_control.read_xml(_xml_temp_kytu);
            DuLieu.KyTu = ds.Tables[0].Rows[0]["ID"].ToString();
        }
        #endregion
        public void tao_toa()
        {
            //DuLieu.Le_ID = DuLieu.List_NhapLe_HoaDon.Tables[0].NewRow();
            //
            DuLieu.Le_ID = Guid.NewGuid().ToString().ToUpper();
            DuLieu.Le_KyTu = DuLieu.KyTu;
            DuLieu.Le_SoHD = (DuLieu.List_NhapLe_HoaDon.Tables[0].Rows.Count + 1);

            DuLieu.Le_Tbl_KhachHang_ID = "";
            DuLieu.Le_Tbl_KhachHang_Name = "Vãng Lai";
            DuLieu.Le_TienMat = 0;

            DuLieu.Le_ID_NCC = "";
            DuLieu.Le_Tbl_NCC_Name = "";
            DuLieu.Le_Toa = DuLieu.KyTu + (DuLieu.List_NhapLe_HoaDon.Tables[0].Rows.Count + 1).ToString();
            DuLieu.Le_TongTien = 0;
            
        }

        public void add_toa_saukhixoa()
        {

            //
            XmlDocument doc = new XmlDocument();
            doc.Load(NhapLe_Class._xmlfile);
            XmlNode root = doc.DocumentElement;
            //Create a new node.
            XmlElement elem = doc.CreateElement("Table");
            //add element
            XmlElement ID = doc.CreateElement("ID");
            XmlElement KyTu = doc.CreateElement("KyTu");
            XmlElement SoHD = doc.CreateElement("SoHD");

            XmlElement Tbl_KhachHang_ID = doc.CreateElement("Tbl_KhachHang_ID");
            XmlElement Tbl_KhachHang_Name = doc.CreateElement("Tbl_KhachHang_Name");
            XmlElement TienMat = doc.CreateElement("TienMat");

            XmlElement ID_NCC = doc.CreateElement("ID_NCC");
            XmlElement Tbl_NCC_Name = doc.CreateElement("Tbl_NCC_Name");
            XmlElement Toa = doc.CreateElement("Toa");
            XmlElement TongTien = doc.CreateElement("TongTien");

            // add inner text
            ID.InnerText = DuLieu.Le_ID;
            KyTu.InnerText = DuLieu.Le_KyTu;
            SoHD.InnerText = DuLieu.Le_SoHD.ToString();

            Tbl_KhachHang_ID.InnerText = DuLieu.Le_Tbl_KhachHang_ID;
            Tbl_KhachHang_Name.InnerText = DuLieu.Le_Tbl_KhachHang_Name;
            //  TienMat.InnerText = DuLieu.Le_TienMat.ToString();
            TienMat.InnerText = (Tbl_KhachHang_ID.InnerText.Trim() == "") ? DuLieu.Le_TongTien.ToString() : "0";
            ID_NCC.InnerText = DuLieu.Le_ID_NCC;
            Tbl_NCC_Name.InnerText = DuLieu.Le_Tbl_NCC_Name;
            Toa.InnerText = DuLieu.Le_Toa;
            TongTien.InnerText = DuLieu.Le_TongTien.ToString();

            // add to element
            elem.AppendChild(ID);
            elem.AppendChild(KyTu);
            elem.AppendChild(SoHD);

            elem.AppendChild(Tbl_KhachHang_ID);
            elem.AppendChild(Tbl_KhachHang_Name);
            elem.AppendChild(TienMat);

            elem.AppendChild(ID_NCC);
            elem.AppendChild(Tbl_NCC_Name);
            elem.AppendChild(Toa);
            elem.AppendChild(TongTien);
           
            root.AppendChild(elem);
            doc.Save(NhapLe_Class._xmlfile);
        }
        public void xoatoa(DataRow dr)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(NhapLe_Class._xmlfile);
            XmlNode root = doc.DocumentElement;
            //Create a new node.
            XmlElement elem = doc.CreateElement("Table");
            //add element
            XmlElement ID = doc.CreateElement("ID");
            XmlElement KyTu = doc.CreateElement("KyTu");
            XmlElement SoHD = doc.CreateElement("SoHD");

            XmlElement Tbl_KhachHang_ID = doc.CreateElement("Tbl_KhachHang_ID");
            XmlElement Tbl_KhachHang_Name = doc.CreateElement("Tbl_KhachHang_Name");
            XmlElement TienMat = doc.CreateElement("TienMat");

            XmlElement ID_NCC = doc.CreateElement("ID_NCC");
            XmlElement Tbl_NCC_Name = doc.CreateElement("Tbl_NCC_Name");
            XmlElement Toa = doc.CreateElement("Toa");
            XmlElement TongTien = doc.CreateElement("TongTien");

            // add inner text
            ID.InnerText = dr["ID"].ToString();
            KyTu.InnerText = dr["KyTu"].ToString();
            SoHD.InnerText = dr["SoHD"].ToString();

            Tbl_KhachHang_ID.InnerText = dr["Tbl_KhachHang_ID"].ToString();
            Tbl_KhachHang_Name.InnerText = dr["Tbl_KhachHang_Name"].ToString();
            TienMat.InnerText = dr["TienMat"].ToString();

            ID_NCC.InnerText = dr["ID_NCC"].ToString();
            Tbl_NCC_Name.InnerText = dr["Tbl_NCC_Name"].ToString();
            Toa.InnerText = "XXX";
            TongTien.InnerText = dr["TongTien"].ToString();

            // add to element
            elem.AppendChild(ID);
            elem.AppendChild(KyTu);
            elem.AppendChild(SoHD);

            elem.AppendChild(Tbl_KhachHang_ID);
            elem.AppendChild(Tbl_KhachHang_Name);
            elem.AppendChild(TienMat);

            elem.AppendChild(ID_NCC);
            elem.AppendChild(Tbl_NCC_Name);
            elem.AppendChild(Toa);
            elem.AppendChild(TongTien);

            root.AppendChild(elem);
            doc.Save(NhapLe_Class._xmlfile);
        }
        #region
        public void save_all()
        {

          //  load_xml_hoadon_chitiet();
            //1 loc bo danh sach xoa
            DataView dtv_ds_toa = new DataView(DuLieu.List_NhapLe_HoaDon.Tables[0]); 
            dtv_ds_toa.RowFilter = "Toa <>'XXX'";
            //2 look qua ds toa
            for (int i = 0; i < dtv_ds_toa.Count; i++)
            {
                //3 lay chi tiet 1 toa
                #region
                DataView dtv_chitiet = new DataView(DuLieu.List_NhapLe_ChiTiet.Tables[0]);
                dtv_chitiet.RowFilter = "ID_HD='"+dtv_ds_toa[i]["ID"].ToString().ToUpper()+"'";
                //3.1 Look qua ds chi tiet
                string noiDungTele = "";
                for (int j = 0; j < dtv_chitiet.Count; j++)
                {
                    //4 luu xuong csdl
                    string nd = "Tbl_GIAODICH_LE ";
                    #region
                    string sql = "Tbl_GIAODICH_LE_insert ";
                       // @STT INT,
                    sql+="N'"+dtv_chitiet[j]["STT_Them"].ToString()+"',";
                    nd += "STT_Them=" + dtv_chitiet[j]["STT_Them"].ToString();
                        //@ID uniqueidentifier,
                    sql += "N'" + dtv_chitiet[j]["ID"].ToString().ToUpper() + "',";
                        //@ID_Hang uniqueidentifier,
                    sql += "N'" + dtv_chitiet[j]["ID_Hang"].ToString().ToUpper() + "',";
                        //@ID_KhachHang uniqueidentifier,
                    if (dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().Length > 10)
                    {
                        sql += "N'" + dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().ToUpper() + "',";
                        nd += " Tbl_KhachHang_ID=" + dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString();
                    }
                    else
                    {
                        sql += "Null,";
                    }
                        //@ID_NCC uniqueidentifier,
                    sql += "N'" + dtv_chitiet[j]["ID_NCC"].ToString() + "',";
                    nd += " ID_NCC=" + dtv_chitiet[j]["ID_NCC"].ToString();

                    //@Date_Num_Final_UpDate int,
                    sql += "N'" + Get_CauHinh.Ngay_DN.ToString() + "',";
                        //@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'" + dtv_chitiet[j]["SL"].ToString() + "',";
                    nd += " SL=" + dtv_chitiet[j]["SL"].ToString();
                    //@Gia_0 decimal,
                    sql += "Null,";
                        //@Gia_1 decimal,
                    sql += "N'" + dtv_chitiet[j]["Gia"].ToString() + "',";
                    nd += " Gia=" + dtv_chitiet[j]["Gia"].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'" + dtv_ds_toa[i]["TongTien"].ToString() + "',";
                    nd += " TongTien=" + dtv_ds_toa[i]["TongTien"].ToString();
                    //@_Num2 decimal,
                    //if (dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().Length > 10)
                    //{
                    //    sql += "N'" + dtv_ds_toa[i]["TienMat"].ToString() + "',";
                    //}
                    //else
                    //{
                    //    sql += "N'" + dtv_ds_toa[i]["TongTien"].ToString() + "',";
                    //}
                    if (dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().Length > 10)
                    {
                        sql += "0,";
                    }
                    else
                    {
                        sql += "N'" + dtv_ds_toa[i]["TienMat"].ToString() + "',";
                    }

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

                        //@_SoHD_LE int,
                    sql += "N'" + dtv_ds_toa[i]["SoHD"].ToString() + "',";
                    nd += " SoHD=" + dtv_ds_toa[i]["SoHD"].ToString();
                    //@_ID_HD_LE uniqueidentifier,
                    sql += "N'" + dtv_ds_toa[i]["ID"].ToString() + "',";
                        //@_Node_LE nvarchar(100),
                    sql += "N'" + dtv_ds_toa[i]["KyTu"].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);
                    DuLieu.LuuLuocSu(Main_Form._tk, "Le_Forms", "", nd, "Thêm");
                    #endregion
                }
                #endregion
                #region goi Telegram

                #endregion
                //5.0 save cong no neu co
                if (dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().Length > 10)
                {
                    #region
                    double CongNo_Cu = 0;
                  
                    decimal Tienmat = decimal.Parse(dtv_ds_toa[i]["TienMat"].ToString());
                    decimal tongtien = decimal.Parse(dtv_ds_toa[i]["TongTien"].ToString());
                    if (Tienmat < tongtien) // luu y khong cho tra tien mat vuoc qua tong tien trong toa
                    {
                        //EXEC Tbl_CONGNO_insert 
                        string sql = "Tbl_CONGNO_insert ";
                        //      @ID_KhachHang
                        sql += "N'" + dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().ToUpper() + "',";
                        //       ,@ID la id nguon co the la  toa
                        sql += "N'" + dtv_ds_toa[i]["ID"].ToString().ToUpper() + "',";
                        //       ,'SI'
                        sql += "N'LE',";
                        //       ,@Date_Num_Final_UpDate
                        sql += "N'" + Get_CauHinh.Ngay_DN + "',";
                        //       ,@THANHTIEN
                        decimal tienno = tongtien - Tienmat;
                        sql += "N'" + tienno + "'";
                        db.T_exc(sql);
                        DuLieu.LuuLuocSu(Main_Form._tk, "Le_Forms", "", tienno.ToString(), "Thêm công nợ ID Khach="+ dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString().ToUpper());
                    }
                    #endregion
                }
                //5 danh dau da xoa
                #region
                DataRow[] dt_xoa = DuLieu.List_NhapLe_HoaDon.Tables[0].Select("ID = '" + dtv_ds_toa[i]["ID"].ToString().ToUpper() + "'");
                if (dt_xoa.Length > 0)
                {
                    //chi cap nhat k remove
                    dt_xoa[0]["Toa"] = "Saved";
                    DuLieu.List_NhapLe_HoaDon.Tables[0].AcceptChanges();
                    _Xml_control.delete_xml_node(NhapLe_Class._xmlfile, dt_xoa[0]["ID"].ToString());
                    xoatoa(dt_xoa[0]);
                  //  msgbox.dialog_OK_info(dt_xoa[0]["Toa"].ToString() + "   Toa  thu" + dt_xoa[0]["SoHD"].ToString());
                }
                #endregion
         
            }

        }
     
        #endregion

      
      
        public void load_dulieudaluu(string nhom)
        {
            //KHOI TAO DL CHUA LUU
            //if (nhom != "All")
            //{
            //    string sql = "select * from NhapLe_SO where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + " and NHOM_NCC_ID ='" + nhom + "'";
            //    DuLieu.Le_daluu = db.T_open(sql).Tables[0];
            //    DuLieu.Le_tatca = DuLieu.Le_daluu.Copy();
            //    string sql2 = "select * from NhapLe_DaLuu where 0=1";
            //    DuLieu.Le_chuaLuu = db.T_open(sql2).Tables[0];
            //    DuLieu.Le_chuaLuu_sx = DuLieu.Le_chuaLuu.Copy();
            //}
            //else
            //{
            //    string sql = "select * from NhapLe_SO where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
            //    DuLieu.Le_daluu = db.T_open(sql).Tables[0];
            //    DuLieu.Le_tatca = DuLieu.Le_daluu.Copy();
            //    string sql2 = "select * from NhapLe_SO where 0=1";
            //    DuLieu.Le_chuaLuu = db.T_open(sql2).Tables[0];
            //   // DuLieu.Le_chuaLuu_sx = DuLieu.Le_chuaLuu.Copy();
            //}
            //tao bang
            DuLieu.Le_chuaLuu = new DataTable();
            DuLieu.Le_chuaLuu.Columns.Add("KH_View");
            DuLieu.Le_chuaLuu.Columns.Add("ID", typeof(Guid));
            DuLieu.Le_chuaLuu.Columns.Add("ID_NCC",typeof(Guid));
            DuLieu.Le_chuaLuu.Columns.Add("ID_NCC_Truoc", typeof(Guid));
            DuLieu.Le_chuaLuu.Columns.Add("Tbl_NCC_Name");
            DuLieu.Le_chuaLuu.Columns.Add("ID_KhachHang", typeof(Guid));          
          
            DuLieu.Le_chuaLuu.Columns.Add("Tbl_KhachHang_Name");
            DuLieu.Le_chuaLuu.Columns.Add("ID_Hang", typeof(Guid));
            DuLieu.Le_chuaLuu.Columns.Add("ID_Hang_Truoc", typeof(Guid));          
            DuLieu.Le_chuaLuu.Columns.Add("Tbl_Hang_Name");
            DuLieu.Le_chuaLuu.Columns.Add("Gia", typeof(decimal));
            DuLieu.Le_chuaLuu.Columns.Add("GiaTruoc", typeof(decimal));          
            DuLieu.Le_chuaLuu.Columns.Add("SL",typeof(float));
            DuLieu.Le_chuaLuu.Columns.Add("ThanhTien", typeof(decimal));          
            
            DuLieu.Le_chuaLuu.Columns.Add("TrangThai");
            //DataView dtv_ds_toa = new DataView(DuLieu.List_NhapLe_HoaDon.Tables[0]);
            DataView dtv_ds_toa = DuLieu.List_NhapLe_HoaDon.Tables[0].Copy().DefaultView;
            dtv_ds_toa.RowFilter = "Toa <>'XXX'";
            for (int i = 0; i < dtv_ds_toa.Count; i++)
            {
                //3 lay chi tiet 1 toa
                #region
                DataView dtv_chitiet = new DataView(DuLieu.List_NhapLe_ChiTiet.Tables[0]);
                dtv_chitiet.RowFilter = "ID_HD='" + dtv_ds_toa[i]["ID"].ToString().ToUpper() + "'";
                //3.1 Look qua ds chi tiet
                for (int j = 0; j < dtv_chitiet.Count; j++)
                {
                    #region
                    //them_1dong_chitiet_sotatca(dtv_chitiet[j]["ID_NCC"].ToString(),
                    //    dtv_chitiet[j]["Tbl_NCC_Name"].ToString(),
                    //    dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString(),
                    //    dtv_ds_toa[i]["Tbl_KhachHang_Name"].ToString(),
                    //    dtv_chitiet[j]["ID_Hang"].ToString(),
                    //    dtv_chitiet[j]["Tbl_Hang_Name"].ToString(), double.Parse(dtv_chitiet[j]["Gia"].ToString()), float.Parse(dtv_chitiet[j]["SL"].ToString()));

                    them_1dong_chitiet_sochualuu(dtv_chitiet[j]["ID_NCC"].ToString(),
                        dtv_chitiet[j]["Tbl_NCC_Name"].ToString(),
                        dtv_ds_toa[i]["Tbl_KhachHang_ID"].ToString(),
                        dtv_ds_toa[i]["Tbl_KhachHang_Name"].ToString(),
                        dtv_chitiet[j]["ID_Hang"].ToString(),
                        dtv_chitiet[j]["Tbl_Hang_Name"].ToString(), double.Parse(dtv_chitiet[j]["Gia"].ToString()), float.Parse(dtv_chitiet[j]["SL"].ToString()), dtv_chitiet[j]["ID"].ToString());
                    #endregion
                }
                #endregion
            }






        }
        
        public void them_1dong_chitiet_sochualuu(string id_tl, string tentl, string id_kh, string tenkh, string id_ca, string tenca, double gia, float sl,string ID)
        {
            


            DataRow[] dr ;
            if (id_kh == "")
            {
                dr = DuLieu.Le_chuaLuu.Select("ID_NCC ='" + id_tl.ToUpper() + "' and ID_KhachHang is null and ID_Hang = '" + id_ca.ToUpper() + "' and Gia = " + gia + " and ID='"+ID+"'");
            }
            else
            {
                dr = DuLieu.Le_chuaLuu.Select("ID_NCC ='" + id_tl.ToUpper() + "' and ID_KhachHang = '" + id_kh.ToUpper() + "' and ID_Hang = '" + id_ca.ToUpper() + "' and Gia = " + gia + " and ID='"+ID+"'");
            }
            if (dr.Length == 0)
            {
                DataRow dr_tatca_new = DuLieu.Le_chuaLuu.NewRow();
                dr_tatca_new["ID"] = ID.ToUpper();
                dr_tatca_new["ID_NCC"] = id_tl.ToUpper(); 
                dr_tatca_new["ID_NCC_Truoc"] = id_tl.ToUpper();
                dr_tatca_new["Tbl_NCC_Name"] = tentl;
                try
                {
                    dr_tatca_new["ID_KhachHang"] = id_kh.ToUpper();
                    dr_tatca_new["Tbl_KhachHang_Name"] = tenkh;
                }
                catch
                {
                    dr_tatca_new["Tbl_KhachHang_Name"] = "Vãng Lai";
                }
                
                dr_tatca_new["KH_View"] = "";
                dr_tatca_new["ID_Hang"] = id_ca.ToUpper();
                dr_tatca_new["ID_Hang_Truoc"] = id_ca;
                dr_tatca_new["Tbl_Hang_Name"] = tenca;
                dr_tatca_new["Gia"] = gia;
                dr_tatca_new["GiaTruoc"] = gia;
                dr_tatca_new["SL"] = sl;
                dr_tatca_new["ThanhTien"] = (sl * gia);
                
                dr_tatca_new["TrangThai"] = "";
                DuLieu.Le_chuaLuu.Rows.Add(dr_tatca_new);
                DuLieu.Le_chuaLuu.AcceptChanges();
            }
            else // se co 1 va chi mot dong
            {
                float sldau = float.Parse(dr[0]["SL"].ToString());
                dr[0]["SL"] = sldau + sl;
                dr[0]["ThanhTien"] = float.Parse(dr[0]["SL"].ToString()) * gia;
                
                DuLieu.Le_chuaLuu.AcceptChanges();
            }
        }

        public void sort_data()
        { 
                //DuLieu.Le_chuaLuu.DefaultView.Sort = "Tbl_NCC_Name,Tbl_Hang_Name,Tbl_Hang_Name ASC";
            DuLieu.Le_chuaLuu.DefaultView.RowFilter = "Tbl_NCC_Name,Tbl_KhachHang_Name,Tbl_Hang_Name ASC";
                DuLieu.Le_chuaLuu = DuLieu.Le_chuaLuu.DefaultView.ToTable();
        }
       

    }

}
