Option Explicit

' Description:
'   Sample script to allocate Collection Groups
'
' Requirements:
'   Three Collection groups assumed to exist O7, O14 and O14+
'   Special Collection Group DC assumed to exist
'
' Comments:
'   Uses LoanFunctions.CollectionsOpenLoansForAllocationRecordset method to return a Recordset of Loans that
'    are Overdue, In Default or currently under Collection.
'
'   Any Loans not currently Overdue or In Default are moved out of Collections.
'
'   Allocates a Collection Group bases upon days overdue.
'
'   If Collection Group has not changed no action is taken.
'
'   When Allocating (via LoanFunctions.CollectionsAllocateGroup method), passes across a blank CollectorManagerId.
'    This has the affect of unassigning any Collector currently assigned to the Loan. Passing across "*" or ommitting
'    the parameter will retain the current Collector. Also passes across a True parameter indicating all Flag and
'    Review information is to be cleared.
'
'   A commented out line at the end of the script shows how you could chain another script to then assign Collectors.

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 Overdue 'As Currency
Dim OverdueDays 'As Long
Dim CurrentCollectionGroupId 'As String
Dim NewCollectionGroupId 'As String
Dim Loan 'As finLoan
Dim LoanFunctions 'As finLoanFunctions
Dim rst 'As Recordset

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

' Create Objects
Set Loan = finBL.CreateLoan()
Set LoanFunctions = finBL.CreateLoanFunctions()

' Begin Transaction
finBL.Database.TransactionBegin

' Open Recordset of Overdue or In Default Loans
' Note, True parameter will also include those currently in Collections even if they are no longer Overdue or In Default
If LoanFunctions.CollectionsOpenLoansForAllocationRecordset(rst, True) Then
  ' Loop through all Records
  Do Until rst.EOF Or Not Main
    ' Initialise
    Handled = False
    CurrentCollectionGroupId = "" & rst("CollectionGroupId").Value
    If IsNull(rst("Overdue").Value) Then Overdue = 0 Else Overdue = rst("Overdue").Value

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

    ' Currently in Collections; but shouldn't be?
    If Main And Not Handled Then
      ' If CollectionGroupId is not blank it indicates it is currently in a Collection Group
      If CurrentCollectionGroupId <> "" Then
        If Not (rst("InDefault").Value Or Overdue > 0) Then
          ' Remove from Collections by setting collections group to blank
          Main = LoanFunctions.CollectionsAllocateGroup(rst("ContractId").Value, "")

          Handled = True
          finBL.DebugPrint "Loan " & rst("ContractId").Value & " removed from collections"
        End If
      End If
    End If

    ' Select Collection Group
    If Main And Not Handled Then
      ' Load Loan
      Loan.Load rst("ContractId").Value

      ' Select Collection Group based upon OverdueDays
      OverdueDays = Loan.OverdueDays
      If OverdueDays < 1 Then
        NewCollectionGroupId = ""
      ElseIf OverdueDays <= 7 Then
        NewCollectionGroupId = "O7"
      ElseIf OverdueDays <= 14 Then
        NewCollectionGroupId = "O14"
      Else
        NewCollectionGroupId = "O14+"
      End If

      ' Allocate
      If NewCollectionGroupId <> "" And CurrentCollectionGroupId <> NewCollectionGroupId Then
        Main = LoanFunctions.CollectionsAllocateGroup(Loan.LoanId, NewCollectionGroupId, "", True)

        finBL.DebugPrint "Loan " & Loan.LoanId & " allocated to " & NewCollectionGroupId
      Else
        finBL.DebugPrint "Loan " & Loan.LoanId & " still at " & CurrentCollectionGroupId
      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
Loan.Dispose: Set Loan = Nothing
Set LoanFunctions = Nothing

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

'  ' Uncomment the lines below to chain a Script to assign Collectors
'  If Main Then
'    Main = finBL.Scripts("AC").ExecuteCollectionsTask(True)
'  End If

End Function





