' SummaryPage2_Client_KeyDetails
Option Explicit On
Option Strict On

' #################################################################
' Client Key Details
' 
' Version: 2.17 (16/05/2024)
' 
' Usage: Clients form
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.12  17/08/2020  ISS
'    2.13  02/02/2023  ISS      J030415   Remove obsoleted function
'    2.14  20/04/2023  ISS      J030717   Updated standard block
'    2.15  23/11/2023  ISS      J030836   Added the ability to show "Related via Hashtags" block
'    2.16  24/04/2024  ISS      J031283   Corrected spelling errors
'    2.17  16/05/2024  ISS      J033462   New constants to include PEP Status of No or Unknown for Individual clients. Defaulted to FALSE.
' #################################################################

'#Const "ImageWidthPixels",Integer,240,"The number of pixels to fix the Client image or video width to. Set to zero to never show the Client image or video."
'#Const "IncludeClosedAccounts",Boolean,False,"Include Closed Accounts."
'#Const "IncludeInvalidContactMethods",Boolean,False,"Include Invalid Contact Methods in Summary."
'#Const "IncludePepStatusNo",Boolean,False,"Include PEP Status of 'No' in warnings (Individual clients only)?"
'#Const "IncludePepStatusUnknown",Boolean,False,"Include PEP Status of 'Unknown' in warnings (Individual clients only)?"
'#Const "HideClosedAccounts",Boolean,True,"Hide Closed Accounts if they have been included."
'#Const "ShowAccounts",Boolean,True,"Show Accounts."
'#Const "ShowAccountsMax",Integer,10,"Number of Accounts to show in table (zero for all). All non-Closed Accounts and Applications will be shown."
'#Const "ShowAccountsOrderBy",String,"","Order that Accounts are listed in.",",Reverse,DateOpenedDesc"
'#Const "ShowAccountDescription",Boolean,False,"Show Accounts 'Description' column."
'#Const "ShowAccountOpening",Boolean,False,"Show Accounts 'Opening' column."
'#Const "ShowAccountOverdue",Boolean,False,"Show Accounts 'Overdue' column."
'#Const "ShowAccountContractualOverdue",Boolean,False,"Show Accounts 'Contractual Overdue' column."
'#Const "ShowAccountTotals",Boolean,True,"Show Accounts Totals row."
'#Const "ShowCreditBureauMonitoring",Boolean,True,"Show Credit Bureau Monitoring."
'#Const "ShowOtherContactsNotes",Boolean,False,"Show Other Contacts 'Notes' column."
'#Const "ShowRelatedEmployments",Boolean,False,"Show Individual Employments related to any Branches or External Parties. Valid only if Licenced for Branches or External Parties."
'#const "ShowRelatedViaHashtagsBlock",Boolean,True,"Show Related via Hashtags block."
'#Const "ShowAkas",Boolean,True,"Show a table of AKAs."
'#Const "ShowMap",Boolean,False,"Show a map of the primary physical Contact Address."

'#const "EventNumberOfDays",Integer,30,"Number of Days of Events to display."
'#const "EventAuditInclude",Boolean,False,"Include Audit Events?"
'#const "ShowEventBlock",Boolean,False,"Show Event block?"

' Constants
Private ImageWidthPixels As Integer = 240
Private IncludeClosedAccounts As Boolean = False
Private IncludeInvalidContactMethods As Boolean = False
Private IncludePepStatusNo As Boolean = False
Private IncludePepStatusUnknown As Boolean = False
Private HideClosedAccounts As Boolean = True
Private ShowAccounts As Boolean = True
Private ShowAccountsMax As Integer = 10
Private ShowAccountsOrderBy As String = ""
Private ShowAccountDescription As Boolean = False
Private ShowAccountOpening As Boolean = False
Private ShowAccountOverdue As Boolean = False
Private ShowAccountContractualOverdue As Boolean = False
Private ShowAccountTotals As Boolean = True
Private ShowCreditBureauMonitoring As Boolean = True
Private ShowOtherContactsNotes As Boolean = False
Private ShowRelatedViaHashtagsBlock As Boolean = True
Private ShowRelatedEmployments As Boolean = False
Private ShowAkas As Boolean = True
Private ShowMap As Boolean = False

Private EventAuditInclude As Boolean = False
Private EventNumberOfDays As Integer = 30
Private ShowEventBlock As Boolean = False

' Objects
Private mAccountClient As finAccountClient
Private mClient As finClient

' Other
Private mAccountClientProductTypeId As String

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

  ' Assume Success
  Main = True

  ' Initialise
  If TypeOf source Is String Then
    mClient = finBL.CreateClient()
    mClient.Load(CStr(source))

    ' Check not filtered out
    If finBL.CurrentUserInformation.IsClientFilteredOut(mClient.ClientId) Then
      Throw New ApplicationException(finBL.Error.Message)
    End If
  Else
    mClient = DirectCast(source, finClient)
  End If

  ' Is Account Client supplied (e.g., previewing from Accounts form, Clients page)?
  If TypeOf eventArgs.GetObject("AccountClient") Is finAccountClient Then
    mAccountClient = DirectCast(eventArgs.GetObject("AccountClient"), finAccountClient)
    mAccountClientProductTypeId = eventArgs.GetString("ProductTypeId")
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ImageWidthPixels = .GetInteger("ImageWidthPixels", ImageWidthPixels)
    ShowAccounts = .GetBoolean("ShowAccounts", ShowAccounts)
    If .Exists("ShowAccountsClosed") Then
      ' Constant was deprecated in 2.03.03 but is still respected if present
      If .GetBoolean("ShowAccountsClosed", False) Then
        IncludeClosedAccounts = True
        HideClosedAccounts = False
      Else
        IncludeClosedAccounts = False
      End If
    Else
      IncludeClosedAccounts = .GetBoolean("IncludeClosedAccounts", IncludeClosedAccounts)
      HideClosedAccounts = .GetBoolean("HideClosedAccounts", HideClosedAccounts)
    End If
    IncludeInvalidContactMethods = .GetBoolean("IncludeInvalidContactMethods", IncludeInvalidContactMethods)
    IncludePepStatusNo = .GetBoolean("IncludePepStatusNo", IncludePepStatusNo)
    IncludePepStatusUnknown = .GetBoolean("IncludePepStatusUnknown", IncludePepStatusUnknown)
    ShowAccountsMax = .GetInteger("ShowAccountsMax", ShowAccountsMax)
    ShowAccountsOrderBy = .GetString("ShowAccountsOrderBy", ShowAccountsOrderBy)
    ShowAccountDescription = .GetBoolean("ShowAccountDescription", ShowAccountDescription)
    ShowAccountOpening = .GetBoolean("ShowAccountOpening", ShowAccountOpening)
    ShowAccountOverdue = .GetBoolean("ShowAccountOverdue", ShowAccountOverdue)
    ShowAccountContractualOverdue = .GetBoolean("ShowAccountContractualOverdue", ShowAccountContractualOverdue)
    ShowAccountTotals = .GetBoolean("ShowAccountTotals", ShowAccountTotals)
    ShowCreditBureauMonitoring = .GetBoolean("ShowCreditBureauMonitoring", ShowCreditBureauMonitoring)
    ShowOtherContactsNotes = .GetBoolean("ShowOtherContactsNotes", ShowOtherContactsNotes)
    ShowRelatedEmployments = .GetBoolean("ShowRelatedEmployments", ShowRelatedEmployments)
    ShowRelatedViaHashtagsBlock = .GetBoolean("ShowRelatedViaHashtagsBlock", ShowRelatedViaHashtagsBlock)    
    ShowAkas = .GetBoolean("ShowAkas", ShowAkas)
    ShowMap = .GetBoolean("ShowMap", ShowMap)

    ShowEventBlock = .GetBoolean("ShowEventBlock", ShowEventBlock)
    EventAuditInclude = .GetBoolean("EventAuditInclude", EventAuditInclude)
    EventNumberOfDays = .GetInteger("EventNumberOfDays", EventNumberOfDays)
  End With

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

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim IncludedContactMethodPks As ISList
  Dim kvl As ISKeyValueList
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SummaryTable As ISSummaryTable 
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As finWarnings
  Dim WorkflowIncompleteCount As Integer

  ' Assume Success
  Ok = True

  ' Initialise
  kvl = finBL.CreateKeyValueList
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin("", isefinSummaryPageStyle.Blocks)
    .HtmlHeaderEnd()
  End With

  If mClient.IsNew AndAlso mClient.AccountAppApplicantPk = 0 Then
    ' No Client loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please enter a code to view a Client record.}}")
  ElseIf finBL.CurrentUserInformation.IsClientFilteredOut(mClient.ClientId) Then
    ' Filtered out Client
    ' Begin Section
    sb.SectionColumnsBegin()

    ' Begin Column 1
    sb.ColumnBegin("100%")

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_SummaryFilteredOut(ScriptInfo, mClient, Me.ScriptInfo.FormKey <> "Clients"))
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  Else
    ' Client
    ' Begin Section
    sb.SectionColumnsBegin()

    ' Begin Column 1
    sb.ColumnBegin("60%")

    ' Summary
    ' Other Parameters
    kvl.Clear
    kvl.SetInteger("ImageWidthPixels", ImageWidthPixels)
    kvl.SetBoolean("SingleTable", True)

    sb.BlockBegin("Summary", "", If(Len(mClient.AlertMessage) = 0, isefinSummaryPageBlockHeadingStyle.Main, isefinSummaryPageBlockHeadingStyle.Warning), False)
    SummaryTables = finBL.SummaryPageStandardBlocks.Client_Summary3(ScriptInfo, mClient, IncludedContactMethodPks, (Me.ScriptInfo.FormKey <> "Clients"), IncludeInvalidContactMethods, ShowAkas, kvl)
    sb.SummaryTablesAppend(SummaryTables)
    sb.BlockEnd()

    ' Credit Bureau Monitoring
    If ShowCreditBureauMonitoring Then
      If mClient.IsCreditBureauMonitored OrElse mClient.CanCreditBureauMonitorBeStarted("CentrixNZ") Then
        sb.BlockBegin("Credit Bureau Monitoring")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_CreditBureauMonitoring(ScriptInfo, mClient))
        sb.BlockEnd()
      End If
    End If

    ' Accounts
    If ShowAccounts AndAlso Not mClient.IsNew Then
      ' Other Parameters
      kvl.Clear
      kvl.SetString("OrderBy", ShowAccountsOrderBy)

      sb.BlockBegin("Accounts", "", isefinSummaryPageBlockHeadingStyle.Normal, True)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_Accounts2(ScriptInfo, mClient, IncludeClosedAccounts, HideClosedAccounts, ShowAccountDescription, ShowAccountOpening, ShowAccountOverdue, ShowAccountContractualOverdue, ShowAccountTotals, ShowAccountsMax, kvl))
      sb.BlockEnd()
    End If

    ' Workflows
    If finBL.Settings.AddOns.Workflow AndAlso Not mClient.IsNew Then
      mClient.GetIncompleteWorkflowsCount(WorkflowIncompleteCount)
      sb.BlockBegin("Workflows", "", If(WorkflowIncompleteCount = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.WarningLow))
      If WorkflowIncompleteCount = 0 Then
        sb.AppendLine("{{info|No current Workflows.}}")
      Else
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_IncompleteWorkflows(ScriptInfo, mClient, , ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      End If
      sb.BlockEnd()
    End If

    ' Related via Hashtags
    If ShowRelatedViaHashtagsBlock AndAlso finBL.Settings.QuickSearchUseVersion2 AndAlso finBL.Settings.QuickSearch2SupportHashtags Then
      SummaryTable = finBL.Utilities.GetSummaryTableOfHashTagReferences(isefinSearchSourceType.Client, mClient.Pk, mClient.ClientId, mClient.Notes, "Related via Hashtags")

      If SummaryTable IsNot Nothing Then
        sb.BlockBegin("Related via Hashtags")
        sb.SummaryTableAppend(SummaryTable)
        sb.BlockEnd()
      End If
    End If

    ' End Column 1
    sb.ColumnEnd()

    ' Begin Column 2
    sb.ColumnBegin("40%", "25em", "50em")

    ' Account Client
    If mAccountClient IsNot Nothing Then
      sb.BlockBegin("Account Client")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountClient_Summary(ScriptInfo, mAccountClient, mAccountClientProductTypeId, False))
      sb.BlockEnd()
    End If

    ' Warnings
    Warnings = mClient.GetWarnings(, , , , , , , IncludePepStatusNo, IncludePepStatusUnknown)
    sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    sb.BlockEnd()

    ' Addresses
    sb.BlockBegin("Addresses")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_Addresses(ScriptInfo, mClient))
    sb.BlockEnd()

    ' Map
    If ShowMap Then
      If mClient.ContactAddresses.GetCurrentPhysicalAddressContactAddress() IsNot Nothing Then
        sb.BlockBegin("Map")
        sb.Append(finBL.SummaryPageStandardBlocks.Client_Map(ScriptInfo, mClient))
        sb.BlockEnd()
      End If
    End If

    ' Other Contacts
    SummaryTables = finBL.SummaryPageStandardBlocks.Client_OtherContacts(ScriptInfo, mClient, IncludedContactMethodPks, ShowOtherContactsNotes)
    sb.BlockBegin("Other Contacts")
    sb.SummaryTablesAppend(SummaryTables)
    sb.BlockEnd()

    ' Related Employment
    If ShowRelatedEmployments AndAlso Not mClient.IsNew AndAlso mClient.IsIndividual AndAlso finBL.Settings.AddOns.ClientAdvanced AndAlso (finBL.Settings.AddOns.ExternalParty OrElse finBL.Settings.AddOns.Branch) Then
      sb.BlockBegin("Related Employment")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_RelatedEmployment(ScriptInfo, mClient))
      sb.BlockEnd()
    End If

    ' Events
    If ShowEventBlock Then
      sb.BlockBegin("Events")
      sb.SummaryTableAppend(finBL.SummaryPageStandardBlocks.Events_Summary(ScriptInfo, mClient.GetEvents(EventNumberOfDays, EventAuditInclude), Nothing))
      sb.BlockEnd()
    End If

    ' End Column 2
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function