<?xml version="1.0" encoding="utf-8"?>
  <finScript version="1.00" exportedUtcDate="2022-09-21T01:08:56.0000000">
    <CreatedUserId>ADMIN</CreatedUserId>
    <CreatedUtcDate>2018-03-27T01:49:27.7810000</CreatedUtcDate>
    <UpdatedUserId>ADMIN</UpdatedUserId>
    <UpdatedUtcDate>2022-09-21T01:04:37.2240000</UpdatedUtcDate>
    <ScriptId>ADDRESSCUP</ScriptId>
    <Description>Update Address for existing Clients</Description>
    <ScriptType>GeneralUserInterface</ScriptType>
    <Active>true</Active>
    <AllowUI>false</AllowUI>
    <Category />
    <Code>' Script_ClientUpdateAddress.xml
Option Explicit On
Option Strict On

' #################################################################
' Utility to update Client Address Information from NZ PAF information.
' 
' Version: 1.04 (21/09/2022)
' 
' WARNING: Sample Script, please ensure this script fits your purposes before using.
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    1.02  09/07/2021  ISS
'    1.03  16/08/2021  ISS      J027199   Fixed issues with PropertyName and Street Address
'    1.04  21/09/2022  ISS      J029586   Fixed compilation warnings
' #################################################################

' Parameters
Private UpdateClientInformation As Boolean

' Properties
Private ExcelWorkbook As ISExcelWorkbook
Private RowFixable As Integer
Private RowUnknown As Integer
Private RowValid As Integer

Public Function Main(userInterface As ISUserInterfaceBL, reports As ISfinReports, parameters As ISKeyValueList) As Boolean

  Dim Client As finClient
  Dim dr As IDataReader
  Dim RowCount As Integer
  Dim RowCounter As Integer
  Dim sqb As ISSelectQueryBuilder
  Dim strTemp As String
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Get Constants
  With parameters
    UpdateClientInformation = .GetBoolean("UpdateClientInformation")
  End With

  ' Validate
  If finBL.SupportedCountries.CurrentSupportedCountry.CountryCode &lt;&gt; "NZ" Then
    Success = False
    finBL.Error.ErrorBegin("This script only supports New Zealand databases.")
  ElseIf Not finBL.settings.AddOns.AddressFullVerification Then
    Success = False
    finBL.Error.ErrorBegin("You are not licensed for the Full Address Verification Add-On.")
  ElseIf Not finBL.Addressing.AddressInterfaceInitialised Then
    Success = False
    finBL.Error.ErrorBegin("The Address Interface not initialised.")
  ElseIf Len(finBL.SettingsUser.ExportDataFolderResolved) = 0 Then
    Success = False
    finBL.Error.ErrorBegin("You must set a file path in Tools, User Preferences, Documents and Templates, Export Folder.")
  End If

  ' Warn that running this document can change addresses in the database
  If Success Then
    If UpdateClientInformation Then
      strTemp = "WARNING:  This process may update Client addresses." &amp; vbNewLine &amp; vbNewLine
      strTemp = strTemp &amp; "1. It is not guaranteed to be 100% accurate in finding the correct address." &amp; vbNewLine
      strTemp = strTemp &amp; "2. You should run this document without updating addresses first, and check the results before proceeding." &amp; vbNewLine &amp; vbNewLine
      strTemp = strTemp &amp; "Continue?"
      Select Case userInterface.MsgBoxEx(strTemp, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        Case MsgBoxResult.No
          Success = False
          finBL.Error.ErrorBegin("Cancelled by User.")
      End Select
    End If
  End If

  ' Create Query (get from BL since since we are using Standard Parameters in this sample)
  If Success Then
    sqb = finBL.DocumentFunctions.GetClientSqlSelectQueryBuilder(parameters)
  End If

  ' Create the Excel Workbook
  If Success Then
    ExcelWorkbook = finBL.CreateExcelWorkbook()
    With ExcelWorkbook
      .Worksheets.Add("Valid")
      With .Worksheets(0)
        .Rows(1).Style.Bold = True
        .Cell(1,1).SetString("ClientId")
        .Cell(2,1).SetString("Name")
        .Cell(3,1).SetString("PostalId")
        .Cell(4,1).SetString("Street")
        .Cell(5,1).SetString("Suburb")
        .Cell(6,1).SetString("City")
        .Cell(7,1).SetString("Postcode")
      End With

      .Worksheets.Add("Fixable")
      With .Worksheets(1)
        .Rows(1).Style.Bold = True
        .Cell(1,1).SetString("ClientId")
        .Cell(2,1).SetString("Name")
        .Cell(3,1).SetString("PostalId")
        .Cell(4,1).SetString("Property")
        .Cell(5,1).SetString("Address")
        .Cell(6,1).SetString("Suburb")
        .Cell(7,1).SetString("City")
        .Cell(8,1).SetString("Postcode")
        .Cell(9,1).SetString("Sanitised PostalId")
        .Cell(10,1).SetString("Property")
        .Cell(11,1).SetString("Address")
        .Cell(12,1).SetString("Suburb")
        .Cell(13,1).SetString("City")
        .Cell(14,1).SetString("Postcode")
      End With

      .Worksheets.Add("Unknown")
      With .Worksheets(2)
        .Rows(1).Style.Bold = True
        .Cell(1,1).SetString("ClientId")
        .Cell(2,1).SetString("Name")
        .Cell(3,1).SetString("PostalId")
        .Cell(4,1).SetString("Street")
        .Cell(5,1).SetString("Suburb")
        .Cell(6,1).SetString("City")
        .Cell(7,1).SetString("Postcode")
        .Cell(8,1).SetString("Error")
      End With
    End With

    If ExcelWorkbook.Save(String.Format("{0}{1}_{2}.xls", finBL.SettingsUser.ExportDataFolderResolved(True), finBL.Runtime.FileUtilities.GetFileNameCleaned(ScriptInfo.ScriptId), Format(finBL.Runtime.TimeZoneUtilities.GetCurrentLocalDateTime,"yyyyMMdd_HHmmss")))
      ' OK
    Else
      Success = False
      ExcelWorkbook = Nothing
    End If
  End If

  ' Process Clients
  If Success Then
    ' Initialise
    Client = finBL.CreateClient()
    RowCount = 0
    RowCounter = 0
    RowFixable = 1
    RowUnknown = 1
    RowValid = 1

    ' Begin Progress
    finBL.StatusProcessBegin("Updating Client address information", "Querying Database", False)

    If sqb.ExecuteDisconnectedDataReader(dr, RowCount, ScriptInfo.TimeoutSeconds) Then
      With dr
        Do While .Read
          RowCounter += 1
          If Client.LoadPk(finBL.Database.GetFieldInteger(!Pk)) Then
            ' Progress
            finBL.StatusProcess((RowCounter / RowCount) * 100, String.Format("{0} - {1}", Client.ClientId, Client.Name))

            ' Check Address
            Me.CheckAddress(Client)
          Else
            Success = False
            Exit Do
          End If
        Loop
      End With
      finBL.Database.DataReaderClose(dr)
    Else
      Success = False
    End If

    ' End Progress
    finBL.StatusProcessEnd()
  End If

  ' Save Workbook (ignore errors)
  If ExcelWorkbook IsNot Nothing Then
    ' Autofit Columns
    If ExcelWorkbook.Worksheets.Count &gt;= 1 Then ExcelWorkbook.Worksheets(0).Columns.Autofit
    If ExcelWorkbook.Worksheets.Count &gt;= 2 Then ExcelWorkbook.Worksheets(1).Columns.Autofit
    If ExcelWorkbook.Worksheets.Count &gt;= 3 Then ExcelWorkbook.Worksheets(2).Columns.Autofit

    If ExcelWorkbook.Save() Then
      ' Open Workbook
      finBL.OpenAssociatedApplication(ExcelWorkbook.Filename)
    End If
  End If

  ' Return Success
  Return Success

End Function

Private Sub CheckAddress(client As finClient)

  Dim ClientAddress As ISAddressDetails
  Dim ClientContactAddress As finClientContactAddress
  Dim SanitisedAddress As ISAddressDetails
  Dim strTemp As String
  Dim TempAddress As ISAddressDetails

  ' Review each address for Client - must be current and postal
  For Each ClientContactAddress In client.ContactAddresses
    If ClientContactAddress.IsCurrent AndAlso ClientContactAddress.AddressPostal Then
      ' Get Address details
      ClientAddress = ClientContactAddress.ToAddressDetails(True)

      ' Validate Address - must have a value and be in New Zealand
      If ClientAddress.HasValue AndAlso (Len(ClientAddress.Country) = 0 OrElse StrComp(ClientAddress.Country, "New Zealand", CompareMethod.Text) = 0) Then
        If Len(ClientAddress.PostalId) = 0 Then
          ' No Postal Id

          ' Clone address and get address text
          TempAddress = ClientAddress.Clone
          TempAddress.Instructions = ""
          TempAddress.StreetAddressFull = Replace(TempAddress.StreetAddressFull, ",", "/")
          strTemp = finBL.Addressing.FormatAddressFromAddressDetails(TempAddress)

          If finBL.Addressing.SanitiseAddress(strTemp, SanitisedAddress) Then
            ' Fixable address
            RowFixable += 1

            With ExcelWorkbook.Worksheets(1)
              .Cell(1, RowFixable).SetString(Client.ClientId)
              .Cell(2, RowFixable).SetString(Client.Name)
              .Cell(3, RowFixable).SetString(ClientAddress.PostalId)
              .Cell(4, RowFixable).SetString(ClientAddress.PropertyName)
              .Cell(5, RowFixable).SetString(ClientAddress.StreetAddressFull)
              .Cell(6, RowFixable).SetString(ClientAddress.Suburb)
              .Cell(7, RowFixable).SetString(ClientAddress.City)
              .Cell(8, RowFixable).SetString(ClientAddress.Postcode)
              .Cell(9, RowFixable).SetString(SanitisedAddress.PostalId)
              .Cell(10, RowFixable).SetString(SanitisedAddress.PropertyName)
              .Cell(11, RowFixable).SetString(SanitisedAddress.StreetAddressFull)
              .Cell(12, RowFixable).SetString(SanitisedAddress.Suburb)
              .Cell(13, RowFixable).SetString(SanitisedAddress.City)
              .Cell(14, RowFixable).SetString(SanitisedAddress.Postcode)
            End With

            ' Update client address
            If UpdateClientInformation Then
              strTemp = ClientContactAddress.Instructions
              ClientContactAddress.FromAddressDetails(SanitisedAddress)
              ClientContactAddress.Instructions = strTemp

              Client.Save()
            End If
          Else
            ' Unknown address
            RowUnknown += 1

            With ExcelWorkbook.Worksheets(2)
              .Cell(1, RowUnknown).SetString(Client.ClientId)
              .Cell(2, RowUnknown).SetString(Client.Name)
              .Cell(3, RowUnknown).SetString(ClientAddress.PostalId)
              .Cell(4, RowUnknown).SetString(ClientAddress.StreetAddressFull)
              .Cell(5, RowUnknown).SetString(ClientAddress.Suburb)
              .Cell(6, RowUnknown).SetString(ClientAddress.City)
              .Cell(7, RowUnknown).SetString(ClientAddress.Postcode)
              .Cell(8, RowUnknown).SetString(finBL.Error.Message(True, , True))
            End With
          End If
        Else
          ' Address has Postal Id
          If finBL.Addressing.ValidatePostalId(ClientAddress) Then
            ' Valid Postal Id with matching address
            RowValid += 1

            With ExcelWorkbook.Worksheets(0)
              .Rows(1).Style.Bold = True
              .Cell(1, RowValid).SetString(Client.ClientId)
              .Cell(2, RowValid).SetString(Client.Name)
              .Cell(3, RowValid).SetString(ClientAddress.PostalId)
              .Cell(4, RowValid).SetString(ClientAddress.StreetAddressFull)
              .Cell(5, RowValid).SetString(ClientAddress.Suburb)
              .Cell(6, RowValid).SetString(ClientAddress.City)
              .Cell(7, RowValid).SetString(ClientAddress.Postcode)
            End With
          Else
            If finBL.Addressing.GetAddressFromPostalId(ClientAddress.PostalId, SanitisedAddress) Then
              ' Valid Postal Id, but address needs updating
              RowFixable += 1

              With ExcelWorkbook.Worksheets(1)
                .Cell(1, RowFixable).SetString(Client.ClientId)
                .Cell(2, RowFixable).SetString(Client.Name)
                .Cell(3, RowFixable).SetString(ClientAddress.PostalId)
                .Cell(4, RowFixable).SetString(ClientAddress.StreetAddressFull)
                .Cell(5, RowFixable).SetString(ClientAddress.PropertyName)
                .Cell(6, RowFixable).SetString(ClientAddress.Suburb)
                .Cell(7, RowFixable).SetString(ClientAddress.City)
                .Cell(8, RowFixable).SetString(ClientAddress.Postcode)
                .Cell(9, RowFixable).SetString(SanitisedAddress.PostalId)
                .Cell(10, RowFixable).SetString(SanitisedAddress.PropertyName)
                .Cell(11, RowFixable).SetString(SanitisedAddress.StreetAddressFull)
                .Cell(12, RowFixable).SetString(SanitisedAddress.Suburb)
                .Cell(13, RowFixable).SetString(SanitisedAddress.City)
                .Cell(14, RowFixable).SetString(SanitisedAddress.Postcode)
              End With

              ' Update client address
              If UpdateClientInformation Then
                strTemp = ClientContactAddress.Instructions
                ClientContactAddress.FromAddressDetails(SanitisedAddress)
                ClientContactAddress.Instructions = strTemp

                Client.Save()
              End If
            Else
              ' Postal Id not found
              RowUnknown += 1

              With ExcelWorkbook.Worksheets(2)
                .Cell(1, RowUnknown).SetString(Client.ClientId)
                .Cell(2, RowUnknown).SetString(Client.Name)
                .Cell(3, RowUnknown).SetString(ClientAddress.PostalId)
                .Cell(4, RowUnknown).SetString(ClientAddress.StreetAddressFull)
                .Cell(5, RowUnknown).SetString(ClientAddress.Suburb)
                .Cell(6, RowUnknown).SetString(ClientAddress.City)
                .Cell(7, RowUnknown).SetString(ClientAddress.Postcode)
                .Cell(8, RowUnknown).SetString(finBL.Error.Message(True, , True))
              End With
            End If
          End If
        End If
      End If
    End If
  Next

End Sub</Code>
    <Language>VisualBasic</Language>
    <Notes>This utility script can be used in New Zealand to update the Client Address Information.

The following constants can be used:

UpdateClientInformation(Boolean, default=False)
Indicates whether the Client Address Information should be updated.</Notes>
    <PermissionKeyExecute />
    <RunWithoutPrompts>false</RunWithoutPrompts>
    <TemplateText />
    <TemplateTextBookmarks />
    <TimeoutSeconds>240</TimeoutSeconds>
    <UniqueId>OZ187OShK0SxQ396RZErcA</UniqueId>
    <AllowSaveLoadParametersOnExecute>false</AllowSaveLoadParametersOnExecute>
    <RunAsAdministrator>false</RunAsAdministrator>
    <ShowInMenu>None</ShowInMenu>
    <ParametersType>Custom</ParametersType>
    <Parameters>
      <ISParameterDefs version="1.00">
        <Parameters>
          <Parameter name="_PageStart1" type="PageStart">
            <BlankTip />
            <Caption>General</Caption>
            <StandaloneOnly>false</StandaloneOnly>
            <TooltipText />
          </Parameter>
          <Parameter name="" type="SectionStart">
            <BlankTip />
            <Caption>Update Details.</Caption>
            <StandaloneOnly>false</StandaloneOnly>
            <TooltipText>Indicates whether the Client Address Information should be updated.</TooltipText>
            <SectionColumns>1</SectionColumns>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="UpdateClientInformation" type="Boolean">
            <BlankTip />
            <Caption>Update Client Information?</Caption>
            <StandaloneOnly>false</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value>False</Value>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="_SectionStart1" type="SectionStart">
            <BlankTip />
            <Caption>Select the Clients and Order By you require.</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <SectionColumns>1</SectionColumns>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="OrderBy" type="List">
            <BlankTip />
            <Caption />
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>true</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value>Code</Value>
            <Visible>true</Visible>
            <List>Code,Name</List>
            <LimitToList>true</LimitToList>
            <MaxLength>0</MaxLength>
            <Rows>1</Rows>
          </Parameter>
          <Parameter name="Clients" type="Range">
            <BlankTip />
            <Caption>Clients</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value />
            <Visible>true</Visible>
            <Rows>8</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>Clients</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
          <Parameter name="ActiveOnly" type="Boolean">
            <BlankTip />
            <Caption>Active Clients only?</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value>True</Value>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="_SectionStart2" type="SectionStart">
            <BlankTip />
            <Caption>Enter any parameters you require.</Caption>
            <StandaloneOnly>false</StandaloneOnly>
            <TooltipText />
            <SectionColumns>1</SectionColumns>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="_SectionStart3" type="SectionStart">
            <BlankTip />
            <Caption>If required, enter Ranges to filter by.</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <SectionColumns>1</SectionColumns>
            <Visible>true</Visible>
          </Parameter>
          <Parameter name="Branches" type="Range">
            <BlankTip />
            <Caption>Branches</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value />
            <Visible>true</Visible>
            <Rows>1</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>Branches</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
          <Parameter name="ClientTypes" type="Range">
            <BlankTip />
            <Caption>Client Types</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value />
            <Visible>true</Visible>
            <Rows>1</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>ClientTypes</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
          <Parameter name="ClientGroups" type="Range">
            <BlankTip />
            <Caption>Client Groups</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value />
            <Visible>true</Visible>
            <Rows>1</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>ClientGroups</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
          <Parameter name="Entities" type="Range">
            <BlankTip />
            <Caption>Entities</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText>Enter '[Default]' to use the Entities Range defined under User Preferences, Reporting</TooltipText>
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value>[Default]</Value>
            <Visible>true</Visible>
            <Rows>1</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>Entities</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
          <Parameter name="TaxCategories" type="Range">
            <BlankTip />
            <Caption>Tax Categories</Caption>
            <StandaloneOnly>true</StandaloneOnly>
            <TooltipText />
            <DefaultValueExpression />
            <Enabled>true</Enabled>
            <HideDescription>false</HideDescription>
            <IndentLevel>0</IndentLevel>
            <Mandatory>false</Mandatory>
            <NonPersistable>false</NonPersistable>
            <ReadOnly>false</ReadOnly>
            <ReadOnly2>false</ReadOnly2>
            <Value />
            <Visible>true</Visible>
            <Rows>1</Rows>
            <RangeSingle>false</RangeSingle>
            <RangeType>Standard</RangeType>
            <RangeLookupSource>TaxCategories</RangeLookupSource>
            <RangeLookupSqlWhere />
          </Parameter>
        </Parameters>
      </ISParameterDefs>
    </Parameters>
    <Constants>
      <ISScriptConstants version="1.00">
        <ScriptConstant name="UpdateClientInformation" constantType="Boolean">
          <Notes>Boolean flag to indicates whether the Client Address Information should be updated.</Notes>
          <Value>false</Value>
        </ScriptConstant>
      </ISScriptConstants>
    </Constants>
  </finScript>