Skip to content

A local-first personal finance app

Do not count hidden categories in tracking budgets + cleanup table math (#4567)

lelemma657021303dd
diff --git a/packages/loot-core/src/server/budget/base.test.ts b/packages/loot-core/src/server/budget/base.test.ts
index 0724a264bd..b8b00747b8 100644
--- a/packages/loot-core/src/server/budget/base.test.ts
+++ b/packages/loot-core/src/server/budget/base.test.ts
@@ -64,4 +64,277 @@ describe('Base budget', () => {
),
).toBe(-5000);
});
+
+ it('Excludes hidden categories from group totals in Report Budget', async () => {
+ await sheet.loadSpreadsheet(db);
+ sheet.get().meta().budgetType = 'report';
+
+ // Create a group with multiple categories
+ await db.insertCategoryGroup({ id: 'group1', name: 'Test Group' });
+ await db.insertCategoryGroup({
+ id: 'group2',
+ name: 'Income',
+ is_income: 1,
+ });
+
+ const visibleCatId = await db.insertCategory({
+ name: 'Visible Category',
+ cat_group: 'group1',
+ });
+
+ const hiddenCatId = await db.insertCategory({
+ name: 'Hidden Category',
+ cat_group: 'group1',
+ hidden: 1,
+ });
+
+ await createAllBudgets();
+ const month = '2017-01';
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ await db.insertAccount({ id: 'account1', name: 'Account 1' });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -1000,
+ account: 'account1',
+ category: visibleCatId,
+ });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -2000,
+ account: 'account1',
+ category: hiddenCatId,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // Verify individual category amounts
+ expect(sheet.getCellValue(sheetName, `sum-amount-${visibleCatId}`)).toBe(
+ -1000,
+ );
+
+ expect(sheet.getCellValue(sheetName, `sum-amount-${hiddenCatId}`)).toBe(
+ -2000,
+ );
+
+ // Verify group total only includes visible category
+ expect(sheet.getCellValue(sheetName, `group-sum-amount-group1`)).toBe(
+ -1000,
+ );
+
+ // Now toggle hidden status of the hidden category to make it visible
+ await db.updateCategory({
+ id: hiddenCatId,
+ name: 'Hidden Category',
+ cat_group: 'group1',
+ is_income: 0,
+ hidden: 0,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // After making hidden category visible, group total should include both
+ expect(sheet.getCellValue(sheetName, `group-sum-amount-group1`)).toBe(
+ -3000,
+ );
+ });
+
+ it('Excludes hidden category groups from budget totals in Report Budget', async () => {
+ await sheet.loadSpreadsheet(db);
+ sheet.get().meta().budgetType = 'report';
+
+ // Create two expense groups - one visible, one hidden
+ await db.insertCategoryGroup({
+ id: 'visible-group',
+ name: 'Visible Group',
+ });
+ await db.insertCategoryGroup({
+ id: 'hidden-group',
+ name: 'Hidden Group',
+ hidden: 1,
+ });
+
+ await db.insertCategoryGroup({
+ id: 'income-group',
+ name: 'Income',
+ is_income: 1,
+ });
+
+ const visibleGroupCatId = await db.insertCategory({
+ name: 'Visible Group Category',
+ cat_group: 'visible-group',
+ });
+
+ const hiddenGroupCatId = await db.insertCategory({
+ name: 'Hidden Group Category',
+ cat_group: 'hidden-group',
+ });
+
+ await createAllBudgets();
+ const month = '2017-01';
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ await db.insertAccount({ id: 'account1', name: 'Account 1' });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -1000,
+ account: 'account1',
+ category: visibleGroupCatId,
+ });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -2000,
+ account: 'account1',
+ category: hiddenGroupCatId,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // Verify individual amounts
+ expect(
+ sheet.getCellValue(sheetName, `sum-amount-${visibleGroupCatId}`),
+ ).toBe(-1000);
+
+ expect(
+ sheet.getCellValue(sheetName, `sum-amount-${hiddenGroupCatId}`),
+ ).toBe(-2000);
+
+ expect(
+ sheet.getCellValue(sheetName, `group-sum-amount-visible-group`),
+ ).toBe(-1000);
+
+ expect(sheet.getCellValue(sheetName, `group-sum-amount-hidden-group`)).toBe(
+ -2000,
+ );
+
+ // Verify total spent only includes visible group
+ expect(sheet.getCellValue(sheetName, 'total-spent')).toBe(-1000);
+
+ // Now toggle hidden status of the hidden group to make it visible
+ await db.updateCategoryGroup({
+ id: 'hidden-group',
+ name: 'Hidden Group',
+ is_income: 0,
+ hidden: 0,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // After making hidden group visible, total should include both
+ expect(sheet.getCellValue(sheetName, 'total-spent')).toBe(-3000);
+ });
+
+ it('Includes hidden categories in group totals for Rollover Budget', async () => {
+ await sheet.loadSpreadsheet(db);
+ // Rollover is the default, but explicit for clarity
+ sheet.get().meta().budgetType = 'rollover';
+
+ // Create a group with multiple categories
+ await db.insertCategoryGroup({ id: 'group1', name: 'Test Group' });
+ await db.insertCategoryGroup({
+ id: 'group2',
+ name: 'Income',
+ is_income: 1,
+ });
+
+ const visibleCatId = await db.insertCategory({
+ name: 'Visible Category',
+ cat_group: 'group1',
+ });
+
+ const hiddenCatId = await db.insertCategory({
+ name: 'Hidden Category',
+ cat_group: 'group1',
+ hidden: 1,
+ });
+
+ await createAllBudgets();
+ const month = '2017-01';
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ await db.insertAccount({ id: 'account1', name: 'Account 1' });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -1000,
+ account: 'account1',
+ category: visibleCatId,
+ });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -2000,
+ account: 'account1',
+ category: hiddenCatId,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // Verify group total includes both visible and hidden category amounts
+ expect(sheet.getCellValue(sheetName, `group-sum-amount-group1`)).toBe(
+ -3000,
+ );
+ });
+
+ it('Includes hidden category groups in budget totals for Rollover Budget', async () => {
+ await sheet.loadSpreadsheet(db);
+ // Rollover is the default, but explicit for clarity
+ sheet.get().meta().budgetType = 'rollover';
+
+ // Create two expense groups - one visible, one hidden
+ await db.insertCategoryGroup({
+ id: 'visible-group',
+ name: 'Visible Group',
+ });
+ await db.insertCategoryGroup({
+ id: 'hidden-group',
+ name: 'Hidden Group',
+ hidden: 1,
+ });
+
+ await db.insertCategoryGroup({
+ id: 'income-group',
+ name: 'Income',
+ is_income: 1,
+ });
+
+ const visibleGroupCatId = await db.insertCategory({
+ name: 'Visible Group Category',
+ cat_group: 'visible-group',
+ });
+
+ const hiddenGroupCatId = await db.insertCategory({
+ name: 'Hidden Group Category',
+ cat_group: 'hidden-group',
+ });
+
+ await createAllBudgets();
+ const month = '2017-01';
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ await db.insertAccount({ id: 'account1', name: 'Account 1' });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -1000,
+ account: 'account1',
+ category: visibleGroupCatId,
+ });
+
+ await db.insertTransaction({
+ date: '2017-01-15',
+ amount: -2000,
+ account: 'account1',
+ category: hiddenGroupCatId,
+ });
+
+ await sheet.waitOnSpreadsheet();
+
+ // Verify total spent includes both visible and hidden group amounts
+ expect(sheet.getCellValue(sheetName, 'total-spent')).toBe(-3000);
+ });
});
diff --git a/packages/loot-core/src/server/budget/base.ts b/packages/loot-core/src/server/budget/base.ts
index 6243bed37b..d184542cc2 100644
--- a/packages/loot-core/src/server/budget/base.ts
+++ b/packages/loot-core/src/server/budget/base.ts
@@ -11,7 +11,6 @@ import { resolveName } from '../spreadsheet/util';
import * as budgetActions from './actions';
import * as envelopeBudget from './envelope';
import * as report from './report';
-import { sumAmounts } from './util';
export function getBudgetType() {
const meta = sheet.get().meta();
@@ -39,7 +38,7 @@ export function getBudgetRange(start: string, end: string) {
return { start, end, range: monthUtils.rangeInclusive(start, end) };
}
-function createCategory(cat, sheetName, prevSheetName, start, end) {
+export function createCategory(cat, sheetName, prevSheetName, start, end) {
sheet.get().createDynamic(sheetName, 'sum-amount-' + cat.id, {
initialValue: 0,
run: () => {
@@ -65,28 +64,6 @@ function createCategory(cat, sheetName, prevSheetName, start, end) {
}
}
-function createCategoryGroup(group, sheetName) {
- sheet.get().createDynamic(sheetName, 'group-sum-amount-' + group.id, {
- initialValue: 0,
- dependencies: group.categories.map(cat => `sum-amount-${cat.id}`),
- run: sumAmounts,
- });
-
- if (!group.is_income || getBudgetType() !== 'rollover') {
- sheet.get().createDynamic(sheetName, 'group-budget-' + group.id, {
- initialValue: 0,
- dependencies: group.categories.map(cat => `budget-${cat.id}`),
- run: sumAmounts,
- });
-
- sheet.get().createDynamic(sheetName, 'group-leftover-' + group.id, {
- initialValue: 0,
- dependencies: group.categories.map(cat => `leftover-${cat.id}`),
- run: sumAmounts,
- });
- }
-}
-
function handleAccountChange(months, oldValue, newValue) {
if (!oldValue || oldValue.offbudget !== newValue.offbudget) {
const rows = db.runQuery<Pick<db.DbTransaction, 'category'>>(
@@ -144,167 +121,6 @@ function handleCategoryMappingChange(months, oldValue, newValue) {
});
}
-function handleCategoryChange(months, oldValue, newValue) {
- function addDeps(sheetName, groupId, catId) {
- sheet
- .get()
- .addDependencies(sheetName, `group-sum-amount-${groupId}`, [
- `sum-amount-${catId}`,
- ]);
- sheet
- .get()
- .addDependencies(sheetName, `group-budget-${groupId}`, [
- `budget-${catId}`,
- ]);
- sheet
- .get()
- .addDependencies(sheetName, `group-leftover-${groupId}`, [
- `leftover-${catId}`,
- ]);
- }
-
- function removeDeps(sheetName, groupId, catId) {
- sheet
- .get()
- .removeDependencies(sheetName, `group-sum-amount-${groupId}`, [
- `sum-amount-${catId}`,
- ]);
- sheet
- .get()
- .removeDependencies(sheetName, `group-budget-${groupId}`, [
- `budget-${catId}`,
- ]);
- sheet
- .get()
- .removeDependencies(sheetName, `group-leftover-${groupId}`, [
- `leftover-${catId}`,
- ]);
- }
-
- const budgetType = getBudgetType();
-
- if (oldValue && oldValue.tombstone === 0 && newValue.tombstone === 1) {
- const id = newValue.id;
- const groupId = newValue.cat_group;
-
- months.forEach(month => {
- const sheetName = monthUtils.sheetForMonth(month);
- removeDeps(sheetName, groupId, id);
- });
- } else if (
- newValue.tombstone === 0 &&
- (!oldValue || oldValue.tombstone === 1)
- ) {
- if (budgetType === 'rollover') {
- envelopeBudget.createBlankCategory(newValue, months);
- }
-
- months.forEach(month => {
- const prevMonth = monthUtils.prevMonth(month);
- const prevSheetName = monthUtils.sheetForMonth(prevMonth);
- const sheetName = monthUtils.sheetForMonth(month);
- const { start, end } = monthUtils.bounds(month);
-
- createCategory(newValue, sheetName, prevSheetName, start, end);
-
- const id = newValue.id;
- const groupId = newValue.cat_group;
-
- if (getBudgetType() === 'rollover') {
- sheet
- .get()
- .addDependencies(sheetName, 'last-month-overspent', [
- `${prevSheetName}!leftover-${id}`,
- `${prevSheetName}!carryover-${id}`,
- ]);
- }
-
- addDeps(sheetName, groupId, id);
- });
- } else if (oldValue && oldValue.cat_group !== newValue.cat_group) {
- // The category moved so we need to update the dependencies
- const id = newValue.id;
-
- months.forEach(month => {
- const sheetName = monthUtils.sheetForMonth(month);
- removeDeps(sheetName, oldValue.cat_group, id);
- addDeps(sheetName, newValue.cat_group, id);
- });
- }
-}
-
-function handleCategoryGroupChange(months, oldValue, newValue) {
- const budgetType = getBudgetType();
-
- function addDeps(sheetName, groupId) {
- sheet
- .get()
- .addDependencies(sheetName, 'total-budgeted', [
- `group-budget-${groupId}`,
- ]);
- sheet
- .get()
- .addDependencies(sheetName, 'total-spent', [
- `group-sum-amount-${groupId}`,
- ]);
- sheet
- .get()
- .addDependencies(sheetName, 'total-leftover', [
- `group-leftover-${groupId}`,
- ]);
- }
-
- function removeDeps(sheetName, groupId) {
- sheet
- .get()
- .removeDependencies(sheetName, 'total-budgeted', [
- `group-budget-${groupId}`,
- ]);
- sheet
- .get()
- .removeDependencies(sheetName, 'total-spent', [
- `group-sum-amount-${groupId}`,
- ]);
- sheet
- .get()
- .removeDependencies(sheetName, 'total-leftover', [
- `group-leftover-${groupId}`,
- ]);
- }
-
- if (newValue.tombstone === 1 && oldValue && oldValue.tombstone === 0) {
- const id = newValue.id;
- months.forEach(month => {
- const sheetName = monthUtils.sheetForMonth(month);
- removeDeps(sheetName, id);
- });
- } else if (
- newValue.tombstone === 0 &&
- (!oldValue || oldValue.tombstone === 1)
- ) {
- const group = newValue;
-
- if (!group.is_income || budgetType !== 'rollover') {
- months.forEach(month => {
- const sheetName = monthUtils.sheetForMonth(month);
-
- // Dirty, dirty hack. These functions should not be async, but this is
- // OK because we're leveraging the sync nature of queries. Ideally we
- // wouldn't be querying here. But I think we have to. At least for now
- // we do
- const categories = db.runQuery(
- 'SELECT * FROM categories WHERE tombstone = 0 AND cat_group = ?',
- [group.id],
- true,
- );
- createCategoryGroup({ ...group, categories }, sheetName);
-
- addDeps(sheetName, group.id);
- });
- }
- }
-}
-
function handleBudgetMonthChange(budget) {
const sheetName = monthUtils.sheetForMonth(budget.id);
sheet.get().set(`${sheetName}!buffered`, budget.buffered);
@@ -331,6 +147,7 @@ function handleBudgetChange(budget) {
export function triggerBudgetChanges(oldValues, newValues) {
const { createdMonths = new Set() } = sheet.get().meta();
+ const budgetType = getBudgetType();
sheet.startTransaction();
try {
@@ -356,9 +173,25 @@ export function triggerBudgetChanges(oldValues, newValues) {
} else if (table === 'category_mapping') {
handleCategoryMappingChange(createdMonths, oldValue, newValue);
} else if (table === 'categories') {
- handleCategoryChange(createdMonths, oldValue, newValue);
+ if (budgetType === 'rollover') {
+ envelopeBudget.handleCategoryChange(
+ createdMonths,
+ oldValue,
+ newValue,
+ );
+ } else {
+ report.handleCategoryChange(createdMonths, oldValue, newValue);
+ }
} else if (table === 'category_groups') {
- handleCategoryGroupChange(createdMonths, oldValue, newValue);
+ if (budgetType === 'rollover') {
+ envelopeBudget.handleCategoryGroupChange(
+ createdMonths,
+ oldValue,
+ newValue,
+ );
+ } else {
+ report.handleCategoryGroupChange(createdMonths, oldValue, newValue);
+ }
} else if (table === 'accounts') {
handleAccountChange(createdMonths, oldValue, newValue);
}
@@ -419,7 +252,11 @@ export async function createBudget(months) {
createCategory(cat, sheetName, prevSheetName, start, end);
});
groups.forEach(group => {
- createCategoryGroup(group, sheetName);
+ if (budgetType === 'rollover') {
+ envelopeBudget.createCategoryGroup(group, sheetName);
+ } else {
+ report.createCategoryGroup(group, sheetName);
+ }
});
if (budgetType === 'rollover') {
@@ -430,7 +267,7 @@ export async function createBudget(months) {
sheetName,
);
} else {
- report.createSummary(groups, categories, sheetName);
+ report.createSummary(groups, sheetName);
}
meta.createdMonths.add(month);
diff --git a/packages/loot-core/src/server/budget/envelope.ts b/packages/loot-core/src/server/budget/envelope.ts
index 122c72fa94..759fbc2c76 100644
--- a/packages/loot-core/src/server/budget/envelope.ts
+++ b/packages/loot-core/src/server/budget/envelope.ts
@@ -1,9 +1,11 @@
// @ts-strict-ignore
import * as monthUtils from '../../shared/months';
import { safeNumber } from '../../shared/util';
+import * as db from '../db';
import * as sheet from '../sheet';
import { resolveName } from '../spreadsheet/util';
+import { createCategory as createCategoryFromBase } from './base';
import { number, sumAmounts, flatten2, unflatten2 } from './util';
function getBlankSheet(months) {
@@ -71,6 +73,28 @@ export function createCategory(cat, sheetName, prevSheetName) {
}
}
+export function createCategoryGroup(group, sheetName) {
+ sheet.get().createDynamic(sheetName, 'group-sum-amount-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories.map(cat => `sum-amount-${cat.id}`),
+ run: sumAmounts,
+ });
+
+ if (!group.is_income) {
+ sheet.get().createDynamic(sheetName, 'group-budget-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories.map(cat => `budget-${cat.id}`),
+ run: sumAmounts,
+ });
+
+ sheet.get().createDynamic(sheetName, 'group-leftover-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories.map(cat => `leftover-${cat.id}`),
+ run: sumAmounts,
+ });
+ }
+}
+
export function createSummary(groups, categories, prevSheetName, sheetName) {
const incomeGroup = groups.filter(group => group.is_income)[0];
const expenseCategories = categories.filter(cat => !cat.is_income);
@@ -178,3 +202,156 @@ export function createBudget(meta, categories, months) {
meta.blankSheet = blankSheet;
}
}
+
+export function handleCategoryChange(months, oldValue, newValue) {
+ function addDeps(sheetName, groupId, catId) {
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-sum-amount-${groupId}`, [
+ `sum-amount-${catId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-budget-${groupId}`, [
+ `budget-${catId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-leftover-${groupId}`, [
+ `leftover-${catId}`,
+ ]);
+ }
+
+ function removeDeps(sheetName, groupId, catId) {
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-sum-amount-${groupId}`, [
+ `sum-amount-${catId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-budget-${groupId}`, [
+ `budget-${catId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-leftover-${groupId}`, [
+ `leftover-${catId}`,
+ ]);
+ }
+
+ if (oldValue && oldValue.tombstone === 0 && newValue.tombstone === 1) {
+ const id = newValue.id;
+ const groupId = newValue.cat_group;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, groupId, id);
+ });
+ } else if (
+ newValue.tombstone === 0 &&
+ (!oldValue || oldValue.tombstone === 1)
+ ) {
+ createBlankCategory(newValue, months);
+
+ months.forEach(month => {
+ const prevMonth = monthUtils.prevMonth(month);
+ const prevSheetName = monthUtils.sheetForMonth(prevMonth);
+ const sheetName = monthUtils.sheetForMonth(month);
+ const { start, end } = monthUtils.bounds(month);
+
+ createCategoryFromBase(newValue, sheetName, prevSheetName, start, end);
+
+ const id = newValue.id;
+ const groupId = newValue.cat_group;
+
+ sheet
+ .get()
+ .addDependencies(sheetName, 'last-month-overspent', [
+ `${prevSheetName}!leftover-${id}`,
+ `${prevSheetName}!carryover-${id}`,
+ ]);
+
+ addDeps(sheetName, groupId, id);
+ });
+ } else if (oldValue && oldValue.cat_group !== newValue.cat_group) {
+ // The category moved so we need to update the dependencies
+ const id = newValue.id;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, oldValue.cat_group, id);
+ addDeps(sheetName, newValue.cat_group, id);
+ });
+ }
+}
+
+export function handleCategoryGroupChange(months, oldValue, newValue) {
+ function addDeps(sheetName, groupId) {
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-budgeted', [
+ `group-budget-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-spent', [
+ `group-sum-amount-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-leftover', [
+ `group-leftover-${groupId}`,
+ ]);
+ }
+
+ function removeDeps(sheetName, groupId) {
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-budgeted', [
+ `group-budget-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-spent', [
+ `group-sum-amount-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-leftover', [
+ `group-leftover-${groupId}`,
+ ]);
+ }
+
+ if (newValue.tombstone === 1 && oldValue && oldValue.tombstone === 0) {
+ const id = newValue.id;
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, id);
+ });
+ } else if (
+ newValue.tombstone === 0 &&
+ (!oldValue || oldValue.tombstone === 1)
+ ) {
+ const group = newValue;
+
+ if (!group.is_income) {
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ // Dirty, dirty hack. These functions should not be async, but this is
+ // OK because we're leveraging the sync nature of queries. Ideally we
+ // wouldn't be querying here. But I think we have to. At least for now
+ // we do
+ const categories = db.runQuery(
+ 'SELECT * FROM categories WHERE tombstone = 0 AND cat_group = ?',
+ [group.id],
+ true,
+ );
+ createCategoryGroup({ ...group, categories }, sheetName);
+
+ addDeps(sheetName, group.id);
+ });
+ }
+ }
+}
diff --git a/packages/loot-core/src/server/budget/report.ts b/packages/loot-core/src/server/budget/report.ts
index 5f63dc1754..fab9dccd2d 100644
--- a/packages/loot-core/src/server/budget/report.ts
+++ b/packages/loot-core/src/server/budget/report.ts
@@ -1,8 +1,11 @@
// @ts-strict-ignore
+import * as monthUtils from '../../shared/months';
import { safeNumber } from '../../shared/util';
+import * as db from '../db';
import * as sheet from '../sheet';
import { resolveName } from '../spreadsheet/util';
+import { createCategory as createCategoryFromBase } from './base';
import { number, sumAmounts } from './util';
export async function createCategory(cat, sheetName, prevSheetName) {
@@ -60,24 +63,47 @@ export async function createCategory(cat, sheetName, prevSheetName) {
sheet.get().createStatic(sheetName, `carryover-${cat.id}`, false);
}
-export function createSummary(groups, categories, sheetName) {
+export function createCategoryGroup(group, sheetName) {
+ // different sum amount dependencies
+ sheet.get().createDynamic(sheetName, 'group-sum-amount-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories
+ .filter(cat => !cat.hidden)
+ .map(cat => `sum-amount-${cat.id}`),
+ run: sumAmounts,
+ });
+ sheet.get().createDynamic(sheetName, 'group-budget-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories
+ .filter(cat => !cat.hidden)
+ .map(cat => `budget-${cat.id}`),
+ run: sumAmounts,
+ });
+ sheet.get().createDynamic(sheetName, 'group-leftover-' + group.id, {
+ initialValue: 0,
+ dependencies: group.categories
+ .filter(cat => !cat.hidden)
+ .map(cat => `leftover-${cat.id}`),
+ run: sumAmounts,
+ });
+}
+
+export function createSummary(groups, sheetName) {
const incomeGroup = groups.filter(group => group.is_income)[0];
- const expenseCategories = categories.filter(cat => !cat.is_income);
+ const expenseGroups = groups.filter(
+ group => !group.is_income && !group.hidden,
+ );
sheet.get().createDynamic(sheetName, 'total-budgeted', {
initialValue: 0,
- dependencies: groups
- .filter(group => !group.is_income)
- .map(group => `group-budget-${group.id}`),
+ dependencies: expenseGroups.map(group => `group-budget-${group.id}`),
run: sumAmounts,
});
sheet.get().createDynamic(sheetName, 'total-spent', {
initialValue: 0,
refresh: true,
- dependencies: expenseCategories.map(
- cat => `${sheetName}!spent-with-carryover-${cat.id}`,
- ),
+ dependencies: expenseGroups.map(group => `group-sum-amount-${group.id}`),
run: sumAmounts,
});
@@ -89,7 +115,7 @@ export function createSummary(groups, categories, sheetName) {
sheet.get().createDynamic(sheetName, 'total-leftover', {
initialValue: 0,
- dependencies: ['total-budgeted', 'total-spent'],
+ dependencies: expenseGroups.map(g => `group-leftover-${g.id}`),
run: sumAmounts,
});
@@ -115,3 +141,168 @@ export function createSummary(groups, categories, sheetName) {
},
});
}
+
+export function handleCategoryChange(months, oldValue, newValue) {
+ function addDeps(sheetName, groupId, catId) {
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-sum-amount-${groupId}`, [
+ `sum-amount-${catId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-budget-${groupId}`, [
+ `budget-${catId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, `group-leftover-${groupId}`, [
+ `leftover-${catId}`,
+ ]);
+ }
+
+ function removeDeps(sheetName, groupId, catId) {
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-sum-amount-${groupId}`, [
+ `sum-amount-${catId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-budget-${groupId}`, [
+ `budget-${catId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, `group-leftover-${groupId}`, [
+ `leftover-${catId}`,
+ ]);
+ }
+
+ if (oldValue && oldValue.tombstone === 0 && newValue.tombstone === 1) {
+ const id = newValue.id;
+ const groupId = newValue.cat_group;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, groupId, id);
+ });
+ } else if (
+ newValue.tombstone === 0 &&
+ (!oldValue || oldValue.tombstone === 1)
+ ) {
+ months.forEach(month => {
+ const prevMonth = monthUtils.prevMonth(month);
+ const prevSheetName = monthUtils.sheetForMonth(prevMonth);
+ const sheetName = monthUtils.sheetForMonth(month);
+ const { start, end } = monthUtils.bounds(month);
+
+ createCategoryFromBase(newValue, sheetName, prevSheetName, start, end);
+
+ const id = newValue.id;
+ const groupId = newValue.cat_group;
+
+ addDeps(sheetName, groupId, id);
+ });
+ } else if (oldValue && oldValue.cat_group !== newValue.cat_group) {
+ // The category moved so we need to update the dependencies
+ const id = newValue.id;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, oldValue.cat_group, id);
+ addDeps(sheetName, newValue.cat_group, id);
+ });
+ } else if (oldValue && oldValue.hidden !== newValue.hidden) {
+ const id = newValue.id;
+ const groupId = newValue.cat_group;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ if (newValue.hidden) {
+ removeDeps(sheetName, groupId, id);
+ } else {
+ addDeps(sheetName, groupId, id);
+ }
+ });
+ }
+}
+
+export function handleCategoryGroupChange(months, oldValue, newValue) {
+ function addDeps(sheetName, groupId) {
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-budgeted', [
+ `group-budget-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-spent', [
+ `group-sum-amount-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .addDependencies(sheetName, 'total-leftover', [
+ `group-leftover-${groupId}`,
+ ]);
+ }
+
+ function removeDeps(sheetName, groupId) {
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-budgeted', [
+ `group-budget-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-spent', [
+ `group-sum-amount-${groupId}`,
+ ]);
+ sheet
+ .get()
+ .removeDependencies(sheetName, 'total-leftover', [
+ `group-leftover-${groupId}`,
+ ]);
+ }
+
+ if (newValue.tombstone === 1 && oldValue && oldValue.tombstone === 0) {
+ const id = newValue.id;
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ removeDeps(sheetName, id);
+ });
+ } else if (
+ newValue.tombstone === 0 &&
+ (!oldValue || oldValue.tombstone === 1)
+ ) {
+ const group = newValue;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+
+ // Dirty, dirty hack. These functions should not be async, but this is
+ // OK because we're leveraging the sync nature of queries. Ideally we
+ // wouldn't be querying here. But I think we have to. At least for now
+ // we do
+ const categories = db.runQuery(
+ 'SELECT * FROM categories WHERE tombstone = 0 AND cat_group = ?',
+ [group.id],
+ true,
+ );
+ createCategoryGroup({ ...group, categories }, sheetName);
+
+ addDeps(sheetName, group.id);
+ });
+ } else if (oldValue && oldValue.hidden !== newValue.hidden) {
+ const group = newValue;
+
+ months.forEach(month => {
+ const sheetName = monthUtils.sheetForMonth(month);
+ if (newValue.hidden) {
+ removeDeps(sheetName, group.id);
+ } else {
+ addDeps(sheetName, group.id);
+ }
+ });
+ }
+}
diff --git a/upcoming-release-notes/4567.md b/upcoming-release-notes/4567.md
new file mode 100644
index 0000000000..cf7ea06179
--- /dev/null
+++ b/upcoming-release-notes/4567.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [psybers, lelemm, youngcw]
+---
+
+Don't include hidden categories or groups in totals in tracking budget.