' SummaryPage2_AccountWithdrawal_WithdrawalSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Withdrawal Summary page

' Version: 2.02 (28/09/2017)

' Usage: Account Withdrawal wizard and Account Refinance wizard
' #################################################################

' Objects
Private mAccount As finAccount
Private mAccountWithdrawal As finAccountWithdrawal
Private mWithdrawalCaption As String

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
  mAccountWithdrawal = DirectCast(source, finAccountWithdrawal)
  mAccount = mAccountWithdrawal.Account

  Select Case eventId
    Case ""
      ' Main event, i.e., return main page content
      Select Case ScriptInfo.FormPageKey
        Case "", "Refund", "Withdrawal", "Refinance"
          ' NOTE: "Refinance" used from Refinance wizard where refinancing into an existing Account

          ' Set Withdrawal Caption
          If len(ScriptInfo.FormPageKey) = 0 Then
            mWithdrawalCaption = "Withdrawal"
          Else
            mWithdrawalCaption = ScriptInfo.FormPageKey
          End If    

          ' Return Withdrawal page content
          Main = CreatePage(text)

        Case "PaymentArrangement"
          ' Set Withdrawal Caption
          If finBL.TransactionTypes.ExistsPk(mAccountWithdrawal.TransactionTypePk) Then
            mWithdrawalCaption = finBL.TransactionTypes.ItemByPk(mAccountWithdrawal.TransactionTypePk).ElementTypeText
          Else
            mWithdrawalCaption = ""
          End If

          ' Return Payment Arrangement page content
          Main = CreatePagePaymentArrangement(text)
      End Select
  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 mAccount.IsNew Then
    ' No Account loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please select an Account.}}")
  ElseIf mAccount.Status <> isefinAccountStatus.Open Then
    ' Invalid Account Status
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please select an 'Open' Account.}}")
  Else
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountWithdrawal_Summary(ScriptInfo, mAccountWithdrawal, mWithdrawalCaption))
    sb.BlockEnd()

    ' Credit Limits
    If mAccount.AccountType.CreditLimitCount > 0 Then
      sb.BlockBegin("Credit Limits")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_CreditLimits_ForTransaction(ScriptInfo, mAccount,maccountwithdrawal.WithdrawalDate, , , mWithdrawalCaption, mAccountWithdrawal.WithdrawalValueResolved, , mAccountWithdrawal.FeeValueResolved + mAccountWithdrawal.PaymentFeeValueResolved, False), "", "")
      sb.BlockEnd()
    End If

    ' End Column 1
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function

Private Function CreatePagePaymentArrangement(ByRef html As String) As Boolean

  Dim NewBalanceAdjustment As Decimal
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim strTemp As String

  ' Assume Success
  Ok = True

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

  ' Summary
  sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
  NewBalanceAdjustment = mAccountWithdrawal.WithdrawalValueResolved + mAccountWithdrawal.FeeValueResolved + mAccountWithdrawal.PaymentFeeValueResolved
  If mAccountWithdrawal.FeeValueResolved = 0 AndAlso mAccountWithdrawal.PaymentFeeValueResolved = 0 Then
    strTemp = String.Format("{0} **{1}**", mWithdrawalCaption, finBL.FormatCurrency(mAccountWithdrawal.WithdrawalValueResolved, True))
  Else
    strTemp = String.Format("{0} **{1}** plus fees of **{2}**", mWithdrawalCaption, finBL.FormatCurrency(mAccountWithdrawal.WithdrawalValueResolved, True), finBL.FormatCurrency(mAccountWithdrawal.FeeValueResolved + mAccountWithdrawal.PaymentFeeValueResolved, True))
  End If
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(ScriptInfo, mAccount, Nothing, Nothing, 120, False, False, False, NewBalanceAdjustment, strTemp, False))
  sb.BlockEnd()

  ' Payment Arrangement Options
  sb.BlockBegin("Payment Arrangement Options")
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountWithdrawal_PaymentArrangementOptions(ScriptInfo, mAccountWithdrawal, mWithdrawalCaption), , "")
  sb.BlockEnd()

  ' End Column 1
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function