Rework currency switching

Fixes #599
This commit is contained in:
Glandos 2020-07-26 16:02:29 +02:00
parent 27d95f2d01
commit 219f308e8c

View file

@ -414,17 +414,30 @@ 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 != CurrencyConverter.no_currency:
if project.default_currency != old_currency:
for bill in project.get_bills():
if bill.original_currency == project.default_currency:
continue
if bill.original_currency == CurrencyConverter.no_currency:
bill.original_currency = project.default_currency
if project.default_currency == CurrencyConverter.no_currency:
# Use old currency 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)