#Description: Example function to Refinance an existing Account into a New Account.
#Author: Intersoft Systems Limited
#Version: 1.00
#Last Updated: 13/06/2016

''' <summary>
'''  Refinances an existing Account into a new Account.
''' </summary>
''' <param name="accountPk">
'''  The Primary Key of the Account Refinancing. If you already have the Account object loaded, set to Account.Pk.
''' </param>
''' <param name="refinanceDate">
'''  The Date to Refinance the Account.
''' </param>
''' <param name="refinanceNote">
'''  Any Notes regarding the Refinance. This may be up to 50 characters and is stored on the Account Refinancing as the Close Note.
''' </param>
''' <param name="newAccountTypeId">
'''  The Account Type of the new Account to be created.
''' </param>
''' <param name="newPromotionId">
'''  Optionally, specify the Account Type Promotion of the new Account to be created.
''' </param>
''' <param name="refinancedAccountId">
'''  Returns the Id of the Account created by the Refinance.
''' </param>
''' <returns>
'''  A Boolean value indicating success.
''' </returns>
''' <remarks>
'''  1. Fees are not charged.
''' </remarks>
Private Function Account_RefinanceToNewAccount(accountPk As Integer,
                                               refinanceDate As Date,
                                               refinanceNote As String,
                                               newAccountTypeId As String,
                                      Optional newPromotionId As String = "",
                                      Optional ByRef refinancedAccountId As String = "") As Boolean

  Dim AccountClose As finAccountClose
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise ByRef
  refinancedAccountId = ""

  ' Initialise
  AccountClose = finBL.CreateAccountClose()

  ' Refinance the Account
  With AccountClose
    ' Load Account
    Success = .AccountLoadPk(accountPk)

    If Success Then
      ' Setup Refinance
      .CloseDate = refinanceDate
      .CloseMethod = isefinAccountCloseMethod.Refinanced
      .CloseNote = refinanceNote
      .FeeChargeFee = False
      .PaymentFeeCharge = False
      .RefinanceOption = isefinAccountCloseRefinanceOption.NewAccount
      .RefinanceAccountTypeId = newAccountTypeId
      If Len(newPromotionId) <> 0 Then .RefinanceAccountTypePromotionId = newPromotionId
      .TransactionNotes = .CloseNote

      ' Calculate
      Success = .Calculate()
    End If

    ' Commit Refinance
    If Success Then
      ' Refinance to New Account must be left Pending
      .ClosePending = True

      If .ExecuteCommit() Then
        ' Return New Account Id
        refinancedAccountId = .RefinancedAccountId
      Else
        Success = False
      End If
    End If
  End With

  ' Return Success
  Return Success

End Function