|
GnuCash 2.4.99
|
00001 #!/usr/bin/env python 00002 00003 # test_imbalance_transaction.py -- Test the transaction imbalace viewing 00004 # mechanisms 00005 # 00006 # Copyright (C) 2010 ParIT Worker Co-operative <transparency@parit.ca> 00007 # This program is free software; you can redistribute it and/or 00008 # modify it under the terms of the GNU General Public License as 00009 # published by the Free Software Foundation; either version 2 of 00010 # the License, or (at your option) any later version. 00011 # 00012 # This program is distributed in the hope that it will be useful, 00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU General Public License 00018 # along with this program; if not, contact: 00019 # Free Software Foundation Voice: +1-617-542-5942 00020 # 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00021 # Boston, MA 02110-1301, USA gnu@gnu.org 00022 # 00023 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca> 00024 00025 ## @file 00026 # @brief Test the transaction imbalace viewing mechanisms 00027 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca> 00028 # @ingroup python_bindings_examples 00029 00030 from sys import argv, exit 00031 00032 from gnucash import Session, Transaction, Split, Account, GncNumeric, \ 00033 GncCommodity 00034 00035 # argv[1] should be the path to an existing gnucash file/database 00036 # for a file, simply pass the pathname, for a database you can use 00037 # these forms: 00038 # mysql://user:password@host/dbname 00039 # postgres://user:password@host[:port]/dbname (the port is optional) 00040 # 00041 # You should try it out with a gnucash file with tranding accounts enabled 00042 # and trading accounts disabled 00043 00044 if len(argv) < 2: 00045 print 'not enough parameters' 00046 print 'usage: test_imbalance_transaction.py {book_url}' 00047 print 'examples:' 00048 print "gnucash-env python test_imbalance_transaction.py '/home/username/test.gnucash'" 00049 exit() 00050 00051 session = Session(argv[1]) 00052 00053 try: 00054 book = session.book 00055 00056 root = book.get_root_account() 00057 root.get_instance() 00058 commod_tab = session.book.get_table() 00059 CAD = commod_tab.lookup("ISO4217","CAD") 00060 USD = commod_tab.lookup("ISO4217","USD") 00061 account = Account(book) 00062 account2 = Account(book) 00063 root.append_child(account) 00064 root.append_child(account2) 00065 account.SetCommodity(CAD) 00066 account.SetName("blahblah") 00067 account.SetType(3) 00068 account2.SetCommodity(USD) 00069 account2.SetName("blahblahsdfs ") 00070 account2.SetType(3) 00071 00072 a = Transaction(book) 00073 a.BeginEdit() 00074 00075 s = Split(book) 00076 s.SetParent(a) 00077 s2 = Split(book) 00078 s2.SetParent(a) 00079 00080 a.SetCurrency(CAD) 00081 s.SetAccount(account) 00082 s.SetValue(GncNumeric(2)) 00083 s.SetAmount(GncNumeric(2)) 00084 00085 s2.SetAccount(account2) 00086 s2.SetValue(GncNumeric(4)) 00087 s2.SetAmount(GncNumeric(4)) 00088 print 'overall imbalance', a.GetImbalanceValue().to_string() 00089 00090 print 'per-currency imbalances' 00091 imbalance_list = a.GetImbalance() 00092 for (commod, value) in imbalance_list: 00093 print value.to_string(), commod.get_mnemonic() 00094 00095 a.CommitEdit() 00096 00097 00098 session.end() 00099 session.destroy() 00100 except: 00101 if not session == None: 00102 session.end() 00103 raise
1.7.4