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_DailySettlement : System.Web.UI.Page
{

    private void setParam(int ID,string newValue)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "SYSTEM_PARAM_EDITOR.edit_parameter";

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

        oracom.Parameters.Add("p_param_id", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_new_value", 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_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.Parameters["p_param_id"].Value = ID;


        oracom.Parameters["p_new_value"].Value = newValue;
       
       
        oracom.ExecuteNonQuery();
        oracon.Close();
    }

    protected void btnClaimReport_Click(object sender, EventArgs e)
    {
        string p1 = dtp_filter_sttlDate.date;
        string p2=   DateTime.ParseExact(p1, "yyMMdd", null).AddDays(1).ToString("yyMMdd");

        setParam(310,p1);
        setParam(311, p2);
        
        string UrlString1 = "rpt=1004&p1=" + p1 + "&p2="+p2 ;
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);
    }
    protected void btnGenReport_Click(object sender, EventArgs e)
    {

        string p1 = dtp_filter_sttlDate.date;


        string UrlString1 = "rpt=1002&p1=" + p1 ;
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                "window.open('../Reports/Default.aspx?" + UrlString1 + "','Report')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.pnl_result.Visible = false;
            this.btn_print.Visible = false;
            tlfFilesArea.Visible = false;


        }
        lbl_debitPOS_SamaAmount.Attributes.Add("oncontextmenu", "return false;");
        lbl_creditPOS_SamaAmount.Attributes.Add("oncontextmenu", "return false;");
        lbl_debitATM_SamaAmount.Attributes.Add("oncontextmenu", "return false;");
        lbl_creditATM_SamaAmount.Attributes.Add("oncontextmenu", "return false;");

        lbl_debitPOS_SamaAmount.Attributes.Add("oncopy", "return false;");
        lbl_creditPOS_SamaAmount.Attributes.Add("oncopy", "return false;");
        lbl_debitATM_SamaAmount.Attributes.Add("oncopy", "return false;");
        lbl_creditATM_SamaAmount.Attributes.Add("oncopy", "return false;");
    }

    private Dictionary<string, double> searchSamaFiles(string settlementDate, ref string errorMsg)
    {
        Dictionary<string, double> resultParams = new Dictionary<string, double>();
        int resCode;

        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "SAMA_SETTL_OPERATIONS.GET_SAMA_SETTL_FILES_AMOUNTS";

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

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

        oracom.Parameters.Add("p_sama_atm_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_atm_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_pos_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_pos_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_gcc_atm_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_gcc_atm_debit", OracleType.Number).Direction = ParameterDirection.Output;

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

        oracom.Parameters.Add("p_atm_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_atm_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_pos_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_pos_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_gcc_atm_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_gcc_atm_debit", OracleType.Number).Direction = ParameterDirection.Output;

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

        oracom.Parameters.Add("p_atm_credit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_atm_debit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_pos_credit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_pos_debit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_gcc_atm_credit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_gcc_atm_debit_diff", OracleType.Number).Direction = ParameterDirection.Output;

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

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

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


        oracom.Parameters.Add("p_EXTL_CR_ATM_ACQ_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXTL_DR_ATM_ISS_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXTL_CR_POS_ACQ_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXTL_DR_POS_ISS_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXCEPTION_CR_ATM_ACQ_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXCEPTION_DR_ATM_ISS_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXCEPTION_DR_POS_ISS_AMT", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_EXCEPTION_CR_POS_ACQ_AMT", OracleType.Number).Direction = ParameterDirection.Output;

        // IRFAN_MOD 190929       
        oracom.Parameters.Add("p_sama_Ecom_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_sama_Ecom_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_Ecom_credit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_Ecom_debit", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_Ecom_credit_diff", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_Ecom_debit_diff", OracleType.Number).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_lang"].Value = "EN";
        oracom.Parameters["p_settl_date"].Value = settlementDate;

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

        oracon.Close();

        ViewState["mode"] = oracom.Parameters["p_status"].Value.ToString();
        /*
            0 - New
            1 - Posted
        */

        errorMsg = oracom.Parameters["p_res_msg"].Value.ToString();
        resCode = int.Parse(oracom.Parameters["p_res_code"].Value.ToString());

        resultParams.Add("p_res_code", resCode);

        if (resCode == 0)
        {
            resultParams.Add("p_sama_atm_credit", double.Parse(oracom.Parameters["p_sama_atm_credit"].Value.ToString()));
            resultParams.Add("p_sama_atm_debit", double.Parse(oracom.Parameters["p_sama_atm_debit"].Value.ToString()));
            resultParams.Add("p_sama_pos_credit", double.Parse(oracom.Parameters["p_sama_pos_credit"].Value.ToString()));
            resultParams.Add("p_sama_pos_debit", double.Parse(oracom.Parameters["p_sama_pos_debit"].Value.ToString()));
            resultParams.Add("p_sama_gcc_atm_credit", double.Parse(oracom.Parameters["p_sama_gcc_atm_credit"].Value.ToString()));
            resultParams.Add("p_sama_gcc_atm_debit", double.Parse(oracom.Parameters["p_sama_gcc_atm_debit"].Value.ToString()));


            //  SAIB CR IRFAN_MOD 190929
            resultParams.Add("p_sama_Ecom_credit", double.Parse(oracom.Parameters["p_sama_Ecom_credit"].Value.ToString()));
            resultParams.Add("p_sama_Ecom_debit", double.Parse(oracom.Parameters["p_sama_Ecom_debit"].Value.ToString()));
            //

            resultParams.Add("p_sama_gcc_atm_credit_FEES", double.Parse(oracom.Parameters["p_sama_gcc_atm_credit_FEES"].Value.ToString()));
            resultParams.Add("p_sama_gcc_atm_debit_FEES", double.Parse(oracom.Parameters["p_sama_gcc_atm_debit_FEES"].Value.ToString()));

            resultParams.Add("p_EXTL_DR_POS_ISS_AMT", double.Parse(oracom.Parameters["p_EXTL_DR_POS_ISS_AMT"].Value.ToString()));
            resultParams.Add("p_EXTL_CR_POS_ACQ_AMT", double.Parse(oracom.Parameters["p_EXTL_CR_POS_ACQ_AMT"].Value.ToString()));
            resultParams.Add("p_EXTL_DR_ATM_ISS_AMT", double.Parse(oracom.Parameters["p_EXTL_DR_ATM_ISS_AMT"].Value.ToString()));
            resultParams.Add("p_EXTL_CR_ATM_ACQ_AMT", double.Parse(oracom.Parameters["p_EXTL_CR_ATM_ACQ_AMT"].Value.ToString()));


            resultParams.Add("p_EXCEPTION_DR_POS_ISS_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_DR_POS_ISS_AMT"].Value.ToString()));
            resultParams.Add("p_EXCEPTION_CR_POS_ACQ_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_CR_POS_ACQ_AMT"].Value.ToString()));
            resultParams.Add("p_EXCEPTION_DR_ATM_ISS_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_DR_ATM_ISS_AMT"].Value.ToString()));
            resultParams.Add("p_EXCEPTION_CR_ATM_ACQ_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_CR_ATM_ACQ_AMT"].Value.ToString()));

            if (ViewState["mode"].ToString().Equals("1"))
            {
                resultParams.Add("p_atm_credit", double.Parse(oracom.Parameters["p_atm_credit"].Value.ToString()));
                resultParams.Add("p_atm_debit", double.Parse(oracom.Parameters["p_atm_debit"].Value.ToString()));
                resultParams.Add("p_pos_credit", double.Parse(oracom.Parameters["p_pos_credit"].Value.ToString()));
                resultParams.Add("p_pos_debit", double.Parse(oracom.Parameters["p_pos_debit"].Value.ToString()));
                //  SAIB CR IRFAN_MOD 190929
                resultParams.Add("p_Ecom_credit", double.Parse(oracom.Parameters["p_Ecom_credit"].Value.ToString()));
                resultParams.Add("p_Ecom_debit", double.Parse(oracom.Parameters["p_Ecom_debit"].Value.ToString()));
                //
                resultParams.Add("p_gcc_atm_credit", double.Parse(oracom.Parameters["p_gcc_atm_credit"].Value.ToString()));
                resultParams.Add("p_gcc_atm_debit", double.Parse(oracom.Parameters["p_gcc_atm_debit"].Value.ToString()));

                if (oracom.Parameters["p_gcc_atm_credit_FEES"].Value.ToString() == "")
                    oracom.Parameters["p_gcc_atm_credit_FEES"].Value = "0";
                if (oracom.Parameters["p_gcc_atm_debit_FEES"].Value.ToString() == "")
                    oracom.Parameters["p_gcc_atm_debit_FEES"].Value = "0";
                resultParams.Add("p_gcc_atm_credit_FEES", double.Parse(oracom.Parameters["p_gcc_atm_credit_FEES"].Value.ToString()));
                resultParams.Add("p_gcc_atm_debit_FEES", double.Parse(oracom.Parameters["p_gcc_atm_debit_FEES"].Value.ToString()));

                resultParams.Add("p_atm_credit_diff", double.Parse(oracom.Parameters["p_atm_credit_diff"].Value.ToString()));
                resultParams.Add("p_atm_debit_diff", double.Parse(oracom.Parameters["p_atm_debit_diff"].Value.ToString()));
                resultParams.Add("p_pos_credit_diff", double.Parse(oracom.Parameters["p_pos_credit_diff"].Value.ToString()));
                resultParams.Add("p_pos_debit_diff", double.Parse(oracom.Parameters["p_pos_debit_diff"].Value.ToString()));
                resultParams.Add("p_gcc_atm_credit_diff", double.Parse(oracom.Parameters["p_gcc_atm_credit_diff"].Value.ToString()));
                resultParams.Add("p_gcc_atm_debit_diff", double.Parse(oracom.Parameters["p_gcc_atm_debit_diff"].Value.ToString()));
                //  SAIB CR IRFAN_MOD 190929
                resultParams.Add("p_Ecom_credit_diff", double.Parse(oracom.Parameters["p_Ecom_credit_diff"].Value.ToString()));
                resultParams.Add("p_Ecom_debit_diff", double.Parse(oracom.Parameters["p_Ecom_debit_diff"].Value.ToString()));
                //
                if (oracom.Parameters["p_gcc_atm_credit_diff_FEES"].Value.ToString() == "")
                    oracom.Parameters["p_gcc_atm_credit_diff_FEES"].Value = "0";
                if (oracom.Parameters["p_gcc_atm_debit_diff_FEES"].Value.ToString() == "")
                    oracom.Parameters["p_gcc_atm_debit_diff_FEES"].Value = "0";

                resultParams.Add("p_gcc_atm_credit_diff_FEES", double.Parse(oracom.Parameters["p_gcc_atm_credit_diff_FEES"].Value.ToString()));
                resultParams.Add("p_gcc_atm_debit_diff_FEES", double.Parse(oracom.Parameters["p_gcc_atm_debit_diff_FEES"].Value.ToString()));

                resultParams.Add("p_is_span_amounts_updatable", double.Parse(oracom.Parameters["p_is_span_amounts_updatable"].Value.ToString()));
                resultParams.Add("p_is_gcc_amounts_updatable", double.Parse(oracom.Parameters["p_is_gcc_amounts_updatable"].Value.ToString()));
                /*
                                resultParams.Add("p_EXTL_DR_POS_ISS_AMT", double.Parse(oracom.Parameters["p_EXTL_DR_POS_ISS_AMT"].Value.ToString()));
                                resultParams.Add("p_EXTL_CR_POS_ACQ_AMT", double.Parse(oracom.Parameters["p_EXTL_CR_POS_ACQ_AMT"].Value.ToString()));
                                resultParams.Add("p_EXTL_DR_ATM_ISS_AMT", double.Parse(oracom.Parameters["p_EXTL_DR_ATM_ISS_AMT"].Value.ToString()));
                                resultParams.Add("p_EXTL_CR_ATM_ACQ_AMT", double.Parse(oracom.Parameters["p_EXTL_CR_ATM_ACQ_AMT"].Value.ToString()));


                                resultParams.Add("p_EXCEPTION_DR_POS_ISS_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_DR_POS_ISS_AMT"].Value.ToString()));
                                resultParams.Add("p_EXCEPTION_CR_POS_ACQ_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_CR_POS_ACQ_AMT"].Value.ToString()));
                                resultParams.Add("p_EXCEPTION_DR_ATM_ISS_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_DR_ATM_ISS_AMT"].Value.ToString()));
                                resultParams.Add("p_EXCEPTION_CR_ATM_ACQ_AMT", double.Parse(oracom.Parameters["p_EXCEPTION_CR_ATM_ACQ_AMT"].Value.ToString()));
                  */

            }
        }

        return resultParams;
    }
    private string getNumber(string amount)
    {
        return amount.Replace(",", "");
    }
    protected double Get_Sama_Summary()
    {
        string settmelentDate = this.dtp_filter_sttlDate.date;
        string errorMsg = "";

        double resCode = -1;

        Dictionary<string, double> results = this.searchSamaFiles(settmelentDate, ref errorMsg);

        resCode = results["p_res_code"];


        if (resCode == 0)
        {
            this.pnl_result.Visible = true;
            //this.pnl_searchCriteria.Enabled = false;
            //this.enableResultControls();
            //this.btn_save.Enabled = true;

            this.lbl_creditATM_SamaAmount.Text = results["p_sama_atm_credit"].ToString("N2");
            this.lbl_debitATM_SamaAmount.Text = results["p_sama_atm_debit"].ToString("N2");
            this.lbl_creditPOS_SamaAmount.Text = results["p_sama_pos_credit"].ToString("N2");
            this.lbl_debitPOS_SamaAmount.Text = results["p_sama_pos_debit"].ToString("N2");
               //  SAIB CR IRFAN_MOD 190929
            this.lbl_debitECOM_SamaAmount.Text = results["p_sama_Ecom_debit"].ToString("N2"); ;
            this.lbl_creditECOM_SamaAmount.Text = results["p_sama_Ecom_credit"].ToString("N2");
             //  SAIB CR IRFAN_MOD 190929

            this.lbl_debitPos_Rvslamt.Text = results["p_EXTL_DR_POS_ISS_AMT"].ToString("N2");
            this.lbl_CreditPos_Rvslamt.Text = results["p_EXTL_CR_POS_ACQ_AMT"].ToString("N2");
            this.lbl_debitATM_Rvslamt.Text = results["p_EXTL_DR_ATM_ISS_AMT"].ToString("N2");
            this.lbl_creditATM_Rvslamt.Text = results["p_EXTL_CR_ATM_ACQ_AMT"].ToString("N2");

            this.lbl_DebitPos_Unmtch.Text = results["p_EXCEPTION_DR_POS_ISS_AMT"].ToString("N2");
            this.lbl_CreditPos_Unmtch.Text = results["p_EXCEPTION_CR_POS_ACQ_AMT"].ToString("N2");
            this.lbl_DebitATM_Unmtch.Text = results["p_EXCEPTION_DR_ATM_ISS_AMT"].ToString("N2");
            this.lbl_CreditATM_Unmtch.Text = results["p_EXCEPTION_CR_ATM_ACQ_AMT"].ToString("N2");

            this.lbl_debitPOS_diff.Text = (results["p_pos_debit"] -results["p_sama_pos_debit"]    ).ToString("N2");
            this.lbl_creditPOS_diff.Text = (results["p_pos_credit"] - results["p_sama_pos_credit"] ).ToString("N2");
            this.lbl_debitATM_diff.Text = (results["p_atm_debit"] - results["p_sama_atm_debit"]    ).ToString("N2");
            this.lbl_creditATM_diff.Text = (results["p_atm_credit"] - results["p_sama_atm_credit"] ).ToString("N2");
            //  SAIB CR IRFAN_MOD 190929
            this.lbl_debitECOM_diff.Text = (results["p_Ecom_debit"] - results["p_sama_Ecom_debit"] ).ToString("N2");
            this.lbl_creditECOM_diff.Text = (results["p_Ecom_credit"] - results["p_sama_Ecom_credit"] ).ToString("N2");
            //
            
   
          


            this.lbl_SpanTotal.Text = (results["p_sama_atm_credit"] + results["p_sama_pos_credit"] - results["p_sama_atm_debit"] - results["p_sama_pos_debit"]
                 + results["p_sama_Ecom_credit"] - results["p_sama_Ecom_debit"]).ToString("N2");

            if (ViewState["mode"].ToString().Equals("0"))
            {
                //  SAIB CR IRFAN_MOD 190929
                this.lbl_creditECOM_BankAmount.Text = "";
                this.lbl_debitECOM_BankAmount.Text = "";
                this.lbl_creditECOM_diff.Text = "";
                this.lbl_debitECOM_diff.Text = "";

                //
                this.lbl_creditATM_BankAmount.Text = "";
                this.lbl_debitATM_BankAmount.Text = "";
                this.lbl_creditPOS_BankAmount.Text = "";
                this.lbl_debitPOS_BankAmount.Text = "";

                this.lbl_creditATM_difference.Text = "";
                this.lbl_creditPOS_difference.Text = "";
                this.lbl_creditATM_difference.Text = "";
                this.lbl_creditPOS_difference.Text = "";

                this.lbl_debitATM_difference.Text = "";
                this.lbl_debitPOS_difference.Text = "";
                this.lbl_BankTotal.Text = "";

                this.lbl_debitPos_Rvslamt.Text = "";
                this.lbl_CreditPos_Rvslamt.Text = "";
                this.lbl_debitATM_Rvslamt.Text = "";
                this.lbl_creditATM_Rvslamt.Text = "";

                this.lbl_DebitPos_Unmtch.Text = "";
                this.lbl_CreditPos_Unmtch.Text = "";
                this.lbl_DebitATM_Unmtch.Text = "";
                this.lbl_CreditATM_Unmtch.Text = "";


                this.btn_print.Enabled = false;



            }
            else if (ViewState["mode"].ToString().Equals("1"))
            {


                this.btn_print.Enabled = true;

                this.lbl_creditATM_BankAmount.Text = results["p_atm_credit"].ToString("N2");
                this.lbl_debitATM_BankAmount.Text = results["p_atm_debit"].ToString("N2");
                this.lbl_creditPOS_BankAmount.Text = results["p_pos_credit"].ToString("N2");
                this.lbl_debitPOS_BankAmount.Text = results["p_pos_debit"].ToString("N2");
                //  SAIB CR IRFAN_MOD 190929
                this.lbl_creditECOM_BankAmount.Text = results["p_Ecom_credit"].ToString("N2");
                this.lbl_debitECOM_BankAmount.Text = results["p_Ecom_debit"].ToString("N2");
          
                //
                /*
                this.lbl_creditATM_difference.Text = (results["p_atm_credit_diff"] + results["p_EXTL_CR_ATM_ACQ_AMT"] + results["p_EXCEPTION_CR_ATM_ACQ_AMT"]).ToString("N2");
                this.lbl_creditPOS_difference.Text = (results["p_pos_credit_diff"] + results["p_EXTL_CR_POS_ACQ_AMT"] + results["p_EXCEPTION_CR_POS_ACQ_AMT"]).ToString("N2");
                this.lbl_debitATM_difference.Text = (results["p_atm_debit_diff"] + results["p_EXTL_DR_ATM_ISS_AMT"] + results["p_EXCEPTION_DR_ATM_ISS_AMT"]).ToString("N2");
                this.lbl_debitPOS_difference.Text = (results["p_pos_debit_diff"] + results["p_EXTL_DR_POS_ISS_AMT"] + results["p_EXCEPTION_DR_POS_ISS_AMT"]).ToString("N2");
                */
                this.lbl_creditATM_difference.Text = (results["p_atm_credit_diff"]).ToString("N2");
                this.lbl_creditPOS_difference.Text = (results["p_pos_credit_diff"]).ToString("N2");
                this.lbl_debitATM_difference.Text = (results["p_atm_debit_diff"]).ToString("N2");
                this.lbl_debitPOS_difference.Text = (results["p_pos_debit_diff"]).ToString("N2");

                this.lbl_BankTotal.Text = (results["p_atm_credit"] + results["p_pos_credit"] - results["p_atm_debit"] - results["p_pos_debit"]
                    + results["p_Ecom_credit"] - results["p_Ecom_debit"] ).ToString("N2");
            }


            lbl_NetDiffTotal.Text = (Convert.ToDecimal(getNumber(this.lbl_BankTotal.Text)) - Convert.ToDecimal(getNumber(this.lbl_SpanTotal.Text))).ToString("N2");

        }
        else
        {
            this.lbl_error.Text = errorMsg;

        }
        return resCode;
    }


    private void Get_TLF_Amounts()
    {
        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_SUMMARY_INQUIRER.TLF_GL_TOTALS";


        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_settle_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.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

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

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

        decimal tranAmount_ReCode8 = 0, tranAmount_ReCode10 = 0, tranAmount_ReCode5 = 0, tranAmount_ReCode14 = 0, tranAmount_ReCode17 = 0,
            tranAmount_ReCode33 = 0, tranAmount_ReCode34 = 0, tranAmount_ReCode35=0;
        LabelTLF01.Text = LabelTLF02.Text = LabelTLF03.Text = LabelTLF04.Text = LabelTLF05.Text = LabelTLF06.Text = "0";
        foreach (DataRow dr in ds.Tables[0].Rows)
        { 
            

            if (dr["re_code"].ToString() == "10")
               tranAmount_ReCode10 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "5")
               tranAmount_ReCode5 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "14")
              tranAmount_ReCode14 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "17")
                tranAmount_ReCode17 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "8")
               tranAmount_ReCode8 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "33")
                tranAmount_ReCode33 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "34")
                tranAmount_ReCode34 = Convert.ToDecimal(dr["tot_amount"]);
            else if (dr["re_code"].ToString() == "35")
                tranAmount_ReCode35 = Convert.ToDecimal(dr["tot_amount"]);
            //33 34 35
            
        }
       
        LabelTLF01.Text =(tranAmount_ReCode10).ToString("N2");
         LabelTLF02.Text =(tranAmount_ReCode5).ToString("N2");
         LabelTLF03.Text =(tranAmount_ReCode14+tranAmount_ReCode8).ToString("N2");
         LabelTLF04.Text = (tranAmount_ReCode17 + tranAmount_ReCode8).ToString("N2");
        // 33 34 35 IRFAN_MOD 190929 CR
         LabelTLF05.Text = (tranAmount_ReCode34 +tranAmount_ReCode33).ToString("N2");
         LabelTLF06.Text = (tranAmount_ReCode35 + tranAmount_ReCode33).ToString("N2");

         LabelTLFTotal.Text = (+tranAmount_ReCode10 + +tranAmount_ReCode14 - tranAmount_ReCode5 -tranAmount_ReCode17
             + tranAmount_ReCode34 - tranAmount_ReCode35).ToString("N2");
        tlfFilesArea.Visible = true;

          

    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        this.lbl_error.Text = "";

        if (Get_Sama_Summary() == 0)
        {
            Get_SPAN_Ticket_Entry();
            Get_TLF_Amounts();
        }
        else
        {
            this.pnl_result.Visible = false;
            this.pnlResult_TicketEntry.Visible = false;
            tlfFilesArea.Visible = false;
        }
    }
    void Get_SPAN_Ticket_Entry()
    {
        pnlResult_TicketEntry.Visible = true;
        loadResult(50, 1);
    }




    protected void btn_clear_Click(object sender, EventArgs e)
    {
        //his.clearResultControls();
        this.dtp_filter_sttlDate.date = "";
        this.pnl_result.Visible = false;
        this.pnl_searchCriteria.Enabled = true;
    }

    //private void clearResultControls()
    //{
    //    this.txt_creditATM_amount.Text = "";
    //    this.txt_debitATM_amount.Text = "";
    //    this.txt_creditPOS_amount.Text = "";
    //    this.txt_debitPOS_amount.Text = "";
    //    this.txt_creditATM_GCC_amount.Text = "";
    //    this.txt_debitATM_GCC_amount.Text = "";

    //    this.lbl_creditATM_difference.Text = "";
    //    this.lbl_creditATM_GCC_difference.Text = "";
    //    this.lbl_creditATM_GCC_SamaAmount.Text = "";
    //    this.lbl_creditATM_SamaAmount.Text = "";
    //    this.lbl_creditPOS_difference.Text = "";
    //    this.lbl_creditPOS_SamaAmount.Text = "";
    //    this.lbl_debitATM_difference.Text = "";
    //    this.lbl_debitATM_GCC_difference.Text = "";
    //    this.lbl_debitATM_GCC_SamaAmount.Text = "";
    //    this.lbl_debitATM_SamaAmount.Text = "";
    //    this.lbl_debitPOS_difference.Text = "";
    //    this.lbl_debitPOS_SamaAmount.Text = "";

    //}

    //private void clearManualControls()
    //{
    //    this.txt_creditATM_amount.Text = "";
    //    this.txt_debitATM_amount.Text = "";
    //    this.txt_creditPOS_amount.Text = "";
    //    this.txt_debitPOS_amount.Text = "";
    //    this.txt_creditATM_GCC_amount.Text = "";
    //    this.txt_debitATM_GCC_amount.Text = "";
    //}

 

    protected void btn_print_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?rpt=222&p1=" + this.dtp_filter_sttlDate.date + "&p2=" + Session["uid"].ToString() + "','Report')", true);

    }

    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;



        }
    }
    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 loadResult(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 = "SAMA_SETTL_OPERATIONS.GET_SPAN_SETTL";

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



        //    date = date.Remove(6, 1);

        oracom.Parameters["p_date"].Value = dtp_filter_sttlDate.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();

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

        //Calculate Sum and display in Footer Row
        Session["File_Number"] = "";
        Session["CS_GL_CODE"] = "";

        lbl_CR_DR_Diff.Text = "";
        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[2].Text = "Total";
            GridView1.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            GridView1.FooterRow.Cells[3].Text = drTotal.ToString("N2");
            GridView1.FooterRow.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            GridView1.FooterRow.Cells[4].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();



        }

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        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 Approval_Reject_GenerateGl(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 = "SAMA_SETTL_OPERATIONS.SUBMITFORAPRVL";

        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_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_file_number"].Value = file_number;
        oracom.Parameters["p_date"].Value = 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")
        {
            lbl_error.ForeColor = System.Drawing.Color.DarkGreen;
            lbl_error.Text = oracom.Parameters["p_res_msg"].Value.ToString();
            return true;
        }
        else
        {
            lbl_error.Text = oracom.Parameters["p_res_msg"].Value.ToString();
            return false;
        }

    }

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

        if (Approval_Reject_GenerateGl(dtp_filter_sttlDate.date, File_Number, "001"))
            loadResult(500, 1); ;
        //  bttn_Approval.Enabled = false;
    }//
    protected void ButtonRejectForApproval_Click(object sender, EventArgs e)
    {
        int File_Number = Convert.ToInt32(lbl_File_Number.Text);

        if (Approval_Reject_GenerateGl(dtp_filter_sttlDate.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);

        if (Approval_Reject_GenerateGl(dtp_filter_sttlDate.date, File_Number, "002"))
            loadResult(500, 1); ;
    }



}