' SummaryPage2_Account_Payments
Option Explicit On
Option Strict On

' #################################################################
' Account Payment Details
' 
' (c) Copyright Intersoft Systems Limited 2024
' 
' Version: 2.07 (30/07/2024)
'
' Usage: Accounts form, Payments page, Details tab.
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.03  26/04/2016  ISS
'    2.04  04/11/2021  ISS      J027713   Updated obsoleted functions
'    2.05  31/08/2023  ISS      J031120   Added Zepto PayID
'    2.06  14/02/2024  ISS      J032155   Added Zepto PayTo
'    2.07  30/07/2024  ISS      J032652   Added Monoova PayTo
' #################################################################

'#Const "BankReferenceLength",Integer,0,"Length of Bank Reference. Will left pad with zeros if necessary, 0=no padding."
'#Const "DisplayBankMatches",Boolean,True,"Display Bank Matches and allow Users to delete them."
'#Const "LastPaymentDaysMaximum",Integer,120,"Maximum Days for warning since last Reversed Payment."
'#Const "ShowPaymentSchedule",Boolean,True,"Show Schedule of Future Payments."
'#Const "ShowPaymentScheduleOverduePaid",Boolean,True,"Show a Schedule of Future Payments given Overdue paid off today."

' Constants
Private BankReferenceLength As Integer = 0
Private DisplayBankMatches As Boolean = True
Private LastPaymentDaysMaximum As Integer = 120
Private ShowPaymentSchedule As Boolean = True
Private ShowPaymentScheduleOverduePaid As Boolean = True

' Objects
Private mAccount As finAccount

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)

  ' Get Constants
  With ScriptInfo.Constants
    BankReferenceLength = .GetInteger("BankReferenceLength", BankReferenceLength)
    DisplayBankMatches = .GetBoolean("DisplayBankMatches", DisplayBankMatches)
    LastPaymentDaysMaximum = .GetInteger("LastPaymentDaysMaximum", LastPaymentDaysMaximum)
    ShowPaymentSchedule = .GetBoolean("ShowPaymentSchedule", ShowPaymentSchedule)
    ShowPaymentScheduleOverduePaid = .GetBoolean("ShowPaymentScheduleOverduePaid", ShowPaymentScheduleOverduePaid)
  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 Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder

  ' 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

  ' Begin Section
  sb.SectionColumnsBegin()

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

  ' Summaries (one per available Banking Details)
  If mAccount.BankingDetails.ExistsBankingDetailsType(isefinAccountBankingDetailsType.Outgoing) Then
    If mAccount.AccountClass = isefinAccountClass.Deposit Then
      sb.BlockBegin("Interest Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    ElseIf mAccount.BankingDetails.Count = 1 Then
      sb.BlockBegin("Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    Else
      sb.BlockBegin("Outgoing Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    End If
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingDetailsSummary(ScriptInfo, mAccount, isefinAccountBankingDetailsType.Outgoing, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  If mAccount.BankingDetails.ExistsBankingDetailsType(isefinAccountBankingDetailsType.Incoming) Then
    If mAccount.BankingDetails.Count = 1 Then
      sb.BlockBegin("Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    Else
      sb.BlockBegin("Incoming Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    End If
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingDetailsSummary(ScriptInfo, mAccount, isefinAccountBankingDetailsType.Incoming, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  If mAccount.BankingDetails.ExistsBankingDetailsType(isefinAccountBankingDetailsType.Maturity) Then
    If mAccount.AccountClass = isefinAccountClass.Deposit AndAlso (Not mAccount.IsFixedTerm) Then
    ' Don't include "On Call" Deposits
    Else
      sb.BlockBegin("Maturity Payment Details", "", isefinSummaryPageBlockHeadingStyle.Main, False)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingDetailsSummary(ScriptInfo, mAccount, isefinAccountBankingDetailsType.Maturity, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If
  End If

  ' Payment Arrangements
  If mAccount.SupportsPaymentArrangements AndAlso Not maccount.IsQuoteOrClosedQuote Then
    sb.BlockBegin("Payment Arrangements")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentArrangementsSummary(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' End Column 1
  sb.ColumnEnd()

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

  ' Banking Reference Block
  If finBL.Settings.AccountCheckDigitMethod <> isefinCheckDigitMethod.NotUsed Then
    sb.BlockBegin(finBL.SupportedCountries.CurrentSupportedCountry.Terminology(isefinCountryTerminology.BankCustomerReference, False))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingReference(ScriptInfo, mAccount, BankReferenceLength, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' PayTo Block
  If finBL.Settings.AddOns.BankingAdvanced_Split OrElse finBL.Settings.AddOns.BankingAdvanced_Monoova Then
    If mAccount.BankingPayToItems.CanAddPayToAgreement OrElse (mAccount.BankingPayToItems.CountPending <> 0 OrElse mAccount.BankingPayToItems.CountActive <> 0) Then
      sb.BlockBegin("PayTo")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingPayTo(ScriptInfo, mAccount))
      sb.BlockEnd()
    End If
  End If

  ' PayID Block
  If finBL.Settings.AddOns.BankingAdvanced_Split Then
    If mAccount.BankingPayIDItems.CanAddPayID OrElse (mAccount.BankingPayIDItems.CountPending <> 0 OrElse mAccount.BankingPayIDItems.CountActive <> 0 OrElse mAccount.BankingPayIDItems.CountDisabled <> 0) Then
      sb.BlockBegin("PayID")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankingPayId(ScriptInfo, mAccount))
      sb.BlockEnd()
    End If
  End If

  ' Minimum Payments Block
  If mAccount.SupportsMinimumPayments AndAlso mAccount.Status = isefinAccountStatus.Open Then
    sb.BlockBegin("Minimum Payments")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_MinimumPaymentsSummary2(ScriptInfo, mAccount, Nothing, -1, False, False,, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Payment Statistics Block
  If mAccount.SupportsPayments AndAlso Not mAccount.IsQuoteOrClosedQuote Then
    sb.BlockBegin("Payment Statistics")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentStatistics(ScriptInfo, mAccount, LastPaymentDaysMaximum, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' Bank Matches Block
  If DisplayBankMatches AndAlso Not mAccount.IsQuoteOrClosedQuote Then
    sb.BlockBegin("Bank Matches")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_BankMatches(ScriptInfo, mAccount, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
  End If

  ' End Column 2
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Payment Schedule(s)
  If mAccount.SupportsPaymentArrangements AndAlso (ShowPaymentSchedule OrElse ShowPaymentScheduleOverduePaid) Then
    ' Begin Section
    sb.SectionColumnsBegin()

    If ShowPaymentSchedule AndAlso ShowPaymentScheduleOverduePaid Then
      ' Show Both Schedules
      ' Begin Column 1
      sb.ColumnBegin("50%")

      sb.BlockBegin("Payment Schedule")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentSchedule(ScriptInfo, mAccount, False, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()

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

      sb.BlockBegin("Payment Schedule", "(Overdues paid today)")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentSchedule(ScriptInfo, mAccount, True, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()

      ' End Column 2
      sb.ColumnEnd()
    ElseIf ShowPaymentSchedule Then
      ' Show Payment Schedule Only
      ' Begin Column 1
      sb.ColumnBegin("60%")

      sb.BlockBegin("Payment Schedule")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentSchedule(ScriptInfo, mAccount, False, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()
    Else
      ' Show Overdue Paid Schedule Only
      ' Begin Column 1
      sb.ColumnBegin("60%")

      sb.BlockBegin("Payment Schedule", "(Overdues paid today)")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_PaymentSchedule(ScriptInfo, mAccount, True, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()

      ' End Column 1
      sb.ColumnEnd()
    End If

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function