Option Explicit

' Note, see the ExportDefault function for Product Type

Const FieldDelimiter = "~"
Const Environment = "PROD" ' TEST or PROD

Dim Export 'As ISInterface8.ISExport
Dim Loan 'As finLoan
Dim mfinBL ' as ISfinSupport
Dim TotalCustomerLines 'As Long
Dim TotalDefaultLines 'As Long
Dim TotalOriginalDefaultValue 'As Currency

Function Main(ByVal finBL, ByRef Variant1, ByRef Variant2, ByRef Variant3, ByRef Variant4) 'VBScript
'VBA Function Main(ByVal finBL As ISfinSupport, ByRef Variant1 As Variant, ByRef Variant2 As Variant, ByRef Variant3 As Variant, ByRef Variant4 As Variant)

Dim LoanDefault 'As finLoanDefault
Dim LoanDefaultsExport 'As finLoanDefaultsExport
Dim strLine 'As String

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

  ' Loan Defaults for Export are passed in using Variant1 (and are already loaded)
  Set mfinBL = finBL
  Set LoanDefaultsExport = Variant1
  
  ' Create Objects
  Set Export = CreateObject("ISInterface8.ISExport")
  Set Loan = finBL.CreateLoan
  
  ' Initialise
  TotalCustomerLines = 0
  TotalDefaultLines = 0
  TotalOriginalDefaultValue = 0

  ' Open Export File
  If Export.ExportOpen(LoanDefaultsExport.ExportFileName) Then
    ' Header
    If Main Then
      strLine = AddElement("", "HD")
      strLine = AddElement(strLine, Environment)
      strLine = AddElement(strLine, LoanDefaultsExport.SubscriberId)
      strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(Now, "YYYYMMDD"))
      strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(Now, "HHNNSS"))
      Main = Export.ExportWrite(strLine)
    End If
    
    ' Body
    If Main Then
      For Each LoanDefault In LoanDefaultsExport
        ' Load Loan
        Loan.Load LoanDefault.LoanId

        If Not ExportLoanDefault(LoanDefault) Then
          Main = False
          Exit For
        End If
      Next
    End If

    ' Trailer
    If Main Then
      strLine = AddElement("", "TR")
      strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(TotalCustomerLines, "##########0"))
      strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(TotalDefaultLines, "##########0"))
      strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(TotalOriginalDefaultValue, "############0.00"))
      Main = Export.ExportWrite(strLine)
   End If

    ' Close Export File
    Export.ExportClose
  Else
    Main = False
    finBL.ErrorMessageSet Export.ErrorMessage
  End If

  ' Dispose Objects
  Set Export = Nothing
  If Not (Loan Is Nothing) Then Loan.Dispose: Set Loan = Nothing

End Function

Private Function ExportLoanDefault(ByVal LoanDefault)

Dim LoanJointName 'As finLoanJointName

  ' Assume Success
  ExportLoanDefault = True
  
  If Loan.Load(LoanDefault.LoanId) Then
    For Each LoanJointName In Loan.JointNames
      Select Case mfinBL.OrganisationTypes(LoanJointName.Client.CType).Class
        Case 0 'iseOTCIndividual
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerHeader(LoanJointName.Client, 0)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerDetails(LoanJointName.Client, 0)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerCurrentAddress(LoanJointName.Client)
          If ExportLoanDefault Then ExportLoanDefault = ExportDefault(LoanDefault)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerFooter(LoanJointName.Client, 0)

        Case 1 'iseOTCJoint
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerHeader(LoanJointName.Client, 1)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerDetails(LoanJointName.Client, 1)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerCurrentAddress(LoanJointName.Client)
          If ExportLoanDefault Then ExportLoanDefault = ExportDefault(LoanDefault)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerFooter(LoanJointName.Client, 1)

          ' Joint Name
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerHeader(LoanJointName.Client, 2)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerDetails(LoanJointName.Client, 2)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerCurrentAddress(LoanJointName.Client)
          If ExportLoanDefault Then ExportLoanDefault = ExportDefault(LoanDefault)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerFooter(LoanJointName.Client, 2)

        Case Else 'iseOTCOrganisation, iseOTCIncorporatedOrganisation
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerHeader(LoanJointName.Client, -1)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerDetails(LoanJointName.Client, -1)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerCurrentAddress(LoanJointName.Client)
          If ExportLoanDefault Then ExportLoanDefault = ExportDefault(LoanDefault)
          If ExportLoanDefault Then ExportLoanDefault = ExportCustomerFooter(LoanJointName.Client, -1)
      End Select
    Next
  Else
    ExportLoanDefault = False
  End If

End Function

Private Function ExportCustomerHeader(ByVal Client, ByVal JointName)

Dim strLine 'As String

  ' Assume Success
  ExportCustomerHeader = True
  
  ' Update Totals
  TotalCustomerLines = TotalCustomerLines + 1

  strLine = AddElement("", "CO")
  Select Case JointName
    Case -1, 0: strLine = AddElement(strLine, Client.ClientId)
    Case 1, 2: strLine = AddElement(strLine, Client.ClientId & "_" & CStr(JointName))
  End Select
  ExportCustomerHeader = Export.ExportWrite(strLine)

End Function

Private Function ExportCustomerDetails(ByVal Client, ByVal JointName)

Dim strLine 'As String

  ' Assume Success
  ExportCustomerDetails = True

  Select Case JointName
    Case -1
      strLine = AddElement("", "OG")
      If Len(Client.LegalName) = 0 Then
        strLine = AddElement(strLine, Left(Client.Name, 50))
      Else
        strLine = AddElement(strLine, Left(Client.LegalName, 50))
      End If
      strLine = AddElement(strLine, Client.CompanyNumber)
      strLine = AddElement(strLine, "")
      strLine = AddElement(strLine, "")
      strLine = AddElement(strLine, "")

    Case 0, 1
      strLine = AddElement("", "DE")
      strLine = AddElement(strLine, Left(Client.LastName(0), 50))
      strLine = AddElement(strLine, Left(Client.FirstName(0), 50))
      strLine = AddElement(strLine, Left(Client.MiddleNames(0), 50))
      strLine = AddElement(strLine, Left(Client.Gender(0), 1))
      If IsNull(Client.DateofBirth(0)) Then
        strLine = AddElement(strLine, "")
      Else 
        strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(Client.DateofBirth(0), "YYYYMMDD"))
      End If

    Case 2
      strLine = AddElement("", "DE")
      strLine = AddElement(strLine, Left(Client.LastName(1), 50))
      strLine = AddElement(strLine, Left(Client.FirstName(1), 50))
      strLine = AddElement(strLine, Left(Client.MiddleNames(1), 50))
      strLine = AddElement(strLine, Left(Client.Gender(1), 1))
      If IsNull(Client.DateofBirth(1)) Then
        strLine = AddElement(strLine, "")
      Else 
        strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(Client.DateofBirth(1), "YYYYMMDD"))
      End If
  End Select
  ExportCustomerDetails = Export.ExportWrite(strLine)

End Function

Private Function ExportCustomerCurrentAddress(ByVal Client)

Dim Request 'As ISCreditBureau5.Request
Dim strLine 'As String

  ' Assume Success
  ExportCustomerCurrentAddress = True

  ' Use Credit Enquiry object to extract Address details  
  Set Request = mfinBL.CreditBureau.CreateRequest
  mfinBL.CreditBureau.LoadClientDetails Client, False, Request

  strLine = AddElement("", "A1")
  strLine = AddElement(strLine, "")
  If Len(Request.UnitNumber) = 0 Then
    strLine = AddElement(strLine, Request.StreetNumber)
  Else
    strLine = AddElement(strLine, Request.UnitNumber & "/" & Request.StreetNumber)
  End If
  strLine = AddElement(strLine, Trim(Request.StreetName & " " & Request.StreetType))
  strLine = AddElement(strLine, Request.Suburb)
  strLine = AddElement(strLine, Request.City)
  strLine = AddElement(strLine, Request.Postcode)
  ' Add other Countries as necessary 
  Select Case Ucase(Request.Country)
    Case "", "NZ", "NZL", "NEW ZEALAND"
      strLine = AddElement(strLine, "NZL")
    Case "AU", "AUS", "AUSTRALIA"
      strLine = AddElement(strLine, "AUS")
    Case Else
      strLine = AddElement(strLine, "")
  End Select
  strLine = AddElement(strLine, Client.PostId(0))
  strLine = AddElement(strLine, "N")
  ExportCustomerCurrentAddress = Export.ExportWrite(strLine)

End Function

Private Function ExportDefault(ByVal LoanDefault)

Dim ProductType 'As String
Dim strLine 'As String

  ' Assume Success
  ExportDefault = True

  ' Find Product Type from Loan Type
  Select Case Loan.LoanTypeId
    Case "" ' Bridging Finance
      ProductType = "BRFN"
    Case "" ' Rental of Equipment
      ProductType = "HIRE"
    Case "" ' Hire Purchase
      ProductType = "HPUR"
    Case "" ' Mortgage
      ProductType = "MTGE"
    Case "" ' Motor Vehicle Finance
      ProductType = "MVEH"
    Case "" ' Revolving Credit
      ProductType = "REVG"
    Case Else ' Personal Loan
      ProductType = "LOAN"
  End Select
  
  ' Update Totals
  TotalDefaultLines = TotalDefaultLines + 1
  TotalOriginalDefaultValue = TotalOriginalDefaultValue + LoanDefault.OriginalDefault

  strLine = AddElement("", "DF")
  strLine = AddElement(strLine, LoanDefault.LoanId)
  strLine = AddElement(strLine, "")
  strLine = AddElement(strLine, ProductType)
  strLine = AddElement(strLine, LoanDefault.LastStatus)
  strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(LoanDefault.OriginalDate, "YYYYMMDD"))
  strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(LoanDefault.OriginalDefault, "############0.00"))
  strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(LoanDefault.LastDefault, "############0.00"))
  If LoanDefault.LastStatusSatisfied Then
    strLine = AddElement(strLine, mfinBL.ScriptFunctions.Format(LoanDefault.LastDate, "YYYYMMDD"))
  Else
    strLine = AddElement(strLine, "")
  End If
  ExportDefault = Export.ExportWrite(strLine)

End Function

Private Function ExportCustomerFooter(ByVal Client, ByVal JointName)

Dim strLine 'As String

  ' Assume Success
  ExportCustomerFooter = True
  
  strLine = AddElement("", "CE")
  Select Case JointName
    Case -1, 0: strLine = AddElement(strLine, Client.ClientId)
    Case 1, 2: strLine = AddElement(strLine, Client.ClientId & "_" & CStr(JointName))
  End Select
  ExportCustomerFooter = Export.ExportWrite(strLine)
  
End Function
      
Private Function AddElement(ByVal strLine, ByVal strElement)

  ' Add Field Delimiter
  If Len(strLine) <> 0 Then strLine = strLine & FieldDelimiter
  
  ' Remove invalid characters
  strElement = Replace(strElement, """", "")
  strElement = Replace(strElement, FieldDelimiter, "")
  
  ' Add Element, including opening/ closing quotes
  AddElement = strLine & """" & strElement & """"

End Function
