swap Money for MoneyAmount, fix a couple of things along the way

This commit is contained in:
Pablo Martin 2024-06-27 17:18:43 +02:00
parent ab2ac1ec6a
commit 0947b34ebf
6 changed files with 26 additions and 27 deletions

View file

@ -3,8 +3,9 @@ from decimal import Decimal
from numbers import Number
from typing import Iterable, Set, Union
from money.currency import Currency, CurrencyHelper
from money.money import Money
from money.currency import Currency
from xexe.money_amount import DEFAULT_MONEY_PRECISION_POSITIONS, MoneyAmount
class ExchangeRate:
@ -13,13 +14,13 @@ class ExchangeRate:
self,
from_currency: Currency,
to_currency: Currency,
rate: Union[Money, Number, str],
rate: Union[MoneyAmount, Number, str],
rate_date: datetime.date,
) -> None:
self.from_currency = from_currency
self.to_currency = to_currency
if not isinstance(rate, Money):
rate = Money(rate, to_currency)
if not isinstance(rate, MoneyAmount):
rate = MoneyAmount(amount=rate, currency=to_currency)
self.rate = rate
self.rate_date = rate_date
@ -100,7 +101,7 @@ def add_equal_rates(rates: ExchangeRates, overwrite: bool = False) -> ExchangeRa
new_rate = ExchangeRate(
from_currency=currency,
to_currency=currency,
rate=Money(1, currency),
rate=MoneyAmount(1, currency),
rate_date=date,
)
if new_rate in rates and not overwrite:
@ -122,7 +123,7 @@ def add_inverse_rates(rates: ExchangeRates) -> ExchangeRates:
from_currency=rate.to_currency,
to_currency=rate.from_currency,
rate_date=rate.rate_date,
rate=f"{1 / rate.amount:.{CurrencyHelper.decimal_precision_for_currency(rate.from_currency)}f}",
rate=f"{1 / rate.amount:.{DEFAULT_MONEY_PRECISION_POSITIONS}f}",
)
rates.add_rate(inverse_rate)