From 131e818a4e8527c30d628b6d744bc15240e1f027 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 2 Jan 2023 20:07:03 +1100 Subject: [PATCH] Allow editing statement lines without breaking reconciliation --- drcr/journal/views.py | 20 +++++++++++++++---- drcr/templates/journal/journal.html | 2 +- .../templates/statements/statement_lines.html | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/drcr/journal/views.py b/drcr/journal/views.py index 87934ea..519b523 100644 --- a/drcr/journal/views.py +++ b/drcr/journal/views.py @@ -21,6 +21,7 @@ from ..database import db from ..models import Amount, Posting, Transaction, TrialBalancer from ..webapp import all_transactions, app from .models import BalanceAssertion +from ..statements.models import StatementLineReconciliation from datetime import datetime @@ -75,10 +76,8 @@ def journal_edit_transaction(): # Edit transaction transaction.dt = datetime.strptime(request.form['dt'], '%Y-%m-%d') transaction.description = request.form['description'] - transaction.postings = [] - - # FIXME: This will orphan StatementLineReconciliations + new_postings = [] for account, sign, amount_str in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')): amount = Amount.parse(amount_str) if sign == 'cr': @@ -89,8 +88,21 @@ def journal_edit_transaction(): quantity=amount.quantity, commodity=amount.commodity ) - transaction.postings.append(posting) + new_postings.append(posting) + # Fix up reconciliations + for old_posting in transaction.postings: + for reconciliation in StatementLineReconciliation.query.filter(StatementLineReconciliation.posting == old_posting): + # See if there is a corresponding new posting + new_posting = next((p for p in new_postings if p.account == old_posting.account and p.quantity == old_posting.quantity and p.commodity == old_posting.commodity), None) + if new_posting is not None: + # Match up reconciliation + reconciliation.posting = new_posting + else: + # No matching reconciliation + db.session.delete(reconciliation) + + transaction.postings = new_postings # This queues the old postings for deletion transaction.assert_valid() db.session.commit() diff --git a/drcr/templates/journal/journal.html b/drcr/templates/journal/journal.html index 4510932..b5394f0 100644 --- a/drcr/templates/journal/journal.html +++ b/drcr/templates/journal/journal.html @@ -43,7 +43,7 @@ {% for transaction in transactions %} {{ transaction.dt.strftime('%Y-%m-%d') }} - {{ transaction.description }} {##} + {{ transaction.description }} diff --git a/drcr/templates/statements/statement_lines.html b/drcr/templates/statements/statement_lines.html index 6b553d4..74fbd26 100644 --- a/drcr/templates/statements/statement_lines.html +++ b/drcr/templates/statements/statement_lines.html @@ -53,12 +53,12 @@ {# TODO #} {% elif line.is_complex() %} (Complex) - {##} + {% else %} {% for posting in line.reconciliation.posting.transaction.postings if posting.account != line.source_account %} {{ posting.account }} {% endfor %} - {##} + {% endif %} {{ line.amount().format() if line.quantity >= 0 else '' }}