#!/usr/bin/env python

global moneydance                           # Entry point into the Moneydance API
mdGUI = moneydance.getUI()                  # Entry point into the GUI
book = moneydance.getCurrentAccountBook()   # Entry point into your dataset
root = moneydance.getCurrentAccount()


class QuickAbortThisScriptException(Exception): pass        # This is a way to quickly exit the script

try:
    # Change the account names in the following two lines!
    # The transactions in fromAccount will be moved to toAccount if both accounts already exist
    fromAccount = root.getAccountByName("Automotive:Fuel")
    toAccount = root.getAccountByName("Automotive:Misc")

    if (not fromAccount or not toAccount) or (fromAccount is toAccount):
        txt = "ERROR: fromAccount: '%s' or toAccount: '%s' NOT found (or they are the same account) - ABORTING!" %(fromAccount, toAccount)
        print(txt)
        raise QuickAbortThisScriptException(txt)

    print("moving transactions from '%s' to '%s'" % (fromAccount, toAccount))

    print("moving now...")
    txns = book.getTransactionSet().getTransactionsForAccount(fromAccount)
    for txn in txns:
        txn.setAccount(toAccount)
        txn.getParentTxn().syncItem()


except QuickAbortThisScriptException: pass
