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;

public partial class ECOMGENERATE_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", "");
        }
    }
    protected void btnGenReport_Click(object sender, EventArgs e)
    {
        btnPostAll.Enabled = true;
        string p1 = DateTime.Now.ToString("yyMMdd");
        string p2 = "0";
        string UrlString1 = "rpt=518&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 = PostAll(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 string getDirectory()
    {
        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_Reg.get_Fees_Max";

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

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

        oracon.Close();

        return "";

    }
    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 = "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 = "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;
        }
    }
}