Option Explicit

' Description:
'   Sample script to assign Collectors to unassigned Loans
'
' Requirements:
'   Collection Groups should have an Allocation Method other than 'Unassigned'
'
' Comments:
'   Uses LoanFunctions.CollectionsSuggestCollector method to allow finPOWER to suggest a Collector.
'
'   Leaves all Loans in special Collection Groups alone
'
'   Orders Loans by Name. This may give a better spread than ordering by Collection Group since not all Collectors
'   may belong to all Collection Groups. Doing them in Collection Group order may favour certain Collectors.

' VBScript cannot use finPOWER Enums so declare them as constants
Const iseCollectorAllocationMethodUnassigned = 0
Const iseCollectorAllocationMethodRandom = 1
Const iseCollectorAllocationMethodWorkload = 2
Const iseCollectorAllocationMethodProportion = 3

Const iseCollectorAllocationHoldNo = 0
Const iseCollectorAllocationHoldIfApplicable = 1
Const iseCollectorAllocationHoldAlways = 2
Const iseCollectorAllocationHoldNever = 3

Function Main(ByVal finBL) 'VBScript
'VBA Function Main(ByVal finBL As ISfinSupport)

' Function should return a boolean value depending on its success
Main = True

Dim Handled 'As Boolean
Dim CollectionGroupId 'As String
Dim CollectorManagerId 'As String
Dim CollectionGroup 'As finCollectionGroup
Dim LoanFunctions 'As finLoanFunctions
Dim rst 'As Recordset

' Show progress
finBL.DebugPrint "Started Collector assignment at " & finBL.ScriptFunctions.Format(Now, "d/mm/yyyy hh:nn:ssam/pm")

' Create Objects
Set LoanFunctions = finBL.CreateLoanFunctions()

' Begin Transaction
finBL.Database.TransactionBegin

' Retrieve a Recordset of all Loans in Collections ordered by Loan Name
If finBL.Database.RecordsetOpen("SELECT Contract.ContractId,ContractCollectionHistory.CollectorManagerId,ContractCollectionHistory.CollectionGroupId FROM ContractCollectionHistory INNER JOIN Contract ON Contract.ContractId=ContractCollectionHistory.ContractId WHERE MovedToDate Is Null AND ContractCollectionHistory.CollectorManagerId Is Null ORDER BY Contract.Name", rst) Then
  ' Loop Through Loans
  Do Until rst.EOF Or Not Main
    ' Initialise
    Handled = False
    CollectionGroupId = "" & rst("CollectionGroupId").Value

    ' Ignore if in 'Special' Collections Groups
    Select Case CollectionGroupId
      Case "", "DC"
        Handled = True
        finBL.DebugPrint "Loan " & rst("ContractId").Value & " unchanged (Collection Group=" & CollectionGroupId & ")"
    End Select

    If Not Handled Then
      ' Get Collection Group
      Set CollectionGroup = finBL.CollectionGroups(CollectionGroupId)

      ' Let finPOWER suggest a Collector for this Loan
      CollectorManagerId = LoanFunctions.CollectionsSuggestCollector(CollectionGroupId, "" & rst("CollectorManagerId").Value, rst("ContractId").Value)

      If Len(CollectorManagerId) = 0 And CollectionGroup.CollectorAllocationMethod <> iseCollectorAllocationMethodUnassigned Then
        ' No Collector Suggested!
        finBL.DebugPrint "finPOWER failed to suggest a Collector for Loan '" & rst("ContractId").Value & "' in Collection Group '" & CollectionGroupId & "'"
      Else
        ' Assign Collector
        Main = LoanFunctions.CollectionsAssignCollector(rst("ContractId").Value, CollectorManagerId)

        finBL.DebugPrint "Loan " & rst("ContractId").Value & " assigned to " & CollectorManagerId
      End If
    End If

    rst.MoveNext
  Loop
  rst.Close: Set rst = Nothing
Else
  Main = False
End If

' Commit/Rollback Transaction
If Main Then
  finBL.Database.TransactionCommit
Else
  finBL.Database.TransactionRollback
End If

' Destroy Objects
Set LoanFunctions = Nothing

' Show progress
finBL.DebugPrint "Finished Collector assignment at " & finBL.ScriptFunctions.Format(Now, "d/mm/yyyy hh:nn:ssam/pm")

End Function
