#region
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.KetNoi;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraReports.UI;
using ABC.Report;
using System.Xml;
using System.IO;
using ABC.File;
#endregion
namespace ABC.Toa
{
    public partial class Toa_New : Form
    {        
        public Toa_New()
        {
            #region
            InitializeComponent();
            kytu();
            Toa_Class.Table_deleted = new DataTable();
            Toa_Class.Table_deleted.Columns.Add("ID");
            event_click();
            enter();
            // lever_chiphi();
            DataTable dt = new DataTable("table");
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            for (int i = 0; i < DuLieu.List_NCC.Tables[0].Rows.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["ID"] = DuLieu.List_NCC.Tables[0].Rows[i]["ID"].ToString();
                row["Name"] = DuLieu.List_NCC.Tables[0].Rows[i]["Name"].ToString();
                dt.Rows.Add(row);
                dt.AcceptChanges();
            }
            Look_tenlai.Properties.DataSource = dt;
            look_nhomhang0.DataSource = DuLieu.List_Nhom_Hang.Tables[0];
            lookup_nhomhang1.DataSource = DuLieu.List_Nhom_Hang.Tables[0];
           // lookup_nhomhang2.DataSource = DuLieu.List_Nhom_Hang.Tables[0];
            btn_save.Click += new EventHandler(btn_save_Click);
            btn_gopdl.Click += new EventHandler(btn_gopdl_Click);
           // this.FormClosing += new FormClosingEventHandler(Toa_New_FormClosing);
            this.Load += new EventHandler(Toa_New_Load);
            this.FormClosing += new FormClosingEventHandler(Toa_New_FormClosing);
            btn_close.Click += new EventHandler(btn_close_Click);
            btn_kiemtra.Click += new EventHandler(btn_kiemtra_Click);
            editgia.Click += new EventHandler(editgia_Click);
            #endregion
        }
        IQ _IQ;
        void editgia_Click(object sender, EventArgs e)
        {
            _IQ = new IQ();
            _Toa_Class.get_ip_may();
            _IQ.FormClosed += new FormClosedEventHandler(_IQ_FormClosed);
            DuLieu.Dr_gia = gv3.GetDataRow(gv3.FocusedRowHandle);
            lechbandau = double.Parse(DuLieu.Dr_gia["Lech"].ToString().Replace(",",""));
            _IQ.Text = DuLieu.Dr_gia["Toa_NhomHang_Ten"].ToString();
            _IQ.ShowDialog();
        }
        double lechbandau;
        void _IQ_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_IQ.DialogResult == DialogResult.OK)
            {
            
                 DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv.RowFilter = "Toa_NhomHang_Ten = '" + DuLieu.Dr_gia["Toa_NhomHang_Ten"].ToString() + "'";
                string id_nhom = dtv[0]["Toa_NhomHang_ID"].ToString().ToUpper(); 
                
                int dongbixoa = dtv.Count - Toa_Class.Ni;
                msgbox.dialog_OK_info(dtv.Count.ToString() + "-" + Toa_Class.Ni.ToString() + "=" + dongbixoa.ToString());
                for (int i = 0; i < dongbixoa; i++)
                {

                    string ID = dtv[i]["ID"].ToString().ToUpper();
                    string Toa_NhomHang_ID = dtv[i]["Toa_NhomHang_ID"].ToString().ToUpper();                    
                    double gia = double.Parse(dtv[i]["Toa_Gia"].ToString().Replace(",", ""));                    
                    xoa_1dong(ID);                    
                    capnhat_group_gia_SL(gia, Toa_NhomHang_ID);
                    capnhat_group_ca_SL(Toa_NhomHang_ID);

                  
                    
                }
                for (int i = 0; i < Toa_Class.Ni; i++)
                {
                    
                    string ID = dtv[i]["ID"].ToString().ToUpper();
                    string Toa_NhomHang_ID = dtv[i]["Toa_NhomHang_ID"].ToString().ToUpper();
                    float SL_cancapnhat = 0;
                    double gia = double.Parse(dtv[i]["Toa_Gia"].ToString().Replace(",", ""));
                        SL_cancapnhat = Toa_Class.Si[i];
                        capnhat_chitiet_SL(ID, Toa_NhomHang_ID, gia, SL_cancapnhat);
                    
                }
                ud_date();
                DataRow[] dtv_lech = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + id_nhom + "'");
                dtv_lech[0]["Lech"] = lechbandau + Toa_Class.lechtinh;
                Toa_Class.Table_Data.Tables[2].AcceptChanges();
                show_data();
              
            }
           
        }
        public void ud_date()
        {
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_NhomHang_Ten = '" + DuLieu.Dr_gia["Toa_NhomHang_Ten"].ToString() + "'";
            for (int i = 0; i < Toa_Class.Ni; i++)
            {
                string ID = dtv[i]["ID"].ToString().ToUpper();
                string Toa_NhomHang_ID = dtv[i]["Toa_NhomHang_ID"].ToString().ToUpper();
                float SL_cancapnhat = float.Parse(dtv[i]["Toa_SL"].ToString().Replace(",", ""));
                double gia = Toa_Class.Gi[i];
                double gia_an = double.Parse(dtv[i]["Toa_Gia_an"].ToString().Replace(",", ""));
                capnhat_chitiet_Gia(ID, Toa_NhomHang_ID, gia, gia_an, SL_cancapnhat);
            }
        }
        void btn_kiemtra_Click(object sender, EventArgs e)
        {
            kiemtra_dt_them();
        }
        kiemtra _kiemtra;
        public void kiemtra_dt_them()
        {
            _kiemtra = new kiemtra();
            _kiemtra.FormClosed += new FormClosedEventHandler(_kiemtra_FormClosed);
            _kiemtra.ShowDialog();
        }

        void _kiemtra_FormClosed(object sender, FormClosedEventArgs e)
        {
            show_data();
        }

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

        void Toa_New_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (daluu == false)
            {
                save_khidong();
            }
        }

      

      
        #region register event
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.F1))
            {
                them_dl();
            }
            if (keyData == (Keys.F2))
            {
                dieu_kien();
            }
            if (keyData == (Keys.F3))
            {
                kiemtra_dt_them();
            }
            if (keyData == (Keys.F4))
            {
                //savechiphi_123();

            }
            if (keyData == (Keys.F5))
            {
                save();
            }

            if (keyData == (Keys.Control | Keys.D))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


        #endregion

        #region event
        public void event_click()
        {
            xoa_chitiet.Click += new EventHandler(xoa_chitiet_Click);
            gv.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_CellValueChanged);
            gv2.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv2_CellValueChanged);
            gv3.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv3_CellValueChanged);
            btn_THEMDULIEU.Click += new EventHandler(btn_THEMDULIEU_Click);
        }

        void btn_THEMDULIEU_Click(object sender, EventArgs e)
        {
            them_dl();
        }
      
        void gv3_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            #region
            switch (e.Column.FieldName.ToString())
            {

                case "Gia":
                    {
                        DataRow dr = gv3.GetDataRow(gv3.FocusedRowHandle);
                        string Toa_NhomHang_ID = dr["Toa_NhomHang_ID"].ToString().ToUpper();
                        double gia = double.Parse(e.Value.ToString().Replace(",", ""));                        
                        update_gv2_gia(Toa_NhomHang_ID, gia);
                        show_data();
                        break;
                    }
                case "MHHPC":
                    {
                        DataRow dr = gv3.GetDataRow(gv3.FocusedRowHandle);
                        if (float.Parse(dr["MHHSL"].ToString()) == 0)
                        {
                           update_HHPC(dr["Toa_NhomHang_ID"].ToString().ToUpper(),float.Parse(e.Value.ToString()));
                           show_data();
                        }
                        else
                        {
                            msgbox.dialog_OK_Questios("Đã cập nhật hoa hồng theo số lượng");
                            gv3.CancelUpdateCurrentRow();
                        }
                        break;
                    }
                case "MHHSL":
                    {
                       DataRow dr = gv3.GetDataRow(gv3.FocusedRowHandle);
                        if (float.Parse(dr["MHHPC"].ToString()) == 0)
                        {
                           update_HHSL(dr["Toa_NhomHang_ID"].ToString().ToUpper(),double.Parse(e.Value.ToString()));
                           show_data();
                        }
                        else
                        {
                            msgbox.dialog_OK_Questios("Đã cập nhật hoa hồng theo phần trăm");
                            gv3.CancelUpdateCurrentRow();
                        }
                        break;
                    }

            }
            #endregion
        }

        void gv2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            #region
            switch (e.Column.FieldName.ToString())
            {
                case "Sum_SL":
                    {
                        DataRow dr = gv2.GetDataRow(gv2.FocusedRowHandle);                        
                        string Toa_NhomHang_ID = dr["Toa_NhomHang_ID"].ToString().ToUpper();
                        float SL_cancapnhat = float.Parse(e.Value.ToString().Replace(",", ""));
                        double gia = double.Parse(dr["Gia"].ToString().Replace(",", ""));
                        update_gv1_SL(Toa_NhomHang_ID,gia,SL_cancapnhat);
                        show_data();
                        break;
                    }
                case "Gia":
                    {
                        DataRow dr = gv2.GetDataRow(gv2.FocusedRowHandle);
                        
                        string Toa_NhomHang_ID = dr["Toa_NhomHang_ID"].ToString().ToUpper();
                        
                        double gia = double.Parse(e.Value.ToString().Replace(",", ""));
                        double gia_an = double.Parse(dr["Gia_an"].ToString().Replace(",", ""));
                        update_gv1_gia(Toa_NhomHang_ID, gia, gia_an);
                        capnhat_group_gia_Gia(Toa_NhomHang_ID, gia_an, gia);
                        capnhat_group_ca_Gia(Toa_NhomHang_ID);
                        show_data();
                        break;
                    }
                case "Toa_NhomHang_Ten":
                    {

                        DataRow dr = gv2.GetDataRow(gv2.FocusedRowHandle);
                        //string ID = dr["ID"].ToString().ToUpper();
                        DataRowView row_nhomhang = look_nhomhang0.GetDataSourceRowByKeyValue(dr["Toa_NhomHang_Ten"].ToString()) as DataRowView;
                        string Toa_NhomHang_ID = row_nhomhang["ID"].ToString().ToUpper();
                        string Toa_NhomHang_ID_an = dr["Toa_NhomHang_ID_an"].ToString().ToUpper();

                        // float SL_cancapnhat = float.Parse(dr["Toa_SL"].ToString().Replace(",", ""));
                        // double gia = double.Parse(e.Value.ToString().Replace(",", ""));
                        double gia = double.Parse(dr["Gia"].ToString().Replace(",", ""));
                        capnhat_gia_nhomca(Toa_NhomHang_ID, e.Value.ToString(), Toa_NhomHang_ID_an, gia);
                        show_data();
                        break;
                    }

            }
            #endregion
        }

        void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            #region
            switch (e.Column.FieldName.ToString())
            {
                case "Toa_SL":
                    {
                        DataRow dr =gv.GetDataRow(gv.FocusedRowHandle);
                        string ID = dr["ID"].ToString().ToUpper();
                        string Toa_NhomHang_ID = dr["Toa_NhomHang_ID"].ToString().ToUpper();
                        float SL_cancapnhat =float.Parse(e.Value.ToString().Replace(",",""));
                        double gia = double.Parse(dr["Toa_Gia"].ToString().Replace(",", ""));
                        capnhat_chitiet_SL(ID, Toa_NhomHang_ID, gia, SL_cancapnhat);
                        show_data();
                        break;
                    }
                case "Toa_Gia":
                    {
                        DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
                        string ID = dr["ID"].ToString().ToUpper();
                        string Toa_NhomHang_ID = dr["Toa_NhomHang_ID"].ToString().ToUpper();
                        float SL_cancapnhat = float.Parse(dr["Toa_SL"].ToString().Replace(",", ""));
                        double gia = double.Parse(e.Value.ToString().Replace(",", ""));
                        double gia_an = double.Parse(dr["Toa_Gia_an"].ToString().Replace(",", ""));
                        capnhat_chitiet_Gia(ID, Toa_NhomHang_ID, gia, gia_an, SL_cancapnhat);
                        show_data();
                        break;
                    }
                case "Toa_NhomHang_Ten":
                    {
                        DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
                        string ID = dr["ID"].ToString().ToUpper();
                        DataRowView row_nhomhang = look_nhomhang0.GetDataSourceRowByKeyValue(dr["Toa_NhomHang_Ten"].ToString()) as DataRowView;
                        string Toa_NhomHang_ID = row_nhomhang["ID"].ToString().ToUpper();
                        string Toa_NhomHang_ID_an = dr["Toa_NhomHang_ID_an"].ToString().ToUpper();
                       
                       // float SL_cancapnhat = float.Parse(dr["Toa_SL"].ToString().Replace(",", ""));
                       // double gia = double.Parse(e.Value.ToString().Replace(",", ""));
                        //double gia_an = double.Parse(dr["Toa_Gia_an"].ToString().Replace(",", ""));
                        capnhat_chitiet_nhomca(ID, Toa_NhomHang_ID, e.Value.ToString(), Toa_NhomHang_ID_an);
                        show_data();
                        break;
                    }

            }
            #endregion
        }

        void xoa_chitiet_Click(object sender, EventArgs e)
        {
            #region
            if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
            {
                DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
                double Gia =double.Parse(dr["Toa_Gia"].ToString());
                string Toa_NhomHang_ID =  dr["Toa_NhomHang_ID"].ToString().ToUpper();
                xoa_1dong(dr["ID"].ToString().ToUpper());
                capnhat_group_gia_SL(Gia,Toa_NhomHang_ID);
                capnhat_group_ca_SL(Toa_NhomHang_ID);
                show_data();
            }
            #endregion
        }
        #endregion
        //----------------------------------------------------------------------------------------------------------BIEN CAN THIET-------------------
        public string ID_Toa;
        public string NCC_ID;
        public string NCC_Name;
        public int SoToa;
        public string KyTu_Toa;
        public string Ngay_Toa, NgayTu, NgayDen;
        public int Ngay_ToaNum, NgayTuNum, NgayDenNum;
        public string TrangThai, GhiChu;
        //public DataSet Toa_Class.Table_Data;
       // public DataTable Table_deleted;
        public string User_Case;
        Toa_Class _Toa_Class = new Toa_Class();
        Class_KetNoi db = new Class_KetNoi();
        public double TKHIEN;
        public double TNUOC;
        public double TUNG;
        public double TXE;
        public double TKY;
        public double T_HH;
        public double TTHEM1;
        public double TTHEM2;
        public double TTHEM3;

        public string TEN_THEM1;
        public string TEN_THEM2;
        public string TEN_THEM3;
        public string xe1;
        public string xe2;
        public string xe3;

        public double TPHAITRA;
        public double SL_Goc, SL_Toa, SL_Lech;
        public double Gia_Goc, Gia_Toa, Gia_Lech;
        public double DS_Goc, DS_Toa, DS_Lech;
        double _GiaT_cophi;
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           Load FUNCTION             -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void load_phu()
        {
            #region
         
         
            if (User_Case == "New")
            {
                #region
                Look_tenlai.EditValue = null;
                Look_tenlai.Text = "";
                Look_tenlai.ClosePopup();
                txt_ghichu.Text = "";
                txt_tungay.Text = Get_CauHinh.ngay_text;
                txtdenngay .Text = Get_CauHinh.ngay_text;
                txtngay .Text = Get_CauHinh.ngay_text;
                checkEdit1.Checked = false;
                KyTu_Toa= _Toa_Class.load_xml_kytu();
                _Toa_Class.load_xml_Toa();
                SoToa = DuLieu.DS_Toa_XML.Tables[0].Rows.Count + 1;
                lbsotoa.Text = SoToa.ToString();
                txtkytu.Text = KyTu_Toa;
                #endregion
                if (NCC_ID != null) //BAN NUT DAU CONG TU FORM CHU
                {
                    Look_tenlai.EditValue = NCC_ID.ToLower().Trim();
                    Look_tenlai.Text = NCC_Name;
                    txt_tungay.Text = NgayTu;
                    txtdenngay.Text = NgayDen;
                }
            }
            else
            {
                #region
                //select cac thong tin tu bang toa sau do gan
              
                Look_tenlai.EditValue = NCC_ID;
                Look_tenlai.Text = NCC_Name;
                Look_tenlai.ClosePopup();
                txt_ghichu.Text = GhiChu;
                txt_tungay.Text = NgayTu;
                txtdenngay.Text = NgayDen;
                txtngay.Text = Ngay_Toa;
                if (TrangThai == "Hoàn tất")
                {
                    checkEdit1.Checked = true;
                }
                else
                {
                    checkEdit1.Checked = false;
                }
               // KyTu_Toa = _Toa_Class.load_xml_kytu();
              //  _Toa_Class.load_xml_Toa();
               // SoToa = DuLieu.DS_Toa_XML.Tables[0].Rows.Count + 1;
                lbsotoa.Text = SoToa.ToString();
                txtkytu.Text = KyTu_Toa;
                lbvidu.Text = "VD : Toa : " + txtkytu.Text + "01";
                //khoa tat ca dieu kien
                txt_tungay.Enabled = false;
                txtdenngay.Enabled = false;
                Look_tenlai.Enabled = false;
                #region
                //tinh chi phi
                //khi cap nhat hh
                //nho tinh gia co phi
                
                txt_khien.Text = String.Format("{0:N0}", TKHIEN);
                //--------
                
                txt_nuocda.Text = String.Format("{0:N0}", TNUOC);
                //--------
                
                txt_ungtruoc.Text = String.Format("{0:N0}", TUNG);
                //--------
                
                txt_ungxe.Text = String.Format("{0:N0}", TXE);
                //   --------
                
                txt_tienky.Text = String.Format("{0:N0}", TKY);
                //--------
                
                txt_k1.Text = String.Format("{0:N0}", TTHEM1);
                //--------
                
                txt_k2.Text = String.Format("{0:N0}", TTHEM2);
                //--------
                
                txt_k3.Text = String.Format("{0:N0}", TTHEM3);
                //--------
                txt_tenk1.Text = TEN_THEM1;
                txt_tenk2.Text = TEN_THEM2;
                txt_tenk3.Text = TEN_THEM3;
                x1.Text = xe1;
                x2.Text = xe2;
                x3.Text = xe3;
                
               
                #endregion
                #endregion
            }
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           KIEM TRA DIEU KIEN             -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public bool kiemtradieukien()
        {
            #region
            if (Look_tenlai.EditValue != "" && Look_tenlai.EditValue != null)
            {
                NCC_ID = Look_tenlai.EditValue.ToString().ToUpper();
                NCC_Name = Look_tenlai.Text;
                if (txt_tungay.Text != "" && txtdenngay.Text != "")
                {
                    NgayTuNum = (int)(Convert.ToDateTime(txt_tungay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                    NgayDenNum = (int)(Convert.ToDateTime(txtdenngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                    NgayTu = txt_tungay.Text;
                    NgayDen = txtdenngay.Text;
                    if (User_Case == "New")
                    {
                        Ngay_Toa = Get_CauHinh.ngay_text;
                        Ngay_ToaNum = Get_CauHinh.Ngay_DN;
                    }
                    else
                    {
                        Ngay_Toa = txtngay.Text;
                        Ngay_ToaNum = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
            #endregion
        }
        #region
        public void dieukien_leaver()
        {
            #region
            Look_tenlai.EditValueChanged += new EventHandler(Look_tenlai_EditValueChanged);
            Look_tenlai.Enter += new EventHandler(Look_tenlai_Enter);
            Look_tenlai.Leave += new EventHandler(Look_tenlai_Leave);

            txt_tungay.Leave += new EventHandler(txt_tungay_Leave);
            txtdenngay.Leave += new EventHandler(txtdenngay_Leave);

            #endregion
        }

      

        void txtdenngay_Leave(object sender, EventArgs e)
        {
            load_landau();
        }

        void txt_tungay_Leave(object sender, EventArgs e)
        {
            load_landau();
        }

        void Look_tenlai_Leave(object sender, EventArgs e)
        {
            load_landau();
        }

        void Look_tenlai_Enter(object sender, EventArgs e)
        {
            Look_tenlai.BackColor = Color.NavajoWhite;
        }

        void Look_tenlai_EditValueChanged(object sender, EventArgs e)
        {
           // ID_Toa = Look_tenlai.EditValue.ToString().ToUpper();
        }
        #endregion
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           Load              -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void load_landau()
        {

            #region
            if (User_Case == "New")
            {
                #region
                if (kiemtradieukien() == true)
                {
                    string sql = "Get_data_toamoi " + NgayTuNum + "," + NgayDenNum + ",'" + NCC_ID + "'";
                    Toa_Class.Table_Data = db.T_opendataset(sql);
                    //tinh sl goc
                    object TLG = Toa_Class.Table_Data.Tables[3].Compute("Sum(SL)", "");
                    try { SL_Goc = (double)TLG; }
                    catch { SL_Goc = 0; }
                    lslgoc.Text = String.Format("{0:N1}", SL_Goc) + " (kg)"; 
                    // tinh ds goc
                    object DSG = Toa_Class.Table_Data.Tables[3].Compute("Sum(TT)", "");
                    try { DS_Goc = (double)DSG; }
                    catch { DS_Goc = 0; }
                    ldoanhsogoc.Text = String.Format("{0:#,#}", DS_Goc) + " (vnđ)";

                    show_data();
                }
                #endregion
            }
            else
            {
                #region
                if (kiemtradieukien() == true)
                {
                    string sql = "Get_data_toa '" + ID_Toa + "'";
                    Toa_Class.Table_Data = db.T_opendataset(sql);
                    //tinh sl goc
                    object TLG = Toa_Class.Table_Data.Tables[3].Compute("Sum(SL)", "");
                    try { SL_Goc = (double)TLG; }
                    catch { SL_Goc = 0; }
                    lslgoc.Text = String.Format("{0:N1}", SL_Goc) + " (kg)";
                    // tinh ds goc
                    object DSG = Toa_Class.Table_Data.Tables[3].Compute("Sum(TT)", "");
                    try { DS_Goc = (double)DSG; }
                    catch { DS_Goc = 0; }
                    ldoanhsogoc.Text = String.Format("{0:#,#}", DS_Goc) + " (vnđ)";
                    show_data();
                }
                #endregion
            }
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           EVENT LOAD            -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        void Toa_New_Load(object sender, EventArgs e)
        {
            #region
            load_phu();
            load_landau();
            dieukien_leaver();
            leaver_chiphi();
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           XU LY TXT_KYTU           -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        #region
        public void kytu()
        {
            txtkytu.Enter+=new EventHandler(txtkytu_Enter);
            txtkytu.Leave+=new EventHandler(txtkytu_Leave);
            txtkytu.TextChanged+=new EventHandler(txtkytu_TextChanged);
        }
        void txtkytu_Leave(object sender, EventArgs e)
        {
            txtkytu.BackColor = Color.White;
        }
        void txtkytu_TextChanged(object sender, EventArgs e)
        {
            if (txtkytu.Text != "")
            {
                lbvidu.Text = "VD : Toa : " + txtkytu.Text + "01";

                string _xml_temp_kytu = Path.GetDirectoryName(Application.ExecutablePath) + @"\Toa\DataToa\KyTu.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(_xml_temp_kytu);
                XmlNodeList aNodes = doc.SelectNodes("/NewDataSet/Table");
                XmlNode node1 = aNodes[0];
                foreach (XmlNode node2 in node1.ChildNodes)
                {
                    if (node2.Name == "ID")
                        node2.InnerText = txtkytu.Text.ToString().ToUpper();

                }
                doc.Save(_xml_temp_kytu);
                _Toa_Class.load_xml_kytu();

            }
            else
            {
                msgbox.dialog_OK_info("Vui lòng nhập ký tự đầu cho toa.");
            }

        }
        void txtkytu_Enter(object sender, EventArgs e)
        {
            txtkytu.BackColor = Color.NavajoWhite;
        }
#endregion

        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           DO DU LIEU LEN GRID           -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        public void show_data()
        {
            #region
            gc.DataSource = Toa_Class.Table_Data.Tables[0];
            gv.ExpandAllGroups();
            gc2.DataSource = Toa_Class.Table_Data.Tables[1];
            gc3.DataSource = Toa_Class.Table_Data.Tables[2];
           
            thongke_saukhishow();
            this.Refresh();
            #endregion
        }
        int showform_gop;
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           THONG KE SAU KHI SHOW     -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void thongke_saukhishow()
        {
            #region
        
                //object TLG = Toa_Class.Table_Data.Tables[3].Compute("Sum(SL)", "");
                //try { SL_Goc = (double)TLG; }
                //catch { SL_Goc = 0; }
                //lslgoc.Text = String.Format("{0:N1}", SL_Goc) + " (kg)"; 
       
          

            //-------------
          
            SL_Toa =double.Parse(csl1.SummaryText.ToString());
            lsltoa.Text = String.Format("{0:N1}", SL_Toa) + " (kg)";
            //-------------

            SL_Lech = SL_Toa - SL_Goc;
            lchenhlechsl.Text = String.Format("{0:N1}", SL_Lech) + " (kg)";

            //-------------DS

            //object DSG = Toa_Class.Table_Data.Tables[3].Compute("Sum(TT)", "");
            //try { DS_Goc = (double)DSG; }
            //catch { DS_Goc = 0; }
            //ldoanhsogoc.Text = String.Format("{0:#,#}", DS_Goc) + " (vnđ)";
            //-------------

            DS_Toa =double.Parse(cthanhtien1.SummaryText.ToString());
            ldoanhsotoa.Text = String.Format("{0:#,#}", DS_Toa) + " (vnđ)";
            //-------------

            DS_Lech = DS_Toa - DS_Goc;
            lchenhlechdoanhso.Text = String.Format("{0:#,#}", DS_Lech) + " (vnđ)";

            //-------------GIA

           // object GIAG = Toa_Class.Table_Data.Tables[3].Compute("Sum(Gia)", "");
            try { Gia_Goc = DS_Goc / SL_Goc; }
            catch { Gia_Goc = 0; }
            lgiagoc.Text = String.Format("{0:N0}", Gia_Goc) + " (vnđ)";
            //-------------

            try { Gia_Toa = DS_Toa / SL_Toa; }
            catch {Gia_Toa=0; }
            lgiatoa.Text = String.Format("{0:N0}", Gia_Toa) + " (vnđ)";

            Gia_Lech = Gia_Toa - Gia_Goc;
            lchenhlechgia.Text = String.Format("{0:N0}", Gia_Lech) + " (vnđ)";
            #endregion
            kiemtra_chiphi();
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           KIEM TRA VA TINH CHI PHI     -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void kiemtra_chiphi()
        {
            #region
            //tinh chi phi
            //khi cap nhat hh
            //nho tinh gia co phi
            try { TKHIEN = double.Parse(txt_khien.Text.Replace(",", "")); txt_khien.Text = String.Format("{0:N0}", TKHIEN); }
            catch { TKHIEN = 0; txt_khien.Text = ""; }
            if (txt_khien.Text == "0") txt_khien.Text = "";
            //--------
            try { TNUOC = double.Parse(txt_nuocda.Text.Replace(",", "")); txt_nuocda.Text = String.Format("{0:N0}", TNUOC); }
            catch { TNUOC = 0; txt_nuocda.Text = ""; }
            if (txt_nuocda.Text == "0") txt_nuocda.Text = "";
            
            //--------
            try { TUNG = double.Parse(txt_ungtruoc.Text.Replace(",", "")); txt_ungtruoc.Text = String.Format("{0:N0}", TUNG); }
            catch { TUNG = 0; txt_ungtruoc.Text = ""; }
            if (txt_ungtruoc.Text == "0") txt_ungtruoc.Text = "";
            
            //--------
            try { TXE = double.Parse(txt_ungxe.Text.Replace(",", "")); txt_ungxe.Text = String.Format("{0:N0}", TXE); }
            catch { TXE = 0; txt_ungxe.Text = ""; }
            if (txt_ungxe.Text == "0") txt_ungxe.Text = "";
            
         //   --------
            try { TKY = double.Parse(txt_tienky.Text.Replace(",", "")); txt_tienky.Text = String.Format("{0:N0}", TKY); }
            catch { TKY = 0; txt_tienky.Text = ""; }
            if (txt_tienky.Text == "0") txt_tienky.Text = "";
            
            //--------
            try { TTHEM1 = double.Parse(txt_k1.Text.Replace(",", "")); txt_k1.Text = String.Format("{0:N0}", TTHEM1); }
            catch { TTHEM1 = 0; txt_k1.Text = ""; }
            if (txt_k1.Text == "0") txt_k1.Text = "";
            
            //--------
            try { TTHEM2 = double.Parse(txt_k2.Text.Replace(",", "")); txt_k2.Text = String.Format("{0:N0}", TTHEM2); }
            catch { TTHEM2 = 0; txt_k2.Text = ""; }
            if (txt_k2.Text == "0") txt_k2.Text = "";
            
            //--------
            try { TTHEM3 = double.Parse(txt_k3.Text.Replace(",", "")); txt_k3.Text = String.Format("{0:N0}", TTHEM3); }
            catch { TTHEM3 = 0; txt_k3.Text = ""; }
            if (txt_k3.Text == "0") txt_k3.Text = "";
            
            //--------

            try { T_HH = double.Parse(ctienhh3.SummaryText.ToString().Replace(",", "")); }
            catch { T_HH = 0; }
           txt_tienhh.Text = String.Format("{0:N0}", T_HH);
           ltienhh.Text = String.Format("{0:N0}", T_HH);
            double TongchiPhi =  TKHIEN + TNUOC;
            lchiphi.Text = String.Format("{0:N0}", TongchiPhi);
            double ChiPhikhac =  TUNG + TXE + TKY + TTHEM1 + TTHEM2 + TTHEM3;
            lbkhac.Text = String.Format("{0:N0}", ChiPhikhac);
            TPHAITRA = DS_Toa - T_HH - TongchiPhi - ChiPhikhac;
            
            if (SL_Toa > 0)
            {
                _GiaT_cophi = (DS_Toa - TongchiPhi - T_HH) / SL_Toa;
            }
            else
            {
                _GiaT_cophi = 0;
            }
            ////--------
            lb_giacophi.Text = String.Format("{0:N0}", _GiaT_cophi);
            lphaitra.Text=String.Format("{0:N0}", TPHAITRA);
            //if (txt_tenk1.Text != "")
            //{
            //    TEN_THEM1 = txt_tenk1.Text;
            //}
            //if (txt_tenk2.Text != "")
            //{
            //    TEN_THEM2 = txt_tenk2.Text;
            //}
            //if (txt_tenk3.Text != "")
            //{
            //    TEN_THEM3 = txt_tenk3.Text;
            //}
            //if (x1.Text != "")
            //{
            //    xe1 = x1.Text;
            //}
            //if (x2.Text != "")
            //{
            //    xe2 = x2.Text;
            //}
            //if (x3.Text != "")
            //{
            //    xe3 = x3.Text;
            //}
            #endregion
        }
        #region
        public void leaver_chiphi()
        {
            #region
            txt_khien.Leave += new EventHandler(txt_khien_Leave);
            txt_nuocda.Leave += new EventHandler(txt_nuocda_Leave);
            txt_ungtruoc.Leave += new EventHandler(txt_ungtruoc_Leave);
            txt_ungxe.Leave += new EventHandler(txt_ungxe_Leave);
            txt_tienky.Leave += new EventHandler(txt_tienky_Leave);
            txt_k1.Leave += new EventHandler(txt_k1_Leave);
            txt_k2.Leave += new EventHandler(txt_k2_Leave);
            txt_k3.Leave += new EventHandler(txt_k3_Leave);
            #endregion
        }

        void txt_k3_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_k2_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_k1_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_tienky_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_ungxe_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_ungtruoc_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }

        void txt_nuocda_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi(); 
        }

        void txt_khien_Leave(object sender, EventArgs e)
        {
            kiemtra_chiphi();
        }
        #endregion

        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------           XOA MOT DONG     -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void xoa_1dong(string ID_Dong)
        {
            DataRow[] dr = Toa_Class.Table_Data.Tables[0].Select("ID='" + ID_Dong.ToUpper() + "'");
            DataRow dr_delete = Toa_Class.Table_deleted.NewRow();
            dr_delete["ID"] = ID_Dong;
            Toa_Class.Table_deleted.Rows.Add(dr_delete);
            Toa_Class.Table_deleted.AcceptChanges();
            Toa_Class.Table_Data.Tables[0].Rows.Remove(dr[0]);
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT SL   -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void capnhat_chitiet_SL(string ID_Dong,string ID_Nhom,double gia,float SL_cancapnhat)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[0].Select("ID='" + ID_Dong.ToUpper() + "'");
            dr[0]["Toa_SL"] = SL_cancapnhat;
            //cap nhat thanh tien
            double thanhtien = SL_cancapnhat * gia;
            dr[0]["ThanhTien"] = thanhtien;
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
            //cap nhat tien hh
            double tienhh;
            if (float.Parse(dr[0]["HHSL"].ToString()) == 0)
            {
                tienhh = (thanhtien * float.Parse(dr[0]["HHPC"].ToString())) / 100;
            }
            else
            {
                tienhh = SL_cancapnhat * double.Parse(dr[0]["HHSL"].ToString());
            }
            dr[0]["TienHoaHong"] = tienhh;
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
            capnhat_group_gia_SL(gia, ID_Nhom);
            capnhat_group_ca_SL(ID_Nhom);
            #endregion
        }
        public void capnhat_group_gia_SL(double gia, string ID_Nhom)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[1].Select("Gia=" + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_Gia = " + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
           // msgbox.dialog_OK_error(SLTinh.ToString());
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[1].Rows.Remove(dr[0]);
            }
            else
            {
                dr[0]["Sum_SL"] = SLTinh;
                double thanhtien =  SLTinh * gia;
                dr[0]["Sum_ThanhTien"] = thanhtien;
                double tienhh;
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (thanhtien * float.Parse(dr[0]["MHHPC"].ToString()))/100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
            }
            

            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion
        }
        public void capnhat_group_ca_SL(string ID_Nhom)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr[0]);
            }
            else
            {
                dr[0]["Sum_SL"] = SLTinh;
                //cap nhat TT
                object TT = dtv.ToTable().Compute("Sum(ThanhTien)", "");
                double TTTinh;
                try { TTTinh = (double)TT; }
                catch { TTTinh = 0; }
                dr[0]["Sum_ThanhTien"] = TTTinh;
                dr[0]["Lech"] = double.Parse(dr[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", "")) - TTTinh;
                //cap nhat gia binh quan
                dr[0]["Gia"] = TTTinh / SLTinh;
                double tienhh;
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (TTTinh * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
            }
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT GIA   -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void capnhat_chitiet_Gia(string ID_Dong, string ID_Nhom, double gia_moi, double Gia_an, float SL)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[0].Select("ID='" + ID_Dong.ToUpper() + "'");
            dr[0]["Toa_Gia"] = gia_moi;
            //cap nhat thanh tien
            double thanhtien = SL * gia_moi;
            dr[0]["ThanhTien"] = thanhtien;
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
            //cap nhat tien hh
            double tienhh;
            if (float.Parse(dr[0]["HHSL"].ToString()) == 0)
            {
                tienhh = (thanhtien * float.Parse(dr[0]["HHPC"].ToString())) / 100;
            }
            else
            {
                tienhh = SL * double.Parse(dr[0]["HHSL"].ToString());
            }
            dr[0]["TienHoaHong"] = tienhh;
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
            //capnhat_group_gia_SL(gia, ID_Nhom);
            //capnhat_group_ca_SL(ID_Nhom);
            #endregion
            capnhat_group_gia_Gia(ID_Nhom, Gia_an, gia_moi);
            capnhat_group_ca_Gia(ID_Nhom);
            //cap nhat gia an

            dr[0]["Toa_Gia_an"] = gia_moi;
            Toa_Class.Table_Data.Tables[0].AcceptChanges();
            
        }
        public void capnhat_group_gia_Gia(string ID_Nhom, double giacu,double gia_moi)
        {
            #region
            //cap nhat gia cu truoc
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[1].Select("Gia=" + giacu + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataRow dr_moi = Toa_Class.Table_Data.Tables[1].NewRow();
            for (int i = 0; i < Toa_Class.Table_Data.Tables[1].Columns.Count; i++)
            {
                dr_moi[Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()] = dr[0][Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()];
            }
            //tao dong moi truoc khi dong cu bi xoa
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);

            dtv.RowFilter = "Toa_Gia = " + giacu + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[1].Rows.Remove(dr[0]);
            }
            else
            {
                dr[0]["Gia"] = giacu;
                dr[0]["Sum_SL"] = SLTinh;
                double thanhtien = SLTinh * giacu;
                dr[0]["Sum_ThanhTien"] = thanhtien;
                double tienhh;
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (thanhtien * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            #endregion


            //cap nhat gia moi
            DataRow[] dr_giamoi = Toa_Class.Table_Data.Tables[1].Select("Gia=" + gia_moi + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            if (dr_giamoi.Length > 0) //da co gia moi truoc do
            {
                #region
                DataView dtv_moi = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv_moi.RowFilter = "Toa_Gia = " + gia_moi + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
                object SLM = dtv_moi.ToTable().Compute("Sum(Toa_SL)", "");
                double SLMoi;
                try { SLMoi = (double)SLM; }
                catch { SLMoi = 0; }
                if (SLMoi == 0)
                {
                    Toa_Class.Table_Data.Tables[1].Rows.Remove(dr_giamoi[0]);
                }
                else
                {
                    dr_giamoi[0]["Gia"] = gia_moi;
                    dr_giamoi[0]["Gia_an"] = gia_moi;
                    dr_giamoi[0]["Sum_SL"] = SLMoi;
                    double thanhtien = SLMoi * gia_moi;
                    dr_giamoi[0]["Sum_ThanhTien"] = thanhtien;
                    double tienhh;
                    if (float.Parse(dr_giamoi[0]["MHHSL"].ToString()) == 0)
                    {
                        tienhh = (thanhtien * float.Parse(dr_giamoi[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        tienhh = SLMoi * double.Parse(dr_giamoi[0]["MHHSL"].ToString());
                    }
                    dr_giamoi[0]["Sum_TienHoaHong"] = tienhh;
                    Toa_Class.Table_Data.Tables[1].AcceptChanges();
                }
                #endregion
            }
            else
            {
                //them 1 dong vo group ca
                //update dong moi
                #region
               DataView dtv_moi = new DataView(Toa_Class.Table_Data.Tables[0]);
               dtv_moi.RowFilter = "Toa_Gia = " + gia_moi + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
                object SLM = dtv_moi.ToTable().Compute("Sum(Toa_SL)", "");
                double SLMoi;
                try { SLMoi = (double)SLM; }
                catch { SLMoi = 0; }
               // msgbox.dialog_OK_info(SLMoi.ToString());
                if (SLMoi == 0)
                {
                    Toa_Class.Table_Data.Tables[1].Rows.Remove(dr_moi);
                }
                else
                {
                    dr_moi["Gia"] = gia_moi;
                    dr_moi["Gia_an"] = gia_moi;
                    dr_moi["Sum_SL"] = SLMoi;
                    double thanhtien = SLMoi * gia_moi;
                    dr_moi["Sum_ThanhTien"] = thanhtien;
                    double tienhh;
                    if (float.Parse(dr_moi["MHHSL"].ToString()) == 0)
                    {
                        tienhh = (thanhtien * float.Parse(dr_moi["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        tienhh = SLMoi * double.Parse(dr_moi["MHHSL"].ToString());
                    }
                    dr_moi["Sum_TienHoaHong"] = tienhh;
                    Toa_Class.Table_Data.Tables[1].Rows.Add(dr_moi);
                    Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                    //Toa_Class.Table_Data.Tables[1] = Toa_Class.Table_Data.Tables[1].DefaultView.ToTable();
                }
                #endregion           
            }
            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion
        }
        public void capnhat_group_ca_Gia(string ID_Nhom)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
           

            float SLTinh = float.Parse(dr[0]["Sum_SL"].ToString().Replace(",",""));
           
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr[0]);
            }
            else
            {
               
                //cap nhat TT
                object TT = dtv.ToTable().Compute("Sum(ThanhTien)", "");
                double TTTinh;
                try { TTTinh = (double)TT; }
                catch { TTTinh = 0; }
                dr[0]["Sum_ThanhTien"] = TTTinh;
                dr[0]["Lech"] = double.Parse(dr[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", "")) - TTTinh;
                //cap nhat gia binh quan
                dr[0]["Gia"] = TTTinh / SLTinh;
                double tienhh;
                //xem lai hh
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (TTTinh * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
            }
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT GIA TRONG GV1,GV2  -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void update_gv1_gia(String ID_Nhom,double gia_moi, double Gia_an)
        {
            #region
            //cap nhat gia table1 dua vao id_nhom va gia truoc
            //cap nhat thanhtien,hoa hong
            
            //loc ca dong trong table 0 theo id_nhom va gia_an
            //update tung dong
            DataRow[] dr = Toa_Class.Table_Data.Tables[0].Select("Toa_Gia = " + Gia_an + " and Toa_NhomHang_ID ='"+ID_Nhom+"'");
            for (int i = 0; i < dr.Length; i++)
            {
                dr[i]["Toa_Gia"] = gia_moi;
                dr[i]["Toa_Gia_an"] = gia_moi;
                double thanhtien = float.Parse(dr[i]["Toa_SL"].ToString()) * gia_moi;
                dr[i]["ThanhTien"] = thanhtien;
                double tienhh;
                if (float.Parse(dr[i]["HHSL"].ToString()) == 0)
                {
                    tienhh = (thanhtien * float.Parse(dr[i]["HHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = float.Parse(dr[i]["Toa_SL"].ToString()) * double.Parse(dr[i]["HHSL"].ToString());
                }
                dr[i]["TienHoaHong"] = tienhh;
                Toa_Class.Table_Data.Tables[0].AcceptChanges();
            }
            //update gia va gia an table 1
            //DataRow[] dr_gv1 = Toa_Class.Table_Data.Tables[1].Select("Gia = " + Gia_an + " and Toa_NhomHang_ID ='" + ID_Nhom + "'");
            //dr_gv1[0]["Gia"] = gia_moi;
            //dr_gv1[0]["Gia_an"] = gia_moi;
            //double thanhtien_gv1 = float.Parse(dr_gv1[0]["Sum_SL"].ToString()) * gia_moi;
            //dr_gv1[0]["Sum_ThanhTien"] = thanhtien_gv1;
            //double tienhhgv1;
            //if (float.Parse(dr_gv1[0]["MHHSL"].ToString()) == 0)
            //{
            //    tienhhgv1 = (thanhtien_gv1 * float.Parse(dr_gv1[0]["MHHPC"].ToString())) / 100;
            //}
            //else
            //{
            //    tienhhgv1 = float.Parse(dr_gv1[0]["Sum_SL"].ToString()) * double.Parse(dr_gv1[0]["MHHSL"].ToString());
            //}
            //dr_gv1[0]["Sum_TienHoaHong"] = tienhhgv1;
            //Toa_Class.Table_Data.Tables[0].AcceptChanges();

            //cap nhat doanh,tien hh so table 2
            //DataRow[] dr_gv2 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '"+ID_Nhom+"'");
            //DataView drv2 =new DataView(Toa_Class.Table_Data.Tables[1]);
            //drv2.RowFilter = "Toa_NhomHang_ID = '"+ID_Nhom+"'";
            //object DS = drv2.ToTable().Compute("Sum(Sum_ThanhTien)", "");
            //    double DSM;
            //    try { DSM = (double)DS; }
            //    catch { DSM = 0; }
            //dr_gv2[0]["Sum_ThanhTien"]=DSM;
            //try { dr_gv2[0]["Gia"] = DSM / float.Parse(dr_gv2[0]["Sum_SL"].ToString()); }
            //catch { dr_gv2[0]["Gia"] = 0; }
            //dr_gv2[0]["Lech"] = double.Parse(dr_gv2[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", "")) - DSM;

            //double tienhh2;
            //    if (float.Parse(dr_gv2[0]["MHHSL"].ToString()) == 0)
            //    {
            //        tienhh2 = (DSM * float.Parse(dr_gv2[0]["MHHPC"].ToString())) / 100;
            //    }
            //    else
            //    {
            //        tienhh2 = float.Parse(dr_gv2[0]["Sum_SL"].ToString()) * double.Parse(dr_gv2[0]["MHHSL"].ToString());
            //    }
            //    dr_gv2[0]["Sum_TienHoaHong"] = tienhh2;
            //    Toa_Class.Table_Data.Tables[2].AcceptChanges();
#endregion
        }
        public void update_gv2_gia(String ID_Nhom, double gia_moi)
        {
            #region
            DataRow[] dr2 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            dr2[0]["Gia"] = gia_moi;
            double thanhtien = float.Parse(dr2[0]["Sum_SL"].ToString()) * gia_moi;
            dr2[0]["Sum_ThanhTien"] = thanhtien;
            dr2[0]["Lech"] = double.Parse(dr2[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", "")) - thanhtien;
            double tienhh2;
            if (float.Parse(dr2[0]["MHHSL"].ToString()) == 0)
            {
                tienhh2 = (thanhtien * float.Parse(dr2[0]["MHHPC"].ToString())) / 100;
            }
            else
            {
                tienhh2 = float.Parse(dr2[0]["Sum_SL"].ToString()) * double.Parse(dr2[0]["MHHSL"].ToString());
            }
            dr2[0]["Sum_TienHoaHong"] = tienhh2;
            Toa_Class.Table_Data.Tables[2].AcceptChanges();

            //cap nhat table 1
            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            for (int i = 0; i < dr1.Length; i++)
            {
                dr1[i]["Gia"] = gia_moi;
                dr1[i]["Gia_an"] = gia_moi;
                double thanhtien1 = float.Parse(dr1[i]["Sum_SL"].ToString().Replace(",", "")) * gia_moi;
                dr1[i]["Sum_ThanhTien"] = thanhtien1;
                double tienhh1;
                if (float.Parse(dr1[i]["MHHSL"].ToString()) == 0)
                {
                    tienhh1 = (thanhtien1 * float.Parse(dr1[i]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh1 = float.Parse(dr1[i]["Sum_SL"].ToString()) * float.Parse(dr1[i]["MHHSL"].ToString());
                }
                dr1[i]["Sum_TienHoaHong"] = tienhh1;
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            //cap nhat table 0
            DataRow[] dr0 = Toa_Class.Table_Data.Tables[0].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            for (int i = 0; i < dr0.Length; i++)
            {
                dr0[i]["Toa_Gia"] = gia_moi;
                dr0[i]["Toa_Gia_an"] = gia_moi;
                double thanhtien0 = float.Parse(dr0[i]["Toa_SL"].ToString().Replace(",", "")) * gia_moi;
                dr0[i]["ThanhTien"] = thanhtien0;
                double tienhh0;
                if (float.Parse(dr0[i]["HHSL"].ToString()) == 0)
                {
                    tienhh0 = (thanhtien0 * float.Parse(dr0[i]["HHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh0 = float.Parse(dr0[i]["Toa_SL"].ToString()) * float.Parse(dr0[i]["HHSL"].ToString());
                }
                dr0[i]["TienHoaHong"] = tienhh0;
                Toa_Class.Table_Data.Tables[0].AcceptChanges();

            }
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT SL TRONG GV1  -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void update_gv1_SL(string ID_Nhom,double Gia,float SL_Can_capnhat)
        {
            #region
            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom + "' and Gia = " + Gia + "");
            dr1[0]["Sum_SL"] = SL_Can_capnhat;
            double thanhtien1 = double.Parse(dr1[0]["Gia"].ToString().Replace(",", "")) * SL_Can_capnhat;
            dr1[0]["Sum_ThanhTien"] = thanhtien1;
            //cap nhat hh
            double tienhh1;
            if (float.Parse(dr1[0]["MHHSL"].ToString()) == 0)
            {
                tienhh1 = (thanhtien1 * float.Parse(dr1[0]["MHHPC"].ToString()))/100;
            }
            else
            {
                tienhh1 = SL_Can_capnhat * double.Parse(dr1[0]["MHHSL"].ToString());
            }
            dr1[0]["Sum_TienHoaHong"] = tienhh1;
            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            //cap nhat table 0
            //cap nhat dong dau 
            DataRow[] dr0 = Toa_Class.Table_Data.Tables[0].Select("Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + Gia + "");
            for (int i = 0; i < dr0.Length; i++)
            {
                if (i == 0)
                {
                    dr0[i]["Toa_SL"] = SL_Can_capnhat;
                    dr0[i]["ThanhTien"] = thanhtien1;
                    dr0[i]["TienHoaHong"] = tienhh1;
                   
                    Toa_Class.Table_Data.Tables[0].AcceptChanges();
                }
                else
                {
                    dr0[i]["Toa_SL"] = 0;
                    dr0[i]["ThanhTien"] = 0;
                    dr0[i]["TienHoaHong"] = 0;
                    Toa_Class.Table_Data.Tables[0].AcceptChanges();
                }
            }
            
            //cap nhat DS table 2,Lech,tienhh
            DataRow[] dr2 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            //loc DS trong table 1
            DataView dtv1 = new DataView(Toa_Class.Table_Data.Tables[1]);
            dtv1.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "'";

            object OSL = dtv1.ToTable().Compute("Sum(Sum_SL)", "");
            double SSL;
            try { SSL = (double)OSL; }
            catch { SSL = 0; }
            dr2[0]["Sum_SL"] = SSL;

            object DS = dtv1.ToTable().Compute("Sum(Sum_ThanhTien)", "");
            double DSM;
            try { DSM = (double)DS; }
            catch { DSM = 0; }

            dr2[0]["Sum_ThanhTien"] = DSM;
            try { dr2[0]["Gia"] = DSM / float.Parse(dr2[0]["Sum_SL"].ToString()); }
            catch { dr2[0]["Gia"] = 0; }

            dr2[0]["Lech"] = double.Parse(dr2[0]["Sum_ThanhTien_Goc"].ToString()) - DSM;
            //tinh hoa hong
            if (float.Parse(dr2[0]["MHHSL"].ToString()) == 0)
            {
                dr2[0]["Sum_TienHoaHong"] = (DSM * float.Parse(dr2[0]["MHHPC"].ToString()))/100;
            }
            else
            {
                dr2[0]["Sum_TienHoaHong"] = float.Parse(dr2[0]["Sum_SL"].ToString()) * float.Parse(dr2[0]["MHHSL"].ToString());
            }

            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT HH  -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void update_HHPC(string nhom_ID,float HHPC)
        {
            //ham kiem tra chon pc hay sl dat trong gv2 de cancel khi khong dung
            #region
            DataRow[] dr2 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            dr2[0]["MHHPC"] = HHPC;
            //cap nhat tien hh
            dr2[0]["Sum_TienHoaHong"] = (double.Parse(dr2[0]["Sum_ThanhTien"].ToString().Replace(",", "")) * HHPC) / 100;
            Toa_Class.Table_Data.Tables[2].AcceptChanges();

            //cap nhat tung dong trong table 1
            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            for (int i = 0; i < dr1.Length; i++)
            {
                dr1[i]["MHHPC"] = HHPC;
                dr1[i]["Sum_TienHoaHong"] = (double.Parse(dr1[i]["Sum_ThanhTien"].ToString()) * HHPC) / 100;
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            

            //cap nhat tung dong trong table 0
            DataRow[] dr0 = Toa_Class.Table_Data.Tables[0].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            for (int i = 0; i < dr0.Length; i++)
            {
                dr0[i]["HHPC"] = HHPC;
                dr0[i]["TienHoaHong"] = (double.Parse(dr0[i]["ThanhTien"].ToString()) * HHPC) / 100;
                Toa_Class.Table_Data.Tables[0].AcceptChanges();
            }
            #endregion
        }
        public void update_HHSL(string nhom_ID, double HHSL)
        {
            //ham kiem tra chon pc hay sl dat trong gv2 de cancel khi khong dung
            #region
            DataRow[] dr2 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            dr2[0]["MHHSL"] = HHSL;
            //cap nhat tien hh
            dr2[0]["Sum_TienHoaHong"] = float.Parse(dr2[0]["Sum_SL"].ToString().Replace(",", "")) * HHSL;
            Toa_Class.Table_Data.Tables[2].AcceptChanges();

            //cap nhat tung dong trong table 1
            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            for (int i = 0; i < dr1.Length; i++)
            {
                dr1[i]["MHHSL"] = HHSL;
                dr1[i]["Sum_TienHoaHong"] = float.Parse(dr1[i]["Sum_SL"].ToString()) * HHSL;
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            //cap nhat tung dong trong table 0
            DataRow[] dr0 = Toa_Class.Table_Data.Tables[0].Select("Toa_NhomHang_ID = '" + nhom_ID + "'");
            for (int i = 0; i < dr0.Length; i++)
            {
                dr0[i]["HHSL"] = HHSL;
                dr0[i]["TienHoaHong"] = float.Parse(dr0[i]["Toa_SL"].ToString()) * HHSL;
                Toa_Class.Table_Data.Tables[0].AcceptChanges();
            }
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          CAP NHAT NHOM CA  -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        public void capnhat_chitiet_nhomca(string ID_Dong,string ID_Nhom,string TenNhom,string ID_Nhom_Truoc)
        {
            #region
            DataRow[] dr0 = Toa_Class.Table_Data.Tables[0].Select("ID = '" + ID_Dong + "'");
            dr0[0]["Toa_NhomHang_ID"] = ID_Nhom;
            dr0[0]["Toa_NhomHang_ID_an"] = ID_Nhom;
            dr0[0]["Toa_NhomHang_Ten"] = TenNhom;
            //cap nhat HH cho no neu da co nhom nay trong table 2
            DataRow[] dt_kiemtra = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            if (dt_kiemtra.Length > 0)
            {
                dr0[0]["HHSL"] = dt_kiemtra[0]["MHHSL"];
                dr0[0]["HHPC"] = dt_kiemtra[0]["MHHPC"];
                if (float.Parse(dr0[0]["HHSL"].ToString()) == 0)
                {
                    dr0[0]["TienHoaHong"] = (double.Parse(dr0[0]["Toa_Gia"].ToString()) * float.Parse(dr0[0]["Toa_SL"].ToString()) * float.Parse(dr0[0]["HHPC"].ToString())) / 100;
                }
                else
                {
                    dr0[0]["TienHoaHong"] =  float.Parse(dr0[0]["Toa_SL"].ToString()) * float.Parse(dr0[0]["HHSL"].ToString());
                }
            }
            else
            {
                //giu nguyen hh
            }
            Toa_Class.Table_Data.Tables[0].AcceptChanges();

            //cap nhat lai nhom group gia,cap nhat SL,DS,HH

            //cap nhat group cu

            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom_Truoc + "' and Gia = "+double.Parse(dr0[0]["Toa_Gia"].ToString())+"");
            DataRow dr_add = Toa_Class.Table_Data.Tables[1].NewRow();
            for (int i = 0; i < Toa_Class.Table_Data.Tables[1].Columns.Count; i++)
            {
                if(Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString().Trim().ToLower()!="sum_sl_an")
                   dr_add[Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()] = dr1[0][Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()].ToString();
                else
                    dr_add[Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()] =double.Parse("0"+ dr1[0][Toa_Class.Table_Data.Tables[1].Columns[i].ColumnName.ToString()].ToString()).ToString();
            }
            DataView dtv1 = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv1.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom_Truoc + "' and Toa_Gia = " + double.Parse(dr0[0]["Toa_Gia"].ToString()) + "";
            object SL = dtv1.ToTable().Compute("Sum(Toa_SL)","");
            double SLT;
            try { SLT = (double)SL; }
            catch { SLT = 0; }
            if (SLT == 0)
            {
                Toa_Class.Table_Data.Tables[1].Rows.Remove(dr1[0]);
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            else
            {
                dr1[0]["Sum_SL"] = SLT;
                double thanhtien1 = SLT * double.Parse(dr1[0]["Gia"].ToString());
                dr1[0]["Sum_ThanhTien"] = thanhtien1;
                if (float.Parse(dr1[0]["MHHSL"].ToString()) == 0)
                {
                    dr1[0]["Sum_TienHoaHong"] = (thanhtien1*float.Parse(dr1[0]["MHHPC"].ToString()))/100;
                }
                else
                {
                    dr1[0]["Sum_TienHoaHong"] = float.Parse(dr1[0]["Sum_SL"].ToString()) * SLT;
                }
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            //cap nhat group moi
            DataRow[] drmoi = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom + "' and Gia = " + double.Parse(dr0[0]["Toa_Gia"].ToString()) + "");
            if (drmoi.Length == 0)
            {
                #region
                //update dong moi
                dr_add["Toa_NhomHang_ID"] = ID_Nhom;
                dr_add["Toa_NhomHang_ID_an"] = ID_Nhom;
                dr_add["Toa_NhomHang_Ten"] = TenNhom;
                //cap nhat sl
                DataView dtvmoi = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtvmoi.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + double.Parse(dr_add["Gia"].ToString()) + "";
                Object OSLmoi = dtvmoi.ToTable().Compute("Sum(Toa_SL)", "");
                double SLmoi;
                try { SLmoi = (double)OSLmoi; }
                catch { SLmoi = 0; }
                double thanhtienmoi = SLmoi * double.Parse(dr_add["Gia"].ToString());
                dr_add["Sum_SL"] = SLmoi;
                dr_add["Sum_ThanhTien"] = thanhtienmoi;
                //cap nhat hh
                //kiem tra nhom moi da co trong table 2 chua neu co thi lay hh cua no neu chua van giu hh cu ->ham tren con thieu dong nay
                DataRow[] dr_kt = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
                if (dr_kt.Length > 0)
                {
                    dr_add["MHHSL"] = dr_kt[0]["MHHSL"];
                    dr_add["MHHPC"] = dr_kt[0]["MHHPC"];
                    //cap nhat cho cac dong chi tiet

                }
                else
                {
                    //giu nguyen doi hinh
                }
                if (float.Parse(dr_add["MHHSL"].ToString()) == 0)
                {
                    dr_add["Sum_TienHoaHong"]=(thanhtienmoi*float.Parse(dr_add["MHHPC"].ToString()))/100;
                }
                else
                {
                    dr_add["Sum_TienHoaHong"] = double.Parse(dr_add["MHHSL"].ToString()) * SLmoi;
                }
                Toa_Class.Table_Data.Tables[1].Rows.Add(dr_add);
                Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
                #endregion
            }
            else
            {
                //cap nhat group da co
                #region
                DataView dtv_daco = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv_daco.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + double.Parse(dr0[0]["Toa_Gia"].ToString()) + "";
                object OSLdaco = dtv_daco.ToTable().Compute("Sum(Toa_SL)", "");
                double SLdaco;
                try { SLdaco = (double)OSLdaco; }
                catch { SLdaco=0;}
                if(SLdaco==0)
                {
                    Toa_Class.Table_Data.Tables[1].Rows.Remove(drmoi[0]);
                    Toa_Class.Table_Data.Tables[1].AcceptChanges();
                }
                else
                {
                    drmoi[0]["Sum_SL"] = SLdaco;
                    double thanhtiendaco = SLdaco * double.Parse(drmoi[0]["Gia"].ToString());
                    drmoi[0]["Sum_ThanhTien"] = thanhtiendaco;
                    if (float.Parse(drmoi[0]["MHHSL"].ToString()) == 0)
                    {
                        drmoi[0]["Sum_TienHoaHong"] = (thanhtiendaco * float.Parse(drmoi[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        drmoi[0]["Sum_TienHoaHong"] = float.Parse(drmoi[0]["Sum_SL"].ToString()) * SLdaco;
                    }
                    Toa_Class.Table_Data.Tables[1].AcceptChanges();
                }
                #endregion

            }
            //cap nhat nhom cu gv2
            //cap nhat lai nhom group ca ,cap nhat SL,DS,HH,Lech,GiaBQ
            DataRow[] dr_gv2_cu = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom_Truoc + "'");
            //tao dongcu
            DataRow dr_gv2_add = Toa_Class.Table_Data.Tables[2].NewRow();
            for (int i = 0; i < Toa_Class.Table_Data.Tables[2].Columns.Count; i++)
            {
                dr_gv2_add[Toa_Class.Table_Data.Tables[2].Columns[i].ColumnName.ToString()] = dr_gv2_cu[0][Toa_Class.Table_Data.Tables[2].Columns[i].ColumnName.ToString()];
            }
            DataView dtv_gv2 = new DataView(Toa_Class.Table_Data.Tables[1]);
            dtv_gv2.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom_Truoc + "'";
            object OSL2 = dtv_gv2.ToTable().Compute("Sum(Sum_SL)","");
            double SL2;
            try { SL2 = (double)OSL2; }
            catch { SL2 = 0; }
            if (SL2 == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr_gv2_cu[0]);

                Toa_Class.Table_Data.Tables[2].AcceptChanges();
            }
            else
            {
                //SL,TT,lech,GiaBQ
                dr_gv2_cu[0]["Sum_SL"] = SL2;
                double thanhtien2 = SL2*double.Parse(dr_gv2_cu[0]["Gia"].ToString());
                dr_gv2_cu[0]["Sum_ThanhTien"] = thanhtien2;
                dr_gv2_cu[0]["Lech"] = double.Parse(dr_gv2_cu[0]["Sum_ThanhTien"].ToString()) - thanhtien2;
                dr_gv2_cu[0]["Gia"] = thanhtien2 / SL2;
                if (float.Parse(dr_gv2_cu[0]["MHHSL"].ToString()) == 0)
                {
                    dr_gv2_cu[0]["Sum_TienHoaHong"] =( thanhtien2 * float.Parse(dr_gv2_cu[0]["MHHPC"].ToString()))/100;
                }
                else
                {
                    dr_gv2_cu[0]["Sum_TienHoaHong"] = SL2 * double.Parse(dr_gv2_cu[0]["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[2].AcceptChanges();
            }

            //cap nhat nhom moi gv2 neu co thi update khong co thi them vao voi hh = hh dong chi tiet
            DataRow[] dr_gv2_kiemtra = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            if (dr_gv2_kiemtra.Length > 0)
            {
                #region
                DataRow[] dr_gv2_moi = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
                DataView dtv2moi = new DataView(Toa_Class.Table_Data.Tables[1]);
                dtv2moi.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "'";
                object OSL3 = dtv2moi.ToTable().Compute("Sum(Sum_SL)", "");
                double SL3;
                try { SL3 = (double)OSL3; }
                catch { SL3 = 0; }
                if (SL3 == 0)
                {
                    Toa_Class.Table_Data.Tables[2].Rows.Remove(dr_gv2_moi[0]);
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                else
                {
                    //SL,TT,lech,GiaBQ
                    dr_gv2_moi[0]["Sum_SL"] = SL3;
                    double thanhtien3 = SL3 * double.Parse(dr_gv2_moi[0]["Gia"].ToString());
                    dr_gv2_moi[0]["Sum_ThanhTien"] = thanhtien3;
                    dr_gv2_moi[0]["Lech"] = double.Parse(dr_gv2_moi[0]["Sum_ThanhTien"].ToString()) - thanhtien3;
                    dr_gv2_moi[0]["Gia"] = thanhtien3 / SL3;
                    if (float.Parse(dr_gv2_moi[0]["MHHSL"].ToString()) == 0)
                    {
                        dr_gv2_moi[0]["Sum_TienHoaHong"] = (thanhtien3 * float.Parse(dr_gv2_moi[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        dr_gv2_moi[0]["Sum_TienHoaHong"] = SL3 * double.Parse(dr_gv2_moi[0]["MHHSL"].ToString());
                    }
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                #endregion
            }
            else
            {
                dr_gv2_add["Toa_NhomHang_ID"] = ID_Nhom;
                dr_gv2_add["Toa_NhomHang_Ten"] = TenNhom;
                DataView _dtv_gv2 = new DataView(Toa_Class.Table_Data.Tables[1]);
                _dtv_gv2.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "'";
                object _OSL2 = _dtv_gv2.ToTable().Compute("Sum(Sum_SL)", "");
                double _SL2;
                try { _SL2 = (double)_OSL2; }
                catch { _SL2 = 0; }

                dr_gv2_add["Sum_SL"] = _SL2;
                double _thanhtien2 = _SL2 * double.Parse(dr_gv2_add["Gia"].ToString());
                dr_gv2_add["Sum_ThanhTien"] = _thanhtien2;
                dr_gv2_add["Lech"] = double.Parse(dr_gv2_add["Sum_ThanhTien"].ToString()) - _thanhtien2;
                dr_gv2_add["Gia"] = _thanhtien2 / _SL2;
                if (float.Parse(dr_gv2_add["MHHSL"].ToString()) == 0)
                {
                    dr_gv2_add["Sum_TienHoaHong"] = (_thanhtien2 * float.Parse(dr_gv2_add["MHHPC"].ToString())) / 100;
                }
                else
                {
                    dr_gv2_add["Sum_TienHoaHong"] = _SL2 * double.Parse(dr_gv2_add["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[2].Rows.Add(dr_gv2_add);
                Toa_Class.Table_Data.Tables[2].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                Toa_Class.Table_Data.Tables[2].AcceptChanges();
            }
            #endregion
        }
        public void capnhat_gia_nhomca(string ID_Nhom,string TenNhom,string ID_Nhom_truoc,double gia)
        {
            #region
            //tim trong table 1 co group nao thoa ID_Nhom moi va gia nay khong
            DataRow[] dr1_kt = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom + "' and Gia = " + gia + "");
            DataRow[] dr1 = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom_truoc + "' and Gia = " + gia + "");
            float HHPC;
            double HHSL;
            if (dr1_kt.Length > 0)
            {
                HHPC = float.Parse(dr1_kt[0]["MHHPC"].ToString());
                HHSL = double.Parse(dr1_kt[0]["MHHSL"].ToString());
            }
            else
            {
                HHPC = float.Parse(dr1[0]["MHHPC"].ToString());
                HHSL = double.Parse(dr1[0]["MHHSL"].ToString());
            }
            dr1[0]["Toa_NhomHang_ID"] = ID_Nhom;
            dr1[0]["Toa_NhomHang_Ten"] = TenNhom;          
            dr1[0]["MHHPC"] = HHPC;
            dr1[0]["MHHSL"] = HHSL;
            Toa_Class.Table_Data.Tables[1].AcceptChanges();
           //cap nhat chi tiet
            DataRow[] dt_chitiet = Toa_Class.Table_Data.Tables[0].Select("Toa_NhomHang_ID = '" + ID_Nhom_truoc + "' and Toa_Gia = " + gia + "");
            for (int i = 0; i < dt_chitiet.Length; i++)
            {
                dt_chitiet[i]["Toa_NhomHang_ID"] = ID_Nhom;
                dt_chitiet[i]["Toa_NhomHang_ID_an"] = ID_Nhom;
                dt_chitiet[i]["Toa_NhomHang_Ten"] = TenNhom;
                dt_chitiet[i]["HHSL"] = HHSL;
                dt_chitiet[i]["HHPC"] = HHPC;
                
                if (HHSL == 0)
                {
                    dt_chitiet[i]["TienHoaHong"] = (double.Parse(dt_chitiet[i]["Toa_SL"].ToString())*float.Parse(dt_chitiet[i]["Toa_Gia"].ToString())*HHPC)/100;
                }
                else
                {
                    dt_chitiet[i]["TienHoaHong"] = double.Parse(dt_chitiet[i]["Toa_SL"].ToString()) * HHSL;
                }
                Toa_Class.Table_Data.Tables[0].AcceptChanges();

            }
            //cap nhat lai group gia moi
            #region
            DataRow[] drmoiupdate = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom + "' and Gia = " + gia + "");
            if (drmoiupdate.Length == 1)
            {
                DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + gia + "";
                double sl;
                object Osl = dtv.ToTable().Compute("Sum(Toa_SL)", "");
                try { sl = (double)Osl; }
                catch { sl = 0; }
                drmoiupdate[0]["Sum_SL"] = sl;
                double thanhtien = sl * gia;
                drmoiupdate[0]["Sum_ThanhTien"] = thanhtien;
                if (HHSL == 0)
                {
                    drmoiupdate[0]["Sum_TienHoaHong"] = (thanhtien*HHPC)/100;
                }
                else
                {
                    drmoiupdate[0]["Sum_TienHoaHong"] = sl * HHSL;
                }
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            else
            {
                //cap nhat 1 dong cong lai remove het
                DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + gia + "";
                double sl;
                object Osl = dtv.ToTable().Compute("Sum(Toa_SL)", "");
                try { sl = (double)Osl; }
                catch { sl = 0; }
                drmoiupdate[0]["Sum_SL"] = sl;
                double thanhtien = sl * gia;
                drmoiupdate[0]["Sum_ThanhTien"] = thanhtien;
                if (HHSL == 0)
                {
                    drmoiupdate[0]["Sum_TienHoaHong"] = (thanhtien * HHPC) / 100;
                }
                else
                {
                    drmoiupdate[0]["Sum_TienHoaHong"] = sl * HHSL;
                }
                Toa_Class.Table_Data.Tables[1].AcceptChanges();

                //xoa cac dong con la
                Toa_Class.Table_Data.Tables[1].Rows.Remove(drmoiupdate[1]);
                Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            #endregion
            //cap nhat group gia cu
            #region
            DataRow[] drcu = Toa_Class.Table_Data.Tables[1].Select("Toa_NhomHang_ID = '" + ID_Nhom_truoc + "' and Gia = " + gia + "");
            if (drcu.Length > 0)
            {
                DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
                dtv.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "' and Toa_Gia = " + gia + "'";
                double sl;
                object Osl = dtv.ToTable().Compute("Sum(Toa_SL)", "");
                try { sl = (double)Osl; }
                catch { sl = 0; }
                drcu[0]["Sum_SL"] = sl;
                double thanhtien = sl * gia;
                drcu[0]["Sum_ThanhTien"] = thanhtien;
                if (float.Parse(drcu[0]["MHHSL"].ToString()) == 0)
                {
                    drcu[0]["Sum_TienHoaHong"] = (thanhtien * float.Parse(drcu[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    drcu[0]["Sum_TienHoaHong"] = sl * double.Parse(drcu[0]["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
            }
            else
            {
                //khong can lam ji
            }
            #endregion
            //cap nhat group ca
            //cap nhat cu
            #region
            DataRow[] dr3 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '"+ID_Nhom_truoc+"'");
            DataRow dr3_add = Toa_Class.Table_Data.Tables[2].NewRow();
            for (int i = 0; i < Toa_Class.Table_Data.Tables[2].Columns.Count; i++)
            {
                dr3_add[Toa_Class.Table_Data.Tables[2].Columns[i].ColumnName.ToString()] = dr3[0][Toa_Class.Table_Data.Tables[2].Columns[i].ColumnName.ToString()];
            }
            DataView dtv3 = new DataView(Toa_Class.Table_Data.Tables[1]);
            dtv3.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom_truoc + "'";
            object osl3 = dtv3.ToTable().Compute("Sum(Sum_SL)", "");
            double sl3;
            try { sl3 = (double)osl3; }
            catch { sl3 = 0; }

            object ott3 = dtv3.ToTable().Compute("Sum(Sum_ThanhTien)", "");
            double tt3;
            try { tt3 = (double)ott3; }
            catch { tt3 = 0; }
            if (sl3 == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr3[0]);
                Toa_Class.Table_Data.Tables[2].AcceptChanges();
            }
            else
            {
                //co anh huong ji den hh va doanh so,lwch
                dr3[0]["Sum_SL"] = sl3;
                dr3[0]["Sum_ThanhTien"] = tt3;
                dr3[0]["Lech"] = double.Parse(dr3[0]["Sum_ThanhTien_Goc"].ToString()) - tt3;
                dr3[0]["Gia"] = tt3 / sl3;
                if (float.Parse(dr3[0]["MHHSL"].ToString()) == 0)
                {
                    dr3[0]["Sum_TienHoaHong"] = (tt3*float.Parse(dr3[0]["MHHPC"].ToString()))/100;
                }
                else
                {
                    dr3[0]["Sum_TienHoaHong"] = sl3 * double.Parse(dr3[0]["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[2].AcceptChanges();
            }
#endregion
            //cap nhat moi
            #region
            DataRow[] __dr3 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
            if (__dr3.Length > 0)
            {
                #region
                DataView _dtv3 = new DataView(Toa_Class.Table_Data.Tables[1]);
                _dtv3.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "'";
                object _osl3 = _dtv3.ToTable().Compute("Sum(Sum_SL)", "");
                double _sl3;
                try { _sl3 = (double)_osl3; }
                catch { _sl3 = 0; }

                object _ott3 = _dtv3.ToTable().Compute("Sum(Sum_ThanhTien)", "");
                double _tt3;
                try { _tt3 = (double)_ott3; }
                catch { _tt3 = 0; }
                if (_sl3 == 0)
                {
                    Toa_Class.Table_Data.Tables[2].Rows.Remove(__dr3[0]);
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                else
                {
                    //co anh huong ji den hh va doanh so,lwch
                    __dr3[0]["Sum_SL"] = _sl3;
                    __dr3[0]["Sum_ThanhTien"] = _tt3;
                    __dr3[0]["Lech"] = double.Parse(__dr3[0]["Sum_ThanhTien_Goc"].ToString()) - _tt3;
                    __dr3[0]["Gia"] = _tt3 / _sl3;
                    if (float.Parse(__dr3[0]["MHHSL"].ToString()) == 0)
                    {
                        __dr3[0]["Sum_TienHoaHong"] = (_tt3 * float.Parse(__dr3[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        __dr3[0]["Sum_TienHoaHong"] = _sl3 * double.Parse(__dr3[0]["MHHSL"].ToString());
                    }
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                #endregion
            }
            else
            {
                //them moi
                dr3_add["Toa_NhomHang_ID"] = ID_Nhom;
                dr3_add["Toa_NhomHang_ID_an"] = ID_Nhom;
                dr3_add["Toa_NhomHang_Ten"] = TenNhom;
                #region
                DataView _dtv3 = new DataView(Toa_Class.Table_Data.Tables[1]);
                _dtv3.RowFilter = "Toa_NhomHang_ID = '" + ID_Nhom + "'";
                object _osl3 = _dtv3.ToTable().Compute("Sum(Sum_SL)", "");
                double _sl3;
                try { _sl3 = (double)_osl3; }
                catch { _sl3 = 0; }

                object _ott3 = _dtv3.ToTable().Compute("Sum(Sum_ThanhTien)", "");
                double _tt3;
                try { _tt3 = (double)_ott3; }
                catch { _tt3 = 0; }
                if (_sl3 == 0)
                {
                    Toa_Class.Table_Data.Tables[2].Rows.Remove(dr3_add);
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                else
                {
                    //co anh huong ji den hh va doanh so,lwch
                    dr3_add["Sum_SL"] = _sl3;
                    dr3_add["Sum_ThanhTien"] = _tt3;
                    dr3_add["Lech"] = 0 - _tt3;
                    dr3_add["Gia"] = _tt3 / _sl3;
                    if (float.Parse(dr3_add["MHHSL"].ToString()) == 0)
                    {
                        dr3_add["Sum_TienHoaHong"] = (_tt3 * float.Parse(dr3_add["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        dr3_add["Sum_TienHoaHong"] = _sl3 * double.Parse(dr3_add["MHHSL"].ToString());
                    }
                    Toa_Class.Table_Data.Tables[2].Rows.Add(dr3_add);
                    Toa_Class.Table_Data.Tables[2].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                    Toa_Class.Table_Data.Tables[0].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Toa_Gia DESC";
                    Toa_Class.Table_Data.Tables[2].AcceptChanges();
                }
                #endregion
            }
            #endregion
            #endregion
        }
        public void capnhat_ca_nhomca()
        {
            #region
            #endregion
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          THEM MOI DL                -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        #region them DL
        DL_Them _DL_Them;

        public void them_dl()
        {
            #region
            if (NCC_ID != null && NCC_ID!="")
            {
                main_tab.SelectedTabPageIndex = 1;
                _DL_Them = new DL_Them();
               
               _DL_Them.TextChanged+=new EventHandler(_DL_Them_TextChanged);
                _DL_Them.ShowDialog();
            }
            #endregion
        }

        void _DL_Them_TextChanged(object sender, EventArgs e)
        {
            add_them(Toa_Class.STT_Them, Toa_Class.SL_Them, Toa_Class.Gia_Them, Toa_Class.Nhom_ID_Them, Toa_Class.Nhom_Ten_Them);
            show_data();
        }
        public void add_them(int STT,float SL,double Gia,string Nhom_ID,string Nhom_Ten)
        {
            #region
            DataRow dr_new = Toa_Class.Table_Data.Tables[0].NewRow();
            dr_new["ID"] = Guid.NewGuid().ToString().ToUpper();
            dr_new["ID_Auto"] = STT;
            dr_new["Ngay"] = Ngay_Toa;
            dr_new["NguonGoc"] = Toa_Class.tenmaytinh.ToString();
            dr_new["Toa_SL"] = SL;
            dr_new["Toa_Gia"] = Gia;
            dr_new["Toa_Gia_an"] = Gia;
            dr_new["ThanhTien"] = SL*Gia;
            dr_new["Toa_NhomHang_ID"] = Nhom_ID;
            dr_new["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr_new["Toa_NhomHang_Ten"] = Nhom_Ten;
            dr_new["ID_NCC"] = NCC_ID;
            dr_new["Tbl_NCC_Name"] = Look_tenlai.Text;
            //tinh hoa hong
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + Nhom_ID + "'");
            if (dr.Length > 0)
            {
                dr_new["HHPC"] = dr[0]["MHHPC"];
                dr_new["HHSL"] = dr[0]["MHHSL"];
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    dr_new["TienHoaHong"] = (SL * Gia * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    dr_new["TienHoaHong"] = SL * double.Parse(dr_new["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[0].Rows.Add(dr_new);
                capnhat_Group_Gia(Nhom_ID,Nhom_Ten, Gia);
                capnhat_Group_Ca(Nhom_ID);
            }
            else
            {
                dr_new["HHPC"] = 0;
                dr_new["HHSL"] = 0;
                dr_new["TienHoaHong"] = 0;
                Toa_Class.Table_Data.Tables[0].Rows.Add(dr_new);
                them_dong_vao_Table1(SL, Gia, Nhom_ID, Nhom_Ten);
                them_dong_vao_Table2(SL, Gia, Nhom_ID, Nhom_Ten);
            }
            #endregion
        }
        public void capnhat_Group_Gia(string ID_Nhom,string Ten_Nhom, double gia)
        {           
            #region
           
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_Gia = " + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            // msgbox.dialog_OK_error(SLTinh.ToString());
            if (SLTinh == 0)
            {
                //Toa_Class.Table_Data.Tables[1].Rows.Remove(dr[0]);
            }
            else
            {
                DataRow[] dr = Toa_Class.Table_Data.Tables[1].Select("Gia=" + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
                if (dr.Length > 0)
                {
                    dr[0]["Sum_SL"] = SLTinh;
                    double thanhtien = SLTinh * gia;
                    dr[0]["Sum_ThanhTien"] = thanhtien;
                    double tienhh;
                    if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                    {
                        tienhh = (thanhtien * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                    }
                    dr[0]["Sum_TienHoaHong"] = tienhh;
                }
                else
                {
                    DataRow dr2 = Toa_Class.Table_Data.Tables[1].NewRow();

                    #region
                    dr2["Gia"] = gia;
                    dr2["Gia_an"] = gia;
                    dr2["Sum_SL"] = SL;
                    dr2["Sum_ThanhTien"] = SLTinh * gia;
                 
                    dr2["Sum_TienHoaHong"] = 0;
                    dr2["Toa_NhomHang_ID"] = ID_Nhom;
                    dr2["Toa_NhomHang_ID_an"] = ID_Nhom;
                    dr2["Toa_NhomHang_Ten"] = Ten_Nhom;
                    DataRow[] dr66 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
                    if (dr66.Length > 0)
                     {
                         dr2["MHHSL"] = dr66[0]["MHHSL"];
                         dr2["MHHPC"] = dr66[0]["MHHPC"];
                         dr2["Sum_TienHoaHong"] = (double.Parse(dr66[0]["MHHPC"].ToString())*SLTinh*gia )/100;
                         
                     }
                     else
                     {
                         dr2["MHHSL"] = 0;
                         dr2["MHHPC"] = 0;
                         dr2["Sum_TienHoaHong"] = 0;
                     }
                    Toa_Class.Table_Data.Tables[1].Rows.Add(dr2);
                    Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                    Toa_Class.Table_Data.Tables[1].AcceptChanges();
                    #endregion
                }
            }


            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion          
        }
        public void capnhat_Group_Ca(string ID_Nhom)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr[0]);
            }
            else
            {
                dr[0]["Sum_SL"] = SLTinh;
                //cap nhat TT
                object TT = dtv.ToTable().Compute("Sum(ThanhTien)", "");
                double TTTinh;
                try { TTTinh = (double)TT; }
                catch { TTTinh = 0; }
                dr[0]["Sum_ThanhTien"] = TTTinh;
                dr[0]["Lech"] = double.Parse(dr[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", "")) - TTTinh;
                //cap nhat gia binh quan
                dr[0]["Gia"] = TTTinh / SLTinh;
                double tienhh;
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (TTTinh * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
            }
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }
        public void them_dong_vao_Table1(float SL, double Gia, string Nhom_ID, string Nhom_Ten)
        {
            DataRow dr2 = Toa_Class.Table_Data.Tables[1].NewRow();

            #region
            dr2["Gia"] = Gia;
            dr2["Gia_an"] = Gia;
            dr2["Sum_SL"] = SL;
            dr2["Sum_ThanhTien"] = SL * Gia;
            dr2["MHHSL"] = 0;
            dr2["MHHPC"] = 0;
            dr2["Sum_TienHoaHong"] = 0;
            dr2["Toa_NhomHang_ID"] = Nhom_ID;
            dr2["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr2["Toa_NhomHang_Ten"] = Nhom_Ten;
            Toa_Class.Table_Data.Tables[1].Rows.Add(dr2);
            Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion
        }
        public void them_dong_vao_Table2(float SL, double Gia, string Nhom_ID, string Nhom_Ten)
        {
            DataRow dr3 = Toa_Class.Table_Data.Tables[2].NewRow();
            #region
            dr3["Gia"] = Gia;

            dr3["Sum_SL"] = SL;
            dr3["Sum_ThanhTien"] = SL * Gia;
            dr3["Sum_ThanhTien_Goc"] = 0;
            dr3["MHHSL"] = 0;
            dr3["MHHPC"] = 0;
            dr3["Sum_TienHoaHong"] = 0;
            dr3["Toa_NhomHang_ID"] = Nhom_ID;
            dr3["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr3["Toa_NhomHang_Ten"] = Nhom_Ten;
            dr3["LEch"] = 0 - (SL * Gia);
            Toa_Class.Table_Data.Tables[2].Rows.Add(dr3);
            Toa_Class.Table_Data.Tables[2].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }

       
        void btn_new_Click(object sender, EventArgs e)
        {
            them_dl();
        }

        #endregion

        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------         NOI DU LIEU TU LAI KHAC     -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        #region Dieu kien
        DieuKien_Form _DieuKien_Form;
        void btn_gopdl_Click(object sender, EventArgs e)
        {
            dieu_kien();
        }
        public void dieu_kien()
        {
                _DieuKien_Form = new DieuKien_Form();
                _DieuKien_Form.FormClosed += new FormClosedEventHandler(_DieuKien_Form_FormClosed);
                _DieuKien_Form.ShowDialog();      
        }

        void _DieuKien_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_DieuKien_Form.DialogResult == DialogResult.OK)
            {
                them_dl_tugop();
                //tinh lai SL_Goc goc
                object SLG_Tem = Toa_Class.Table_Data_gop.Tables[0].Compute("Sum(Toa_SL)", "");
                double sl_tem;
                try { sl_tem = (double)SLG_Tem; }
                catch { sl_tem = 0; };
                SL_Goc +=sl_tem;
                //tinh lai ds goc
                 object TTG_Tem = Toa_Class.Table_Data_gop.Tables[0].Compute("Sum(ThanhTien)", "");
                double tt_tem;
                try { tt_tem = (double)TTG_Tem; }
                catch { tt_tem = 0; };
                DS_Goc +=tt_tem;
                show_data();
            }
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          THEM MOI DL TU FORM GOP              -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        #region them DL tu gop
       

        public void them_dl_tugop()
        {
            #region
            for (int i = 0; i < Toa_Class.Table_Data_gop.Tables[0].Rows.Count; i++)
            {
                add_them_tugop(Toa_Class.Table_Data_gop.Tables[0].Rows[i]["ID"].ToString()
                    , int.Parse(Toa_Class.Table_Data_gop.Tables[0].Rows[i]["ID_Auto"].ToString())
                    , float.Parse(Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Toa_SL"].ToString())
                    , double.Parse(Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Toa_Gia"].ToString())
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Toa_NhomHang_ID"].ToString()
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Toa_NhomHang_Ten"].ToString()
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["NguonGoc"].ToString()
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["ID_NCC"].ToString()
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Tbl_NCC_Name"].ToString()
                    , Toa_Class.Table_Data_gop.Tables[0].Rows[i]["Ngay"].ToString());
            }
            #endregion
        }

        public void add_them_tugop(string id,int STT, float SL, double Gia, string Nhom_ID, string Nhom_Ten,string nguon,string ncc_id,string ncc_ten,string ngay)
        {
            #region
            DataRow dr_new = Toa_Class.Table_Data.Tables[0].NewRow();
            dr_new["ID"] = id;
            dr_new["ID_Auto"] = STT;
            dr_new["Ngay"] = ngay;
            dr_new["NguonGoc"] = nguon;
            dr_new["Toa_SL"] = SL;
            dr_new["Toa_Gia"] = Gia;
            dr_new["Toa_Gia_an"] = Gia;
            dr_new["ThanhTien"] = SL * Gia;
            dr_new["Toa_NhomHang_ID"] = Nhom_ID;
            dr_new["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr_new["Toa_NhomHang_Ten"] = Nhom_Ten;
            dr_new["ID_NCC"] = ncc_id;
            dr_new["Tbl_NCC_Name"] = ncc_ten;
            //tinh hoa hong
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + Nhom_ID + "'");
            if (dr.Length > 0)
            {
                dr_new["HHPC"] = dr[0]["MHHPC"];
                dr_new["HHSL"] = dr[0]["MHHSL"];
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    dr_new["TienHoaHong"] = (SL * Gia * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    dr_new["TienHoaHong"] = SL * double.Parse(dr_new["MHHSL"].ToString());
                }
                Toa_Class.Table_Data.Tables[0].Rows.Add(dr_new);
                capnhat_Group_Gia_gop(Nhom_ID, Nhom_Ten, Gia);
                capnhat_Group_Ca_gop(Nhom_ID, SL * Gia);
            }
            else
            {
                dr_new["HHPC"] = 0;
                dr_new["HHSL"] = 0;
                dr_new["TienHoaHong"] = 0;
                Toa_Class.Table_Data.Tables[0].Rows.Add(dr_new);
                them_dong_vao_Table1_gop(SL, Gia, Nhom_ID, Nhom_Ten);
                them_dong_vao_Table2_gop(SL, Gia, Nhom_ID, Nhom_Ten);
            }
            #endregion
        }
        public void capnhat_Group_Gia_gop(string ID_Nhom, string Ten_Nhom, double gia)
        {
            #region

            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_Gia = " + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            // msgbox.dialog_OK_error(SLTinh.ToString());
            if (SLTinh == 0)
            {
                //Toa_Class.Table_Data.Tables[1].Rows.Remove(dr[0]);
            }
            else
            {
                DataRow[] dr = Toa_Class.Table_Data.Tables[1].Select("Gia=" + gia + " and Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
                if (dr.Length > 0)
                {
                    dr[0]["Sum_SL"] = SLTinh;
                    double thanhtien = SLTinh * gia;
                    dr[0]["Sum_ThanhTien"] = thanhtien;
                    double tienhh;
                    if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                    {
                        tienhh = (thanhtien * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                    }
                    else
                    {
                        tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                    }
                    dr[0]["Sum_TienHoaHong"] = tienhh;
                }
                else
                {
                    DataRow dr2 = Toa_Class.Table_Data.Tables[1].NewRow();

                    #region
                    dr2["Gia"] = gia;
                    dr2["Gia_an"] = gia;
                    dr2["Sum_SL"] = SL;
                    dr2["Sum_ThanhTien"] = SLTinh * gia;

                    dr2["Sum_TienHoaHong"] = 0;
                    dr2["Toa_NhomHang_ID"] = ID_Nhom;
                    dr2["Toa_NhomHang_ID_an"] = ID_Nhom;
                    dr2["Toa_NhomHang_Ten"] = Ten_Nhom;
                    DataRow[] dr66 = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID = '" + ID_Nhom + "'");
                    if (dr66.Length > 0)
                    {
                        dr2["MHHSL"] = dr66[0]["MHHSL"];
                        dr2["MHHPC"] = dr66[0]["MHHPC"];
                        dr2["Sum_TienHoaHong"] = (double.Parse(dr66[0]["MHHPC"].ToString()) * SLTinh * gia) / 100;

                    }
                    else
                    {
                        dr2["MHHSL"] = 0;
                        dr2["MHHPC"] = 0;
                        dr2["Sum_TienHoaHong"] = 0;
                    }
                    Toa_Class.Table_Data.Tables[1].Rows.Add(dr2);
                    Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                    Toa_Class.Table_Data.Tables[1].AcceptChanges();
                    #endregion
                }
            }


            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion
        }
        public void capnhat_Group_Ca_gop(string ID_Nhom,double tiengocdangco)
        {
            #region
            DataRow[] dr = Toa_Class.Table_Data.Tables[2].Select("Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'");
            DataView dtv = new DataView(Toa_Class.Table_Data.Tables[0]);
            dtv.RowFilter = "Toa_NhomHang_ID='" + ID_Nhom.ToUpper() + "'";
            object SL = dtv.ToTable().Compute("Sum(Toa_SL)", "");
            double SLTinh;
            try { SLTinh = (double)SL; }
            catch { SLTinh = 0; }
            if (SLTinh == 0)
            {
                Toa_Class.Table_Data.Tables[2].Rows.Remove(dr[0]);
            }
            else
            {
                dr[0]["Sum_SL"] = SLTinh;
                //cap nhat TT
                object TT = dtv.ToTable().Compute("Sum(ThanhTien)", "");
                double TTTinh;
                try { TTTinh = (double)TT; }
                catch { TTTinh = 0; }
                dr[0]["Sum_ThanhTien"] = TTTinh;
                dr[0]["Sum_ThanhTien_Goc"] =double.Parse(dr[0]["Sum_ThanhTien_Goc"].ToString())+ tiengocdangco;
                dr[0]["Lech"] = double.Parse(dr[0]["Sum_ThanhTien_Goc"].ToString().Replace(",", ""))  - TTTinh;
                //cap nhat gia binh quan
                dr[0]["Gia"] = TTTinh / SLTinh;
                double tienhh;
                if (float.Parse(dr[0]["MHHSL"].ToString()) == 0)
                {
                    tienhh = (TTTinh * float.Parse(dr[0]["MHHPC"].ToString())) / 100;
                }
                else
                {
                    tienhh = SLTinh * double.Parse(dr[0]["MHHSL"].ToString());
                }
                dr[0]["Sum_TienHoaHong"] = tienhh;
            }
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }
        public void them_dong_vao_Table1_gop(float SL, double Gia, string Nhom_ID, string Nhom_Ten)
        {
            DataRow dr2 = Toa_Class.Table_Data.Tables[1].NewRow();

            #region
            dr2["Gia"] = Gia;
            dr2["Gia_an"] = Gia;
            dr2["Sum_SL"] = SL;
            dr2["Sum_ThanhTien"] = SL * Gia;
            dr2["MHHSL"] = 0;
            dr2["MHHPC"] = 0;
            dr2["Sum_TienHoaHong"] = 0;
            dr2["Toa_NhomHang_ID"] = Nhom_ID;
            dr2["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr2["Toa_NhomHang_Ten"] = Nhom_Ten;
            Toa_Class.Table_Data.Tables[1].Rows.Add(dr2);
            Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
            Toa_Class.Table_Data.Tables[1].AcceptChanges();
            #endregion
        }
        public void them_dong_vao_Table2_gop(float SL, double Gia, string Nhom_ID, string Nhom_Ten)
        {
            DataRow dr3 = Toa_Class.Table_Data.Tables[2].NewRow();
            #region
            dr3["Gia"] = Gia;

            dr3["Sum_SL"] = SL;
            dr3["Sum_ThanhTien"] = SL * Gia;
            dr3["Sum_ThanhTien_Goc"] = SL * Gia;
            dr3["MHHSL"] = 0;
            dr3["MHHPC"] = 0;
            dr3["Sum_TienHoaHong"] = 0;
            dr3["Toa_NhomHang_ID"] = Nhom_ID;
            dr3["Toa_NhomHang_ID_an"] = Nhom_ID;
            dr3["Toa_NhomHang_Ten"] = Nhom_Ten;
            dr3["Lech"] = 0;
            Toa_Class.Table_Data.Tables[2].Rows.Add(dr3);
            Toa_Class.Table_Data.Tables[2].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
            Toa_Class.Table_Data.Tables[2].AcceptChanges();
            #endregion
        }


    
        #endregion
        #endregion


        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          LUU DL                -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        #region luu toa
        void btn_save_Click(object sender, EventArgs e)
        {
            save();
        }
        public void save()
        {


            if (msgbox.dialog_yes_no("BẠN CÓ MUỐN LƯU TOA KHÔNG ?") == DialogResult.Yes)
            {
                save_chitiet();
                savechiphi();
                update_id_dongxoa();
                daluu = true;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                daluu = true;

            }
                
                
           
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        public bool daluu = false;
        public void save_khidong()
        {
           
                if (msgbox.dialog_yes_no("Bạn muốn lưu không?") == DialogResult.Yes)
                {
                    save_chitiet();
                    savechiphi();
                    update_id_dongxoa();
                    daluu = true;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    daluu = true;

                }
            
        }
        public void save_chitiet()
        {
            if (User_Case == "New")
            {
                ID_Toa = Guid.NewGuid().ToString();
            }
            #region
            DataRow[] dr_si = Toa_Class.Table_Data.Tables[0].Select("NguonGoc = 'Nhap Si'");
            DataRow[] dr_le = Toa_Class.Table_Data.Tables[0].Select("NguonGoc = 'Nhap Le'");
            DataRow[] dr_khac = Toa_Class.Table_Data.Tables[0].Select("NguonGoc <> 'Nhap Si' and NguonGoc <> 'Nhap Le'");
            for (int i = 0; i < dr_si.Length; i++)
            {
                string sql = "Update Tbl_GIAODICH set Toa_ID = '"+ID_Toa+"'";
                sql += ",_ID1 ='" + dr_si[i]["Toa_NhomHang_ID"].ToString().Replace(",", "") + "'";
                sql += ",Toa_SL = " +dr_si[i]["Toa_SL"].ToString().Replace(",","") + "";
                sql += ",Toa_Gia = " + dr_si[i]["Toa_Gia"].ToString().Replace(",", "") + "";
                sql += ",Toa_HHSL = " + dr_si[i]["HHSL"].ToString().Replace(",", "") + "";
                sql += ",Toa_HHPC = " + dr_si[i]["HHPC"].ToString().Replace(",", "") + "";
                sql += " where ID = '" + dr_si[i]["ID"].ToString().ToUpper() + "'";
                db.T_exc(sql);
            }
            for (int i = 0; i < dr_le.Length; i++)
            {
                string sql = "Update Tbl_GIAODICH_LE set Toa_ID = '" + ID_Toa + "'";
                sql += ",_ID1 = '" + dr_le[i]["Toa_NhomHang_ID"].ToString().Replace(",", "") + "'";
                sql += ",Toa_SL = " + dr_le[i]["Toa_SL"].ToString().Replace(",", "") + "";
                sql += ",Toa_Gia = " + dr_le[i]["Toa_Gia"].ToString().Replace(",", "") + "";
                sql += ",Toa_HHSL = " + dr_le[i]["HHSL"].ToString().Replace(",", "") + "";
                sql += ",Toa_HHPC = " + dr_le[i]["HHPC"].ToString().Replace(",", "") + "";
                sql += " where ID = '" + dr_le[i]["ID"].ToString().ToUpper() + "'";
                db.T_exc(sql);
            }
            for (int i = 0; i < dr_khac.Length; i++)
            {
                string xoa = "delete from Tbl_GIAODICH_Them where ID = '" + dr_khac[i]["ID"].ToString().ToUpper() + "'";
                db.T_exc(xoa);

                //string sql = "Update Tbl_GIAODICH_Them set Toa_ID = '" + dr_khac[i]["ID"].ToString().ToUpper() + "'";
                //sql += ",_ID = " + dr_khac[i]["Toa_NhomHang_ID"].ToString().Replace(",", "") + "";
                //sql += ",Toa_SL = " + dr_khac[i]["Toa_SL"].ToString().Replace(",", "") + "";
                //sql += ",Toa_Gia = " + dr_khac[i]["Toa_Gia"].ToString().Replace(",", "") + "";
                //sql += ",Toa_HHSL = " + dr_khac[i]["Toa_HHSL"].ToString().Replace(",", "") + "";
                //sql += ",Toa_HHPC = " + dr_khac[i]["Toa_HHPC"].ToString().Replace(",", "") + "";
                //sql += " where ID = '" + dr_khac[i]["ID"].ToString().ToUpper() + "'";
               // db.T_exc(sql);
                #region
                //save and add vao muc du lieu da luu
                string sql = "Tbl_GIAODICH_Them_insert ";
                //  @STT
                sql += "N'" + dr_khac[i]["ID_Auto"].ToString() + "',";
                
                //,@ID
                sql += "N'" + dr_khac[i]["ID"].ToString().ToUpper() + "',";
                //,@ID_Hang
                sql += "N'" + dr_khac[i]["Toa_NhomHang_ID"].ToString().ToUpper() + "',";
                //,@ID_KhachHang
                sql += "Null,";
                //,@ID_NCC
                sql += "N'" + dr_khac[i]["ID_NCC"].ToString().ToUpper() + "',";
                //,@Date_Num_Final_UpDate
                sql += "N'" + Ngay_ToaNum + "',";
                //,@User_ID_Final_UpDate
                sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                //,@IS_User
                sql += "N'1',";
                //,@NguonGoc
                sql += "N'" + dr_khac[i]["NguonGoc"].ToString().ToUpper() + "',";
                //,@SL
                sql += "0,";

                //,@Gia_0
                sql += "N'0',";
                //,@Gia_1
                sql += "0,";

                //,@Gia_2
                sql += "N'0',";
                //,@Sot
                sql += "0,";
                //,@Ket
                sql += "0,";
                //,@TienMat
                sql += "N'0',";
                //,@Toa_ID               
                    sql += "N'" +ID_Toa + "',";
                
                //,@Toa_SL
                    sql += "N'" + dr_khac[i]["Toa_SL"].ToString() + "',";
                //,@Toa_Gia
                    sql += "N'" + dr_khac[i]["Toa_Gia"].ToString() + "',";
                //,@Toa_HHSL
                    sql += "N'" + dr_khac[i]["HHSL"].ToString() + "',";
                //,@Toa_HHPC
                sql += "N'" + dr_khac[i]["HHPC"].ToString().Replace(',','.') + "',";
                //,@Toa_Xoa
                sql += "Null,";
                //,@_Text1
                sql += "Null,";
                //,@_Text2
                sql += "Null,";
                //,@_Text3
                sql += "Null,";
                //,@_Text4
                sql += "Null,";
                //,@_Text5
                sql += "Null,";
                //,@_Num1
                sql += "0,";
                //,@_Num2
                sql += "0,";
                //,@_Num3
                sql += "0,";
                //,@_ID1
                sql += "N'" + dr_khac[i]["Toa_NhomHang_ID"].ToString() + "',";
                //,@Lu
                sql += "0,";
                //,@Cat
                sql += "0,";
                //,@Bom
                sql += "0";
                db.T_exc(sql);
                #endregion
            }
            #endregion
        }
        public void savechiphi()
        {
            #region
            
            //create PROCEDURE TOA_insert
            string sql = "TOA_insert ";
            //@ID uniqueidentifier ,
            sql += "'" + ID_Toa+ "',";
            //@SOTOA int ,
            sql += "'" + SoToa + "',";
            //@KYTU NVARCHAR(10),
            sql += "'" + KyTu_Toa + "',";
            //@NGAYLAMTOA int ,
            sql += "'" + Ngay_ToaNum + "',";
            //@FROM_N int ,
            sql += "'" + NgayDenNum + "',";
            //@TO_N int ,
            sql += "'" + NgayTuNum + "',";
            //@TINHTRANG nvarchar(50) ,
            if (checkEdit1.Checked == true)
            {
                sql += "N'Hoàn tất',";
            }
            else
            {
                sql += "N'Chưa hoàn tất',";
            }

            //@NCC_ID uniqueidentifier ,
            sql += "'" + NCC_ID+ "',";
            //@TKHIEN decimal ,
            sql += "'" + TKHIEN + "',";
            //@TIEN decimal ,
            sql += "'" + 0 + "',";
            //@TNUOC decimal ,
            sql += "'" + TNUOC + "',";
            //@TUNG decimal ,
            sql += "'" + TUNG + "',";
            //@TXE decimal ,
            sql += "'" + TXE + "',";
            //@TKY decimal ,
            sql += "'" + TKY + "',";
            //@TEN_THEM1 nvarchar(50) ,
            sql += "N'" + txt_tenk1.Text.ToString() + "',";
            //@TEN_THEM2 nvarchar(50) ,
            sql += "N'" + txt_tenk2.Text.ToString() + "',";
            //@TEN_THEM3 nvarchar(50) ,
            sql += "N'" + txt_tenk3.Text.ToString() + "',";
            //@TTHEM1 decimal ,
            sql += "'" + TTHEM1 + "',";
            //@TTHEM2 decimal ,
            sql += "'" + TTHEM2 + "',";
            //@TTHEM3 decimal ,
            sql += "'" + TTHEM3+ "',";
            //@TTHANG decimal ,
            sql += "'" + DS_Toa + "',";
            sql += "'" + T_HH + "',";
            //@C1 nvarchar(50) ,
            sql += "N'" + x1.Text.ToString() + "',";
            //@C2 nvarchar(50) ,
            sql += "N'" + x2.Text.ToString() + "',";
            //@C3 nvarchar(50) ,
            sql += "N'" + x3.Text.ToString() + "',";
            //@C4 nvarchar(50) ,
            sql += "Null,";

            //@C5 nvarchar(50) 
            sql += "N'"+txt_ghichu.Text+"'";
            db.T_exc(sql);
            // add node
            if (User_Case == "New")
            {
                try
                {
                    save_XML();
                }
                catch
                {
                }
            }
            
            #endregion
        }

        public void update_id_dongxoa()
        {
            for (int i = 0; i < Toa_Class.Table_deleted.Rows.Count; i++)
            {
                db.T_exc("UPDATE Tbl_GIAODICH SET Toa_SL = 0,Toa_Gia=0 where ID = '" + Toa_Class.Table_deleted.Rows[i]["ID"].ToString().ToUpper() + "'");
                db.T_exc("UPDATE Tbl_GIAODICH_LE SET Toa_SL = 0,Toa_Gia=0 where ID = '" + Toa_Class.Table_deleted.Rows[i]["ID"].ToString().ToUpper() + "'");
                db.T_exc("UPDATE Tbl_GIAODICH_Them SET Toa_SL = 0,Toa_Gia=0 where ID = '" + Toa_Class.Table_deleted.Rows[i]["ID"].ToString().ToUpper() + "'");
            }
        }
        Xml_control _Xml_control = new Xml_control();
        public void save_XML()
        {
            #region
            _Xml_control.delete_xml_node(Toa_Class._xmlfile, ID_Toa);
            #endregion
            //them lai
            #region
            XmlDocument doc123 = new XmlDocument();
            doc123.Load(Toa_Class._xmlfile);
            XmlNode root123 = doc123.DocumentElement;
            //Create a new node.
            XmlElement elem123 = doc123.CreateElement("Table");
            //add element
            XmlElement _ID = doc123.CreateElement("ID");


            // add inner text
            _ID.InnerText = ID_Toa;


            // add to element
            elem123.AppendChild(_ID);




            root123.AppendChild(elem123);
            doc123.Save(Toa_Class._xmlfile);

            DuLieu.DS_Toa_XML = _Xml_control.read_xml_toa(Toa_Class._xmlfile);
            _Toa_Class.load_xml_Toa();
            #endregion
        }
        #endregion


        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          IN TOA                -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        #region ham progess in
        chang _chang = new chang();
        public void progess_print(string ID_TOA)
        {
            #region
            //   msg.dialog_OK_info("in");
            if (Pow.kiemtra("Toa_QuanLyDuLieu", "Inan") == true)
            {
                #region
                //gan bien 
                T_TL.NCC_Name = NCC_Name;
                T_TL.SoToa = SoToa;

                T_TL.KyTu_Toa = KyTu_Toa;
                T_TL.Ngay_Toa = Ngay_Toa;
              
                T_TL.TKHIEN = TKHIEN;
                T_TL.TNUOC = TNUOC;
                T_TL.TUNG = TUNG;
                T_TL.TXE = TXE;
                T_TL.TKY = TKY;
                T_TL.T_HH = T_HH;
                T_TL.TTHEM1 = TTHEM1;
                T_TL.TTHEM2 = TTHEM2;
                T_TL.TTHEM3 = TTHEM3;

                T_TL.TEN_THEM1 = txt_tenk1.Text.ToString();
                T_TL.TEN_THEM2 = txt_tenk2.Text.ToString();
                T_TL.TEN_THEM3 = txt_tenk3.Text.ToString();
                T_TL.xe1 = x1.Text.ToString();
                T_TL.xe2 = x2.Text.ToString();
                T_TL.xe3 = x3.Text.ToString();

                T_TL.TPHAITRA = TPHAITRA;
                // public double SL_Goc, SL_Toa, SL_Lech;
                T_TL.Gia_Toa = Gia_Toa;
                // public double DS_Goc, DS_Toa, DS_Lech;
                T_TL._GiaT_cophi = _GiaT_cophi;
                #endregion
                DataTable dsSelectData = new DataTable();
                double pt = T_TL.TPHAITRA / 1000;
                double pt1 = System.Math.Round(pt, 0, MidpointRounding.ToEven);
                double tientra = pt1 * 1000;
                // msgbox.dialog_OK_info(tientra.ToString());
                T_TL.TPHAITRA = tientra;
                DuLieu.tien_chu = "Bằng chữ : " + _chang.ChuyenSo(tientra.ToString().Replace(",", ""));
              //  DuLieu.tien_chu = "Bằng chữ : " + _chang.ChuyenSo(TPHAITRA.ToString().Replace(",", "").Replace(".", ""));
                Toa_Class.Table_Data.Tables[1].DefaultView.Sort = "Toa_NhomHang_Ten ASC, Gia DESC";
                Toa_Class.Table_Data.Tables[1].AcceptChanges();
                dsSelectData = Toa_Class.Table_Data.Tables[1].Copy();
                //dsSelectData.Tables[0].DefaultView.Sort = " criterian";
                string CA = dsSelectData.Rows[0]["Toa_NhomHang_Ten"].ToString();
                string HSL = dsSelectData.Rows[0]["MHHSL"].ToString();
                string HPC = dsSelectData.Rows[0]["MHHPC"].ToString();
                dsSelectData.Columns.Add("HH");
                for (int i = 1; i < dsSelectData.Rows.Count; i++)
                {
                    if (dsSelectData.Rows[i]["Toa_NhomHang_Ten"].ToString() == CA && dsSelectData.Rows[i]["MHHSL"].ToString() == HSL && dsSelectData.Rows[i]["MHHPC"].ToString() == HPC)
                    {
                        dsSelectData.Rows[i]["Toa_NhomHang_Ten"] = "";
                        dsSelectData.AcceptChanges();
                    }
                    else
                    {
                        CA = dsSelectData.Rows[i]["Toa_NhomHang_Ten"].ToString();
                        HPC = dsSelectData.Rows[i]["MHHPC"].ToString();
                        HSL = dsSelectData.Rows[i]["MHHSL"].ToString();
                    }
                   
                }
                for (int i = 0; i < dsSelectData.Rows.Count; i++)
                {
                    if (float.Parse(dsSelectData.Rows[i]["MHHSL"].ToString()) > 0)
                    {
                        dsSelectData.Rows[i]["HH"] = String.Format("{0:#,#}", double.Parse(dsSelectData.Rows[i]["MHHSL"].ToString()))+" vnđ";
                    }
                    else
                    {
                        dsSelectData.Rows[i]["HH"] = String.Format("{0:N1}", double.Parse(dsSelectData.Rows[i]["MHHPC"].ToString())) + " %";
                    }
                }
                dsSelectData.AcceptChanges();
                dsSelectData.Columns.Add("ABC");
                for (int i = 0; i < dsSelectData.Rows.Count; i++)
                {
                    if (dsSelectData.Rows[i]["Toa_NhomHang_Ten"].ToString() != "")
                    {

                        DataView dtv = new DataView(Toa_Class.Table_Data.Tables[2]);
                        dtv.RowFilter = "Toa_NhomHang_Ten = '" + dsSelectData.Rows[i]["Toa_NhomHang_Ten"].ToString() + "' AND MHHSL = " + dsSelectData.Rows[i]["MHHSL"].ToString() + " AND MHHPC = " + dsSelectData.Rows[i]["MHHPC"].ToString() + "";
                        double sl, g;
                        try
                        {
                            sl = double.Parse(dtv[0]["Sum_SL"].ToString());
                        }
                        catch
                        {
                            sl = 0;
                        }
                        try
                        {
                            g = double.Parse(dtv[0]["Gia"].ToString());
                        }
                        catch
                        {
                            g = 0;
                        }

                        dsSelectData.Rows[i]["ABC"] = "  (" + String.Format("{0:N1}", sl) + " x " + String.Format("{0:N0}", g) + ")";
                        dsSelectData.AcceptChanges();
                    }
                }


                //xoa cac dong co sl =0
                DataRow[] dtrx = dsSelectData.Select("Sum_SL=0");
                for (int i = 0; i < dtrx.Length; i++)
                {
                    dtrx[i].Delete();

                }
                dsSelectData.AcceptChanges();
                //
                MessageBoxManager.Yes = "Ngắt Trang";
                MessageBoxManager.No = "Không Ngắt";
                MessageBoxManager.Cancel = "Bỏ qua";
                MessageBoxManager.Register();
                DialogResult da = MessageBox.Show("Chọn Mẫu in? ", "Phần mềm quản lý cá", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (da == DialogResult.Yes)
                {
                    DuLieu.ngattrang = 1;
                }
                if (da == DialogResult.No)
                {
                    DuLieu.ngattrang = 0;
                }
                MessageBoxManager.Unregister();
        
                T_TL rapport1 = new T_TL();
                rapport1.DataSource = dsSelectData;
                rapport1.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport1.ShowPreviewDialog();
            }
            #endregion

        }
        #endregion
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          lenh focus               -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        #region
         void x1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                x2.Focus();
            }
        }
        void x2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                x3.Focus();
            }
        }
        void x3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btn_save.Focus();
            }
        }
        public void enter()
        {
            txt_khien.KeyPress += new KeyPressEventHandler(txt_khien_KeyPress);
            txt_nuocda.KeyPress += new KeyPressEventHandler(txt_nuocda_KeyPress);
            txt_ungtruoc.KeyPress += new KeyPressEventHandler(txt_ungtruoc_KeyPress);
            txt_ungxe.KeyPress += new KeyPressEventHandler(txt_ungxe_KeyPress);
            txt_tienky.KeyPress += new KeyPressEventHandler(txt_tienky_KeyPress);
            txt_k1.KeyPress += new KeyPressEventHandler(txt_k1_KeyPress);
            txt_tenk1.KeyPress += new KeyPressEventHandler(txt_tenk1_KeyPress);
            txt_k2.KeyPress += new KeyPressEventHandler(txt_k2_KeyPress);
            txt_tenk2.KeyPress += new KeyPressEventHandler(txt_tenk2_KeyPress);
            txt_k3.KeyPress += new KeyPressEventHandler(txt_k3_KeyPress);
            txt_tenk3.KeyPress += new KeyPressEventHandler(txt_tenk3_KeyPress);
            x1.KeyPress += new KeyPressEventHandler(x1_KeyPress);
            x2.KeyPress += new KeyPressEventHandler(x2_KeyPress);
            x3.KeyPress += new KeyPressEventHandler(x3_KeyPress);
        }

       

        void txt_ungxe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_tienky.Focus();
            }
        }

        void txt_k1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_tenk1.Focus();
            }
        }

        void txt_tenk3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                x1.Focus();
            }
        }

        void txt_k3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_tenk3.Focus();
            }
        }

        void txt_tenk2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_k3.Focus();
            }
        }

        void txt_k2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_tenk2.Focus();
            }
        }

        void txt_tenk1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_k2.Focus();
            }
        }

        void txt_tienky_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_k1.Focus();
            }
        }

        void txt_ungtruoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_ungxe.Focus();
            }
        }

        void txt_nuocda_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_ungtruoc.Focus();
            }
        }

        void txt_khien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_nuocda.Focus();
            }
        }

        
        #endregion
    }
}
