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

Commit

Permalink
Modified to change handling of existing and updated due values
Browse files Browse the repository at this point in the history
current_payment_instructions view used in place of the new
payment_instructions_due table which has now been removed

New update_due method added to participant to carry over existing
due values to new (modified) payment instructions
  • Loading branch information
rorepo committed Sep 4, 2015
1 parent aa12b9d commit 105082b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
15 changes: 0 additions & 15 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,6 @@ def update_balances(cursor):
SELECT *, (SELECT id FROM paydays WHERE extract(year from ts_end) = 1970)
FROM payday_payments;
""")
# Copy due value back to payment_instructions
cursor.run("""
UPDATE payment_instructions pi
SET due = pi2.due
FROM payment_instructions_due pi2
WHERE pi.id = pi2.id
""")
# Reset older due values to zero
cursor.run("""
UPDATE payment_instructions pi
SET due = '0'
WHERE pi.id NOT IN (
SELECT id
FROM payment_instructions_due pi2)
""")

log("Updated the balances of %i participants." % len(participants))

Expand Down
28 changes: 28 additions & 0 deletions gratipay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,13 @@ def set_payment_instruction(self, team, amount, update_self=True, update_team=Tr
"""
args = dict(participant=self.username, team=team.slug, amount=amount)
t = (cursor or self.db).one(NEW_PAYMENT_INSTRUCTION, args)
t_dict = t._asdict()

if update_self:
# Update giving amount of participant
self.update_giving(cursor)
# Carry over any existing due
self.update_due(t_dict['team'],t_dict['id'],cursor)

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Sep 7, 2015

Contributor

I'm not sure this is necessary. When I added the get_due method in aa12b9d, I also added the ability to update the is_funded and due fields of the payment_instructions table via the current_payment_instructions view (reusing a pattern from the old current_tips view). It doesn't reset due (or is_funded on non-current payment instructions, but that's not strictly necessary anyway (right?).

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Sep 7, 2015

Contributor

I went back and checked, and our test suite does exercise this functionality:

  • Modify update_payment_instruction() to return early or otherwise not do its work.
  • make test-schema
  • Run the test_billing_payday.py tests.

I see three failures.

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Sep 7, 2015

Contributor

Oh! This is in Participant.set_payment_instruction, and not during payday.

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Sep 7, 2015

Contributor

I commented out this line, and did find a failure in our test suite. I wanted to make sure we're exercising this functionality.

if update_team:
# Update receiving amount of team
team.update_receiving(cursor)
Expand Down Expand Up @@ -1002,6 +1005,31 @@ def update_giving(self, cursor=None):

return updated

def update_due(self, team, id, cursor=None):
"""Transfer existing due value to newly inserted record
"""
# Copy due to new record
(cursor or self.db).run("""
UPDATE payment_instructions p
SET due = COALESCE((
SELECT due
FROM payment_instructions s
WHERE participant=%(username)s
AND team = %(team)s
AND due > 0
), 0)
WHERE p.id = %(id)s
""", dict(username=self.username,team=team,id=id))

# Reset older due values to 0
(cursor or self.db).run("""
UPDATE payment_instructions p
SET due = 0
WHERE participant = %(username)s
AND team = %(team)s
AND due > 0
AND p.id != %(id)s
""", dict(username=self.username,team=team,id=id))

def update_taking(self, cursor=None):
(cursor or self.db).run("""
Expand Down
19 changes: 2 additions & 17 deletions sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,6 @@ CREATE INDEX ON payday_payment_instructions (participant);
CREATE INDEX ON payday_payment_instructions (team);
ALTER TABLE payday_payment_instructions ADD COLUMN is_funded boolean;

UPDATE payday_payment_instructions ppi
SET due = s.due
FROM (SELECT participant, team, SUM(due) AS due
FROM payment_instructions
GROUP BY participant, team) s
WHERE ppi.participant = s.participant
AND ppi.team = s.team;

DROP TABLE IF EXISTS payment_instructions_due;
CREATE TABLE payment_instructions_due AS
SELECT * FROM payday_payment_instructions;

CREATE INDEX ON payment_instructions_due (participant);
CREATE INDEX ON payment_instructions_due (team);

ALTER TABLE payday_participants ADD COLUMN giving_today numeric(35,2);
UPDATE payday_participants pp
SET giving_today = COALESCE((
Expand Down Expand Up @@ -141,7 +126,7 @@ RETURNS void AS $$
UPDATE payday_teams
SET balance = (balance + team_delta)
WHERE slug = $2;
UPDATE payment_instructions_due
UPDATE current_payment_instructions
SET due = 0
WHERE participant = $1
AND team = $2
Expand Down Expand Up @@ -176,7 +161,7 @@ RETURNS void AS $$
BEGIN
IF ($3 = 0) THEN RETURN; END IF;

UPDATE payment_instructions_due
UPDATE current_payment_instructions
SET due = $3
WHERE participant = $1
AND team = $2;
Expand Down
1 change: 0 additions & 1 deletion tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def test_payday_preserves_due_until_charged(self, fch):
fch.return_value = {}
Payday.start().run() # payday 4


obama = Participant.from_username('obama')
picard = Participant.from_username('picard')

Expand Down

0 comments on commit 105082b

Please sign in to comment.