Ensure unique transaction IDs

This commit is contained in:
RunasSudo 2020-05-18 23:56:31 +10:00
parent 4e180f5151
commit 9d1e8af875
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
5 changed files with 21 additions and 6 deletions

View File

@ -304,7 +304,7 @@ def transactions():
@app.route('/transaction')
def transaction():
tid = flask.request.args['tid']
uuid = flask.request.args['uuid']
cash = flask.request.args.get('cash', False)
commodity = flask.request.args.get('commodity', False)
@ -314,7 +314,7 @@ def transaction():
if cash:
l = accounting.ledger_to_cash(l, report_commodity)
transaction = next((t for t in l.transactions if str(t.id) == tid))
transaction = next((t for t in l.transactions if str(t.uuid) == uuid))
if commodity:
total_dr = sum((p.amount for p in transaction.postings if p.amount > 0), Balance()).clean()

View File

@ -67,7 +67,7 @@
{% endif %}
{% for transaction in transactions %}
{% set trn_url = '/transaction?' + {'tid': transaction.id, 'cash': 'on' if cash else ''}|urlencode %}
{% set trn_url = '/transaction?' + {'uuid': transaction.uuid, 'cash': 'on' if cash else ''}|urlencode %}
{% if transaction.has_comment_detail %}
<tr class="trn-first">

View File

@ -66,7 +66,7 @@
{% set ns.balance = opening_balance %}
{% for transaction in transactions %}
{% set trn_url = '/transaction?' + {'tid': transaction.id, 'cash': 'on' if cash else ''}|urlencode %}
{% set trn_url = '/transaction?' + {'uuid': transaction.uuid, 'cash': 'on' if cash else ''}|urlencode %}
{% for posting in transaction.postings if posting.account == account %}
{% set ns.balance = ns.balance + posting.amount %}
{% endfor %}

View File

@ -20,6 +20,7 @@ from .model import *
import csv
from datetime import datetime, timedelta
from decimal import Decimal
import hashlib
import re
import subprocess
@ -92,11 +93,24 @@ def raw_transactions_at_date(date):
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')
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:
if not ledger.transactions or trn_id != ledger.transactions[-1].id:
transaction = Transaction(ledger, trn_id, datetime.strptime(date_str, '%Y-%m-%d'), payee, code=code)
if trn_id in uuids:
digest = hashlib.sha256()
digest.update(trn_id.encode('utf-8'))
digest.update(date_str.encode('utf-8'))
digest.update(payee.encode('utf-8'))
uuid = digest.hexdigest()
else:
uuid = trn_id
transaction = Transaction(ledger, trn_id, datetime.strptime(date_str, '%Y-%m-%d'), payee, code=code, uuid=uuid)
ledger.transactions.append(transaction)
uuids.add(uuid)
else:
# Transaction ID matches: continuation of previous transaction
transaction = ledger.transactions[-1]

View File

@ -65,12 +65,13 @@ class Ledger:
return max(prices, key=lambda p: p[0])[2]
class Transaction:
def __init__(self, ledger, id, date, description, code=None):
def __init__(self, ledger, id, date, description, code=None, uuid=None):
self.ledger = ledger
self.id = id
self.date = date
self.description = description
self.code = code
self.uuid = uuid
self.postings = []