' SummaryPage2_Account_KeyDetails
Option Explicit On
Option Strict On

' #################################################################
' Account Key Details
'
' Version: 2.15 (23/11/2023)
'
' Usage: Accounts form
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.12  09/10/2020  ISS
'    2.13  04/11/2021  ISS      J027713   Updated obsoleted functions
'    2.14  12/04/2022  ISS      J028469   Made more robust for Account Applications
'    2.15  23/11/2023  ISS      J030836   Added the ability to show "Related via Hashtags" block
' #################################################################

'#Const "BankReferenceLength",Integer,0,"Length of Bank Reference. Will left pad with zeros if necessary, 0=no padding."
'#const "ShowClientAddressInSummary",Boolean,False,"Show the Main Client's postal address in the Summary block."
'#const "ShowClientCity",Boolean,True,"Show the City (or suburb) in the Clients block."
'#const "ShowClientAddressFull",Boolean,True,"Show the full Address in the Clients block."
'#const "ShowClientContactMethods",Boolean,False,"Show Client Contact Methods in the Clients block."
'#const "ContactMethodIds",String,"","A range of Contact Methods to include or a blank String to include all uncategorised Contact Methods."
'#const "ShowClientDateOfBirth",Boolean,False,"Show the Client's date of birth in the Clients block."
'#const "ShowClientPassword",Boolean,True,"Show the Client's Pass Code popup in the Clients block."
'#const "ShowCreditLimitBlock",Boolean,True,"Show Credit Limits block."
'#const "ShowMinimumPaymentsDueBlock",Boolean,True,"Show Minimum Payments Due block."
'#const "ShowInterestBlock",Boolean,False,"Show Interest block."
'#const "ShowRelatedViaHashtagsBlock",Boolean,True,"Show Related via Hashtags block."
'#const "ShowWarningBankTransactions",Boolean,False,"Show warnings related to Bank Transactions, e.g. Cash Payments not lodged, Unprinted Cheques, Direct Debits still to be processed."
'#const "LastPaymentDaysMaximum",Integer,120,"Maximum Days for warning since last Reversed Payment."
'#const "HistoryRating1Include",Boolean,True,"Show History Rating 1 (Account Type must also show)?"
'#const "HistoryRating1Heading",String,"","The heading for History Rating 1."
'#const "HistoryRating2Include",Boolean,False,"Show History Rating 2 (Account Type must also show)?"
'#const "HistoryRating2Heading",String,"","The heading for History Rating 2."
'#const "HistoryRating3Include",Boolean,False,"Show History Rating 3 (Account Type must also show)?"
'#const "HistoryRating3Heading",String,"","The heading for History Rating 3."
'#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 BankReferenceLength As Integer = 0
Private ContactMethodIds As String = ""
Private EventAuditInclude As Boolean = False
Private EventNumberOfDays As Integer = 30
Private HistoryRating1Include As Boolean = True
Private HistoryRating1Heading As String = ""
Private HistoryRating2Include As Boolean = False
Private HistoryRating2Heading As String = ""
Private HistoryRating3Include As Boolean = False
Private HistoryRating3Heading As String = ""
Private LastPaymentDaysMaximum As Integer = 120
Private ShowClientAddressInSummary As Boolean = False
Private ShowClientCity As Boolean = True
Private ShowClientAddressFull As Boolean = False
Private ShowClientContactMethods As Boolean = False
Private ShowClientDateOfBirth As Boolean = False
Private ShowClientPassCode As Boolean = True
Private ShowCreditLimitBlock As Boolean = True
Private ShowEventBlock As Boolean = False
Private ShowMinimumPaymentsDueBlock As Boolean = True
Private ShowInterestBlock As Boolean = False
Private ShowRelatedViaHashtagsBlock As Boolean = True
Private ShowWarningBankTransactions As Boolean = False

' Objects
Private mAccount As finAccount

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
    mAccount = finBL.CreateAccount()
    mAccount.Load(CStr(source))

    ' Check not filtered out
    ' NOTE: Not necessary since this is done in the business layer
  Else
    mAccount = DirectCast(source, finAccount)
  End If

  ' Get Constants
  With ScriptInfo.Constants
    BankReferenceLength = .GetInteger("BankReferenceLength", BankReferenceLength)
    ContactMethodIds = .GetString("ContactMethodIds", ContactMethodIds)
    EventAuditInclude = .GetBoolean("EventAuditInclude", EventAuditInclude)
    EventNumberOfDays = .GetInteger("EventNumberOfDays", EventNumberOfDays)
    HistoryRating1Include = .GetBoolean("HistoryRating1Include", HistoryRating1Include)
    HistoryRating1Heading = .GetString("HistoryRating1Heading", HistoryRating1Heading)
    HistoryRating2Include = .GetBoolean("HistoryRating2Include", HistoryRating2Include)
    HistoryRating2Heading = .GetString("HistoryRating2Heading", HistoryRating2Heading)
    HistoryRating3Include = .GetBoolean("HistoryRating3Include", HistoryRating3Include)
    HistoryRating3Heading = .GetString("HistoryRating3Heading", HistoryRating3Heading)
    LastPaymentDaysMaximum = .GetInteger("LastPaymentDaysMaximum", LastPaymentDaysMaximum)
    ShowClientAddressInSummary = .GetBoolean("ShowClientAddressInSummary", ShowClientAddressInSummary)
    ShowClientCity = .GetBoolean("ShowClientCity", ShowClientCity)
    ShowClientAddressFull = .GetBoolean("ShowClientAddressFull", ShowClientAddressFull)
    ShowClientContactMethods = .GetBoolean("ShowClientContactMethods", ShowClientContactMethods)
    ShowClientDateOfBirth = .GetBoolean("ShowClientDateOfBirth", ShowClientDateOfBirth)
    ShowClientPassCode = .GetBoolean("ShowClientPassword", ShowClientPassCode)
    ShowCreditLimitBlock = .GetBoolean("ShowCreditLimitBlock", ShowCreditLimitBlock)
    ShowEventBlock = .GetBoolean("ShowEventBlock", ShowEventBlock)
    ShowMinimumPaymentsDueBlock = .GetBoolean("ShowMinimumPaymentsDueBlock", ShowMinimumPaymentsDueBlock)
    ShowInterestBlock = .GetBoolean("ShowInterestBlock", ShowInterestBlock)
    ShowRelatedViaHashtagsBlock = .GetBoolean("ShowRelatedViaHashtagsBlock", ShowRelatedViaHashtagsBlock)
    ShowWarningBankTransactions = .GetBoolean("ShowWarningBankTransactions", ShowWarningBankTransactions)
  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 AccountOverdues As finAccountOverdues
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim strTemp As String
  Dim SummaryTable As ISSummaryTable 
  Dim Warnings As finWarnings
  Dim WorkflowIncompleteCount As Integer

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  If mAccount.IsNew AndAlso mAccount.AccountApp Is Nothing Then
    ' No Account loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please enter a code to view an Account record.}}")
  Else
    ' Client
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", If(Len(mAccount.AlertMessage) = 0, isefinSummaryPageBlockHeadingStyle.Main, isefinSummaryPageBlockHeadingStyle.Warning), False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Summary(ScriptInfo, mAccount, Me.ScriptInfo.FormKey <> "Accounts", ShowClientAddressInSummary, BankReferenceLength))
    sb.BlockEnd()

    ' Financial Status
    If mAccount.Status = isefinAccountStatus.Open Then
      sb.BlockBegin("Financial Status", "", isefinSummaryPageBlockHeadingStyle.Normal, True)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(ScriptInfo, mAccount, AccountOverdues))
      sb.BlockEnd()
    End If

    ' History Rating
    If mAccount.AccountType IsNot Nothing AndAlso (mAccount.AccountType.ShowHistoryRating AndAlso (HistoryRating1Include OrElse HistoryRating2Include OrElse HistoryRating3Include)) Then
      Select Case mAccount.Status
        Case isefinAccountStatus.Open, isefinAccountStatus.ClosedPending, isefinAccountStatus.Closed
          strTemp = finBL.SummaryPageStandardBlocks.Account_HistoryRatingDiagrams(ScriptInfo, mAccount, HistoryRating1Include AndAlso Len(mAccount.HistoryRating) <> 0, HistoryRating1Heading, HistoryRating2Include AndAlso Len(mAccount.HistoryRating2) <> 0, HistoryRating2Heading, HistoryRating3Include AndAlso Len(mAccount.HistoryRating3) <> 0, HistoryRating3Heading, "640px")

          If Len(strTemp) <> 0 Then
            sb.BlockBegin("History Rating", "", isefinSummaryPageBlockHeadingStyle.Normal, True)
            sb.Append(strTemp)
            sb.BlockEnd()
          End If
      End Select
    End If

    ' Clients
    sb.BlockBegin("Clients", "", If(mAccount.Clients.HasClientWithAlertMessage(), isefinSummaryPageBlockHeadingStyle.Warning, isefinSummaryPageBlockHeadingStyle.Normal))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Clients2(ScriptInfo, mAccount, ShowClientCity, ShowClientAddressFull, ShowClientContactMethods, ContactMethodIds, ShowClientDateOfBirth, ShowClientPassCode, True, True, True, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' Credit Limits
    If ShowCreditLimitBlock AndAlso mAccount.AccountType IsNot Nothing AndAlso mAccount.AccountType.CreditLimitCount > 0 Then
      sb.BlockBegin("Credit Limits")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_CreditLimits(ScriptInfo, mAccount, False), "", "")
      sb.BlockEnd()
    End If

    ' Minimum Payments Due
    If ShowMinimumPaymentsDueBlock AndAlso mAccount.AccountClass = isefinAccountClass.LoanRC AndAlso mAccount.Status = isefinAccountStatus.Open  Then
      sb.BlockBegin("Minimum Payments Due")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_MinimumPaymentsDue(ScriptInfo, mAccount, AccountOverdues, Nothing, False))
      sb.BlockEnd()
    End If

    ' Workflows
    If finBL.Settings.AddOns.Workflow AndAlso Not mAccount.IsNew Then
      mAccount.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.Account_IncompleteWorkflows(ScriptInfo, mAccount, , 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.Account, mAccount.Pk, mAccount.AccountId, mAccount.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")

    ' Warnings
    Warnings = mAccount.GetWarnings(True, ShowWarningBankTransactions, True, False, True, True, True)
    sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    sb.BlockEnd()

    ' Monitoring
    If finBL.Settings.MonitorCategoriesUsed AndAlso Not mAccount.IsNew Then
      sb.BlockBegin("Monitoring", "", If(mAccount.MonitorCategoryAPk = 0 AndAlso mAccount.MonitorCategoryBPk = 0 AndAlso mAccount.MonitorCategoryCPk = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Monitoring(ScriptInfo, mAccount, False, False))
      sb.BlockEnd()
    End If

    ' Interest
    If ShowInterestBlock AndAlso mAccount.SupportsInterest Then
      sb.BlockBegin("Interest")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Interest2(ScriptInfo, mAccount, Nothing, True, True, True, True,, False))
      sb.BlockEnd()
    End If

    ' Events
    If ShowEventBlock Then
      sb.BlockBegin("Events")
      sb.SummaryTableAppend(finBL.SummaryPageStandardBlocks.Events_Summary(ScriptInfo, mAccount.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