' SummaryPage2_AccountingLedgersActivate_ItemSummary
 Option Explicit On
 Option Strict On

' #################################################################
' Activate Accounting Ledgers Summary

' Version: 2.00 (5/05/2016)

' Usage: Activate Accounting Ledgers form
' #################################################################

'#const "ShowJournalsBlock",Boolean,True,"Show the Journals for the Accounting Ledger?"
'#const "ShowGeneralLedgerBlock",Boolean,True,"Show the General Ledger details for the Accounting Ledger?"
'#const "ShowUserDataBlock",Boolean,True,"Show User Data for the Accounting Ledger if available?"

' Constants
Private ShowJournalsBlock As Boolean = True
Private ShowGeneralLedgerBlock As Boolean = True
Private ShowUserDataBlock As Boolean = True

' Other
Private mIndex As Integer

' Objects
Private mAccountAccountingLedgersActivate As finAccountAccountingLedgersActivate

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
  mAccountAccountingLedgersActivate = DirectCast(source, finAccountAccountingLedgersActivate)
  mIndex = eventArgs.GetInteger("Index")
  
  ' Get Constants
  With ScriptInfo.Constants
    ShowJournalsBlock = .GetBoolean("ShowJournalsBlock", ShowJournalsBlock)
    ShowGeneralLedgerBlock = .GetBoolean("ShowGeneralLedgerBlock", ShowGeneralLedgerBlock)
    ShowUserDataBlock = .GetBoolean("ShowUserDataBlock", ShowUserDataBlock)
  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 AccountAccountingLedger As finAccountAccountingLedger
  Dim Success As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder

  ' Assume Success
  Success = True
  
  ' Create Temporary Accounting Ledger so we can piggy-back existing Summary Page Blocks
  AccountAccountingLedger = mAccountAccountingLedgersActivate(mIndex).CreateAccountAccountingLedger()

  ' 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("50%")

  ' Summary
  sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_AccountingLedgers_SummaryItem(ScriptInfo, AccountAccountingLedger))
  sb.BlockEnd()

  ' User Defined
  If ShowUserDataBlock AndAlso AccountAccountingLedger.UserData.Count <> 0 Then
    sb.BlockBegin("User Defined")
    sb.SummaryTablesAppend(finBL.Utilities.KeyValueListToSummaryTables(AccountAccountingLedger.UserData, True))
    sb.BlockEnd()
  End If

  ' End Column 1
  sb.ColumnEnd()

  ' Begin Column 2
  sb.ColumnBegin("50%", "25em", "70em")

  ' General Ledger Block
  If ShowGeneralLedgerBlock Then
    sb.BlockBegin("General Ledger")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_AccountingLedgers_GeneralLedger(ScriptInfo, AccountAccountingLedger))
    sb.BlockEnd()
  End If

  ' Journals Block
  If ShowJournalsBlock Then
    sb.BlockBegin("Journals")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_AccountingLedgers_Journals(ScriptInfo, AccountAccountingLedger))
    sb.BlockEnd()
  End If

  ' End Column 2
  sb.ColumnEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Success

End Function