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;
using System.Diagnostics;

public partial class Business_Repl_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadBranches();
            loadStatus();
            loadModifyReasons();
            loadValues(sender, e);
            loadCurrency();
            ExceptionUtility.LogException(0, "", "23", "Page_Load", "");
        }
        lblError.Text = "";
    }

    private void loadValues(object sender, EventArgs e)
    {
        if (Request.QueryString["atmno"] != null)
        {
            txtTerminal.Text = Request.QueryString["atmno"];

            if (Request.QueryString["pReplDate"] != null)
                DatePicker1.date = Request.QueryString["pReplDate"];

            if (Request.QueryString["pReplTime"] != null)
                txtPrevTime.Text = Request.QueryString["pReplTime"];

            if (Request.QueryString["ReplDate"] != null)
                DatePicker2.date = Request.QueryString["ReplDate"];

            if (Request.QueryString["ReplTime"] != null)
                txtTime.Text = Request.QueryString["ReplTime"];

            Button1_Click(sender, e);
            row1.Visible = true;
            lblTime.Visible = true;
            txtTime.Visible = true;
        }
        else
        {
            row1.Visible = false;
            lblTime.Visible = false;
            txtTime.Visible = false;
        }
    }

    private void loadBranches()
    {
        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 = "LOOKUP_GETTER.Get_Branches";

            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branches", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";

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

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

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Branches","", "loadBranches", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadStatus()
    {
        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 = "LOOKUP_GETTER.get_repl_status";

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


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

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

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.get_repl_status","", "loadStatus", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadCurrency()
    {
        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 = "LOOKUP_GETTER.get_repl_curr";

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

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

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

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.get_repl_curr","", "loadCurrency", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadModifyReasons()
    {
        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 = "LOOKUP_GETTER.Get_Repl_Mod_Reasons";

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


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

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

            ddlModifyReason.Items.Insert(0, "------ Reasons ------");

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Repl_Mod_Reasons","", "loadModifyReasons", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(int pageSize, int pageNumber)
    {
        string parameters = "";
        try
        {
            string currentMethod = "";
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "ATM_DEP_REPLENISHMENT_EDITOR.get_atm_replenishments";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).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.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters["p_atm_num"].Value = txtTerminal.Text;
            if (txtTerminal.Text.Length > 0)
                parameters = "p_atm_num-" + txtTerminal.Text;
            oracom.Parameters["p_prev_repl_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = parameters + ":p_prev_repl_date" + DatePicker1.date;
            oracom.Parameters["p_prev_repl_time"].Value = txtPrevTime.Text;
            if(txtPreTime .Text .Length >0)
                parameters = parameters + ":p_prev_repl_time" + txtPrevTime.Text;
            oracom.Parameters["p_repl_date"].Value = DatePicker2.date;
            if(DatePicker2 .date .Length >0)
                parameters = parameters + ":p_repl_date" + DatePicker2.date;
            oracom.Parameters["p_repl_time"].Value = txtTime.Text;
            if(txtTime .Text .Length >0)
                parameters = parameters + ":p_repl_time" + txtTime.Text;
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            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);

            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
            if(currentMethod .Contains ("Button1_Click"))
                ExceptionUtility.LogException(0, "ATM_DEP_REPLENISHMENT_EDITOR.get_atm_replenishments", "162.1", "loadResult", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_DEP_REPLENISHMENT_EDITOR.get_atm_replenishments","162.1", "loadResult",parameters );
            GridView1.DataSource = null;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "23.162", "Button1_Click", "");
        pnlResult.Visible = true;
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
        loadResult(Helper.gridSize(), 1);
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
        loadResult(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }
    
    protected void btnAdd_Click(object sender, EventArgs e) 
    {
        DropDownList1.Enabled = true;
        txtATMNumberAdd.Enabled = true;
        clear();
        lblReplDate.Visible = false;
        lblPrevReplDate.Visible = false;
        datePrev.Visible = true;
        dateCurrent.Visible = true;
        TextBox5.Enabled = true;
        TextBox6.Enabled = true;
        TextBox7.Enabled = true;
        TextBox8.Enabled = true;

        pnlControls.Visible = true;
        pnlResult.Enabled = false;
        ViewState["mode"] = "a";

        DropDownList1.SelectedIndex = 0;
        pnlReadOnly.Visible = false;

        ddlCurrency.Visible = true;
        TextBox5.Visible = false;
        ExceptionUtility.LogException(0, "", "23.66", "btnAdd_Click", "");
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("e"))
        {
            pnlReadOnly.Visible = true;
            ddlCurrency.Visible = false;
            TextBox5.Visible = true;
            clear();
            ExceptionUtility.LogException(0, "", "23.67", "GridView1_RowCommand.Edit", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "atm_dep_replenishment_editor.get_repl_by_id";


            oracom.Parameters.Add("p_rid", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_rid"].Value = rowID;
            oracom.Parameters["p_lang"].Value = "EN";

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

            string terminalID = ds.Tables[0].Rows[0]["terminal_id"].ToString();
            string branchID = ds.Tables[0].Rows[0]["branch_code"].ToString();

            DropDownList1.SelectedValue = branchID;
            txtATMNumberAdd.Text = terminalID;
            DropDownList1.Enabled = false;
            txtATMNumberAdd.Enabled = false;

            TextBox5.Enabled = false;
            TextBox7.Enabled = false;
            TextBox8.Enabled = false;

            datePrev.date = ds.Tables[0].Rows[0]["prev_replenishment_date"].ToString();
            lblPrevReplDate.Text = ds.Tables[0].Rows[0]["prev_replenishment_date"].ToString();

            txtPreTime.Text = ds.Tables[0].Rows[0]["prev_replenishment_time"].ToString();

            dateCurrent.date = ds.Tables[0].Rows[0]["replenishment_date"].ToString();
            lblReplDate.Text = ds.Tables[0].Rows[0]["replenishment_date"].ToString();//

            txtRepTime.Text = ds.Tables[0].Rows[0]["replenishment_time"].ToString();//GridView1.Rows[index].Cells[4].Text.ToString();
            TextBox5.Text = ds.Tables[0].Rows[0]["replenishment_currency"].ToString();//GridView1.Rows[index].Cells[2].Text.ToString();
            TextBox6.Text = ds.Tables[0].Rows[0]["amount_dispensed"].ToString();//GridView1.Rows[index].Cells[11].Text.ToString();
            TextBox7.Text = ds.Tables[0].Rows[0]["amount_frm_vault"].ToString();//GridView1.Rows[index].Cells[12].Text.ToString();
            TextBox8.Text = ds.Tables[0].Rows[0]["amount_to_vault"].ToString();//GridView1.Rows[index].Cells[13].Text.ToString();
            ddlStatus.SelectedValue = ds.Tables[0].Rows[0]["status_code"].ToString();//GridView1.Rows[index].Cells[24].Text.ToString();
            TextBox10.Text = ds.Tables[0].Rows[0]["record_date"].ToString();//GridView1.Rows[index].Cells[14].Text.ToString();
            TextBox11.Text = ds.Tables[0].Rows[0]["opening_balance"].ToString();//GridView1.Rows[index].Cells[16].Text.ToString();
            TextBox12.Text = ds.Tables[0].Rows[0]["closing_balance"].ToString();//GridView1.Rows[index].Cells[17].Text.ToString();
            TextBox13.Text = ds.Tables[0].Rows[0]["gl_atm_difference"].ToString();//GridView1.Rows[index].Cells[18].Text.ToString();
            TextBox14.Text = ds.Tables[0].Rows[0]["ej_atm_difference"].ToString();//GridView1.Rows[index].Cells[19].Text.ToString();
            TextBox15.Text = ds.Tables[0].Rows[0]["overall_atm_diff"].ToString();//GridView1.Rows[index].Cells[20].Text.ToString();
            TextBox16.Text = ds.Tables[0].Rows[0]["rec_type_code"].ToString();//GridView1.Rows[index].Cells[21].Text.ToString();
            TextBox17.Text = ds.Tables[0].Rows[0]["verified_date"].ToString();//GridView1.Rows[index].Cells[22].Text.ToString();
            TextBox18.Text = ds.Tables[0].Rows[0]["verified_time"].ToString();//GridView1.Rows[index].Cells[23].Text.ToString();
            TextBox19.Text = ds.Tables[0].Rows[0]["sw_amount_dispensed"].ToString();//GridView1.Rows[index].Cells[15].Text.ToString();
            TextBox1.Text = ds.Tables[0].Rows[0]["error_description"].ToString();//GridView1.Rows[index].Cells[25].Text.ToString();
            TextBox3.Text = ds.Tables[0].Rows[0]["GL_amount_dispensed"].ToString();//GridView1.Rows[index].Cells[26].Text.ToString();
            if (ds.Tables[0].Rows[0]["Modify_Reason"].ToString().Length > 0)
                ddlModifyReason.SelectedValue = ds.Tables[0].Rows[0]["Modify_Reason"].ToString();//GridView1.Rows[index].Cells[27].Text.ToString();
            if (ds.Tables[0].Rows[0]["from_b24"].ToString() == "Y")
                chkFromBase24.Checked = true;
            lblClAdd.Text = ds.Tables[0].Rows[0]["ej_print_flag"].ToString();//GridView1.Rows[index].Cells[29].Text.ToString();
            lblBANCS.Text = ds.Tables[0].Rows[0]["bancs_recon_result"].ToString();//GridView1.Rows[index].Cells[30].Text.ToString();

            TextBox20.Text = ds.Tables[0].Rows[0]["b24_amount_dispensed"].ToString();//GridView1.Rows[index].Cells[31].Text.ToString();
            TextBox21.Text = ds.Tables[0].Rows[0]["b24_amount_frm_vault"].ToString();//GridView1.Rows[index].Cells[32].Text.ToString();
            TextBox22.Text = ds.Tables[0].Rows[0]["b24_amount_to_vault"].ToString();//GridView1.Rows[index].Cells[33].Text.ToString();

            txtB24Time.Text = ds.Tables[0].Rows[0]["b24_rpl_time"].ToString();//GridView1.Rows[index].Cells[34].Text.ToString();
            txtPrevB24Time.Text = ds.Tables[0].Rows[0]["b24_p_rpl_time"].ToString();//GridView1.Rows[index].Cells[35].Text.ToString();

            lblUser.Text = ds.Tables[0].Rows[0]["created_by"].ToString();//GridView1.Rows[index].Cells[36].Text.ToString();
            lblUserDate.Text = ds.Tables[0].Rows[0]["create_date"].ToString();//GridView1.Rows[index].Cells[37].Text.ToString();

            dateB24Date.date = ds.Tables[0].Rows[0]["b24_rpl_date"].ToString();//GridView1.Rows[index].Cells[38].Text.ToString();
            dateB24PrevDate.date = ds.Tables[0].Rows[0]["b24_p_rpl_date"].ToString();//GridView1.Rows[index].Cells[39].Text.ToString();


            pnlControls.Visible = true;
            pnlResult.Enabled = false;

            ViewState["mode"] = "u";
            ViewState["rowid"] = rowID;
            ViewState["index"] = index;
            Session["fromdate"] = ds.Tables[0].Rows[0]["prev_repl_date"].ToString();
            Session["todate"] = ds.Tables[0].Rows[0]["repl_date"].ToString();
            Session["fromtime"] = ds.Tables[0].Rows[0]["prev_repl_time"].ToString();
            Session["totime"] = ds.Tables[0].Rows[0]["repl_time"].ToString();
            viewClaims(Session["fromdate"].ToString(), Session["todate"].ToString(), Session["fromtime"].ToString(), Session["totime"].ToString(), index, Helper.gridSize(), 1);



            if (chkFromBase24.Checked)
            {
                txtRejected.Text = "NA";
                txtRetracted.Text = "NA";
            }
            else
            {
                oracom = new OracleCommand();
                oracom.Connection = oracon;

                oracom.CommandType = CommandType.StoredProcedure;
                oracom.CommandText = "atm_replenishment_editor.get_atm_ret_rej";

                oracom.Parameters.Clear();
                oracom.Parameters.Add("p_atm_id", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_pre_clear_date", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_pre_clear_time", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_msg_date", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_msg_time", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_amount_rej", OracleType.Number).Direction = ParameterDirection.Output;
                oracom.Parameters.Add("p_amount_ret", OracleType.Number).Direction = ParameterDirection.Output;


                oracom.Parameters["p_atm_id"].Value = txtATMNumberAdd.Text;
                oracom.Parameters["p_pre_clear_date"].Value = datePrev.date;
                oracom.Parameters["p_pre_clear_time"].Value = txtPreTime.Text.Substring(0, 5);
                oracom.Parameters["p_msg_date"].Value = dateCurrent.date;
                oracom.Parameters["p_msg_time"].Value = txtRepTime.Text.Substring(0, 5);
                              
                da = new OracleDataAdapter(oracom);
                ds = new DataSet();
                da.Fill(ds);
                txtRetracted.Text = oracom.Parameters["p_amount_ret"].Value.ToString();
                txtRejected.Text = oracom.Parameters["p_amount_rej"].Value.ToString();
            }

        }
    }

    private void viewClaims(string fromdate, string todate, string fromtime, string totime, int index, int pageSize, int pageNumber)
    {
        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 = "claim_process.get_atm_claims_for_period";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_rec_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_from_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_to_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_from_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_to_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).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.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_atm_num"].Value = txtTerminal.Text;
            oracom.Parameters["p_rec_type"].Value = "030";
            oracom.Parameters["p_from_date"].Value = fromdate;
            oracom.Parameters["p_to_date"].Value = todate;
            oracom.Parameters["p_from_time"].Value = fromtime;
            oracom.Parameters["p_to_time"].Value = totime;

            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            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);

            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();

            GridView2.DataSource = ds;
            GridView2.DataBind();
            if (pageNumber == 1)
            {
                DropDownList3.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    DropDownList3.Items.Add(i.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "claim_process.get_atm_claims_for_period","", "viewClaims", "fromdate: "+fromdate +", todate: "+todate +", fromtime: "+fromtime+", totime: "+totime +", index: "+index+", pageSize: "+pageSize +", pageNumber: "+pageNumber );
            GridView2.DataSource = null;
            GridView2.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "DropDownList3_SelectedIndexChanged", "");
        int index = int.Parse(ViewState["index"].ToString());
        viewClaims(Session["fromdate"].ToString(), Session["todate"].ToString(), Session["fromtime"].ToString(), Session["totime"].ToString(), index, Helper.gridSize(), int.Parse(DropDownList3.SelectedValue));
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
        pnlControls.Visible = false;
        pnlResult.Enabled = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtPrevB24Time.Text.Length != 0 && txtPrevB24Time.Text.Length != 8)
                lblError.Text = "time fields must be entered correctly";
            else if (txtB24Time.Text.Length != 0 && txtB24Time.Text.Length != 8)
                lblError.Text = "time fields must be entered correctly";
            else if (txtPreTime.Text.Length != 0 && txtPreTime.Text.Length != 8)
                lblError.Text = "time fields must be entered correctly";
            else if (txtRepTime.Text.Length != 0 && txtRepTime.Text.Length != 8)
                lblError.Text = "time fields must be entered correctly";
            else
            {
                string mode = ViewState["mode"].ToString();
                string error = "";

                if (mode.Equals("a"))
                {
                    ExceptionUtility.LogException(0, "", "23.387", "btnSave_Click", "");
                    bool saved = addRepl(ref error);
                    if (saved)
                    {
                        lblError.Text = "Replineshment data Saved";
                    }
                    else
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "An Error Occured!";
                            ExceptionUtility.LogException(9, "", "23.387", "btnSave_Click", error);
                        }
                        else
                            lblError.Text = error;
                    }
                }
                else if (mode.Equals("u"))
                {
                    string Uid = ViewState["rowid"].ToString();
                    if (TextBox11.Text.Length > 0)
                    {
                        bool saved = updateRepl(Uid, ref error);
                        if (saved)
                        {
                            lblError.Text = "Replineshment data Saved";
                        }
                        else
                        {
                            if (Helper.OracleError(error))
                            {
                                lblError.Text = "An Error Occured!";
                                ExceptionUtility.LogException(9, "", "23.387", "btnSave_Click", error);
                            }
                            else
                                lblError.Text = error;
                        }
                    }
                    else
                    {
                        lblError.Text = "Please enter opening balance";
                    }
                }
                //getrepS();
                ViewState["mode"] = null;
                ViewState["rowid"] = null;
                pnlControls.Visible = false;
                pnlResult.Enabled = true;
                lblError.Visible = true;
            }
        }
        catch (Exception ex)
        {
        }
    }

    private bool addRepl(ref string error)
    {
        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 = "ATM_DEP_REPLENISHMENT_EDITOR.add_atm_replenishment";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_b24_prepl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_prepl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_repl_cur", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_amt_disp", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_amt_frm_vault", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_amt_to_vault", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_record_status", 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["p_branch_code"].Value = DropDownList1.SelectedValue;
            if (DropDownList1.SelectedIndex > 0)
                parameters = "p_branch_code-" + DropDownList1.SelectedItem.Text;
            oracom.Parameters["p_atm_num"].Value = txtATMNumberAdd.Text;
            if (txtATMNumberAdd.Text.Length > 0)
                parameters = parameters + ":p_atm_num" + txtATMNumberAdd.Text;
            oracom.Parameters["p_prev_repl_date"].Value = datePrev.date;
            if(datePrev .date .Length >0)
                parameters = parameters + ":p_prev_repl_date" + datePrev.date;
            oracom.Parameters["p_prev_repl_time"].Value = txtPreTime.Text.Replace(":", "");
            if(txtPreTime .Text .Length >0)
                parameters = parameters + ":p_prev_repl_time" + txtPreTime.Text.Replace(":", "");
            oracom.Parameters["p_repl_date"].Value = dateCurrent.date;
            if(dateCurrent .date .Length >0)
                parameters = parameters + ":p_repl_date" + dateCurrent.date;
            oracom.Parameters["p_repl_time"].Value = txtRepTime.Text.Replace(":", "");
            if(txtRepTime .Text .Length >0)
                parameters = parameters + ":p_repl_time" + txtRepTime.Text.Replace(":", "");
            oracom.Parameters["p_b24_prepl_date"].Value = dateB24PrevDate.date;
            if(dateB24PrevDate .date .Length >0)
                parameters = parameters + ":p_b24_prepl_date" + dateB24PrevDate.date;
            oracom.Parameters["p_b24_prepl_time"].Value = txtPrevB24Time.Text.Replace(":", "");
            if(txtPrevB24Time .Text .Length >0)
                parameters = parameters + ":p_b24_prepl_time" + txtPrevB24Time.Text.Replace(":", "");
            oracom.Parameters["p_b24_repl_date"].Value = dateB24Date.date;
            if(dateB24Date .date .Length >0)
                parameters = parameters + ":p_b24_repl_date" + dateB24Date.date;
            oracom.Parameters["p_b24_repl_time"].Value = txtB24Time.Text.Replace(":", "");
            if(txtB24Time .Text .Length >0)
                parameters = parameters + ":p_b24_repl_time" + txtB24Time.Text.Replace(":", "");

            oracom.Parameters["p_repl_cur"].Value = ddlCurrency.SelectedValue; //TextBox5.Text;

            if (getNumber(TextBox6.Text).Length > 0)
            {
                parameters = parameters + ":p_amt_disp" + getNumber(TextBox6.Text);
                oracom.Parameters["p_amt_disp"].Value = getNumber(TextBox6.Text);
            }
            else
                oracom.Parameters["p_amt_disp"].Value = OracleNumber.Null;

            if (getNumber(TextBox7.Text).Length > 0)
            {
                parameters = parameters + ":p_amt_frm_vault" + getNumber(TextBox7.Text);
                oracom.Parameters["p_amt_frm_vault"].Value = getNumber(TextBox7.Text);
            }
            else
                oracom.Parameters["p_amt_frm_vault"].Value = OracleNumber.Null;

            if (getNumber(TextBox8.Text).Length > 0)
            {
                parameters = parameters + ":p_amt_to_vault" + getNumber(TextBox8.Text);
                oracom.Parameters["p_amt_to_vault"].Value = getNumber(TextBox8.Text);
            }
            else
                oracom.Parameters["p_amt_to_vault"].Value = OracleNumber.Null;

            oracom.Parameters["p_record_status"].Value = ddlStatus.SelectedValue;
            parameters = parameters + ":p_record_status" + ddlStatus.SelectedItem.Text +":p_lang-EN";
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "ATM_DEP_REPLENISHMENT_EDITOR.add_atm_replenishment", "387.1", "addRepl", parameters);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "ATM_DEP_REPLENISHMENT_EDITOR.add_atm_replenishment", "387.1", "addRepl", parameters);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_DEP_REPLENISHMENT_EDITOR.add_atm_replenishment","387.1" ,"addRepl",parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool updateRepl(string rowID, ref string error)
    {
        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 = "ATM_DEP_REPLENISHMENT_EDITOR.update_atm_replenishment";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_rid", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_prev_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_b24_prepl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_prepl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_b24_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_amt_disp", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_record_status", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_open_bal", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_close_bal", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_error_desc", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_reason", 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["p_rid"].Value = rowID;
            parameters = "p_rid-" + rowID;
            oracom.Parameters["p_b24_prepl_date"].Value = dateB24PrevDate.date;
            if (dateB24PrevDate.date.Length > 0)
                parameters = parameters + ":p_b24_prepl_date-" + dateB24PrevDate.date;
            oracom.Parameters["p_b24_prepl_time"].Value = txtPrevB24Time.Text.Replace(":", "");
            if(txtPrevB24Time .Text .Length >0)
                parameters = parameters + ":p_b24_prepl_time-" + txtPrevB24Time.Text.Replace(":", "");
            oracom.Parameters["p_b24_repl_date"].Value = dateB24Date.date;
            if(dateB24Date .date .Length>0)
                parameters = parameters + ":p_b24_repl_date-" + dateB24Date.date;
            oracom.Parameters["p_b24_repl_time"].Value = txtB24Time.Text.Replace(":", "");
            if(txtB24Time.Text .Length >0)
                parameters = parameters + ":p_b24_repl_time-" + txtB24Time.Text.Replace(":", "");
            oracom.Parameters["p_prev_repl_date"].Value = datePrev.date;
            if(datePrev .date .Length >0)
                parameters = parameters + ":p_prev_repl_date-" + datePrev.date;
            oracom.Parameters["p_repl_date"].Value = dateCurrent.date;
            if(dateCurrent .date .Length >0)
                parameters = parameters + ":p_repl_date-" + dateCurrent.date;
            oracom.Parameters["p_prev_repl_time"].Value = txtPreTime.Text.Replace(":", "");
            if(txtPreTime .Text .Length >0)
                parameters = parameters + ":p_prev_repl_time-" + txtPreTime.Text.Replace(":", "");
            oracom.Parameters["p_repl_time"].Value = txtRepTime.Text.Replace(":", "");

            if (getNumber(TextBox6.Text).Length > 0)
            {
                parameters = parameters + ":p_amt_disp-" + getNumber(TextBox6.Text);
                oracom.Parameters["p_amt_disp"].Value = getNumber(TextBox6.Text);
            }
            else
                oracom.Parameters["p_amt_disp"].Value = OracleNumber.Null;

            oracom.Parameters["p_record_status"].Value = ddlStatus.SelectedValue;
            if(ddlStatus .SelectedIndex >0)
                parameters = parameters + ":p_record_status-" + ddlStatus.SelectedItem .Text ;
            oracom.Parameters["p_open_bal"].Value = getNumber(TextBox11.Text);
            if(TextBox11 .Text .Length >0)
                parameters = parameters + ":p_open_bal-" + getNumber(TextBox11.Text);
            oracom.Parameters["p_close_bal"].Value = getNumber(TextBox12.Text);
            if(TextBox12 .Text .Length >0)
                parameters = parameters + ":p_close_bal-" + getNumber(TextBox12.Text);
            oracom.Parameters["p_error_desc"].Value = TextBox1.Text;
            if (ddlModifyReason.SelectedIndex > 0)
            {
                parameters = parameters + ":p_reason-" + ddlModifyReason.SelectedItem.Text;
                oracom.Parameters["p_reason"].Value = ddlModifyReason.SelectedValue;
            }
            else
                oracom.Parameters["p_reason"].Value = OracleString.Null;

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

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "ATM_DEP_REPLENISHMENT_EDITOR.update_atm_replenishment", "", "updateRepl", parameters);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(0, "ATM_DEP_REPLENISHMENT_EDITOR.update_atm_replenishment", "", "updateRepl", parameters);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ATM_DEP_REPLENISHMENT_EDITOR.update_atm_replenishment","", "updateRepl",parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    for (int i = 7; i <= 39;i++ )
        //        e.Row.Cells[i].Visible = false;
        //}
    }

    private string getNumber(string amount)
    {
        return amount.Replace(",", "");
    }

    private void clear()
    {
        datePrev.date = "";
        lblPrevReplDate.Text = "";
        txtPreTime.Text = "";
        dateCurrent.date = "";
        lblReplDate.Text = "";
        txtRepTime.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        ddlStatus.SelectedIndex = 0;
        TextBox10.Text = "";
        TextBox11.Text = "";
        TextBox12.Text = "";
        TextBox13.Text = "";
        TextBox14.Text = "";
        TextBox15.Text = "";
        TextBox16.Text = "";
        TextBox17.Text = "";
        TextBox18.Text = "";
        TextBox19.Text = "";
        TextBox1.Text = "";
        TextBox3.Text = "";
        ddlModifyReason.SelectedIndex = 0;
        chkFromBase24.Checked = false;
        lblClAdd.Text = "";
        lblBANCS.Text = "";
        TextBox20.Text = "";
        TextBox21.Text = "";
        TextBox22.Text = "";
        txtATMNumberAdd.Text = "";
        lblUser.Text = "";
        lblUserDate.Text = "";
        dateB24PrevDate.date = "";
        dateB24Date.date = "";
        txtB24Time.Text = "";
        txtPrevB24Time.Text = "";

    }
}
