Option Explicit

Function Main(ByVal finBL, ByVal ManagerId, ByVal CollectionLoans, ByVal CollectionLoansView, ByVal FolderKey, ByVal CollectionFolders) 'VBScript
'VBA Function Main(ByVal finBL As ISfinSupport, ByVal ManagerId As String, ByVal CollectionLoans As finCollectionLoans, ByVal CollectionLoansView As finCollectionLoansView, ByVal FolderKey As String, ByVal CollectionFolders As finCollectionFolders)

' Function should return (html) text summarising the Collection view's statistics
Main = ""

Dim s 'As String
Dim EndDate 'As Variant
Dim MinutesRemaining 'As Long
Dim Workload 'As String
Dim CountAssigned 'As Long
Dim CountFlagged 'As Long
Dim CountReview 'As Long
Dim CountTotal 'As Long
Dim CountUnassigned 'As Long
Dim CollectionLoan 'As finCollectionLoan

  ' Initialise HTML
  s = s & "<html><head><style>"
  s = s & "BODY {font-family:verdana; font-size:8pt; background-color:white;}"
  s = s & "TH {font-size:8pt; text-align:left; color:#000090;}"
  s = s & "TD {font-size:8pt;}"
  s = s & "</style></head><body leftmargin=0 rightmargin=0 topmargin=0 bottommargin=0 scroll=no>"

  ' Loaded Status
  If CollectionLoans.LoadedStatus = 0 Then
    ' Nothing Loaded
    s = s & "<span style='color:red;font-weight:bold'>No loans loaded.</span></br></br>Click <span style='color:green;font-weight:bold'>Refresh</span> to load."
  ElseIf CollectionLoans.LoadedStatus = 2 Then
    ' Select Loans Loaded
    s = s & "<span style='color:red;font-weight:bold'>Only selected loans are loaded.</span></br></br>Click <span style='color:green;font-weight:bold'>Refresh</span> to load."
  Else
    ' Add Table
    s = s & "<table cellspacing='0' border='1' width='100%' style='border:1px solid silver;'>"

    ' Statistics for Collector
    If Len(ManagerId) <> 0 Then
      CountAssigned = 0
      CountFlagged = 0
      CountReview = 0
      CountTotal = CollectionLoans.Count
      CountUnassigned = 0
      For Each CollectionLoan In CollectionLoans
        If CollectionLoan.CollectorManagerId = "" Then
          CountUnassigned = CountUnassigned + 1
        Else
          CountAssigned = CountAssigned + 1
          If Not IsNull(CollectionLoan.ReviewDate) Then
            If CollectionLoan.ReviewDate < Now Then CountReview = CountReview + 1
          End If
        End If
        If CollectionLoan.FlagColour <> 0 Then CountFlagged = CountFlagged + 1
      Next
      Workload = CollectionLoans.TotalWorkloadText(True, False)
      EndDate = finBL.Managers(ManagerId).CollectionsCalculateEndDate(Now, CollectionLoans.TotalWorkloadMinutes, MinutesRemaining)
      If MinutesRemaining = -1 Then
        EndDate = ""
      Else
        EndDate = finBL.ISFormat(EndDate, "DateLong")
      End If

      s = s & "<tr><th colspan=2>Collector: " & finBL.Managers(ManagerId).Name & "</th></tr>"
      s = s & "<tr><td>Loans</td><td align=right>" & CountTotal & "</td></tr>"
      s = s & "<tr><td>Assigned to " & ManagerId & "</td><td align=right>" & CountAssigned & "</td></tr>"
      If CountReview > 0 Then s = s & "<tr><td><b>For Review</b></td><td align=right><b>" & CountReview & "</b></td></tr>"
      If CountFlagged > 0 Then s = s & "<tr><td><b>Flagged</b></td><td align=right><b>" & CountFlagged & "</b></td></tr>"
      If CountUnassigned > 0 Then s = s & "<tr><td>Unassigned</td><td align=right>" & CountUnassigned & "</td></tr>"
      If Workload <> "0:00" Then s = s & "<tr><td>Workload</td><td align=right>" & Workload & "</td></tr>"
      If EndDate <> "" Then s = s & "<tr><td>Est. End Date</td><td align=right>" & EndDate & "</td></tr>"
    End If

    ' Statistics for Folder
    If Len(FolderKey) <> 0 Then
      CountAssigned = 0
      CountFlagged = 0
      CountReview = 0
      CountTotal = CollectionLoansView.Count
      CountUnassigned = 0
      For Each CollectionLoan In CollectionLoansView
        If CollectionLoan.CollectorManagerId = "" Then
          CountUnassigned = CountUnassigned + 1
        Else
          CountAssigned = CountAssigned + 1
          If Not IsNull(CollectionLoan.ReviewDate) Then
            If CollectionLoan.ReviewDate < Now Then CountReview = CountReview + 1
          End If
        End If
        If CollectionLoan.FlagColour <> 0 Then CountFlagged = CountFlagged + 1
      Next
      Workload = CollectionLoansView.TotalWorkloadText(True, False)

      s = s & "<tr><th colspan=2><br>Folder: " & CollectionFolders(FolderKey).Name & "</th></tr>"
      s = s & "<tr><td>Loans</td><td align=right>" & CountTotal & "</td></tr>"
      s = s & "<tr><td>Assigned to " & ManagerId & "</td><td align=right>" & CountAssigned & "</td></tr>"
      If CountReview > 0 Then s = s & "<tr><td><b>For Review</b></td><td align=right><b>" & CountReview & "</b></td></tr>"
      If CountFlagged > 0 Then s = s & "<tr><td><b>Flagged</b></td><td align=right><b>" & CountFlagged & "</b></td></tr>"
      If CountUnassigned > 0 Then s = s & "<tr><td>Unassigned</td><td align=right>" & CountUnassigned & "</td></tr>"
      If Workload <> "0:00" Then s = s & "<tr><td>Workload</td><td align=right>" & Workload & "</td></tr>"
    End If

    ' Finalise Table
    s = s & "</table>"
  End If
  
  ' Finalise HTML
  s = s & "</body></html>"
  
  ' Return
  Main = s

End Function

Function HTMLEncode(ByVal Text)

  HTMLEncode = Replace(Text, "&", "&amp;")
  HTMLEncode = Replace(HTMLEncode, "<", "&lt;")
  HTMLEncode = Replace(HTMLEncode, ">", "&lt;")
  HTMLEncode = Replace(HTMLEncode, vbNewLine, "<br/>")

End Function




