' SummaryPage2_BankingInterfaceSummary_ZeptoNZ
Option Explicit On
Option Strict On

' #################################################################
' Zepto New Zealand Banking Interface Summary Page Script
'
' Version: 1.00 (01/11/2022)
'
' Usage: Various forms
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    1.00  01/11/2022  ISS                Created
' #################################################################

' Constants

' Special Constants
Const TestMode As Boolean = False

' Properties
Private ServiceLogs As ISServiceLogs

Public Function Main(source As Object,
                     eventId As String,
                     eventArgs As ISKeyValueList,
                     ByRef handled As Boolean,
                     ByRef returnValues As ISKeyValueList,
                     ByRef text As String) As Boolean

  Dim Response As ISBankInterfaceResponse

  ' Assume Success
  Main = True

  ' Get Constants
  With ScriptInfo.Constants
  End With

  ' Get Service Logs
  Try
    Main = finBL.BankingInterface.GetServiceLogs(CInt(source), ServiceLogs)
  Catch ex As Exception
    Main = False
    finBL.Error.ErrorBeginFromException(ex)
  End Try
  If Main Then
    If ServiceLogs.Count = 0 Then
      Main = False
      finBL.Error.ErrorBegin("Invalid Service Log for the Banking Interface.")
    ElseIf ServiceLogs(0).RequestStatus <> iseAuditLogStatus.Success Then
      Main = False
      finBL.Error.ErrorBegin("Invalid Service Log. Initial Request was not successful.")
    Else
      Response = DirectCast(ServiceLogs(0).ExResponse, ISBankInterfaceResponse)
    End If
  End If

  ' Handle Events
  If Main Then
    Select Case eventId
      Case ""
        ' Main event, i.e., return main page content
        Main = Me.CreatePage(Response, text)
    End Select
  End If

End Function

Private Function CreatePage(response As ISBankInterfaceResponse,
                            ByRef html As String) As Boolean

  Dim HtmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise ByRef
  html = ""

  ' Initialise
  HtmlSummaryPage2StringBuilder = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With HtmlSummaryPage2StringBuilder
    ' Display test watermark
    If serviceLogs(0).TestMode Then .Watermark = "Test Mode"

    ' Note: Embed images (will also embed CSS) since we want to be able to easily produce a PDF document
    .Initialise(ScriptInfo, False, True)

    ' Begin HTML Header
    .HtmlHeaderBegin(String.Format("{0} {1}", response.ServiceName, response.ProductId), isefinSummaryPageStyle.ServiceReport)

    ' Theme
    .HtmlHeaderInsertServiceReportTheme("#3BB287", "#F0F0F0", "", "#F2F2F2") ' 141618

    ' Intersoft PDF Options (including style tweaks)
    .HtmlHeaderInsertPdfGenerationHints("A4", "Portrait", 880, "1.5cm", "1.5cm", "1cm", "1cm", "font-size:0.9em")

    ' Finalise HTML Header
    .HtmlHeaderEnd()
  End With

  ' Body
  Select Case response.GetType()
    Case GetType(ISBankInterfaceResponse_ZeptoNZ_AddAnyoneContact)
      Me.CreatePage_AddAnyoneContact(DirectCast(response, ISBankInterfaceResponse_ZeptoNZ_AddAnyoneContact), HtmlSummaryPage2StringBuilder)
    Case GetType(ISBankInterfaceResponse_ZeptoNZ_CreateKycAgreement)
      Me.CreatePage_CreateKycAgreement(DirectCast(response, ISBankInterfaceResponse_ZeptoNZ_CreateKycAgreement), HtmlSummaryPage2StringBuilder)
    Case GetType(ISBankInterfaceResponse_ZeptoNZ_GetContact)
      Me.CreatePage_GetContact(DirectCast(response, ISBankInterfaceResponse_ZeptoNZ_GetContact), HtmlSummaryPage2StringBuilder)
    Case GetType(ISBankInterfaceResponse_ZeptoNZ_MakePayment)
      Me.CreatePage_MakePayment(DirectCast(response, ISBankInterfaceResponse_ZeptoNZ_MakePayment), HtmlSummaryPage2StringBuilder)
    Case GetType(ISBankInterfaceResponse_ZeptoNZ_RequestPayment)
      Me.CreatePage_RequestPayment(DirectCast(response, ISBankInterfaceResponse_ZeptoNZ_RequestPayment), HtmlSummaryPage2StringBuilder)
    Case Else
      Success = False
      finBL.Error.ErrorBeginFormat("Unexpected Response Type '{0}'.", TypeName(response))
  End Select

  ' Disclaimer
  HtmlSummaryPage2StringBuilder.AppendLine("<hr>")
  HtmlSummaryPage2StringBuilder.AppendLine("{{disclaimer|finPOWER Connect version " & finBL.VersionMajorMinorBuild & " using " & response.ServiceName & " service.}}")

  ' Finalise HTML
  HtmlSummaryPage2StringBuilder.HtmlFinalise()

  ' Resolve Templates
  html = HtmlSummaryPage2StringBuilder.ToString(True)

  Return Success

End Function

Private Sub CreatePage_AddAnyoneContact(response As ISBankInterfaceResponse_ZeptoNZ_AddAnyoneContact,
                                        htmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder)

  Dim ServiceLogSourceObjects As finServiceLogSourceObjects

  ' Get Requested Account or Client from Service Log
  ServiceLogSourceObjects = finBL.CreateServiceLogSourceObjects()
  ServiceLogSourceObjects.Load(ServiceLogs(0))

  With HtmlSummaryPage2StringBuilder
    .AppendH1(response.Request_AddAnyoneContact.Name)
    .AppendH2(String.Format("{0} - {1}", response.ProductId, response.ServiceName))

    .SummaryTablesAppend(Me.ZeptoNZ_AddAnyoneContact_Request(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_Contact(response.Contact, ServiceLogSourceObjects))
 End With

End Sub

Private Sub CreatePage_CreateKycAgreement(response As ISBankInterfaceResponse_ZeptoNZ_CreateKycAgreement,
                                          htmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder)

  Dim ServiceLogSourceObjects As finServiceLogSourceObjects

  ' Get Requested Account or Client from Service Log
  ServiceLogSourceObjects = finBL.CreateServiceLogSourceObjects()
  ServiceLogSourceObjects.Load(ServiceLogs(0))

  With HtmlSummaryPage2StringBuilder
    .AppendH1(response.Request_CreateKycAgreement.Name)
    .AppendH2(String.Format("{0} - {1}", response.ProductId, response.ServiceName))

    .SummaryTablesAppend(Me.ZeptoNZ_CreateKycAgreement_Request(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_Agreement(response.Agreement))
 End With

End Sub

Private Sub CreatePage_GetContact(response As ISBankInterfaceResponse_ZeptoNZ_GetContact,
                                  htmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder)

  Dim Agreements As ISBankInterfaceResponse_ZeptoNZ_Agreements
  Dim ServiceLog As ISServiceLog
  Dim ServiceLogSourceObjects As finServiceLogSourceObjects

  ' Get Requested Account or Client from Service Log
  ServiceLogSourceObjects = finBL.CreateServiceLogSourceObjects()
  ServiceLogSourceObjects.Load(ServiceLogs(0))

  ' Get Related Service Log for List of Agreements
  For Each ServiceLog In ServiceLogs
    Select Case ServiceLog.ExResponse.GetType()
      Case GetType(ISBankInterfaceResponse_ZeptoNZ_ListAgreements)
        Agreements = DirectCast(ServiceLog.ExResponse, ISBankInterfaceResponse_ZeptoNZ_ListAgreements).Agreements
    End Select
  Next

  With HtmlSummaryPage2StringBuilder
    .AppendH1(response.Contact.Name)
    '.AppendH2(String.Format("{0} - {1}", response.ProductId, response.ServiceName))

    .SummaryTablesAppend(Me.ZeptoNZ_GetContact_Request(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_Contact(response.Contact, ServiceLogSourceObjects))

    ' Agreements
    If Agreements IsNot Nothing Then
      .AppendH2("Agreements")
      .SummaryTablesAppend(Me.ZeptoNZ_ContactAgreements(Agreements))
    End If
 End With

End Sub

Private Sub CreatePage_MakePayment(response As ISBankInterfaceResponse_ZeptoNZ_MakePayment,
                                   htmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder)

  Dim Name As String
  Dim ServiceLogSourceObjects As finServiceLogSourceObjects

  ' Get Requested Account or Client from Service Log
  ServiceLogSourceObjects = finBL.CreateServiceLogSourceObjects()
  ServiceLogSourceObjects.Load(ServiceLogs(0))

  ' Get Name
  If ServiceLogSourceObjects.BankTransactionView Is Nothing Then
    Name = Response.Request_MakePayment.Payouts(0).RecipientContactId
  Else
    Name = ServiceLogSourceObjects.BankTransactionView.AccountTransactions(0).AccountName
  End If

  With HtmlSummaryPage2StringBuilder
    .AppendH1(Name)
    .AppendH2(String.Format("{0}", response.ServiceName))

    .SummaryTablesAppend(Me.ZeptoNZ_MakePayment_Request(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_MakePayment_Status(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_History())
 End With

End Sub

Private Sub CreatePage_RequestPayment(response As ISBankInterfaceResponse_ZeptoNZ_RequestPayment,
                                      htmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder)

  Dim Name As String
  Dim ServiceLogSourceObjects As finServiceLogSourceObjects

  ' Get Requested Account or Client from Service Log
  ServiceLogSourceObjects = finBL.CreateServiceLogSourceObjects()
  ServiceLogSourceObjects.Load(ServiceLogs(0))

  ' Get Name
  If ServiceLogSourceObjects.BankTransactionView Is Nothing Then
    Name = Response.Request_RequestPayment.ContactId
  Else
    Name = ServiceLogSourceObjects.BankTransactionView.AccountTransactions(0).AccountName
  End If

  With HtmlSummaryPage2StringBuilder
    .AppendH1(Name)
    .AppendH2(String.Format("{0}", response.ServiceName))

    .SummaryTablesAppend(Me.ZeptoNZ_RequestPayment_Request(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_RequestPayment_Status(response, ServiceLogSourceObjects))
    .SummaryTablesAppend(Me.ZeptoNZ_History())
 End With

End Sub

Private Function ZeptoNZ_AddAnyoneContact_Request(response As ISBankInterfaceResponse_ZeptoNZ_AddAnyoneContact,
                                                serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim BankAccount As finBankAccountRO
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Bank Account
  If finBL.BankAccounts.ExistsPk(response.Request_AddAnyoneContact.BankAccountPk) Then
    BankAccount = finBL.BankAccounts.ItemByPk(response.Request_AddAnyoneContact.BankAccountPk)
  Else
    BankAccount = Nothing
  End If

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", response.RequestUtcDate, finBL.Settings.TimeZoneId)
    .AddRowCaptionCodeDescription("Bank Account:", response.Request_AddAnyoneContact.BankAccountPk, finBL.BankAccounts, False)
    If serviceLogSourceObjects.Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Account:", serviceLogSourceObjects.Account.AccountId, serviceLogSourceObjects.Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", serviceLogSourceObjects.Account.AccountId, "Payments")
      End With
    ElseIf serviceLogSourceObjects.Client IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Client:", serviceLogSourceObjects.Client.ClientId, serviceLogSourceObjects.Client.Name)
        .Cells(1).HyperlinkSetFormShow("Clients", serviceLogSourceObjects.Client.ClientId, "Tax & Banking")
      End With
    End If
    .AddRowCaptionText("Name:", response.Request_AddAnyoneContact.Name)
    .AddRowCaptionText("Email:", response.Request_AddAnyoneContact.Email)
    .AddRowCaptionText("Phone:", response.Request_AddAnyoneContact.Phone)
    .AddRowCaptionText("Account Number:", finBL.BankingInterface.Service_ZeptoNZ.GetFormattedBankAccountNumber(response.Request_AddAnyoneContact.AccountNumber))
  End With
  SummaryTables.Add("Request", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_CreateKycAgreement_Request(response As ISBankInterfaceResponse_ZeptoNZ_CreateKycAgreement,
                                                    serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim BankAccount As finBankAccountRO
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Bank Account
  If finBL.BankAccounts.ExistsPk(response.Request_CreateKycAgreement.BankAccountPk) Then
    BankAccount = finBL.BankAccounts.ItemByPk(response.Request_CreateKycAgreement.BankAccountPk)
  Else
    BankAccount = Nothing
  End If

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", response.RequestUtcDate, finBL.Settings.TimeZoneId)
    .AddRowCaptionCodeDescription("Bank Account:", response.Request_CreateKycAgreement.BankAccountPk, finBL.BankAccounts, False)
    If serviceLogSourceObjects.Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Account:", serviceLogSourceObjects.Account.AccountId, serviceLogSourceObjects.Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", serviceLogSourceObjects.Account.AccountId, "Payments")
      End With
    ElseIf serviceLogSourceObjects.Client IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Client:", serviceLogSourceObjects.Client.ClientId, serviceLogSourceObjects.Client.Name)
        .Cells(1).HyperlinkSetFormShow("Clients", serviceLogSourceObjects.Client.ClientId, "Tax & Banking")
      End With
    End If
    .AddRowCaptionText("Name:", response.Request_CreateKycAgreement.Name)
    .AddRowCaptionText("Email:", response.Request_CreateKycAgreement.Email)
    .AddRowCaptionText("Phone", response.Request_CreateKycAgreement.Phone)
    .AddRowCaptionText("Account Number:", finBL.BankingInterface.Service_ZeptoNZ.GetFormattedBankAccountNumber(response.Request_CreateKycAgreement.AccountNumber))
  End With
  SummaryTables.Add("Request", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_GetContact_Request(response As ISBankInterfaceResponse_ZeptoNZ_GetContact,
                                            serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim AccountBankingDetail As finAccountBankingDetail
  Dim AccountClient As finAccountClient
  Dim BankAccount As finBankAccountRO
  Dim Found As Boolean
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As String

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Bank Account
  If finBL.BankAccounts.ExistsPk(response.Request_GetContact.BankAccountPk) Then
    BankAccount = finBL.BankAccounts.ItemByPk(response.Request_GetContact.BankAccountPk)
  Else
    BankAccount = Nothing
  End If

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", response.RequestUtcDate, finBL.Settings.TimeZoneId)
    If BankAccount IsNot Nothing Then
      .AddRowCaptionCodeDescription("Bank Account:", BankAccount.Pk, finBL.BankAccounts, False)
    End If  
    If serviceLogSourceObjects.Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Account:", serviceLogSourceObjects.Account.AccountId, serviceLogSourceObjects.Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", serviceLogSourceObjects.Account.AccountId, "Payments")
      End With
    ElseIf serviceLogSourceObjects.Client IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Client:", serviceLogSourceObjects.Client.ClientId, serviceLogSourceObjects.Client.Name)
        .Cells(1).HyperlinkSetFormShow("Clients", serviceLogSourceObjects.Client.ClientId, "Tax & Banking")
      End With
    End If

    ' Warnings
    Warnings = ""

    ' Verification Warnings
    If Len(response.Request_GetContact.VerifyAccountNumber) <> 0 AndAlso StrComp(response.Contact.BankAccount.AccountNumber, response.Request_GetContact.VerifyAccountNumber, CompareMethod.Text) <> 0 Then
      Warnings &= "{{warning|" & String.Format("Bank Account Numbers do not match - '{0}' differs from '{1}'.", finBL.BankingInterface.Service_ZeptoNZ.GetFormattedBankAccountNumber(Response.Contact.BankAccount.AccountNumber), finBL.BankingInterface.Service_ZeptoNZ.GetFormattedBankAccountNumber(response.Request_GetContact.VerifyAccountNumber)) & "|true}}"
    End If
    If Len(response.Request_GetContact.VerifyName) <> 0 AndAlso StrComp(response.Contact.Name, response.Request_GetContact.VerifyName, CompareMethod.Text) <> 0 Then
      Warnings &= "{{warning|" & String.Format("Names do not match - '{0}' differs from '{1}'.", Response.Contact.Name, response.Request_GetContact.VerifyName) & "|true}}"
    End If
    If Len(response.Request_GetContact.VerifyEmail) <> 0 AndAlso StrComp(response.Contact.Email, response.Request_GetContact.VerifyEmail, CompareMethod.Text) <> 0 Then
      Warnings &= "{{warning|" & String.Format("Email Addresses do not match - '{0}' differs from '{1}'.", Response.Contact.Email, response.Request_GetContact.VerifyEmail) & "|true}}"
    End If
    If Len(response.Request_GetContact.VerifyPhone) <> 0 AndAlso StrComp(response.Contact.PhoneE164, response.Request_GetContact.VerifyPhone, CompareMethod.Text) <> 0 Then
      Warnings &= "{{warning|" & String.Format("Phone Numbers do not match - '{0}' differs from '{1}'.", Response.Contact.PhoneRaw, response.Request_GetContact.VerifyPhone) & "|true}}"
    End If

    ' Account Warnings
    If serviceLogSourceObjects.Account IsNot Nothing Then
      ' Account Status
      Select Case serviceLogSourceObjects.Account.Status
        Case isefinAccountStatus.Open
        Case Else
          Warnings &= "{{warning|" & String.Format("Status is '{0}'.", serviceLogSourceObjects.Account.StatusText) & "}}"
      End Select

      ' Client Match
      Found = False
      For Each AccountClient In serviceLogSourceObjects.Account.Clients
        If AccountClient.IsOwner Then
          If StrComp(AccountClient.Client.Name, Response.Contact.Name, CompareMethod.Text) = 0 Then
            Found = True

            ' Other Checks
            If AccountClient.Client.ContactMethods.GetCurrentEmail().Value <> Response.Contact.Email Then
              Warnings &= "{{warning|Client Email address does not match Zepto Contact.}}"
            End If
          End If
        End If
      Next
      If Not Found Then
        Warnings &= "{{warning|Cannot match Name to an Owner of the Account.}}"
      End If

      ' Used for Payments?
      Found = False
      For Each AccountBankingDetail In serviceLogSourceObjects.Account.BankingDetails
        Select Case AccountBankingDetail.BankingDetails.PaymentTypeClass
          Case isePaymentTypeClass.ZeptoNZ
            Found = True
        End Select
      Next
      If Not Found Then
        Warnings &= "{{warning|Zepto (New Zealand) is NOT currently being used for Payments.}}"
      End If
    End If

    ' Client Warnings
    If serviceLogSourceObjects.Client IsNot Nothing Then
      ' Client Status
      If Not serviceLogSourceObjects.Client.Active Then
        Warnings &= "{{warning|Client is inactive.}}"
      End If

      ' Test name
      If StrComp(serviceLogSourceObjects.Client.Name, Response.Contact.Name, CompareMethod.Text) <> 0 Then
        Warnings &= "{{info|Client's Name does not match the Zepto Contact. Note, the Zepto Contact Name should match the Name of the Bank Account.}}"
      End If

      ' Other Checks
      If serviceLogSourceObjects.Client.ContactMethods.GetCurrentEmail().Value <> Response.Contact.Email Then
        Warnings &= "{{info|Client's current Email address does not match the Zepto Contact.}}"
      End If
      If serviceLogSourceObjects.Client.ContactMethods.GetCurrentMobile().ValueAsPhoneNumberE164 <> Response.Contact.PhoneE164 Then
        Warnings &= "{{info|Client's current Phone Number does not match the Zepto Contact.}}"
      End If
    End If
    .TextAfter = Warnings
  End With
  SummaryTables.Add("Request", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_Agreement(agreement As ISBankInterfaceResponse_ZeptoNZ_Agreement,
                        Optional headingLevel As Integer = 2) As ISSummaryTables

  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Agreement Details", "Agreement",headingLevel,,, TestMode)
  With SummaryTable
    .AddRowCaptionText("Reference:", agreement.Reference)
    .AddRowCaptionText("Status:", agreement.StatusText,,, agreement.StatusReason)
    .AddRowCaptionDateTime("Created:", agreement.CreatedAtDate)
    .AddRowCaptionDateTime("Responded:", agreement.RespondedAtDate)

    .AddRowCaptionText("Initiator:", agreement.InitiatorId)
    .AddRowCaptionText("Authoriser:", agreement.AuthoriserId)
    .AddRowCaptionText("Contact:", agreement.ContactId)
    .AddRowCaptionText("Bank Account:", agreement.BankAccountId)
    .AddRowCaptionText("Open Agreement:", agreement.OpenAgreementId)
  End With
  SummaryTables.Add("Agreement", SummaryTable)

  ' Terms
  SummaryTable = SummaryTables.CreateSummaryTableInformation("Terms", "Terms",headingLevel+1,,, TestMode)
  With SummaryTable
    .AddRowCaptionCurrency("Payout Minimum:", agreement.Terms.PerPayoutAmountMinimum)
    .AddRowCaptionCurrency("Payout Maximum:", agreement.Terms.PerPayoutAmountMaximum)
    .AddRowCaptionInteger("Frequency (Days):", agreement.Terms.PerFrequencyDays)
    .AddRowCaptionCurrency("Maximum per Frequency:", agreement.Terms.PerFrequencyAmountMaximum)
  End With
  SummaryTables.Add("Terms", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_Contact(contact As ISBankInterfaceResponse_ZeptoNZ_Contact,
                               serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim Account As finAccount
  Dim Client As finClient
  Dim Kvli As ISKeyValueListItem
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As String

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Related Account or Client
  finBL.BankingInterface.Service_ZeptoNZ.GetRelatedRecord(contact, Account, Client)

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Contact Details", "Contact",2,,, TestMode)
  With SummaryTable
    ' Related Account/ Client
    If Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Linked Account:", Account.AccountId, Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", Account.AccountId, "Payments")
      End With
    ElseIf Client IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Linked Client:", Client.ClientId, Client.Name)
        .Cells(1).HyperlinkSetFormShow("Clients", Client.ClientId, "Tax & Banking")
      End With
    End If

    ' Customer Information
    .AddRowCaptionText("Id:", Contact.Id)
    .AddRowCaptionText("Reference:", Contact.Reference)
    .AddRowCaptionText("Type:", Contact.TypeText)
    .AddRowCaptionText("Name:", Contact.Name)
    .AddRowCaptionText("Email:", Contact.Email)
    .AddRowCaptionText("Phone:", Contact.Phone)

    .AddRowCaptionText("Anyone Account:", Contact.AnyoneAccountId)

    ' Meta data
    If Contact.MetaData.Count <> 0 Then
      For Each Kvli In Contact.MetaData
        Select Case UCase(kvli.Key)
          Case "SOURCE_TYPE", "SOURCE_ID"
            ' Ignore standard Keys
          Case Else
            .AddRowCaptionText(kvli.Key & ":", kvli.ValueToString)
          End Select
      Next
    End If

    ' Warnings
    Warnings = ""
    If Account Is Nothing AndAlso Client Is Nothing Then
      Warnings &= "{{warning|Contact not found.|true}}"
    End If
    If serviceLogSourceObjects.Account IsNot Nothing Then
      If Account Is Nothing Then
      ElseIf serviceLogSourceObjects.Account.Pk <> Account.Pk Then
        Warnings &= "{{warning|The Account linked to the Contact DOES NOT match the requested Account.|True}}"
      End If
    ElseIf serviceLogSourceObjects.Client IsNot Nothing Then
      If Client Is Nothing Then
      ElseIf serviceLogSourceObjects.Client.Pk <> Client.Pk Then
        Warnings &= "{{warning|The Client linked to the Contact DOES NOT match the requested Client.|True}}"
      End If
    End If
    .TextAfter = Warnings
  End With
  SummaryTables.Add("Contact", SummaryTable)

  ' Bank Account
  If Contact.BankAccount.HasData Then
    SummaryTable = SummaryTables.CreateSummaryTableInformation("Bank Account", "BankAccount",3,,, TestMode)
    With SummaryTable
      ' Bank Information
      .AddRowCaptionText("Id:", Contact.BankAccount.Id)
      .AddRowCaptionText("Bank:", Contact.BankAccount.BankName)
      .AddRowCaptionText("Account Number:", finBL.BankingInterface.Service_ZeptoNZ.GetFormattedBankAccountNumber(Contact.BankAccount.AccountNumber))
      .AddRowCaptionText("State:", Contact.BankAccount.StateText)

      ' IAV Status
      Select Case Contact.BankAccount.IavStatus
        Case iseBankInterface_ZeptoNZ_IavStatus.Undefined
        Case Else
          .AddRowCaptionText("IAV Status:", Contact.BankAccount.IavStatusText)
          .AddRowCaptionText("IAV Provider:", Contact.BankAccount.IavProvider)
      End Select

      ' Warnings
      Warnings = ""
      If Contact.BankAccount.CreditsBlocked Then
        Warnings &= "{{warning|Credits blocked.|True}}"
      End If
      If Contact.BankAccount.DebitsBlocked Then
        Warnings &= "{{warning|Debits blocked.|True}}"
      End If
      .TextAfter = Warnings
    End With
    SummaryTables.Add("BankAccount", SummaryTable)
  End If

  Return SummaryTables

End Function

Private Function ZeptoNZ_ContactAgreements(agreements As ISBankInterfaceResponse_ZeptoNZ_Agreements) As ISSummaryTables

  Dim Agreement As ISBankInterfaceResponse_ZeptoNZ_Agreement
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  For Each Agreement In agreements
    SummaryTables.AppendSummaryTables(Me.ZeptoNZ_ContactAgreement(Agreement))
  Next

  Return SummaryTables

End Function

Private Function ZeptoNZ_ContactAgreement(agreement As ISBankInterfaceResponse_ZeptoNZ_Agreement) As ISSummaryTables

  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Agreement " & agreement.Reference, "Agreement",3,,, TestMode)
  With SummaryTable
    .AddRowCaptionText("Status:", agreement.StatusText,,, agreement.StatusReason)
    .AddRowCaptionDateTime("Created:", agreement.CreatedAtDate)
    .AddRowCaptionDateTime("Responded:", agreement.RespondedAtDate)

    .AddRowCaptionText("Initiator:", agreement.InitiatorId)
    .AddRowCaptionText("Authoriser:", agreement.AuthoriserId)
    .AddRowCaptionText("Zepto Bank Account:", agreement.BankAccountId)
    .AddRowCaptionText("Open Agreement:", agreement.OpenAgreementId)
  End With
  SummaryTables.Add("Agreement", SummaryTable)

  ' Terms
  SummaryTable = SummaryTables.CreateSummaryTableInformation("Terms", "Terms",4,,, TestMode)
  With SummaryTable
    .AddRowCaptionCurrency("Payout Minimum:", agreement.Terms.PerPayoutAmountMinimum)
    .AddRowCaptionCurrency("Payout Maximum:", agreement.Terms.PerPayoutAmountMinimum)
    .AddRowCaptionInteger("Frequency (Days):", agreement.Terms.PerFrequencyDays)
    .AddRowCaptionCurrency("Maximum per Frequency:", agreement.Terms.PerFrequencyAmountMaximum)
  End With
  SummaryTables.Add("Terms", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_MakePayment_Request(response As ISBankInterfaceResponse_ZeptoNZ_MakePayment,
                                             serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim Account As finAccount
  Dim BankAccount As finBankAccountRO
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Information
  If finBL.BankAccounts.ExistsPk(response.Request_MakePayment.BankAccountPk) Then
    BankAccount = finBL.BankAccounts.ItemByPk(response.Request_MakePayment.BankAccountPk)
  Else
    BankAccount = Nothing
  End If
  If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
    Account = serviceLogSourceObjects.BankTransactionView.AccountTransactions(0).Account
  Else
    Account = Nothing
  End If

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", response.RequestUtcDate, finBL.Settings.TimeZoneId)
    If BankAccount Is Nothing Then
      .AddRowCaptionText("Zepto Bank Account:", response.Request_MakePayment.YourBankAccountId)
    Else
      .AddRowCaptionCodeDescription("Bank Account:", BankAccount.Pk, finBL.BankAccounts, False)
    End If
    If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Bank Transaction:", serviceLogSourceObjects.BankTransactionView.BankTransactionPk.ToString(), serviceLogSourceObjects.BankTransactionView.ElementTypeText)
        .Cells(1).HyperlinkSetFormShow("BankTransactionView", serviceLogSourceObjects.BankTransactionView.BankTransactionPk.ToString())
      End With
    End If
    .AddRowCaptionText("Batch Description:", response.Request_MakePayment.Description)
  End With
  SummaryTables.Add("Request", SummaryTable)

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Payment", "Transaction", 3, , , TestMode)
  With SummaryTable
    .AddRowCaptionDate("Date:", response.Request_MakePayment.MaturesAtDate)
    If Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Account:", Account.AccountId, Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", Account.AccountId)
      End With
    End If
    .AddRowCaptionText("Contact Id:", response.Request_MakePayment.Payouts(0).RecipientContactId)
    .AddRowCaptionCurrency("Amount:", response.Request_MakePayment.Payouts(0).Amount, True)
    .AddRowCaptionText("Description:", response.Request_MakePayment.Payouts(0).Description)
  End With
  SummaryTables.Add("Transaction", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_MakePayment_Status(response As ISBankInterfaceResponse_ZeptoNZ_MakePayment,
                                            serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim BankTransactionView As finBankTransactionView
  Dim ZeptoNZCreditTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim ZeptoNZDebitTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim ZeptoNZReversalTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Information
  If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
    BankTransactionView = serviceLogSourceObjects.BankTransactionView
  Else
    BankTransactionView = Nothing
  End If

  ' Get Latest ZeptoNZ Transactions
  Me.GetLatestTransactions(ZeptoNZCreditTransaction, ZeptoNZDebitTransaction, ZeptoNZReversalTransaction)

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Overall Status", "Status", 2, , , TestMode)
  With SummaryTable
    If BankTransactionView Is Nothing Then
      .AddRowCaptionText("Status:", serviceLogs(0).ResponseStatusText)
      .AddRowCaptionText("Reference:", response.ResponseReference)
    Else
      With .Rows.AddCaptionText("Status:", BankTransactionView.BankInterfaceStatusText)
        Select Case BankTransactionView.BankInterfaceStatus
          Case isefinBankTransactionBankInterfaceStatus.Aborted
            .BackgroundColour = "gold"
          Case isefinBankTransactionBankInterfaceStatus.Failed, isefinBankTransactionBankInterfaceStatus.SendFailed
            .BackgroundColour = "pink"
          Case isefinBankTransactionBankInterfaceStatus.Pending, isefinBankTransactionBankInterfaceStatus.Queued
            .BackgroundColour = "orange"
          Case isefinBankTransactionBankInterfaceStatus.Success
            .BackgroundColour = "lightgreen"
        End Select
      End With
      .AddRowCaptionText("Reference:", BankTransactionView.BankInterfaceReference)
      If BankTransactionView.BankInterfaceSettlementDate <> Nothing Then
        .AddRowCaptionDate("Settlement Date:", BankTransactionView.BankInterfaceSettlementDate)
        .AddRowCaptionText("Settlement Reference:", BankTransactionView.BankInterfaceSettlementReference)
      End If
    End If

    ' Actions
    With .actions
      If BankTransactionView IsNot Nothing AndAlso BankTransactionView.CanSendViaBankInterface Then
        .AddApplicationShortcutUrl("Resend", "Resend", String.Format("App://Global?Type=BankingInterface.Send&BankTransactionPk={0}", bankTransactionView.BankTransactionPk), True).ShowInWebInterfaces = True
      End If

      If ServiceLogs(0).ResponseStatus = iseServiceLogResponseStatus.InProgress Then
        If finBL.CurrentUser.Permissions.Allow("Banking.BankInterface.Requests.Update") Then
          .AddApplicationShortcutUrl("UpdateStatus", "Update Status", String.Format("App://Global?Type=BankingInterface.UpdateStatus&ServiceLogPk={0}", ServiceLogs(0).Pk), True).ShowInWebInterfaces = True
        End If
        If finBL.CurrentUser.Permissions.Allow("Banking.BankInterface.Requests.Cancel") Then
          .AddApplicationShortcutUrl("Cancel", "Cancel", String.Format("App://Global?Type=BankingInterface.Cancel&ServiceLogPk={0}", ServiceLogs(0).Pk), True).ShowInWebInterfaces = True
        End If
      End If
    End With
  End With
  SummaryTables.Add("Status", SummaryTable)

  ' Transaction
  SummaryTable = SummaryTables.CreateSummaryTableInformation("Zepto Transaction", "Transaction", 3, , , TestMode)
  If ZeptoNZDebitTransaction IsNot Nothing Then
    With SummaryTable
      .AddRowCaptionText("Type:", ZeptoNZDebitTransaction.TypeText)
      .AddRowCaptionText("Category:", ZeptoNZDebitTransaction.CategoryText)
      .Rows.AddCaptionText("Status:", ZeptoNZDebitTransaction.StatusText).BackgroundColour = ZeptoNZCreditTransaction.StatusHtmlColour
      .AddRowCaptionDateTime("Created:", ZeptoNZDebitTransaction.CreatedAtDate)
      .AddRowCaptionText("Bank Account Id:", ZeptoNZDebitTransaction.BankAccountId)
      .AddRowCaptionText("Reference:", ZeptoNZDebitTransaction.Reference)
      .AddRowCaptionText("Bank Reference:", ZeptoNZDebitTransaction.BankReference)
      ''.AddRowCaptionText("Failure Reason:", ZeptoNZDebitTransaction.FailureReasonResolved)
      .AddRowCaptionDateTime("Matures:", ZeptoNZDebitTransaction.MaturesAtDate)
      .AddRowCaptionDateTime("Cleared:", ZeptoNZDebitTransaction.ClearedAtDate)

      .Rows.AddText("Other Party", iseSummaryTableCellClass.SectionHeading3)
      .AddRowCaptionText("Contact Id:", ZeptoNZDebitTransaction.PartyContactId)
      .AddRowCaptionText("Name:", ZeptoNZDebitTransaction.PartyName)
      .AddRowCaptionText("Nickname:", ZeptoNZDebitTransaction.PartyNickname)
      .AddRowCaptionText("Bank Reference:", ZeptoNZDebitTransaction.PartyBankRef)
    End With
  End If
  If ZeptoNZCreditTransaction IsNot Nothing Then
    With SummaryTable
      If ZeptoNZCreditTransaction.Failure.HasData Then
        .Rows.AddText("Failure", iseSummaryTableCellClass.SectionHeading3)
        .AddRowCaptionText("Type:", ZeptoNZCreditTransaction.Failure.Title,,,,,ZeptoNZCreditTransaction.Failure.Code)
        .AddRowCaptionText("Detail:", ZeptoNZCreditTransaction.Failure.Detail)
      End If
    End With
  End If  
  If ZeptoNZReversalTransaction IsNot Nothing Then
    With SummaryTable
      .Rows.AddText("Payout Reversal", iseSummaryTableCellClass.SectionHeading3)
      If ZeptoNZReversalTransaction.Failure.HasData Then
        .AddRowCaptionText("Failure:", ZeptoNZCreditTransaction.Failure.Title,,,,,ZeptoNZCreditTransaction.Failure.Code)
        .AddRowCaptionText("Detail:", ZeptoNZCreditTransaction.Failure.Detail)
      End If
    End With
  End If  
  If SummaryTable.HasData Then  SummaryTables.Add("Transaction", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_RequestPayment_Request(response As ISBankInterfaceResponse_ZeptoNZ_RequestPayment,
                                              serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim Account As finAccount
  Dim BankAccount As finBankAccountRO
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Information
  If finBL.BankAccounts.ExistsPk(response.Request_RequestPayment.BankAccountPk) Then
    BankAccount = finBL.BankAccounts.ItemByPk(response.Request_RequestPayment.BankAccountPk)
  Else
    BankAccount = Nothing
  End If
  If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
    Account = serviceLogSourceObjects.BankTransactionView.AccountTransactions(0).Account
  Else
    Account = Nothing
  End If

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", response.RequestUtcDate, finBL.Settings.TimeZoneId)
    If BankAccount Is Nothing Then
      .AddRowCaptionText("Zepto Bank Account:", response.Request_RequestPayment.YourBankAccountId)
    Else  
      .AddRowCaptionCodeDescription("Bank Account:", BankAccount.Pk, finBL.BankAccounts, False)
    End If
    If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Bank Transaction:", serviceLogSourceObjects.BankTransactionView.BankTransactionPk.ToString(), serviceLogSourceObjects.BankTransactionView.ElementTypeText)
        .Cells(1).HyperlinkSetFormShow("BankTransactionView", serviceLogSourceObjects.BankTransactionView.BankTransactionPk.ToString())
      End With
    End If
  End With
  SummaryTables.Add("Request", SummaryTable)

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Payment Request", "Transaction", 3, , , TestMode)
  With SummaryTable
    .AddRowCaptionDate("Date:", response.Request_RequestPayment.MaturesAtDate)
    If Account IsNot Nothing Then
      With .Rows.AddCaptionCodeDescription("Account:", Account.AccountId, Account.Name)
        .Cells(1).HyperlinkSetFormShow("Accounts", Account.AccountId)
      End With
    End If
    .AddRowCaptionText("Contact Id:", response.Request_RequestPayment.ContactId)
    .AddRowCaptionCurrency("Amount:", response.Request_RequestPayment.Amount, True)
    .AddRowCaptionText("Description:", response.Request_RequestPayment.Description)
  End With
  SummaryTables.Add("Transaction", SummaryTable)

  Return SummaryTables

End Function

Private Function ZeptoNZ_RequestPayment_Status(response As ISBankInterfaceResponse_ZeptoNZ_RequestPayment,
                                               serviceLogSourceObjects As finServiceLogSourceObjects) As ISSummaryTables

  Dim BankTransactionView As finBankTransactionView
  Dim ZeptoNZCreditTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim ZeptoNZDebitTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim ZeptoNZReversalTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables
  
  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Information
  If serviceLogSourceObjects.BankTransactionView IsNot Nothing Then
    BankTransactionView = serviceLogSourceObjects.BankTransactionView
  Else
    BankTransactionView = Nothing
  End If

  ' Get Latest ZeptoNZ Transactions
  Me.GetLatestTransactions(ZeptoNZCreditTransaction, ZeptoNZDebitTransaction, ZeptoNZReversalTransaction)

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Overall Status", "Status", 2, , , TestMode)
  With SummaryTable
    If BankTransactionView Is Nothing Then
      .AddRowCaptionText("Status:", serviceLogs(0).ResponseStatusText)
      .AddRowCaptionText("Reference:", response.ResponseReference)
    Else
      With .Rows.AddCaptionText("Status:", BankTransactionView.BankInterfaceStatusText)
        Select Case BankTransactionView.BankInterfaceStatus
          Case isefinBankTransactionBankInterfaceStatus.Aborted
            .BackgroundColour = "gold"
          Case isefinBankTransactionBankInterfaceStatus.Failed, isefinBankTransactionBankInterfaceStatus.SendFailed
            .BackgroundColour = "pink"
          Case isefinBankTransactionBankInterfaceStatus.Pending, isefinBankTransactionBankInterfaceStatus.Queued
            .BackgroundColour = "orange"
          Case isefinBankTransactionBankInterfaceStatus.Success
            .BackgroundColour = "lightgreen"
        End Select
      End With
      .AddRowCaptionText("Reference:", BankTransactionView.BankInterfaceReference)
      If BankTransactionView.BankInterfaceSettlementDate <> Nothing Then
        .AddRowCaptionDate("Settlement Date:", BankTransactionView.BankInterfaceSettlementDate)
        .AddRowCaptionText("Settlement Reference:", BankTransactionView.BankInterfaceSettlementReference)
      End If
    End If

    ' Actions
    With .actions
      If BankTransactionView IsNot Nothing AndAlso BankTransactionView.CanSendViaBankInterface Then
        .AddApplicationShortcutUrl("Resend", "Resend", String.Format("App://Global?Type=BankingInterface.Send&BankTransactionPk={0}", bankTransactionView.BankTransactionPk), True).ShowInWebInterfaces = True
      End If

      If ServiceLogs(0).ResponseStatus = iseServiceLogResponseStatus.InProgress Then
        If finBL.CurrentUser.Permissions.Allow("Banking.BankInterface.Requests.Update") Then
          .AddApplicationShortcutUrl("UpdateStatus", "Update Status", String.Format("App://Global?Type=BankingInterface.UpdateStatus&ServiceLogPk={0}", ServiceLogs(0).Pk), True).ShowInWebInterfaces = True
        End If
        If finBL.CurrentUser.Permissions.Allow("Banking.BankInterface.Requests.Cancel") Then
          .AddApplicationShortcutUrl("Cancel", "Cancel", String.Format("App://Global?Type=BankingInterface.Cancel&ServiceLogPk={0}", ServiceLogs(0).Pk), True).ShowInWebInterfaces = True
        End If
      End If
    End With
  End With
  SummaryTables.Add("Status", SummaryTable)

  ' Credit Transaction
  If ZeptoNZCreditTransaction IsNot Nothing Then
    SummaryTable = SummaryTables.CreateSummaryTableInformation("Zepto Transaction", "Transaction", 3, , , TestMode)
    With SummaryTable
      .AddRowCaptionText("Type:", ZeptoNZCreditTransaction.TypeText)
      .AddRowCaptionText("Category:", ZeptoNZCreditTransaction.CategoryText)
      .Rows.AddCaptionText("Status:", ZeptoNZCreditTransaction.StatusText).BackgroundColour = ZeptoNZCreditTransaction.StatusHtmlColour
      .AddRowCaptionDateTime("Created:", ZeptoNZCreditTransaction.CreatedAtDate)
      .AddRowCaptionText("Bank Account Id:", ZeptoNZCreditTransaction.BankAccountId)
      .AddRowCaptionText("Reference:", ZeptoNZCreditTransaction.Reference)
      .AddRowCaptionText("Bank Reference:", ZeptoNZCreditTransaction.BankReference)
      ''.AddRowCaptionText("Failure Reason:", ZeptoNZCreditTransaction.FailureReasonResolved)
      .AddRowCaptionDateTime("Matures:", ZeptoNZCreditTransaction.MaturesAtDate)
      .AddRowCaptionDateTime("Cleared:", ZeptoNZCreditTransaction.ClearedAtDate)

      .Rows.AddText("Other Party", iseSummaryTableCellClass.SectionHeading3)
      .AddRowCaptionText("Contact Id:", ZeptoNZCreditTransaction.PartyContactId)
      .AddRowCaptionText("Name:", ZeptoNZCreditTransaction.PartyName)
      .AddRowCaptionText("Nickname:", ZeptoNZCreditTransaction.PartyNickname)
      .AddRowCaptionText("Bank Reference:", ZeptoNZCreditTransaction.PartyBankRef)
    End With
    SummaryTables.Add("Transaction", SummaryTable)
  End If

  Return SummaryTables

End Function

Private Function ZeptoNZ_History() As ISSummaryTables

  Dim ServiceLog As ISServiceLog
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableTabular("History", "History", 2, TestMode)
  With SummaryTable
    ' Columns
    With .Columns
      .AddText("Service Log")
      .AddDate("Date")
      .AddText("User")
      .AddText("Type")
      .AddText("Status")
    End With

    ' Rows
    With .Rows
      For Each ServiceLog In ServiceLogs
        With .Add
          If Len(ServiceLog.ExStatusHtmlColour) <> 0 Then
            .BackgroundColour = ServiceLog.ExStatusHtmlColour
            If finBL.Runtime.ColourUtilities.HtmlColourIsDark(ServiceLog.ExStatusHtmlColour) Then .ForegroundColour = "white"
          End If
          With .Cells
            .AddText(CStr(ServiceLog.Pk)).HyperlinkSetFormShow("ServiceLog", CStr(ServiceLog.Pk))
            .AddDateTime(ServiceLog.CreatedDate,, True)
            .AddCodeDescription(ServiceLog.CreatedUserPk, finBL.Users)
            .AddText(ServiceLog.ProductId)
            .AddText(ServiceLog.ExStatus).Tooltip = ServiceLog.ExStatusTooltip
          End With
        End With
      Next
    End With
  End With
  SummaryTables.Add("History", SummaryTable)

  Return SummaryTables

End Function

Private Sub GetLatestTransactions(ByRef ZeptoNZCreditTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction,
                                  ByRef ZeptoNZDebitTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction,
                                  ByRef ZeptoNZReversalTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction)

  Dim ServiceLog As ISServiceLog
  Dim ZeptoNZTransaction As ISBankInterfaceResponse_ZeptoNZ_Transaction

  ' Initialise ByRef
  ZeptoNZCreditTransaction = Nothing
  ZeptoNZDebitTransaction = Nothing
  ZeptoNZReversalTransaction=Nothing

  ' Find Latest ZeptoNZ Transactions
  For Each ServiceLog In ServiceLogs
    If Servicelog.RequestStatus = iseAuditLogStatus.Success Then
      Select Case ServiceLog.ExResponse.GetType()
        Case GetType(ISBankInterfaceResponse_ZeptoNZ_ListTransactions)
          With DirectCast(ServiceLog.ExResponse, ISBankInterfaceResponse_ZeptoNZ_ListTransactions)
            For Each ZeptoNZTransaction In .Transactions
              If ZeptoNZTransaction.ParentReference = serviceLogs(0).ResponseReference Then
                Select Case ZeptoNZTransaction.Category
                  Case iseBankInterface_ZeptoNZ_TransactionCategory.Payout  
                    If ZeptoNZTransaction.Type = iseBankInterface_ZeptoNZ_TransactionType.Credit Then
                      ZeptoNZCreditTransaction = ZeptoNZTransaction
                    Else
                      ZeptoNZDebitTransaction = ZeptoNZTransaction
                    End If
                Case iseBankInterface_ZeptoNZ_TransactionCategory.PayoutReversal
                    ZeptoNZReversalTransaction = ZeptoNZTransaction
                End Select    
              End If
            Next
          End With

        Case GetType(ISBankInterfaceResponse_ZeptoNZ_Webhook)
          'TODO:
          
          With DirectCast(ServiceLog.ExResponse, ISBankInterfaceResponse_ZeptoNZ_Webhook)
            If .Data.ItemAsTransaction(0) IsNot Nothing Then
              If .Data.ItemAsTransaction(0).Type = iseBankInterface_ZeptoNZ_TransactionType.Credit Then
                ZeptoNZCreditTransaction = .Data.ItemAsTransaction(0)
              Else
                ZeptoNZDebitTransaction = .Data.ItemAsTransaction(0)
              End If
            End If
          End With
      End Select
    End If
  Next

End Sub