#Description: Example function to Restructure a Fixed Term Loan Account.
#Author: Intersoft Systems Limited
#Version: 1.01
#Last Updated: 18/07/2016

''' <summary>
'''  Restructure a Fixed Term Loan Account.
''' </summary>
''' <param name="accountPk">
'''  The Primary Key of the Account. If you already have the Account object loaded, set to Account.Pk.
''' </param>
''' <param name="restructureDate">
'''  The date of the Restructure.
''' </param>
''' <param name="restructureByWhom">
'''  Who made the Restructure.
''' </param>
''' <param name="restructureType">
'''  The Type of Restructure.
''' </param>
''' <param name="restructureReason">
'''  Reason for the Restructure.
''' </param>
''' <param name="newDateMaturity">
'''  The new Maturity Date. Nothing indicates Maturity Date is recalculated based upon the Regular Payment, either the existing or newPaymentRegular.
''' </param>
''' <param name="newPaymentRegular">
'''  The new Regular Payment. 0 indicates no change.
''' </param>
''' <param name="newInterestRate">
'''  The new Interest Rate. -1 indicates no change.
''' </param>
''' <param name="clearOverdue">
'''  Set to True to clear existing Overdues.
''' </param>
''' <returns>
'''  A Boolean value indicating success.
''' </returns>
''' <remarks>
'''  1. You can only set a new Maturity Date or new Regular Payment, not both.
'''  2. Fees are charged based upon Account Type settings.
''' </remarks>
Private Function Account_Restructure(accountPk As Integer,
                                     restructureDate As Date,
                                     restructureByWhom As String,
                                     restructureType As String,
                                     restructureReason As String,
                            Optional newDateMaturity As Date = Nothing,        
                            Optional newPaymentRegular As Decimal = 0,
                            Optional newInterestRate As Double = -1,
                            Optional clearOverdue As Boolean = True) As Boolean

  Dim AccountRestructure As finAccountRestructure
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise
  AccountRestructure = finBL.CreateAccountRestructure()

  ' Create Restructure
  With AccountRestructure
    ' Load Account
    Success = .AccountLoadPk(accountPk)

    If Success Then
      ' Update Properties
      .RestructureDate = restructureDate
      .RestructureByWhom = restructureByWhom
      .RestructureType = restructureType
      .RestructureReason = restructureReason

      ' Update Current Calculation
      Success = .CalculateCurrent()
    End If  

    If Success Then
      ' Update calculation
      With .CalculationNew
        If newDateMaturity = Nothing Then
          .PaymentFinalDateHold = False
        Else
          .PaymentFinalDateHold = True
          .PaymentFinalDateHoldDate = .DateMaturity
        End If
        .PaymentRegularOverride = newPaymentRegular
        If newInterestRate <> -1 Then .InterestRate = newInterestRate
      End With

      ' Optionally clear Overdue
      If clearOverdue Then
        .BalanceOverdue = 0
      Else
        ' Add Promises
      End If

      Success = .CalculateNew()
    End If

    ' Commit Restructure
    If Success Then
      Success = .ExecuteCommit()
    End If
  End With

  ' Return result
  Return Success

End Function
