' SummaryPage2_AccountLog_StatementSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Log Summary Page
'
' Version: 2.01 (26/04/2016)
'
' Usage: Account Log form
' #################################################################

' Properties
Private mAccountLog As finAccountLog

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
  mAccountLog = DirectCast(source, finAccountLog)

  ' Get Constants
  With ScriptInfo.Constants
  End With

  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 Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  ' Begin Section
  sb.SectionColumnsBegin()

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

  ' Summary
  sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
  sb.SummaryTablesAppend(AccountLog_StatementSummary(ScriptInfo, mAccountLog))
  sb.BlockEnd()

  ' User Data
  If mAccountLog.StatementUserData.HasValues() Then
    sb.BlockBegin("User Data")
    sb.SummaryTablesAppend(finBL.Utilities.KeyValueListToSummaryTables(mAccountLog.StatementUserData))
    sb.BlockEnd()
  End If

  ' End Column 1
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function

Private Function AccountLog_StatementSummary(callerScriptInfo As ISScriptExecutionInfo,
                                             accountLog As finAccountLog) As ISSummaryTables

  Dim Account As finAccount
  Dim SummaryTableMain As ISSummaryTable
  Dim SummaryTables As ISSummaryTables
  Dim strTemp As String
  Dim Warnings As String

  ' Initialise
  If callerScriptInfo Is Nothing Then callerScriptInfo = finBL.CreateScriptExecutionInfo()
  SummaryTableMain = finBL.CreateSummaryTable()
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Account
  Account = finBL.CreateAccount()

  ' Main
  With SummaryTableMain
    ' Initialise
    .TableClass = iseSummaryTableClass.Information
    .Caption = ""

    With .Columns
      .AddText(15).NoBreakChars = 15
      .AddCurrency(15)
      .AddText()
    End With

    With .Rows
      .AddCaptionDate("Statement From: ", accountLog.StatementDateFrom)
      .AddCaptionDate("Statement To: ", accountLog.StatementDateTo)

      ' Balance
      Select Case Account.AccountClass
      Case isefinAccountClass.Deposit
        If accountLog.StatementBalance < 0 Then
          .AddCaptionText("Balance as at Statement:", finBL.FormatCurrency(-accountLog.StatementBalance, True), iseSummaryTableCellClass.Highlight1)
        Else
          With .AddCaptionText("Balance as at Statement:", finBL.FormatCurrency(-accountLog.StatementBalance, True), iseSummaryTableCellClass.Highlight1)
            .Cells.AddWiki(String.Format("DEBIT Balance {0}", accountLog.StatementDateTo))
          End With
        End If

      Case Else
        If accountLog.StatementBalance < 0 Then
          With .AddCaptionWiki("Balance as at Statement:", finBL.FormatCurrency(accountLog.StatementBalance, True), iseSummaryTableCellClass.Highlight1)
            .Cells.AddWiki(String.Format("CREDIT Balance {0}", finBL.FormatDateLong(accountLog.StatementDateTo)))
          End With
        Else
          .AddCaptionWiki("Balance as at Statement:", finBL.FormatCurrency(accountLog.StatementBalance, True), iseSummaryTableCellClass.Highlight1)
        End If
      End Select

      ' Overdue
      If Account.SupportsOverdue Then
        If accountLog.StatementOverdue < 0 Then
          ' Green/Positive
          With .AddCaptionWiki("Overdue as at Statement:", String.Format("++{0}++", finBL.FormatCurrency(accountLog.StatementOverdue, True)), iseSummaryTableCellClass.Highlight2)
            .RowClass = iseSummaryTableRowClass.Alt2
            .Cells.AddWiki(String.Format("--Prepaid as at **{0}**--", finBL.FormatDateLong(accountLog.StatementDateTo)))
          End With
        ElseIf accountLog.StatementOverdue > 0  Then
          ' Red/Warning
          With .AddCaptionWiki("Overdue as at Statement:", String.Format("!!{0}!!", finBL.FormatCurrency(accountLog.StatementOverdue, True)), iseSummaryTableCellClass.Highlight2)
            .RowClass = iseSummaryTableRowClass.Alt2
          End With
        End If
      End If

      ' Next Payment
      If accountLog.StatementPaymentNextDate <> Nothing Then
        With .AddCaptionCurrency("Next Payment:", accountLog.StatementPaymentNextValue, True)
          If accountLog.StatementPaymentNextDirectDebit Then
            .Cells.AddWiki(String.Format("--due-- **{0}** --(To be Direct Debited)--", finBL.FormatDateLong(accountLog.StatementPaymentNextDate)))
          Else
            .Cells.AddWiki(String.Format("--due-- **{0}**", finBL.FormatDateLong(accountLog.StatementPaymentNextDate)))
          End If
        End With
      End If

      ' Minimum Payment
      If accountLog.StatementMinimumPaymentDate <> Nothing Then
        With .AddCaptionCurrency("Minimum Payment:", accountLog.StatementMinimumPaymentValue, True)
          .Cells.AddWiki(finBL.FormatDateLong(accountLog.StatementMinimumPaymentDate))
        End With
        If accountLog.StatementMinimumPaymentDirectDebitValue <> 0 Then
          With .AddCaptionCurrency("Direct Debit:", accountLog.StatementMinimumPaymentDirectDebitValue, True)
            .Cells.AddWiki("--on-- " & finBL.FormatDateLong(accountLog.StatementMinimumPaymentDate))
          End With
        End If
      End If
    End With

    ' Document/ Publish
    Select Case accountLog.PublishStatus
      Case isefinLogPublishStatus.Failed
        If accountLog.PublishFailDate = Nothing Then
          strTemp = ""
        Else
          strTemp = String.Format(" on {0}", finBL.FormatDate(accountLog.PublishFailDate, True))
        End If
        If Len(accountLog.PublishResult) <> 0 Then
          strTemp &= String.Format(" ({0})", accountLog.PublishResult)
        End If
        Warnings &= "{{warning|Publishing of this Statement 'Failed' " & strTemp & ".}}"

      Case isefinLogPublishStatus.NotIssued
        Warnings &= "{{info|Statement not Issued on " & finBL.FormatDateTimeLong(accountLog.PublishedDate, False, True, True) & ".}}"

      Case isefinLogPublishStatus.Open
        Warnings &= "{{info|Publish status is 'Open'.}}"

      Case isefinLogPublishStatus.Published
        Warnings &= "{{info|Published on " & finBL.FormatDateTimeLong(accountLog.PublishedDate, False, True, True) & ".}}"

      Case isefinLogPublishStatus.ToSend
        If accountLog.PublishOnDate = Nothing Then
          Warnings &= " {{info|Publish status is 'To Send'.}}"
        ElseIf accountLog.PublishOnDate.Date < finBL.TimeZoneFunctions.GetCurrentLocalDateTime() Then
          Warnings &= "{{warning|" & String.Format("Publish Status is 'To Send' on {0}. Publish date has passed.", finBL.FormatDateTimeLong(accountLog.PublishOnDate, False, True, True)) & "}}"
        Else
          Warnings &= "{{info|" & String.Format("Publish Status is 'To Send' on {0}.", finBL.FormatDateTimeLong(accountLog.PublishOnDate, False, True, True)) & "}}"
        End If
    End Select

    ' Final Statement
    If accountLog.StatementFinal Then
      Warnings &= "{{info|" & String.Format("This is the Final Statement for Account '{0}'.", accountLog.AccountId) & "}}"
    End If

    ' Reversed
    If accountLog.Source = isefinLogSource.AccountStatementReversed Then
      Warnings &= "{{warning|This Statement has been 'Reversed'.}}"
    End If

    ' Warnings
    .TextAfter = Warnings
  End With

  ' Add Summary Tables
  SummaryTables.Add("Main", SummaryTableMain)

  Return SummaryTables

End Function