Repository: liberapay/liberapay.com

Issue:
Fix update_cached_amounts - part 2
Completes #255.



Discussion:
Better, but still slightly off. :-/


Resolving patch:
diff --git a/liberapay/billing/payday.py b/liberapay/billing/payday.py
--- a/liberapay/billing/payday.py
+++ b/liberapay/billing/payday.py
@@ -468,8 +468,9 @@ def update_cached_amounts(self):
               FROM ( SELECT p2.id
                           , COALESCE((
                                 SELECT sum(amount)
-                                  FROM payday_transfers t
+                                  FROM payday_tips t
                                  WHERE t.tipper = p2.id
+                                   AND t.is_funded
                             ), 0) AS giving
                        FROM participants p2
                    ) p2
@@ -493,10 +494,11 @@ def update_cached_amounts(self):
             UPDATE participants p
                SET receiving = p2.receiving
               FROM ( SELECT p2.id
-                          , COALESCE((
+                          , p2.taking + COALESCE((
                                 SELECT sum(amount)
-                                  FROM payday_transfers t
-                                 WHERE t.tippee = p2.id OR t.team = p2.id
+                                  FROM payday_tips t
+                                 WHERE t.tippee = p2.id
+                                   AND t.is_funded
                             ), 0) AS receiving
                        FROM participants p2
                    ) p2


Test patch:
diff --git a/tests/py/test_billing_payday.py b/tests/py/test_billing_payday.py
--- a/tests/py/test_billing_payday.py
+++ b/tests/py/test_billing_payday.py
@@ -44,7 +44,7 @@ def test_update_cached_amounts(self):
         alice.set_tip_to(dana, '3.00')
         alice.set_tip_to(bob, '6.00')
         alice.set_tip_to(emma, '0.50')
-        alice.set_tip_to(team, '1.00')
+        alice.set_tip_to(team, '1.20')
         bob.set_tip_to(alice, '5.00')
         team.add_member(bob)
         team.set_take_for(bob, D('1.00'), bob)
@@ -58,7 +58,7 @@ def check():
             carl = Participant.from_username('carl')
             dana = Participant.from_username('dana')
             emma = Participant.from_username('emma')
-            assert alice.giving == D('10.00')
+            assert alice.giving == D('10.20')
             assert alice.receiving == D('5.00')
             assert bob.giving == D('7.00')
             assert bob.receiving == D('7.00')
@@ -70,10 +70,10 @@ def check():
             assert emma.receiving == D('0.50')
             assert emma.npatrons == 1
             funded_tips = self.db.all("SELECT amount FROM tips WHERE is_funded ORDER BY id")
-            assert funded_tips == [3, 6, 0.5, 1, 5, 2]
+            assert funded_tips == [3, 6, 0.5, D('1.20'), 5, 2]
 
             team = Participant.from_username('team')
-            assert team.receiving == D('1.00')
+            assert team.receiving == D('1.20')
             assert team.npatrons == 1
 
             janet = self.janet.refetch()
@@ -403,7 +403,7 @@ def test_it_notifies_participants(self):
         assert david.balance == D('4.73')
         janet = self.janet.refetch()
         assert janet.balance == D('1.77')
-        assert janet.giving == D('0.23')
+        assert janet.giving == D('0.25')
         emails = self.get_emails()
         assert len(emails) == 3
         assert emails[0]['to'][0]['email'] == self.david.email