Simpl-Resultat/src/utils/splitOffset.ts
le king fu 6db898b7ae fix: a split's offset line follows its parent when it is recategorized (#454)
Recategorizing a split transaction moved the parent only: its offset
line, which cancels in the parent's category the part sent to other
categories, stayed in the former category. Every report adds all rows,
so a transaction of -120 with -50 split to B, moved from A to C, left
+50 in A and -120 in C instead of -70 in C. The keyword path had the
same defect, and its preview also offered the children of a split,
which it then recategorized alone.

- src/utils/splitOffset.ts: isOffsetChild(parent, child), the rule the
  split window already used, extracted and freed from the category: the
  parent's description, the sign opposite to the parent's, a non-zero
  amount (decision D1: no role column, no migration).
- src/services/splitOffsetService.ts: propagateCategoryToOffsets(db,
  parentId, categoryId) gives the parent's new category to the offset
  line, whatever its current category (decision D2: an offset left in a
  former category is repaired at the parent's next change). It runs on
  the caller's transaction handle: no BEGIN/COMMIT, no getDb(), no
  notification. Nothing for an unknown id, a transaction that is not
  split, or a child (children carry is_split = 1 too). Its own module:
  transactionService.ts already imports categorizationService.ts.
- updateTransactionCategory now runs in withTransaction, with its own
  BEGIN, the parent's UPDATE (unchanged, toSqlFlag), the propagation and
  COMMIT, or ROLLBACK and rethrow; it notifies after the COMMIT only.
- previewKeywordMatches offers rows without a parent only; the keyword
  loop's UPDATE skips a child (AND parent_transaction_id IS NULL),
  counts rowsAffected, and propagates for each id before the COMMIT.
- SplitAdjustmentModal leaves out the child isOffsetChild recognizes,
  so an offset left in a former category is no longer shown as a row.

Tests: the regression test of transactionService.test.ts fails on main
(no UPDATE of the offset); the tests of #390 and #396 now find the
parent's UPDATE by its SQL and answer the reads of the propagation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 22:13:08 -04:00

40 lines
1.9 KiB
TypeScript

// splitOffset — which child of a split transaction is its offset line (#454).
//
// Unrelated to src/shared/splitGate.ts, which decides whether the split window
// opens in the current edition (#369): this module only recognizes rows.
//
// `saveSplitAdjustment` (transactionService.ts) writes a split as children of
// the parent transaction (`parent_transaction_id`): one row per entry, with
// the parent's sign (the split window gives every entry that sign, and the
// parent's description when it has none), plus one offset line of
// `-(sum of the entries)`, with the parent's description, that cancels in the
// parent's category the part sent to other categories. No column marks the
// offset line (decision D1 of #454: no role column, no migration), so this
// predicate is the one rule that recognizes it, shared by the split window,
// which leaves it out of the entries, and by `propagateCategoryToOffsets`
// (splitOffsetService.ts), which moves it with its parent.
//
// "amount = -(sum of the other children)" would not tell it apart: the
// children of a split always sum to zero, so every child satisfies it. The
// rule is ambiguous only for an entry that carries the parent's description
// with the opposite sign, which the split window never writes.
//
// The callers only pass the children of `parent` (`WHERE
// parent_transaction_id = $1`): the predicate reads neither the category nor
// the parent link, so an offset line left in a former category is still
// recognized (decision D2 of #454).
/** The two fields the rule reads, on the parent and on its child. */
export interface SplitLine {
description: string;
amount: number;
}
/** True when `child`, a child of `parent`, is the offset line of its split. */
export function isOffsetChild(parent: SplitLine, child: SplitLine): boolean {
return (
child.amount !== 0 &&
child.description === parent.description &&
Math.sign(child.amount) === -Math.sign(parent.amount)
);
}