using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
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 CLS_BL;
using CLS_DL;
using CLS_PL;
using System.Data.OleDb;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Text;
namespace SUNWEB
{
    public partial class SpecialDiscount : System.Web.UI.Page
    {

        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 Dt1 = 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();
        DataTable DTSession = 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_SpecialDiscount = 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 BraNam = "";
        string FDate = "";
        string TDate = "";
        string SubQryValue = string.Empty;
        double totalSlot1 = 0, totalSlot2 = 0, totalSlot3 = 0, totalSlot4 = 0, totalAmount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "36";    // This is for Special Discount Report Page 


            if (!IsPostBack)
            {
                txtFromDt.Text = Curr_DateOnly;
                txtToDt.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_SpecialDiscount"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    // XSS Attack Prevention 
                    string StartDT = Microsoft.Security.Application.Encoder.HtmlEncode(txtFromDt.Text.Trim());
                    string EndDt = Microsoft.Security.Application.Encoder.HtmlEncode(txtToDt.Text.Trim());


                    // 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
                        {
                            btnGenerate.Visible = true;

                            if (cmbBranches.Visible == false)
                            {
                                LoadSpecialDiscountQry(Session["Branch_HQ"].ToString(), StartDT, EndDt);
                            }

                        }


                    }

                }

            }
            else
            {

            }


        }


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


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

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].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_SpecialDiscount = DtLicense.Rows[0]["SPECIALDISCOUNT"].ToString();
                Session["L_SpecialDiscount"] = L_SpecialDiscount;
            }
            if (Session["L_SpecialDiscount"].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;
                cd_BranchAlias = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);
                // BindGridview(ActiveCallvalue, txtStartDt.Text, txtEndDt.Text);
                System.Threading.Thread.Sleep(2000);

            }

        }


        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 cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            string StartDT = Microsoft.Security.Application.Encoder.HtmlEncode(txtFromDt.Text.Trim());
            string EndDt = Microsoft.Security.Application.Encoder.HtmlEncode(txtToDt.Text.Trim());

            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Redirect("SpecialDiscount.aspx", false);
            }
            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;
                }

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

                if (ISHQ != "")
                {
                    ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
                    UPDATEBRANCH(ActiveCallvalue);
                }
                else
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(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);

                }


                if (DtLicense.Rows.Count > 0)
                {
                    L_MaxUserCnt = DtLicense.Rows[0]["UserCnt"].ToString();
                    L_SpecialDiscount = DtLicense.Rows[0]["SPECIALDISCOUNT"].ToString();
                    // Create Session for License Variables 
                    Session["L_MaxUserCnt"] = L_MaxUserCnt;
                    Session["L_SpecialDiscount"] = L_SpecialDiscount;
                }
                if (Session["L_SpecialDiscount"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    Response.Redirect("DeniedPage.aspx", false);
                }
                else
                {
                    //btnExcel.Enabled = true;
                    //lblMsgTotalSelection.Text = "Group Sales Report Between" + "&nbsp;&nbsp;&nbsp;" + txtFromDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtToDt.Text;
                    lblMsgTotalSelection.Text = "Search Result - Special Discount Between" + "&nbsp;&nbsp;&nbsp;" + StartDT + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + EndDt;
                    LoadSpecialDiscountQry(cd_BranchAlias, StartDT, EndDt);

                }
            }
            UPDATEBRANCH(Cmb_BranchName);
        }

        //from here onwards report code begins
        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            // XSS Attack Prevention 
            string StartDT = Microsoft.Security.Application.Encoder.HtmlEncode(txtFromDt.Text.Trim());
            string EndDt = Microsoft.Security.Application.Encoder.HtmlEncode(txtToDt.Text.Trim());

            if (cmbBranches.Visible == false)
            {
                btnGenerate.Visible = true;
                // lblMsgTotalSelection.Text = "Search Result - Detailed Stock Between" + "&nbsp;&nbsp;&nbsp;" + txtFromDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtToDt.Text;
                lblMsgTotalSelection.Text = "Search Result - from " + "&nbsp;&nbsp;" + StartDT + "&nbsp;&nbsp;  to  &nbsp;&nbsp;" + EndDt;
                idGenReport.Attributes.Add("style", "background-color: #4c4c4c; color: White; font-size:14px;");
                LoadSpecialDiscountQry(Session["Branch_HQ"].ToString(), StartDT, EndDt);
                System.Threading.Thread.Sleep(2000);
            }
            else if (cmbBranches.Visible == true)
            {
                lblMsgTotalSelection.Text = " Search Result - Special Discount Between" + "&nbsp;&nbsp;&nbsp;" + StartDT + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + EndDt;
                Curr_BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);
                cd_BranchAlias = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Curr_BranchFullName);  // This will Return BranchName( Actual Database Name )
                BraNam = cd_BranchAlias;
                FDate = txtFromDt.Text;
                TDate = txtToDt.Text;
                LoadSpecialDiscountQry(cd_BranchAlias, StartDT, EndDt);
            }
        }

        protected void LoadSpecialDiscountQry(string BranchAlias, string D1, string D2)
        {

            // XSS Attack Prevention 
            string StartDT = Microsoft.Security.Application.Encoder.HtmlEncode(txtFromDt.Text.Trim());
            string EndDt = Microsoft.Security.Application.Encoder.HtmlEncode(txtToDt.Text.Trim());


            System.Threading.Thread.Sleep(1000);

            string sCmd;

            if (txtCustNo.Text == "")
            {
                SubQryValue = "";
            }
            else
            {
                SubQryValue = " Having K.CustomerNumber=@CustNo";
            };

            sCmd = "Select DATE_FORMAT(TransDate, '%Y-%m-%d') as TransDate,CustomerNumber,CustomerName,Round(Sum(Slot1),2) as Slot1, Round(Sum(Slot2),2) as Slot2,Round(Sum(Slot3),2) as Slot3,Round(Sum(Slot4),2) as Slot4, Round(Sum(Slot1+Slot2+Slot3+Slot4),2) as Total From ( " +
                " Select Date(T.BusinessDate) as TransDate,T.CustomerNumber as CustomerNumber,C.CustomerName as CustomerName, Sum(T.MemberDiscAmount) as Slot1,0.00 as Slot2, 0.00 as Slot3, 0.00 as Slot4 " +
                " From {0}.Trans T Left Outer Join {0}.Customers C On T.CustomerNumber=C.CustomerNumber , {0}.Officehoursetting O" +
                " Where T.BusinessDate Between  @StartDT and  @EndDt  And T.TransStatus in (2) And T.CustomerNumber<>'' " +
                " And Time(TransTime)  Between O.Shift1 And O.Shift1E " +
                " Group by Date(T.BusinessDate),T.CustomerNumber" +
                " Union" +
                " Select Date(T.BusinessDate) as TransDate,T.CustomerNumber as CustomerNumber,C.CustomerName as CustomerName, 0.00 as Slot1,Sum(T.MemberDiscAmount) as Slot2, 0.00 as Slot3, 0.00 as Slot4" +
                " From {0}.Trans T Left Outer Join {0}.Customers C On T.CustomerNumber=C.CustomerNumber , {0}.Officehoursetting O" +
                " Where T.BusinessDate Between @StartDT and  @EndDt And T.TransStatus in (2) And T.CustomerNumber<>''" +
                " And Time(TransTime)  Between O.Shift2 And O.Shift2E " +
                " Group by Date(T.BusinessDate),T.CustomerNumber " +
                " Union" +
                " Select Date(T.BusinessDate) as TransDate,T.CustomerNumber as CustomerNumber,C.CustomerName as CustomerName, 0.00 as Slot1,0.00 as Slot2,Sum(T.MemberDiscAmount) as Slot3, 0.00 as Slot4" +
                " From {0}.Trans T Left Outer Join {0}.Customers C On T.CustomerNumber=C.CustomerNumber , {0}.Officehoursetting O" +
                " Where T.BusinessDate Between @StartDT and  @EndDt And T.TransStatus in (2) And T.CustomerNumber<>''" +
                " And Time(TransTime)  Between O.Shift3 And O.Shift3E " +
                " Group by Date(T.BusinessDate),T.CustomerNumber" +
                " Union" +
                " Select Date(T.BusinessDate) as TransDate,T.CustomerNumber as CustomerNumber,C.CustomerName as CustomerName, 0.00 as Slot1,0.00 as Slot2,0.00 as Slot3, Sum(T.MemberDiscAmount) as Slot4 " +
                " From {0}.Trans T Left Outer Join {0}.Customers C On T.CustomerNumber=C.CustomerNumber , {0}.Officehoursetting O" +
                " Where T.BusinessDate Between @StartDT and  @EndDt And T.TransStatus in (2) And T.CustomerNumber<>''" +
                " And Time(TransTime)  Between O.Shift4 And '23:59:59' " +
                " Group by Date(T.BusinessDate),T.CustomerNumber" +
                " Union" +
                " Select Date(T.BusinessDate) as TransDate,T.CustomerNumber as CustomerNumber,C.CustomerName as CustomerName, 0.00 as Slot1,0.00 as Slot2,0.00 as Slot3, Sum(T.MemberDiscAmount) as Slot4 " +  //nid added for adjusting overlapping day shift disc claims
                " From {0}.Trans T Left Outer Join {0}.Customers C On T.CustomerNumber=C.CustomerNumber , {0}.Officehoursetting O" +
                " Where T.BusinessDate Between @StartDT and  @EndDt And T.TransStatus in (2) And T.CustomerNumber<>''" +
                " And Time(TransTime)  Between '00:00:00' And O.Shift4E " +
                " Group by Date(T.BusinessDate),T.CustomerNumber" +
                " ) as K" +
                " Group By TransDate,CustomerNumber " + SubQryValue +
                " Having Total>0 Order by 1,2";

            // Prevents SQL Injection 
            // Parameterized Approach
            string command = string.Format(sCmd, BranchAlias);
            MySqlParameter[] parameters =
                  {
                     new MySqlParameter("@StartDT",StartDT),
                     new MySqlParameter("@EndDt",EndDt),
                     new MySqlParameter("@CustNo", Microsoft.Security.Application.Encoder.HtmlEncode(txtCustNo.Text))
                  };
            Dt1 = ObjMain.NewGetData_DT(command, parameters);
            Session["SpecialDiscount"] = Dt1;
            GridView1.DataSource = Dt1;
            //gridview totals
            foreach (DataRow dr in Dt1.Rows)
            {
                totalSlot1 += Convert.ToDouble(dr["Slot1"]);
                totalSlot2 += Convert.ToDouble(dr["Slot2"]);
                totalSlot3 += Convert.ToDouble(dr["Slot3"]);
                totalSlot4 += Convert.ToDouble(dr["Slot4"]);
                totalAmount += Convert.ToDouble(dr["Total"]);
            }
            GridView1.Columns[2].FooterText = "G.Total:";
            GridView1.Columns[3].FooterText = totalSlot1.ToString("#,##0.00");
            GridView1.Columns[4].FooterText = totalSlot2.ToString("#,##0.00");
            GridView1.Columns[5].FooterText = totalSlot3.ToString("#,##0.00");
            GridView1.Columns[6].FooterText = totalSlot4.ToString("#,##0.00");
            GridView1.Columns[7].FooterText = totalAmount.ToString("#,##0.00");
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // XSS Attack Prevention 
            string StartDT = Microsoft.Security.Application.Encoder.HtmlEncode(txtFromDt.Text.Trim());
            string EndDt = Microsoft.Security.Application.Encoder.HtmlEncode(txtToDt.Text.Trim());



            if (cmbBranches.Visible == false)
            {
                GridView1.PageIndex = e.NewPageIndex;
                LoadSpecialDiscountQry(Session["Branch_HQ"].ToString(), StartDT, EndDt);
            }
            else
            {
                BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(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 )

                GridView1.PageIndex = e.NewPageIndex;
                LoadSpecialDiscountQry(Cmb_BranchName, StartDT, EndDt);
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {


            DTSession = (DataTable)Session["SpecialDiscount"];
            //Create a dummy GridView
            GridView GridView2 = new GridView();
            GridView2.AllowPaging = false;
            GridView2.DataSource = DTSession;
            GridView2.DataBind();

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
             "attachment;filename=SpecialDiscount.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                //Apply text style to each Row
                GridView2.Rows[i].Attributes.Add("class", "textmode");
            }

            GridView2.RenderControl(hw);
            string style = @"<style> td { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }






    }
}