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 NCBEAI.Adapters.Transport;
using System.Xml;
using System.Xml.Serialization;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Net.Security;


public partial class Business_GLPosting : System.Web.UI.Page
{
    //static int intCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        btnSavePost.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSavePost, null) + ";");
        if (!IsPostBack)
        {
            loadChannels();
            loadEvents();
            loadAccType();
            loadBranches();
            loadATMTypes();
            loadChannelTypes();
            loadSAMAAccountType();
            channelsRow.Visible = false;
            ATMNoRow.Visible = false;
            ATMTypeRow.Visible = false;
            BranchRow.Visible = false;
            POSRow.Visible = false;
            POSRow2.Visible = false;
            ClaimRow.Visible = false;
            ClaimRow2.Visible = false;
            btnPostAll.Visible = false;
            btnGenReport.Visible = false;
            samaAccountType.Visible = false;
            AutoApproveRow.Visible = false;
            btnSavePost.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnSavePost).ToString());
            ExceptionUtility.LogException(0, "", "12", "Page_Load", "");
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
                rdDiscovery.Visible = false;
        }
        lblError.Text = "";

        if (Session["BtnPostSystemApproved"] as string == "Y")
            BtnPostSystemApproved.Enabled = true;
    }

    private void loadChannels()
    {
        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_Recon_Channels";

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

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

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


            ddlChannels.Items.Insert(0, "------ Channels ------");
            oracon.Close();
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            ddlChannels.Items.Remove(ddlChannels.Items.FindByValue(dgn));
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Channels", "", "loadChannels", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadEvents()
    {
        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_Recon_Events";

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

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

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

            ddlEvents.Items.Insert(0, "------ Events ------");
            ddlEvents.Items.Insert(1, new ListItem("ALL Events", "-1"));
            oracon.Close();
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
            {
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("41"));
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("42"));
                ddlEvents.Items.Remove(ddlEvents.Items.FindByValue("43"));
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Events", "", "loadEvents", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadAccType()
    {
        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_Post_Acct_Type";

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

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

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

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

    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);

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

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

    private void loadATMTypes()
    {
        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_ATM_Types";

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

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

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

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

            ddlATMType.Items.Insert(0, "------ ATM Type ------");
            oracon.Close();
        }

        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_ATM_Types", "", "loadATMTypes", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadChannelTypes()
    {
        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_Channel_Types";

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

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

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

            ddlType.Items.Insert(0, "------- ALL -------");
            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Channel_Types", "", "loadChannelTypes", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(string ChannelID, string EventID, string BranchID, string Approved, string AutoSystemApproved, string Posted, string Declined, int pageSize, int pageNumber)
    {
        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 = "GL_POSTING_APPROVER.Get_GL_Entities";


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_branch", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_no", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_client_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_claim_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_card_no", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_trace_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_approved", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_posted", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_decline", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_date", OracleType.VarChar, 250).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_acct_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_ch_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_claim_src", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_sama_acct_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_product", OracleType.Number).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 (ChannelID.Length != 0)
            {
                parameters = "p_rc_code-" + ChannelID;
                oracom.Parameters["p_rc_code"].Value = ChannelID;
            }
            else
                oracom.Parameters["p_rc_code"].Value = OracleNumber.Null;

            if (EventID.Length != 0)
            {
                parameters = parameters + ":p_re_code-" + EventID;
                oracom.Parameters["p_re_code"].Value = EventID;
            }
            else
            {
                oracom.Parameters["p_re_code"].Value = OracleNumber.Null;
            }

            if (BranchID.Length != 0)
            {
                parameters = parameters + ":p_branch-" + BranchID;
                oracom.Parameters["p_branch"].Value = BranchID;
            }
            else
                oracom.Parameters["p_branch"].Value = OracleString.Null;


            if (ddlATMType.SelectedIndex != 0)
            {
                parameters = parameters + ":p_atm_type-" + ddlATMType.SelectedItem.Text;
                oracom.Parameters["p_atm_type"].Value = ddlATMType.SelectedValue;
            }
            else
                oracom.Parameters["p_atm_type"].Value = OracleString.Null;


            if (rdEvent.Checked)
            {
                if (ddlType.SelectedIndex == 0)
                    oracom.Parameters["p_ch_type"].Value = OracleNumber.Null;
                else
                {
                    parameters = parameters + ":p_ch_type-" + ddlType.SelectedItem.Text;
                    oracom.Parameters["p_ch_type"].Value = ddlType.SelectedValue;
                }
            }

            if (rdChannel.Checked)
            {
                parameters = parameters + ":p_ch_type-3";
                oracom.Parameters["p_ch_type"].Value = "3";
            }
            if (rdATM.Checked)
            {
                parameters = parameters + ":p_ch_type-5";
                oracom.Parameters["p_ch_type"].Value = "5";
            }
            if (rdPOSSET.Checked)
            {
                parameters = parameters + ":p_ch_type-6";
                oracom.Parameters["p_ch_type"].Value = "6";
            }
            if (rdPOSRENT.Checked)
                oracom.Parameters["p_ch_type"].Value = "7";
            if (rdClaims.Checked)
            {
                oracom.Parameters["p_ch_type"].Value = "8";
                parameters = parameters + ":p_ch_type-8";
            }
            if (rdSAMAMonthlySettlement.Checked)
            {
                oracom.Parameters["p_ch_type"].Value = "9";
                parameters = parameters + ":p_ch_type-9";
            }
            if (rdClaims.Checked)
            {
                oracom.Parameters["p_card_no"].Value = txtCardNo.Text;
                oracom.Parameters["p_trace_id"].Value = txtTraceID.Text;
                oracom.Parameters["p_claim_id"].Value = txtClaimID.Text;
                parameters = parameters + ":p_card_no-" + Helper.Mask_CardNumber(txtCardNo.Text) + ":p_trace_id-" + txtTraceID.Text + ":p_claim_id-" + txtClaimID.Text;
            }
            else
            {
                oracom.Parameters["p_card_no"].Value = OracleString.Null;
                oracom.Parameters["p_trace_id"].Value = OracleString.Null;
                oracom.Parameters["p_claim_id"].Value = OracleString.Null;
            }

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

            if (txtTerminalID.Text.Length > 0)
            {
                parameters = parameters + ":p_term_id-" + txtTerminalID.Text;
                oracom.Parameters["p_term_id"].Value = txtTerminalID.Text;
            }
            else
                oracom.Parameters["p_term_id"].Value = OracleString.Null;

            if (rdATMClaim.Checked)
            {
                oracom.Parameters["p_claim_src"].Value = "1";
                parameters = parameters + ":p_claim_src-1";
            }
            else if (rdPOSClaim.Checked)
            {
                oracom.Parameters["p_claim_src"].Value = "2";
                parameters = parameters + ":p_claim_src-2";
            }
            else 
            {
                oracom.Parameters["p_claim_src"].Value = "3";
                parameters = parameters + ":p_claim_src-3";
            }
            if (rdSAMAMonthlySettlement.Checked)
            {
                if (ddlSAMAAccountType.SelectedIndex > 0)
                    oracom.Parameters["p_sama_acct_type"].Value = ddlSAMAAccountType.SelectedValue;
                else
                    oracom.Parameters["p_sama_acct_type"].Value = OracleNumber.Null;
                parameters = parameters + ":p_sama_acct_type-" + ddlSAMAAccountType.SelectedItem.Text;

            }
            else
                oracom.Parameters["p_sama_acct_type"].Value = OracleNumber.Null;

            if (rdPOSSETL.Checked)
            {
                oracom.Parameters["p_product"].Value = 2;
                parameters = parameters + ":p_product-2" ;
            }
            else if (rdECOMSETL.Checked)
            {
                oracom.Parameters["p_product"].Value = 3;
                parameters = parameters + ":p_product-3";
            }
            else
            {
                oracom.Parameters["p_product"].Value = 4;
                parameters = parameters + ":p_product-4";
            }

            oracom.Parameters["p_acct_type"].Value = ddlAccType.SelectedValue; oracom.Parameters["p_atm_no"].Value = txtATMnumber.Text;
            oracom.Parameters["p_approved"].Value = Approved;
            oracom.Parameters["p_posted"].Value = Posted;
            oracom.Parameters["p_decline"].Value = Declined;
            if (rdSAMAMonthlySettlement.Checked)
                oracom.Parameters["p_date"].Value = DatePicker1.date.Substring(0, 4) + "01";
            else
                oracom.Parameters["p_date"].Value = DatePicker1.date;
            parameters = parameters + ":p_acct_type-" + ddlAccType.SelectedItem.Text + ":p_atm_no-" + txtATMnumber.Text + ":p_approved-" + Approved + ":p_posted-" + Posted + ":p_decline-" + Declined + ":p_date-" + DatePicker1.date;
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            oracom.Parameters["p_lang"].Value = "EN";

            if (AutoSystemApproved == "1" && Posted == "0")
                oracom.Parameters["p_current_uid"].Value = 0;
            else
                oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

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

            if (ViewState["PostAll"] as string == "Y") // IRFAN_MOD
                GridView1.PageSize = Helper.MaxPostSize();
            else
                GridView1.PageSize = Helper.gridSize();

            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());
                }
            }
            ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Get_GL_Entities", "145.1.1", "Approve", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Get_GL_Entities", "145.1.1", "Approve", parameters);//ChannelID: " + ChannelID + ", EventID: " + EventID + ", BranchID: " + BranchID + ", Approved: " + Approved + ", Posted: " + Posted + ", Declined: " + Declined + ", pageSize: " + pageSize + ", pageNumber: " + pageNumber);
            GridView1.DataSource = null;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //intCount = 0;

        txtATMnumber.Text = txtATMnumber.Text.Trim();
        txtClientNo.Text = txtClientNo.Text.Trim();
        txtTerminalID.Text = txtTerminalID.Text.Trim();
        txtCardNo.Text = txtCardNo.Text.Trim();
        txtTraceID.Text = txtTraceID.Text.Trim();
        txtClaimID.Text = txtClaimID.Text.Trim();

        pnlResult.Visible = false;

        if (ddlChannels.SelectedIndex == 0 && rdChannel.Checked)
        {
            ExceptionUtility.LogException(9, "", "12.145", "Button1_Click", "");
            lblError.Text = "You must choose a Channel";
        }
        else if (ddlEvents.SelectedIndex == 0 && rdEvent.Checked)
        {
            ExceptionUtility.LogException(9, "", "12.145", "Button1_Click", "");
            lblError.Text = "You must choose an Event";
        }

        else if (DatePicker1.date.Length == 0 && rdPOSRENT.Checked)
            lblError.Text = "You must enter Date";
        
        else if (DatePicker1.date.Length == 0)
        {
            ExceptionUtility.LogException(9, "", "12.145", "Button1_Click", "");
            lblError.Text = "You must enter Date";
        }
        else
        {
            ExceptionUtility.LogException(9, "", "12.145", "Button1_Click", "");
            getSetup();
        }


        // btnPostAll.Visible = rdATM.Checked || rdClaims.Checked || rdChannel.Checked || rdEvent.Checked || rdPOSSET.Checked;
        //if (chkPosted.Checked)
        //    btnPostAll.Visible = false;
        //else
        //    btnPostAll.Visible = true;

        //if (btnPostAll.Visible)
        //    btnGenReport.Visible = true;
        //else
        //   

        pnlControls.Visible = false;
        pnlPost.Visible = false;
    }

    private void getSetup()
    {
        string channelID = "", EventID = "", BranchID = "", approved = "", posted = "", declined = "", AutoSystemApproved = "";

        
        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        if (ddlBranch.SelectedIndex > 0)
            BranchID = ddlBranch.SelectedValue;



        if (chkApproved.Checked)
            approved = "1";
        else
            approved = "0";

        // IRFAN_MOD 150415
        if (chkAutoSystemApproved.Checked)
        {
            approved = "1";
            AutoSystemApproved = "1";
        }
        else
        {
            AutoSystemApproved = "0";
            // approved = "0";
        }

        if (chkPosted.Checked)
            posted = "1";
        else
            posted = "0";

        if (chkDeclined.Checked)
            declined = "1";
        else
            declined = "0";
        pnlResult.Visible = true;
        pnlControls.Visible = false;
        pnlResult.Enabled = true;

        GridView1.Columns[10].Visible = true;
        GridView1.Columns[11].Visible = true;

        if (rdSAMAMonthlySettlement.Checked)
        {
            GridView1.Columns[0].Visible = false;
            GridView1.Columns[1].Visible = false;
            //GridView1.Columns[3].Visible = false ;
            //GridView1.Columns[4].Visible = true;

        }

        if (rdATM.Checked)
        {
            GridView1.Columns[3].Visible = false;
            GridView1.Columns[4].Visible = true;
        }
        else
        {
            GridView1.Columns[4].Visible = false;
            GridView1.Columns[3].Visible = true;
        }
        ExceptionUtility.LogException(0, "", "145.1", "getSetup", "");

        if (ViewState["PostAll"] as string == "Y")
            loadResult(channelID, EventID, BranchID, approved, AutoSystemApproved, posted, declined, Helper.MaxPostSize(), 1);
        else
            loadResult(channelID, EventID, BranchID, approved, AutoSystemApproved, posted, declined, Helper.gridSize(), 1); ;

        if (!rdEvent.Checked)
        {
            GridView1.Columns[10].Visible = false;
            GridView1.Columns[11].Visible = false;
        }

        if (rdPOSSET.Checked)
        {
            if (txtClientNo.Text.Length == 0)
            {
                btnApproveAll.Visible = true;
                //btnPostAll.Visible = true;
            }
            else
            {
                btnApproveAll.Visible = false;
                //btnPostAll.Visible = false;
            }
        }
        else
        {
            btnApproveAll.Visible = false;
            //btnPostAll.Visible = false;
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        string channelID = "", EventID = "", BranchID = "", approved = "", posted = "", declined = "", AutoSystemApproved = "";

        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        if (ddlBranch.SelectedIndex > 0)
            BranchID = ddlBranch.SelectedValue;


        if (chkApproved.Checked)
            approved = "1";
        else
            approved = "0";

        // IRFAN_MOD 150415
        if (chkAutoSystemApproved.Checked)
        {
            approved = "1";
            AutoSystemApproved = "1";
        }
        else
        {
            AutoSystemApproved = "0";
            //approved = "0";
        }

        if (chkPosted.Checked)
            posted = "1";
        else
            posted = "0";
        if (chkDeclined.Checked)
            declined = "1";
        else
            declined = "0";
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
        loadResult(channelID, EventID, BranchID, approved, AutoSystemApproved, posted, declined, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        pnlControls.Visible = false;
        pnlPost.Visible = false;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //string post_amount = "";
        //string post_drAccount = "";
        //string post_crAccount = "";
        //string post_narrative1 = "";
        //string post_narrative2 = "";
        //string post_narrative3 = "";
        if (e.CommandName.Equals("a"))
        {
            ExceptionUtility.LogException(0, "", "12.13", "GridView1_RowCommand.approve", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            //can't approve a declined claim
            string declined = GridView1.Rows[index].Cells[8].Text.ToString();
            string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            string error = "";
            if (declined.ToLower().Equals("no"))
            {
                if (!approved.ToLower().Equals("yes"))
                {

                    bool saved = Approve(rowID, ref error);
                    if (saved)
                    {
                        lblError.Text = "Entity Approved";
                    }
                    else
                    {
                        lblError.Text = error;
                    }
                }
                else
                {
                    lblError.Text = "Entry is already approved";
                }
            }
            else
            {
                lblError.Text = "Cannot approve a declined entry";
            }
        }
        else if (e.CommandName.Equals("d"))
        {
            ExceptionUtility.LogException(0, "", "12.71", "GridView1_RowCommand.Decline", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            //can't post a claim until it's approved
            string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            string declined = GridView1.Rows[index].Cells[8].Text.ToString();
            if (approved.ToLower().Equals("no"))
            {
                if (!declined.ToLower().Equals("yes"))
                {
                    bool saved = Decline(rowID, ref error);
                    if (saved)
                    {
                        lblError.Text = "Entity Declined";
                    }
                    else
                    {
                        lblError.Text = error;
                    }
                }
                else
                {
                    lblError.Text = "Entry already declined";
                }
            }
            else
            {
                lblError.Text = "Cannot decline an approved entry";
            }
        }
        else if (e.CommandName.Equals("p"))
        {
            //intCount = 0;
            ExceptionUtility.LogException(0, "", "12.15", "GridView1_RowCommand.Post", "");
            //UMER_MOD 140721 starts 
            /*
            if (rdATM.Checked || rdChannel.Checked)
            {
                lblError.Text = "Post is not allowed for single transaction" +
                Environment.NewLine + "Please press POST All to do the posting";
                
            }
             
            else  if (rdClaims.Checked || rdSAMAMonthlySettlement.Checked)
             */

            if (rdClaims.Checked || rdSAMAMonthlySettlement.Checked || rdATM.Checked || rdPOSSET.Checked || rdChannel.Checked || rdEvent.Checked)
            //UMER_MOD 140721 ends 
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string gpe_code = GridView1.DataKeys[index].Value.ToString();
                //UMER_MOD 140621 starts
                string approved = GridView1.Rows[index].Cells[7].Text.ToString();
                string posted = GridView1.Rows[index].Cells[6].Text.ToString();
                string cashacc = GridView1.Rows[index].Cells[29].Text.ToString();


                if (posted.ToLower().Equals("no"))
                {
                    string error = "";
                    if (!approved.ToLower().Equals("yes"))
                    {

                        bool saved = Approve(gpe_code, ref error);
                        if (!saved)
                        {
                            lblError.Text = error;
                            return;
                        }
                    }
                    if (Can_Post(gpe_code, ref error))
                    {
                        ViewState["index"] = index;

                        string amount = "";
                        string drAccount = "";
                        string crAccount = "";
                        string narrative1 = "";
                        string narrative2 = "";
                        string narrative3 = "";

                        if (GetGLEntryForOnlinePosting(gpe_code, ref drAccount, ref crAccount, ref amount, ref narrative1, ref narrative2, ref narrative3))
                        {
                            pnlPost.Visible = true;
                            txtAmount.Text = amount;
                            if (rdClaims.Checked == true) // IRFAN_MOD 150618 for claims account are reversed
                            {
                                txtAccount.Text = crAccount;
                                txtSettlementAcc.Text = drAccount;
                            }
                            else
                            {
                                txtAccount.Text = drAccount;
                                txtSettlementAcc.Text = crAccount;
                            }
                            txtNarrative1.Text = narrative1;
                            txtNarrative2.Text = narrative2;
                            txtNarrative3.Text = narrative3;
                        }
                        else
                        {
                            lblError.Text = "Error in Retrieving the GL Entity.";
                        }
                    }
                    else
                        lblError.Text = error;

                }
                else
                {
                    if (posted.ToLower().Equals("yes"))
                        lblError.Text = "Cannot post an already posted entry";

                }
            }
            //else if (rdSAMAMonthlySettlement.Checked)
            //{
            //    int index = int.Parse(e.CommandArgument.ToString());
            //    string gpe_code = GridView1.DataKeys[index].Value.ToString();
            //    //UMER_MOD 140621 starts
            //    string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            //    string posted = GridView1.Rows[index].Cells[6].Text.ToString();
            //    string cashacc = GridView1.Rows[index].Cells[29].Text.ToString();


            //    if (posted.ToLower().Equals("no"))
            //    {
            //        string error = "";
            //        if (!approved.ToLower().Equals("yes"))
            //        {

            //            bool saved = Approve(gpe_code, ref error);
            //            if (!saved)
            //            {
            //                lblError.Text = error;
            //                return;
            //            }
            //        }
            //        if (Can_Post(gpe_code, ref error))
            //        {
            //            ViewState["index"] = index;

            //            string amount = "";
            //            string drAccount = "";
            //            string crAccount = "";
            //            string narrative1 = "";
            //            string narrative2 = "";
            //            string narrative3 = "";

            //            if (GetGLEntryForOnlinePosting(gpe_code, ref drAccount, ref crAccount, ref amount, ref narrative1, ref narrative2, ref narrative3))
            //            {
            //                pnlPost.Visible = true;
            //                txtAmount.Text = amount;
            //                txtAccount.Text = crAccount;
            //                txtSettlementAcc.Text = drAccount;
            //                txtNarrative1.Text = narrative1;
            //                txtNarrative2.Text = narrative2;
            //                txtNarrative3.Text = narrative3;
            //            }
            //            else
            //            {
            //                lblError.Text = "Error in Retrieving the GL Entity.";
            //            }
            //        }
            //        else
            //            lblError.Text = error;

            //    }
            //    else
            //    {
            //        if (posted.ToLower().Equals("yes"))
            //            lblError.Text = "Cannot post an already posted entry";

            //    }
            //}
            else
                lblError.Text = "Posting is not allowed for the transaction";






            //UMER_MOD 140621 ends






            //ViewState["index"] = index;

            //string amount = "";
            //string drAccount = "";
            //string crAccount = "";
            //string narrative1 = "";
            //string narrative2 = "";
            //string narrative3 = "";

            //if (GetGLEntryForOnlinePosting(gpe_code, ref drAccount, ref crAccount, ref amount, ref narrative1, ref narrative2, ref narrative3))
            //{
            //    pnlPost.Visible = true;
            //    txtAmount.Text = amount;
            //    txtAccount.Text = crAccount;
            //    txtSettlementAcc.Text = drAccount;
            //    txtNarrative1.Text = narrative1;
            //    txtNarrative2.Text = narrative2;
            //    txtNarrative3.Text = narrative3;
            //}
            //else
            //{
            //    lblError.Text = "Error Occured";
            //}
            //can't post a claim until it's approved
            //string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            //string posted = GridView1.Rows[index].Cells[6].Text.ToString();
            //string cashacc = GridView1.Rows[index].Cells[29].Text.ToString();
            //if (approved.ToLower().Equals("yes") && posted.ToLower().Equals("no"))
            //{
            //    string rowID = GridView1.DataKeys[index].Value.ToString();
            //    string trans_type = GridView1.Rows[index].Cells[5].Text.ToString();
            //    //string error = "";
            //    //if (Can_Post(rowID, ref error))
            //    //{
            //        txtAccount.Text = account;
            //        txtAmount.Text = amount;
            //        txtSettlementAcc.Text = cashacc;
            //        if (!trans_type.Equals("&nbsp;"))
            //            lblGLType.Text = trans_type;
            //        else
            //            lblGLType.Text = "";

            //        pnlPost.Visible = true;
            //    //}
            //    //else
            //    //    lblError.Text = error;
            //}
            //else
            //{
            //    if (posted.ToLower().Equals("yes"))
            //        lblError.Text = "Cannot post an already posted entry";
            //    if (approved.ToLower().Equals("no"))
            //        lblError.Text = "Cannot post an entry until it is approved";
            //}
        }
        else if (e.CommandName.Equals("print"))
        {
            ExceptionUtility.LogException(0, "", "", "GridView1_RowCommand.Print", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('PrintGL.aspx?id=" + rowID + "','Report')", true);
        }
        else if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "12.13", "GridView1_RowCommand.View", "");
            pnlControls.Visible = true;
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string eventID = GridView1.Rows[index].Cells[1].Text.ToString();
            string channelID = GridView1.Rows[index].Cells[0].Text.ToString();
            string account = GridView1.Rows[index].Cells[2].Text.ToString();
            string settleAccount = GridView1.Rows[index].Cells[29].Text.ToString();
            string trans_type = GridView1.Rows[index].Cells[5].Text.ToString();
            string posted = GridView1.Rows[index].Cells[6].Text.ToString();
            string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            string declined = GridView1.Rows[index].Cells[8].Text.ToString();
            string amount = GridView1.Rows[index].Cells[9].Text.ToString();
            string record_date = GridView1.Rows[index].Cells[17].Text.ToString();
            string posting_for_date = GridView1.Rows[index].Cells[18].Text.ToString();
            string post_date = GridView1.Rows[index].Cells[19].Text.ToString();
            string approve_date = GridView1.Rows[index].Cells[20].Text.ToString();
            string file_number = GridView1.Rows[index].Cells[21].Text.ToString();
            string re_code = GridView1.Rows[index].Cells[22].Text.ToString();
            string rc_code = GridView1.Rows[index].Cells[23].Text.ToString();
            string decline_date = GridView1.Rows[index].Cells[24].Text.ToString();
            string journal = GridView1.Rows[index].Cells[25].Text.ToString();
            string postedby = GridView1.Rows[index].Cells[26].Text.ToString();
            string approvedby = GridView1.Rows[index].Cells[27].Text.ToString();
            string declinedby = GridView1.Rows[index].Cells[28].Text.ToString();
            string cashacc = GridView1.Rows[index].Cells[29].Text.ToString();
            string glDesc = GridView1.Rows[index].Cells[32].Text.ToString();
            string gridDesc = GridView1.Rows[index].Cells[33].Text.ToString();
            string totAmount = GridView1.Rows[index].Cells[35].Text.ToString();
            string merFeeAmt = GridView1.Rows[index].Cells[36].Text.ToString();

            if (!record_date.Equals("&nbsp;"))
                Label1.Text = record_date;
            else
                Label1.Text = "";

            if (!gridDesc.Equals("&nbsp;"))
                Label21.Text = gridDesc;
            else
                Label21.Text = "";

            if (!posting_for_date.Equals("&nbsp;"))
                Label2.Text = posting_for_date;
            else
                Label2.Text = "";

            if (!account.Equals("&nbsp;"))
                Label3.Text = account;
            else
                Label3.Text = "";

            if (!settleAccount.Equals("&nbsp;"))
                Label4.Text = settleAccount;
            else
                Label4.Text = "";

            if (!trans_type.Equals("&nbsp;"))
                Label15.Text = trans_type;
            else
                Label15.Text = "";
            if (!posted.Equals("&nbsp;"))
                Label5.Text = posted;
            else
                Label5.Text = "";
            if (!post_date.Equals("&nbsp;"))
                Label6.Text = post_date;
            else
                Label6.Text = "";
            if (!approved.Equals("&nbsp;"))
                Label7.Text = approved;
            else
                Label7.Text = "";
            if (!approve_date.Equals("&nbsp;"))
                Label8.Text = approve_date;
            else
                Label8.Text = "";
            /* Amjath Modn for Merchant Fee and Net Amount Starts*/
            if (!totAmount.Equals("&nbsp;"))
                lblTotAmount.Text = totAmount;
            else
                lblTotAmount.Text = "";
            if (!merFeeAmt.Equals("&nbsp;"))
                lblMerchantFeeAmt.Text = merFeeAmt;
            else
                lblMerchantFeeAmt.Text = "";
            /* End*/
            if (!amount.Equals("&nbsp;"))
                Label9.Text = amount;
            else
                Label9.Text = "";
            if (!file_number.Equals("&nbsp;"))
                Label10.Text = file_number;
            else
                Label10.Text = "";
            if (!eventID.Equals("&nbsp;"))
                Label11.Text = eventID;
            else
                Label11.Text = "";
            if (!channelID.Equals("&nbsp;"))
                Label12.Text = channelID;
            else
                Label12.Text = "";
            if (!declined.Equals("&nbsp;"))
                Label16.Text = declined;
            else
                Label16.Text = "";
            if (!decline_date.Equals("&nbsp;"))
                Label17.Text = decline_date;
            else
                Label17.Text = "";
            if (!journal.Equals("&nbsp;"))
                Label14.Text = journal;
            else
                Label14.Text = "";
            if (!postedby.Equals("&nbsp;"))
                Label13.Text = postedby;
            else
                Label13.Text = "";
            if (!approvedby.Equals("&nbsp;"))
                Label18.Text = approvedby;
            else
                Label18.Text = "";
            if (!declinedby.Equals("&nbsp;"))
                Label19.Text = declinedby;
            else
                Label19.Text = "";
            if (!glDesc.Equals("&nbsp;"))
                Label20.Text = glDesc;
            else
                Label20.Text = "";
            if (rdATM.Checked && chkPosted.Checked)
            {
                if (!cashacc.Equals("&nbsp;"))
                    lblCashAcc.Text = cashacc;
                cashaccrow.Visible = true;
            }
            else
                cashaccrow.Visible = false;

            return;
        }
        else if (e.CommandName.Equals("r"))
        {
            if (!rdClaims.Checked)
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string rowID = GridView1.DataKeys[index].Value.ToString();
                string error = "", error1 = "";
                if (Can_Reset(rowID, ref error1))
                {
                    bool saved = Reset(rowID, ref error);
                    if (saved)
                    {
                        lblError.Text = "Entity Reset Successful";
                    }
                    else
                    {
                        lblError.Text = error;
                    }
                }
                else
                    lblError.Text = error1;
            }
            else
            {
                //IJR --> 04-04-2012 
                /* Change the way to handle the claims reset,
                 * When user want to Reset the Claim following actions will be taken:
                 * 1- Claim to be reseted in the claim table as 000 (Entered)
                 * 2- Delete from the GL posting table
                 * Re- Load the data in the Grid View to eliminate the rested claim
                */



                int index = int.Parse(e.CommandArgument.ToString());
                string rowID = GridView1.DataKeys[index].Value.ToString();
                string error = "", error1 = "";
                if (Can_Reset(rowID, ref error1))
                {
                    bool saved = Reset_Claim(rowID, ref error);
                    if (saved)
                    {
                        lblError.Text = "Entity Reset Successful";

                    }
                    else
                    {
                        lblError.Text = error;
                    }
                }
                else
                    lblError.Text = error1;

            }
        }

        if (!e.CommandName.Equals("p"))
        {
            if (ddlChannels.SelectedIndex == 0 && rdChannel.Checked)
                lblError.Text = "You must choose a Channel";
            else if (ddlEvents.SelectedIndex == 0 && rdEvent.Checked)
                lblError.Text = "You must choose an Event";
            else
                getSetup();
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 17; i <= 30; i++)
                e.Row.Cells[i].Visible = false;
            e.Row.Cells[5].Visible = false;
            //e.Row.Cells[4].Visible = false;
            e.Row.Cells[32].Visible = false;
            e.Row.Cells[33].Visible = false;
            e.Row.Cells[34].Visible = false;
            e.Row.Cells[35].Visible = false;
            e.Row.Cells[36].Visible = false;
        }
    }

    private bool Approve(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Approve_GL_Entity";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Approve_GL_Entity", "15.1", "Approve", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Approve_GL_Entity", "15.1", "Approve", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Approve_GL_Entity", "15.1", "Approve", "index: " + index);
            return false;
        }
    }

    private bool Decline(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Decline_GL_Entity";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Decline_GL_Entity", "71.1", "Decline", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Decline_GL_Entity", "71.1", "Decline", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Decline_GL_Entity", "71.1", "Decline", "index: " + index);
            return false;
        }
    }

    private bool Post(string index, string SettleAcct, string Desc, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Post_GL_Entity";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_mw_journal_ref", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_mw_resp", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_settle_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gl_desc", 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_gpe_code"].Value = index;
            if (ViewState["JID"] != null)
                oracom.Parameters["p_mw_journal_ref"].Value = ViewState["JID"].ToString();
            else
                oracom.Parameters["p_mw_journal_ref"].Value = OracleString.Null;

            if (ViewState["MESSAGE"] != null)
                oracom.Parameters["p_mw_resp"].Value = ViewState["MESSAGE"].ToString();
            else
                oracom.Parameters["p_mw_resp"].Value = OracleString.Null;
            oracom.Parameters["p_settle_acct"].Value = SettleAcct;
            oracom.Parameters["p_gl_desc"].Value = Desc;

            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, "GL_POSTING_APPROVER.Post_GL_Entity", "15.1", "Post", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Post_GL_Entity", "15.1", "Post", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Post_GL_Entity", "15.1", "Post", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool PostBegin(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.begin_post_gl";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.begin_post_gl", "505.1", "PostBegin", "p_gpe_code- " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.begin_post_gl", "505.1", "PostBegin", "p_gpe_code- " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.begin_post_gl", "505.1", "PostBegin", "p_gpe_code- " + index);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool PostFailed(string index, string SettleAcct, string Desc, ref string error)
    {
        if (ViewState["MESSAGE"] == null)
            ViewState["MESSAGE"] = "NO RESPONSE FROM MW";
        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 = "GL_POSTING_APPROVER.post_gl_failed";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_mw_resp", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_settle_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gl_desc", 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_gpe_code"].Value = index;
            oracom.Parameters["p_mw_resp"].Value = ViewState["MESSAGE"].ToString();
            oracom.Parameters["p_settle_acct"].Value = SettleAcct;
            oracom.Parameters["p_gl_desc"].Value = Desc;

            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, "GL_POSTING_APPROVER.post_gl_failed", "", "PostFailed", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.post_gl_failed", "", "PostFailed", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.post_gl_failed", "", "PostFailed", "index: " + index + ", SettlAcct: " + SettleAcct + ", Desc: " + Desc);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool Reset(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Reset_GL_Entity";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Reset_GL_Entity", "18.1", "Reset", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Reset_GL_Entity", "18.1", "Reset", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Reset_GL_Entity", "18.1", "Reset", "index: " + index);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }



    private bool Reset_Claim(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.reset_claim";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.reset_claim", "", "Reset_Claim", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.reset_claim", "", "Reset_Claim", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.reset_claim", "", "Reset_Claim", "index: " + index);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }



    private bool postGL(string Type, string Account, string Amount, string Desc)
    {
        string Directory = ConfigurationSettings.AppSettings["MWCertificate"];
        string XmlMessage = "";
        string XmlResponse = "";
        string TransportVia = "I";
        string TransportMethod = "Synch";
        string LangItem = "1";

        //NCBEAI.Adapters.Transport.Message Request = new Message(Directory);

        //if (Type.Equals("Cr"))
        //{
        //    XmlMessage = "<AccID>" + Account + "</AccID><Amount>" + Amount + "</Amount><Description>" + Desc + "</Description><VoucherNo></VoucherNo><Currency>SAR</Currency><BaseAmount>" + Amount + "</BaseAmount><ReferenceNo></ReferenceNo><Override>Y</Override>";
        //    Request.TCod = "AccountCredit";
        //    int finalError = Request.PrepareMessage(XmlMessage, LangItem, Directory, TransportVia, TransportMethod, out XmlResponse);
        //}
        //else
        //{
        //    XmlMessage = "<AccID>" + Account + "</AccID><Amount>" + Amount + "</Amount><Description>" + Desc + "</Description><VoucherNo></VoucherNo><Currency>SAR</Currency><BaseAmount>" + Amount + "</BaseAmount><ReferenceNo></ReferenceNo><Override>Y</Override><Retry>N</Retry>";
        //    Request.TCod = "AccountDebit";
        //    int finalError = Request.PrepareMessage(XmlMessage, LangItem, Directory, TransportVia, TransportMethod, out XmlResponse);
        //}

        //log the transaction request
        Helper.writeMWLog("Request (" + DateTime.Now + "): " + XmlMessage);

        if (XmlResponse.Contains("OK"))
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XmlResponse);
            XmlNode node = doc.SelectSingleNode("/OK");
            string temp = node.Attributes["Journal"].InnerText;
            ViewState["JID"] = temp;
            ViewState["MESSAGE"] = "successful";
            //log the transaction response
            Helper.writeMWLog("Response (" + DateTime.Now + "): " + XmlResponse);
            return true;
        }
        else if (XmlResponse.Contains("ERR"))
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XmlResponse);
            XmlNode node = doc.SelectSingleNode("/ERR");
            string temp = node.Attributes["Desc"].InnerText;
            string temp1 = node.Attributes["NativeErr"].InnerText;
            string temp2 = node.Attributes["NativeDesc"].InnerText;

            ViewState["MESSAGE"] = temp + " " + temp1 + " " + temp2;
            //log the transaction response
            Helper.writeMWLog("Response (" + DateTime.Now + "): " + XmlResponse);
            return false;
        }


        ExceptionUtility.LogException(0, "", "", "postGL", "");
        return false;
    }

    private bool postGLTR(string Type, string Account, string settleAcc, string Amount, string Desc)
    {
        string Directory = ConfigurationSettings.AppSettings["MWCertificate"];
        string XmlMessage = "";
        string XmlResponse = "";
        string TransportVia = "I";
        string TransportMethod = "Synch";
        string LangItem = "1";

        //NCBEAI.Adapters.Transport.Message Request = new Message(Directory);

        //if (Type.Equals("Dr"))
        //{
        //    XmlMessage      = "<FromAccID>" + Account + "</FromAccID><ToAccID>" + settleAcc +
        //                      "</ToAccID><FromAmount>" + Amount + "</FromAmount><Desc>"
        //                      + Desc + "</Desc>";
        //    Request.TCod    = "AccountTransfer";
        //    int finalError  = Request.PrepareMessage(XmlMessage, LangItem, Directory, TransportVia, TransportMethod, out XmlResponse);
        //}
        //else
        //{
        //    XmlMessage = "<AccountTransfer><FromAccID>" + settleAcc + "</FromAccID><ToAccID>" + Account +
        //                   "</ToAccID><FromAmount>" + Amount + "</FromAmount><PurposeOfTransfer>" + Desc + "</PurposeOfTransfer></AccountTransfer>";
        //    Request.TCod = "AccountTransfer";
        //    int finalError = Request.PrepareMessage(XmlMessage, LangItem, Directory, TransportVia, TransportMethod, out XmlResponse);
        //}

        //log the transaction request
        Helper.writeMWLog("Request (" + DateTime.Now + "): " + XmlMessage);

        if (XmlResponse.Contains("OK"))
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XmlResponse);
            XmlNode node = doc.SelectSingleNode("/OK");
            string temp = node.Attributes["Journal"].InnerText;
            ViewState["JID"] = temp;
            ViewState["MESSAGE"] = "successful";
            //log the transaction response
            Helper.writeMWLog("Response (" + DateTime.Now + "): " + XmlResponse);
            return true;
        }
        else if (XmlResponse.Contains("ERR"))
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XmlResponse);
            XmlNode node = doc.SelectSingleNode("/ERR");
            string temp = node.Attributes["System"].InnerText + " - " + node.Attributes["NativeErr"].InnerText +
                          " : " + node.Attributes["Desc"].InnerText + " ( " + node.Attributes["NativeDesc"].InnerText + " ) ";
            ViewState["MESSAGE"] = temp;
            //log the transaction response
            Helper.writeMWLog("Response (" + DateTime.Now + "): " + XmlResponse);
            return false;
        }
        ExceptionUtility.LogException(0, "", "", "postGLTR", "");
        return false;
    }

    protected void btnSavePost_Click(object sender, EventArgs e)
    {
        //if (intCount < 1)
        //    intCount++;
        //else
        //{
        //    pnlPost.Visible = false;
        //    lblError.Text = "Transaction Posted";
        //    return;
        //}
        int index = int.Parse(ViewState["index"].ToString());
        string rowID = GridView1.DataKeys[index].Value.ToString();
        string atmno = GridView1.Rows[index].Cells[4].Text.ToString();
        string trans_type = GridView1.Rows[index].Cells[5].Text.ToString();
        string AccName = GridView1.Rows[index].Cells[3].Text.ToString();
        string Date = GridView1.Rows[index].Cells[12].Text.ToString();
        string ShortName = GridView1.Rows[index].Cells[30].Text.ToString();
        string SettleAcc = GridView1.Rows[index].Cells[29].Text.ToString();
        string approver = GridView1.Rows[index].Cells[34].Text.ToString();
        string Desc = GridView1.Rows[index].Cells[32].Text.ToString();
        if (rdSAMAMonthlySettlement.Checked)
            Desc = GridView1.Rows[index].Cells[3].Text.ToString();
        if (Desc.ToLower().Equals("&nbsp;"))
            Desc = "";
        //if (!approver.Equals(Session["uid"].ToString()))
        //{            
        //if (rdChannel.Checked)
        //  Desc = ShortName + " " + Date.Substring(0, 5);
        //else if (rdEvent.Checked)
        // Desc = ShortName + " " + Date.Substring(0, 5);
        //else if (rdATM.Checked)
        //  Desc = atmno + " " + Date.Substring(0, 5);

        string account = txtAccount.Text;
        string amount = txtAmount.Text;
        string error = "";
        // call web service
        //UMER_MOD 201406 25 starts
        // bool bCallWebSrvc = CallWebServiceAcctXfer();
        //UMER_MOD 201406 25 starts
        //Begin Post

        //if (bCallWebSrvc)
        //{
        ExceptionUtility.LogException(0, "", "505", "btnSavePost_Click", "");
        //UMER_MOD 140908 starts  
        if (ViewState["PostAll"] as string != "Y") // IRFAN_MOD
            getSetup();
        //UMER_MOD 140908 ends
        bool beginPost = PostBegin(rowID, ref error);
        if (beginPost)
        {
            bool posted = false;
            //if (account.Equals(SettleAcc))
            //UMER_MOD 140625 starts
            //posted = postGL(trans_type, account, amount, Desc);
            posted = CallWebServiceAcctXfer();
            //UMER_MOD 140625 ends
            //else
            //  posted = postGLTR(trans_type, account, SettleAcc, amount, Desc);
            if (posted)
            {
                bool saved = Post(rowID, SettleAcc, Desc, ref error);
                if (saved)
                {
                    if (ViewState["PostAll"] as string != "Y") // IRFAN_MOD
                    {
                        lblError.Text = "Transaction Posted";
                        getSetup();
                    }
                }
                else
                {
                    lblError.Text = error;
                }
            }
            else
            {

                if (ViewState["MESSAGE"] == null)
                    lblError.Text = "posting failed";
                else
                    lblError.Text = ViewState["MESSAGE"].ToString();

                bool saved1 = PostFailed(rowID, SettleAcc, Desc, ref error);
                if (!saved1)
                {
                    lblError.Text = error;
                    lblError.Text = "Posting Failed";
                }
            }

            pnlPost.Visible = false;
            if (ddlChannels.SelectedIndex == 0 && rdChannel.Checked)
                lblError.Text = "You must choose a Channel";
            else if (ddlEvents.SelectedIndex == 0 && rdEvent.Checked)
                lblError.Text = "You must choose an Event";
            else
            {
                if (ViewState["PostAll"] as string != "Y") // IRFAN_MOD
                    getSetup();
            }
        }
        else
        {
            // In case another user is posting the same record

            lblError.Text = error;
        }
        //    }

        ////}
        //else
        //{
        //    lblError.Text = "The user doing the posting must be different than the approver.";
        //}
    }

    protected void btnCancelPost_Click(object sender, EventArgs e)
    {
        pnlPost.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btnCancelPost_Click", "");
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void rdEvent_CheckedChanged(object sender, EventArgs e)
    {

        AutoApproveRow.Visible = false;
        samaAccountType.Visible = false;
        channelsRow.Visible = false;
        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        BranchRow.Visible = false;
        POSRow.Visible = false;
        POSRow2.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        eventsRow.Visible = true;
        AccTypeRow.Visible = true;

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdEvent_CheckedChanged", "");
    }

    protected void rdChannel_CheckedChanged(object sender, EventArgs e)
    {
        AutoApproveRow.Visible = false;
        samaAccountType.Visible = false;
        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        eventsRow.Visible = false;
        BranchRow.Visible = false;
        POSRow.Visible = false;
        POSRow2.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        channelsRow.Visible = true;
        AccTypeRow.Visible = true;

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdChannel_CheckedChanged", "");
    }

    protected void rdATM_CheckedChanged(object sender, EventArgs e)
    {
        AutoApproveRow.Visible = false;
        samaAccountType.Visible = false;
        eventsRow.Visible = false;
        channelsRow.Visible = false;
        AccTypeRow.Visible = false;
        POSRow.Visible = false; 
        POSRow2.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        ATMNoRow.Visible = true;
        ATMTypeRow.Visible = true;
        BranchRow.Visible = true;

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdATM_CheckedChanged", "");
    }

    protected void rdPOSSET_CheckedChanged(object sender, EventArgs e)
    {
        AutoApproveRow.Visible = false;
        samaAccountType.Visible = false;
        eventsRow.Visible = false;
        channelsRow.Visible = false;
        AccTypeRow.Visible = false;

        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        BranchRow.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        POSRow.Visible = true;
        POSRow2.Visible = true;

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdPOSSET_CheckedChanged", "");
    }

    protected void rdPOSRENT_CheckedChanged(object sender, EventArgs e)
    {
        
       

        AutoApproveRow.Visible = false;
        samaAccountType.Visible = false;
        eventsRow.Visible = false;
        channelsRow.Visible = false;
        AccTypeRow.Visible = false;

        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        BranchRow.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        POSRow.Visible = true;
        POSRow2.Visible = false; //true for pos rental fee not for terninal charges

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdPOSRENT_CheckedChanged", "");
    }

    protected void rdClaims_CheckedChanged(object sender, EventArgs e)
    {
        AutoApproveRow.Visible = true;
        samaAccountType.Visible = false;
        eventsRow.Visible = false;
        channelsRow.Visible = false;
        AccTypeRow.Visible = false;

        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        BranchRow.Visible = false;
        POSRow.Visible = false;
        POSRow2.Visible = false;

        ClaimRow.Visible = true;
        ClaimRow2.Visible = true;


        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";

        chkApproved.Checked = true;
        ExceptionUtility.LogException(0, "", "", "rdClaims_CheckedChanged", "");
    }
    protected void rdSAMAMonthlySettlement_CheckedChanged(object sender, EventArgs e)
    {
        AutoApproveRow.Visible = false;
        eventsRow.Visible = false;
        channelsRow.Visible = false;
        AccTypeRow.Visible = false;

        ATMNoRow.Visible = false;
        ATMTypeRow.Visible = false;
        BranchRow.Visible = false;
        ClaimRow.Visible = false;
        ClaimRow2.Visible = false;

        POSRow.Visible = false;
        POSRow2.Visible = false;
        samaAccountType.Visible = true;

        ddlEvents.SelectedIndex = 0;
        ddlChannels.SelectedIndex = 0;
        ddlATMType.SelectedIndex = 0;
        ddlBranch.SelectedIndex = 0;
        txtATMnumber.Text = "";
        txtClientNo.Text = "";
        txtCardNo.Text = "";
        txtTraceID.Text = "";
        txtTerminalID.Text = "";
        ExceptionUtility.LogException(0, "", "", "rdSAMAMonthlySettlement_CheckedChanged", "");
    }
    private void loadSAMAAccountType()
    {
        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_sama_monthly_gl";

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

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

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



            ddlSAMAAccountType.Items.Insert(0, "------ All ------");

            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.p_sama_monthly", "", "loadSAMAAccountType", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
    private bool Can_Post(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Can_Post";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Can_Post", "15.2", "Can_Post", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Can_Post", "15.2", "Can_Post", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Can_Post", "15.2", "Can_Post", "index: " + index);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool Can_Reset(string index, ref string error)
    {
        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 = "GL_POSTING_APPROVER.Can_Reset";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_gpe_code", 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_gpe_code"].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")
            {
                ExceptionUtility.LogException(0, "GL_POSTING_APPROVER.Can_Reset", "", "Can_Reset", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Can_Reset", "", "Can_Reset", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Can_Reset", "", "Can_Reset", "index: " + index);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnApproveAll_Click(object sender, EventArgs e)
    {
        string error = "";
        ExceptionUtility.LogException(0, "", "12.146", "btnApproveAll_Click", "");//index: " + index);
        bool saved = ApproveAll(ref error);
        if (saved)
        {
            lblError.Text = "Approve Successful";
            getSetup();
        }
        else
        {
            lblError.Text = error;
        }
    }

    protected void btnPostAll_Click(object sender, EventArgs e)
    {
        string error = "";
        bool saved = PostAll(ref error);
        if (saved)
        {
            lblError.Text = "Post Successful";
            getSetup();
            btnPostAll.Visible = false;
            btnGenReport.Visible = false;
        }
        else
        {
            lblError.Text = error;
        }
    }

    protected void btnGenAutoAprroveReport_Click(object sender, EventArgs e)
    {
        //btnPostAll.Enabled = true;

        string date = DatePicker1.date;
        if ("".Equals(date))
            date = DateTime.Now.ToString("yyMMdd");

        string p1 = date;
        string p2 = Session["uid"].ToString();

        string UrlString1 = "rpt=1060&p1=" + p1 + "&p2=" + p2;
        ExceptionUtility.LogException(0, "", "79.462", "btnGenAutoAprroveReport_Click", "");
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);

        Session["BtnPostSystemApproved"] = "Y";
        BtnPostSystemApproved.Enabled = true;
    }

    private bool ApproveAll(ref string error)
    {
        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 = "GL_POSTING_APPROVER.Approve_All";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_ch_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_product", OracleType.Number).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_date"].Value = DatePicker1.date;

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

            // IRFAM_MOD 190219 to fix ecom approval issue
            if (rdPOSSETL.Checked)//MrNashaat 2019-03-19 change from rdPOSSET to rdPOSSETL
            {
                oracom.Parameters["p_ch_type"].Value = "6";
                oracom.Parameters["p_product"].Value = 2;
            }
            else if (rdECOMSETL.Checked)
            {
                oracom.Parameters["p_ch_type"].Value = "6"; 
                oracom.Parameters["p_product"].Value = 3;
            }


            if (rdPOSRENT.Checked)
                oracom.Parameters["p_ch_type"].Value = "7";

            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, "GL_POSTING_APPROVER.Approve_All", "146.1", "ApproveAll", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GL_POSTING_APPROVER.Approve_All", "146.1", "ApproveAll", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_POSTING_APPROVER.Approve_All", "146.1", "ApproveAll", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool PostAll(ref string error)
    {
        //IJR 
        //New Code --> for Offline posting, this will use the procedure name
        //GENERATE_GL_FILE.GW56_files for posting the results 
        //the result file will be on the specified path inside the procedure called OUTPUT
        //This will depend on the channel type : 
        //3	channel Gl
        //5	ATM GL
        //8	claims
        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 = "GENERATE_GL_FILE.GW56_files";



            oracom.Parameters.Add("p_file_dir", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_channel_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("v_res", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_date"].Value = DatePicker1.date;

            if (rdClaims.Checked)
                oracom.Parameters["p_channel_type"].Value = 8;
            else if (rdATM.Checked)
                oracom.Parameters["p_channel_type"].Value = 5;
            else if (rdChannel.Checked)
                oracom.Parameters["p_channel_type"].Value = 3;


            else if (rdEvent.Checked)
                oracom.Parameters["p_channel_type"].Value = 2;

            else if (rdPOSSET.Checked)
                oracom.Parameters["p_channel_type"].Value = 6;
            else if (rdSAMAMonthlySettlement.Checked)
                oracom.Parameters["p_channel_type"].Value = 9;
            else if (rdPOSRENT.Checked)
                oracom.Parameters["p_channel_type"].Value = "7";

            //OUTPUT is a directory  defined inside the DB Directories
            oracom.Parameters["p_file_dir"].Value = "OUTPUT";


            oracom.ExecuteNonQuery();
            oracon.Close();


            string a = oracom.Parameters["v_res"].Value.ToString();
            //error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "Successful")
                return true;
            else
                return false;

            /*Previous Code 
            //string DBConnection = Helper.dbConnection();

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

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

            //oracom.CommandType = CommandType.StoredProcedure;
            //oracom.CommandText = "GL_POSTING_APPROVER.Post_All";

            //oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_ch_type", 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_date"].Value = DatePicker1.date;

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

            //if (rdPOSSET.Checked)
            //    oracom.Parameters["p_ch_type"].Value = "6";
            //if (rdPOSRENT.Checked)
            //    oracom.Parameters["p_ch_type"].Value = "7";

            //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;
             * */
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GENERATE_GL_FILE.GW56_files", "", "PostAll", "p_date-" + DatePicker1.date);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnGenReport_Click(object sender, EventArgs e)
    {

        string p1 = DatePicker1.date;
        string p2 = "";

        if (rdClaims.Checked)
            p2 = "8";
        else if (rdATM.Checked)
            p2 = "5";
        else if (rdChannel.Checked)
            p2 = "3";
        else if (rdEvent.Checked)
            p2 = "2";
        else if (rdPOSSET.Checked)
            p2 = "6";


        string UrlString = "rpt=503&p1=" + p1 + "&p2=" + p2;
        ExceptionUtility.LogException(0, "", "", "btnGenReport_Click", "");
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?" + UrlString + "','Report')", true);


    }

    //Code for posting online using WebService starts 
    //140617
    public string GetAcctXferSeqVal()
    {
        string result;
        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 ASR.SEQ_ACCTXFER.nextval from dual";

        DataTable dt = new DataTable();
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
            result = dt.Rows[0][0].ToString();
        else
            result = "";

        oracon.Close();
        return result;
    }

    private bool ValidateRemoteCertificate(
           object sender,
            X509Certificate cert,
           X509Chain chain,
           SslPolicyErrors sslpolicy)
    {
        return true;
    }

    public string GetEncryptedEairPass()
    {
        try
        {
            ExceptionUtility.LogException(0, "select EAIRPRD.ENCRYPT_DATA('{0}','{1}') from dual", "GetEncryptedEairPass", "", "");
            string result;
            string DBEairConnection = ConfigurationSettings.AppSettings["DB_EAIR_Connection"];//Helper.dbEairConnection();
            string EairEncKey = ConfigurationSettings.AppSettings["EAIR_EncryptKey"];
            string LDAPPass = ConfigurationSettings.AppSettings["RECONSS_LDAP_PASS"];


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

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

            oracom.CommandType = CommandType.Text;
            oracom.CommandText = string.Format("select EAIRPRD.ENCRYPT_DATA('{0}','{1}') from dual", LDAPPass, EairEncKey);

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
                result = dt.Rows[0][0].ToString();

            else
                result = "";

            oracon.Close();
            return result;

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "select EAIRPRD.ENCRYPT_DATA('{0}','{1}') from dual", "GetEncryptedEairPass", "", "");
            return "";
        }

    }
    //public string CreateXML(object YourClassObject)
    //{
    //    XmlDocument xmlDoc = new XmlDocument();
    //    XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());

    //    using (MemoryStream xmlStream = new MemoryStream())
    //    {
    //        xmlSerializer.Serialize(xmlStream, YourClassObject);
    //        xmlStream.Position = 0;
    //        xmlDoc.Load(xmlStream);
    //       return  xmlDoc.InnerXml;
    //    }



    //}

    public bool CallWebServiceAcctXfer()
    {
        try
        {
            string strSeqValue = "";
            string strNoOfRepeatRev = "";
            string strMWACode = "0";
            FNExtAcctXferServiceRef.MsgRqHdr_Type msgHeaderReq = new FNExtAcctXferServiceRef.MsgRqHdr_Type();
            FNExtAcctXferServiceRef.FNExtAcctXferBodyRq_Type msgBodyReq = new FNExtAcctXferServiceRef.FNExtAcctXferBodyRq_Type();
            FNExtAcctXferServiceRef.Sec_Type msgHeaderSeType = new FNExtAcctXferServiceRef.Sec_Type();

            FNExtAcctXferServiceRef.MsgRsHdr_Type msgHeaderRes = new FNExtAcctXferServiceRef.MsgRsHdr_Type();
            FNExtAcctXferServiceRef.FNExtAcctXferBodyRs_Type msgBodyRes = new FNExtAcctXferServiceRef.FNExtAcctXferBodyRs_Type();

            FNExtAcctXferServiceRef.FNExtAcctXferService FenAcctTransferSvc = new FNExtAcctXferServiceRef.FNExtAcctXferService();
            FNExtAcctXferServiceRef.FeeInfo_Type FeeInfoTypeObj = new FNExtAcctXferServiceRef.FeeInfo_Type();
            if (ConfigurationSettings.AppSettings["Environment"] as string == "UAT")
                msgHeaderSeType.Info = "9C0CC668EB505B5CB747FDB1DEFF85B2"; // UAT only//
            else
                msgHeaderSeType.Info = GetEncryptedEairPass();  //PRODuction 

            msgHeaderSeType.InfoType = "1";//1 is for password
            msgHeaderReq.Sec = new FNExtAcctXferServiceRef.Sec_Type[1];
            msgHeaderReq.Sec[0] = msgHeaderSeType;


            msgHeaderReq.SCId = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_SCID"];//"RCON";
            msgHeaderReq.FuncId = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_FUNCID"]; //"38504003";
            msgHeaderReq.RqMode = 0;
            msgHeaderReq.AgentId = ConfigurationSettings.AppSettings["RECONSS_LDAP_USER"];//"ReconSS";
            msgHeaderReq.Version = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_VER"]; //"V01";
            msgHeaderReq.UsrId = ConfigurationSettings.AppSettings["RECONSS_LDAP_USER"]; //"ReconSS";
            strSeqValue = GetAcctXferSeqVal();
            msgHeaderReq.RqUID = "RCON" + DateTime.Now.ToString("ddMMyyyy") + strSeqValue; //"RCON170620141231231";
            //msgHeaderReq.ClientDt = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
            msgHeaderReq.ClientDt = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");



            msgBodyReq.SvcVer = ConfigurationSettings.AppSettings["ACCTXFER_FINACLE_VER"];//"V01";
            //msgBodyReq.HostUsrId = "TAPSIN";
            //msgBodyReq.HostUsrId = "016";
            msgBodyReq.HostUsrId = ConfigurationSettings.AppSettings["RECONSS_HOST_USERID"];//"ReconSS";//"ReconSS";
            msgBodyReq.TrnCategoryType = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_TRN_CAT"];// "00012";
            msgBodyReq.TrnSubCategoryType = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_TRN_SUBCAT"]; //"00001";

            strNoOfRepeatRev = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_REV_REPEAT_CNT"];

            msgBodyReq.PostingDt = DateTime.Now.ToString("yyyy-MM-dd");
            msgBodyReq.PostingTm = DateTime.Now.ToString("HH:mm:ss");
            msgBodyReq.TrnRefNum = strSeqValue;//"12345";
            if (rdATM.Checked)
                msgBodyReq.ExternalRefNum = "5-" + strSeqValue; //"14524444";
            else if (rdClaims.Checked)
                msgBodyReq.ExternalRefNum = "8-" + strSeqValue; //"14524444";

            else if (rdEvent.Checked)
                msgBodyReq.ExternalRefNum = "2-" + strSeqValue; //"14524444";

            else if (rdChannel.Checked)
                msgBodyReq.ExternalRefNum = "3-" + strSeqValue; //"14524444";

            else if (rdPOSSET.Checked)
                msgBodyReq.ExternalRefNum = "6-" + strSeqValue; //"14524444";

            else if (rdSAMAMonthlySettlement.Checked)
                msgBodyReq.ExternalRefNum = "9-" + strSeqValue; //"14524444";

            msgBodyReq.ValDt = System.DateTime.Now;
            msgBodyReq.DebitAcctNum = txtAccount.Text;//"1111111111111";
            msgBodyReq.TrnAmt = Convert.ToDecimal(txtAmount.Text);//1000;
            msgBodyReq.CreditAcctNum = txtSettlementAcc.Text;//"2222222222222";
            msgBodyReq.OtherCurrTrnAmt = Convert.ToDecimal(txtAmount.Text);//1000;
            //UMER_MOD 140617 starts
            msgBodyReq.EnNarr1 = txtNarrative1.Text;
            msgBodyReq.EnNarr2 = txtNarrative2.Text;
            msgBodyReq.EnNarr3 = txtNarrative3.Text;
            //UMER_MOD 140617 ends

            //UMER_MOD 140907 starts

            msgBodyReq.ArNarr1 = txtNarrative1.Text;
            msgBodyReq.ArNarr2 = txtNarrative2.Text;
            msgBodyReq.ArNarr3 = txtNarrative3.Text;
            //UMER_MOD 140907 ends
            msgBodyReq.ExRate = 1;
            msgBodyReq.BaseEquiv = "1";

            FeeInfoTypeObj.FeeAmtSpecified = false;


            /*set url for the service */
            FenAcctTransferSvc.Url = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_URL"];//"https://10.101.121.100:27836/FNExtAcctXfer";
            string xmlMsg = CreateXML(msgBodyReq);
            if (xmlMsg.Length > 2000)
                xmlMsg.Substring(0, 1999);
            if (InsertAcctXferRecord(msgHeaderReq.RqUID, xmlMsg, 0, out strMWACode))
            {
                string strCertPath = ConfigurationSettings.AppSettings["ACCTXFER_WEBSVC_CERT_PATH"];//"D:\TestWebService\TempWebSvc\WSDL\MessageBroker.anb.com.sa.crt"
                System.Security.Cryptography.X509Certificates.X509Certificate myCert = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(strCertPath);

                //System.Security.Cryptography.X509Certificates.X509Certificate myCert = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(@"D:\TestWebService\TempWebSvc\WSDL\MessageBroker.anb.com.sa.crt");

                #region register SSL Validation Callback Method
                ServicePointManager.ServerCertificateValidationCallback += (new RemoteCertificateValidationCallback(ValidateRemoteCertificate));
                #endregion
                /*Give credentials to the Service */
                //msgBodyReq.ToString();
                /* Call the Service now */
                /*Start 
                 * To fix Error "Underlying connection was closed on send" thrown by production server.
                 * Not required in UAT*/
                //Commented by Nashaat 07-12-2015
                //if (ConfigurationSettings.AppSettings["Environment"] as string != "UAT")
                //    ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
                /*End*/
                try
                {

                    msgHeaderRes = FenAcctTransferSvc.FNExtAcctXferOperation(msgHeaderReq, msgBodyReq, out msgBodyRes);
                    if (msgHeaderRes == null)
                    {
                        ExceptionUtility.LogException(9, "FNExtAcctXferOperation", "1.2", " No message header", "Req msg: " + xmlMsg);

                    }
                    else
                    {
                        ExceptionUtility.LogException(9, "FNExtAcctXferOperation", "1.2", "msgHeaderRes: " + msgHeaderRes, "Req msg: " + xmlMsg);
                    }
                    string statusCode = msgHeaderRes.StatusCode;
                    string statusDesc = msgHeaderRes.StatusDesc;

                    if (statusCode.Length > 10)
                        statusCode.Substring(0, 10);

                    if (statusDesc.Length > 100)
                        statusDesc.Substring(0, 100);

                    if (statusCode == "I000000")
                    {
                        string xmlMsgRs = CreateXML(msgBodyRes);
                        if (msgBodyRes.FNTrnRefNum != null)
                        {
                            ViewState["JID"] = msgBodyRes.FNTrnRefNum;
                            ViewState["MESSAGE"] = "Successful";
                        }
                        else
                        {
                            ViewState["JID"] = null;
                            //As per Al-Malaki email on 15th July 2014, no ref no will come from finacle
                            //ViewState["MESSAGE"] = "No Reference Number found, Can't be posted";
                            //return false;
                            ViewState["MESSAGE"] = "Succesful, Without a Finacle Reference Number";
                        }

                        if (xmlMsgRs.Length > 2000)
                            xmlMsgRs.Substring(0, 1999);
                        UpdateAcctXferRecord(strMWACode, xmlMsgRs, statusCode, statusDesc);
                        //update gl entry status as posted and put the FT ref no in the table
                        //TODO:
                        return true;
                    }
                    else
                    {
                        ViewState["MESSAGE"] = statusDesc;
                        UpdateAcctXferRecord(strMWACode, "", statusCode, statusDesc);
                        //do not update gl entry status
                        if (CheckIfReversalRequired(statusCode))
                        {
                            int rNoOfRepeatRev = Convert.ToInt32(strNoOfRepeatRev);
                            int iRevRepeat = -1;

                            do
                            {
                                msgHeaderReq.RqMode = iRevRepeat;
                                if (InsertAcctXferRecord(msgHeaderReq.RqUID, xmlMsg, iRevRepeat, out strMWACode))
                                {
                                    msgHeaderRes = FenAcctTransferSvc.FNExtAcctXferOperation(msgHeaderReq, msgBodyReq, out msgBodyRes);
                                    statusCode = msgHeaderRes.StatusCode;
                                    statusDesc = msgHeaderRes.StatusDesc;


                                    if (statusCode == "I000000")
                                    {
                                        string xmlMsgRs = CreateXML(msgBodyRes);


                                        if (xmlMsgRs.Length > 2000)
                                            xmlMsgRs.Substring(0, 1999);
                                        UpdateAcctXferRecord(strMWACode, xmlMsgRs, statusCode, statusDesc);
                                        rNoOfRepeatRev = 0;

                                    }
                                    else
                                    {
                                        ViewState["MESSAGE"] = statusDesc;
                                        UpdateAcctXferRecord(strMWACode, "", statusCode, statusDesc);
                                        iRevRepeat--;
                                        rNoOfRepeatRev--;
                                    }
                                }
                                else
                                {
                                    ExceptionUtility.LogException(9, "CallWebServiceAcctXfer", "1.2", "The reversal record is not inserted", "");
                                    lblError.Text = "The posting was not successful, please check the error log";
                                    rNoOfRepeatRev = 0;

                                }
                            } while (rNoOfRepeatRev > 0);

                            return false;

                        }
                        else
                        {

                            UpdateAcctXferRecord(strMWACode, "", statusCode, statusDesc);
                            ExceptionUtility.LogException(9, "", "505.2", "CallWebServiceAcctXfer", "");
                            return false;
                        }
                    }


                }


                catch (Exception ex)
                {
                    ExceptionUtility.LogException(ex, "", "505.2", "CallWebServiceAcctXfer", "");
                    lblError.Text = "The posting was not successful, please check the error log";
                    return false;
                }

            }
            else
            {
                ExceptionUtility.LogException(9, "", "505.2", "CallWebServiceAcctXfer", "");
                lblError.Text = "The posting was not successful, please check the error log";
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "", "505.2", "CallWebServiceAcctXfer", "");
            return false;
        }
    }

    public bool CheckIfReversalRequired(string strStatusCode)
    {



        try
        {
            string DBConnection = Helper.dbConnection();
            string error = "0";
            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "PrepareGLEntryForOnlinePosting.CheckReversalReq";

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

            oracom.Parameters.Add("v_res", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_Err_Code"].Value = strStatusCode;



            oracom.ExecuteNonQuery();
            oracon.Close();

            error = oracom.Parameters["v_res"].Value.ToString();
            if (error == "1")
            {
                ExceptionUtility.LogException(0, "PrepareGLEntryForOnlinePosting.CheckReversalReq", "1.2.4", "CheckIfReversalRequired", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "PrepareGLEntryForOnlinePosting.CheckReversalReq", "1.2.4", "CheckIfReversalRequired", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "PrepareGLEntryForOnlinePosting.CheckReversalReq", "1.2.4", "CheckIfReversalRequired", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    public string CreateXML(object YourClassObject)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());

        using (MemoryStream xmlStream = new MemoryStream())
        {
            xmlSerializer.Serialize(xmlStream, YourClassObject);
            xmlStream.Position = 0;
            xmlDoc.Load(xmlStream);

            string xmlNoNS = xmlDoc.InnerXml.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
            xmlNoNS = xmlNoNS.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
            xmlNoNS = xmlNoNS.Replace("xmlns=\"http://www.anb.com.sa/\"", "");
            if (xmlNoNS.IndexOf("<FNExtAcctXferBodyRq_Type  >") > 0)
                xmlNoNS = xmlNoNS.Substring(xmlNoNS.IndexOf("<FNExtAcctXferBodyRq_Type  >"), (xmlNoNS.Length - "<FNExtAcctXferBodyRq_Type  >".Length));
            if (xmlNoNS.IndexOf("<FNExtAcctXferBodyRs_Type  >") > 0)
                xmlNoNS = xmlNoNS.Substring(xmlNoNS.IndexOf("<FNExtAcctXferBodyRs_Type  >"), (xmlNoNS.Length - "<FNExtAcctXferBodyRs_Type  >".Length));

            return xmlNoNS;
        }



    }
    private bool InsertAcctXferRecord(string strReqID, string strReqMsg, int iRepeatCount, out string strMWACode)
    {
        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 = "AcctXferMsgEditor.sp_INSERT_AcctXferReq";

            oracom.Parameters.Add("p_RECORD_DATE", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_REQ_ID", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_REQ_MSG", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_GL_CHANNEL_TYPE", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_RVSL_REPEAT_COUNT", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_MWA_CODE", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_RES_CODE", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_RECORD_DATE"].Value = DateTime.Now.ToString("yyMMdd");
            oracom.Parameters["p_RVSL_REPEAT_COUNT"].Value = iRepeatCount;
            oracom.Parameters["p_REQ_ID"].Value = strReqID;
            oracom.Parameters["p_REQ_MSG"].Value = strReqMsg;



            if (rdPOSSET.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "6";
            else if (rdClaims.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "8";
            else if (rdATM.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "5";
            else if (rdEvent.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "2";
            else if (rdChannel.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "3";
            if (rdSAMAMonthlySettlement.Checked)
                oracom.Parameters["p_GL_CHANNEL_TYPE"].Value = "9";

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            strMWACode = oracom.Parameters["p_MWA_CODE"].Value.ToString();
            if (a == "0")
            {
                //  strMWACode =oracom.Parameters["p_MWA_CODE"].Value.ToString();
                ExceptionUtility.LogException(0, "AcctXferMsgEditor", "505.2.1", "sp_INSERT_AcctXferReq", "");
                return true;
            }
            else
            {
                //strMWACode =oracom.Parameters["p_MWA_CODE"].Value.ToString();
                ExceptionUtility.LogException(9, "AcctXferMsgEditor", "505.2.1", "sp_INSERT_AcctXferReq", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "AcctXferMsgEditor", "505.2.1", "sp_INSERT_AcctXferReq", "");
            strMWACode = "0";
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool UpdateAcctXferRecord(string strMWACode, string strResMsg, string strMsgStatus, string strMsgStatusDesc)
    {
        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 = "AcctXferMsgEditor.sp_Update_AcctXferReq";


            oracom.Parameters.Add("p_MWA_CODE", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_RES_MSG", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_MSG_STATUS", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_MSG_STATUS_DESC", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_RES_CODE", OracleType.Number).Direction = ParameterDirection.Output;


            oracom.Parameters["p_MWA_CODE"].Value = strMWACode;
            oracom.Parameters["p_RES_MSG"].Value = strResMsg;
            oracom.Parameters["p_MSG_STATUS"].Value = strMsgStatus;
            oracom.Parameters["p_MSG_STATUS_DESC"].Value = strMsgStatusDesc;





            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();

            if (a == "0")
            {
                ExceptionUtility.LogException(0, "AcctXferMsgEditor.sp_Update_AcctXferReq", "505.2.2", "UpdateAcctXferRecord", "p_MWA_CODE-" + strMWACode + ":p_RES_MSG-" + strResMsg + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS_DESC-" + strMsgStatusDesc);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "AcctXferMsgEditor.sp_Update_AcctXferReq", "505.2.2", "UpdateAcctXferRecord", "p_MWA_CODE-" + strMWACode + ":p_RES_MSG-" + strResMsg + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS_DESC-" + strMsgStatusDesc);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "AcctXferMsgEditor.sp_Update_AcctXferReq", "505.2.2", "UpdateAcctXferRecord", "p_MWA_CODE-" + strMWACode + ":p_RES_MSG-" + strResMsg + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS-" + strMsgStatus + ":p_MSG_STATUS_DESC-" + strMsgStatusDesc);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    //140617
    //Code for posting online using WebService ends
    private bool GetGLEntryForOnlinePosting(string gpe_code, ref string drAcct, ref string crAcct, ref string amt, ref string narrt1, ref string narrt2, ref string narrt3)
    {
        //string drAccount, CrAccount, amount, narrative1, narrative2, narrative3;
        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 = "PrepareGLEntryForOnlinePosting.GetEntry";


            oracom.Parameters.Add("p_gpe_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("v_Cr_Account", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_Dr_Account", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_Amount", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_Narrative1", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_Narrative2", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_Narrative3", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_res", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_gpe_code"].Value = Convert.ToInt32(gpe_code);


            oracom.ExecuteNonQuery();
            oracon.Close();

            string a = oracom.Parameters["v_res"].Value.ToString();

            if (a.Equals("Successful"))
            {
                drAcct = oracom.Parameters["v_Dr_Account"].Value.ToString();
                crAcct = oracom.Parameters["v_Cr_Account"].Value.ToString();
                amt = oracom.Parameters["v_Amount"].Value.ToString();
                narrt1 = oracom.Parameters["v_Narrative1"].Value.ToString();
                narrt2 = oracom.Parameters["v_Narrative2"].Value.ToString();
                narrt3 = oracom.Parameters["v_Narrative3"].Value.ToString();
                ExceptionUtility.LogException(0, "PrepareGLEntryForOnlinePosting.GetEntry", "15.3", "GetEntry", "p_gpe_code- " + gpe_code);
                return true;
            }
            else
            {
                drAcct = "";
                crAcct = "";
                amt = "";
                narrt1 = "";
                narrt2 = "";
                narrt3 = "";
                ExceptionUtility.LogException(9, "PrepareGLEntryForOnlinePosting.GetEntry", "15.3", "GetEntry", "p_gpe_code- " + gpe_code);
                return false;
            }


        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "PrepareGLEntryForOnlinePosting.GetEntry", "15.3", "GetEntry", "p_gpe_code- " + gpe_code);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void BtnPostSystemApproved_Click(object sender, EventArgs e)
    {
        if (chkAutoSystemApproved.Checked == false)
        {
            lblError.Text = "Only System Approved Claim can be posted in Bulk";
            return;
        }
        if (GridView1.Rows.Count < 1)
        {
            lblError.Text = "There is no Claim to post, use Search button to load Claim";
            return;
        }
        ViewState["PostAll"] = "Y";
        Button1_Click(sender, e);


        int index = 0;

        //DataTable DT_GridView = (DataTable)ViewState["GridView"];
        //DataTable 

        //for(int i=0 i<10;i++)

        for (int iLoop = 0; iLoop < GridView1.Rows.Count; iLoop++)
        {
            index = iLoop;
            string gpe_code = GridView1.DataKeys[index].Value.ToString();
            //UMER_MOD 140621 starts
            string approved = GridView1.Rows[index].Cells[7].Text.ToString();
            string posted = GridView1.Rows[index].Cells[6].Text.ToString();
            string cashacc = GridView1.Rows[index].Cells[29].Text.ToString();


            if (posted.ToLower().Equals("no") && approved.ToLower().Equals("yes"))
            {
                string error = "";

                if (Can_Post(gpe_code, ref error))
                {
                    ViewState["index"] = index;

                    string amount = "";
                    string drAccount = "";
                    string crAccount = "";
                    string narrative1 = "";
                    string narrative2 = "";
                    string narrative3 = "";

                    if (GetGLEntryForOnlinePosting(gpe_code, ref drAccount, ref crAccount, ref amount, ref narrative1, ref narrative2, ref narrative3))
                    {
                        pnlPost.Visible = true;
                        txtAmount.Text = amount;
                        //IRFAN_MOD  20150618  Claims only
                        txtAccount.Text = crAccount;
                        txtSettlementAcc.Text = drAccount;
                        //txtAccount.Text = drAccount;
                        //txtSettlementAcc.Text = crAccount;

                        txtNarrative1.Text = narrative1;
                        txtNarrative2.Text = narrative2;
                        txtNarrative3.Text = narrative3;

                        //POST
                        btnSavePost_Click(sender, e);
                        //if (lblError.Text != "Transaction Posted")
                        //{
                        //    lblError.Text = lblError.Text + " GPE CODE" + gpe_code + " Claim "+ narrative2;
                        //    return;
                        //}
                        //
                    }
                    else
                    {
                        lblError.Text = "Error in Retrieving the GL Entity. GPE CODE" + gpe_code + narrative2;
                        //return;
                    }
                }
                else
                {
                    lblError.Text = error;
                    //return;
                }

            }
            else
            {
                if (posted.ToLower().Equals("yes"))
                {
                    lblError.Text = "Cannot post an already posted entry. GPE CODE" + gpe_code;
                    // return;
                }

            }
        }

        Button1_Click(sender, e);
        lblError.Text = "Number of Claims which are not posted -- " + GridView1.Rows.Count;
        ViewState["PostAll"] = "N";

    }

}
