Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Add payments to history page
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 16, 2015
1 parent f5c4c8b commit d1b145a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
34 changes: 26 additions & 8 deletions gratipay/utils/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,33 @@ def iter_payday_events(db, participant, year=None):
WHERE participant=%(username)s
AND extract(year from timestamp) = %(year)s
""", locals(), back_as=dict)
payments = db.all("""
SELECT *
FROM payments
WHERE participant=%(username)s
AND extract(year from timestamp) = %(year)s
""", locals(), back_as=dict)
transfers = db.all("""
SELECT *
FROM transfers
WHERE (tipper=%(username)s OR tippee=%(username)s)
AND extract(year from timestamp) = %(year)s
""", locals(), back_as=dict)

if not (exchanges or transfers):
if not (exchanges or payments or transfers):
return

if transfers:
yield dict(
kind='totals',
given=sum(t['amount'] for t in transfers if t['tipper'] == username and t['context'] != 'take'),
received=sum(t['amount'] for t in transfers if t['tippee'] == username),
)
if payments or transfers:
payments_given = sum([p['amount'] for p in payments if p['direction'] == 'to-team'])
payments_received = sum([p['amount'] for p in payments \
if p['direction'] == 'to-participant'])
transfers_given = sum(t['amount'] for t in transfers \
if t['tipper'] == username and t['context'] != 'take')
transfers_received = sum(t['amount'] for t in transfers if t['tippee'] == username)
yield dict( kind='totals'
, given=payments_given + transfers_given
, received=payments_received + transfers_received
)

payday_dates = db.all("""
SELECT ts_start::date
Expand All @@ -101,7 +112,7 @@ def iter_payday_events(db, participant, year=None):
balance = get_end_of_year_balance(db, participant, year, current_year)
prev_date = None
get_timestamp = lambda e: e['timestamp']
events = sorted(exchanges+transfers, key=get_timestamp, reverse=True)
events = sorted(exchanges+payments+transfers, key=get_timestamp, reverse=True)
for event in events:

event['balance'] = balance
Expand Down Expand Up @@ -129,6 +140,13 @@ def iter_payday_events(db, participant, year=None):
kind = 'credit'
if event['status'] != 'failed':
balance -= event['amount'] - event['fee']
elif 'direction' in event:
kind = 'payment'
if event['direction'] == 'to-participant':
balance -= event['amount']
else:
assert event['direction'] == 'to-team'
balance += event['amount']
else:
kind = 'transfer'
if event['tippee'] == username:
Expand Down
6 changes: 5 additions & 1 deletion tests/py/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def make_history(harness):

class TestHistory(Harness):

@pytest.mark.xfail(reason="haven't migrated transfer_takes yet")
def test_iter_payday_events(self):
Payday.start().run()
team = self.make_participant('team', number='plural', claimed_time='now')
Expand Down Expand Up @@ -127,6 +126,11 @@ def setUp(self):
def test_participant_can_view_history(self):
assert self.client.GET('/~alice/history/', auth_as='alice').code == 200

def test_history_page_includes_payments(self):
body = self.client.GET('/~alice/history/', auth_as='alice').body
import pdb; pdb.set_trace()
assert '' in body

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3454')
def test_admin_can_view_closed_participant_history(self):
self.make_participant('bob', claimed_time='now', is_admin=True)
Expand Down
27 changes: 27 additions & 0 deletions www/~/%username/history/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ else:
{% endif %}
</td>
</tr>
{% elif event['kind'] == 'payment' %}
<tr>
<td class="bank"></td>
<td class="card"></td>
<td class="fees"></td>

{% if event['direction'] == 'to-participant' %}
<td class="credits">{{ event['amount'] }}</td>
<td class="debits"></td>
{% else %}
<td class="credits"></td>
<td class="debits">{{ event['amount'] }}</td>
{% endif %}

<td class="balance">{{ event['balance'] }}</td>

<td class="status"></td>
<td class="notes">
{% set context = event['context'] %}
{% if event['direction'] == 'to-participant' %}
taken from <a href="/{{ event['team'] }}/">{{ event['team'] }}</a>
{% else %}
given to <a href="/{{ event['team'] }}/">{{ event['team'] }}</a>
{% endif %}
</td>

</tr>
{% elif event['kind'] == 'transfer' %}
<tr>
<td class="bank"></td>
Expand Down

0 comments on commit d1b145a

Please sign in to comment.