mirror of
https://github.com/YunoHost-Apps/ihatemoney_ynh.git
synced 2024-09-03 19:26:15 +02:00
More code cleanup for "settle bills"
This commit is contained in:
parent
1fa0cff180
commit
0d7c82b122
4 changed files with 9 additions and 9 deletions
|
@ -49,7 +49,7 @@ class Project(db.Model):
|
|||
|
||||
return balances
|
||||
|
||||
def settle_bills(self):
|
||||
def get_transactions_to_settle_bill(self):
|
||||
"""Return a list of transactions that could be made to settle the bill"""
|
||||
credits, debts, transactions = [],[],[]
|
||||
# Create lists of credits and debts
|
||||
|
@ -63,17 +63,17 @@ class Project(db.Model):
|
|||
match = self.exactmatch(credit["balance"], debts)
|
||||
if match:
|
||||
for m in match:
|
||||
transactions.append({"ower": m["person"], "payer": credit["person"], "amount": m["balance"]})
|
||||
transactions.append({"ower": m["person"], "receiver": credit["person"], "amount": m["balance"]})
|
||||
debts.remove(m)
|
||||
credits.remove(credit)
|
||||
# Split any remaining debts & credits
|
||||
while credits and debts:
|
||||
if credits[0]["balance"] > debts[0]["balance"]:
|
||||
transactions.append({"ower": debts[0]["person"], "payer": credits[0]["person"], "amount": debts[0]["balance"]})
|
||||
transactions.append({"ower": debts[0]["person"], "receiver": credits[0]["person"], "amount": debts[0]["balance"]})
|
||||
credits[0]["balance"] = credits[0]["balance"] - debts[0]["balance"]
|
||||
del debts[0]
|
||||
else:
|
||||
transactions.append({"ower": debts[0]["person"], "payer": credits[0]["person"], "amount": credits[0]["balance"]})
|
||||
transactions.append({"ower": debts[0]["person"], "receiver": credits[0]["person"], "amount": credits[0]["balance"]})
|
||||
debts[0]["balance"] = debts[0]["balance"] - credits[0]["balance"]
|
||||
del credits[0]
|
||||
return transactions
|
||||
|
|
|
@ -34,9 +34,9 @@
|
|||
<thead><tr><th>{{ _("Who pays?") }}</th><th>{{ _("To whom?") }}</th><th>{{ _("How much?") }}</th></tr></thead>
|
||||
<tbody>
|
||||
{% for bill in bills %}
|
||||
<tr class="{{ loop.cycle("odd", "even") }}" owers={{bill.owers|join(',','id')}} payer={{bill.payer.id}}>
|
||||
<tr class="{{ loop.cycle("odd", "even") }}" receiver={{bill.receiver.id}}>
|
||||
<td>{{ bill.ower }}</td>
|
||||
<td>{{ bill.payer }}</td>
|
||||
<td>{{ bill.receiver }}</td>
|
||||
<td>{{ "%0.2f"|format(bill.amount) }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
|
@ -510,12 +510,12 @@ class BudgetTestCase(TestCase):
|
|||
'amount': '10',
|
||||
})
|
||||
project = models.Project.query.get('raclette')
|
||||
transactions = project.settle_bills()
|
||||
transactions = project.get_transactions_to_settle_bill()
|
||||
members = defaultdict(int)
|
||||
#We should have the same values between transactions and project balances
|
||||
for t in transactions:
|
||||
members[t['ower']]-=t['amount']
|
||||
members[t['payer']]+=t['amount']
|
||||
members[t['receiver']]+=t['amount']
|
||||
balance = models.Project.query.get("raclette").balance
|
||||
for m, a in members.items():
|
||||
self.assertEqual(a, balance[m.id])
|
||||
|
|
|
@ -386,7 +386,7 @@ def change_lang(lang):
|
|||
@main.route("/<project_id>/settle_bills")
|
||||
def settle_bill():
|
||||
"""Compute the sum each one have to pay to each other and display it"""
|
||||
bills = g.project.settle_bills()
|
||||
bills = g.project.get_transactions_to_settle_bill()
|
||||
return render_template("settle_bills.html", bills=bills)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue