1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
"""
report.py
Generate a multicolumn report for an account
"""
import sys
import datetime
from gnucashxml import from_filename
def multicolumn(book, account, date1, date2):
mybook = from_filename(book)
account = mybook.find_account(account)
if account is None:
raise Exception("Cannot find account "+account)
splits = [j for j in account.splits if date1 <= j.transaction.date.date() <= date2]
otheraccountlist = []
for split in splits: # For every transaction in this account
for multisplits in split.transaction.splits: # Round up its list of accounts
if multisplits.account not in otheraccountlist: # Compare with list of known accounts
otheraccountlist.append(multisplits.account) # Add it to list if not known
print("Date", end=",")
for i in otheraccountlist:
print(i.fullname(), end=",")
print('Description')
totals = {}
for split in sorted(splits):
print(split.transaction.date.date(), end=",")
for j in otheraccountlist:
value = sum([i.value for i in split.transaction.splits if j==i.account])
totals[j] = totals.setdefault(j, 0) + value
print(value, end=",")
print(split.transaction.description)
print("", end=",")
for i in otheraccountlist:
print(totals[i], end=",")
print("Total")
if __name__ == "__main__":
date1=datetime.date(2000, 1, 1)
date2=datetime.date(2017, 1, 1)
multicolumn("test.gnucash", "Salary", date1, date2)
|