Public Class GPBySalesmanCustomerReportViewer
    Inherits Functions
    Dim Report As New GPBySalesmanCustomerReport

    Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/GPBySalesmanCustomerReportViewer.aspx?pdf=true&SD=" & txtStartDate.Text & "&ED=" & txtEndDate.Text & "&CustDet=" & chkDetailsCust.Checked & "&SKUDet=" & chkDetailsSku.Checked & "','_blank')")
        Response.Write("</script>")
    End Sub
    Protected Sub btnXLS_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnXLS.Click
        Response.Write("<script>")
        Response.Write("window.open('/Reports/GPBySalesmanCustomerReportViewer.aspx?pdf=false&SD=" & txtStartDate.Text & "&ED=" & txtEndDate.Text & "&CustDet=" & chkDetailsCust.Checked & "&SKUDet=" & chkDetailsSku.Checked & "','_blank')")
        Response.Write("</script>")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim TDataSet As New dsGPBySalesmanCustomer
        Dim TTableAdapter As New dsGPBySalesmanCustomerTableAdapters.GPBYSalesmanCustomerTableAdapter
        Dim connection As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim StartDate As String = Request.QueryString("SD")
        Dim EndDate As String = Request.QueryString("ED")
        Dim CustDet As String = Request.QueryString("CustDet")
        Dim SKUDet As String = Request.QueryString("SKUDet")

        Authenticate()

        If Request.QueryString("pdf") <> "" Then
            TTableAdapter.SelectCommandTimeout = 1800
            TTableAdapter.Connection = connection
            TTableAdapter.FillByDate(TDataSet.GPBySalesmanCustomerDataTable, StartDate, EndDate)

            Report.SetDataSource(TDataSet)
            Dim TextField As CrystalDecisions.CrystalReports.Engine.TextObject
            TextField = Report.ReportDefinition.ReportObjects.Item("txtDateRange")
            TextField.Text = StartDate & " - " & EndDate

            Me.CrystalReportViewer1.ReportSource = Report

            If CustDet = "False" Then
                Report.GroupHeaderSection2.SectionFormat.EnableSuppress = True
            Else
                Report.GroupHeaderSection2.SectionFormat.EnableSuppress = False
            End If
            If SKUDet = "False" Then
                Report.GroupHeaderSection1.SectionFormat.EnableSuppress = True
            Else
                Report.GroupHeaderSection1.SectionFormat.EnableSuppress = False
            End If

            If Request.QueryString("pdf") = True Then
                Report.Section1.SectionFormat.EnableSuppress = False
                Report.Section5.SectionFormat.EnableSuppress = False
                ExportReport()
            Else
                Report.Section1.SectionFormat.EnableSuppress = True
                Report.Section5.SectionFormat.EnableSuppress = True
                ExportReportXLS()
            End If
        Else
            If Page.IsPostBack = False Then
                txtStartDate.Text = Now.Date
                txtEndDate.Text = Now.Date
            End If
        End If
    End Sub

    Protected Sub ExportReport()

        Dim oStream As New System.IO.MemoryStream

        oStream = Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/pdf"
        '--------------------------------------------------------------

        Try
            Response.BinaryWrite(oStream.ToArray())
            Response.End()
        Catch err As Exception
            Response.Write("< BR >")
            Response.Write(err.Message.ToString)
        End Try
    End Sub
    Protected Sub ExportReportXLS()

        Dim oStream As New System.IO.MemoryStream

        oStream = Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        '--------------------------------------------------------------

        Try
            Response.BinaryWrite(oStream.ToArray())
            Response.End()
        Catch err As Exception
            Response.Write("< BR >")
            Response.Write(err.Message.ToString)
        End Try
    End Sub

End Class