using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Globalization;
using System.Data.SqlClient;
using System.Text;

public partial class Business_ManualGLEntry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadStatus();
            loadCurrency();
            //loadValues();
            ddlRecordStatus.SelectedValue = "007";
            ViewState["mode"] = "d";
            loadATMListOnPageLoad(Helper.gridSize(), 1);
            txtGLAccountNo.Attributes.Add("onKeyPress", "return KeyPressFuncIsWholeNumber();");
        }
        
        
        lblError.Text = "";
        
       // btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");

    }
    private void loadValues()
    {
        if (Request.QueryString["atmno"] != null)
        {
            //UserControl uc = Page.Master.FindControl("Menu1") as UserControl;
            //uc.Visible = false;
            Page.Master.FindControl("Menu").Visible = false;
            Page.Master.FindControl("tblHeader").Visible = false;
            /*
            UserControl uc = (UserControl)FindControl("DatePicker");
            TextBox TextBox1 = (TextBox)uc.FindControl("txtDate");
            TextBox1.Enabled = false;
             * */
            //dtReplDate.Visible = false;
            //txtReplDate.Visible = true;
            //txtReplDate.Enabled = false;
            txtTerminalID.Text = Request.QueryString["atmno"];
            txtTerminalID.Enabled = false;
            //txtReplTime.Enabled = false;
            //txtGLAccountNo.Enabled = false;
            //if (Request.QueryString["branchNo"] != null)
            //{
            //    loadBranches();
            //    ddlBranch.SelectedValue = Request.QueryString["branchNo"];
            //}

            if (Request.QueryString["replDate"] != null)
            {
                txtReplDate.Visible = false;
                dtReplDate.Visible = true;
                //txtReplDate.Text = Request.QueryString["replDate"];
                
                string d, m, y;
                d = Request.QueryString["replDate"].Substring(4,2);
                m = Request.QueryString["replDate"].Substring(2, 2);
                y = Request.QueryString["replDate"].Substring(0, 2);
                dtReplDate.date =d+"-"+m+"-"+"20"+y;
                 
            }

            if (Request.QueryString["replTime"] != null)
                txtReplTime.Text = Request.QueryString["replTime"];

            if (Request.QueryString["atmType"] != null)            
                ddlReplType.SelectedItem.Text = Request.QueryString["atmType"];            

            if (Request.QueryString["replCurrency"] != null)
                ddlCurrency.SelectedItem.Text = Request.QueryString["replCurrency"];

            if (Request.QueryString["glAcctNo"] != null)
                txtGLAccountNo.Text = Request.QueryString["glAcctNo"];

            if (Request.QueryString["openingBalance"] != null)
            {
                
                txtOpeningBalance.Text = Request.QueryString["openingBalance"];
                //if (double.Parse(Request.QueryString["openingBalance"]) > 0)
                //    txtOpeningBalance.Enabled = false;
                //else
                //    txtOpeningBalance.Enabled = true;
            }

            if (Request.QueryString["closingBalance"] != null)
            {
                txtClosingBalance.Text = Request.QueryString["closingBalance"];
                //if (double.Parse(Request.QueryString["closingBalance"]) > 0)
                //    txtClosingBalance.Enabled = false;
                //else
                //    txtClosingBalance.Enabled = true;
            }

            btnApprove.Visible = false;
            panel1.Visible = false;
            pnlControls.Visible = true;
        }
        else
        {
            panel1.Visible = true;
            pnlControls.Visible = false;
        }
    }
    //private void loadBranches()
    //{
    //    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);

    //    ddlBranch.DataSource = ds;
    //    ddlBranch.DataBind();

    //    ddlBranch.Items.Insert(0, "------ Branches ------");
    //    oracon.Close();
    //}

    //private void loadStatus()
    //{
    //    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);

    //    ddlReplType.DataSource = ds;
    //    ddlReplType.DataBind();

    //    oracon.Close();
    //}

    //protected void btnSave_Click(object sender, EventArgs e)
    //{
    //    string error = "";
       
        
    //    if (double.Parse(getNumber(txtOpeningBalance.Text)) > 0 || double.Parse(getNumber(txtClosingBalance.Text)) > 0)
    //    {
    //        if (dtReplDate.date.Length > 0)
    //        {
    //            if (txtReplTime.Text.Length > 0)
    //            {
    //                if (txtGLAccountNo.Text.Length > 0)
    //                {
    //                    btnSave.Enabled = false;
    //                    bool saved = addBancsRepl(ref error);
    //                    if (saved)
    //                    {
    //                        lblError1.Visible = true;
    //                        lblError1.Text = "Manual GL Entry Saved!!";
    //                        //Session["Msg"] = lblError1.Text;
    //                        //Label lbl = ((Label)PreviousPage.FindControl("lblError1"));
    //                        //lbl.Text = lblError1.Text;
    //                        ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);

    //                    }
    //                    else
    //                    {
    //                        lblError1.Visible = true;
    //                        lblError1.Text = error;
    //                    }
    //                }
    //                else
    //                {
    //                    lblError1.Visible = true;
    //                    lblError1.Text = "Please enter Account No.";
    //                }
    //            }
    //            else
    //            {
    //                lblError1.Visible = true;
    //                lblError1.Text = "Please enter Replenishment Time";
    //            }
    //        }
    //        else
    //        {
    //            lblError1.Visible = true;
    //            lblError1.Text = "Please enter Replenishment Date";
    //        }
    //    }
    //    else
    //    {
    //        lblError1.Visible = true;
    //        lblError1.Text = "Either Opening Balance or Closing Balance greater than zero";
    //    }
    //}
    private bool addBancsRepl(ref string error)
    {
        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_summary_viewer.add_manual_gl_entry";

        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_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_time", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_cur", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gl_account", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_opening_balance", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_closing_balance", 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["p_atm_num"].Value = txtTerminalID.Text.Substring(txtTerminalID.Text.Length - 4);    
        oracom.Parameters["p_repl_date"].Value = dtReplDate.date;
       // oracom.Parameters["p_repl_date"].Value = txtReplDate.Text;
        oracom.Parameters["p_repl_time"].Value = txtReplTime.Text.Replace(":", "");
        oracom.Parameters["p_atm_type"].Value = ddlReplType.SelectedValue;
        oracom.Parameters["p_repl_cur"].Value = int.Parse(ddlCurrency.SelectedValue);
        oracom.Parameters["p_gl_account"].Value = txtGLAccountNo.Text;

        if (getNumber(txtOpeningBalance.Text).Length > 0)
            oracom.Parameters["p_opening_balance"].Value = getNumber(txtOpeningBalance.Text);
        else
            oracom.Parameters["p_opening_balance"].Value = OracleNumber.Null;

        if (getNumber(txtClosingBalance.Text).Length > 0)
            oracom.Parameters["p_closing_balance"].Value = getNumber(txtClosingBalance.Text);
        else
            oracom.Parameters["p_closing_balance"].Value = OracleNumber.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")
            return true;
        else
            return false;
    }
    private void loadCurrency()
    {
        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();
    }
    private string getNumber(string amount)
    {
        if (amount.Length > 0)
            return amount.Replace(",", "");
        else
            return "0";
    }
    protected void btnIgnore_Click(object sender, EventArgs e)
    {

    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        string error = "";
        bool saved = false;

        saved = Approve( ref error);
        if (saved)
        {
            lblError.ForeColor = System.Drawing.Color.DarkGreen;
            lblError.Text = "Approved Successful";
            lnkAtmCashReconSummary.Visible = true;
        }
        else
        {
            lblError.Text = error;
        }
        pnlControls.Visible = false;
        if (ViewState["mode"].Equals("d"))
            loadATMListOnPageLoad(Helper.gridSize(), 1);
        else
            loadATMList(Helper.gridSize(), 1);
    }
    private bool Approve(ref string error)
    {
        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_summary_viewer.approve_banc_atm_rep";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        
        oracom.Parameters.Add("p_atm_no", 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_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_cur", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gl_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_opening_balance", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_closing_balance", 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["p_status"].Value = ViewState["status"].ToString();
        oracom.Parameters["p_atm_no"].Value = txtTerminalID.Text.Substring(txtTerminalID.Text.Length - 4);
        //ViewState["atm_no"] = txtTerminalID.Text.Substring(txtTerminalID.Text.Length - 4);
        oracom.Parameters["p_repl_date"].Value = ViewState["repl_date"].ToString();
        oracom.Parameters["p_repl_time"].Value = txtReplTime.Text.Replace(":", "");
        //ViewState["repl_time"] = txtReplTime.Text.Replace(":", "");
        if (ddlReplType.SelectedItem.Text.Contains("Deposit"))
        {
            oracom.Parameters["p_atm_type"].Value = "030";
            ViewState["atm_type"] = "030";
        }
        else
        {
            oracom.Parameters["p_atm_type"].Value = "009";
            ViewState["atm_type"] = "009";
        }
        //oracom.Parameters["p_atm_type"].Value = ddlReplType.SelectedItem.Value;
        ViewState["atm_type"] = ddlReplType.SelectedItem.Value;
        oracom.Parameters["p_repl_cur"].Value = ddlCurrency.SelectedValue;
        oracom.Parameters["p_gl_acct"].Value = txtGLAccountNo.Text;
        oracom.Parameters["p_opening_balance"].Value = getNumber ( txtOpeningBalance.Text);
        oracom.Parameters["p_closing_balance"].Value =getNumber( txtClosingBalance.Text);

        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")
            return true;
        else
            return false;
    }
    private bool Reject(string index, ref string error)
    {
        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_summary_viewer.reject_banc_atm_rep";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_bard_id", 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["p_bard_id"].Value = index;
        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")
            return true;
        else
            return false;
    }
    private bool Ignore(string index, ref string error)
    {
        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_summary_viewer.ignore_gl_entry";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_bard_id", 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["p_bard_id"].Value = int.Parse(index);
        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")
            return true;
        else
            return false;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
         if (Request.QueryString["atmno"] != null)
             Page.ClientScript.RegisterStartupScript(this.GetType(), "ManualGLEntry", "window.close()", true);
         else
            pnlControls.Visible = false;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "s";
        if (dtSearchReplDate.date.Length > 0)
            loadATMList(Helper.gridSize(), 1);
        else
            lblError.Text = "Please enter Replenishment Date";
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ViewState["mode"].Equals("s"))
            loadATMList(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        else
            loadATMListOnPageLoad(Helper.gridSize(),int.Parse(ddlPages.SelectedValue));
    }
    private void loadATMList(int pageSize, int pageNumber)
    {
        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_SUMMARY_VIEWER.search_atm_banc_rep";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_atm_no", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_rec_status", 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;

        if(dtSearchReplDate.date.Length>0)
            oracom.Parameters["p_repl_date"].Value = dtSearchReplDate.date;
        else
            oracom.Parameters["p_repl_date"].Value = OracleString.Null;

        if(ddlSearchRecordType.SelectedIndex !=-1)
            oracom.Parameters["p_repl_type"].Value = ddlSearchRecordType.SelectedValue;
        else
            oracom.Parameters["p_repl_type"].Value = OracleString.Null;

        if(txtATMNo.Text.Trim().Length !=0)
            oracom.Parameters["p_atm_no"].Value = txtATMNo.Text.Trim();
        else
            oracom.Parameters["p_atm_no"].Value = OracleString.Null;

        if(ddlRecordStatus.SelectedIndex!=-1)
            oracom.Parameters["p_rec_status"].Value = ddlRecordStatus.SelectedValue; 
        else
            oracom.Parameters["p_rec_status"].Value = OracleString.Null;

        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());
            }
        }
    }
    private void loadATMListOnPageLoad(int pageSize, int pageNumber)
    {
        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_SUMMARY_VIEWER.search_atm_banc_rep";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_atm_no", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_rec_status", 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;

        //if (dtSearchReplDate.date.Length > 0)
        //    oracom.Parameters["p_repl_date"].Value = dtSearchReplDate.date;
        //else
            oracom.Parameters["p_repl_date"].Value = OracleString.Null;

        //if (ddlSearchRecordType.SelectedIndex != -1)
        //    oracom.Parameters["p_repl_type"].Value = ddlSearchRecordType.SelectedValue;
        //else
            oracom.Parameters["p_repl_type"].Value = OracleString.Null;

        //if (txtATMNo.Text.Trim().Length != 0)
        //    oracom.Parameters["p_atm_no"].Value = txtATMNo.Text.Trim();
        //else
            oracom.Parameters["p_atm_no"].Value = OracleString.Null;

        //if (ddlRecordStatus.SelectedIndex != -1)
        //    oracom.Parameters["p_rec_status"].Value = ddlRecordStatus.SelectedValue;
        //else
            oracom.Parameters["p_rec_status"].Value = "007";

        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());
            }
        }
    }
    private void loadStatus()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "poslookup_getter.get_banc_repl_status";

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

      

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

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

       

    }

    private void loadOpeningClosingAmount(string atmNo,string replDate,string atmType)
    {
        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_summary_viewer.get_opening_closing_amt";

        oracom.Parameters.Add("p_atm_no", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_repl_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

        oracom.Parameters["p_atm_no"].Value = atmNo;
        oracom.Parameters["p_repl_date"].Value = replDate;
        oracom.Parameters["p_atm_type"].Value = atmType;

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

        txtOpeningBalance.Text = ds.Tables[0].Rows[0]["v_opening_balance"].ToString();
        txtClosingBalance.Text = ds.Tables[0].Rows[0]["v_closing_balance"].ToString();




    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("a"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string branchCode = GridView1.Rows[index].Cells[0].Text.Trim();
            string atmNo = GridView1.Rows[index].Cells[1].Text.Trim();
            string recordType = GridView1.Rows[index].Cells[2].Text.Trim();            
            string replDate = GridView1.Rows[index].Cells[4].Text.Trim();
            string replTime = GridView1.Rows[index].Cells[5].Text.Trim();
            string openingBalance = GridView1.Rows[index].Cells[6].Text.Trim();
            string closingBalance = GridView1.Rows[index].Cells[7].Text.Trim();
            string recordStatus = GridView1.Rows[index].Cells[8].Text.Trim();
            string glAcctNo = GridView1.Rows[index].Cells[9].Text.Trim();
            string atmNumber=GridView1.Rows[index].Cells[13].Text.Trim();
            string replenishmentDate = GridView1.Rows[index].Cells[14].Text.Trim();
            string status = GridView1.Rows[index].Cells[15].Text.Trim();
            string atmType = GridView1.Rows[index].Cells[16].Text.Trim();
            string replCurrency = GridView1.Rows[index].Cells[17].Text.Trim();
            string glRetract = GridView1.Rows[index].Cells[18].Text.Trim(); ;

            ViewState["status"] = status;
            
            if (!recordStatus.ToLower().Equals("auto-entered"))
            {
                if (!recordStatus.ToLower().Equals("approved"))
                {
                    if (!recordStatus.ToLower().Equals("rejected"))
                    {
                        DisableControls();
                        pnlControls.Visible = true;
                        // loadBranches();
                       // btnSave.Visible = false;
                        ViewState["bardId"] = rowID;


                        if (!atmNo.Equals("&nbsp;"))
                            txtTerminalID.Text = atmNo;
                        else
                            txtTerminalID.Text = "";


                        if (!replDate.Equals("&nbsp;"))
                            txtReplDate.Text = replDate;
                        else
                            txtReplDate.Text = "";

                        if (!replenishmentDate.Equals("&nbsp;"))
                            ViewState["repl_date"] = replenishmentDate;
                        else
                            dtReplDate.date = "";

                        if (!replTime.Equals("&nbsp;"))
                            txtReplTime.Text = replTime;
                        else
                            txtReplTime.Text = "";

                        if (!recordType.Equals("&nbsp;"))
                            ddlReplType.SelectedItem.Text = recordType;
                        else
                            ddlReplType.SelectedIndex = -1;

                        if (!replCurrency.Equals("&nbsp;"))
                        {
                            //txtReplCurrency.Text = replCurrency;
                            ddlCurrency.SelectedValue = replCurrency;
                        }
                        else
                        {
                            ddlCurrency.SelectedIndex = -1;
                            //txtReplCurrency.Text = "";
                        }

                        if (!glAcctNo.Equals("&nbsp;"))
                            txtGLAccountNo.Text = glAcctNo;
                        else
                            txtGLAccountNo.Text = "";

                        if (recordType.Equals("Deposit"))
                        {
                            txtGlRetract.Visible = false;
                            lbl_Retract.Visible = false;

                        }
                         else
                        {
                            txtGlRetract.Visible = true;
                            lbl_Retract.Visible = true;

                            if (!glRetract.Equals("&nbsp;"))
                                txtGlRetract.Text = glRetract;
                            else
                                txtGlRetract.Text = "";
                        }
                        /* if (!openingBalance.Equals("&nbsp;"))
                             txtOpeningBalance.Text = openingBalance;
                         else
                             txtOpeningBalance.Text = "";
               
                         if (!closingBalance.Equals("&nbsp;"))
                             txtClosingBalance.Text = closingBalance;
                         else
                             txtClosingBalance.Text = "";*/

                        loadOpeningClosingAmount(atmNumber, replenishmentDate,atmType);
                    }
                    else
                    {
                        lblError.Text = "Rejected Entry can't be Approved";
                        pnlControls.Visible = false;
                    }

                }
                else
                {
                    lblError.Text = "Already Approved";
                    pnlControls.Visible = false;
                }
            }
            else
            {
                lblError.Text = "Only Pre-Approved Entry can be Approved";
                pnlControls.Visible = false;
            }

        }
        else if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string recordStatus = GridView1.Rows[index].Cells[8].Text.Trim();
            if (recordStatus.ToLower().Equals("pre-approved"))
            {
                if (!recordStatus.ToLower().Equals("ignored"))
                {
                    if (!recordStatus.ToLower().Equals("rejected"))
                    {
                        if (!recordStatus.ToLower().Equals("approved"))
                        {
                            string error = "";
                            bool saved = Reject(rowID, ref error);
                            if (saved)
                            {
                                lblError.ForeColor = System.Drawing.Color.DarkGreen;
                                lblError.Text = "Rejected Successful";
                            }
                            else
                            {
                                lblError.Text = error;
                            }
                        }
                        else
                            lblError.Text = "Approved Entry can't be Rejected";
                    }
                    else
                        lblError.Text = "Rejected Already";
                }
                else
                    lblError.Text = "Ignored Entry Can't be Rejected";
            }
            else
                lblError.Text = "Only Pre-Approved Entry can be Rejected";
        }
        else if (e.CommandName.Equals("i"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string recordStatus = GridView1.Rows[index].Cells[8].Text.Trim();
            if (recordStatus.ToLower().Equals("auto-entered"))
            {
                
                    string error = "";
                    bool saved = Ignore(rowID, ref error);
                    if (saved)
                    {
                        lblError.ForeColor = System.Drawing.Color.DarkGreen;
                        lblError.Text = "Ignored Successful";
                    }
                    else
                    {
                        lblError.Text = error;
                    }
                
            }
            
        }
        loadATMList(Helper.gridSize(), 1);
    }
    private void DisableControls()
    {
        txtTerminalID.Enabled = false;
        txtReplDate.Enabled = false;
       // ddlBranch.Enabled = false;
        txtReplTime.Enabled = false;
        ddlReplType.Enabled = false;
        ddlCurrency.Enabled = false;
        txtGLAccountNo.Enabled = false;
        txtOpeningBalance.Enabled = false;
        txtClosingBalance.Enabled = false;
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[13].Visible = false;
            e.Row.Cells[14].Visible = false;
            e.Row.Cells[15].Visible = false;
            e.Row.Cells[16].Visible = false;
            e.Row.Cells[17].Visible = false;
            e.Row.Cells[18].Visible = false; //Retract_Amt

            //int KeyValueToSearch = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
            //foreach (TableCell tc in e.Row.Cells)
            //{
            //    if (tc.Text == KeyValueToSearch.ToString())
            //    {
            //        tc.Visible = false;


            //    }
            //}

            //e.Row.Cells[8].Text.ToLower().Trim().Equals("auto-entered");
               
        }
    }
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static List<string> GetCompletionList(string prefixText, int count)
    {
        return AutoFillAtmNo(prefixText);

    } 
    private static List<string> AutoFillAtmNo(string prefixText)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.Text;
        oracom.CommandText = "select atm_number from atm_data_table where status=1 and atm_number like '" + prefixText + "%' ";

        //oracom.Parameters.Add("p_value",OracleType.VarChar).Direction=ParameterDirection.Input;
       

        //oracom.Parameters["p_value"].Value = prefixText;

                List<string> atmList = new List<string>();
                OracleDataReader odr = oracom.ExecuteReader();
                
                    while (odr.Read())
                    {
                        atmList.Add(odr["atm_number"].ToString());
                    }

                    oracon.Close();
                return atmList;
         
    }
    protected void txtATMNo_TextChanged(object sender, EventArgs e)
    {

    }
    protected void lnkAtmCashReconSummary_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        
        sb.Append("repl_date=" + ViewState["repl_date"] + "&");
        sb.Append("atm_type=" + ViewState["atm_type"] + "&");
        string queryString = sb.ToString();
        queryString = queryString.TrimEnd('&');
        Response.Redirect("ATMCashReconS.aspx?" + queryString);
    }
}