A local-first personal finance app
Fix template over-budget when a rule's schedule has a split with no category (#9316)
Evan Smith72924f9b270c
diff --git a/packages/loot-core/src/server/budget/schedule-template.test.ts b/packages/loot-core/src/server/budget/schedule-template.test.tsindex a7f01d0c6f..94357772d5 100644--- a/packages/loot-core/src/server/budget/schedule-template.test.ts+++ b/packages/loot-core/src/server/budget/schedule-template.test.ts@@ -41,6 +41,7 @@ type RuleSpec = {amount: number;frequency: 'monthly' | 'yearly' | 'weekly' | 'daily';interval?: number;+ actions?: ConstructorParameters<typeof Rule>[0]['actions'];};function makeRule({@@ -49,6 +50,7 @@ function makeRule({amount,frequency,interval = 1,+ actions = [],}: RuleSpec): Rule {return new Rule({id,@@ -73,7 +75,7 @@ function makeRule({},{ op: 'is', field: 'amount', value: amount, type: 'number' },],- actions: [],+ actions,});}@@ -1482,3 +1484,69 @@ describe('runScheduleForecast on reviewer budgets', () => {expect(result.perScheduleMonthly.get(template_lines[2])).toBe(71429);});});++describe('runScheduleForecast with split rules', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.mocked(db.getAccounts).mockResolvedValue([]);+ vi.mocked(aqlQuery).mockResolvedValue({ data: [], dependencies: [] });+ });++ // A paycheck-style schedule: a -$100.00 transaction split into a $30.00+ // portion categorized as the budgeted category and a remainder split that+ // sets only a payee (e.g. a transfer to another account) and no category.+ // Only the categorized split belongs to the budgeted category.+ it('should only budget the splits explicitly assigned to the category', async () => {+ const template_lines = [+ {+ type: 'schedule',+ name: 'Test Schedule',+ priority: 0,+ directive: 'template',+ } as const,+ ];+ mockSingleSchedule({+ start: '2024-08-01',+ amount: -10000,+ frequency: 'monthly',+ actions: [+ {+ op: 'set-split-amount',+ value: -3000,+ options: { method: 'fixed-amount', splitIndex: 1 },+ },+ {+ op: 'set',+ field: 'category',+ value: defaultCategory.id,+ type: 'id',+ options: { splitIndex: 1 },+ },+ {+ op: 'set-split-amount',+ value: null,+ options: { method: 'remainder', splitIndex: 2 },+ },+ {+ op: 'set',+ field: 'payee',+ value: 'other-payee',+ type: 'id',+ options: { splitIndex: 2 },+ },+ ],+ });++ const result = await runScheduleForecast(+ template_lines,+ '2024-08-01',+ 0,+ 0,+ defaultCategory,+ );++ // Only the categorized split ($30.00) counts; the uncategorized remainder+ // split must not inherit the budgeted category.+ expect(result.to_budget).toBe(3000);+ });+});diff --git a/packages/loot-core/src/server/budget/schedule-template.ts b/packages/loot-core/src/server/budget/schedule-template.tsindex edc3988e3a..c65798b16c 100644--- a/packages/loot-core/src/server/budget/schedule-template.ts+++ b/packages/loot-core/src/server/budget/schedule-template.ts@@ -120,9 +120,12 @@ export async function createScheduleList(// Use the schedule's next occurrence date so "balance as of this moment"// matches the scheduled date; id/sort_order are unset so we don't exclude a// non-existent transaction from the balance query.+ //+ // The category is deliberately left unset: split transactions inherit their+ // parent's category unless a rule action sets one, so seeding it here would+ // attribute every uncategorized split (e.g. a transfer) to this category.const scheduleRuleContext: TransactionEntity = {amount: scheduleAmount,- category: category.id,subtransactions: [],...(next_date_string ? { date: next_date_string } : {}),id: null,diff --git a/upcoming-release-notes/schedule-template-uncategorized-splits.md b/upcoming-release-notes/schedule-template-uncategorized-splits.mdnew file mode 100644index 0000000000..0807e7f2cf--- /dev/null+++ b/upcoming-release-notes/schedule-template-uncategorized-splits.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [e13h]+---++Fix schedule templates over-budgeting a category when the schedule's rule has a split with no category (e.g. a transfer)