' SummaryPage2_Account_Status
Option Explicit On
Option Strict On

' #################################################################
' Account Status

' Version: 2.10 (29/11/2019)

' Usage: Accounts form, Status page.
' #################################################################

'#Const "LastPaymentDaysMaximum",Integer,120,"Maximum Days for warning since last Reversed Payment."
'#const "ShowComprehensiveCreditReporting",Boolean,True,"Show Comprehensive Credit Reporting block"
'#const "ShowOverdueAgedSummary",Boolean,True,"Show the Aged Overdue breakdown table"
'#const "ShowOverdueAgedSummaryFull",Boolean,False,"Show all rows in the Aged Overdue breakdown table"
'#const "ShowOverdueTransactions",Boolean,True,"Show the Overdue Transactions table"
'#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."

' Constants
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 ShowComprehensiveCreditReporting As Boolean = True
Private ShowOverdueAgedSummary As Boolean = True
Private ShowOverdueAgedSummaryFull As Boolean = False
Private ShowOverdueTransactions As Boolean = True

' 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
  mAccount = DirectCast(source, finAccount)

  ' Get Constants
  With ScriptInfo.Constants
    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)
    ShowComprehensiveCreditReporting = .GetBoolean("ShowComprehensiveCreditReporting", ShowComprehensiveCreditReporting)
    ShowOverdueAgedSummary = .GetBoolean("ShowOverdueAgedSummary", ShowOverdueAgedSummary)
    ShowOverdueAgedSummaryFull = .GetBoolean("ShowOverdueAgedSummaryFull", ShowOverdueAgedSummaryFull)
    ShowOverdueTransactions = .GetBoolean("ShowOverdueTransactions", ShowOverdueTransactions)
  End With

  ' Handle Events
  Select Case eventId
    Case ""
      ' Main event, i.e., return main page content
      If ScriptInfo.FormRecordMode = iseFormRecordMode.NoRecord Then
        ' Not applicable
      Else
        Main = CreatePage(text)
      End If
  End Select

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim AccountOverdues As finAccountOverdues
  Dim AccountOverduesC As finAccountOverdues
  Dim Ok As Boolean
  Dim Overdue As Decimal
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim strTemp As String
  Dim SummaryTables As ISSummaryTables
  Dim WorkflowIncompleteCount As Integer

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin(String.Format("{0}: {1}", mAccount.AccountId, mAccount.Name), isefinSummaryPageStyle.Blocks)
    .HtmlHeaderEnd()
  End With

  ' Begin Section
  sb.SectionColumnsBegin()

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

  ' Summary (Financial Status)
  sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(ScriptInfo, mAccount, AccountOverdues, AccountOverduesC, LastPaymentDaysMaximum))
  sb.BlockEnd()

  ' Comprehensive Credit Reporting
  If finBL.Settings.AddOns.CreditReporting AndAlso ShowComprehensiveCreditReporting AndAlso mAccount.CreditReports.Count <> 0 Then
    sb.BlockBegin("Comprehensive Credit Reporting")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_ComprehensiveCreditReporting(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' History Rating
  If 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

  ' Credit Limits
  If mAccount.AccountType.CreditLimitCount > 0 AndAlso mAccount.Status = isefinAccountStatus.Open Then
    sb.BlockBegin("Credit Limits")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_CreditLimits(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded), "", "")
    sb.BlockEnd()
  End If

  ' Workflows
  If finBL.Settings.AddOns.Workflow 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

  ' Certificates
  If finBL.CurrentCountry.SupportsTaxCertificates AndAlso mAccount.SupportsWithholdingTax Then
    sb.BlockBegin("Tax Certificates")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TaxCertificates(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Overdues Block (AccountsOverdues objects already loaded in 'Summary' block)
  If mAccount.SupportsOverdue AndAlso mAccount.AccountType.ShowOverdue AndAlso mAccount.Status = isefinAccountStatus.Open Then
    ' Note, AccountOverdues is created back in finBL.SummaryPageStandardBlocks.Account_FinancialStatus
    If AccountOverdues IsNot Nothing Then
      Overdue = AccountOverdues.TotalOverdue - AccountOverdues.TotalOverdueToday

      sb.BlockBegin("Overdues", String.Format("(as at {0})", finBL.FormatDateLong(AccountOverdues.DateAsAtResolved)), If(Overdue > 0, isefinSummaryPageBlockHeadingStyle.Warning, isefinSummaryPageBlockHeadingStyle.Normal))

      If AccountOverdues.TotalOverdue = 0 Then
        sb.Append("{{info|Account is not overdue.}}")
      ElseIf AccountOverdues.TotalOverdue < 0 Then
        sb.Append("{{info|Account is prepaid.}}")
      ElseIf Not (ShowOverdueTransactions OrElse ShowOverdueAgedSummary) Then
        sb.Append("{{warning|Account is overdue.}}")
      Else
        ' Aged Breakdown
        If ShowOverdueAgedSummary AndAlso AccountOverdues.TotalOverdue > 0 Then
          SummaryTables = finBL.SummaryPageStandardBlocks.Account_OverdueAgedBreakdown(ScriptInfo, AccountOverdues, ShowOverdueAgedSummaryFull)
          If SummaryTables.Count <> 0 Then sb.SummaryTableAppend(SummaryTables.ItemByIndex(0), "", "", 0, "", "left")
        End If

        ' Transactions
        If ShowOverdueTransactions Then
          SummaryTables = finBL.SummaryPageStandardBlocks.Account_OverdueTransactions(ScriptInfo, AccountOverdues)
          If SummaryTables.Count <> 0 Then sb.SummaryTableAppend(SummaryTables.ItemByIndex(0), "", "", 0, "", "left")
        End If
      End If

      sb.BlockEnd()
    End If
  End If

  ' Contractual Overdues Block
  If mAccount.SupportsOverdue AndAlso mAccount.AccountType.ShowContractualOverdue AndAlso mAccount.Status = isefinAccountStatus.Open Then
    ' Note, AccountOverdues is created back in finBL.SummaryPageStandardBlocks.Account_FinancialStatus
    If AccountOverduesC IsNot Nothing Then
      Overdue = AccountOverduesC.TotalOverdue - AccountOverduesC.TotalOverdueToday

      sb.BlockBegin("Contractual Overdues", String.Format("(as at {0})", finBL.FormatDateLong(AccountOverduesC.DateAsAtResolved)), If(Overdue > 0, isefinSummaryPageBlockHeadingStyle.Warning, isefinSummaryPageBlockHeadingStyle.Normal))

      If AccountOverduesC.TotalOverdue = 0 Then
        sb.Append("{{info|Account is not contractually overdue.}}")
      ElseIf AccountOverduesC.TotalOverdue < 0 Then
        sb.Append("{{info|Account is contractually prepaid.}}")
      ElseIf Not (ShowOverdueTransactions OrElse ShowOverdueAgedSummary) Then
        sb.Append("{{warning|Account is contractually overdue.}}")
      Else
        ' Aged Breakdown
        If ShowOverdueAgedSummary AndAlso AccountOverduesC.TotalOverdue > 0 Then
          SummaryTables = finBL.SummaryPageStandardBlocks.Account_OverdueAgedBreakdown(ScriptInfo, AccountOverduesC, ShowOverdueAgedSummaryFull)
          If SummaryTables.Count <> 0 Then sb.SummaryTableAppend(SummaryTables.ItemByIndex(0), "", "", 0, "", "left")
        End If

        ' Transactions
        If ShowOverdueTransactions Then
          SummaryTables = finBL.SummaryPageStandardBlocks.Account_OverdueTransactions(ScriptInfo, AccountOverduesC)
          If SummaryTables.Count <> 0 Then sb.SummaryTableAppend(SummaryTables.ItemByIndex(0), "", "", 0, "", "left")
        End If
      End If

      sb.BlockEnd()
    End If
  End If

  ' End Column 1
  sb.ColumnEnd()

  ' Begin Column 2
  sb.ColumnBegin("40%")

  ' Monitoring
  If finBL.Settings.MonitorCategoriesUsed 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, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Credit Bureau Default
  If finBL.Settings.AddOns.CreditEnquiryDefaults AndAlso mAccount.SupportsOverdue AndAlso Not mAccount.IsQuoteOrClosedQuote Then
    sb.BlockBegin("Credit Bureau Default", "", If(mAccount.HistoryItems.HasCurrentCreditBureauDefault(), isefinSummaryPageBlockHeadingStyle.Warning, isefinSummaryPageBlockHeadingStyle.Normal))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_CreditBureauDefault(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Suspension
  If mAccount.Status = isefinAccountStatus.Open Then
    sb.BlockBegin("Suspension", "", If(mAccount.IsSuspended, isefinSummaryPageBlockHeadingStyle.Warning, isefinSummaryPageBlockHeadingStyle.Normal))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Suspension(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Statements
  If mAccount.AccountType.StatementUse AndAlso Not mAccount.IsQuoteOrClosedQuote Then
    sb.BlockBegin("Statements")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Statements(ScriptInfo, mAccount, Nothing, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' End Column 2
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function