' SummaryPage2_AccountSchedule_ScheduleSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Schedule Summary
'
' Version: 2.01 (26/04/2016)
'
' Usage: Account Schedule drilldown form
' #################################################################

'#const "AlwaysShowAuditInterestAccruedMethod",Boolean,False,"Always show the Interest Accrued Method in the Audit block?"
'#const "ShowScriptId",Boolean,True,"Show the Script Id if a script was involved in the calculation?"

' Constants
Private AlwaysShowAuditInterestAccruedMethod As Boolean = False
Private ShowScriptId As Boolean = True

' Objects
Private mAccount As finAccount
Private mAccountCalcSchedule As finAccountCalcSchedule
Private mIndex As Integer

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
  mAccountCalcSchedule = DirectCast(source, finAccountCalcSchedule)
  mAccount = DirectCast(eventArgs.GetObject("Account"), finAccount)
  mIndex = eventArgs.GetInteger("Index")

  ' Get Constants
  With ScriptInfo.Constants
    AlwaysShowAuditInterestAccruedMethod = .GetBoolean("AlwaysShowAuditInterestAccruedMethod", AlwaysShowAuditInterestAccruedMethod)
    ShowScriptId = .GetBoolean("ShowScriptId", ShowScriptId)
  End With

  ' Always show ScriptId if an Administrator
  If finBL.CurrentUser.IsAdministrator Then ShowScriptId = True

  ' 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 AccountCalcScheduleItem As finAccountCalcScheduleItem
  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

  If mIndex < 0 OrElse mIndex >= mAccountCalcSchedule.Count Then
    ' No Transaction
  Else
    ' Schedule Item
    AccountCalcScheduleItem = mAccountCalcSchedule(mIndex)

    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_ScheduleSummary(ScriptInfo, mAccount, mAccountCalcSchedule, mIndex))
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

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

    ' Allocations Block
    sb.BlockBegin("Allocations")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_ScheduleAllocations(ScriptInfo, mAccount, mAccountCalcSchedule, mIndex))
    sb.BlockEnd()

    ' End Column 2
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()

    ' Audit Block
    If Len(AccountCalcScheduleItem.AuditData) <> 0 Then
      ' Begin Audit Section
      sb.SectionColumnsBegin()
      sb.ColumnBegin("100%")

      ' Audit Block
      sb.BlockBegin("Audit")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionAuditSummary(ScriptInfo, mAccount, mAccountCalcSchedule.Item(mIndex).GetAuditDays, AlwaysShowAuditInterestAccruedMethod, ShowScriptId))
      sb.BlockEnd()

      ' End Audit Section
      sb.SectionColumnsEnd()
    End If
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function