' SummaryPage2_AccountClients_ClientSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Client Summary

' Version: 2.00 (20/08/2013)

' Usage: Accounts form, Clients page
' #################################################################

'#Const "ImageWidthPixels",Integer,200,"The number of pixels to fix the Client image width to. Set to zero to never show the Client image."
'#Const "ShowAccounts",Boolean,True,"Show Accounts."
'#Const "ShowAccountsClosed",Boolean,False,"Show Closed Accounts."
'#Const "ShowAccountsMax",Integer,10,"Number of Accounts to show in table (zero for all)."
'#Const "ShowAccountDescription",Boolean,False,"Show Accounts 'Description' column."
'#Const "ShowAccountOverdue",Boolean,False,"Show Accounts 'Overdue' column."
'#Const "ShowAccountContractualOverdue",Boolean,False,"Show Accounts 'Overdue' column."
'#Const "ShowAccountTotals",Boolean,True,"Show Accounts Totals row."
'#Const "ShowOtherContactsNotes",Boolean,False,"Show Other Contacts 'Notes' column."
'#Const "ShowRelatedEmployments",Boolean,False,"Show Individual Employments related to any Branches or External Parties. Vaild only if Licened for Branches or External Parties."
'#Const "ShowAkas",Boolean,True,"Show a table of AKAs."
'#Const "ShowMap",Boolean,False,"Show a map of the primary physical Contact Address."

'#Const "ClientSummaryScriptId",String,"","The Id of a Client Summary Script to use instead of the Built-In Script."

' Constats
Private ClientSummaryScriptId 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

  Dim Account As finAccount
  Dim AccountClient As finAccountClient
  Dim EventArgs2 As ISKeyValueList
  Dim Index As Integer
  Dim ReturnValue As Boolean

  ' Assume Success
  Main = True

  ' Initialise
  Account = DirectCast(source, finAccount)
  Index = eventArgs.GetInteger("Index")
  If Index >= 0 AndAlso Index < Account.Clients.Count Then
    AccountClient = Account.Clients(Index)
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ClientSummaryScriptId = .GetString("ClientSummaryScriptId", ClientSummaryScriptId)
  End With

  ' Validate
  If Len(ClientSummaryScriptId) <> 0 Then
    If Not finBL.Scripts.Exists(ClientSummaryScriptId) Then
      Main = False
      finBL.Error.ErrorBeginFormat("ClientSummaryScriptId '{0}' does not exist.", ClientSummaryScriptId)
    End If
  End If

  If Main Then
    ' Create Event Args to pass AccountClient object across to a Client Summary Script
    EventArgs2 = finBL.CreateKeyValueList()
    With EventArgs2
      .SetObject("AccountClient", AccountClient)
      .SetString("ProductTypeId", eventArgs.GetString("ProductTypeId")) ' Used when summarising from New Account wizard since the Account Type may not yet have been selected
    End With

    If Len(ClientSummaryScriptId) = 0 Then
      ' Use Built-In Script to generate Client Summary (also pass through Constants since this script defines an identical set)
      text = finBL.ScriptFunctions.BuiltInClientGeneralSummaryScript2(AccountClient.Client, eventId, EventArgs2, handled, returnValues, ScriptInfo.ContextData.ToKeyValueList(), ScriptInfo.Properties, ScriptInfo.Constants)
    Else
      ' Use specified Script to generate Client Summary
      Main = finBL.Scripts(ClientSummaryScriptId).ExecuteSummaryPage2(ReturnValue, AccountClient.Client, eventId, EventArgs2, handled, returnValues, ScriptInfo.ContextData.ToKeyValueList(), ScriptInfo.Properties, text)
    End If
  End If

End Function