' SummaryPage2_AccountTransactions_TransactionSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Transaction Summary
' 
' (c) Copyright Intersoft Systems Limited 2024
' 
' Version: 2.02 (25/09/2024)
' 
' Usage: Accounts form, Transactions page and other Transaction wizards
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.01  26/04/2016  ISS
'    2.02  25/09/2024  ISS      J034036   Show Audit Data on Default Interest transactions
' #################################################################

'#const "AlwaysShowAuditInterestAccruedMethod",Boolean,False,"Always show the Interest Accrued Method in the Audit block?"
'#const "ShowAllocationsBlock",Boolean,True,"Show Transaction Allocations?"
'#const "ShowBatchBlock",Boolean,False,"Show the Batch details in a separate block?"
'#const "ShowDisbursementsBlock",Boolean,True,"Show Disbursement information?"
'#const "ShowScriptId",Boolean,True,"Show the Script Id if a script was involved in the calculation?"
'#const "ShowUserDetails",Boolean,True,"Show the User flag and notes if set?"

' Constants
Private AlwaysShowAuditInterestAccruedMethod As Boolean = False
Private ShowAllocationsBlock As Boolean = True
Private ShowBatchBlock As Boolean = False
Private ShowDisbursementsBlock As Boolean = True
Private ShowScriptId As Boolean = True
Private ShowUserDetails As Boolean = True

' Objects
Private mAccount As finAccount
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
  mAccount = DirectCast(source, finAccount)
  mIndex = eventArgs.GetInteger("Index")

  ' Get Constants
  With ScriptInfo.Constants
    AlwaysShowAuditInterestAccruedMethod = .GetBoolean("AlwaysShowAuditInterestAccruedMethod", AlwaysShowAuditInterestAccruedMethod)
    ShowAllocationsBlock = .GetBoolean("ShowAllocationsBlock", ShowAllocationsBlock)
    ShowBatchBlock = .GetBoolean("ShowBatchBlock", ShowBatchBlock)
    ShowDisbursementsBlock = .GetBoolean("ShowDisbursementsBlock", ShowDisbursementsBlock)
    ShowScriptId = .GetBoolean("ShowScriptId", ShowScriptId)
    ShowUserDetails = .GetBoolean("ShowUserDetails", ShowUserDetails)
  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 AccountCalcDays As finAccountCalcDays
  Dim AccountTransaction As finAccountTransaction
  Dim BatchView As finBatchView
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim ShowAccount As Boolean
  Dim ShowBatchInformation As Boolean

  ' 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 >= mAccount.Transactions.Count Then
    ' No Transaction
  Else
    ' Transaction
    AccountTransaction = mAccount.Transactions(mIndex)

    ' Show Batch Information?
    Select Case ScriptInfo.FormKey
      Case "AccountTransactionView_Current", "AccountTransactionView_Deposit", "AccountTransactionView_Loan"
        ' These forms already show Batch Information, so don't show again
        ShowBatchInformation = False
      Case Else
        ShowBatchInformation = True
    End Select

    ' Show Account in the Summary block?
    Select Case ScriptInfo.FormKey
      Case "AccountTransactionAdjustment", "AccountTransactionReverse"
        ShowAccount = True
      Case "AccountTransactionView_Current", "AccountTransactionView_Deposit", "AccountTransactionView_Loan"
        ShowAccount = True
      Case Else
        ShowAccount = False
    End Select

    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionSummary(ScriptInfo, mAccount, mIndex, ShowAccount, Not ShowBatchBlock AndAlso ShowBatchInformation, ShowUserDetails, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

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

    ' Disbursements Block
    If ShowDisbursementsBlock Then
      If AccountTransaction.ElementType = isefinElementType.Disbursement Then
        sb.BlockBegin("Disbursement")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionDisbursementSummary(ScriptInfo, mAccount, mIndex))
        sb.BlockEnd()
      End If
    End If

    ' Allocations Block
    If ShowAllocationsBlock Then
      If AccountTransaction.ReversePk = 0 OrElse AccountTransaction.ElementType = isefinElementType.PaymentReceived Then
        sb.BlockBegin("Allocations", String.Format("({0})", AccountTransaction.AllocationMethodValueText))
      Else
        sb.BlockBegin("Allocations")
      End If
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionAllocations(ScriptInfo, mAccount, mIndex))
      sb.BlockEnd()
    End If

    ' Batch Block
    If ShowBatchBlock AndAlso ShowBatchInformation Then
      If AccountTransaction.BatchHeaderPk = 0 Then
        BatchView = Nothing
      Else
        AccountTransaction.GetBatchView(BatchView)
      End If

      sb.BlockBegin("Batch")
      If BatchView Is Nothing Then
        sb.AppendLine("{{info|Transaction is not part of a Batch.|True}}")
      Else
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionBatchSummary(ScriptInfo, BatchView, AccountTransaction, ShowScriptId))
      End If
      sb.BlockEnd()
    End If

    ' End Column 2
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()

    ' Find Audit Data
    If Len(AccountTransaction.AuditData) = 0 Then
      ' Show on Default Interest
      Select Case AccountTransaction.ScheduleType
        Case isefinAccountCalcScheduleType.DefaultInterest
          If mIndex > 1 Then
            With mAccount.Transactions(mIndex - 1)
              If .ScheduleType = isefinAccountCalcScheduleType.Interest Then AccountCalcDays = .GetAuditDays 
            End With  
          End If  
      End Select
    Else
      AccountCalcDays = AccountTransaction.GetAuditDays
    End If
  
    ' Audit Block
    If AccountCalcDays IsNot Nothing Then
      ' Begin Audit Section
      sb.SectionColumnsBegin()
      sb.ColumnBegin("100%")

      ' Audit Block
      sb.BlockBegin("Audit")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TransactionAuditSummary(ScriptInfo, mAccount, AccountCalcDays, 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