diff --git a/gratipay/billing/payday.py b/gratipay/billing/payday.py index b05a7920b2..d01e7db4d9 100644 --- a/gratipay/billing/payday.py +++ b/gratipay/billing/payday.py @@ -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)) diff --git a/gratipay/models/participant.py b/gratipay/models/participant.py index 32086fb002..7979ad5eb4 100644 --- a/gratipay/models/participant.py +++ b/gratipay/models/participant.py @@ -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) if update_team: # Update receiving amount of team team.update_receiving(cursor) @@ -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(""" diff --git a/sql/payday.sql b/sql/payday.sql index 9d60cd427a..cd1b581254 100644 --- a/sql/payday.sql +++ b/sql/payday.sql @@ -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(( @@ -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 @@ -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; diff --git a/tests/py/test_billing_payday.py b/tests/py/test_billing_payday.py index 1cf5e9d9aa..878f63fd61 100644 --- a/tests/py/test_billing_payday.py +++ b/tests/py/test_billing_payday.py @@ -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')