GnuCash  4.11-243-g1cac132214+
simple_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
5 
6 from gnucash import (
7  Session, Account, Transaction, Split, GncNumeric, SessionOpenMode)
8 
9 FILE_1 = "/tmp/example.gnucash"
10 
11 with Session("xml://%s" % FILE_1, SessionOpenMode.SESSION_NEW_STORE) as session:
12 
13  book = session.book
14  root_acct = Account(book)
15  expenses_acct = Account(book)
16  savings_acct = Account(book)
17  opening_acct = Account(book)
18  trans1 = Transaction(book)
19  trans1.BeginEdit()
20  trans2 = Transaction(book)
21  trans2.BeginEdit()
22 
23  split1 = Split(book)
24  split3 = Split(book)
25  comm_table = book.get_table()
26  cad = comm_table.lookup("CURRENCY", "CAD")
27 
28  num1 = GncNumeric(4, 1)
29  num2 = GncNumeric(100, 1)
30 
31  #Set new root account
32  book.set_root_account(root_acct)
33 
34  #Set up root account and add sub-accounts
35  root_acct.SetName("Root")
36  root_acct.SetType(13) #ACCT_TYPE_ROOT = 13
37  root_acct.append_child(expenses_acct)
38  root_acct.append_child(savings_acct)
39  root_acct.append_child(opening_acct)
40 
41  #Set up Expenses account
42  expenses_acct.SetCommodity(cad)
43  expenses_acct.SetName("Expenses")
44  expenses_acct.SetType(9) #ACCT_TYPE_EXPENSE = 9
45 
46  #Set up Savings account
47  savings_acct.SetCommodity(cad)
48  savings_acct.SetName("Savings")
49  savings_acct.SetType(0) #ACCT_TYPE_BANK = 0
50 
51  #Set up Opening Balance account
52  opening_acct.SetCommodity(cad)
53  opening_acct.SetName("Opening Balance")
54  opening_acct.SetType(10) #ACCT_TYPE_EQUITY = 10
55 
56  split1.SetValue(num1)
57  split1.SetAccount(expenses_acct)
58  split1.SetParent(trans1)
59 
60  split3.SetValue(num2)
61  split3.SetAccount(savings_acct)
62  split3.SetParent(trans2)
63 
64  trans1.SetCurrency(cad)
65  trans1.SetDate(14, 3, 2006)
66  trans1.SetDescription("Groceries")
67 
68  trans2.SetCurrency(cad)
69  trans2.SetDate(7, 11, 1995)
70  trans2.SetDescription("Opening Savings Balance")
71 
72  split2 = Split(book)
73  split2.SetAccount(savings_acct)
74  split2.SetParent(trans1)
75  split2.SetValue(num1.neg())
76 
77  split4 = Split(book)
78  split4.SetAccount(opening_acct)
79  split4.SetParent(trans2)
80  split4.SetValue(num2.neg())
81 
82 
83  trans1.CommitEdit()
84  trans2.CommitEdit()
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:59