using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SunWeb.Model;
using System.Web.Services;
using CLS_BL;
using CLS_DL;
using CLS_PL;
namespace SUNWEB
{
    public partial class CampaignReport : System.Web.UI.Page
    {
        BL_API ObjApi = new BL_API();
        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 S = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        DateTime MonthstartDt;
        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        MySqlCommand Cmdv = new MySqlCommand();
        // User Variables 
        string UserId = string.Empty;
        string UserName = string.Empty;
        string PortalTypeId = string.Empty;
        string cd_Id = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string IsMultiBranch = string.Empty;
        string IsSuperAdmin = string.Empty;
        string ActiveCallvalue = string.Empty;
        // License Variables ..
        string PID = string.Empty;
        string Page_Acess = string.Empty;
        string Read_Acess = string.Empty;
        string Write_Acess = string.Empty;
        string Isdowld_Acess = string.Empty;
        string Rcd_id = string.Empty;
        string RPortalid = string.Empty;
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string L_MaxUserCnt = string.Empty;
        string L_campaignReport = string.Empty;
        
        string Branchidval = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;

        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string ISHQ = string.Empty;
        DataSet DS = new DataSet();
        
        string Vw_Chk = string.Empty;
        string BranchAlias = string.Empty;

        string AES_Key = string.Empty;
        string en_userName = string.Empty;

        string en_CustomerName = string.Empty;
        string en_Mobile = string.Empty;
        string en_password = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "52";    // This is for Campaign Report Page 


            if (!IsPostBack)
            {
               // txtStartDt.Text = Curr_DateOnly;
               // txtEndDt.Text = Curr_DateOnly;
            }
            MonthstartDt = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == "") && ((Convert.ToBoolean(Session["IsMultiBranch"])) == true))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();

                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "Select Branch")
                    {
                        ActiveCall();
                    }
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                }
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                //LoadInventoryListGrid(cd_BranchAlias, txtStartDt.Text, txtEndDt.Text);
            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName();
                    ActiveCall();
                }
            }
            else if (((Session["PortalTypeId"].ToString() != "") || (Convert.ToBoolean(Session["IsSuperAdmin"])) == false) && ((Convert.ToBoolean(Session["IsMultiBranch"])) == false))
            {
                cmbBranches.Enabled = false;
                cmbBranches.Visible = false;

                Cm_id = ObjSel.GETcmid(Session["Cd_Id"].ToString());  // Get cm_id
                HQ = ObjSel.Get_HQ(Cm_id);     // Get the HQ Value
                Curr_BranchFullName = ObjSel.GetBranchFullName(HQ, Session["Branch_HQ"].ToString());
                lblBranchAliasName.ForeColor = System.Drawing.Color.DodgerBlue;
                lblBranchAliasName.Text = "Branch Name" + "&nbsp;" + "-" + "&nbsp;" + Curr_BranchFullName + "&nbsp; ";

                cd_BranchAlias = Session["Branch_HQ"].ToString();
                Datapath = Session["DataPath"].ToString();
                PortalTypeId = Session["PortalTypeId"].ToString();

                DT = ObjUserBL.GetPageRightsDetailsPid(Session["UserId"].ToString(), PID);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }
                if ((Session["L_campaignReport"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    // lblMsgTotalSelection.Text = "Item Sale  Between" + "&nbsp;&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  and  &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {
                            btnGetReports.Visible = true;

                            if (cmbBranches.Visible == false)
                            {
                                //  BindGridview(Session["Branch_HQ"].ToString(), txtStartDt.Text, txtEndDt.Text);
                            }

                        }


                    }

                }

            }
            else
            {

            }


        }


        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
            ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
            if (ActiveCallvalue == "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            }


            ISHQ = ObjMain.ExecuteScalar("select BranchName from " + ActiveCallvalue + ".branches where Branchname='" + ActiveCallvalue + "' and branchID=1;");  //now 

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
                UPDATEBRANCH(ActiveCallvalue);
            }

            cd_BranchAlias = ActiveCallvalue;
            Curr_BranchFullName = ObjUserBL.GetBranchCurr_BranchVal(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), cd_BranchAlias);
            Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Curr_BranchFullName);  // This will Return BranchName( Actual Database Name )
            cmbBranches.Items.FindByText(Curr_BranchFullName).Selected = true;
            Branchidval = ObjUserBL.GetBranchIDbyName(ActiveCallvalue, ActiveCallvalue);
            if (Branchidval != "")
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Session["Fistbranch"].ToString());

            }


            if (DT.Rows.Count > 0)
            {
                Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                Session["Page_Acess"] = Page_Acess;
                Session["Read_Acess"] = Read_Acess;
                Session["Write_Acess"] = Write_Acess;
                Session["Isdowld_Acess"] = Isdowld_Acess;
            }
            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + ActiveCallvalue + "';");
            RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
            DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);
            if (DtLicense.Rows.Count > 0)
            {
                L_campaignReport = DtLicense.Rows[0]["CAMPAIGNREPORT"].ToString();
                Session["L_campaignReport"] = L_campaignReport;
            }
            if (Session["L_campaignReport"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {
                Response.Redirect("DeniedPage.aspx");
            }
            else
            {
                // btnExcel.Enabled = true;
                // lblMsgTotalSelection.Text = "Total Sale Selection Between" + "&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                // BindGridview(ActiveCallvalue, txtStartDt.Text, txtEndDt.Text);
            
                string _pagename = System.IO.Path.GetFileName(Request.Url.ToString());
                if (_pagename.Equals("CampaignReport.aspx"))
                {

                    if (!IsPostBack)
                    {
                        Load_CampaingnType();
                    }
                    string Is_CashBack = Get_Generic_Webordingini(BranchAlias, "46");
                    string Is_PointRedemption = Get_Generic_Webordingini(BranchAlias, "47");

                    if (Is_CashBack.Equals("1"))
                    {
                        cmbCampaignType.SelectedIndex = 0;
                    }
                    else if (Is_PointRedemption.Equals("1"))
                    {
                        cmbCampaignType.SelectedIndex = 1;
                    }


                    if (cmbCampaignType.SelectedIndex == 0)
                    {
                        GridA.Visible = true;
                        GridB.Visible = false;

                        // Call the Grid methods 
                        LoadITransHistory(Session["Branch_HQ"].ToString());
                        LoadMemberDataList(Session["Branch_HQ"].ToString());
                    }
                    else if (cmbCampaignType.SelectedIndex==1)
                    {
                        GridA.Visible = false;
                        GridB.Visible = true;

                        // Method Call for PointsRedemption 
                        LoadPointRedeption_Data(Session["Branch_HQ"].ToString());
                        LoadMemberDataList(Session["Branch_HQ"].ToString());
                        

                    }


                }

            }

        }


        public void LoadBranchName()
        {
            DtNormalUser = ObjUserBL.GetBranchOnLoginValues8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtNormalUser.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtNormalUser;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void LoadBranchName4SuperAdmin()
        {
            DtSuperAdmin = ObjUserBL.GetBranchOnSuperAdmin8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtSuperAdmin.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtSuperAdmin;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void UPDATEBRANCH(string ActiveCallvalue)
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + ActiveCallvalue + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);
            }
            else
            {
                Response.Redirect("Login.aspx");

            }

        }


        protected void btnGetReports_Click(object sender, EventArgs e)
        {


            if (cmbCampaignType.SelectedItem.Text == "CashBack")
            {
                //  If MobileNo Only 
                if (txtStartDt.Text == "" && txtEndDt.Text == "" && txtRMobileNo.Text != "")
                {
                    LoadIcashBack_By_Mobile(Session["Branch_HQ"].ToString(), hidMobileNo.Value);
                }
                //  If MobileNo and between Dates  
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text != "")
                {
                    LoadIcashBack_By_Date_and_Mobile(Session["Branch_HQ"].ToString(), txtRMobileNo.Text, txtStartDt.Text, txtEndDt.Text);
                }
                // If beween two dates /No mobileNo
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text == "")
                {
                    LoadIcashBack_By_Date_Alone(Session["Branch_HQ"].ToString(), txtStartDt.Text, txtEndDt.Text);
                }

            }
            else if (cmbCampaignType.SelectedItem.Text == "Points")
            {
                // Bring the All search Points related  Functions 


                if (txtStartDt.Text == "" && txtEndDt.Text == "" && txtRMobileNo.Text != "")
                {
                    
                    LoadPoints_By_Mobile(Session["Branch_HQ"].ToString(), hidMobileNo.Value);

                }
                //  If MobileNo and between Dates  
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text != "")
                {
                    
                    LoadPoints_By_Mobile_and_Dates(Session["Branch_HQ"].ToString(), txtRMobileNo.Text, txtStartDt.Text, txtEndDt.Text);
                }
                // If beween two dates /No mobileNo
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text == "")
                {
                    
                    LoadPoints_By_Dates_Alone(Session["Branch_HQ"].ToString(), txtStartDt.Text, txtEndDt.Text);
                }




            }





        }

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Write("CampaignReport.aspx");
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, cmbBranches.SelectedItem.Value);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();
                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }

                if (cmbBranches.SelectedIndex == 1)
                {

                    BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                    Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                    UPDATEBRANCH(Cmb_BranchName);
                    Session["BranchDB"] = Cmb_BranchName;
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + Cmb_BranchName + "';");
                    RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
                    DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);
                    Session["BranchDB"] = Cmb_BranchName;
                }
                if (DtLicense.Rows.Count > 0)
                {
                    L_campaignReport = DtLicense.Rows[0]["CAMPAIGNREPORT"].ToString();
                    Session["L_campaignReport"] = L_campaignReport;
                }
                if ((Session["L_campaignReport"] == null) || Session["L_campaignReport"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    if (cmbBranches.SelectedIndex == 1)
                    {
                        // btnExcel.Enabled = true;
                        #region  Validation Between HQ Branch and Normal Branch 
                        BranchFullName = cmbBranches.SelectedItem.Text;          // This will Return Branch FullName 
                        if (BranchFullName == "HQ")
                        {
                            BranchAlias = Session["Branch_HQ"].ToString();
                        }
                        else
                        {
                            BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                            Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                            BranchAlias = Cmb_BranchName;
                        }

                        #endregion

                        string Is_CashBack = Get_Generic_Webordingini(BranchAlias, "46");
                        string Is_PointRedemption = Get_Generic_Webordingini(BranchAlias, "47");
                        string _pagename = System.IO.Path.GetFileName(Request.Url.ToString());
                        if (_pagename.Equals("CampaignReport.aspx"))
                        {
                            if (Is_CashBack.Equals("1"))
                             {
                              cmbCampaignType.SelectedIndex = 0;
                             }
                             else if(Is_PointRedemption.Equals("1"))
                             {
                               cmbCampaignType.SelectedIndex = 1;
                             }
                             if (cmbCampaignType.SelectedIndex==0)
                             {
                               GridA.Visible = true;
                               GridB.Visible = false;
                               // Call the Grid methods 
                              LoadITransHistory(Session["Branch_HQ"].ToString());
                              LoadMemberDataList(Session["Branch_HQ"].ToString());
                                
                            }
                            else if(cmbCampaignType.SelectedIndex==1)
                            {
                                GridA.Visible = false;
                                GridB.Visible = true;
                                // Method Call for PointsRedemption 
                                LoadPointRedeption_Data(Session["Branch_HQ"].ToString());
                                LoadMemberDataList(Session["Branch_HQ"].ToString());
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("DeniedPage.aspx");
                    }
                }
                else if (Session["Write_Acess"].ToString() == "N")
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    #region  Validation Between HQ Branch and Normal Branch 
                    BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
                    if (BranchFullName == "HQ")
                    {
                        BranchAlias = Session["Branch_HQ"].ToString();
                    }
                    else
                    {
                        BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                        Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                        BranchAlias = Cmb_BranchName;
                    }
                    Session["BranchDB"] = BranchAlias;
                    #endregion
                    string _pagename = System.IO.Path.GetFileName(Request.Url.ToString());
                    string Is_CashBack = Get_Generic_Webordingini(BranchAlias, "46");
                    string Is_PointRedemption = Get_Generic_Webordingini(BranchAlias, "47");
                    if (_pagename.Equals("CampaignReport.aspx"))
                    {



                        if (Is_CashBack.Equals("1"))
                        {
                            cmbCampaignType.SelectedIndex = 0;
                        }
                        else if (Is_PointRedemption.Equals("1"))
                        {
                            cmbCampaignType.SelectedIndex = 1;
                        }



                        if (cmbCampaignType.SelectedIndex ==0)
                        {
                            GridA.Visible = true;
                            GridB.Visible = false;

                            // Call the Grid methods 
                            LoadITransHistory(Session["Branch_HQ"].ToString());
                            LoadMemberDataList(Session["Branch_HQ"].ToString());
                        }
                        else if (cmbCampaignType.SelectedIndex==1)
                        {
                            GridA.Visible = false;
                            GridB.Visible = true;

                            // Method Call for PointsRedemption 
                            LoadPointRedeption_Data(Session["Branch_HQ"].ToString());
                            LoadMemberDataList(Session["Branch_HQ"].ToString());


                        }




                    }

                }
            }
            UPDATEBRANCH(BranchAlias);
           
        }
        public void LoadITransHistory(string HQ)
        {
           
            Dt2 = ObjSel.Get_iTransHistory(HQ);
            grdWebTransHistory.DataSource = Dt2;
            grdWebTransHistory.DataBind();
        }

        public void LoadPointRedeption_Data(string HQ)
        {



            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }


            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion



            //grdMember.DataSource = DtMemberList;
            //grdMember.DataBind();

            DT = ObjSel.Get_Point_Redemption(HQ);

            if (DT.Rows.Count > 0)
            {
                foreach (DataRow row in DT.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }

                grdMain.DataSource = DT;
                grdMain.DataBind();
            }
            else
            {
                grdMain.DataSource =null;
                grdMain.DataBind();

            }

        
        }




        public void LoadIcashBack_By_Date_and_Mobile(string HQ,string username,string D1,string D2)
        {

            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }


            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }
            #endregion

            string en_username = ObjSel.EncryptString(username, AES_Key);

            DataTable Dt = new DataTable();
            
            Dt = ObjSel.Get_iTrans_CashBack_By_MobileNo_and_Date(HQ, en_username, D1,D2);


            if (Dt.Rows.Count > 0)
            {
                foreach (DataRow row in Dt.Rows)
                {
                    string value = row.Field<string>("UserName");
                   row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }

              
                grdWebTransHistory.DataSource = Dt;
                grdWebTransHistory.DataBind();

            }
            else
            {
                grdWebTransHistory.DataSource =null;
                grdWebTransHistory.DataBind();
            }



     
        }
        
        //  Here Mobile no column string username 
        public void LoadIcashBack_By_Mobile(string HQ,string username)
        {

            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }


            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }
            #endregion

            DataTable Dt = new DataTable();

            string en_username = ObjSel.EncryptString(username, AES_Key);
            Dt = ObjSel.Get_iTrans_CashBack_By_MobileNo(HQ, en_username);


            if (Dt.Rows.Count > 0)
            {
                foreach (DataRow row in Dt.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }


                grdWebTransHistory.DataSource = Dt;
                grdWebTransHistory.DataBind();

            }
            else
            {
                grdWebTransHistory.DataSource = null;
                grdWebTransHistory.DataBind();

            }



            
        }
        public void LoadIcashBack_By_Date_Alone(string HQ, string D1,string D2)
        {
            DataTable Dt = new DataTable();
            Dt = ObjSel.Get_iTrans_CashBack_By_Date_Alone(HQ,D1,D2);
            grdWebTransHistory.DataSource = Dt;
            grdWebTransHistory.DataBind();
        }


        // Load Points Related Search Functions 
        public void LoadPoints_By_Mobile(string HQ,string username )
        {

            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion

            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion

            string en_username = ObjSel.EncryptString(username, AES_Key);

            DataTable Dt = new DataTable();

            Dt = ObjSel.Get_Point_Redemption_By_MobileNo(HQ, en_username);




            if (Dt.Rows.Count > 0)
            {
                foreach (DataRow row in Dt.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }


                grdMain.DataSource = DT;
                grdMain.DataBind();
            }
            else
            {
                grdMain.DataSource =null;
                grdMain.DataBind();
            }


        }
        public void LoadPoints_By_Mobile_and_Dates(string HQ, string  username, string D1, string D2)
        {

            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion

            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion


            //#1 Encrypt Username 

            DataTable Dt = new DataTable();
            en_userName = ObjSel.EncryptString(username, AES_Key);

            Dt = ObjSel.Get_Point_Redemption_By_MobileNo_Date(HQ, en_userName, D1, D2);
            if (Dt.Rows.Count > 0)
            {
                foreach (DataRow row in Dt.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }


                grdMain.DataSource = Dt;
                grdMain.DataBind();
            }
            else
            {
                grdMain.DataSource =null;
                grdMain.DataBind();

            }


        }
        public void LoadPoints_By_Dates_Alone(string HQ,string D1, string D2)
        {


            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion

            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion



            DataTable Dt = new DataTable();
            Dt = ObjSel.Get_Point_Redemption_By_Date_Alone(HQ, D1, D2);

            if (Dt.Rows.Count > 0)
            {
                foreach (DataRow row in Dt.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }


                grdMain.DataSource = Dt;
                grdMain.DataBind();
            }
            else
            {
                grdMain.DataSource = null;
                grdMain.DataBind();
            }

            

        }




        protected void grdWebTransHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string orderid = grdWebTransHistory.DataKeys[e.Row.RowIndex].Value.ToString();
                GridView grdview = e.Row.FindControl("grdWebtrans") as GridView;

                GridView grdviewAdjustment = e.Row.FindControl("grdAdjustment") as GridView;
                string str_Check_Adjustment = "ADJ";
                if (orderid.Contains(str_Check_Adjustment))
                {

                    grdviewAdjustment.DataSource= ObjSel.Get_CashBack_Adjustment_Details (Session["Branch_HQ"].ToString(), orderid);
                    grdviewAdjustment.DataBind();
                }
                else
                {
                    grdview.DataSource = ObjSel.Get_iTransHistory_WebTrans(BranchAlias, orderid);
                    grdview.DataBind();

                }

               }


            decimal _total_cb_Redeem=0;
            decimal _total_cb_Earn=0;
            string Cal_Query = string.Empty;
            string Cal_Query1 = string.Empty;

            // Call the Total Condition Method 
            if (cmbCampaignType.SelectedItem.Text == "CashBack")
            {
                // If MobileNo Only 
                if (txtStartDt.Text == "" && txtEndDt.Text == "" && txtRMobileNo.Text != "")
                {
                 Cal_Query="SELECT Sum(amount) FROM "+ Session["Branch_HQ"].ToString() + ".icashbacktrans where TransType = 1 AND Mobile ="+ hidMobileNo.Value + " ;";
                 _total_cb_Redeem = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query));
                 Cal_Query1 = "SELECT Sum(amount) FROM "+ Session["Branch_HQ"].ToString() + ".icashbacktrans  where TransType=0 and Mobile=" + hidMobileNo.Value + " ;";
                 _total_cb_Earn =decimal.Parse(ObjMain.ExecuteScalar(Cal_Query1));
                }
                // If MobileNo and between Dates  
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text != "")
                {
                 Cal_Query = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() + ".icashbacktrans where TransType = 1 AND Mobile =" + hidMobileNo.Value + " and date(BusinessDate) between  '"+txtStartDt.Text+"' and  '"+txtEndDt.Text +"'";
                _total_cb_Redeem = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query));
                 Cal_Query1 = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() + ".icashbacktrans  where TransType=0 and Mobile=" + hidMobileNo.Value + " and date(BusinessDate) between  '" + txtStartDt.Text + "' and  '" + txtEndDt.Text + "' ;";
                _total_cb_Earn = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query1));

                }
                // If beween two dates / No mobileNo
                else if (txtStartDt.Text != "" && txtEndDt.Text != "" && txtRMobileNo.Text == "")
                {
                  Cal_Query = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() + ".icashbacktrans where TransType = 1 AND  date(BusinessDate) between  '" + txtStartDt.Text + "' and  '" + txtEndDt.Text + "'";
                 _total_cb_Redeem = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query));
                  Cal_Query1 = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() + ".icashbacktrans  where TransType=0  and date(BusinessDate) between  '" + txtStartDt.Text + "' and  '" + txtEndDt.Text + "' ;";
                 _total_cb_Earn = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query1));

                }
                else // It will return all Result without any condition 
                {
                 Cal_Query = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() +  ".icashbacktrans where TransType = 1 ;";
                _total_cb_Redeem = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query));
                 Cal_Query1 = "SELECT Sum(amount) FROM " + Session["Branch_HQ"].ToString() + ".icashbacktrans where  TransType= 0 ;";
                _total_cb_Earn = decimal.Parse(ObjMain.ExecuteScalar(Cal_Query1));

                }
                 
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                (e.Row.FindControl("lblTotalCB_Redeem") as Label).Text = _total_cb_Redeem.ToString();
                (e.Row.FindControl("lblTotalCB_Earn") as Label).Text = _total_cb_Earn.ToString();
            }



        }

        protected void grdWebtransdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView grid = (GridView)sender;
                string orderid = grid.DataKeys[e.Row.RowIndex].Value.ToString();
                Label _ItemId = (Label)e.Row.FindControl("lblItemid");

                GridView grdview_condiment = e.Row.FindControl("grdWebtransdetailsCondiment") as GridView;
                grdview_condiment.DataSource =ObjSel.Get_iTransHistory_WebTransDetailscondiment(BranchAlias, orderid, _ItemId.Text);
                grdview_condiment.DataBind();
            }

        }
        protected void grdWebtrans_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (cmbBranches.Visible == true)
            {
                #region  Validation Between HQ Branch and Normal Branch 
                BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;

                }
                #endregion
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView grid = (GridView)sender;
                string orderId = grid.DataKeys[e.Row.RowIndex].Value.ToString();
                GridView grdview = e.Row.FindControl("grdWebtransdetails") as GridView;

              
               grdview.DataSource = ObjSel.Get_iTransHistory_WebTransDetails(BranchAlias, orderId);
               grdview.DataBind();
            


                
            }
        }

        protected void grdMember_SelectedIndexChanged(object sender, EventArgs e)
        {
            // XSS Attack Prevention 
            if (cmbBranches.Visible == false)
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    // btnExcel.Enabled = true;
                    // HQ Validation    02-12-21
                    BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                    Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                    UPDATEBRANCH(Cmb_BranchName);
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;
                }
              }




            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion



            DataTable DTMemberDL = new DataTable();
            string  RCustomerID = (grdMember.SelectedRow.FindControl("lblCustomerID") as Label).Text;
            DTMemberDL = ObjSel.ReadMemberInformations(Session["Branch_HQ"].ToString(), RCustomerID);
            string R_Mobile = DTMemberDL.Rows[0]["Mobile"].ToString();
            string R_Cid    = DTMemberDL.Rows[0]["CID"].ToString();
            string R_CustomerName = DTMemberDL.Rows[0]["CustomerName"].ToString();
            string R_UserName = DTMemberDL.Rows[0]["UserName"].ToString();
            string R_MembershipType = DTMemberDL.Rows[0]["MembershipType"].ToString();
            // Assign   Read value to Textbox control
            txtRMobileNo.Text = ObjSel.DecryptString(R_UserName, AES_Key);       // Here MobileNo is  Username value 
            hidMobileNo.Value = ObjSel.DecryptString(R_UserName, AES_Key);
        }

        protected void btnMemSearch_Click(object sender, EventArgs e)
        {
            BranchAlias = Session["Branch_HQ"].ToString();
            Load_SearchData(BranchAlias, txtMobSearch.Text.ToString().Trim());
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalMemberInformationList();", true);


        }

        public void Load_SearchData(string BranchNam, string SearchTxt)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Session["Branch_HQ"].ToString();
            }


            string QuerySrc = "Select CustomerID,Mobile,CustomerName  From   {0}.icustomers  Where  Mobile like CONCAT('%', @Mobile, '%')  ;";
            string command = string.Format(QuerySrc, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Mobile",SearchTxt),
                   };

            DataTable DtMemberList = new DataTable();

            DtMemberList = ObjMain.NewGetData_DT(command, parameters);

            //N   DtCateoryList = ObjCloudMain.GetData_DT(QuerySrc);

            if (DtMemberList.Rows.Count > 0)
            {
                grdMember.DataSource = DtMemberList;
                grdMember.DataBind();
            }
            else
            {
                LoadMemberDataList(BranchAlias);
            }
        }

        public void LoadMemberDataList(string sDb)
        {

            DataTable DtMemberList = new DataTable();

            DtMemberList = ObjSel.GetMemberList(sDb);

            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion



            #region UserName Decrption 
            if (Cmb_BranchName != "" || BranchAlias != "")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    AES_Key = ObjApi.Get_AESKey(BranchAlias);
                }
                else
                {
                    AES_Key = ObjApi.Get_AESKey(Cmb_BranchName);
                }


            }

            #endregion



            //grdMember.DataSource = DtMemberList;
            //grdMember.DataBind();



            if (DtMemberList.Rows.Count > 0)
            {
                foreach (DataRow row in DtMemberList.Rows)
                {
                    string value = row.Field<string>("UserName");
                    row.SetField("UserName", ObjSel.DecryptString(value, AES_Key));
                    string value1 = row.Field<string>("CustomerName");
                    row.SetField("CustomerName", ObjSel.DecryptString(value1, AES_Key));
                }

                grdMember.DataSource = DtMemberList;
                grdMember.DataBind();
            }
            else
            {
                grdMember.DataSource = null;
                grdMember.DataBind();
            }







        }


        decimal _tot_Points;
        decimal _tot_RdPoints;
        protected void grdMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            #region  Validation Between HQ Branch and Normal Branch 
            BranchFullName = cmbBranches.SelectedItem.Text;             // This will Return Branch FullName 
            if (BranchFullName == "HQ")
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;         // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }
            Session["BranchDB"] = BranchAlias;
            #endregion


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string orderid = grdMain.DataKeys[e.Row.RowIndex].Value.ToString();
                string _orderid = ObjMain.ExecuteScalar("select orderid from "+ BranchAlias + ".webtrans  where Webtransid='"+ orderid + "';");

                GridView grdview2 = e.Row.FindControl("grdWebtrans2") as GridView;
                GridView grdviewAdjustment2 = e.Row.FindControl("grdAdjustment2") as GridView;

                string str_Check_Adjustment = "ADJ";
                if (orderid.Contains(str_Check_Adjustment))
                {

                    grdviewAdjustment2.DataSource = ObjSel.Get_CashBack_Adjustment_Details(Session["Branch_HQ"].ToString(), orderid);
                    grdviewAdjustment2.DataBind();
                }
                else
                {
                    grdview2.DataSource = ObjSel.Get_iTransHistory_WebTrans(BranchAlias, _orderid);
                    grdview2.DataBind();

                }


            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gvInventoryList = e.Row.FindControl("grdMain") as GridView;    
                Label totalpoints = e.Row.FindControl("lblRPointsEarn") as Label;
                Label totalRdpoints = e.Row.FindControl("lblRDPoints") as Label;
                _tot_Points += Convert.ToDecimal(totalpoints.Text);
                _tot_RdPoints += Convert.ToDecimal(totalRdpoints.Text);

            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                (e.Row.FindControl("lblTotalPoint") as Label).Text = _tot_Points.ToString();
                (e.Row.FindControl("lblTotalRDPoint") as Label).Text = _tot_RdPoints.ToString();
            }

        }
        protected void grdWebtrans2_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (cmbBranches.Visible == true)
            {
                #region  Validation Between HQ Branch and Normal Branch 
                BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;

                }
                #endregion
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView grid = (GridView)sender;
                string orderId = grid.DataKeys[e.Row.RowIndex].Value.ToString();
                GridView grdview2 = e.Row.FindControl("grdWebtransdetails2") as GridView;
                grdview2.DataSource = ObjSel.Get_iTransHistory_WebTransDetails(BranchAlias, orderId);
                grdview2.DataBind();
            }


        }

        protected void grdWebtransdetails2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;
            }


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView grid = (GridView)sender;
                string orderid = grid.DataKeys[e.Row.RowIndex].Value.ToString();
                Label _ItemId = (Label)e.Row.FindControl("lblItemid");

                GridView grdview_condiment = e.Row.FindControl("grdWebtransdetailsCondiment2") as GridView;
                grdview_condiment.DataSource = ObjSel.Get_iTransHistory_WebTransDetailscondiment(BranchAlias, orderid, _ItemId.Text);
                grdview_condiment.DataBind();
            }


        }

        protected void cmbCampaignType_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbCampaignType.SelectedItem.Text == "CashBack") 
            {
                //gridA.Visible = true; 
                //gridB.Visible = false;
                // Call the Grid methods 
                LoadITransHistory(Session["Branch_HQ"].ToString());
                //LoadMemberDataList(Session["Branch_HQ"].ToString());
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "return ShowGridA();", true);
                GridA.Visible = true;
                GridB.Visible = false;

            }
            else if(cmbCampaignType.SelectedItem.Text == "Points")
            {
                //gridA.Visible = false;
                //gridB.Visible = true;
                // Method Call for PointsRedemption 
                LoadPointRedeption_Data(Session["Branch_HQ"].ToString());
                // LoadMemberDataList(Session["Branch_HQ"].ToString());
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "return ShowGridB();", true);
                GridB.Visible = true;
                GridA.Visible = false;


            }
        }


        public void Load_CampaingnType()
        {
            if (cmbBranches.Visible == true)
            {
                #region  Validation Between HQ Branch and Normal Branch 
                BranchFullName = cmbBranches.SelectedItem.Text;                // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;            // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;

                }
                #endregion
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }

            string Is_CashBack = Get_Generic_Webordingini(BranchAlias, "46");
            string Is_PointRedemption = Get_Generic_Webordingini(BranchAlias, "47");


            if (Is_CashBack.Equals("1"))
            {
                cmbCampaignType.Items.Add("Points");
                cmbCampaignType.Items.Insert(0, "CashBack");
                cmbCampaignType.DataBind();
                // Load the Cashback value in Dropdownlist 
                cmbCampaignType.SelectedIndex = 0;
            }
            else if (Is_PointRedemption.Equals("1"))
            {
                cmbCampaignType.Items.Add("Points");
                cmbCampaignType.Items.Insert(0, "CashBack");
                cmbCampaignType.DataBind();
                // Load the Point value in Dropdownlist 
                cmbCampaignType.SelectedIndex = 1;

            }

            

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {

            cmbCampaignType.SelectedIndex = 0;
            txtRMobileNo.Text = string.Empty;
            txtStartDt.Text = string.Empty;
            txtEndDt.Text = string.Empty;


        }

        private string Get_Generic_Webordingini(string DB, string IniID)
        {
            if (cmbBranches.Visible == true)
            {
                #region  Validation Between HQ Branch and Normal Branch 
                BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;     // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    BranchAlias = Cmb_BranchName;

                }
                #endregion
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }




            BL_API objAPI = new BL_API();
            string returnvalue = string.Empty;
            string sBearerToken = objAPI.GetBarerToken();

            string JSONRetValue = objAPI.API_GET_INIWebOrderingIniValue(sBearerToken, DB, IniID);
            var sObject = "[" + JsonConvert.DeserializeObject(JSONRetValue).ToString().Trim() + "]";
            DataTable dtResult = (DataTable)JsonConvert.DeserializeObject(sObject, (typeof(DataTable)));
            if (dtResult.Rows.Count == 0)
            {
                returnvalue = "";
            }
            else
            {
                returnvalue = (dtResult.Rows[0]["ini_value"]).ToString();
            }
            return returnvalue;
        }

    }
}