mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 05:01:48 +02:00
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:
parent
2e9e656acd
commit
af22696a01
3 changed files with 30 additions and 25 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue