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 Business_GENERATE_GL_FILES : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnGenReport.Enabled = true;
            btnPostAll.Enabled = false;
            ExceptionUtility.LogException(0, "", "79", "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=503&p1=" + p1 + "&p2=" + p2;
        ExceptionUtility.LogException(0, "", "79.462", "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, "", "79.463", "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, "", "79.463", "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_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_settl_cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_settl_amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_claim_amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_claim_cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_atm_gl_amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_atm_gl_cnt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_monthly_amt", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_monthly_cnt", 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")
            {
                lblFileName.Text = oracom.Parameters["p_file_name"].Value.ToString();
                lblclaimGlAmount.Text = oracom.Parameters["p_claim_amt"].Value.ToString();
                lblClaimGlCount.Text = oracom.Parameters["p_claim_cnt"].Value.ToString();
                lblATMGlAmount.Text = oracom.Parameters["p_atm_gl_amt"].Value.ToString();
                lblATMGlCount.Text = oracom.Parameters["p_atm_gl_cnt"].Value.ToString();
                lblSettGlAmount.Text = oracom.Parameters["p_settl_amt"].Value.ToString();
                lblSettGlCount.Text = oracom.Parameters["p_settl_cnt"].Value.ToString();
                lblMonthlySAMAGLAmount.Text = oracom.Parameters["p_monthly_amt"].Value.ToString();
                lblMonthlySAMAGLCount.Text = oracom.Parameters["p_monthly_cnt"].Value.ToString();
                ExceptionUtility.LogException(0, "GENERATE_GL_FILE.GW56_files", "463.1", "PostAll", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "GENERATE_GL_FILE.GW56_files", "463.1", "PostAll", "");
                return false;
            }

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GENERATE_GL_FILE.GW56_files","463.1", "PostAll", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
      
}