-
Notifications
You must be signed in to change notification settings - Fork 0
/
member_report.py
executable file
·72 lines (54 loc) · 2 KB
/
member_report.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
#!/bin/sh
"exec" "poetry" "run" "$0" "$@"
# coding: utf8
import logging
import sys
import typing
from datetime import date
from dateutil.relativedelta import relativedelta
import db
import util
from schema.fee_entry import FeeEntry
from schema.member import Member
import config
logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO,
format="%(levelname) 7s %(message)s")
def main(args, to=sys.stdout, months=12):
if '--debug' in args:
logging.getLogger('').setLevel(logging.DEBUG)
db.init()
end_month = date.today()
if end_month.day <= config.report_month_end:
end_month = end_month - relativedelta(months=1)
end_month = end_month.replace(day=1)
start_month = end_month - relativedelta(months=months)
with db.tx() as session:
members = session.query(Member) \
.order_by(Member.last_name, Member.first_name)
members_table(session, members, start_month, end_month, to)
def members_table(session, members, start_month, end_month, to: typing.TextIO=sys.stdout):
months = list(util.months(start_month, end_month))
members = members.all()
sys.stderr.flush()
to.flush()
to.write(' {name:^20} | '.format(name="Name"))
for month, next_month in months:
to.write(' {:2} '.format(month.month))
to.write('\n' + '-' * (20+3 + 5*len(months)) + '\n')
for member in members:
to.write('{:2} '.format(member.id))
to.write('{:>20.20} | '.format(member.name))
for month, next_month in months:
fees = session.query(FeeEntry)\
.filter(FeeEntry.member_id == member.id)\
.filter(FeeEntry.month == month).all()
if len(fees) == 0:
to.write(' ')
else:
to.write(' {:>2.0f}{:1} '.format(
sum([f.fee for f in fees]),
'' if len(fees) == 1 else len(fees)
))
to.write('\n')
if __name__ == "__main__":
sys.exit(main(sys.argv))