From 979fa43c4a3d5e0ab087a84ef3b74c8972aebf1c Mon Sep 17 00:00:00 2001 From: lougeorge <105462540+lougeorge@users.noreply.github.com> Date: Thu, 3 Jul 2025 12:26:47 -0400 Subject: [PATCH] correctly ignore hidden categories when using "Set Average Budget" (#5239) * fixed setAverage functions to ignore hidden categories * Add logic to ignore hidden categories in budget actions * Modify SQL statements in setAverage functions to also exclude categoryGroups and change DbViewCategory type definition to include hidden flag of the group a category belongs to * [autofix.ci] apply automated fixes * Revert setZero functionality to set all budget categories and category groups to zero, including hidden ones * Add new type DbViewCategoryWithGroupHidden which includes a flag for whether the group a category belongs to is hidden or not, and have setAvg functions use that type instead --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../loot-core/src/server/budget/actions.ts | 27 ++++++++++++++----- .../loot-core/src/server/db/types/index.ts | 11 ++++++++ upcoming-release-notes/5239.md | 6 +++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 upcoming-release-notes/5239.md diff --git a/packages/loot-core/src/server/budget/actions.ts b/packages/loot-core/src/server/budget/actions.ts index 50142538a6..234e217f07 100644 --- a/packages/loot-core/src/server/budget/actions.ts +++ b/packages/loot-core/src/server/budget/actions.ts @@ -261,8 +261,13 @@ export async function set3MonthAvg({ }: { month: string; }): Promise { - const categories = await db.all( - 'SELECT * FROM v_categories WHERE tombstone = 0', + const categories = await db.all( + ` + SELECT c.* + FROM categories c + LEFT JOIN category_groups g ON c.cat_group = g.id + WHERE c.tombstone = 0 AND c.hidden = 0 AND g.hidden = 0 + `, ); const prevMonth1 = monthUtils.prevMonth(month); @@ -304,8 +309,13 @@ export async function set12MonthAvg({ }: { month: string; }): Promise { - const categories = await db.all( - 'SELECT * FROM v_categories WHERE tombstone = 0', + const categories = await db.all( + ` + SELECT c.* + FROM categories c + LEFT JOIN category_groups g ON c.cat_group = g.id + WHERE c.tombstone = 0 AND c.hidden = 0 AND g.hidden = 0 + `, ); await batchMessages(async () => { @@ -323,8 +333,13 @@ export async function set6MonthAvg({ }: { month: string; }): Promise { - const categories = await db.all( - 'SELECT * FROM v_categories WHERE tombstone = 0', + const categories = await db.all( + ` + SELECT c.* + FROM categories c + LEFT JOIN category_groups g ON c.cat_group = g.id + WHERE c.tombstone = 0 AND c.hidden = 0 AND g.hidden = 0 + `, ); await batchMessages(async () => { diff --git a/packages/loot-core/src/server/db/types/index.ts b/packages/loot-core/src/server/db/types/index.ts index c6accb44f0..4358620540 100644 --- a/packages/loot-core/src/server/db/types/index.ts +++ b/packages/loot-core/src/server/db/types/index.ts @@ -286,6 +286,17 @@ export type DbViewCategory = { tombstone: DbCategory['tombstone']; }; +export type DbViewCategoryWithGroupHidden = { + id: DbCategory['id']; + name: DbCategory['name']; + is_income: DbCategory['is_income']; + hidden: DbCategory['hidden']; + group: DbCategoryGroup['id']; + group_hidden: DbCategoryGroup['hidden']; + sort_order: DbCategory['sort_order']; + tombstone: DbCategory['tombstone']; +}; + export type DbViewPayee = { id: DbPayee['id']; name: DbAccount['name'] | DbPayee['name']; diff --git a/upcoming-release-notes/5239.md b/upcoming-release-notes/5239.md new file mode 100644 index 0000000000..6af17332fd --- /dev/null +++ b/upcoming-release-notes/5239.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [lougeorge] +--- + +Fixed the functions to set budget targets based on past averages to correctly ignore hidden categories. Further work is required to handle hidden category groups, including potentially creating new views or migrations to expose relevant information to the target setting functions.