1
0
Fork 0
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:
A.Avenel 2013-04-07 22:14:32 +02:00
parent 1fa0cff180
commit 0d7c82b122
4 changed files with 9 additions and 9 deletions

View file

@ -49,7 +49,7 @@ class Project(db.Model):
return balances 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""" """Return a list of transactions that could be made to settle the bill"""
credits, debts, transactions = [],[],[] credits, debts, transactions = [],[],[]
# Create lists of credits and debts # Create lists of credits and debts
@ -63,17 +63,17 @@ class Project(db.Model):
match = self.exactmatch(credit["balance"], debts) match = self.exactmatch(credit["balance"], debts)
if match: if match:
for m in 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) debts.remove(m)
credits.remove(credit) credits.remove(credit)
# Split any remaining debts & credits # Split any remaining debts & credits
while credits and debts: while credits and debts:
if credits[0]["balance"] > debts[0]["balance"]: 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"] credits[0]["balance"] = credits[0]["balance"] - debts[0]["balance"]
del debts[0] del debts[0]
else: 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"] debts[0]["balance"] = debts[0]["balance"] - credits[0]["balance"]
del credits[0] del credits[0]
return transactions return transactions

View file

@ -34,9 +34,9 @@
<thead><tr><th>{{ _("Who pays?") }}</th><th>{{ _("To whom?") }}</th><th>{{ _("How much?") }}</th></tr></thead> <thead><tr><th>{{ _("Who pays?") }}</th><th>{{ _("To whom?") }}</th><th>{{ _("How much?") }}</th></tr></thead>
<tbody> <tbody>
{% for bill in bills %} {% 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.ower }}</td>
<td>{{ bill.payer }}</td> <td>{{ bill.receiver }}</td>
<td>{{ "%0.2f"|format(bill.amount) }}</td> <td>{{ "%0.2f"|format(bill.amount) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -510,12 +510,12 @@ class BudgetTestCase(TestCase):
'amount': '10', 'amount': '10',
}) })
project = models.Project.query.get('raclette') project = models.Project.query.get('raclette')
transactions = project.settle_bills() transactions = project.get_transactions_to_settle_bill()
members = defaultdict(int) members = defaultdict(int)
#We should have the same values between transactions and project balances #We should have the same values between transactions and project balances
for t in transactions: for t in transactions:
members[t['ower']]-=t['amount'] members[t['ower']]-=t['amount']
members[t['payer']]+=t['amount'] members[t['receiver']]+=t['amount']
balance = models.Project.query.get("raclette").balance balance = models.Project.query.get("raclette").balance
for m, a in members.items(): for m, a in members.items():
self.assertEqual(a, balance[m.id]) self.assertEqual(a, balance[m.id])

View file

@ -386,7 +386,7 @@ def change_lang(lang):
@main.route("/<project_id>/settle_bills") @main.route("/<project_id>/settle_bills")
def settle_bill(): def settle_bill():
"""Compute the sum each one have to pay to each other and display it""" """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) return render_template("settle_bills.html", bills=bills)