New algorithm when switching currencies

Fixes #599

Be careful, when switching to no currency, all bills are first converted
to the last known currency, and currency is erased
This commit is contained in:
Glandos 2020-07-26 21:53:37 +02:00
parent 2e9e656acd
commit af22696a01
3 changed files with 30 additions and 25 deletions

View file

@ -152,7 +152,7 @@ class EditProjectForm(FlaskForm):
project.contact_email = self.contact_email.data
project.logging_preference = self.logging_preference
project.default_currency = self.default_currency.data
project.switch_currency(self.default_currency.data)
return project

View file

@ -17,6 +17,7 @@ from sqlalchemy_continuum import make_versioned, version_class
from sqlalchemy_continuum.plugins import FlaskPlugin
from werkzeug.security import generate_password_hash
from ihatemoney.currency_convertor import CurrencyConverter
from ihatemoney.patch_sqlalchemy_continuum import PatchedBuilder
from ihatemoney.versioning import (
ConditionalVersioningManager,
@ -263,6 +264,34 @@ class Project(db.Model):
)
return pretty_bills
def switch_currency(self, new_currency):
# Update converted currency
if new_currency != self.default_currency:
for bill in self.get_bills():
if bill.original_currency == new_currency:
continue
if new_currency == CurrencyConverter.no_currency:
# Use old currency to flatten all amount before stripping
bill.converted_amount = CurrencyConverter().exchange_currency(
bill.amount, bill.original_currency, self.default_currency
)
# Strip currency
bill.amount = bill.converted_amount
bill.original_currency = CurrencyConverter.no_currency
else:
# Switch to new currency for everyone
bill.converted_amount = CurrencyConverter().exchange_currency(
bill.amount, bill.original_currency, new_currency
)
# Add the currency for previously un-currency-ied
if bill.original_currency == CurrencyConverter.no_currency:
bill.original_currency = new_currency
db.session.add(bill)
self.default_currency = new_currency
db.session.add(self)
db.session.commit()
def remove_member(self, member_id):
"""Remove a member from the project.

View file

@ -414,31 +414,7 @@ def edit_project():
# Edit form
if edit_form.validate_on_submit():
old_currency = g.project.default_currency
project = edit_form.update(g.project)
# Update converted currency
if project.default_currency != old_currency:
for bill in project.get_bills():
if bill.original_currency == project.default_currency:
continue
if project.default_currency == CurrencyConverter.no_currency:
# Use old currency to flatten all amount before stripping
bill.converted_amount = CurrencyConverter().exchange_currency(
bill.amount, bill.original_currency, old_currency
)
# Strip currency
bill.amount = bill.converted_amount
bill.original_currency = CurrencyConverter.no_currency
else:
# Switch to new currency for everyone
bill.converted_amount = CurrencyConverter().exchange_currency(
bill.amount, bill.original_currency, project.default_currency
)
# Add the currency for previously un-currency-ied
if bill.original_currency == CurrencyConverter.no_currency:
bill.original_currency = project.default_currency
db.session.add(bill)
db.session.add(project)
db.session.commit()