mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-17 17:51:50 +02:00
Compare commits
1 commit
488e4e2fc9
...
596e0a6eb6
Author | SHA1 | Date | |
---|---|---|---|
596e0a6eb6 |
6 changed files with 49 additions and 204 deletions
|
@ -14,8 +14,6 @@ from wtforms.fields import (
|
||||||
BooleanField,
|
BooleanField,
|
||||||
DateField,
|
DateField,
|
||||||
DecimalField,
|
DecimalField,
|
||||||
HiddenField,
|
|
||||||
IntegerField,
|
|
||||||
Label,
|
Label,
|
||||||
PasswordField,
|
PasswordField,
|
||||||
SelectField,
|
SelectField,
|
||||||
|
@ -471,22 +469,6 @@ class BillForm(FlaskForm):
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
|
||||||
class HiddenCommaDecimalField(HiddenField, CommaDecimalField):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class HiddenIntegerField(HiddenField, IntegerField):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class SettlementForm(FlaskForm):
|
|
||||||
"""Used internally for validation, not directly visible to users"""
|
|
||||||
|
|
||||||
amount = HiddenCommaDecimalField("Amount", validators=[DataRequired()])
|
|
||||||
sender_id = HiddenIntegerField("Sender", validators=[DataRequired()])
|
|
||||||
receiver_id = HiddenIntegerField("Receiver", validators=[DataRequired()])
|
|
||||||
|
|
||||||
|
|
||||||
class MemberForm(FlaskForm):
|
class MemberForm(FlaskForm):
|
||||||
name = StringField(_("Name"), validators=[
|
name = StringField(_("Name"), validators=[
|
||||||
DataRequired()], filters=[strip_filter])
|
DataRequired()], filters=[strip_filter])
|
||||||
|
|
|
@ -466,10 +466,6 @@ class Project(db.Model):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return person
|
return person
|
||||||
|
|
||||||
def has_member(self, member_id):
|
|
||||||
person = Person.query.get(member_id, self)
|
|
||||||
return person is not None
|
|
||||||
|
|
||||||
def remove_project(self):
|
def remove_project(self):
|
||||||
# We can't import at top level without circular dependencies
|
# We can't import at top level without circular dependencies
|
||||||
from ihatemoney.history import purge_history
|
from ihatemoney.history import purge_history
|
||||||
|
|
|
@ -1,49 +1,33 @@
|
||||||
{% extends "sidebar_table_layout.html" %} {% block sidebar %}
|
{% extends "sidebar_table_layout.html" %}
|
||||||
<div id="table_overflow">{{ balance_table(show_weight=False) }}</div>
|
|
||||||
{% endblock %} {% block content %}
|
{% block sidebar %}
|
||||||
<table id="bill_table" class="split_bills table table-striped">
|
<div id="table_overflow">
|
||||||
<thead>
|
{{ balance_table(show_weight=False) }}
|
||||||
<tr>
|
</div>
|
||||||
<th>{{ _("Who pays?") }}</th>
|
{% endblock %}
|
||||||
<th>{{ _("To whom?") }}</th>
|
|
||||||
<th>{{ _("How much?") }}</th>
|
|
||||||
<th>{{ _("Settled?") }}</th>
|
{% block content %}
|
||||||
</tr>
|
<table id="bill_table" class="split_bills table table-striped">
|
||||||
</thead>
|
<thead><tr><th>{{ _("Who pays?") }}</th><th>{{ _("To whom?") }}</th><th>{{ _("How much?") }}</th><th>{{ _("Settled?") }}</th></tr></thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for transaction in transactions %}
|
{% for bill in bills %}
|
||||||
<tr receiver="{{transaction.receiver.id}}">
|
<tr receiver={{bill.receiver.id}}>
|
||||||
<td>{{ transaction.ower }}</td>
|
<td>{{ bill.ower }}</td>
|
||||||
<td>{{ transaction.receiver }}</td>
|
<td>{{ bill.receiver }}</td>
|
||||||
<td>{{ transaction.amount|currency }}</td>
|
<td>{{ bill.amount|currency }}</td>
|
||||||
<td>
|
<td>
|
||||||
<span id="settle-bill" class="ml-auto pb-2">
|
<span id="settle-bill" class="ml-auto pb-2">
|
||||||
<form class="" action="{{ url_for(".add_settlement_bill") }}" method="POST">
|
<a href="{{ url_for('.settle', amount = bill.amount, ower_id = bill.ower.id, payer_id = bill.receiver.id) }}" class="btn btn-primary">
|
||||||
{{ settlement_form.csrf_token }}
|
<div data-toggle="tooltip" title='{{ _("Click here to record that the money transfer has been done") }}'>
|
||||||
{{ settlement_form.amount(value=transaction.amount) }}
|
{{ ("Settle") }}
|
||||||
{{ settlement_form.sender_id(value=transaction.ower.id) }}
|
</div>
|
||||||
{{ settlement_form.receiver_id(value=transaction.receiver.id) }}
|
</a>
|
||||||
<button class="btn btn-primary" type="submit" title="{{ _("Settle") }}">
|
</span>
|
||||||
<div
|
</td>
|
||||||
data-toggle="tooltip"
|
|
||||||
title='{{ _("Click here to record that the money transfer has been done") }}'
|
|
||||||
>
|
|
||||||
{{ _("Settle") }}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
<a
|
|
||||||
href="{{ url_for('.add_settlement_bill', amount = transaction.amount, sender_id = transaction.ower.id, receiver_id = transaction.receiver.id) }}"
|
|
||||||
class="btn btn-primary"
|
|
||||||
>
|
|
||||||
{{ ("Settle") }}
|
|
||||||
</div>
|
|
||||||
</a>
|
|
||||||
</span>
|
|
||||||
</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1358,25 +1358,23 @@ class TestBudget(IhatemoneyTestCase):
|
||||||
count = 0
|
count = 0
|
||||||
for t in transactions:
|
for t in transactions:
|
||||||
count += 1
|
count += 1
|
||||||
self.client.post(
|
self.client.get(
|
||||||
"/raclette/settle",
|
"/raclette/settle"
|
||||||
data={
|
+ "/"
|
||||||
"amount": t["amount"],
|
+ str(t["amount"])
|
||||||
"sender_id": t["ower"].id,
|
+ "/"
|
||||||
"receiver_id": t["receiver"].id,
|
+ str(t["ower"].id)
|
||||||
},
|
+ "/"
|
||||||
|
+ str(t["receiver"].id)
|
||||||
)
|
)
|
||||||
temp_transactions = project.get_transactions_to_settle_bill()
|
temp_transactions = project.get_transactions_to_settle_bill()
|
||||||
# test if the one has disappeared
|
# test if the one has disappeared
|
||||||
assert len(temp_transactions) == len(transactions) - count
|
assert len(temp_transactions) == len(transactions) - count
|
||||||
|
|
||||||
# test if there is a new one with bill_type reimbursement
|
# test if theres a new one with bill_type reimbursement
|
||||||
bill = project.get_newest_bill()
|
bill = project.get_newest_bill()
|
||||||
assert bill.bill_type == models.BillType.REIMBURSEMENT
|
assert bill.bill_type == models.BillType.REIMBURSEMENT
|
||||||
|
return
|
||||||
# There should be no more settlement to do at the end
|
|
||||||
transactions = project.get_transactions_to_settle_bill()
|
|
||||||
assert len(transactions) == 0
|
|
||||||
|
|
||||||
def test_settle_zero(self):
|
def test_settle_zero(self):
|
||||||
self.post_project("raclette")
|
self.post_project("raclette")
|
||||||
|
@ -1465,78 +1463,6 @@ class TestBudget(IhatemoneyTestCase):
|
||||||
# Create and log in as another project
|
# Create and log in as another project
|
||||||
self.post_project("tartiflette")
|
self.post_project("tartiflette")
|
||||||
|
|
||||||
# Add a participant in this second project
|
|
||||||
self.client.post("/tartiflette/members/add", data={"name": "pirate"})
|
|
||||||
pirate = models.Person.query.filter(models.Person.id == 5).one()
|
|
||||||
assert pirate.name == "pirate"
|
|
||||||
|
|
||||||
# Try to add a new bill to another project
|
|
||||||
resp = self.client.post(
|
|
||||||
"/raclette/add",
|
|
||||||
data={
|
|
||||||
"date": "2017-01-01",
|
|
||||||
"what": "fromage frelaté",
|
|
||||||
"payer": 2,
|
|
||||||
"payed_for": [2, 3, 4],
|
|
||||||
"bill_type": "Expense",
|
|
||||||
"amount": "100.0",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Ensure it has not been created
|
|
||||||
raclette = self.get_project("raclette")
|
|
||||||
assert raclette.get_bills().count() == 1
|
|
||||||
|
|
||||||
# Try to add a new bill in our project that references members of another project.
|
|
||||||
# First with invalid payed_for IDs.
|
|
||||||
resp = self.client.post(
|
|
||||||
"/tartiflette/add",
|
|
||||||
data={
|
|
||||||
"date": "2017-01-01",
|
|
||||||
"what": "soupe",
|
|
||||||
"payer": 5,
|
|
||||||
"payed_for": [3],
|
|
||||||
"bill_type": "Expense",
|
|
||||||
"amount": "5000.0",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Ensure it has not been created
|
|
||||||
piratebill = models.Bill.query.filter(models.Bill.what == "soupe").one_or_none()
|
|
||||||
assert piratebill is None, "piratebill 1 should not exist"
|
|
||||||
|
|
||||||
# Then with invalid payer ID
|
|
||||||
self.client.post(
|
|
||||||
"/tartiflette/add",
|
|
||||||
data={
|
|
||||||
"date": "2017-02-01",
|
|
||||||
"what": "pain",
|
|
||||||
"payer": 3,
|
|
||||||
"payed_for": [5],
|
|
||||||
"bill_type": "Expense",
|
|
||||||
"amount": "5000.0",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Ensure it has not been created
|
|
||||||
piratebill = models.Bill.query.filter(models.Bill.what == "pain").one_or_none()
|
|
||||||
assert piratebill is None, "piratebill 2 should not exist"
|
|
||||||
|
|
||||||
# Make sure we can actually create valid bills
|
|
||||||
self.client.post(
|
|
||||||
"/tartiflette/add",
|
|
||||||
data={
|
|
||||||
"date": "2017-03-01",
|
|
||||||
"what": "baguette",
|
|
||||||
"payer": 5,
|
|
||||||
"payed_for": [5],
|
|
||||||
"bill_type": "Expense",
|
|
||||||
"amount": "5.0",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Ensure it has been created
|
|
||||||
okbill = models.Bill.query.filter(models.Bill.what == "baguette").one_or_none()
|
|
||||||
assert okbill is not None, "Bill baguette should exist"
|
|
||||||
assert okbill.what == "baguette"
|
|
||||||
|
|
||||||
# Now try to access and modify existing bills
|
|
||||||
modified_bill = {
|
modified_bill = {
|
||||||
"date": "2018-12-31",
|
"date": "2018-12-31",
|
||||||
"what": "roblochon",
|
"what": "roblochon",
|
||||||
|
@ -1630,24 +1556,6 @@ class TestBudget(IhatemoneyTestCase):
|
||||||
member = models.Person.query.filter(models.Person.id == 1).one_or_none()
|
member = models.Person.query.filter(models.Person.id == 1).one_or_none()
|
||||||
assert member is None
|
assert member is None
|
||||||
|
|
||||||
# test new settle endpoint to add bills with wrong ids
|
|
||||||
self.client.post("/exit")
|
|
||||||
self.client.post(
|
|
||||||
"/authenticate", data={"id": "tartiflette", "password": "tartiflette"}
|
|
||||||
)
|
|
||||||
self.client.post(
|
|
||||||
"/tartiflette/settle",
|
|
||||||
data={
|
|
||||||
"sender_id": 4,
|
|
||||||
"receiver_id": 5,
|
|
||||||
"amount": "42.0",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
piratebill = models.Bill.query.filter(
|
|
||||||
models.Bill.bill_type == models.BillType.REIMBURSEMENT
|
|
||||||
).one_or_none()
|
|
||||||
assert piratebill is None, "piratebill 3 should not exist"
|
|
||||||
|
|
||||||
@pytest.mark.skip(reason="Currency conversion is broken")
|
@pytest.mark.skip(reason="Currency conversion is broken")
|
||||||
def test_currency_switch(self):
|
def test_currency_switch(self):
|
||||||
# A project should be editable
|
# A project should be editable
|
||||||
|
|
|
@ -452,9 +452,7 @@ def format_form_errors(form, prefix):
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
error_list = "</li><li>".join(
|
error_list = "</li><li>".join(
|
||||||
f"<strong>{field}</strong> {error}"
|
str(error) for (field, errors) in form.errors.items() for error in errors
|
||||||
for (field, errors) in form.errors.items()
|
|
||||||
for error in errors
|
|
||||||
)
|
)
|
||||||
errors = f"<ul><li>{error_list}</li></ul>"
|
errors = f"<ul><li>{error_list}</li></ul>"
|
||||||
# I18N: Form error with a list of errors
|
# I18N: Form error with a list of errors
|
||||||
|
|
|
@ -56,7 +56,6 @@ from ihatemoney.forms import (
|
||||||
ProjectForm,
|
ProjectForm,
|
||||||
ProjectFormWithCaptcha,
|
ProjectFormWithCaptcha,
|
||||||
ResetPasswordForm,
|
ResetPasswordForm,
|
||||||
SettlementForm,
|
|
||||||
get_billform_for,
|
get_billform_for,
|
||||||
)
|
)
|
||||||
from ihatemoney.history import get_history, get_history_queries, purge_history
|
from ihatemoney.history import get_history, get_history_queries, purge_history
|
||||||
|
@ -857,46 +856,24 @@ 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"""
|
||||||
transactions = g.project.get_transactions_to_settle_bill()
|
bills = g.project.get_transactions_to_settle_bill()
|
||||||
settlement_form = SettlementForm()
|
return render_template("settle_bills.html", bills=bills, current_view="settle_bill")
|
||||||
return render_template(
|
|
||||||
"settle_bills.html",
|
|
||||||
transactions=transactions,
|
|
||||||
settlement_form=settlement_form,
|
|
||||||
current_view="settle_bill",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.route("/<project_id>/settle", methods=["POST"])
|
@main.route("/<project_id>/settle/<amount>/<int:ower_id>/<int:payer_id>")
|
||||||
def add_settlement_bill():
|
def settle(amount, ower_id, payer_id):
|
||||||
"""Create a bill to register a settlement"""
|
new_reinbursement = Bill(
|
||||||
form = SettlementForm(id=g.project.id)
|
amount=float(amount),
|
||||||
if not form.validate():
|
|
||||||
flash(
|
|
||||||
format_form_errors(form, _("Error creating settlement bill")),
|
|
||||||
category="danger",
|
|
||||||
)
|
|
||||||
return redirect(url_for(".settle_bill"))
|
|
||||||
|
|
||||||
# Ensure that the sender and receiver ID are valid and part of this project
|
|
||||||
receiver_id = form.receiver_id.data
|
|
||||||
sender_id = form.sender_id.data
|
|
||||||
|
|
||||||
if not g.project.has_member(sender_id):
|
|
||||||
return redirect(url_for(".settle_bill"))
|
|
||||||
|
|
||||||
settlement = Bill(
|
|
||||||
amount=form.amount.data,
|
|
||||||
date=datetime.datetime.today(),
|
date=datetime.datetime.today(),
|
||||||
owers=[Person.query.get(receiver_id, g.project)],
|
owers=[Person.query.get(payer_id)],
|
||||||
payer_id=sender_id,
|
payer_id=ower_id,
|
||||||
project_default_currency=g.project.default_currency,
|
project_default_currency=g.project.default_currency,
|
||||||
bill_type=BillType.REIMBURSEMENT,
|
bill_type=BillType.REIMBURSEMENT,
|
||||||
what=_("Settlement"),
|
what=_("Settlement"),
|
||||||
)
|
)
|
||||||
session.update()
|
session.update()
|
||||||
|
|
||||||
db.session.add(settlement)
|
db.session.add(new_reinbursement)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
flash(_("Settlement bill has been successfully added"), category="success")
|
flash(_("Settlement bill has been successfully added"), category="success")
|
||||||
|
|
Loading…
Reference in a new issue