'SummaryPage2_AccountInterestRateChange_Summary
Option Explicit On
Option Strict On

' #################################################################
' Displays summary on Account Interest Rate Change wizard
'
' Version: 2.00 (13/11/2018)
'
' Usage: Account Interest Rate Change and Account Floating
'        Interest Rate Review wizards.
' #################################################################

' Objects
Private mAccountInterestRateChange As I_finAccountInterestRateChange
Private Account As finAccount
Private AccountOverdues As finAccountOverdues
Private AccountOverduesC As finAccountOverdues
Private sb As ISHtmlSummaryPageStringBuilder

' Other
Private Balance As Decimal
Private Overdue As Decimal
Private OverdueToday As Decimal
Private OverdueContractual As Decimal
Private OverdueTodayContractual As Decimal

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
  mAccountInterestRateChange  = DirectCast(source, I_finAccountInterestRateChange)

  ' Get Event Args
  With eventArgs
  End With

  ' Get Constants
  With ScriptInfo.Constants
  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 sb As finHtmlSummaryPage2StringBuilder
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin("", isefinSummaryPageStyle.Blocks)
    .HtmlHeaderEnd()
  End With

  ' Begin Section
  sb.SectionColumnsBegin()

  Select Case ScriptInfo.FormKey 
    Case "AccountFloatingInterestRatesReview" 
      ' Begin Column 1
      sb.ColumnBegin()

      ' Summary
      sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Summary(Nothing, mAccountInterestRateChange.Account))
      sb.BlockEnd()

      sb.BlockBegin("Financial Status", "", isefinSummaryPageBlockHeadingStyle.Normal, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(Nothing, mAccountInterestRateChange.Account))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()

      ' Begin Column 2
      sb.ColumnBegin()

      sb.BlockBegin("Payment Options", "", isefinSummaryPageBlockHeadingStyle.Normal, False)
      sb.SummaryTablesAppend(AccountInterestRateChange_PaymentOptions(mAccountInterestRateChange))
      sb.BlockEnd()

      ' End Column 2
      sb.ColumnEnd()
      
    Case Else
      ' Begin Column 1
      sb.ColumnBegin()

      ' Summary
      sb.BlockBegin("Current Summary", "", isefinSummaryPageBlockHeadingStyle.Normal, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(Nothing, mAccountInterestRateChange.Account))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()

      ' Begin Column 2
      sb.ColumnBegin()

      sb.BlockBegin("Payment Options", "", isefinSummaryPageBlockHeadingStyle.Normal, False)
      sb.SummaryTablesAppend(AccountInterestRateChange_PaymentOptions(mAccountInterestRateChange))
      sb.BlockEnd()

      ' End Column 2
      sb.ColumnEnd()
  End Select

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Success

End Function

Private Function AccountInterestRateChange_PaymentOptions(accountInterestRateChange As I_finAccountInterestRateChange) As ISSummaryTables

  Dim Account As finAccount
  Dim AccountCalc As finAccountCalc
  Dim DateEffective As Date
  Dim DateCycle As finDateCycle
  Dim FinalPaymentPlusDays As String
  Dim MaturityDate As Date
  Dim MaturityDatePlusDays As String
  Dim PaymentFinalDate As Date
  Dim PaymentFinalRegular As Decimal
  Dim PaymentRegular As Decimal
  Dim NewMaturityDate As Date
  Dim NewPaymentFinalDate As Date
  Dim NewPaymentRegularFirstDate As Date
  Dim NewPaymentRegularFinal As Decimal
  Dim NewPaymentRegular As Decimal
  Dim SummaryTable As ISSummaryTable
  Dim SummaryTableRowClass As iseSummaryTableRowClass
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As String

  ' Initialise
  Account = accountInterestRateChange.Account
  SummaryTables = finBL.CreateSummaryTables()

  ' Get Effective Date
  accountInterestRateChange.GetInterestDetails(DateEffective, isefinAccountInterestType.FloatingRate, 0, 0, 0)

  ' Pre-Change
  AccountInterestRateChange.Account.Calculation.GetPaymentRegularFinal(Nothing, PaymentFinalRegular, False)
  PaymentRegular = AccountInterestRateChange.Account.Calculation.PaymentRegular
  PaymentFinalDate = AccountInterestRateChange.Account.Calculation.PaymentFinalDate
  MaturityDate = AccountInterestRateChange.Account.Calculation.DateMaturity

  SummaryTable = finBL.CreateSummaryTable()
  With SummaryTable
    .TableClass = iseSummaryTableClass.TabularData

    With .Columns
      .AddText(" ", 32)
      .AddText(" ", 3)
      .AddCurrency("First New").NoBreakChars = 12
      .AddCurrency("Regular")
      .AddCurrency("+Value")
      .AddCurrency("Final")
      .AddDate("Final Date")
      .AddText("Change").NoBreakChars = 12
      .AddDate("Maturity")
      .AddCurrency("Change").NoBreakChars = 12
    End With

    With .Rows
      With .Add.Cells
        .AddCaption("Pre-Change").CellClass = iseSummaryTableCellClass.NormalBold
        .AddText("")
        .AddText("")
        .AddCurrency(PaymentRegular, True)
        .AddText("")
        .AddCurrency(PaymentFinalRegular, True)
        .AddDate(PaymentFinalDate, True)
        .AddText("")
        .AddDate(MaturityDate, True)
        .AddText("")
      End With

      ' 1. No Action
      If accountInterestRateChange.GetAccountCalc(isefinAccountInterestRateChangeRegularPaymentAdjustmentType.None, AccountCalc) Then
        NewPaymentRegular = AccountInterestRateChange.NewPaymentRegular
        NewPaymentRegularFinal = AccountInterestRateChange.NewPaymentRegularFinal
        NewPaymentFinalDate = AccountCalc.PaymentFinalDate
        NewMaturityDate = AccountCalc.DateMaturity

        If PaymentFinalDate = Nothing OrElse NewPaymentFinalDate = Nothing Then
          FinalPaymentPlusDays = ""
        ElseIf PaymentFinalDate = NewPaymentFinalDate Then
          FinalPaymentPlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(PaymentFinalDate, NewPaymentFinalDate, AccountCalc.PaymentCycle)
          If PaymentFinalDate < NewPaymentFinalDate Then
            FinalPaymentPlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            FinalPaymentPlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If

        If MaturityDate = Nothing OrElse NewMaturityDate = Nothing Then
          MaturityDatePlusDays = ""
        ElseIf MaturityDate = NewMaturityDate Then
          MaturityDatePlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(MaturityDate, NewMaturityDate, AccountCalc.PaymentCycle)
          If MaturityDate < NewMaturityDate Then
            MaturityDatePlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            MaturityDatePlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If

        If AccountInterestRateChange.RegularPaymentAdjustmentType = isefinAccountInterestRateChangeRegularPaymentAdjustmentType.None Then
          SummaryTableRowClass = iseSummaryTableRowClass.Footer
        Else
          SummaryTableRowClass = iseSummaryTableRowClass.Normal
        End If

        With .Add(SummaryTableRowClass).Cells
          .AddCaption("1. No Action").CellClass = iseSummaryTableCellClass.NormalBold
          .AddWiki("{{icon|Schedule}}").HyperlinkSetFormAction("ShowSchedule","type=None") ' title='Show Schedule'
          .AddText("")
          .AddCurrency(NewPaymentRegular, True)
          .AddCurrency(NewPaymentRegular - PaymentRegular, True)
          .AddCurrency(NewPaymentRegularFinal, True)
          If NewPaymentFinalDate > NewMaturityDate Then
            With .AddDate(NewPaymentFinalDate, True)
              .CellClass = iseSummaryTableCellClass.Warning
              .Tooltip = "Past Maturity Date"
            End With
          Else
            .AddDate(NewPaymentFinalDate, True)
          End If
          .AddText(FinalPaymentPlusDays)
          .AddDate(NewMaturityDate, True)
          .AddText(MaturityDatePlusDays)
        End With
      Else
        With .Add.Cells
          .AddCaption("1. No Action").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Failed to calculate.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      End If

      ' 2. Hold Current Regular Payment
      If Account.Calculation.PaymentNextDate < Now.Date Then
        With .Add.Cells
          .AddCaption("2. Hold Regular Payment").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Next Payment Date is prior to today.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      ElseIf accountInterestRateChange.GetAccountCalc(isefinAccountInterestRateChangeRegularPaymentAdjustmentType.HoldRegularPayment, AccountCalc) Then

        NewPaymentRegular = AccountInterestRateChange.NewPaymentRegular
        NewPaymentRegularFinal = AccountInterestRateChange.NewPaymentRegularFinal
        NewPaymentFinalDate = AccountCalc.PaymentFinalDate
        NewMaturityDate = AccountInterestRateChange.NewMaturityDate

        If PaymentFinalDate = Nothing OrElse NewPaymentFinalDate = Nothing Then
          FinalPaymentPlusDays = ""
        ElseIf PaymentFinalDate = NewPaymentFinalDate Then
          FinalPaymentPlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(PaymentFinalDate, NewPaymentFinalDate, AccountCalc.PaymentCycle)
          If PaymentFinalDate < NewPaymentFinalDate Then
            FinalPaymentPlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            FinalPaymentPlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If
        If MaturityDate = Nothing OrElse NewMaturityDate = Nothing Then
          MaturityDatePlusDays = ""
        ElseIf MaturityDate = NewMaturityDate Then
          MaturityDatePlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(MaturityDate, NewMaturityDate, AccountCalc.PaymentCycle)
          If MaturityDate < NewMaturityDate Then
            MaturityDatePlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            MaturityDatePlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If

        If AccountInterestRateChange.RegularPaymentAdjustmentType = isefinAccountInterestRateChangeRegularPaymentAdjustmentType.HoldRegularPayment Then
          SummaryTableRowClass = iseSummaryTableRowClass.Footer
        Else
          SummaryTableRowClass = iseSummaryTableRowClass.Normal
        End If

        With .Add(SummaryTableRowClass).Cells
          With .AddCaption("2. Hold Current Payment")
            .CellClass = iseSummaryTableCellClass.NormalBold
          End With
          .AddText("{{icon|Schedule}}").HyperlinkSetFormAction("ShowSchedule","type=AdjustMaturityDate")
          .AddText("")
          .AddCurrency(NewPaymentRegular, True)
          .AddCurrency(NewPaymentRegular - PaymentRegular, True)
          .AddCurrency(NewPaymentRegularFinal, True)
          .AddDate(NewPaymentFinalDate, True)
          .AddText(FinalPaymentPlusDays)
          .AddDate(NewMaturityDate, True)
          .AddText(MaturityDatePlusDays)
        End With
      Else
        With .Add.Cells
          .AddCaption("2. Hold Current Payment").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Failed to calculate.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      End If

      ' 3. Adjust Regular Payment
      If Account.DateMaturity < DateEffective Then
        With .Add.Cells
          .AddCaption("3. Adjust Current Payment").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Maturity Date is prior to Effective Date.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      ElseIf Account.Calculation.PaymentNextDate < Now.Date Then
        With .Add.Cells
          .AddCaption("3. Adjust Current Payment").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Next Payment Date is prior to today.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      ElseIf accountInterestRateChange.GetAccountCalc(isefinAccountInterestRateChangeRegularPaymentAdjustmentType.AdjustRegularPayment, AccountCalc) Then
        NewPaymentRegular = AccountInterestRateChange.NewPaymentRegular
        NewPaymentRegularFinal = AccountInterestRateChange.NewPaymentRegularFinal
        NewPaymentRegularFirstDate = AccountInterestRateChange.NewPaymentRegularFirstDate
        NewPaymentFinalDate = AccountCalc.PaymentFinalDate
        NewMaturityDate = AccountInterestRateChange.NewMaturityDate

        If PaymentFinalDate = Nothing OrElse NewPaymentFinalDate = Nothing Then
          FinalPaymentPlusDays = ""
        ElseIf PaymentFinalDate = NewPaymentFinalDate Then
          FinalPaymentPlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(PaymentFinalDate, NewPaymentFinalDate, AccountCalc.PaymentCycle)
          If PaymentFinalDate < NewPaymentFinalDate Then
            FinalPaymentPlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            FinalPaymentPlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If
        If MaturityDate = Nothing OrElse NewMaturityDate = Nothing Then
          MaturityDatePlusDays = ""
        ElseIf MaturityDate = NewMaturityDate Then
          MaturityDatePlusDays = "No change"
        Else
          DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(MaturityDate, NewMaturityDate, AccountCalc.PaymentCycle)
          If MaturityDate < NewMaturityDate Then
            MaturityDatePlusDays = "+" & DateCycle.CycleMnemonicFriendly
          Else
            MaturityDatePlusDays = "-" & DateCycle.CycleMnemonicFriendly
          End If
        End If

        If accountInterestRateChange.RegularPaymentAdjustmentType = isefinAccountInterestRateChangeRegularPaymentAdjustmentType.AdjustRegularPayment Then
          SummaryTableRowClass = iseSummaryTableRowClass.Footer
        Else
          SummaryTableRowClass = iseSummaryTableRowClass.Normal
        End If

        With .Add(SummaryTableRowClass).Cells
          .AddCaption("3. Adjust Current Payment").CellClass = iseSummaryTableCellClass.NormalBold
          .AddText("{{icon|Schedule}}").HyperlinkSetFormAction("ShowSchedule","type=AdjustRegularPayment")
          .AddDate(NewPaymentRegularFirstDate)
          .AddCurrency(NewPaymentRegular, True)
          .AddCurrency(NewPaymentRegular - PaymentRegular, True)
          .AddCurrency(NewPaymentRegularFinal, True)
          .AddDate(NewPaymentFinalDate, True)
          .AddText(FinalPaymentPlusDays)
          .AddDate(NewMaturityDate, True)
          .AddText(MaturityDatePlusDays)
        End With
      Else
        With .Add.Cells
          .AddCaption("3. Adjust Current Payment").CellClass = iseSummaryTableCellClass.NormalBold
          With .AddWiki("{{warning|Failed to calculate.}}")
            .ColSpan = 8
            .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
          End With
        End With
      End If

      ' 4. Overriding Regular Payment
      If accountInterestRateChange.RegularPaymentNew > 0 Then
        If Account.DateMaturity < DateEffective Then
          With .Add.Cells
            .AddCaption("4. Override Regular Payment").CellClass = iseSummaryTableCellClass.NormalBold
            With .AddWiki("{{warning|Maturity Date is prior to Effective Date.}}")
              .ColSpan = 8
              .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
            End With
          End With
        ElseIf Account.Calculation.PaymentNextDate < Now.Date Then
          With .Add.Cells
            .AddCaption("4. Override Regular Payment").CellClass = iseSummaryTableCellClass.NormalBold
            With .AddWiki("{{warning|Next Payment Date is prior to today.}}")
              .ColSpan = 8
              .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
            End With
          End With
        ElseIf accountInterestRateChange.GetAccountCalc(isefinAccountInterestRateChangeRegularPaymentAdjustmentType.OverrideRegularPayment, AccountCalc) Then
          NewPaymentRegular = AccountInterestRateChange.NewPaymentRegular
          NewPaymentRegularFinal = AccountInterestRateChange.NewPaymentRegularFinal
          NewPaymentRegularFirstDate = AccountInterestRateChange.NewPaymentRegularFirstDate
          NewPaymentFinalDate = AccountInterestRateChange.NewMaturityDate
          NewMaturityDate = AccountInterestRateChange.NewMaturityDate

          If PaymentFinalDate = Nothing OrElse NewPaymentFinalDate = Nothing Then
            FinalPaymentPlusDays = ""
          ElseIf PaymentFinalDate = NewPaymentFinalDate Then
            FinalPaymentPlusDays = "No change"
          Else
            DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(PaymentFinalDate, NewPaymentFinalDate, AccountCalc.PaymentCycle)
            If PaymentFinalDate < NewPaymentFinalDate Then
              FinalPaymentPlusDays = "+" & DateCycle.CycleMnemonicFriendly
            Else
              FinalPaymentPlusDays = "-" & DateCycle.CycleMnemonicFriendly
            End If
          End If
          If MaturityDate = Nothing OrElse NewMaturityDate = Nothing Then
            MaturityDatePlusDays = ""
          ElseIf MaturityDate = NewMaturityDate Then
            MaturityDatePlusDays = "No change"
          Else
            DateCycle = finbl.Utilities.DateCycleGetSimplifiedTerm(MaturityDate, NewMaturityDate, AccountCalc.PaymentCycle)
            If MaturityDate < NewMaturityDate Then
              MaturityDatePlusDays = "+" & DateCycle.CycleMnemonicFriendly
            Else
              MaturityDatePlusDays = "-" & DateCycle.CycleMnemonicFriendly
            End If
          End If

          If accountInterestRateChange.RegularPaymentAdjustmentType = isefinAccountInterestRateChangeRegularPaymentAdjustmentType.OverrideRegularPayment Then
            SummaryTableRowClass = iseSummaryTableRowClass.Footer
          Else
            SummaryTableRowClass = iseSummaryTableRowClass.Normal
          End If

          With .Add(SummaryTableRowClass).Cells
            .AddCaption("4. Override Regular Payment").CellClass = iseSummaryTableCellClass.NormalBold
            .AddText("{{icon|Schedule}}").HyperlinkSetFormAction("ShowSchedule","type=OverrideRegularPayment")
            .AddDate(NewPaymentRegularFirstDate)
            .AddCurrency(NewPaymentRegular, True)
            .AddCurrency(NewPaymentRegular - PaymentRegular, True)
            .AddCurrency(NewPaymentRegularFinal, True)
            .AddDate(NewPaymentFinalDate, True)
            .AddText(FinalPaymentPlusDays)
            .AddDate(NewMaturityDate, True)
            .AddText(MaturityDatePlusDays)
          End With
        Else
          With .Add.Cells
            .AddCaption("4. Override Regular Payment").CellClass = iseSummaryTableCellClass.NormalBold
            With .AddWiki("{{warning|Failed to calculate.}}")
              .ColSpan = 8
              .Tooltip = finBL.HtmlEncode(finBL.Error.Message(True))
            End With
          End With
        End If
      End If
    End With

    ' Warnings
    .TextAfter = Warnings
  End With

  SummaryTables.Add("Main", SummaryTable)

  Return SummaryTables

End Function