A local-first personal finance app
[AI] Fix split double-counting in balance forecast (#7955)
Sebastián Maluka3f651c8da9b
diff --git a/packages/loot-core/src/server/forecast/app.test.ts b/packages/loot-core/src/server/forecast/app.test.tsindex 2149efe773..3f71e36159 100644--- a/packages/loot-core/src/server/forecast/app.test.ts+++ b/packages/loot-core/src/server/forecast/app.test.ts@@ -341,6 +341,50 @@ describe('forecast app', () => {expect(balanceByDate['2024-03-31']).toBe(-25);});+ it('does not double-count split parents in posted transaction balances', async () => {+ const accountId = await db.insertAccount({ id: 'acct', name: 'Checking' });++ const parentId = await db.insertTransaction({+ id: 'split-parent',+ account: accountId,+ amount: 100,+ date: '2024-03-10',+ is_parent: true,+ category: null,+ });++ await db.insertTransaction({+ id: 'split-child-1',+ account: accountId,+ amount: 60,+ date: '2024-03-10',+ is_child: true,+ parent_id: parentId,+ });+ await db.insertTransaction({+ id: 'split-child-2',+ account: accountId,+ amount: 40,+ date: '2024-03-10',+ is_child: true,+ parent_id: parentId,+ });++ const result = await generateForecast({+ accountIds: [accountId],+ startDate: '2024-03-01',+ endDate: '2024-03-31',+ });++ const balanceByDate = Object.fromEntries(+ result.dataPoints.map(({ date, balance }) => [date, balance]),+ );++ expect(balanceByDate['2024-03-09']).toBe(0);+ expect(balanceByDate['2024-03-10']).toBe(100);+ expect(balanceByDate['2024-03-31']).toBe(100);+ });+it('filters future schedule occurrences using rule-derived fields like category', async () => {const accountId = await db.insertAccount({ id: 'acct', name: 'Checking' });const groupId = await db.insertCategoryGroup({ name: 'Bills' });diff --git a/packages/loot-core/src/server/forecast/forecast-filters.ts b/packages/loot-core/src/server/forecast/forecast-filters.tsindex ccd858fe72..a3717fe806 100644--- a/packages/loot-core/src/server/forecast/forecast-filters.ts+++ b/packages/loot-core/src/server/forecast/forecast-filters.ts@@ -1,7 +1,6 @@import { aqlQuery } from '#server/aql';import { conditionsToAQL } from '#server/transactions/transaction-rules';import { q } from '#shared/query';-import { ungroupTransactions } from '#shared/transactions';import type { RuleConditionEntity, TransactionEntity } from '#types/models';import { getAccountRestrictionMode } from './forecast-accounts';@@ -81,7 +80,7 @@ export async function getTransactions(let query = q('transactions').filter({ tombstone: false }).select('*')- .options({ splits: 'grouped' });+ .options({ splits: 'inline' });if (accountIds !== undefined) {if (accountIds.length === 0) {@@ -96,7 +95,7 @@ export async function getTransactions(}const { data } = await aqlQuery(query);- return ungroupTransactions(data);+ return data;}function escapeRegex(value: string) {diff --git a/upcoming-release-notes/7955.md b/upcoming-release-notes/7955.mdnew file mode 100644index 0000000000..56afea1ed5--- /dev/null+++ b/upcoming-release-notes/7955.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [samaluk]+---++Fix Balance Forecast double-counting posted split transactions.