fix(bookkeeping): storno of a residual booking's main verifikat releases the bank row whole (#2348)
* fix(bookkeeping): storno of a residual booking's main verifikat releases the bank row whole A residual booking anchors a bank row twice: the pointer column holds the main verifikat and one transaction_voucher_links row of role 'other' holds the small residual verifikat. reverseEntry reset the pointer unconditionally and left the 'other' row behind, so the row split across surfaces: the worklist showed it as att bokfora (is_business IS NULL) while every reader that counts junction rows (the unmatched list behind BookDirectlyDialog, the bulk_book_transactions RPC, is_transaction_booked(), the reconciliation bridge) went on calling it booked. Bulk-book refused it with BULK_BOOK_TX_ALREADY_BOOKED on a row displayed as unbooked. reverseEntry now reads the rows whose pointer it is about to reset and drops their junction rows to any other verifikat right after the reset, before the existing cleanup of the reversed entry's own junction rows. No anchor survives, so every reader agrees without a role fork or a migration; the residual verifikat stays posted and surfaces as unmatched, which is honest because its main sibling is gone. This mirrors what koppla-bort and the 1:N partial-split path already do. Tests: engine.test.ts gains the residual case and the no-pointer case and pins the pointer read before the reset; the opening-balance mock learns the read. The bank_line-only re-booking guards from #2029 stay as defense for rows left behind before this change (prod holds zero such rows). Fixes #2061 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016xry8E1FuYbbedbwvZAxLv * fix(bookkeeping): release reversed-entry transactions and drop their supplementary links in one RPC statement Review finding on #2348 (CodeRabbit, Swedish review note): the pointer read, the pointer reset and the supplementary-link delete were three PostgREST statements. A failed read left the links behind with the pointer already reset, the exact half-anchored row #2061 describes, and a link created between the reset and the delete would have been removed from a stale id set. release_reversed_entry_transactions(p_company_id, p_entry_id) does both in a single data-modifying CTE under the UPDATE's row locks and one snapshot: the DELETE only sees links that existed when the statement started and only for the rows the UPDATE actually released. SECURITY INVOKER, so RLS and the writer-role trigger apply exactly as they did to the direct statements. Links to the reversed entry itself are still left to the engine's junction cleanup (bulk-book N=1 writes a pointer and a bank_line row to the same entry). Migration 20260906172540 applied to staging and covered by tests/pg/release-reversed-entry-transactions.pg.test.ts (main storno releases whole, residual storno touches nothing, bank_line-to-self left for the junction cleanup, tenant scope, viewer refused). Engine unit tests pin the RPC call and the best-effort fallthrough on RPC error. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016xry8E1FuYbbedbwvZAxLv --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
7448490fb7
commit
3c033e466f
+30
-10
@@ -1252,18 +1252,38 @@ export async function reverseEntry(
|
||||
// (is_business, category, journal_entry_id), plus reconciliation_method:
|
||||
// it describes how the link was made, and the link is gone (the koppla-bort
|
||||
// path in lib/reconciliation/bank-reconciliation.ts resets it the same way).
|
||||
const { error: unlinkError } = await supabase
|
||||
.from('transactions')
|
||||
.update({
|
||||
journal_entry_id: null,
|
||||
is_business: null,
|
||||
category: null,
|
||||
reconciliation_method: null,
|
||||
})
|
||||
.eq('company_id', companyId)
|
||||
.eq('journal_entry_id', entryId)
|
||||
//
|
||||
// A released row must have no anchor left. A residual booking
|
||||
// (lib/reconciliation/residual.ts) keeps the main verifikat in the pointer
|
||||
// column and anchors the small residual verifikat through a junction row of
|
||||
// role 'other'. With the pointer gone that row would be the only thing left,
|
||||
// and it explains a few kronor of fee, not the bank amount: every reader
|
||||
// that counts junction rows (fetchJunctionLinkedTxIds, the bulk_book RPC,
|
||||
// is_transaction_booked()) would go on calling the row booked while the
|
||||
// worklist shows it as att bokföra (#2061). The release_reversed_entry_
|
||||
// transactions RPC therefore resets the pointer AND drops the released
|
||||
// rows' links to other verifikat in one statement (one snapshot, the
|
||||
// UPDATE's row locks): no read-then-write window in which a failed read or
|
||||
// a concurrent booking leaves the half-anchored row behind. That mirrors
|
||||
// what koppla-bort removes and what the 1:N partial-split path below drops;
|
||||
// the residual verifikat stays posted and surfaces as unmatched again,
|
||||
// which is honest: its main sibling is gone. Links to the reversed entry
|
||||
// itself are left to the junction cleanup below, which owns them.
|
||||
const { data: releaseData, error: unlinkError } = await supabase.rpc(
|
||||
'release_reversed_entry_transactions',
|
||||
{ p_company_id: companyId, p_entry_id: entryId },
|
||||
)
|
||||
if (unlinkError) {
|
||||
log.error('failed to unlink transactions from reversed entry', unlinkError, { entryId })
|
||||
} else {
|
||||
const counts = (releaseData ?? {}) as { released?: number; dropped?: number }
|
||||
if ((counts.dropped ?? 0) > 0) {
|
||||
log.info('dropped supplementary voucher links of released transactions', {
|
||||
entryId,
|
||||
released: counts.released ?? 0,
|
||||
dropped: counts.dropped,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Same promise, second anchor. Bulk-booked rows (bulk_book_transactions RPC)
|
||||
|
||||
Reference in New Issue
Block a user