GnuCash  5.6-150-g038405b370+
test_imbalance_transaction.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # test_imbalance_transaction.py -- Test the transaction imbalace viewing
4 # mechanisms
5 #
6 # Copyright (C) 2010 ParIT Worker Co-operative <transparency@parit.ca>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, contact:
19 # Free Software Foundation Voice: +1-617-542-5942
20 # 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21 # Boston, MA 02110-1301, USA gnu@gnu.org
22 #
23 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
24 
25 
29 
30 from sys import argv, exit
31 
32 from gnucash import Session, Transaction, Split, Account, GncNumeric, \
33  GncCommodity
34 
35 # argv[1] should be the path to an existing gnucash file/database
36 # for a file, simply pass the pathname, for a database you can use
37 # these forms:
38 # mysql://user:password@host/dbname
39 # postgres://user:password@host[:port]/dbname (the port is optional)
40 #
41 # You should try it out with a gnucash file with tranding accounts enabled
42 # and trading accounts disabled
43 
44 if len(argv) < 2:
45  print('not enough parameters')
46  print('usage: test_imbalance_transaction.py {book_url}')
47  print('examples:')
48  print("python3 test_imbalance_transaction.py '/home/username/test.gnucash'")
49  exit()
50 
51 
52 try:
53  session = Session(argv[1])
54 
55  book = session.book
56 
57  root = book.get_root_account()
58  root.get_instance()
59  commod_tab = session.book.get_table()
60  CAD = commod_tab.lookup("ISO4217","CAD")
61  USD = commod_tab.lookup("ISO4217","USD")
62  account = Account(book)
63  account2 = Account(book)
64  root.append_child(account)
65  root.append_child(account2)
66  account.SetCommodity(CAD)
67  account.SetName("blahblah")
68  account.SetType(3)
69  account2.SetCommodity(USD)
70  account2.SetName("blahblahsdfs ")
71  account2.SetType(3)
72 
73  a = Transaction(book)
74  a.BeginEdit()
75 
76  s = Split(book)
77  s.SetParent(a)
78  s2 = Split(book)
79  s2.SetParent(a)
80 
81  a.SetCurrency(CAD)
82  s.SetAccount(account)
83  s.SetValue(GncNumeric(2))
84  s.SetAmount(GncNumeric(2))
85 
86  s2.SetAccount(account2)
87  s2.SetValue(GncNumeric(4))
88  s2.SetAmount(GncNumeric(4))
89  print('overall imbalance', a.GetImbalanceValue().to_string())
90 
91  print('per-currency imbalances')
92  imbalance_list = a.GetImbalance()
93  for (commod, value) in imbalance_list:
94  print(value.to_string(), commod.get_mnemonic())
95 
96  a.CommitEdit()
97 
98 
99  session.end()
100  session.destroy()
101 except:
102  if "session" in locals():
103  session.end()
104  raise
STRUCTS.
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:60