mirror of
https://github.com/YunoHost-Apps/ihatemoney_ynh.git
synced 2024-09-03 19:26:15 +02:00
use "member.id" instead of "member"
This commit is contained in:
parent
b410e48851
commit
1fa0cff180
5 changed files with 29 additions and 31 deletions
|
@ -45,21 +45,19 @@ class Project(db.Model):
|
|||
|
||||
for person in self.members:
|
||||
balance = should_receive[person] - should_pay[person]
|
||||
balances[person] = round(balance, 2)
|
||||
balances[person.id] = round(balance, 2)
|
||||
|
||||
return balances
|
||||
|
||||
def settle_bill(self):
|
||||
def settle_bills(self):
|
||||
"""Return a list of transactions that could be made to settle the bill"""
|
||||
balances = self.balance
|
||||
credits, debts = list(), list()
|
||||
transactions = list()
|
||||
credits, debts, transactions = [],[],[]
|
||||
# Create lists of credits and debts
|
||||
for person in balances.keys():
|
||||
if balances[person] > 0:
|
||||
credits.append({"person": person, "balance": balances[person]})
|
||||
elif balances[person] < 0:
|
||||
debts.append({"person": person, "balance": -balances[person]})
|
||||
for person in self.members:
|
||||
if self.balance[person.id] > 0:
|
||||
credits.append({"person": person, "balance": self.balance[person.id]})
|
||||
elif self.balance[person.id] < 0:
|
||||
debts.append({"person": person, "balance": -self.balance[person.id]})
|
||||
# Try and find exact matches
|
||||
for credit in credits:
|
||||
match = self.exactmatch(credit["balance"], debts)
|
||||
|
@ -83,7 +81,7 @@ class Project(db.Model):
|
|||
def exactmatch(self, credit, debts):
|
||||
"""Recursively try and find subsets of 'debts' whose sum is equal to credit"""
|
||||
if not debts:
|
||||
return []
|
||||
return None
|
||||
if debts[0]["balance"] > credit:
|
||||
return self.exactmatch(credit, debts[1:])
|
||||
elif debts[0]["balance"] == credit:
|
||||
|
|
|
@ -61,20 +61,20 @@
|
|||
<div id="table_overflow">
|
||||
<table class="balance table">
|
||||
{% set balance = g.project.balance %}
|
||||
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %}
|
||||
<tr id="bal-member-{{ member }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
|
||||
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
|
||||
<tr id="bal-member-{{ member.id }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
|
||||
<td class="balance-name">{{ member.name }}</td>
|
||||
{% if member.activated %}
|
||||
<td>
|
||||
<form class="action delete" action="{{ url_for(".remove_member", member_id=member) }}" method="POST">
|
||||
<form class="action delete" action="{{ url_for(".remove_member", member_id=member.id) }}" method="POST">
|
||||
<button type="submit">{{ _("delete") }}</button></form></td>
|
||||
{% else %}
|
||||
<td>
|
||||
<form class="action reactivate" action="{{ url_for(".reactivate", member_id=member) }}" method="POST">
|
||||
<form class="action reactivate" action="{{ url_for(".reactivate", member_id=member.id) }}" method="POST">
|
||||
<button type="submit">{{ _("reactivate") }}</button></form></td>
|
||||
{% endif %}
|
||||
<td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member] < 0 %}negative{% endif %}">
|
||||
{% if balance[member] > 0 %}+{% endif %}{{ balance[member] }}
|
||||
<td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member.id] < 0 %}negative{% endif %}">
|
||||
{% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
{% endblock %}
|
||||
{% block navbar %}
|
||||
<li><a href="{{ url_for(".list_bills") }}">{{ _("Bills") }}</a></li>
|
||||
<li class="active"><a href="{{ url_for(".settle_bill") }}">{{ _("Settle") }}</a></li>
|
||||
<li class="active"><a href="{{ url_for(".settle_bills") }}">{{ _("Settle") }}</a></li>
|
||||
{% endblock %}
|
||||
|
||||
{% block sidebar %}
|
||||
|
@ -14,11 +14,11 @@
|
|||
<div id="table_overflow">
|
||||
<table class="balance table">
|
||||
{% set balance = g.project.balance %}
|
||||
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %}
|
||||
<tr id="bal-member-{{ member }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
|
||||
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
|
||||
<tr id="bal-member-{{ member.id }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
|
||||
<td class="balance-name">{{ member.name }}</td>
|
||||
<td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member] < 0 %}negative{% endif %}">
|
||||
{% if balance[member] > 0 %}+{% endif %}{{ balance[member] }}
|
||||
<td class="balance-value {% if balance[member.id] > 0 %}positive{% elif balance[member.id] < 0 %}negative{% endif %}">
|
||||
{% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
|
@ -436,9 +436,9 @@ class BudgetTestCase(TestCase):
|
|||
|
||||
balance = models.Project.query.get("raclette").balance
|
||||
result = {}
|
||||
result[models.Project.query.get("raclette").members[0]] = 8.12
|
||||
result[models.Project.query.get("raclette").members[1]] = 0.0
|
||||
result[models.Project.query.get("raclette").members[2]] = -8.12
|
||||
result[models.Project.query.get("raclette").members[0].id] = 8.12
|
||||
result[models.Project.query.get("raclette").members[1].id] = 0.0
|
||||
result[models.Project.query.get("raclette").members[2].id] = -8.12
|
||||
self.assertDictEqual(balance, result)
|
||||
|
||||
def test_edit_project(self):
|
||||
|
@ -472,7 +472,7 @@ class BudgetTestCase(TestCase):
|
|||
|
||||
def test_settle_page(self):
|
||||
self.post_project("raclette")
|
||||
response = self.app.get("/raclette/settle_bill")
|
||||
response = self.app.get("/raclette/settle_bills")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_settle(self):
|
||||
|
@ -510,7 +510,7 @@ class BudgetTestCase(TestCase):
|
|||
'amount': '10',
|
||||
})
|
||||
project = models.Project.query.get('raclette')
|
||||
transactions = project.settle_bill()
|
||||
transactions = project.settle_bills()
|
||||
members = defaultdict(int)
|
||||
#We should have the same values between transactions and project balances
|
||||
for t in transactions:
|
||||
|
@ -518,7 +518,7 @@ class BudgetTestCase(TestCase):
|
|||
members[t['payer']]+=t['amount']
|
||||
balance = models.Project.query.get("raclette").balance
|
||||
for m, a in members.items():
|
||||
self.assertEqual(a, balance[m])
|
||||
self.assertEqual(a, balance[m.id])
|
||||
return
|
||||
|
||||
|
||||
|
|
|
@ -383,11 +383,11 @@ def change_lang(lang):
|
|||
return redirect(request.headers.get('Referer') or url_for('.home'))
|
||||
|
||||
|
||||
@main.route("/<project_id>/settle_bill")
|
||||
@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_bill()
|
||||
return render_template("settle_bill.html", bills=bills)
|
||||
bills = g.project.settle_bills()
|
||||
return render_template("settle_bills.html", bills=bills)
|
||||
|
||||
|
||||
@main.route("/<project_id>/archives/create", methods=["GET", "POST"])
|
||||
|
|
Loading…
Reference in a new issue