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

using ABC.GiaoDich;
using ABC.Report;
using ABC.KetNoi;
using System.IO;
using DevExpress.XtraReports.UI;
using ABC.File;

namespace ABC.BaoCao
{
    public partial class BaoCao_BaoCaoDoanhSo : DevExpress.XtraEditors.XtraUserControl
    {
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public BaoCao_BaoCaoDoanhSo()
        {
            InitializeComponent();            
           // data();
            event_click();
           // gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);

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

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

       

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

        void _BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
          
        }
        public void bochon()
        {
          
        }

        public void tailai()
        {
            string filter_string = "select * from doanhso_si_le_tong where Date_Num_Final_UpDate >0 ";
            string sql_hoadon;
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                sql_hoadon = "select * from Nhaple_HOADON where Date_Num_Final_UpDate = " + ng + "";
                filter_string += "AND Date_Num_Final_UpDate = " + ng + " ";
                if (check_ten_thuonglai.Checked == true && Look_tenkh.EditValue != null)
                {
                    filter_string += " And ID_NCC = '" + Look_tenkh.EditValue.ToString().ToUpper() + "'";
                }
               
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_NHOM.Checked == true && txtnhom.EditValue != null && txtnhom.EditValue != "")
                {
                    filter_string += " And NHOMLAI = N'" + txtnhom.EditValue.ToString() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            else
            {
                filter_string += "AND Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
                sql_hoadon = "select * from Nhaple_HOADON where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
                if (check_ten_thuonglai.Checked == true && Look_tenkh.EditValue != null)
                {
                    filter_string += " And ID_NCC = '" + Look_tenkh.EditValue.ToString().ToUpper() + "'";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_NHOM.Checked == true && txtnhom.EditValue != null && txtnhom.EditValue != "")
                {
                    filter_string += " And NHOMLAI = N'" + txtnhom.EditValue.ToString() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            DuLieu.BaoCa_DoanhSo = _NhapSi_Class.get_dataset(filter_string);
            DataSet dts_hoadon = _NhapSi_Class.get_dataset(sql_hoadon);
            //tinh tong no va tm 1 ngay
            object TIENMAT = dts_hoadon.Tables[0].Compute("Sum(SUM_TM)", "");
            object TIENNO = dts_hoadon.Tables[0].Compute("Sum(SUM_TNO)", "");
           // DuLieu.tongtienmat_nhaple_baoca = decimal.Parse(TIENMAT.ToString().Replace(",", ""));
           // DuLieu.tongtienno_nhaple_baoca = decimal.Parse(TIENNO.ToString().Replace(",", ""));
            //tinh tien mat cua hoa don & tien no
            DuLieu.BaoCa_DoanhSo.Tables[0].Columns.Add("Sum_TongTienMat", typeof(float));
            DuLieu.BaoCa_DoanhSo.Tables[0].Columns.Add("Sum_TongTienNo", typeof(float));
              //gan tien mat va no vao cac dong cua chi tiet
            for (int i = 0; i < DuLieu.BaoCa_DoanhSo.Tables[0].Rows.Count; i++)
            {
                DataRow[] dr = dts_hoadon.Tables[0].Select("ID_NCC = '" + DuLieu.BaoCa_DoanhSo.Tables[0].Rows[i]["ID_NCC"].ToString() + "'");
                if (dr.Length > 0)
                {
                    DuLieu.BaoCa_DoanhSo.Tables[0].Rows[i]["Sum_TongTienMat"] = float.Parse(dr[0]["SUM_TM"].ToString());
                    DuLieu.BaoCa_DoanhSo.Tables[0].Rows[i]["Sum_TongTienNo"] = float.Parse(dr[0]["SUM_TNO"].ToString());
                }
                else
                {
                    DuLieu.BaoCa_DoanhSo.Tables[0].Rows[i]["Sum_TongTienMat"] = 0;
                    DuLieu.BaoCa_DoanhSo.Tables[0].Rows[i]["Sum_TongTienNo"] = 0;
                }
                DuLieu.BaoCa_DoanhSo.Tables[0].AcceptChanges();
            }
            
                data();
        }
        public void print()
        {
           
            if (Pow.kiemtra("BaoCao_BaoCaoDoanhSo", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region

                
                Get_CauHinh.baocao_tonghop = "Ngày " + txtngay.Value.Day.ToString() + "  Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();


                T_baocaodoanhso rapport = new T_baocaodoanhso();
                rapport.DataSource = DuLieu.BaoCa_DoanhSo;
                rapport.ShowRibbonPreview();
                #endregion
            }

           
        }
        public void print_bill()// in tong hop
        {
            if (Pow.kiemtra("BaoCao_BaoCaoDoanhSo", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {
           
            //btn_del.Click += new EventHandler(btn_del_Click);          
            //btn_all.Click += new EventHandler(btn_all_Click);
            //btn_none.Click += new EventHandler(btn_none_Click);
            //btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
            btn_in.Click += new EventHandler(btn_in_Click);
            btn_pdf.Click += new EventHandler(btn_pdf_Click);
           // btn_sua.Click += new EventHandler(btn_sua_Click);
        }

        void btn_pdf_Click(object sender, EventArgs e)
        {
            pdf();
        }
        public void check_va_taothumuc(string pa)
        {
            string path = pa;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

            }
            else
            {

            }
        }
        public void pdf()
        {
            if (Pow.kiemtra("BaoCao_BaoCaoDoanhSo", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                string ten_file_pdf = "DAO CAO DOANH SO" + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.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
                {
                    Get_CauHinh.baocao_tonghop = "Ngày " + txtngay.Value.Day.ToString() + "  Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();


                    T_baocaodoanhso rapport = new T_baocaodoanhso();
                    rapport.DataSource = DuLieu.BaoCa_DoanhSo;


                    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();
                    rapport.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.");
                }
            }
        }
        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();
        } 

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




        #endregion

        #region data       
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label ThongkeGiaBQ = new Label();
        public void data()
        {
            gc_daluu.DataSource = DuLieu.BaoCa_DoanhSo.Tables[0];
            gv_daluu.ExpandAllGroups();
            gv_daluu.ExpandAllGroups();

            Thongkesoluong.Text = clum_sl.SummaryText + " (kg)";
            thongketien.Text = clum_ds.SummaryText + " (vnđ)";
            ThongkeGiaBQ.Text = clum_gia.SummaryText + " (vnđ)";


        }






        #endregion

        private void Look_tenkh_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
