-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
114 lines (92 loc) · 4.48 KB
/
web.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import shutil
def make_category(f,org,category,balances,ledger,asof,positive):
accountnames = balances.keys()
accountnames.sort()
f.write("<table>")
f.write("<thead><tr><td colspan='2'>%s</td></tr></thead>" % category)
f.write("<tbody>")
for account in [i[len(org+":"+category)+1:] for i in accountnames if (org+":"+category in i and org+":"+category != i)]:
if len(balances[org+":"+category+":"+account]) == 0 or \
sum(balances[org+":"+category+":"+account].values()) == 0:
continue
f.write("<tr><td class='subcat'>%s</td><td class='total'>" % account)
if len(balances[org+":"+category+":"+account]) == 0:
f.write("-")
else:
f.write("<br/>".join(
"%s %.2f" % (commodity, amount * (1 if positive else -1)) for (commodity,amount) in balances[org+":"+category+":"+account].items() if amount != 0
))
f.write("</tr>")
f.write("</tbody>")
f.write("<tfoot><tr><td>Total</td><td class='total'>")
if len(ledger.balance_children(org+":"+category,asof)) == 0:
f.write("-")
else:
f.write("<br/>".join(
"%s %.2f" % (commodity, amount * (1 if positive else -1)) for (commodity,amount) in ledger.balance_children(org+":"+category,asof).items()
))
f.write("</tr></tfoot>")
f.write("</table>")
def make_report(ledger,destdir):
startdate = ledger.startdate()
enddate = ledger.enddate()
firstyear = startdate.split("-")[0]
endyear = enddate.split("-")[0]
# Year by year
categories = ["Expenses","Assets","Liabilities","Income","Equity"]
positive = { "Expenses": True, "Assets":True, "Liabilities":False, "Income": False, "Equity":False }
if not os.path.isdir(os.path.join(destdir,"css")):
shutil.copytree(os.path.join(os.path.dirname(__file__), "css"), os.path.join(destdir,"css"))
with open(os.path.join(destdir,"report.html"),"w") as f:
f.write("<!DOCTYPE html>")
f.write("<html><head><title>Report</title>")
f.write("""
<link href="http://fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<!-- CSS -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/skeleton.css">
<style type='text/css'>
* { font-family: sans-serif; margin: 0; padding: 0;}
html { font-size: 50%; }
h3 { border-bottom: 3px solid black; background: black; color: white; }
h4 { border-bottom: 1px solid black; }
table { border-collapse: collapse; width: 100%}
td { vertical-align: top; }
td.subcat { padding-left: 1em; }
.total { text-align: right; white-space: nowrap; }
thead td { font-weight: bold; border-bottom: 2px solid black; padding-left: 1.25rem }
tfoot td { font-weight: bold; border-top: 3px double black; padding-left: 1.25rem }
td { padding-top: 0.1rem; padding-bottom: 0.1rem; }
.category { float: left; padding: 1em; margin: 1em; width: 50% }
.year { page-break-after:always; }
</style>""")
f.write("</head>");
f.write("<body>")
for year in range(int(endyear),int(firstyear)-1,-1):
asof = "%d-12-31" % year
balances = ledger.balances(asof)
orgs = set()
for account in balances.keys():
orgs.add(account.split(":")[0])
for org in orgs:
f.write("<div class='container year'>")
f.write("<h4>%s EOY %d</h4>" % (org, year))
f.write("<div class='row'>")
for categories in [["Assets"],["Liabilities","Equity"]]:
f.write("<div class='six columns'>")
for category in categories:
make_category(f, org, category, balances, ledger, asof, positive[category])
f.write("</div>")
f.write("</div>")
f.write("<div class='row'>")
for categories in [["Income"],["Expenses"]]:
f.write("<div class='six columns'>")
for category in categories:
make_category(f, org, category, balances, ledger, asof, positive[category])
f.write("</div>")
f.write("</div>")
f.write("</div>")
f.write("</body>")
f.write("</html>")
print "Report written to %s" % os.path.join(destdir,"report.html")