Bug 625976 - Python Bindings Patch for Transaction.GetImbalance(), patch by Mark Jenkins

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19401 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Geert Janssens 2010-08-04 21:11:11 +00:00
parent bd033d20a6
commit fe928cda93
4 changed files with 108 additions and 3 deletions

View File

@ -130,7 +130,8 @@ typedef char gchar;
}
}
%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList * {
%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList *,
MonetaryList * {
guint i;
gpointer data;
PyObject *list = PyList_New(0);
@ -146,7 +147,10 @@ typedef char gchar;
else if (GNC_IS_COMMODITY(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_gnc_commodity, 0));
else if (GNC_IS_LOT(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCLot, 0));
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCLot, 0));
else if ($1_descriptor == $descriptor(MonetaryList *)){
PyList_Append(list, SWIG_NewPointerObj(data, $descriptor(gnc_monetary *), 0));
}
else
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_void, 0));
}

View File

@ -56,6 +56,7 @@ EXTRA_DIST = \
example_scripts/change_tax_code.py \
example_scripts/account_analysis.py \
example_scripts/new_book_with_opening_balances.py \
example_scripts/test_imbalance_transaction.py \
glib.i
MAINTAINERCLEANFILES = gnucash-core.c

View File

@ -0,0 +1,81 @@
#!/usr/bin/env python
# test_imbalance_transaction.py -- Test the transaction imbalace viewing
# mechanisms
#
# Copyright (C) 2010 ParIT Worker Co-operative <transparency@parit.ca>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact:
# Free Software Foundation Voice: +1-617-542-5942
# 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
# Boston, MA 02110-1301, USA gnu@gnu.org
#
# @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
from sys import argv
from gnucash import Session, Transaction, Split, Account, GncNumeric, \
GncCommodity
# must be sqlite:///path_to_file or xml:///path_to_file
# and must be an existing gnucash file
#
# You should try it out with a gnucash file with tranding accounts enabled
# and trading accounts disabled
session = Session(argv[1])
book = session.book
root = book.get_root_account()
root.get_instance()
commod_tab = session.book.get_table()
CAD = commod_tab.lookup("ISO4217","CAD")
USD = commod_tab.lookup("ISO4217","USD")
account = Account(book)
account2 = Account(book)
root.append_child(account)
root.append_child(account2)
account.SetCommodity(CAD)
account.SetName("blahblah")
account.SetType(3)
account2.SetCommodity(USD)
account2.SetName("blahblahsdfs ")
account2.SetType(3)
a = Transaction(book)
a.BeginEdit()
s = Split(book)
s.SetParent(a)
s2 = Split(book)
s2.SetParent(a)
a.SetCurrency(CAD)
s.SetAccount(account)
s.SetValue(GncNumeric(2))
s.SetAmount(GncNumeric(2))
s2.SetAccount(account2)
s2.SetValue(GncNumeric(4))
s2.SetAmount(GncNumeric(4))
print 'overall imbalance', a.GetImbalanceValue().to_string()
print 'per-currency imbalances'
imbalance_list = a.GetImbalance()
for (commod, value) in imbalance_list:
print value.to_string(), commod.get_mnemonic()
a.CommitEdit()
session.end()
session.destroy()

View File

@ -241,6 +241,14 @@ class Transaction(GnuCashCoreClass):
Consists of at least one (generally two) splits to represent a transaction
between two accounts.
Has a GetImbalance() method that returns a list of all the imbalanced
currencies. Each list item is a two element tuple, the first element is
the imbalanced commodity, the second element is the value.
Warning, the commodity.get_instance() value can be None when there
is no currency set for the transaction.
"""
_new_instance = 'xaccMallocTransaction'
def GetNthSplit(self, n):
@ -251,6 +259,15 @@ class Transaction(GnuCashCoreClass):
return self.do_lookup_create_oo_instance(
gncInvoiceGetInvoiceFromTxn, Transaction )
def decorate_monetary_list_returning_function(orig_function):
def new_function(self):
# warning, item.commodity has been shown to be None
# when the transaction doesn't have a currency
return [(GncCommodity(instance=item.commodity),
GncNumeric(instance=item.value))
for item in orig_function(self) ]
return new_function
class Split(GnuCashCoreClass):
"""A GnuCash Split
@ -402,7 +419,7 @@ trans_dict = {
'Clone': Transaction,
'Reverse': Transaction,
'GetReversedBy': Transaction,
'GetImbalance': GncNumeric,
'GetImbalanceValue': GncNumeric,
'GetAccountValue': GncNumeric,
'GetAccountAmount': GncNumeric,
'GetAccountConvRate': GncNumeric,
@ -411,6 +428,8 @@ trans_dict = {
'GetGUID': GUID
}
methods_return_instance(Transaction, trans_dict)
Transaction.decorate_functions(
decorate_monetary_list_returning_function, 'GetImbalance')
# Split
Split.add_methods_with_prefix('xaccSplit')