Repository: gratipay/gratipay.com

Issue:
members page does not reflect available take from account balance
when a balance is available, the members page of a team does not reflect that members will be paid from the team balance.
![balance](https://cloud.githubusercontent.com/assets/1970586/3528854/761a8492-0793-11e4-8f7a-d51c5ae9808f.png)

members page does not reflect available take from account balance
when a balance is available, the members page of a team does not reflect that members will be paid from the team balance.
![balance](https://cloud.githubusercontent.com/assets/1970586/3528854/761a8492-0793-11e4-8f7a-d51c5ae9808f.png)



Discussion:
might be related to 
https://github.com/gittip/www.gittip.com/pull/2527 
or
https://github.com/gittip/www.gittip.com/issues/112

Also, it doesn't look like the back-balance is actually getting paid. I'm one of the people in the bottom of this list in the screenshot, and the group tip + individual tips I think I'm currently receiving don't seem to add up at all.

@YesCT @davereid Thanks for the report and sorry for the trouble!

@Changaco Up for taking a look at this?

@whit537 I'm already looking. :-)

!m @Changaco :-)

might be related to 
https://github.com/gittip/www.gittip.com/pull/2527 
or
https://github.com/gittip/www.gittip.com/issues/112

Also, it doesn't look like the back-balance is actually getting paid. I'm one of the people in the bottom of this list in the screenshot, and the group tip + individual tips I think I'm currently receiving don't seem to add up at all.

@YesCT @davereid Thanks for the report and sorry for the trouble!

@Changaco Up for taking a look at this?

@whit537 I'm already looking. :-)

!m @Changaco :-)


Resolving patch:
diff --git a/gittip/models/_mixin_team.py b/gittip/models/_mixin_team.py
--- a/gittip/models/_mixin_team.py
+++ b/gittip/models/_mixin_team.py
@@ -248,11 +248,12 @@ def compute_actual_takes(self, cursor=None):
         actual_takes = OrderedDict()
         nominal_takes = self.get_takes(cursor=cursor)
         nominal_takes.append(self.get_team_take(cursor=cursor))
-        budget = balance = self.receiving
+        budget = balance = self.balance + self.receiving
         for take in nominal_takes:
             nominal_amount = take['nominal_amount'] = take.pop('amount')
             actual_amount = take['actual_amount'] = min(nominal_amount, balance)
-            balance -= actual_amount
+            if take['member'] != self.username:
+                balance -= actual_amount
             take['balance'] = balance
             take['percentage'] = (actual_amount / budget) if budget > 0 else 0
             actual_takes[take['member']] = take


Test patch:
diff --git a/tests/py/test_take.py b/tests/py/test_take.py
--- a/tests/py/test_take.py
+++ b/tests/py/test_take.py
@@ -94,6 +94,34 @@ def test_get_members(self):
         assert members[0]['take'] == 42
         assert members[0]['balance'] == 58
 
+    def test_compute_actual_takes_counts_the_team_balance(self):
+        team = self.make_team(balance=D('59.46'))
+        alice = self.make_participant('alice', take_last_week='100.00', claimed_time='now')
+        team.add_member(alice)
+        team.set_take_for(alice, D('142.00'), team)
+        takes = team.compute_actual_takes().values()
+        assert len(takes) == 2
+        assert takes[0]['member'] == 'alice'
+        assert takes[0]['actual_amount'] == 142
+        assert takes[0]['balance'] == D('17.46')
+        assert takes[1]['member'] == TEAM
+        assert takes[1]['actual_amount'] == 0
+        assert takes[1]['balance'] == D('17.46')
+
+    def test_compute_actual_takes_gives_correct_final_balance(self):
+        team = self.make_team(balance=D('53.72'))
+        alice = self.make_participant('alice', take_last_week='100.00', claimed_time='now')
+        team.add_member(alice)
+        team.set_take_for(alice, D('86.00'), team)
+        takes = team.compute_actual_takes().values()
+        assert len(takes) == 2
+        assert takes[0]['member'] == 'alice'
+        assert takes[0]['actual_amount'] == 86
+        assert takes[0]['balance'] == D('67.72')
+        assert takes[1]['member'] == TEAM
+        assert takes[1]['actual_amount'] == 14
+        assert takes[1]['balance'] == D('67.72')
+
     def test_taking_and_receiving_are_updated_correctly(self):
         team = self.make_team()
         alice = self.make_participant('alice', take_last_week='40.00', claimed_time='now')