import string

rootAccount = moneydance.getRootAccount()

## Change the account names in the following two lines!
## The transactions from Auto->Fuel will be moved to Auto->Service
## if both accounts already exist
accountName1 = "Auto:Fuel"
accountName2 = "Auto:Service"

def matchAccount(parent, acctName):
  if parent.getAccountType()==0:
    acctName = parent.getAccountName()+":"+acctName
  print "matching %s under %s"%(acctName, parent)
  colIdx = string.find(acctName, ":")
  if colIdx>=0 and string.upper(acctName[:colIdx])==string.upper(parent.getAccountName()):
    for subAcctIdx in range(0, parent.getSubAccountCount()):
      subAcctMatch = matchAccount(parent.getSubAccount(subAcctIdx), acctName[colIdx+1:])
      if subAcctMatch:
        return subAcctMatch
    return None
  else:
    if string.upper(acctName)==string.upper(parent.getAccountName()):
      return parent
  return None


acct1 = matchAccount(rootAccount, accountName1)
acct2 = matchAccount(rootAccount, accountName2)

print "moving transactions from %s to %s"%(acct1, acct2)
if acct1 and acct2:
  txns = rootAccount.getTransactionSet().getTransactionsForAccount(acct1)
  print "got txns: %s"%(txns)
  txnEnum = txns.getAllTxns()
  while txnEnum.hasMoreElements():
    txnEnum.nextElement().setAccount(acct2)

rootAccount.refreshAccountBalances()

