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

Commit

Permalink
Refined and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rorepo committed Sep 3, 2015
1 parent aa12b9d commit 2493f71
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
26 changes: 10 additions & 16 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,16 @@ def f(p):
return 1
else:
holds[p.id] = hold
threaded_map(f, participants)
n_failures = sum(filter(None, threaded_map(f, participants)))


# Record the number of failures
cursor.one("""
UPDATE paydays
SET ncc_failing = %s
WHERE ts_end='1970-01-01T00:00:00+00'::timestamptz
RETURNING id
""", (n_failures,), default=NoPayday)

# Update the values of card_hold_ok in our temporary table
if not holds:
Expand Down Expand Up @@ -347,21 +356,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)
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
25 changes: 9 additions & 16 deletions sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,13 @@ 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);
--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;

ALTER TABLE payday_participants ADD COLUMN giving_today numeric(35,2);
UPDATE payday_participants pp
Expand Down Expand Up @@ -141,7 +134,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 +169,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

0 comments on commit 2493f71

Please sign in to comment.