Skip to content

Commit

Permalink
fix export of ledger commodities to ledger (#215)
Browse files Browse the repository at this point in the history
- add is_currency() to Commodity
- do not add the "check commodity" in ledger export if commodity is not a currency
- properly escape commodity names
  • Loading branch information
sebastiendementen committed Jun 17, 2024
1 parent c0b84a1 commit f24c263
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions piecash/core/commodity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ class Price(DeclarativeBaseGuid):
foreign_keys=[currency_guid],
)

def __init__(
self, commodity, currency, date, value, type="unknown", source="user:price"
):
def __init__(self, commodity, currency, date, value, type="unknown", source="user:price"):
self.commodity = commodity
self.currency = currency
assert _type(date) is datetime.date
Expand All @@ -89,9 +87,7 @@ def __init__(
self.source = source

def __str__(self):
return "Price<{:%Y-%m-%d} : {} {}/{}>".format(
self.date, self.value, self.currency.mnemonic, self.commodity.mnemonic
)
return "Price<{:%Y-%m-%d} : {} {}/{}>".format(self.date, self.value, self.currency.mnemonic, self.commodity.mnemonic)

def object_to_validate(self, change):
if change[-1] != "deleted":
Expand Down Expand Up @@ -159,9 +155,7 @@ class Commodity(DeclarativeBaseGuid):
def base_currency(self):
b = self.book
if b is None:
raise GnucashException(
"The commodity should be linked to a session to have a 'base_currency'"
)
raise GnucashException("The commodity should be linked to a session to have a 'base_currency'")

if self.namespace == "CURRENCY":
# get the base currency as first commodity in DB
Expand Down Expand Up @@ -238,6 +232,9 @@ def __str__(self):
def precision(self):
return len(str(self.fraction)) - 1

def is_currency(self):
return self.namespace == "CURRENCY"

def currency_conversion(self, currency):
"""
Return the latest conversion factor to convert self to currency
Expand All @@ -253,16 +250,12 @@ def currency_conversion(self, currency):
"""
# conversion is done from self.commodity to commodity (if possible)
sc2c = (
self.prices.filter_by(currency=currency).order_by(Price.date.desc()).first()
)
sc2c = self.prices.filter_by(currency=currency).order_by(Price.date.desc()).first()
if sc2c:
return sc2c.value

# conversion is done directly from commodity to self.commodity (if possible)
c2sc = (
currency.prices.filter_by(currency=self).order_by(Price.date.desc()).first()
)
c2sc = currency.prices.filter_by(currency=self).order_by(Price.date.desc()).first()
if c2sc:
return Decimal(1) / c2sc.value

Expand All @@ -285,9 +278,7 @@ def update_prices(self, start_date=None):
.. todo:: add some frequency to retrieve prices only every X (week, month, ...)
"""
if self.book is None:
raise GncPriceError(
"Cannot update price for a commodity not attached to a book"
)
raise GncPriceError("Cannot update price for a commodity not attached to a book")

# get last_price updated
last_price = self.prices.order_by(Price.date.desc()).limit(1).first()
Expand All @@ -298,7 +289,7 @@ def update_prices(self, start_date=None):
if last_price:
start_date = max(last_price.date + datetime.timedelta(days=1), start_date)

if self.namespace == "CURRENCY":
if self.is_currency():
# get reference currency (from book.root_account)
default_currency = self.base_currency
if default_currency == self:
Expand Down Expand Up @@ -337,8 +328,6 @@ def object_to_validate(self, change):
def validate(self):
# check uniqueness of namespace/mnemonic
try:
self.book.query(Commodity).filter_by(
namespace=self.namespace, mnemonic=self.mnemonic
).one()
self.book.query(Commodity).filter_by(namespace=self.namespace, mnemonic=self.mnemonic).one()
except MultipleResultsFound:
raise ValueError("{} already exists in this book".format(self))

0 comments on commit f24c263

Please sign in to comment.