Imports RGBC_ScoreCard.db

Public Class ProjectFunctions

   Public Shared Function sql_get_Users(ByVal strDomainLogin As String, ByVal strDomain As String) As String
      Return "SELECT * " & _
        "FROM mstUserGroup" & strDomain & " INNER JOIN mstUser ON mstUserGroup" & strDomain & ".UserGroupID = mstUser.refUserGroup" & strDomain & "ID " & _
        "WHERE mstUser.strDomainLogin = '" & strDomainLogin & "'"

   End Function

   Public Overloads Shared Function FormatPercent(ByVal MyValue As Object, Optional ByVal intDecimals As Integer = 0, Optional ByVal IncludeLeadingDigit As TriState = TriState.UseDefault, Optional ByVal UseParensForNeg As TriState = TriState.UseDefault, Optional ByVal GroupDigits As TriState = TriState.UseDefault) As String
      'overloads the standard formatPerc function to add in "+" sign for positives
      Try
         If CDbl(MyValue) > 0 Then
            Return "+" & Microsoft.VisualBasic.FormatPercent(MyValue, intDecimals, IncludeLeadingDigit, UseParensForNeg, GroupDigits)
         Else
            Return Microsoft.VisualBasic.FormatPercent(MyValue, intDecimals, IncludeLeadingDigit, UseParensForNeg, GroupDigits)
         End If

      Catch ex As Exception
         Return "0.0"
      End Try

   End Function


   Public Overloads Shared Function FormatNumber(ByVal MyValue As Object, Optional ByVal intDecimals As Integer = 0, Optional ByVal IncludeLeadingDigit As TriState = TriState.UseDefault, Optional ByVal UseParensForNeg As TriState = TriState.UseDefault, Optional ByVal GroupDigits As TriState = TriState.UseDefault) As String
      'rgbc's server is set up with number format = "999 999.99", which is lame. it should be "999,999.99"
      Try

         Return Microsoft.VisualBasic.FormatNumber(MyValue, intDecimals, IncludeLeadingDigit, UseParensForNeg, GroupDigits).ToString.Replace(Chr(160), ",")
         'OMG! I HAVE NEVER SEEN SOMETHING THIS STUPID! LIVE SERVER SETTINGS NUMBER FORMATS NOT A SPACE CHARACTER, BUT A chr(160). WTF IS 160?! #$^%$* *#*#*@(! *@**@!!!
      Catch ex As Exception
         Return "0.0"
      End Try
   End Function

   Public Shared Function sql_insert_BrandQF_Job(ByVal BrandID As Integer, ByVal Finyear As Integer, ByVal BrandName As String, Optional ByVal CopyBrandID As Integer = 0) As String
      'Dim para As New Collection

      'para.Add(BrandID, "BrandID")
      'para.Add(Finyear, "FinYear")
      'para.Add(CopyBrandID, "CopyBrandID")

      Dim para As String = "BrandID=" & BrandID & "|Finyear=" & Finyear & "|CopyBrandID=" & CopyBrandID & "|BrandName=" & BrandName

      Return "INSERT INTO sysCronJob(dtCronJob, Type, param, Status)" & _
             "VALUES('" & Date.Now.ToString("yyyyMMddHHmm") & "', 'BrandQF', '" & para.ToString & "', 'New')"
   End Function

   Public Shared Function sql_remove_duplicate_BrandQF_Job(ByVal BrandID As Integer, ByVal Finyear As Integer, ByVal BrandName As String, Optional ByVal CopyBrandID As Integer = 0) As String

      Dim para As String = "BrandID=" & BrandID & "|Finyear=" & Finyear & "|CopyBrandID=" & CopyBrandID & "|BrandName=" & BrandName

      Return "UPDATE sysCronJob SET Status = 'Removed', Result = 'Duplicate Job in Queue [" & Date.Now.ToString("yyyyMMdd HH:mm:ss") & "]' WHERE Type = 'BrandQF' AND param like '" & para.ToString & "' AND Status = 'New' "
   End Function

   Public Shared Function WriteLogPromo(PromoID As Integer, strField As String, OldValue As String, NewValue As String, strUser As String, Optional refStamp As String = "") As String

      Dim dtStamp As String = Date.Now.Ticks.ToString
      Dim strDateTime As String = Date.Now.ToString("u")
      Dim sql As String = ""
      Dim db As New RGBC_ScoreCard.db

      If refStamp = "" Then refStamp = dtStamp

      sql = "INSERT INTO logPromo (PromoID, strDateTime, strField, valueOld, valueNew, strLastUser, refStamp, dtStamp) " & _
         "VALUES (" & PromoID & ", '" & strDateTime & "', '" & strField & "', '" & db.CleanString(OldValue) & "', '" & db.CleanString(NewValue) & "', '" & strUser & "', '" & refStamp & "', '" & dtStamp & "') "
      db.doQuery(sql)
      Return sql

   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) + 200
      g = CInt(strTime.Substring(2, 2) / 2) + 200
      b = CInt(strTime.Substring(4, 2) / 2) + 200

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

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

      r = CInt(strTime.Substring(0, 2) / 2) + 200
      g = CInt(strTime.Substring(2, 2) / 2) + 200
      b = CInt(strTime.Substring(4, 2) / 2) + 200
      Select Case strComponent
         Case "R"
            Return r
         Case "G"
            Return g
         Case "B"
            Return b
      End Select
    End Function

    'Public Shared Function getSurveyQuestions(ByVal strID As Integer) As String
    '    Dim dt As DataTable

    '    Return dt
    'End Function

    Public Shared Sub WriteLog(LogType As String, ProcessName As String, ProcessEvent As String, Log As String, Optional blnEmailed As Integer = 0, Optional tblLog As String = "stgSuperbowlLog")
        Dim sql As String
        Dim db As New RGBC_ScoreCard.db

        sql = "INSERT INTO " & tblLog & " (strStamp, dtLog, strType, strProcess, strAction, txtLog, blnEmail) " & _
              "VALUES ('" & Date.Now.Ticks.ToString() & "', '" & Date.Now.ToString("u") & "', '" & LogType & "', '" & ProcessName & "', '" & ProcessEvent & "', '" & db.CleanString(Log) & "', " & blnEmailed & ")"

        db.doQuery(sql)
   End Sub

End Class
