' SummaryPage2_AccountAccountingLedgers_AccountingLedgersSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Accounting Ledger Summary
'
' Version: 2.01 (5/05/2016)
'
' Usage: Accounts form, Accounting Ledgers page
' #################################################################

'#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

' Properties
Private mAccount As finAccount
Private mAccountingLedger As finAccountAccountingLedger

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

  Dim Index As Integer

  ' Assume Success
  Main = True

  ' Initialise
  mAccount = DirectCast(source, finAccount)
  Index = eventArgs.GetInteger("Index")
  mAccountingLedger = mAccount.AccountingLedgers(Index)

  ' Get Constants
  With ScriptInfo.Constants
    ShowJournalsBlock = .GetBoolean("ShowJournalsBlock", ShowJournalsBlock)
    ShowGeneralLedgerBlock = .GetBoolean("ShowGeneralLedgerBlock", ShowGeneralLedgerBlock)
    ShowUserDataBlock = .GetBoolean("ShowUserDataBlock", ShowUserDataBlock)
  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

  If mAccountingLedger Is Nothing Then
    ' No Accounting Ledger
  Else
    ' Begin Section
    sb.SectionColumnsBegin()

    If (ShowGeneralLedgerBlock OrElse ShowJournalsBlock) Then
      ' Begin Column 1
      sb.ColumnBegin("50%")

      ' Summary
      sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_AccountingLedgers_Summary(ScriptInfo, mAccount.AccountingLedgers))
      sb.BlockEnd()

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

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

      ' End Column 1
      sb.ColumnEnd()

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

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

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

      ' End Column 2
      sb.ColumnEnd()
    Else
      ' Begin Column 1
      sb.ColumnBegin("50%")

      ' Summary
      sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_AccountingLedgers_Summary(ScriptInfo, mAccount.AccountingLedgers))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()

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

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

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

      ' End Column 2
      sb.ColumnEnd()
    End If

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function