Also read transaction metadata

This commit is contained in:
RunasSudo 2021-01-02 21:27:04 +11:00
parent 4a9e4d34e9
commit 4a76f605b4
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 14 additions and 7 deletions

View File

@ -91,12 +91,12 @@ def raw_transactions_at_date(date):
ledger = Ledger(date)
ledger.prices = get_pricedb()
output = run_ledger_date(date, 'csv', '--csv-format', '%(quoted(parent.id)),%(quoted(format_date(date))),%(quoted(parent.code)),%(quoted(payee)),%(quoted(account)),%(quoted(display_amount)),%(quoted(comment)),%(quoted(state))\n')
output = run_ledger_date(date, 'csv', '--csv-format', '%(quoted(parent.id)),%(quoted(format_date(date))),%(quoted(parent.code)),%(quoted(payee)),%(quoted(account)),%(quoted(display_amount)),%(quoted(comment)),%(quoted(state)),%(quoted(note))\n')
uuids = set()
reader = csv.reader(output.splitlines(), dialect='ledger')
for trn_id, date_str, code, payee, account_str, amount_str, comment, state_str in reader:
reader = csv.reader(output.splitlines(True), dialect='ledger')
for trn_id, date_str, code, payee, account_str, amount_str, comment, state_str, note_str in reader:
if not ledger.transactions or trn_id != ledger.transactions[-1].id:
if trn_id in uuids:
digest = hashlib.sha256()
@ -107,7 +107,13 @@ def raw_transactions_at_date(date):
else:
uuid = trn_id
transaction = Transaction(ledger, trn_id, datetime.strptime(date_str, '%Y-%m-%d'), payee, code=code, uuid=uuid)
metadata = {}
for line in note_str.splitlines():
line = line.strip()
if ': ' in line:
metadata[line.split(': ')[0]] = line.split(': ')[1].strip()
transaction = Transaction(ledger, trn_id, datetime.strptime(date_str, '%Y-%m-%d'), payee, code, uuid, metadata)
ledger.transactions.append(transaction)
uuids.add(uuid)

View File

@ -67,13 +67,14 @@ class Ledger:
return max(prices, key=lambda p: p[0])[2]
class Transaction:
def __init__(self, ledger, id, date, description, code=None, uuid=None):
def __init__(self, ledger, id, date, description, code=None, uuid=None, metadata=None):
self.ledger = ledger
self.id = id
self.date = date
self.description = description
self.code = code
self.uuid = uuid
self.metadata = metadata or {}
self.postings = []
@ -91,12 +92,12 @@ class Transaction:
return '\n'.join(result)
def reverse(self, id, date, description, code=None, uuid=None):
result = Transaction(self.ledger, id, date, description, code, uuid)
result = Transaction(self.ledger, id, date, description, code, uuid, self.metadata)
result.postings = [Posting(p.transaction, p.account, -p.amount, p.comment, p.state) for p in self.postings]
return result
def exchange(self, commodity):
result = Transaction(self.ledger, self.id, self.date, self.description, self.code, self.uuid)
result = Transaction(self.ledger, self.id, self.date, self.description, self.code, self.uuid, self.metadata)
# Combine like postings
for k, g in itertools.groupby(self.postings, key=lambda p: (p.account, p.comment, p.state)):