Skip to content

A local-first personal finance app

fix: exclude income from spending analysis budget (#9318)

EMAD BASHARIe1254b5ac9f8
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.integration.test.ts b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.integration.test.ts
new file mode 100644
index 0000000000..e4c33d441c
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.integration.test.ts
@@ -0,0 +1,84 @@
+import {
+ clearServer,
+ initServer,
+} from '@actual-app/core/platform/client/connection';
+import { renderHook } from '@testing-library/react';
+import { afterEach, expect, it, vi } from 'vitest';
+
+import { SpreadsheetProvider, useSpreadsheet } from '#hooks/useSpreadsheet';
+
+import { createSpendingSpreadsheet } from './spending-spreadsheet';
+
+vi.mock(
+ '@actual-app/core/platform/client/connection',
+ () => import('#mocks/connection'),
+);
+
+afterEach(async () => {
+ await clearServer();
+});
+
+it('excludes income from the tracking spending budget query', async () => {
+ let budgetQueryCalled = false;
+
+ initServer({
+ 'make-filters-from-conditions': async () => ({ filters: [] }),
+ 'get-categories': async () => ({
+ list: [
+ {
+ id: 'cat-food',
+ name: 'Food',
+ group: 'group-expenses',
+ is_income: false,
+ },
+ {
+ id: 'cat-income',
+ name: 'Salary',
+ group: 'group-income',
+ is_income: true,
+ },
+ ],
+ grouped: [
+ { id: 'group-expenses', name: 'Expenses' },
+ { id: 'group-income', name: 'Income' },
+ ],
+ }),
+ query: async query => {
+ if (query.table !== 'reflect_budgets') {
+ return { data: [], dependencies: [] };
+ }
+
+ budgetQueryCalled = true;
+
+ expect(query.filterExpressions).toContainEqual({
+ $and: [
+ { month: { $eq: 202609 } },
+ { category: { $oneof: ['cat-food'] } },
+ ],
+ });
+
+ return {
+ data: [{ category: 'cat-food', amount: 10000 }],
+ dependencies: [],
+ };
+ },
+ });
+
+ const { result } = renderHook(useSpreadsheet, {
+ wrapper: SpreadsheetProvider,
+ });
+
+ let finalBudget: number | undefined;
+
+ await createSpendingSpreadsheet({
+ budgetType: 'tracking',
+ compare: '2026-09',
+ compareTo: '2026-08',
+ conditions: [],
+ })(result.current, data => {
+ finalBudget = data.intervalData.at(-1)?.budget;
+ });
+
+ expect(budgetQueryCalled).toBe(true);
+ expect(finalBudget).toBe(-10000);
+});
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.test.ts b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.test.ts
index bace5a5d68..a0b3036cac 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.test.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.test.ts
@@ -37,7 +37,7 @@ describe('getSpendingBudgetFilters', () => {
]);
});
- it('does not filter budgets when no category condition is present', () => {
+ it('excludes income categories when no category condition is present', () => {
const result = getSpendingBudgetFilters({
categories,
categoryGroups,
@@ -50,7 +50,13 @@ describe('getSpendingBudgetFilters', () => {
] satisfies RuleConditionEntity[],
});
- expect(result).toEqual([]);
+ expect(result).toEqual([
+ {
+ category: {
+ $oneof: ['cat-rent', 'cat-electric', 'cat-dining'],
+ },
+ },
+ ]);
});
it('filters budget categories by direct category IDs', () => {
@@ -70,4 +76,57 @@ describe('getSpendingBudgetFilters', () => {
{ category: { $oneof: ['cat-rent', 'cat-dining'] } },
]);
});
+ it('excludes income categories when no category filter is selected', () => {
+ const result = getSpendingBudgetFilters({
+ categories: [
+ ...categories,
+ {
+ id: 'cat-income',
+ name: 'Salary',
+ group: 'group-income',
+ is_income: true,
+ },
+ ],
+ categoryGroups,
+ conditions: [],
+ });
+
+ expect(result).toEqual([
+ {
+ category: {
+ $oneof: ['cat-rent', 'cat-electric', 'cat-dining'],
+ },
+ },
+ ]);
+ });
+
+ it('excludes income categories even when explicitly selected', () => {
+ const result = getSpendingBudgetFilters({
+ categories: [
+ ...categories,
+ {
+ id: 'cat-income',
+ name: 'Salary',
+ group: 'group-income',
+ is_income: true,
+ },
+ ],
+ categoryGroups,
+ conditions: [
+ {
+ field: 'category',
+ op: 'oneOf',
+ value: ['cat-rent', 'cat-income'],
+ },
+ ] satisfies RuleConditionEntity[],
+ });
+
+ expect(result).toEqual([
+ {
+ category: {
+ $oneof: ['cat-rent'],
+ },
+ },
+ ]);
+ });
});
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
index f356730a5f..e317adbad9 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
@@ -43,22 +43,29 @@ export function getSpendingBudgetFilters({
conditions: RuleConditionEntity[];
conditionsOp?: 'and' | 'or';
}) {
+ const expenseCategories = categories.filter(category => !category.is_income);
+
const budgetConditions = conditions.filter(
cond =>
!cond.customName &&
(cond.field === 'category' || cond.field === 'category_group'),
);
- if (budgetConditions.length === 0) {
- return [];
- }
-
- if (!budgetConditions.every(isSupportedCategoryCondition)) {
- return [];
+ if (
+ budgetConditions.length === 0 ||
+ !budgetConditions.every(isSupportedCategoryCondition)
+ ) {
+ return [
+ {
+ category: {
+ $oneof: expenseCategories.map(category => category.id),
+ },
+ },
+ ];
}
const matchingCategoryIds = filterCategoriesByConditions(
- categories,
+ expenseCategories,
categoryGroups,
budgetConditions,
conditionsOp ?? 'and',
@@ -191,24 +198,20 @@ export function createSpendingSpreadsheet({
totalsByDate.set(debt.date, totals);
});
- const budgetMonth = parseInt(compareMonth.replace('-', ''));
+ const budgetMonth = parseInt(compareMonth.replace('-', ''), 10);
+
const budgetTable =
budgetType === 'tracking' ? 'reflect_budgets' : 'zero_budgets';
- const hasBudgetConditions = conditions.some(
- cond =>
- !cond.customName &&
- (cond.field === 'category' || cond.field === 'category_group'),
- );
- const budgetFilters = hasBudgetConditions
- ? await send('get-categories').then(({ list, grouped }) =>
- getSpendingBudgetFilters({
- categories: list,
- categoryGroups: grouped,
- conditions,
- conditionsOp,
- }),
- )
- : [];
+
+ const { list: categories, grouped: categoryGroups } =
+ await send('get-categories');
+
+ const budgetFilters = getSpendingBudgetFilters({
+ categories,
+ categoryGroups,
+ conditions,
+ conditionsOp,
+ });
const [budgets] = await Promise.all([
aqlQuery(
q(budgetTable)
diff --git a/upcoming-release-notes/fix-spending-analysis-tracking-budget.md b/upcoming-release-notes/fix-spending-analysis-tracking-budget.md
new file mode 100644
index 0000000000..fd939c2667
--- /dev/null
+++ b/upcoming-release-notes/fix-spending-analysis-tracking-budget.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [emadleo13]
+---
+
+Exclude income from the budgeted spending total in Spending Analysis when using Tracking Budget.