Allow editing statement lines without breaking reconciliation

This commit is contained in:
RunasSudo 2023-01-02 20:07:03 +11:00
parent 3c661100e5
commit 131e818a4e
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 19 additions and 7 deletions

View File

@ -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()

View File

@ -43,7 +43,7 @@
{% for transaction in transactions %}
<tr>
<td>{{ transaction.dt.strftime('%Y-%m-%d') }}</td>
<td colspan="3">{{ transaction.description }} {#<a href="/journal/edit-transaction?id={{ transaction.id }}"><i class="bi bi-pencil text-muted"></i></a>#}</td>
<td colspan="3">{{ transaction.description }} <a href="/journal/edit-transaction?id={{ transaction.id }}"><i class="bi bi-pencil text-muted"></i></a></td>
<td></td>
<td></td>
</tr>

View File

@ -53,12 +53,12 @@
{# TODO #}
{% elif line.is_complex() %}
<i>(Complex)</i>
{#<a href="/journal/edit-transaction?id={{ line.reconciliation.posting.transaction.id }}" class="text-muted"><i class="bi bi-pencil"></i></a>#}
<a href="/journal/edit-transaction?id={{ line.reconciliation.posting.transaction.id }}" class="text-muted"><i class="bi bi-pencil"></i></a>
{% else %}
{% for posting in line.reconciliation.posting.transaction.postings if posting.account != line.source_account %}
<a href="#" class="text-body" onclick="classifyLine({{ line.id }});return false;">{{ posting.account }}</a>
{% endfor %}
{#<a href="/journal/edit-transaction?id={{ line.reconciliation.posting.transaction.id }}" class="text-muted"><i class="bi bi-pencil"></i></a>#}
<a href="/journal/edit-transaction?id={{ line.reconciliation.posting.transaction.id }}" class="text-muted"><i class="bi bi-pencil"></i></a>
{% endif %}
</td>
<td class="text-end">{{ line.amount().format() if line.quantity >= 0 else '' }}</td>