Dim Manager
Set Manager = CreateObject("TopalServerAPI.Manager") 'Create Topal API COM object
Dim ip, userName, userPass, clientName, fiscalYear,  found, firstAccountCode, secondAccountCode

ip = "localhost"
userName = "TopUser"
userPass = ""
clientName = "Mon Bijou AG"
fiscalYear = "2014"
firstAccountCode = "1000"
secondAccountCode = "1103"


manager.UseDefaults = true
Dim errCode
Dim message
Dim logined
'Login to Topal server
logined = Manager.Login( ip, userName, userPass, message) 'returns true  if login was successful
if (logined) then
  MsgBox("Connected successfully") 
else
  MsgBox(message)'Show error message if not logined
end if 

'Set current client client
found = false
Manager.Clients.Reset()
While Manager.Clients.MoveNext()
  Set currClient = Manager.Clients.Current
  If currClient.Name = clientName Then   
      errCode = Manager.SetGlobalClient(currClient)
	  if (errCode <> 0) then
		MsgBox(Manager.GetErrorMessage(errCode))
	  else
		MsgBox("client succesfully set")
		found = true
	  end if
  End if
Wend

if(found = false) then
	MsgBox("client not found")
end if

'Set current fiscal year
Manager.Client.FiscalYears.Reset()
While Manager.Client.FiscalYears.MoveNext()
  Set currFY = Manager.Client.FiscalYears.Current
  If currFY.Name = fiscalYear Then   
      errCode = Manager.SetGlobalFiscalYear(currFY)
	  if (errCode <> 0) then
		MsgBox(Manager.GetErrorMessage(errCode))
	  else
		MsgBox("fiscal year succesfully set")
	  end if
  End if
Wend

Set transaction = CreateObject("TopalServerAPI.Transaction")
transaction.DocDate = "19.09.2014"' by default - current date

Set posting1 = CreateObject("TopalServerAPI.Posting") 
posting1.AccountFID = manager.FindAccountID(firstAccountCode)
posting1.Text = "first posting"
posting1.Amount = 10
posting1.IsDebit = True
transaction.Postings.Add(posting1) 

Set posting2 = CreateObject("TopalServerAPI.Posting") 
posting2.AccountFID = manager.FindAccountID(secondAccountCode)
posting2.Text = "first posting"
posting2.Amount = 10
posting2.FcAmount = 11.11
posting2.ExchangeRate = 0.9
posting2.IsDebit = False
transaction.Postings.Add(posting2) 

'save transaction
errCode = Manager.SaveTransaction(transaction)
if (errCode <> 0) then
 MsgBox(Manager.GetErrorMessage(errCode))
else
 MsgBox("docNum# of created transaction: " & transaction.DocNum)
end if 

'Logout and close connection
errCode = Manager.Logout()
if (errCode <> 0) then
	MsgBox(Manager.GetErrorMessage(errCode))
else
	MsgBox("logout succesfully")
end if

Manager.Close()




