'ONLY contains shared methods/functions
Imports SNELL_CronDaily.ProjectFunctions
Imports SNELL_CronDaily.db


  Public Class SystemFunctions

    Public Shared Function DisplayDate(ByVal strDT As String, Optional ByVal format As String = "") As String
      '20100930 - pj
      'universal display date function to convert db date strings to formatted date strings
      Dim dt As DateTime

      Try 'note: remove breakpoints from each case when tested successfully
      Select Case strDT.Length
        Case 6
          dt = New DateTime(CInt(strDT.Substring(0, 4)), CInt(strDT.Substring(4, 2)), 1)
          If format = "" Then Return dt.ToString("MMM yyyy") Else Return dt.ToString(format)
        Case 8
          dt = New DateTime(CInt(strDT.Substring(0, 4)), CInt(strDT.Substring(4, 2)), CInt(strDT.Substring(6, 2)))
          If format = "" Then Return dt.ToString("d MMM yyyy") Else Return dt.ToString(format)
        Case 12
          dt = New DateTime(CInt(strDT.Substring(0, 4)), CInt(strDT.Substring(4, 2)), CInt(strDT.Substring(6, 2)), CInt(strDT.Substring(8, 2)), CInt(strDT.Substring(10, 2)), 1)
          If format = "" Then Return dt.ToString("d MMM yyyy HH:mm") Else Return dt.ToString(format)
        Case 14
          dt = New DateTime(CInt(strDT.Substring(0, 4)), CInt(strDT.Substring(4, 2)), CInt(strDT.Substring(6, 2)), CInt(strDT.Substring(8, 2)), CInt(strDT.Substring(10, 2)), CInt(strDT.Substring(12, 2)))
          If format = "" Then Return dt.ToString("d MMM yyyy HH:mm") Else Return dt.ToString(format)
        Case 18
          dt = New DateTime(CLng(strDT))
          If format = "" Then Return dt.ToString("d MMM yyyy HH:mm:ss") Else Return dt.ToString(format)
      End Select

      Catch icEx As InvalidCastException
        Return "n/a"
      Catch ex As Exception
        Return "err"
      End Try

  End Function

   Public Shared Function getLastCompletedFiscalStart() As Date
      Dim dr, drFiscal

      dr = db.getRow("SELECT * FROM mstCompany WHERE CompanyID = 1")
      drFiscal = db.getRow("SELECT * FROM mstPeriod WHERE strPeriod = '" & Date.Today.ToString("yyyyMM") & "'")

      If dr!strFYStart = "01" Then
         Return New Date(drFiscal!intFiscalYear - 1, 1, 1)
      Else
         Return New Date(drFiscal!intFiscalYear - 2, CInt(dr!strFYStart), 1)
      End If

   End Function

  Public Shared Function getLastCompletedFiscalEnd() As Date
    Dim dr, drFiscal

    dr = db.getRow("SELECT * FROM mstCompany WHERE CompanyID = 1")
    drFiscal = db.getRow("SELECT * FROM mstPeriod WHERE strPeriod = '" & Date.Today.ToString("yyyyMM") & "'")

    If dr!strFYStart = "01" Then
      Return New Date(drFiscal!intFiscalYear, 12, 1)
    Else
      Return New Date(drFiscal!intFiscalYear - 1, CInt(dr!strFYEnd), 1)
    End If

   End Function


   Public Shared Function getHexColour(ByVal strTime As String) As String
      Dim r, g, b As Integer

      r = CInt(strTime.Substring(0, 2) / 2)
      g = CInt(strTime.Substring(2, 2) / 2)
      b = CInt(strTime.Substring(4, 2) / 2)

      Return "#" & Hex(r + 200) & Hex(g + 200) & Hex(b + 200)
   End Function

   Public Shared Function getHexColourString(strSting As String) As String
      Dim arrChars() As Char
      Dim dblChars As Double = 1

      arrChars = strSting.ToCharArray
      For Each letter In arrChars
         dblChars = dblChars + Asc(letter)
      Next

      dblChars = dblChars * Math.PI * 100000

      Return getHexColour(dblChars.ToString)

   End Function

End Class
