예제 #1
0
파일: sandbox.py 프로젝트: univic/piecash
    def lookup_account(root, name):
        path = name.split(':')
        return lookup_account_by_path(root, path)


    session = Session("/path/to/file.gnucash")  # , ignore_lock=True)
    # or use URI string: ('mysql://*****:*****@HOST/DATABASE')

    today = datetime.now()
    book = session.book  # All actions are performed through the book object (or its children)
    root = book.get_root_account()  # Parent of all accounts
    currency = book.get_table().lookup('ISO4217', "USD")
    tx = Transaction(book)
    tx.BeginEdit()
    tx.SetCurrency(currency)
    tx.SetDateEnteredTS(today)
    tx.SetDatePostedTS(today)  # or another datetime object for the transaction's "register date"
    tx.SetDescription("Transaction Description!")
    # tx.SetNum(int_variable) # if you need a transaction number
    amount = 24
    sp1 = Split(book)  # First half of transaction
    sp1.SetParent(tx)
    # The lookup string needs to match your account path exactly.
    sp1.SetAccount(lookup_account(root, "Expenses:Some Expense Account"))
    # amount is an int (no $ or .), so $5.23 becomes amount=523
    sp1.SetValue(GncNumeric(amount, 100))  # Assuming you only have one split
    # For multiple splits, you need to make sure the totals all balance out.
    sp1.SetAmount(GncNumeric(amount, 100))
    sp1.SetMemo("Split Memo!")  # optional

    sp2 = Split(book)  # Need a balancing split