using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using RKLib.ExportData;

public partial class AccountSummery : System.Web.UI.Page
{
    decimal BalanceAmount;
    string date = DateTime.Now.ToLongDateString();
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    DataSet dsMergedContract = new DataSet();
    int loginvariable;
    static string courentlevel;
    string loginid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                btndownload.Visible = false;
            }
            lbluserid.Visible = false;
            ddl_partner_id.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }    

    private void BindDataAccSummery()
    { 
        try
        {            
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@loginid",courentlevel),
                new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONMD", param);
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                ds.Tables[0].Merge(ds.Tables[i]);
            } 
            if (ds.Tables[0].Rows.Count != 0)
            {
                DataTable dt = ds.Tables["Table"].Copy();
                DataView dv = new DataView(dt);
                dv.Sort = "TRANSACTION_DATETIME ASC";
                gvAccountReport.DataSource = dv;
                gvAccountReport.DataBind();
                lblerror.Visible = false;
                btndownload.Visible = true;
                gvAccountReportAGDS.Visible = false;
                gvAccountReportAGDS.Visible = true;
            }               
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
                gvAccountReport.Visible = false;
                lblopeningbalance.Visible = false;
                lblBalance.Visible = false;
                gvAccountReportAGDS.Visible = false;
            }           

        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "Date must be less then  current date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                if (DDL_UserType.SelectedItem.Text.Equals("Self"))
                {
                    loginid = Session["LOGINID"].ToString();
                    courentlevel = loginid;
                    BindDataAccSummery();
                }
                else
                {
                    loginid = ddl_partner_id.SelectedValue.ToString();
                    courentlevel = loginid;
                    AccountSummary();
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {
        DateTime todaysdate = DateTime.Now;
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure,"USP_ALLTRANSACTIONMD", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    lblerror.Visible = false;
                    Session["Dataset"] = ds;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 2, 3, 1, 11, 7, 9, 12, 8, 6, 13 };
                    RKLib.ExportData.Export objExport = new
                    RKLib.ExportData.Export("Web");
                    objExport.ExportDetails(dtEmployee,
                    iColumns, Export.ExportFormat.CSV,
                    "C:\\TxnAccountSummery.csv");
                }               
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    lblerror.Visible = false;
                    Session["Dataset"] = ds;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 3, 4, 2, 12, 6, 7, 8, 9, 10, };
                    RKLib.ExportData.Export objExport = new
                    RKLib.ExportData.Export("Web");
                    objExport.ExportDetails(dtEmployee,
                    iColumns, Export.ExportFormat.CSV,
                    "C:\\TxnAccountSummery.csv");
                } 
            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    protected void gvAccountReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvAccountReport.PageIndex = e.NewPageIndex;
            BindDataAccSummery();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  


    }

    protected void gvAccountReportAGDS_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvAccountReportAGDS.PageIndex = e.NewPageIndex;
            AccountSummary();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  


    }
    protected void DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                lbluserid.Visible = false;
                ddl_partner_id.Visible = false;
            }
            else if (DDL_UserType.SelectedItem.Text.Equals("MDS"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","MDS"), 
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetMDS_DS_AG_OFWL", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
            else if (DDL_UserType.SelectedItem.Text.Equals("Distributor"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Distributor"), 
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetMDS_DS_AG_OFWL", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
            if (DDL_UserType.SelectedItem.Text.Equals("Agent"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Agent"),
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetMDS_DS_AG_OFWL", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    private void BindDataAccSummeryAGDS()
    {
        string todaysdate = DateTime.Now.ToShortDateString();
        string[] datetoday = todaysdate.Split('/');
        string tomonth = Convert.ToString(datetoday.GetValue(0));
        string todate1 = Convert.ToString(datetoday.GetValue(1));
        string year = Convert.ToString(datetoday.GetValue(2));

        string nowdate = (tomonth + "-" + todate1 + "-" + year);
        try
        {
            if (txtFrmDate.Text == "" )
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",nowdate),
                    new SqlParameter("@Todate",nowdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                DataTable dt = ds.Tables["Table"].Copy();
                DataView dv = new DataView(dt);
                dv.Sort = "TRANSACTION_DATETIME ASC";
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReportAGDS.DataSource = dv;
                    gvAccountReportAGDS.DataBind();
                    lblerror.Visible = false;
                    lblopeningbalance.Visible = true;
                    lblBalance.Visible = true;
                    btndownload.Visible = true;
                    gvAccountReportAGDS.Visible = true;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                if (ds.Tables[0].Rows.Count != 0)
                {
                    DataTable dt = ds.Tables["Table"].Copy();
                    DataView dv = new DataView(dt);
                    dv.Sort = "TRANSACTION_DATETIME ASC";
                    gvAccountReportAGDS.DataSource = dv;
                    gvAccountReportAGDS.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    lblopeningbalance.Visible = true;
                    lblBalance.Visible = true;
                    gvAccountReport.Visible = false;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvAccountReport.Visible = false;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                    gvAccountReport.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    private void AccountSummary()
    {
        string todaysdate = DateTime.Now.ToShortDateString();
        string[] datetoday = todaysdate.Split('/');
        string tomonth = Convert.ToString(datetoday.GetValue(0));
        string todate1 = Convert.ToString(datetoday.GetValue(1));
        string year = Convert.ToString(datetoday.GetValue(2));
        string nowdate = (tomonth + "-" + todate1 + "-" + year);
        try
        {            
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@loginid",courentlevel),
                new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                gvAccountReport.DataSource = ds;
                gvAccountReport.DataBind();
                lblerror.Visible = false;
                btndownload.Visible = true;
                gvAccountReport.Visible = true;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
                gvAccountReport.Visible = false;
                lblopeningbalance.Visible = false;
                lblBalance.Visible = false;
            }            
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
}
