Fix bug with prefixed commodities of more than one character

Thanks to Ricardo for the report
This commit is contained in:
RunasSudo 2021-01-02 21:38:48 +11:00
parent 7b9ae9d56e
commit 09f2c3961d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 14 additions and 9 deletions

View File

@ -368,9 +368,9 @@ def filter_amount_positive(amt):
@app.template_filter('bc') @app.template_filter('bc')
def filter_commodity_positive(amt): def filter_commodity_positive(amt):
if amt.commodity.is_prefix: if amt.commodity.is_prefix:
return flask.Markup('<span class="copyable-amount" title="{}">{}{:,.2f}</span>'.format(amt.tostr(False), amt.commodity.name, amt.amount).replace(',', '&#8239;')) return flask.Markup('<span class="copyable-amount" title="{}">{}{}{:,.2f}</span>'.format(amt.tostr(False), amt.commodity.name, ' ' if amt.commodity.is_space else '', amt.amount).replace(',', '&#8239;'))
else: else:
return flask.Markup('<span class="copyable-amount" title="{}">{:,.2f} {}</span>'.format(amt.tostr(False), amt.amount, amt.commodity.name).replace(',', '&#8239;')) return flask.Markup('<span class="copyable-amount" title="{}">{:,.2f}{}{}</span>'.format(amt.tostr(False), amt.amount, ' ' if amt.commodity.is_space else '', amt.commodity.name).replace(',', '&#8239;'))
@app.template_filter('bt') @app.template_filter('bt')
def filter_commodity_table_positive(amt, show_price, link=None): def filter_commodity_table_positive(amt, show_price, link=None):

View File

@ -53,6 +53,9 @@ def financial_year(date):
csv.register_dialect('ledger', doublequote=False, escapechar='\\', strict=True) csv.register_dialect('ledger', doublequote=False, escapechar='\\', strict=True)
RE_COMMODITY1 = re.compile(r'([0123456789.,-]+)( *)(.+)')
RE_COMMODITY2 = re.compile(r'(.+?)( *)([0123456789.,-]+)')
def parse_amount(amount): def parse_amount(amount):
if '{' in amount: if '{' in amount:
amount_str = amount[:amount.index('{')].strip() amount_str = amount[:amount.index('{')].strip()
@ -63,13 +66,14 @@ def parse_amount(amount):
if amount_str[0] in list('0123456789-'): if amount_str[0] in list('0123456789-'):
# Commodity follows number # Commodity follows number
bits = amount_str.split() result = RE_COMMODITY1.match(amount_str)
amount_num = Decimal(bits[0].replace(',', '')) amount_num = Decimal(result.group(1).replace(',', ''))
commodity = Commodity(bits[1].strip('"'), False) commodity = Commodity(result.group(3).strip('"'), False, len(result.group(2)) > 0)
else: else:
# Commodity precedes number # Commodity precedes number
commodity = Commodity(amount_str[0], True) result = RE_COMMODITY2.match(amount_str)
amount_num = Decimal(amount_str[1:].replace(',', '')) amount_num = Decimal(result.group(3).replace(',', ''))
commodity = Commodity(result.group(1).strip('"'), True, len(result.group(2)) > 0)
if price_str: if price_str:
commodity.price = parse_amount(price_str) commodity.price = parse_amount(price_str)

View File

@ -435,9 +435,10 @@ class Balance:
return self + (-other) return self + (-other)
class Commodity: class Commodity:
def __init__(self, name, is_prefix, price=None): def __init__(self, name, is_prefix, is_space, price=None):
self.name = name self.name = name
self.is_prefix = is_prefix self.is_prefix = is_prefix
self.is_space = is_space
self.price = price self.price = price
def __repr__(self): def __repr__(self):
@ -449,7 +450,7 @@ class Commodity:
return self.name == other.name and self.price == other.price return self.name == other.name and self.price == other.price
def strip_price(self): def strip_price(self):
return Commodity(self.name, self.is_prefix) return Commodity(self.name, self.is_prefix, self.is_space)
class TrialBalance: class TrialBalance:
def __init__(self, ledger, date, pstart, label=None): def __init__(self, ledger, date, pstart, label=None):