' SummaryPage2_Account_Financial
Option Explicit On
Option Strict On

' #################################################################
' Account Financial
'
' Version: 2.04 (04/11/2021)
'
' Usage: Accounts form, Financial page.
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.03  19/04/2020  ISS
'    2.04  04/11/2021  ISS      J027713   Updated obsoleted functions
' #################################################################

'#Const "ShowDatesBlock",Boolean,False,"Show Dates block? Note, will always show for a Quote."
'#Const "ShowScheduleOmitPayments",Boolean,False,"Show link to open Schedule omitting future payments."
'#Const "ShowScheduleRestructured",Boolean,True,"Show link to open Schedule including Restructured Transactions."

' Constants
Private ShowDatesBlock As Boolean = False
Private ShowScheduleOmitPayments As Boolean = False
Private ShowScheduleRestructured As Boolean = True

' Objects
Private mAccount As finAccount
Private mAccountCalc As finAccountCalc
Private mAccountCalcOpening As finAccountCalc
Private mAccountCalcRestructured As finAccountCalc
Private mAccountCalcVersionIndex As Integer
Private mAccountSummary As finAccountSummary

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)
  If mAccount.IsQuoteOrClosedQuote Then
    ' Quote status
    mAccountCalcVersionIndex = -1

    mAccountCalc = mAccount.Calculation
    mAccountCalcOpening = mAccount.Calculation
    mAccountCalcRestructured = mAccount.Calculation
    mAccountSummary = mAccountCalc.Summary
  Else
    ' Historic
    mAccountCalcVersionIndex = eventArgs.GetInteger("Index", -1)
    If mAccountCalcVersionIndex = -1 Then mAccountCalcVersionIndex = mAccount.CalculationVersions.Count - 1

    If Not mAccount.CalculationVersions.GetLatestCalculation(0, mAccountCalcOpening) Then
      Main = False
      finBL.Error.ErrorExtend("Failed to load 'Opening' Account Calculation.")
    End If

    If Main Then
      If Not mAccount.CalculationVersions.GetLatestCalculation(mAccountCalcVersionIndex, mAccountCalcRestructured) Then
        Main = False
        finBL.Error.ErrorExtend("Failed to load 'Restructured' Account Calculation.")
      End If
    End If

    If Main Then
      With mAccount.CalculationVersions(mAccountCalcVersionIndex)
        If .GetAccountCalc(mAccountCalc) Then
          mAccountSummary = .Summary
        Else
          Main = False
          finBL.Error.ErrorExtend("Failed to load Account Calculation.")
        End If
      End With
    End If
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ShowDatesBlock  = .GetBoolean("ShowDatesBlock", ShowDatesBlock)
    ShowScheduleOmitPayments = .GetBoolean("ShowScheduleOmitPayments", ShowScheduleOmitPayments)
    ShowScheduleRestructured = .GetBoolean("ShowScheduleRestructured", ShowScheduleRestructured)
  End With

  ' Handle Events
  If Main Then
    Select Case eventId
      Case ""
        ' Main event, i.e., return main page content
        Main = CreatePage(text)
    End Select
  End If

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim strTemp As String
  Dim SummaryPageBlockHeadingStyle As isefinSummaryPageBlockHeadingStyle

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin(String.Format("{0}: {1}", mAccount.AccountId, mAccount.Name), isefinSummaryPageStyle.Blocks)
    .HtmlHeaderEnd()
  End With

  If mAccount.Status = isefinAccountStatus.Quote AndAlso mAccount.Calculation.IsCalculationRequired Then
    ' Requires Calculation
    If mAccount.Calculation.HasOpeningTransactions Then
      sb.SectionColumnsAddQuickBlock("Summary", "{{info|Click 'Calculate' to display updated information.|True}}")
    Else
      sb.SectionColumnsAddQuickBlock("Summary", "{{info|Enter an Opening Transaction and then click 'Calculate'.|True}}")
    End If
  Else
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary (Financial Status)
    If mAccount.IsQuoteOrClosedQuote Then
      strTemp = ""
    ElseIf Len(mAccountCalc.Description) = 0 Then
      strTemp = ""
    Else
      strTemp = "(" & mAccountCalc.Description & ")"
    End If
    sb.BlockBegin("Summary", strTemp, isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialSummary(ScriptInfo, mAccount, mAccountCalc, mAccountSummary, mAccountCalc.IsCalcVersionFuture, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' Balance Allocations Block
    If mAccountCalc.IsCalcVersionFuture AndAlso (Not mAccountCalc.OptionInterestChargeUpfront) Then
      sb.BlockBegin("Balance Allocations")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BalanceAllocations(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' End Column 1
    sb.ColumnEnd()

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

    Select Case mAccount.AccountClass
      Case isefinAccountClass.Deposit
        ' Financial Details block
        sb.BlockBegin("Financial Details")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialDetails(ScriptInfo, mAccount, mAccountCalc, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()

      Case isefinAccountClass.Disbursement
        ' Financial Details block
        sb.BlockBegin("Financial Details")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialDetails(ScriptInfo, mAccount, mAccountCalc, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()

      Case isefinAccountClass.LoanFR, isefinAccountClass.LoanFT
        ' Term & Payments block
        sb.BlockBegin("Term & Payments")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_TermAndPayments(ScriptInfo, mAccount,  mAccountCalcVersionIndex, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()

        ' Remaining Payments block
        If mAccountCalc.IsCalcVersionFuture Then
          sb.BlockBegin("Remaining Payments")
          sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_RemainingPayments(ScriptInfo, mAccount, mAccountCalcOpening, mAccountCalc, True, ShowScheduleRestructured, ShowScheduleOmitPayments, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
          sb.BlockEnd()
        End If

      Case isefinAccountClass.LoanRC
        ' Financial Details block
        sb.BlockBegin("Financial Details")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialDetails(ScriptInfo, mAccount, mAccountCalc, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()

        ' Minimum Payments block
        sb.BlockBegin("Minimum Payments")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_MinimumPaymentsSummary2(ScriptInfo, mAccount, mAccountCalc, mAccountCalcVersionIndex, True, ShowScheduleOmitPayments,, mAccountCalc.IsCalcVersionFuture AndAlso ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()

        ' Statement block
        sb.BlockBegin("Statements")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Statements(ScriptInfo, mAccount, mAccountCalc, mAccountCalc.IsCalcVersionFuture AndAlso ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()
    End Select

    ' End Column 2
    sb.ColumnEnd()

    ' Begin Column 3
    sb.ColumnBegin("33%")

    ' Dates block
    Select Case scriptinfo.FormKey
      Case "Accounts"
        ' Force block if a Quote and record has been saved
        If Not ShowDatesBlock Then
          ShowDatesBlock = mAccountCalc.IsCalcVersionOpening AndAlso (Not mAccount.IsNew)
        End If
      Case Else
        ShowDatesBlock = False
    End Select
    If ShowDatesBlock Then
      sb.BlockBegin("Dates")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Dates(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' NZ High-Cost Consumer Credit Contract block
    Select Case mAccount.ConsumerRegulationType
      Case isefinAccountConsumerRegulationType.NZ_HighCostConsumerCreditContract
        sb.BlockBegin("High-Cost Consumer Credit Contract")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_ConsumerRegulation_NZHighCostConsumerCreditContract(ScriptInfo, mAccount, mAccountCalc,mAccountCalc.IsCalcVersionFuture AndAlso ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()
    End Select

    ' Interest block
    If mAccount.SupportsInterest Then
      sb.BlockBegin("Interest")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Interest2(ScriptInfo, mAccount, mAccountCalc,,,,,, mAccountCalc.IsCalcVersionFuture AndAlso ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_WithholdingTaxDetails2(ScriptInfo, mAccount,, mAccountCalc.IsCalcVersionFuture AndAlso ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' Comparison Rate block
    If mAccountCalc.IsCalcVersionOpening AndAlso mAccountCalc.ComparisonRateType <> isefinAccountCalcComparisonRateType.None Then
      ' Heading
      Select Case mAccountCalc.ComparisonRateType
        Case isefinAccountCalcComparisonRateType.NewZealandRateOfCharge
          strTemp = "Rate of Charge"
        Case Else
          strTemp = "Comparison Rate"
      End Select
      If mAccountCalc.ComparisonRate < 0 OrElse mAccountCalc.AccountType.ComparisonRateMaximum <> 0 AndAlso mAccountCalc.ComparisonRate > mAccount.AccountType.ComparisonRateMaximum Then
        SummaryPageBlockHeadingStyle = isefinSummaryPageBlockHeadingStyle.Warning
      Else
        SummaryPageBlockHeadingStyle = isefinSummaryPageBlockHeadingStyle.Normal
      End If

      sb.BlockBegin(strTemp,, SummaryPageBlockHeadingStyle)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_ComparisonRate(ScriptInfo, mAccount, mAccountCalc, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' End Column 3
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function