' SummaryPage2_TaskManagerTasks_TaskSummary
Option Explicit On
Option Strict On

' #################################################################
' Task Summary
' 
' Version: 2.04 (18/01/2024)
' 
' Usage: Task Manager when viewing Tasks
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.02  14/05/2021 
'    2.03  14/12/2021  ISS      J027922   Mailbox Items may need refreshing
'    2.04  18/01/2024  ISS      J032745   Client DateReview added
' #################################################################

' Properties
Private mIndex As Integer
Private mTasks As finTasks

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
  mTasks = DirectCast(source, finTasks)

  ' Get Event Args
  With eventArgs
    mIndex = .GetInteger("Index")
  End With

  ' Get Constants
  With ScriptInfo.Constants
  End With

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

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim Success As Boolean

  ' Assume Success
  Success = True

  Select Case mTasks(mIndex).TaskType
    Case isefinTaskType.AccountLog, isefinTaskType.AccountAppLog, isefinTaskType.ClientLog, isefinTaskType.SecurityStmtItemLog, isefinTaskType.SecurityStmtLog, isefinTaskType.UserLog
      Me.CreatePage_LogSummary(html)

    Case isefinTaskType.MailboxItemActionDate
      Me.CreatePage_MailboxItems(html)

    Case Else
      Me.CreatePage_TasksSummary(html)
  End Select

  Return Success

End Function

Private Function CreatePage_MailboxItems(ByRef html As String) As Boolean

  Dim i As Integer
  Dim Index As Integer
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Find Index of this Mailbox Item in the corresponding Task Manager collection
  Index = -1
  For i = 0 To mTasks.Parent.MailboxItems.Count - 1
    If mTasks.Parent.MailboxItems(i).Pk = mTasks(mIndex).ParentPk Then
      Index = i
      Exit For
    End If
  Next

  ' Refresh the Collection and try again?
  If Index = -1 Then
    mTasks.Parent.MailboxItemsRefresh
    For i = 0 To mTasks.Parent.MailboxItems.Count - 1
      If mTasks.Parent.MailboxItems(i).Pk = mTasks(mIndex).ParentPk Then
        Index = i
        Exit For
      End If
    Next
  End If

  ' Use Built-In Script
  html = finBL.ScriptFunctions.BuiltInTaskManagerMailboxItemSummaryScript2(mTasks.Parent.MailboxItems, Index, "", Nothing, False, Nothing, ScriptInfo.ContextData.ToKeyValueList(), ScriptInfo.Properties)

  Return Success
  
End Function

Private Function CreatePage_LogSummary(ByRef html As String) As Boolean

  Dim i As Integer
  Dim Index As Integer
  Dim Logs As finLogs
  Dim ParentLogPk As Integer
  Dim Success As Boolean
  Dim Task As finTask

  ' Assume Success
  Success = True

  ' Initialise
  Logs = finBL.CreateLogs()
  Task = mTasks(mIndex)
  If Task.ParentLogPk = 0 Then
    ParentLogPk = Task.Pk
  Else
    ParentLogPk = Task.ParentLogPk
  End If

  ' Load
  Select Case Task.TaskType
    Case isefinTaskType.AccountLog
      Logs.LoadAccountThread(Task.Code, ParentLogPk)

    Case isefinTaskType.AccountAppLog
      Logs.LoadAccountAppThread(Task.Code, ParentLogPk)

    Case isefinTaskType.ClientLog
      Logs.LoadClientThread(Task.Code, ParentLogPk)

    Case isefinTaskType.SecurityStmtItemLog
      Logs.LoadSecurityStmtItemThread(Task.SecurityStmtItemPk, ParentLogPk)

    Case isefinTaskType.SecurityStmtLog
      Logs.LoadSecurityStmtThread(Task.Code, ParentLogPk)

    Case isefinTaskType.UserLog
      Logs.LoadUserThread(Task.Code, ParentLogPk)
  End Select

  ' Index
  Index = -1
  For i = 0 To Logs.Count -1
    If Logs(i).Pk = Task.Pk Then
      index = i
      Exit For
    End If
  Next

  ' Use Built-In Generic Logs Script
  html = finBL.SummaryPageFunctions.LogSummary(False, Logs, i, Task.ParentLogPk <> 0, ScriptInfo.ContextData.ToKeyValueList())

  Return Success

End Function

Private Function CreatePage_TasksSummary(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()

  ' Begin Column 1
  sb.ColumnBegin()

  ' Summary
  sb.BlockBegin("", "", isefinSummaryPageBlockHeadingStyle.Normal, False)
  sb.SummaryTablesAppend(Tasks_Summary(mTasks(mIndex)))
  sb.BlockEnd()

  ' End Column 1
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Success

End Function

Private Function Tasks_Summary(task As finTask) As ISSummaryTables

  Dim SummaryTables As ISSummaryTables
  Dim SummaryTable As ISSummaryTable
  Dim Account As finAccount
  Dim HoldNotes As Boolean
  Dim Warnings As String

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = finBL.CreateSummaryTable()
  With SummaryTable
    .TableClass = iseSummaryTableClass.Information
    .Columns.AddText(12).NoBreakChars = 20
    .Columns.AddText()

    ' Rows
    With .Rows
      Select Case task.TaskType
        Case isefinTaskType.AccountAppReviewDate
          ' Account Application Review Date
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionCodeDescription("Application:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=AccountApps&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.AccountClosedPendingReviewDate
          ' Account Closed Pending
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.AccountPendingWithdrawalReviewDate
          ' Account Pending Withdrawal Review Date
          .AddCaptionText("Task:", "Account Credit Limit Pending Withdrawal Review")
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          If Len(task.Subject) <> 0 Then .AddCaptionText("Reference:", task.Subject)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.AccountQuoteOutstanding
          ' Account Quote Outstanding
          Account = finBL.CreateAccount()
          Account.Load(task.Code)
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          .AddCaptionCodeDescription("Type:", Account.AccountTypeId, Account.AccountType.Description)
          .AddCaptionDateTime("Quoted:", task.LogDate)
          If Len(task.Notes) <> 0 AndAlso Instr(task.Notes, vbNewLine) = 0 Then
            HoldNotes = True
            .AddCaptionText("Notes:", task.Notes)
          End If

        Case isefinTaskType.AccountReadyToOpen
          ' Account Ready to Open
          Account = finBL.CreateAccount()
          Account.Load(task.Code)
          .AddCaptionText("Task:", "Quote Ready to Open")
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          .AddCaptionCodeDescription("Type:", Account.AccountTypeId, Account.AccountType.Description)
          .AddCaptionDateTime("Quoted:", task.LogDate)
          .AddCaptionDateTime("Ready On:", task.ActionDate)
          If Len(task.Notes) <> 0 AndAlso Instr(task.Notes, vbNewLine) = 0 Then
            HoldNotes = True
            .AddCaptionText("Notes:", task.Notes)
          End If

        Case isefinTaskType.AccountReviewDate
          ' Account Review Date
          .AddCaptionText("Task:",task.TaskTypeText)
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.AccountSuspendReviewDate
          ' Account Suspension Review Date
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionCodeDescription("Account:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Accounts&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.ClientReviewDate
          ' Client Review Date
          .AddCaptionText("Task:",task.TaskTypeText)
          .AddCaptionCodeDescription("Client:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Client&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)
          
        Case isefinTaskType.PendingPPSRG2BTransaction
          ' Pending PPSR G2B Transaction
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionText("Request Type:", task.Subject)
          If task.SecurityStmtItemPk = 0 Then
            .AddCaptionCodeDescription("Security Stmt:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=SecurityStmts&id={0}&page=PPSR Transactions", task.Code)
          Else
            .AddCaptionCodeDescription("Security Item:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=SecurityStmts&id={0}&itemid={1}&itempage=PPSR", task.Code, task.SecurityStmtItemPk)
          End If

        Case isefinTaskType.SecurityStmtReviewDate
          ' Security Stmt Review Date
          .AddCaptionText("Task:", "Security Statement Review")
          .AddCaptionCodeDescription("Security Stmt:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=SecurityStmts&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case isefinTaskType.WorkflowReviewDate
          ' Workflow Review Date
          .AddCaptionText("Task:", task.TaskTypeText)
          .AddCaptionCodeDescription("Workflow:", task.Code, task.Name, iseSummaryTableCellClass.Highlight1).Cells(1).Hyperlink = String.Format("app://FormShow?form=Workflows&id={0}", task.Code)
          .AddCaptionDateTime("Review on:", task.ActionDate)

        Case Else
          Warnings = "{{warning|Task Summary Script does not handle Tasks of type '" & task.TaskTypetext & "'.}}"
      End Select
    End With

    ' Actions
    With .Actions
      ' Accept/ Decline Account
      If Not finBL.Database.IsReadOnly AndAlso finBL.Users(finBL.CurrentUser.UserId).IsAccountAuthoriser Then
        Select Case task.TaskType
          Case isefinTaskType.AccountQuoteOutstanding, isefinTaskType.AccountReadyToOpen
            .AddFormShow("AccountAcceptDecline", "Accept/Decline Account", "AccountAcceptDecline",,"id=" & finBL.UrlEncode(task.Code))
        End Select
      End If
    End With

    ' Warnings
    .TextAfter = Warnings
  End With
  SummaryTables.Add("Summary", SummaryTable)

  ' Notes
  If Not HoldNotes AndAlso Len(task.Notes) <> 0 Then
    SummaryTable = finBL.CreateSummaryTable()
    With SummaryTable
      .Caption = "Notes"
      .TableClass = iseSummaryTableClass.Information

      ' Rows
      With .Rows
        .AddText(task.Notes)
      End With
    End With
    SummaryTables.Add("Notes", SummaryTable)
  End If

  Return SummaryTables

End Function