using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.OracleClient;

public partial class POS_MerchantSettle : System.Web.UI.Page
{
    bool diners;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["diners"] = "f";
            rowAccount.Visible = false;
            rowMerchant.Visible = true;
            rowDates.Visible = true;
            loadServices1();
            dtFrom.date = getDayofMonth(1);
            dtTo.date = getDayofMonth(DateTime.Now.Day);

            if (Request.QueryString["clID"] != null)
            {
                txtClientNumber.Text = Request.QueryString["clID"];
                btnSearch_Click(sender, e);
            }
            ExceptionUtility.LogException(0, "", "40", "Page_Load", "");
        }
        lblError.Text = "";
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string servType = ddlServices.SelectedValue;
        if (servType.Equals("007"))
        {
            gvTerminals.Columns[3].HeaderText = "Requested Count";
            gvTerminals.Columns[4].HeaderText = "Requested Amount";
            gvTerminals.Columns[7].HeaderText = "Settle Count";
            gvTerminals.Columns[8].HeaderText = "Settle Amount";

            gvServices.Columns[4].HeaderText = "Requested Count";
            gvServices.Columns[5].HeaderText = "Requested Amount";
            gvServices.Columns[8].HeaderText = "Settle Count";
            gvServices.Columns[9].HeaderText = "Settle Amount";
            ViewState["diners"] = "t";
        }
        else
        {
            gvTerminals.Columns[3].HeaderText = "L1 Count";
            gvTerminals.Columns[4].HeaderText = "L1 Amount";
            gvTerminals.Columns[7].HeaderText = "Mada Count";
            gvTerminals.Columns[8].HeaderText = "Mada Amount";

            gvServices.Columns[4].HeaderText = "L1 Count";
            gvServices.Columns[5].HeaderText = "L1 Amount";
            gvServices.Columns[8].HeaderText = "Mada Count";
            gvServices.Columns[9].HeaderText = "Mada Amount";
            ViewState["diners"] = "f";
        }
        if (rdTerminalNo.Checked)
        {
            if (txtTerminalNo.Text.Length > 0 && dtFrom.date.Length > 0 && dtTo.date.Length > 0)
            {
                ExceptionUtility.LogException(0, "", "40.217", "btnSearch_Click", "");
                loadTerminals(txtTerminalNo.Text, servType);
               
            }
            else
            {
                ExceptionUtility.LogException(9, "", "40.217", "btnSearch_Click", "");
                lblError.Text = "Please Enter All Data...";
            }
        }
        else if (rdMerchants.Checked)
        {
            if (txtClientNumber.Text.Length > 0)
            {
                ExceptionUtility.LogException(0, "", "40.217", "btnSearch_Click", "");
                loadMerchantTerminals(txtClientNumber.Text, servType);
                
            }
            else
            {
                ExceptionUtility.LogException(9, "", "40.217", "btnSearch_Click", "");
                lblError.Text = "Please Enter All Data...";
            }
        }
        
    }

    protected void rdTerminalNo_CheckedChanged(object sender, EventArgs e)
    {
        rowAccount.Visible = true;
        rowMerchant.Visible = false;
        rowDates.Visible = true;
    
        ExceptionUtility.LogException(0, "", "", "rdTerminalNo_CheckedChanged", "");
    }

    protected void rdMerchants_CheckedChanged(object sender, EventArgs e)
    {
        rowAccount.Visible = false;
        rowMerchant.Visible = true;
        rowDates.Visible = true;
 
        ExceptionUtility.LogException(0, "", "", "rdMerchants_CheckedChanged", "");
    }

    //****************************** Terminals ******************************\\

    private void loadMerchantTerminals(string MerID, string servType)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "pos_merch_settl_viewer.get_client_terms";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_client_no", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_frm_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_to_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_service_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_client_no"].Value = MerID;
            oracom.Parameters["p_frm_date"].Value = dtFrom.date;
            oracom.Parameters["p_to_date"].Value = dtTo.date;
            oracom.Parameters["p_service_code"].Value = servType;


            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();

            gvTerminals.DataSource = ds;
            gvTerminals.DataBind();

            pnlTerminals.Visible = true;
            pnlServices.Visible = false;
            pnlTxn.Visible = false;
            ExceptionUtility.LogException(0, "pos_merch_settl_viewer.get_client_terms", "217.1", "loadMerchantTerminals", "MerID: " + MerID +",Service Type: " + servType);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_merch_settl_viewer.get_client_terms", "217.1", "loadMerchantTerminals", "MerID: " + MerID + ", p_frm_date: " + dtFrom.date + ", p_to_date: " + dtTo.date + ", Serv Type: " + servType + ", p_lang: EN");
            gvTerminals.DataSource = null ;
            gvTerminals.DataBind(); 
        }
    }

    private void loadTerminals(string termID, string servType)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "pos_merch_settl_viewer.get_term_period";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_frm_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_to_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_service_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_term_id"].Value = termID;
            oracom.Parameters["p_frm_date"].Value = dtFrom.date;
            oracom.Parameters["p_to_date"].Value = dtTo.date;
            oracom.Parameters["p_service_code"].Value = servType;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();

            gvTerminals.DataSource = ds;
            gvTerminals.DataBind();

            string code = oracom.Parameters["p_res_code"].Value.ToString();
            string msg = oracom.Parameters["p_res_msg"].Value.ToString();

            if (!code.Equals("0"))
                lblError.Text = msg;

            pnlTerminals.Visible = true;
            pnlServices.Visible = false;
            pnlTxn.Visible = false;
            ExceptionUtility.LogException(0, "pos_merch_settl_viewer.get_term_period", "217.1", "loadTerminals", "termID: " + termID + ", p_frm_date: " + dtFrom.date + ", p_to_date: " + dtTo.date + ", p_lang: EN");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_merch_settl_viewer.get_term_period", "loadTerminals","217.1", "termID: " + termID+", p_frm_date: "+dtFrom .date +", p_to_date: "+dtTo .date +", p_lang: EN");
            gvTerminals.DataSource = null;
            gvTerminals.DataBind();
        }
    }

    protected void gvTerminals_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string terminalID = gvTerminals.DataKeys[index].Value.ToString();
            string date = gvTerminals.Rows[index].Cells[9].Text.ToString();
            string time = gvTerminals.Rows[index].Cells[10].Text.ToString();
            string srvCnt = gvTerminals.Rows[index].Cells[3].Text.ToString();
            string srvAmt = gvTerminals.Rows[index].Cells[4].Text.ToString();
            
            ExceptionUtility.LogException(0, "", "40.219", "gvTerminals_RowCommand.view", "");
            loadServices(terminalID, date, time, srvCnt, srvAmt);
            ViewState["terminalID"] = terminalID;
            ViewState["date"] = date;
            ViewState["time"] = time;
            ViewState["L1Count"] = srvCnt;
            ViewState["L1Amount"] = srvAmt;
            
        }
        else if (e.CommandName.Equals("p"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string terminalID = gvTerminals.DataKeys[index].Value.ToString();
            string date = gvTerminals.Rows[index].Cells[9].Text.ToString();
            string time = gvTerminals.Rows[index].Cells[10].Text.ToString();
            string srvCnt = gvTerminals.Rows[index].Cells[3].Text.ToString();
            string srvAmt = gvTerminals.Rows[index].Cells[4].Text.ToString();

            string p1 = Session["uid"].ToString();
            string p2 = terminalID;
            string p3 = date;
            string p4 = time;
            string p5 = srvCnt;
            string p6 = srvAmt;
            ExceptionUtility.LogException(0, "", "40.218", "gvTerminals_RowCommand.print", "");
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?rpt=101&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "','Report')", true);
        }
    }

    protected void gvTerminals_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 9; i <= 10; i++)
                e.Row.Cells[i].Visible = false;
            if(ViewState["diners"].ToString().Equals("t"))
                e.Row.Cells[6].Visible = false;
             //e.Row.Cells[12].Visible = true; // VAT_CEE
        }
       
    }

    private void loadServices1()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "pos_merch_settl_viewer.Get_Service_Types";

            oracom.Parameters.Add("P_ServiceType_Name", OracleType.Cursor).Direction = ParameterDirection.Output;


            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ddlServices.DataSource = ds;
            ddlServices.DataBind();

            oracon.Close();

            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
                ddlServices.Items.Remove(ddlServices.Items.FindByValue("007"));
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "pos_merch_settl_viewer.Get_Service_Types", "", "loadServices", "");

        }
    }
    //****************************** Service ******************************\\

    private void loadServices(string termID, string date, string time, string srvCnt, string srvAmt)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "POS_MERCH_SETTL_VIEWER.Get_Term_Services";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_srv_cnt", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_srv_amt", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_term_id"].Value = termID;
            oracom.Parameters["p_date"].Value = date;
            oracom.Parameters["p_time"].Value = time;
            oracom.Parameters["p_srv_cnt"].Value = srvCnt;
            oracom.Parameters["p_srv_amt"].Value = srvAmt;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();

            gvServices.DataSource = ds;
            gvServices.DataBind();

            pnlServices.Visible = true;
            pnlTxn.Visible = false;
            ExceptionUtility.LogException(0, "POS_MERCH_SETTL_VIEWER.Get_Term_Services","219.1", "loadServices", "termID: " + termID + ", date: " + date + ", time: " + time );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "POS_MERCH_SETTL_VIEWER.Get_Term_Services", "219.1", "loadServices", "termID: " + termID + ", date: " + date + ", time: " + time );
            gvServices.DataSource = null;
            gvServices.DataBind();
        }
    }

    private void loadServiceTxn( string ptsCode )
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "POS_MERCH_SETTL_VIEWER.Get_Term_Service_Trx";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pts_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_pts_code"].Value = ptsCode;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();

            gvTxn.DataSource = ds;
            gvTxn.DataBind();

            pnlTxn.Visible = true;
            ExceptionUtility.LogException(0, "POS_MERCH_SETTL_VIEWER.Get_Term_Service_Trx","220.1", "loadServiceTxn", "ptsCode: " + ptsCode);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "POS_MERCH_SETTL_VIEWER.Get_Term_Service_Trx","220.1", "loadServiceTxn", "ptsCode: " + ptsCode );
            gvTxn.DataSource = null;
            gvTxn.DataBind();
        }
    }

    protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();
            ViewState["rowID"] = rowID;
            string status = gvServices.Rows[index].Cells[11].Text.ToString();
            if (!status.ToLower().Equals("loaded"))
            {
                ExceptionUtility.LogException(0, "", "40.220", "gvServices_RowCommand.view", "");
                loadServiceTxn(rowID);
                
            }
            else
            {
                ExceptionUtility.LogException(9, "", "40.220", "gvServices_RowCommand", "");
                lblError.Text = "Can't view transactions for loaded services";
            }
        }
        else if (e.CommandName.Equals("p"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = gvServices.DataKeys[index].Value.ToString();

            string p1 = Session["uid"].ToString();
            string p2 = rowID;
            ExceptionUtility.LogException(0, "", "40.221", "gvServices_RowCommand.print", "");
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?rpt=100&p1=" + p1 + "&p2=" + p2 + "','Report')", true);
        }
    }

    protected void gvServices_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvServices.PageIndex = e.NewPageIndex;
        loadServices(ViewState["terminalID"].ToString(), ViewState["date"].ToString(),
                        ViewState["time"].ToString(), ViewState["srvCnt"].ToString(), ViewState["srvAmt"].ToString() 
                        );
        ExceptionUtility.LogException(0, "", "", "gvServices_PageIndexChanging", "");
    }

    //****************************** Transactions ******************************\\

    protected void gvTxn_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTxn.PageIndex = e.NewPageIndex;
        loadServiceTxn(ViewState["rowID"].ToString());
        ExceptionUtility.LogException(0, "", "", "gvTxn_PageIndexChanging", "");
    }

    private string getDayofMonth(int pDay)
    {
        string day = "", month = "", year = "";
        if (pDay < 10)
            day = "0" + pDay.ToString();
        else
            day = pDay.ToString();
        if (DateTime.Now.Month < 10)
            month = "0" + DateTime.Now.Month.ToString();
        else
            month = DateTime.Now.Month.ToString();
        year = DateTime.Now.Year.ToString();
        string date = day + "-" + month + "-" + year;
        return date;
    }
    protected void gvServices_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            if (ViewState["diners"].ToString().Equals("t"))
                e.Row.Cells[7].Visible = false;
        }
    }
}
