Imports CAMS_ADMIN
Imports CAMS_REPORTS
Imports System.Text

Public Class frmPrintWorkbyAreas
    Inherits CAMSBasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblPrintDate As System.Web.UI.WebControls.Label
    Protected WithEvents lblUser As System.Web.UI.WebControls.Label
    Protected WithEvents lblQuarter As System.Web.UI.WebControls.Label
    Protected WithEvents lblYears As System.Web.UI.WebControls.Label
    Protected WithEvents litWorkAreas As System.Web.UI.WebControls.Literal

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private strDateFormat As String = "dd/MM/yyyy HH:mm"

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillReport()

        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Response.AppendHeader("content-disposition", "attachment; filename=WorkByAreas.xls")
        Me.EnableViewState = False

    End Sub

    Private Sub FillReport()
        Dim oUser As EC_User = Me.GetUserFromSession
        Dim strYearIni As String = "", strYearEnd As String = "", strQuarter As String = ""
        Dim strDateIni, strDateEnd As String

        If Not Request.QueryString("yearini") Is Nothing Then
            strYearIni = Request.QueryString("yearini").Trim
        End If

        If Not Request.QueryString("yearend") Is Nothing Then
            strYearEnd = Request.QueryString("yearend").Trim
        End If

        If Not Request.QueryString("quarter") Is Nothing Then
            strQuarter = Request.QueryString("quarter").Trim
        End If

        If Not oUser Is Nothing Then
            lblUser.Text = oUser.Name & " " & oUser.Surname
        End If

        lblPrintDate.Text = Now.ToString(strDateFormat)
        lblYears.Text = strYearIni & " - " & strYearEnd

        Select Case strQuarter
            Case "Q1"
                lblQuarter.Text = "Quarter 1: April " & strYearIni.Substring(2) & " - " & " June " & strYearIni.Substring(2)
                strDateIni = "01/04/" & strYearIni
                strDateEnd = "30/06/" & strYearIni
            Case "Q2"
                lblQuarter.Text = "Quarter 2: July " & strYearIni.Substring(2) & " - " & " September " & strYearIni.Substring(2)
                strDateIni = "01/07/" & strYearIni
                strDateEnd = "30/09/" & strYearIni
            Case "Q3"
                lblQuarter.Text = "Quarter 3: October " & strYearIni.Substring(2) & " - " & " December " & strYearIni.Substring(2)
                strDateIni = "01/10/" & strYearIni
                strDateEnd = "31/12/" & strYearIni
            Case "Q4"
                lblQuarter.Text = "Quarter 4: January " & strYearEnd.Substring(2) & " - " & " March " & strYearEnd.Substring(2)
                strDateIni = "01/01/" & strYearEnd
                strDateEnd = "31/03/" & strYearEnd
            Case "All"
                lblQuarter.Text = "All Quarters: April " & strYearIni.Substring(2) & " - " & " March " & strYearEnd.Substring(2)
                strDateIni = "01/04/" & strYearIni
                strDateEnd = "31/03/" & strYearEnd
        End Select

        FillTable(strDateIni, strDateEnd)


    End Sub

    Private Sub FillTable(ByVal prmDateIni As String, ByVal prmDateEnd As String)
        Dim oCC_WorkbyAreas As New CC_WorkByAreas
        Dim dsResults As DataSet

        Try
            dsResults = oCC_WorkbyAreas.GetWorkbyAreas(prmDateIni, prmDateEnd)
        Catch exc As Exception
            dsResults = Nothing
        Finally
            CreateTable(dsResults)
        End Try
    End Sub

    Private Function CreateTable(ByVal dsResults As DataSet)
        Dim i, j As Integer
        Dim nColumns As Integer = dsResults.Tables(0).Rows.Count
        Dim nRows As Integer = dsResults.Tables(0).Columns.Count
        Dim msTable As System.Text.StringBuilder = New System.Text.StringBuilder

        msTable.Append("<TABLE>")
        Dim dtWorkAreas As DataTable
        Dim row As DataRow

        For i = 0 To dsResults.Tables(0).Columns.Count - 1
            msTable.Append("<TR>")
            For j = 0 To dsResults.Tables(0).Rows.Count - 1
                msTable.Append("<TD style='BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-SIZE: 10px; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: Verdana, Arial'>" + dsResults.Tables(0).Rows(j)(i).ToString + "</TD>")
            Next
            msTable.Append("<TD style='BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-SIZE: 10px; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: Verdana, Arial'>" + dsResults.Tables(1).Rows(0)(i).ToString + "</TD>")
            msTable.Append("</TR>")
        Next

        msTable.Append("</TABLE>")
        litWorkAreas.Text = msTable.ToString

    End Function


End Class
