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 ABC.Report;
using System.IO;
using DevExpress.XtraReports.UI;
using DevExpress.XtraEditors;
using ABC.File;

namespace ABC.Toa
{
    public partial class Toa_QuanLy :XtraUserControl
    {
        public Toa_QuanLy()
        {
            InitializeComponent();
            event_click();
            txtngay.Text = Get_CauHinh.ngay_text;
            Look_tenlai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            lool_nhomLai.Properties.DataSource = DuLieu.List_Nhom_NCC.Tables[0];
            this.Load += new EventHandler(Toa_QuanLyDuLieu_Load);
            Link_add.Click += new EventHandler(Link_add_Click);
            link_in.Click += new EventHandler(link_in_Click);
            Link_xoa.Click += new EventHandler(Link_xoa_Click);
            link_pdf.Click += new EventHandler(link_pdf_Click);
        }

        void link_pdf_Click(object sender, EventArgs e)
        {
            xuatpdf();
        }

        void Link_xoa_Click(object sender, EventArgs e)
        {
            DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
            if (msgbox.dialog_yes_no("Xóa toa "+dr["TOA1"].ToString()+" ?") == DialogResult.Yes)
            {
                string sql = "Xoa_toa '"+dr["ID"].ToString().ToUpper()+"'";
                db.T_exc(sql);
                tailai();

            }
        }

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

        void Link_add_Click(object sender, EventArgs e)
        {
            // thiet lap
             DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
             if (dr["TINHTRANG"].ToString() == "Hoàn tất" || dr["TINHTRANG"].ToString() == "Chưa hoàn tất")
             {
                 //DuLieu.User_case = "Edit";
                 ////  DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                 //DuLieu.Ma_Toa = dr["ID"].ToString();
                 //DuLieu.KyTuToa = dr["KYTU"].ToString();
                 //DuLieu.So_Toa = int.Parse(dr["SOTOA"].ToString());
                 //DuLieu.Ngay_hienthi_Toa = int.Parse(dr["NGAYLAMTOA"].ToString());
                 //DuLieu.Tungay_toa = int.Parse(dr["FROM_N"].ToString());
                 //DuLieu.Denngay_toa = int.Parse(dr["TO_N"].ToString());
                 //DuLieu.Ngay_hienthi_Toa_text = Convert.ToDateTime(dr["NGAYLAMTOA_TEXT"].ToString()).ToShortDateString();
                 //DuLieu.tungay_toa_text = Convert.ToDateTime(dr["FROM_TEXT"].ToString()).ToShortDateString();
                 //DuLieu.DenNgay_toa_text = Convert.ToDateTime(dr["TO_TEXT"].ToString()).ToShortDateString();

                 //DuLieu.ID_NCC_Toa = dr["NCC_ID"].ToString().ToUpper();
                 //DuLieu.Ten_NCC_Toa = dr["Tbl_NCC_Name"].ToString();
                 ////load chi tra
                 //DuLieu.TKHIEN = decimal.Parse(dr["TKHIEN"].ToString());
                 //DuLieu.TNUOC = decimal.Parse(dr["TNUOC"].ToString());
                 //DuLieu.TUNG = decimal.Parse(dr["TUNG"].ToString());
                 //DuLieu.TXE = decimal.Parse(dr["TXE"].ToString());
                 //DuLieu.TKY = decimal.Parse(dr["TKY"].ToString());
                 //DuLieu.T_HH = decimal.Parse(dr["THH"].ToString());


                 //DuLieu.TTHEM1 = decimal.Parse(dr["TTHEM1"].ToString());
                 //DuLieu.TTHEM2 = decimal.Parse(dr["TTHEM2"].ToString());
                 //DuLieu.TTHEM3 = decimal.Parse(dr["TTHEM3"].ToString());

                 //DuLieu.TEN_THEM1 = dr["TEN_THEM1"].ToString();
                 //DuLieu.TEN_THEM2 = dr["TEN_THEM2"].ToString();
                 //DuLieu.TEN_THEM3 = dr["TEN_THEM3"].ToString();

                 //DuLieu.x1 = dr["C1"].ToString();
                 //DuLieu.x2 = dr["C2"].ToString();
                 //DuLieu.x3 = dr["C3"].ToString();


                 //_Toa_New = new Toa_New();
                 //_Toa_New.FormClosed += new FormClosedEventHandler(_Toa_New_FormClosed);
                 //_Toa_New.ShowDialog();
             }
             else
             {
                 msgbox.dialog_OK_info("Toa đã gộp! \n Chỉ để tra cứu.");
             }
        }

        void Toa_QuanLyDuLieu_Load(object sender, EventArgs e)
        {
            tailai();
        }
        #region lệnh dùng chung
        void _BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
   
        }
        public void bochon()
        {
     
        }
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public void tailai()
        {
            #region
            string filter_string = "select * from Toa_DaTao where khoa is null and NGAYLAMTOA >0 ";
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                int ngden = (int)(Convert.ToDateTime(txtngayden.Text) - new DateTime(1900, 1, 1)).TotalDays;
                filter_string += "AND NGAYLAMTOA >= " + ng + " AND NGAYLAMTOA <= " + ngden + "";
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                {
                    filter_string += " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                }

                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
            }
            else
            {
                filter_string += "AND NGAYLAMTOA = " + Get_CauHinh.Ngay_DN + "";
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                {
                    filter_string += " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                }

                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
            }
            DuLieu.QuanLyToa = _NhapSi_Class.get_dataset(filter_string);

            DuLieu.QuanLyToa.Tables[0].Columns.Add("Select", typeof(bool));
            data();
            #endregion
        }
        public void print()
        {
            if (Pow.kiemtra("Toa_QuanLy", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                #endregion
            }
        }
        public void print_bill()// in tong hop
        {
            if (Pow.kiemtra("Toa_QuanLy", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
 
                #endregion
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {
           // btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
            
        }
        Toa_New _Toa_New;
        void btn_new_Click(object sender, EventArgs e)
        {
           
        }
        Class_KetNoi db = new Class_KetNoi();
        void _Toa_New_FormClosed(object sender, FormClosedEventArgs e)
        {
         
           
           
            
        }
        Toa_Class _Toa_Class = new Toa_Class();
        void btn_sua_Click(object sender, EventArgs e)
        {
            print_bill();
        }



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

        void btn_load_Click(object sender, EventArgs e)
        {
            tailai();
        }

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

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

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

      




        #endregion

        #region data
        public void data()
        {
            gc_daluu.DataSource = DuLieu.QuanLyToa.Tables[0];
            gv_daluu.ExpandAllGroups();

            lbdoanhso.Text = cds.SummaryText + " (vnđ)";
            lbphaitra.Text = cpt.SummaryText + " (vnđ)";

            lbhh.Text = chh.SummaryText + " (vnđ)";
            lbkhien.Text = ctk.SummaryText + " (vnđ)";
            lbnd.Text = cnd.SummaryText + " (vnđ)";

            lbut.Text = cut.SummaryText + " (vnđ)";
            lbux.Text = cux.SummaryText + " (vnđ)";
            lbtky.Text = ctky.SummaryText + " (vnđ)";

            lbk1.Text = ck1.SummaryText + " (vnđ)";
            lbk2.Text = ck2.SummaryText + " (vnđ)";
            lbk3.Text = ck3.SummaryText + " (vnđ)";

           
        }
        #endregion


        #region in
        chang _chang = new chang();
        public void print1()
        {

            //   DataRow dr321 = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);

            #region

            if (Pow.kiemtra("LamToa", "Inan") == true)
            {

                progess_print("");
            }
            #endregion


        }
        #endregion


        #region pdf
        Toa_Class tl = new Toa_Class();
        public void xuatpdf()
        {

            DataRow dtrchon = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);


            pdf1();


        }
        public void pdf1()
        {
            #region

            if (Pow.kiemtra("LamToa", "Inan") == true)
            {
                #region
                DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                #region


                string ID_Toa = dr["ID"].ToString();
                //------------------thong tin chi tiet toa
                string sql = "Get_data_toa '" + ID_Toa + "'";
                Toa_Class.Table_Data = db.T_opendataset(sql);
                T_TL.KyTu_Toa = dr["KYTU"].ToString();
                T_TL.SoToa = int.Parse(dr["SOTOA"].ToString());
                T_TL.Ngay_ToaNum = int.Parse(dr["NGAYLAMTOA"].ToString());
                T_TL.NgayTuNum = int.Parse(dr["FROM_N"].ToString());
                T_TL.NgayDenNum = int.Parse(dr["TO_N"].ToString());

                T_TL.Ngay_Toa = Convert.ToDateTime(dr["NGAYLAMTOA_TEXT"].ToString()).ToShortDateString();
                T_TL.NgayTu = Convert.ToDateTime(dr["FROM_TEXT"].ToString()).ToShortDateString();
                T_TL.NgayDen = Convert.ToDateTime(dr["TO_TEXT"].ToString()).ToShortDateString();

                //  T_TL.NCC_ID = dr["NCC_ID"].ToString().ToUpper();
                T_TL.NCC_Name = dr["Tbl_NCC_Name"].ToString();
                //load chi tra
                T_TL.TKHIEN = double.Parse(dr["TKHIEN"].ToString());
                T_TL.TNUOC = double.Parse(dr["TNUOC"].ToString());
                T_TL.TUNG = double.Parse(dr["TUNG"].ToString());
                T_TL.TXE = double.Parse(dr["TXE"].ToString());
                T_TL.TKY = double.Parse(dr["TKY"].ToString());
                T_TL.T_HH = double.Parse(dr["THH"].ToString());


                T_TL.TTHEM1 = double.Parse(dr["TTHEM1"].ToString());
                T_TL.TTHEM2 = double.Parse(dr["TTHEM2"].ToString());
                T_TL.TTHEM3 = double.Parse(dr["TTHEM3"].ToString());

                T_TL.TEN_THEM1 = dr["TEN_THEM1"].ToString();
                T_TL.TEN_THEM2 = dr["TEN_THEM2"].ToString();
                T_TL.TEN_THEM3 = dr["TEN_THEM3"].ToString();

                T_TL.xe1 = dr["C1"].ToString();
                T_TL.xe2 = dr["C2"].ToString();
                T_TL.xe3 = dr["C3"].ToString();
                T_TL.TrangThai = dr["TINHTRANG"].ToString();
                T_TL.GhiChu = dr["C5"].ToString();
                //--------------------------------------------------------------
                double TongchiPhi = T_TL.TKHIEN + T_TL.TNUOC;

                double ChiPhikhac = T_TL.TUNG + T_TL.TXE + T_TL.TKY + T_TL.TTHEM1 + T_TL.TTHEM2 + T_TL.TTHEM3;
                double DS_Toa;
                object Sum = Toa_Class.Table_Data.Tables[0].Compute("Sum(ThanhTien)", "");
                try { DS_Toa = (double)Sum; }
                catch { DS_Toa = 0; }
                T_TL.TPHAITRA = DS_Toa - T_TL.T_HH - TongchiPhi - ChiPhikhac;
                //--------------------------------------------------------------

                double SL_Toa;
                object SumSL = Toa_Class.Table_Data.Tables[0].Compute("Sum(Toa_SL)", "");
                try { SL_Toa = (double)SumSL; }
                catch { SL_Toa = 0; }
                // public double SL_Goc, SL_Toa, SL_Lech;
                try { T_TL.Gia_Toa = DS_Toa / SL_Toa; }
                catch { T_TL.Gia_Toa = 0; }
                // public double DS_Goc, DS_Toa, DS_Lech;
                try { T_TL._GiaT_cophi = (DS_Toa - TongchiPhi - T_TL.T_HH) / SL_Toa; }
                catch { T_TL._GiaT_cophi = 0; }
                //get du lieu chi tiet

                //get toa
                #endregion

                #region
                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(T_TL.TPHAITRA.ToString().Replace(",", "").Replace(".", ""));
                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();
                //gan bien 



                T_TL rapport1 = new T_TL();
                rapport1.DataSource = dsSelectData;
                rapport1.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                //  rapport1.ShowPreviewDialog();
                #endregion
                string ten_file_pdf = dr["Tbl_NCC_Name"].ToString() + "_" + DateTime.Now.Hour.ToString() + "h" + DateTime.Now.Minute.ToString();

                string thumuc = Get_CauHinh.Dt_DAT.Rows[0]["Fordel_pdf2"].ToString();

                if (!Directory.Exists(thumuc))
                {
                    msgbox.dialog_OK_warning("Không tìm thấy thư mục " + thumuc + " \n Vui lòng kiểm tra lại");

                }
                else
                {
                    string thumucmoi = thumuc + @"\" + Get_CauHinh.ngay_text.Replace(@"\", "_").Replace("-", "_").Replace("/", "_");
                    check_va_taothumuc(thumucmoi);
                    var reportPath = string.Concat(thumucmoi, @"\" + ten_file_pdf, ".pdf");

                    var stream = new MemoryStream();
                    rapport1.ExportToPdf(stream);

                    using (var fs = System.IO.File.Create(reportPath))
                    {
                        stream.WriteTo(fs);
                    }

                    var reportStream = System.IO.File.OpenRead(reportPath);
                    msgbox.dialog_OK_info("Đã xuất pdf.");
                }
                #endregion
            }
            #endregion

        }
        public void check_va_taothumuc(string pa)
        {
            string path = pa;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

            }
            else
            {

            }
        }
        #endregion
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //---------------------------------------                                     ---------------------
        //---------------------------------------          IN TOA                -------------------
        //---------------------------------------                                     --------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        #region ham progess in

        public void progess_print(string ID_TOA)
        {
            #region
            DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
            #region


            string ID_Toa = dr["ID"].ToString();
            string sql = "Get_data_toa '" + ID_Toa + "'";
            Toa_Class.Table_Data = db.T_opendataset(sql);
            //------------------thong tin chi tiet toa

            T_TL.KyTu_Toa = dr["KYTU"].ToString();
            T_TL.SoToa = int.Parse(dr["SOTOA"].ToString());
            T_TL.Ngay_ToaNum = int.Parse(dr["NGAYLAMTOA"].ToString());
            T_TL.NgayTuNum = int.Parse(dr["FROM_N"].ToString());
            T_TL.NgayDenNum = int.Parse(dr["TO_N"].ToString());

            T_TL.Ngay_Toa = Convert.ToDateTime(dr["NGAYLAMTOA_TEXT"].ToString()).ToShortDateString();
            T_TL.NgayTu = Convert.ToDateTime(dr["FROM_TEXT"].ToString()).ToShortDateString();
            T_TL.NgayDen = Convert.ToDateTime(dr["TO_TEXT"].ToString()).ToShortDateString();

            //  T_TL.NCC_ID = dr["NCC_ID"].ToString().ToUpper();
            T_TL.NCC_Name = dr["Tbl_NCC_Name"].ToString();
            //load chi tra
            T_TL.TKHIEN = double.Parse(dr["TKHIEN"].ToString());
            T_TL.TNUOC = double.Parse(dr["TNUOC"].ToString());
            T_TL.TUNG = double.Parse(dr["TUNG"].ToString());
            T_TL.TXE = double.Parse(dr["TXE"].ToString());
            T_TL.TKY = double.Parse(dr["TKY"].ToString());
            T_TL.T_HH = double.Parse(dr["THH"].ToString());


            T_TL.TTHEM1 = double.Parse(dr["TTHEM1"].ToString());
            T_TL.TTHEM2 = double.Parse(dr["TTHEM2"].ToString());
            T_TL.TTHEM3 = double.Parse(dr["TTHEM3"].ToString());

            T_TL.TEN_THEM1 = dr["TEN_THEM1"].ToString();
            T_TL.TEN_THEM2 = dr["TEN_THEM2"].ToString();
            T_TL.TEN_THEM3 = dr["TEN_THEM3"].ToString();

            T_TL.xe1 = dr["C1"].ToString();
            T_TL.xe2 = dr["C2"].ToString();
            T_TL.xe3 = dr["C3"].ToString();
            T_TL.TrangThai = dr["TINHTRANG"].ToString();
            T_TL.GhiChu = dr["C5"].ToString();
            //--------------------------------------------------------------
            double TongchiPhi = T_TL.TKHIEN + T_TL.TNUOC;

            double ChiPhikhac = T_TL.TUNG + T_TL.TXE + T_TL.TKY + T_TL.TTHEM1 + T_TL.TTHEM2 + T_TL.TTHEM3;
            double DS_Toa;
            object Sum = Toa_Class.Table_Data.Tables[0].Compute("Sum(ThanhTien)", "");
            try { DS_Toa = (double)Sum; }
            catch { DS_Toa = 0; }
            T_TL.TPHAITRA = DS_Toa - T_TL.T_HH - TongchiPhi - ChiPhikhac;
            //--------------------------------------------------------------

            double SL_Toa;
            object SumSL = Toa_Class.Table_Data.Tables[0].Compute("Sum(Toa_SL)", "");
            try { SL_Toa = (double)SumSL; }
            catch { SL_Toa = 0; }
            // public double SL_Goc, SL_Toa, SL_Lech;
            try { T_TL.Gia_Toa = DS_Toa / SL_Toa; }
            catch { T_TL.Gia_Toa = 0; }
            // public double DS_Goc, DS_Toa, DS_Lech;
            try { T_TL._GiaT_cophi = (DS_Toa - TongchiPhi - T_TL.T_HH) / SL_Toa; }
            catch { T_TL._GiaT_cophi = 0; }
            //get du lieu chi tiet

            //get toa
            #endregion

            #region
            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(",", ""));
            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();
            //gan bien 



            T_TL rapport1 = new T_TL();
            rapport1.DataSource = dsSelectData;
            rapport1.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
            rapport1.ShowPreviewDialog();
            #endregion

            #endregion

        }
        #endregion

    }
}
