using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OracleClient;
using System.Linq;
using SingleRateRequestMsgProcessor;
using EjadaMQProcessor;

public partial class VisaSettlSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            loadProducts();
            loadCurrency();
            load_CS_Events();
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;

            e.Row.Cells[10].Visible = false;
          
        }
    }
    protected void GridViewVAT_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;

            e.Row.Cells[10].Visible = false;

        }
    }
    private void loadCurrency()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_Currency_lookup";

        oracom.Parameters.Add("p_output_cur", OracleType.Cursor).Direction = ParameterDirection.Output;
         
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

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

        oracon.Close();
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_product_lookup";

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

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

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

        oracon.Close();
    }

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

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_cs_Event";

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

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

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

        oracon.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblError.Text = ""; Lbl_MannualDif.Text = ""; lbl_CR_DR_Diff.Text = "";
        if (ddlProducts.SelectedIndex == -1)
            lblError.Text = "Please Choose the product";
        else if (dt_SettleDate.date.Length == 0)
            lblError.Text = "Please Choose the date";
        else
        {
            string status = getFileStatus();
            if (status == "1")
            {
                pnlResult.Visible = true;
                pnlBR.Visible = false;
                loadResult(500, 1);
            }
            if (status == "5")
            {
                DataSet ds = getCurrencies();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    callMQ(ds.Tables[0].Rows[i]["CARDSCHEME_CURR_NAME"].ToString(), ds.Tables[0].Rows[i]["EXCHG_ACCOUNT_NUMBER"].ToString());
                }
                processVatEntrie();
                pnlResult.Visible = true;
                pnlBR.Visible = false;
                loadResult(500, 1);
            }
            if (status == "9")
            {
                lblError.Text = "Files not loaded";
                pnlResult.Visible = true;
                pnlBR.Visible = false;
                loadResult(500, 1);
            }

        }
    }
    private void loadResult(int pageSize, int pageNumber)
    {
        loadResult(pageSize, pageNumber, "E");
        loadResult(pageSize, pageNumber, "V");
    }

        private void loadResult( int pageSize, int pageNumber,string VAT_ENT_FLAG)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_visa_settl";
        
        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_product", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_currency", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_num", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        // V VAT or E Entry 
        oracom.Parameters.Add("p_Evnt_Code", OracleType.VarChar).Direction = ParameterDirection.Input;

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


        oracom.Parameters["p_product"].Value = ddlProducts.SelectedValue;

        oracom.Parameters["p_Evnt_Code"].Value = VAT_ENT_FLAG;

        

        if ( ddlProducts.SelectedValue=="2")
             oracom.Parameters["p_currency"].Value =  ddlCurrency.SelectedValue;
        else
             oracom.Parameters["p_currency"].Value =  OracleString.Null;

        //2017019
        string date =  dt_SettleDate.date;
        //if (date[6] == '0')
        //    date = date.Remove(6, 1);
        oracom.Parameters["p_date"].Value = date;

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

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

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

        if (VAT_ENT_FLAG == "E")
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
       
        //Calculate Sum and display in Footer Row
        Session["File_Number"] = "";
        Session["CS_GL_CODE"] = "";
       
        if (ds.Tables[0].Rows.Count > 0)
        {
            decimal drTotal = ds.Tables[0].AsEnumerable().Sum(row => row.Field<decimal>("DbAmt"));
            decimal crTotal = ds.Tables[0].AsEnumerable().Sum(row => row.Field<decimal>("CRAmt"));
            GridView1.FooterRow.Cells[4].Text = "Total";
            GridView1.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            GridView1.FooterRow.Cells[5].Text = drTotal.ToString("N2");
            GridView1.FooterRow.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            GridView1.FooterRow.Cells[6].Text = crTotal.ToString("N2");

            lbl_CR_DR_Diff.Text = (crTotal-drTotal).ToString("N2");
            
            lbl_File_Number.Text = ds.Tables[0].Rows[0]["File_Number"].ToString();
            lbl_Status.Text = ds.Tables[0].Rows[0]["CS_GL_CODE"].ToString();

            ViewState["status"] = ds.Tables[0].Rows[0]["CS_GL_Code"].ToString();
           //.ro=Cells[6].Visible = false;
            
          
        }

        }
        else// if (VAT_ENT_FLAG == "V") V or Full Event Code
        {
            if (ds.Tables[0].Rows.Count > 0)
                ViewState["status"] = ds.Tables[0].Rows[0]["CS_GL_Code"].ToString();
            GridViewVAT.DataSource = ds;
            GridViewVAT.DataBind();

        }

        
    }


    
  

    protected void GridViewVAT_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        Lbl_MannualDif.Text = "";
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridViewVAT.DataKeys[index].Value.ToString();

            lbl_Detail.Text = GridViewVAT.Rows[index].Cells[0].Text.ToString();
            pnlBR.Visible = true;
            Page.SetFocus(lbl_Detail.ClientID);

            //pnlBR.Enabled = false;
            ViewState["rowID"] = rowID;

            string evnt_code = GridViewVAT.Rows[index].Cells[7].Text.ToString();
            ViewState["evnt_code"] = evnt_code;

            loadDetails(rowID, evnt_code, Helper.gridSize(), 1);

            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "scrollWin()", true);

        }
      
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        Lbl_MannualDif.Text = "";
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
         
            lbl_Detail.Text = GridView1.Rows[index].Cells[0].Text.ToString();
            pnlBR.Visible = true;
            Page.SetFocus(lbl_Detail.ClientID);

            //pnlBR.Enabled = false;
            ViewState["rowID"]=rowID;
            
            string evnt_code = GridView1.Rows[index].Cells[7].Text.ToString();
            ViewState["evnt_code"] = evnt_code;

            loadDetails(rowID, evnt_code, Helper.gridSize(), 1);

            loadResult(500, 1, evnt_code);
            rowBttn.Visible = false;

            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "scrollWin()", true);

        }
        else if (e.CommandName.Equals("d"))
        {
          

             int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();

            string evnt_code = GridView1.Rows[index].Cells[7].Text.ToString();
            int file_no = Convert.ToInt16( GridView1.Rows[index].Cells[8].Text);

            string error = ""; ;

            string status_flag = GridView1.Rows[index].Cells[9].Text.ToString();
            string editable_flag = GridView1.Rows[index].Cells[10].Text.ToString();

            if (status_flag.Substring(0, 6).ToLower().Equals("cancel") || status_flag.ToLower().Equals("entered"))
            {
                if (editable_flag.ToUpper().Equals("Y"))
                {
                    bool saved = Delete_CS_Entry(evnt_code, file_no, ref error);

                    if (saved)
                    {
                        Lbl_MannualDif.Text = "Successfuly deleted Entry";
                        loadResult(500, 1); ;
                    }
                    else
                    {
                        Lbl_MannualDif.Text = error;
                    }
                }
                else
                    Lbl_MannualDif.Text = "Deletion Failed - Auto Entry cannot be  deleted";
            }
            else
                Lbl_MannualDif.Text = "Deletion Failed - Entry can be deleted only when status is Entered/Caneled";
        }

    }
    private bool Delete_CS_Entry(string event_code, int File_no, ref string error)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.Delete_SettlEntry";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_File_no", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_event_code", 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_current_uid"].Value = Session["uid"].ToString();
        oracom.Parameters["p_File_no"].Value = Convert.ToDecimal( File_no);
        oracom.Parameters["p_event_code"].Value = event_code;

    
      

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        if (a == "0")
            return true;
        else
            return false;
    }

    private void loadDetails(string rowID, string evnt_code, int pageSize, int pageNumber)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.get_visa_settl_trans";

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

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

        oracom.Parameters["p_csr_code"].Value = rowID;

        oracom.Parameters["p_page_size"].Value = pageSize;
        oracom.Parameters["p_page_num"].Value = pageNumber;
        oracom.Parameters["p_lang"].Value = "EN";

        oracom.Parameters["p_evnt_code"].Value = evnt_code;

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

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

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

        GridView2.DataSource = ds;
        GridView2.DataBind();
        if (pageNumber == 1)
        {
            ddlPages2.Items.Clear();
            for (int i = 1; i <= nop; i++)
            {
                ddlPages2.Items.Add(i.ToString());
            }
        }
    }

    protected void ddlPages2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string rowID = ViewState["rowID"].ToString();
        string evnt_code=  ViewState["evnt_code"].ToString();
        loadDetails(rowID, evnt_code, Helper.gridSize(), int.Parse(ddlPages2.SelectedValue));
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlBR.Visible = false;
        rowBttn.Visible = true;
        //pnlResult.Enabled = true;
        loadResult(500, 1, "V");
    }

     
    private bool Approval_Reject_GenerateGl( int product,string Currency_code, string   settleDate,int file_number, string status)
    {
      
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.SubmitForApprvl";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_product", OracleType.Number).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_currency", OracleType.VarChar).Direction = ParameterDirection.Input;
        
        oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_file_number", 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_status"].Value = status;
        oracom.Parameters["p_product"].Value = product;

        if (product == 2) //  travel vard 
            oracom.Parameters["p_currency"].Value = Currency_code;
        else
            oracom.Parameters["p_currency"].Value = "";


        oracom.Parameters["p_file_number"].Value = file_number;
        oracom.Parameters["p_date"].Value = "20" + settleDate;
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        OracleDataAdapter da = new OracleDataAdapter(oracom);
 

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

        if (a == "0")
        {
            lblError.Text = oracom.Parameters["p_res_msg"].Value.ToString();
            return true;
        }
        else
        {
            lblError.Text = oracom.Parameters["p_res_msg"].Value.ToString();
            return false;
        }

    }
    protected void ShowMannualGL(string event_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 = "VISA_SETTL_INFO_EDITOR.get_cs_Event_Details";

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

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


        oracom.Parameters["P_Evnt_code"].Value = event_code;
        
        
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        oracon.Close();

        lbl_Narr.Text =   lbl_FromAcct.Text = lbl_ToAcct.Text = lbl_Sett_Curr.Text = "";

        if (ds.Tables[0].Rows.Count >= 1)
        {
            lbl_Narr.Text = ds.Tables[0].Rows[0]["Narrative"].ToString();
            lbl_FromAcct.Text = ds.Tables[0].Rows[0]["From_Account"].ToString();
            lbl_ToAcct.Text = ds.Tables[0].Rows[0]["To_Account"].ToString();
            lbl_Sett_Curr.Text = ds.Tables[0].Rows[0]["Settl_Currency"].ToString();
          
        }
        


    }
    bool SaveMannualDifference(string Evnt_code, int product, decimal Amount, string Amount_Sign, int File_Nmber, string Settl_Date)
    {    
        
         string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INFO_EDITOR.AddManualGLEntry";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Product", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Evnt_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Amt", OracleType.VarChar).Direction = ParameterDirection.Input;
         oracom.Parameters.Add("P_Amt_Sign", OracleType.VarChar).Direction = ParameterDirection.Input;
         oracom.Parameters.Add("P_File_Number", OracleType.Number).Direction = ParameterDirection.Input;
         oracom.Parameters.Add("P_Settl_Date", 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_current_uid"].Value = Session["uid"].ToString();
         oracom.Parameters["p_product"].Value = product;
        oracom.Parameters["P_Evnt_code"].Value = Evnt_code;
        oracom.Parameters["P_Amt"].Value = Amount;
        oracom.Parameters["P_Amt_Sign"].Value = Amount_Sign;   
        oracom.Parameters["p_file_number"].Value = File_Nmber;
        oracom.Parameters["P_Settl_Date"].Value = Settl_Date;   
     
   

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

        if (a == "0")
        {
            Lbl_MannualDif.Text = oracom.Parameters["p_res_msg"].Value.ToString();
            return true;
        }
        else
        {
            Lbl_MannualDif.Text = "Failed to Complete Request.";
            return false;
        }
    }
    protected void btnSavePost_Click(object sender, EventArgs e)
    {
        Decimal Amount= 0;
        if(! Decimal.TryParse(txtAmount.Text.ToString(), out Amount) || Amount <=0  )
        {
            Lbl_MannualDif.Text = "Enter Valid Amount";
            return;
        }
        Lbl_MannualDif.Text = "";

        if (SaveMannualDifference(ddl_CS_EventList.SelectedValue,
              Convert.ToInt32(ddlProducts.SelectedValue),
              Convert.ToDecimal(txtAmount.Text),
               ddlGLtype.SelectedValue,
              Convert.ToInt32(lbl_File_Number.Text),
              dt_SettleDate.date
              ))
        {
            btnSavePost.Enabled = false;
            pnlAddMannualDiff.Visible = false;
            loadResult(500, 1); ;

        }
     //   SaveMannualDifference(ddl_CS_EventList.Selev File_Number ,)
    }
    protected void btnCancelPost_Click(object sender, EventArgs e)
    {
        pnlAddMannualDiff.Visible = false;
        btnSavePost.Enabled = true;
    }
    protected void ButtonSubmitForApproval_Click(object sender, EventArgs e)
    {
        int File_Number = Convert.ToInt32(lbl_File_Number.Text);

        Approval_Reject_GenerateGl(Convert.ToInt32(ddlProducts.SelectedValue),ddlCurrency.SelectedValue, dt_SettleDate.date, File_Number, "001");
          loadResult(500, 1);;
      //  bttn_Approval.Enabled = false;
    }//

    protected void btnReprocess_Click(object sender, EventArgs e)
    {
        DataSet ds1 = getCurrencies();
        for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
        {
            callMQ(ds1.Tables[0].Rows[i]["CARDSCHEME_CURR_NAME"].ToString(), ds1.Tables[0].Rows[i]["EXCHG_ACCOUNT_NUMBER"].ToString());
        }

        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INTERFACE.REPROCESS_SETTLEMENT_ENTRIES";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Product", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Currency", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Settl_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_Rsp", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        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_current_uid"].Value = Session["uid"].ToString();
        oracom.Parameters["p_product"].Value = ddlProducts.SelectedValue;
        oracom.Parameters["p_Currency"].Value = ddlCurrency.SelectedValue;
        oracom.Parameters["P_Settl_Date"].Value = dt_SettleDate.date;
        oracom.Parameters["p_Status"].Value = ViewState["status"].ToString();
        oracom.Parameters["p_lang"].Value = "en";



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

        if (a == "0")
        {
            loadProducts();
            loadCurrency();
            load_CS_Events();
            Lbl_MannualDif.Text = oracom.Parameters["p_res_msg"].Value.ToString();
        }
        else
        {
            Lbl_MannualDif.Text = "Failed to Complete Requeset.";
        }
    }//
  
    protected void bttn_AddDiff_Click(object sender, EventArgs e)
    {
        pnlAddMannualDiff.Visible = true;
        btnSavePost.Enabled = true;
        Lbl_MannualDif.Text = "";
           
        ShowMannualGL(ddl_CS_EventList.SelectedValue);
      
   

    }
  
    protected void ButtonRejectForApproval_Click(object sender, EventArgs e)
    {
        int File_Number = Convert.ToInt32(lbl_File_Number.Text);

        Approval_Reject_GenerateGl(Convert.ToInt32(ddlProducts.SelectedValue), ddlCurrency.SelectedValue, dt_SettleDate.date, File_Number, "008");
        loadResult(500, 1); ;
      //  bttn_Reject.Enabled=false;
    }
    protected void bttn_ApproveAndCreateGL(object sender, EventArgs e)
    {
        int File_Number = Convert.ToInt32(lbl_File_Number.Text);

        Approval_Reject_GenerateGl(Convert.ToInt32(ddlProducts.SelectedValue), ddlCurrency.SelectedValue, dt_SettleDate.date, File_Number, "002");
        loadResult(500, 1); ;
    }
    protected void ddl_CS_EventList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowMannualGL(ddl_CS_EventList.SelectedValue);
    }
    protected void btnGenDtlReport_Click(object sender, EventArgs e)
    {

        string p1 = "20"+dt_SettleDate.date;
        string p2 = ddlProducts.SelectedValue;
          string p3 ="";
          if( ddlProducts.SelectedValue=="2")
              p3 = ddlCurrency.SelectedValue;

          string UrlString1 = "rpt=1001&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3;
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);
    }


    protected void btnGenSumReport_Click(object sender, EventArgs e)
    {
        string p1 = "20" + dt_SettleDate.date;

        string UrlString1 = "rpt=1003&p1=" + p1;
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);

    }

    string getFileStatus()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INTERFACE.ChkFileProcessStatus";

        oracom.Parameters.Add("P_FILE_DATE", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_PrcStatus", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("P_FileNumber", OracleType.Number).Direction = ParameterDirection.Output;

        oracom.Parameters["P_FILE_DATE"].Value = dt_SettleDate.date;



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

        ViewState["filenumber"] = oracom.Parameters["P_FileNumber"].Value.ToString();

        return a;
    }//
    void callMQ(string currency,string account)
    {
        string result, msgRecieved, respCode;
        string strQMgrName = ConfigurationManager.AppSettings["QMgrName"];
        string timeOut = ConfigurationManager.AppSettings["TimeOut"];
        string strSendQName = ConfigurationManager.AppSettings["SendQName"];
        string strRecieveQName = ConfigurationManager.AppSettings["RecieveQName"];
        string strChannel = ConfigurationManager.AppSettings["Channel"];
        string strHost = ConfigurationManager.AppSettings["Host"];
        string strPort = ConfigurationManager.AppSettings["Port"];
        string strUser = DecryptLibrary.DecryptLibrary.DecryptData(ConfigurationManager.AppSettings["UserName"]);
        string strPwd = DecryptLibrary.DecryptLibrary.DecryptData(ConfigurationManager.AppSettings["Password"]);
        string strCon = DecryptLibrary.DecryptLibrary.DecryptData(ConfigurationSettings.AppSettings["DBConnection"]);
        string strMWLog = ConfigurationManager.AppSettings["MWLOG"]; 
        string BuySellCurrency = ConfigurationManager.AppSettings["ExchgRateCurFlg"];

        SingleRateRequestMsgProcessorCls SingleRateRequestMsgProObj;

        string UID_SID = Session["uid"] as string + HttpContext.Current.Session.SessionID;
        EjadaMQProcessorCls EjadaMQProcessorObj = new EjadaMQProcessorCls(strQMgrName, strSendQName, strRecieveQName, strChannel, strHost, strPort, strUser, strPwd, strMWLog, UID_SID);

        //instantiate Object
        SingleRateRequestMsgProObj = new SingleRateRequestMsgProcessorCls(strCon, strMWLog);


        //Build the outgoing message
        string strOutGoingMsg = SingleRateRequestMsgProObj.BuildMsg("SAIB  ", BuySellCurrency, "SAR",currency, "100", "123456", account);
        //string strOutGoingMsg = SingleRateRequestMsgProObj.BuildMsg("SAIB  ", "S", "SAR", "USD", "100", "123456", account);
                                                                   
        //MQ Call to Send Receive
        string strIncomingMsg = EjadaMQProcessorObj.ProcessMessage(strOutGoingMsg, "60000",false);

        //Parse the incoming message
        string strAmtChg , strAmtTot, strXchgSel, strUseSel, strXchgBuy, strUseBuy, strAmtVat ;
        string strRspCode = SingleRateRequestMsgProObj.ParseRespCode(strIncomingMsg,out strAmtChg , out strAmtTot, out strXchgSel, out strUseSel, out strXchgBuy, out strUseBuy, out strAmtVat );

        //lblError.Text = strIncomingMsg + " : " + strAmtChg+ " - " + strAmtTot+ " - " + strXchgSel+ " - " + strUseSel+ " - " + strXchgBuy+ " - " + strUseBuy + " - " + strAmtVat +  " - " + strXchgSel + " - " + strXchgBuy;
        updateExchangeRate(currency, strXchgSel, strXchgBuy);
    }
    DataSet getCurrencies()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INTERFACE.Get_All_Curr_Rec";

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

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

        return ds;
    }

    void updateExchangeRate(string Currency, string sellRate, string buyRate)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INTERFACE.UpdtExChgRate";

        oracom.Parameters.Add("P_CurCode", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_sellRate", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_buyRate", OracleType.VarChar).Direction = ParameterDirection.Input;

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

        oracom.Parameters["P_CurCode"].Value = Currency;
        oracom.Parameters["P_sellRate"].Value = sellRate;
        oracom.Parameters["P_buyRate"].Value = buyRate;

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

    void processVatEntrie()
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "VISA_SETTL_INTERFACE.PROCESS_CS_SETTLEMENT_ENTRIES";

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

        oracom.Parameters.Add("P_RSP", OracleType.VarChar,250).Direction = ParameterDirection.Output;

        oracom.Parameters["P_FILE_NUMBER"].Value = ViewState["filenumber"].ToString();



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