' AccountApp_PageSet_finPOWER5

' =======================================================================================
' finPOWER 5 Loan Application Page Hander Script
'
' Add the following text to KB articles related to this script:
' Note: This summary page is not currently accessible for customisation.
'
' Version: 1.00
' ======================================================================================

' Page Set Handler, Reporting and User Interface Objects
Public mPsh As finPageSetHandler
Public mReports As ISfinReports
Public mUI As ISUserInterfaceBL

' Data Source and Account Application
Private mAccountApp As finAccountApp
Private mDataSource As finDataSourceAccountApp

Public Function Main(ByVal pageSetHandler As finPageSetHandler, ByVal userInterface As ISUserInterfaceBL, ByVal reports As ISfinReports, ByVal eventId As String, ByVal eventArgs As ISKeyValueList, ByRef handled As Boolean, ByRef returnValues As ISKeyValueList, ByVal webState As ISKeyValueList, ByRef text As String) As Boolean

  ' Assume Success
  Main = True

  ' Initialise
  mPsh = pageSetHandler
  mUI = userInterface
  mReports = reports
  mDataSource = DirectCast(mPsh.DataSource, finDataSourceAccountApp)
  mAccountApp = mDataSource.AccountApp

  ' Handle Events
  Select Case eventId
    Case "Initialise"
      ' Runs when the Page Set Handler is first initialised. Initialise any module-level variables here.

    Case "CommandButtonClick"
      ' Occurs when a command button is clicked, e.g., the Cancel and Finish buttons on a wizard. Set a return value of 'Cancel' to True to prevent built-in functionality from occuring, e.g., prevent the wizard from being cancelled

    Case "FieldsLoad"
      ' Occurs when fields (Page Objects) should be loaded with a value. Generally when no automatic data binding is being used

    Case "FieldsSave"
      ' Occurs when the data in fields (Page Objects) should saved back to the data source. Generally when no automatic data binding is being used

    Case "PageObjectClick"
      ' Occurs when a button or label on the page is clicked. Set handled to True so not perform built-in functionality, e.g., following a hyperlink
      Main = PageObjectClick(eventArgs.GetString("PageCode"), eventArgs.GetString("PageObjectName"))

    Case "PageObjectAfterSpecialAction"
      ' Occurs after a button or label linked to a special action, e.g., showing an Account Schedule is clicked and the action performed or cancelled

    Case "PageObjectValueChanged"
      ' Occurs when a button or label on the page is clicked. Set handled to True so not perform built-in functionality, e.g., following a hyperlink

    Case "PageDisplay"
      ' Wizard navigation only, after all display rules have successfully passed. Set handled to False to skip this page. Direction will be either 1 or -1 depending on whether Next or Back has been clicked

    Case "PageResize"
      ' Occurs when the page is resized due to the window size changing. Use this event to reposition page objects if the page layout is not set to 'Flow'

    Case "PageValidate"
      ' Wizard navigation only, after all validation rules have successfully passed. Validate a page when moving forward through the wizard. Set this function's return value to False to fail validation and optionally specify a page object (or the name of a Page Object) that caused the failure

    Case "CustomHyperlinkClick"
      ' A hyperlink on a Web Browser page object has been clicked, e.g., the URL is custom://ActionName?param1=value1&param2=value2
  End Select

End Function

Private Function PageObjectClick(ByVal pageCode As String, _
                                 ByVal pageObjectName As String) As Boolean

  Dim ApplicantKey As String

  ' Assume Success
  PageObjectClick = True

  Select UCase(pageCode)
     Case "SYSCLIENTS"
        Select Case UCase(pageObjectName)
          Case "CMDAPPLICANT1CLIENTLOAD", "CMDAPPLICANT2CLIENTLOAD", "CMDAPPLICANT3CLIENTLOAD", "CMDAPPLICANT4CLIENTLOAD"
            ' Load Applicant details from Client
            ApplicantKey = CInt(Mid(pageObjectName, 13, 1))
            If mUI.MsgBox(String.Format("Load Applicant {0} details from from Cient '{1}'?", ApplicantKey, mAccountApp.Applicants.ItemByKey(ApplicantKey).ClientId), MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
              PageObjectClick = LoadApplicantFromClient(ApplicantKey)
            End If
        End Select
  End Select

  ' Error
  If Not PageObjectClick Then
    finBL.Error.ErrorExtend(String.Format("Failed to handle PageObjectClick for Page Object '{0}' on Page '{1}'.", pageObjectName, pageCode))
  End If

End Function

Private Function LoadApplicantFromClient(ByVal key As String) As Boolean

  Dim AddressDetails As ISAddressDetails
  Dim Applicant As finAccountAppApplicant
  Dim Client As finClient

  ' Assume Success
  LoadApplicantFromClient = True

  ' Initialise
  Applicant = mAccountApp.Applicants.ItemByKey(key)
  Client = Applicant.Client

  ' Update Applicant
  With Applicant
    ' Personal
    .Title = Client.Title
    .FirstName = Client.FirstName
    .MiddleNames = Client.MiddleNames
    .LastName = Client.LastName
    .DateOfBirth = Client.DateOfBirth
    .Gender = Client.Gender
    .MaritalStatus = Client.MaritalStatus
    .Dependants = Client.Dependants

    ' Contact Details
    .ContactEmail = Client.ContactMethods.GetCurrentEmail().Value
    .ContactMobile = Client.ContactMethods.GetCurrentMobile().Value
    If Client.IsIndividual Then
      .ContactPhone = Client.ContactMethods.GetCurrentPhone(,,,iseTriState.True).Value ' Home Phone Number
      .ContactPhoneWork = Client.ContactMethods.GetCurrentPhoneWork().Value
    Else
      .ContactPhone = Client.ContactMethods.GetCurrentPhone().Value
    End If

    ' Addresses
    AddressDetails = Client.ContactAddresses.GetCurrentPhysicalAddress()
    With .AddressPhysical
      .PropertyName = AddressDetails.PropertyName
      .StreetAddressFull = AddressDetails.StreetAddressFull
      .StreetUnit = AddressDetails.StreetUnit
      .StreetNumber = AddressDetails.StreetNumber
      .StreetName = AddressDetails.StreetName
      .StreetType = AddressDetails.StreetType
      .StreetDirection = AddressDetails.StreetDirection
      .Suburb = AddressDetails.Suburb
      .City = AddressDetails.City
      .State = AddressDetails.State
      .Postcode = AddressDetails.Postcode
      .Country = .Country
    End With
    AddressDetails = Client.ContactAddresses.GetCurrentPostalAddress()
    With .AddressPostal
      .PropertyName = AddressDetails.PropertyName
      .StreetAddressFull = AddressDetails.StreetAddressFull
      .StreetUnit = AddressDetails.StreetUnit
      .StreetNumber = AddressDetails.StreetNumber
      .StreetName = AddressDetails.StreetName
      .StreetType = AddressDetails.StreetType
      .StreetDirection = AddressDetails.StreetDirection
      .Suburb = AddressDetails.Suburb
      .City = AddressDetails.City
      .State = AddressDetails.State
      .Postcode = AddressDetails.Postcode
      .Country = .Country
    End With

    'TODO: Company

  End With

  ' Force Page Set Handler to reload all values
  mPsh.FieldsLoad(False)

  ' Error
  If Not LoadApplicantFromClient Then
    finBL.Error.ErrorExtend(String.Format("Failed to load Applicant {0} details from Client.", key))
  End If

End Function