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

Cap unfunded dues #3981

Merged
merged 5 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BEGIN;
UPDATE payment_instructions SET due = 0 WHERE amount = 0 AND due != 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. Do we do this normally? When I drop my tip to zero, does my due get reset as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


UPDATE payment_instructions pi
SET due = 0
WHERE due > 9.41
AND ( -- Doesn't have a valid CC
SELECT COUNT(*)
FROM current_exchange_routes r
WHERE r.participant = (SELECT id FROM participants WHERE username = pi.participant)
AND network = 'braintree-cc'
AND error = ''
) = 0;
END;
4 changes: 3 additions & 1 deletion sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ CREATE OR REPLACE FUNCTION process_payment_instruction() RETURNS trigger AS $$
FROM payday_participants p
WHERE username = NEW.participant
);

IF (NEW.amount + NEW.due <= participant.new_balance OR participant.card_hold_ok) THEN
EXECUTE pay(NEW.participant, NEW.team, NEW.amount + NEW.due, 'to-team');
RETURN NEW;
ELSE
ELSIF participant.has_credit_card THEN
EXECUTE park(NEW.participant, NEW.team, NEW.amount + NEW.due);
RETURN NULL;
END IF;

RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Expand Down
22 changes: 22 additions & 0 deletions tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ def test_payday_preserves_due_until_charged(self, fch):
assert obama.balance == D('0.00')
assert obama.get_due('TheEnterprise') == D('0.00')

@mock.patch.object(Payday, 'fetch_card_holds')
def test_payday_only_adds_to_dues_if_valid_cc_exists(self, fch):
Enterprise = self.make_team(is_approved=True)
self.obama.set_payment_instruction(Enterprise, '4.00')
assert self.obama.get_due('TheEnterprise') == D('0.00')

fch.return_value = {}
Payday.start().run() # payday 0

assert self.obama.get_due('TheEnterprise') == D('4.00')

fch.return_value = {}
self.obama_route.update_error("failed") # card fails
Payday.start().run() # payday 1

assert self.obama.get_due('TheEnterprise') == D('4.00')

fch.return_value = {}
Payday.start().run() # payday 2

assert self.obama.get_due('TheEnterprise') == D('4.00')

@mock.patch.object(Payday, 'fetch_card_holds')
def test_payday_does_not_move_money_below_min_charge(self, fch):
Enterprise = self.make_team(is_approved=True)
Expand Down