From 219f308e8c065a69e27ee0dd17b4f8abd767721f Mon Sep 17 00:00:00 2001 From: Glandos Date: Sun, 26 Jul 2020 16:02:29 +0200 Subject: [PATCH] Rework currency switching Fixes #599 --- ihatemoney/web.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ihatemoney/web.py b/ihatemoney/web.py index baacff47..576d6e10 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -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 - - bill.converted_amount = CurrencyConverter().exchange_currency( - bill.amount, 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)