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 %}