using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

public partial class Business_GENERATE_GATEWAY : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnGenReport.Enabled = true;
            btnPostAll.Enabled = false;
            ExceptionUtility.LogException(0, "", "81", "Page_Load", "");
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            if (!dgn.Equals("0"))
                rdDiscovery.Visible = false;
        }
    }

    protected void btnGenReport_Click(object sender, EventArgs e)
    {
        btnPostAll.Enabled = true;
        string p1 = DateTime.Now.ToString("yyMMdd");
        string p2 = "0";
        string reportID = "";
        if (rdMada.Checked) reportID = "515";
        if (rdEcom.Checked) reportID = "518";
        if (rdDiscovery.Checked) reportID = "515";
        string UrlString1 = "rpt="+reportID+"&p1=" + p1 + "&p2=" + p2;
        ExceptionUtility.LogException(0, "", "81.470", "btnGenReport_Click", "");
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);
    }
    protected void btnPostAll_Click(object sender, EventArgs e)
    {
        string error = "";
        ExceptionUtility.LogException(0, "", "81.469", "btnPostAll_Click", "");
        bool saved = false;
        vatfilerow.Visible = false;
        if (rdMada.Checked) saved = PostAll(ref error);
        if (rdEcom.Checked) saved = PostAllEcom(ref error);
        if (rdDiscovery.Checked) saved = PostAllDiners(ref error);
        if (saved)
        {
            lblError.Text = "Post Successful";
        }
        else
        {
            if (Helper.OracleError(error))
            {
                lblError.Text = "An Error Occured!";
                ExceptionUtility.LogException(9, "", "81.469", "btnPostAll_Click", error);
            }
            else
                lblError.Text = error;
        }
    }

    private bool PostAll(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 = "GENERATE_GL_FILE.GW56_pos_files";
            oracom.Parameters.Add("p_file_dir", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pos_settl_cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_pos_settl_amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_file_name", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("P_VAT_file_name", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_res", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            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")
            {
                ExceptionUtility.LogException(0, "GENERATE_GL_FILE.GW56_pos_files", "469.1", "PostAll", "");
                lblFileName.Text = oracom.Parameters["p_file_name"].Value.ToString();
                lblVATFileName.Text = oracom.Parameters["P_VAT_file_name"].Value.ToString();
                vatfilerow.Visible = true;
                lblPOSSettlementAmount.Text = oracom.Parameters["p_pos_settl_amt"].Value.ToString();
                lblPOSSettlementCount.Text = oracom.Parameters["p_pos_settl_cnt"].Value.ToString();
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GENERATE_GL_FILE.GW56_pos_files", "469.1", "PostAll", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GENERATE_GL_FILE.GW56_pos_files","469.1", "PostAll", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool PostAllEcom(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 = "Ecom_Merchant_Settlement.Generate_GW21_Ecom_File";
            oracom.Parameters.Add("p_file_dir", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_ECom_Settl_Cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_ECom_Settl_Amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_file_name", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_res", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_file_dir"].Value = Helper.getParamByID("612");//"ANB_ECOMGW21";
            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["v_res"].Value.ToString();
            //error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "Successful")
            {
                ExceptionUtility.LogException(0, "Ecom_Merchant_Settlement.Generate_GW21_Ecom_File", "469.1", "PostAll", "");
                lblFileName.Text = oracom.Parameters["p_file_name"].Value.ToString();
                lblPOSSettlementAmount.Text = oracom.Parameters["p_ECom_Settl_Amt"].Value.ToString();
                lblPOSSettlementCount.Text = oracom.Parameters["p_ECom_Settl_Cnt"].Value.ToString();
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "Ecom_Merchant_Settlement.Generate_GW21_Ecom_File", "469.1", "PostAll", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "Ecom_Merchant_Settlement.Generate_GW21_Ecom_File", "469.1", "PostAll", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool PostAllDiners(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 = "Pos_Merchant_Settlement.Generate_GW_DGN_File";
            oracom.Parameters.Add("p_file_dir", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("P_DGN_Settl_Cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("P_DGN_Settl_Amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_file_name", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("v_res", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            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")
            {
                ExceptionUtility.LogException(0, "Pos_Merchant_Settlement.Generate_GW_DGN_File", "469.1", "PostAll", "");
                lblFileName.Text = oracom.Parameters["p_file_name"].Value.ToString();
                lblPOSSettlementAmount.Text = oracom.Parameters["P_DGN_Settl_Amt"].Value.ToString();
                lblPOSSettlementCount.Text = oracom.Parameters["P_DGN_Settl_Cnt"].Value.ToString();
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "Pos_Merchant_Settlement.Generate_GW_DGN_File", "469.1", "PostAll", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "Pos_Merchant_Settlement.Generate_GW_DGN_File", "469.1", "PostAll", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
}