Skip to content

A local-first personal finance app

[AI] Fix phantom overspending after deleting a category (#8756)

youngcw8f2b8fcb61e5
diff --git a/packages/loot-core/src/server/budget/envelope.ts b/packages/loot-core/src/server/budget/envelope.ts
index e7d755e611..1e7695a820 100644
--- a/packages/loot-core/src/server/budget/envelope.ts
+++ b/packages/loot-core/src/server/budget/envelope.ts
@@ -238,49 +238,59 @@ export function createBudget(meta, categories, months) {
}
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}`,
+ // Build list of cells and dependencies that need updated
+ function getDeps(sheetName, prevSheetName, groupId, cat) {
+ const deps: Array<[string, string[]]> = [
+ [`group-sum-amount-${groupId}`, [`sum-amount-${cat.id}`]],
+ [`group-budget-${groupId}`, [`budget-${cat.id}`]],
+ [`group-leftover-${groupId}`, [`leftover-${cat.id}`]],
+ ];
+
+ if (cat.is_income) {
+ deps.push([
+ 'buffered-auto',
+ [
+ `${sheetName}!sum-amount-${cat.id}`,
+ `${sheetName}!carryover-${cat.id}`,
+ ],
]);
- sheet
- .get()
- .addDependencies(sheetName, `group-leftover-${groupId}`, [
- `leftover-${catId}`,
+ } else {
+ deps.push([
+ 'last-month-overspent',
+ [
+ `${prevSheetName}!leftover-${cat.id}`,
+ `${prevSheetName}!carryover-${cat.id}`,
+ ],
]);
+ }
+
+ return deps;
}
- 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}`,
- ]);
+ function addDeps(sheetName, prevSheetName, groupId, cat) {
+ getDeps(sheetName, prevSheetName, groupId, cat).forEach(
+ ([cellName, deps]) => {
+ sheet.get().addDependencies(sheetName, cellName, deps);
+ },
+ );
}
- if (oldValue && oldValue.tombstone === 0 && newValue.tombstone === 1) {
- const id = newValue.id;
- const groupId = newValue.cat_group;
+ function removeDeps(sheetName, prevSheetName, groupId, cat) {
+ getDeps(sheetName, prevSheetName, groupId, cat).forEach(
+ ([cellName, deps]) => {
+ sheet.get().removeDependencies(sheetName, cellName, deps);
+ },
+ );
+ }
+ if (oldValue && oldValue.tombstone === 0 && newValue.tombstone === 1) {
months.forEach(month => {
+ const prevSheetName = monthUtils.sheetForMonth(
+ monthUtils.prevMonth(month),
+ );
const sheetName = monthUtils.sheetForMonth(month);
- removeDeps(sheetName, groupId, id);
+
+ removeDeps(sheetName, prevSheetName, newValue.cat_group, newValue);
});
} else if (
newValue.tombstone === 0 &&
@@ -296,38 +306,18 @@ export function handleCategoryChange(months, oldValue, newValue) {
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);
- if (newValue.is_income) {
- sheet
- .get()
- .addDependencies(
- sheetName,
- 'buffered-auto',
- flatten2([
- `${sheetName}!sum-amount-${id}`,
- `${sheetName}!carryover-${id}`,
- ]),
- );
- }
+ addDeps(sheetName, prevSheetName, newValue.cat_group, newValue);
});
} 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 prevSheetName = monthUtils.sheetForMonth(
+ monthUtils.prevMonth(month),
+ );
const sheetName = monthUtils.sheetForMonth(month);
- removeDeps(sheetName, oldValue.cat_group, id);
- addDeps(sheetName, newValue.cat_group, id);
+
+ removeDeps(sheetName, prevSheetName, oldValue.cat_group, newValue);
+ addDeps(sheetName, prevSheetName, newValue.cat_group, newValue);
});
}
}
diff --git a/packages/loot-core/src/server/main.test.ts b/packages/loot-core/src/server/main.test.ts
index 46f0ad9690..6145851e23 100644
--- a/packages/loot-core/src/server/main.test.ts
+++ b/packages/loot-core/src/server/main.test.ts
@@ -388,4 +388,58 @@ describe('Categories', () => {
categories = await db.getCategories();
expect(categories.find(cat => cat.id === 'income1')).not.toBeDefined();
});
+
+ test('do not count as overspent when deleted', async () => {
+ await sheet.loadSpreadsheet(db);
+
+ await runMutator(async () => {
+ await db.insertCategoryGroup({ id: 'group1', name: 'group1' });
+ await db.insertCategoryGroup({
+ id: 'group2',
+ name: 'income',
+ is_income: 1,
+ });
+ await db.insertCategory({ id: 'foo', name: 'foo', cat_group: 'group1' });
+ await db.insertCategory({ id: 'bar', name: 'bar', cat_group: 'group1' });
+ await db.insertAccount({ id: 'acct', name: 'acct' });
+
+ // A deposit straight into the category, like a refund
+ await db.insertTransaction({
+ date: '2017-01-01',
+ account: 'acct',
+ amount: 10000,
+ category: 'foo',
+ });
+ });
+
+ await budget.createAllBudgets();
+
+ // Move 1000 from `foo` to `bar`, which leaves `foo` with a negative
+ // budgeted amount
+ await budgetActions.setBudget({
+ category: 'foo',
+ month: '2017-01',
+ amount: -1000,
+ });
+ await budgetActions.setBudget({
+ category: 'bar',
+ month: '2017-01',
+ amount: 1000,
+ });
+ await sheet.waitOnSpreadsheet();
+
+ const nextSheetName = monthUtils.sheetForMonth('2017-02');
+ expect(sheet.getCellValue(nextSheetName, 'last-month-overspent')).toBe(0);
+ const toBudget = sheet.getCellValue(nextSheetName, 'to-budget');
+
+ await runHandler(handlers['category-delete'], {
+ id: 'foo',
+ transferId: 'bar',
+ });
+ await sheet.waitOnSpreadsheet();
+
+ // The deleted category should not leave any overspending behind
+ expect(sheet.getCellValue(nextSheetName, 'last-month-overspent')).toBe(0);
+ expect(sheet.getCellValue(nextSheetName, 'to-budget')).toBe(toBudget);
+ });
});
diff --git a/upcoming-release-notes/fix-phantom-overspend-deleted-category.md b/upcoming-release-notes/fix-phantom-overspend-deleted-category.md
new file mode 100644
index 0000000000..0abcb8616d
--- /dev/null
+++ b/upcoming-release-notes/fix-phantom-overspend-deleted-category.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [youngcw]
+---
+
+Fix a phantom overspent amount appearing after deleting a category that had money moved out of it