Skip to content

A local-first personal finance app

automation UI: seperate percentage sources in error logic (#7694)

Matt Fiddaman8366c442a241
diff --git a/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts b/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
index 3fb6074420..f324c28d9f 100644
--- a/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
+++ b/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
@@ -7,7 +7,7 @@ export function useBudgetAutomationCategories() {
const { t } = useTranslation();
const { data: { grouped } = { grouped: [] } } = useCategories();
const categories = useMemo(() => {
- const incomeGroup = grouped.filter(group => group.name === 'Income')[0];
+ const incomeGroups = grouped.filter(group => group.is_income);
return [
{
id: '',
@@ -21,7 +21,10 @@ export function useBudgetAutomationCategories() {
},
],
},
- { ...incomeGroup, name: t('Income categories') },
+ ...incomeGroups.map(group => ({
+ ...group,
+ name: t('Income categories'),
+ })),
];
}, [grouped, t]);
diff --git a/packages/desktop-client/src/components/budget/goals/validateAutomation.test.ts b/packages/desktop-client/src/components/budget/goals/validateAutomation.test.ts
new file mode 100644
index 0000000000..f8569dd04e
--- /dev/null
+++ b/packages/desktop-client/src/components/budget/goals/validateAutomation.test.ts
@@ -0,0 +1,69 @@
+import type { Template } from '@actual-app/core/types/models/templates';
+import { describe, expect, it } from 'vitest';
+
+import { validatePercentageAllocation } from './validateAutomation';
+
+function percent(
+ category: string,
+ percent: number,
+ previous = false,
+): Template {
+ return {
+ type: 'percentage',
+ percent,
+ previous,
+ category,
+ directive: 'template',
+ priority: 1,
+ };
+}
+
+describe('validatePercentageAllocation', () => {
+ it('returns null when no percentage templates are present', () => {
+ expect(validatePercentageAllocation([])).toBeNull();
+ });
+
+ it('flags a single source over 100%', () => {
+ expect(
+ validatePercentageAllocation([
+ percent('Salary', 60),
+ percent('Salary', 50),
+ ]),
+ ).toEqual({ kind: 'percent-over-100', total: 110 });
+ });
+
+ it('does not sum across distinct income sources', () => {
+ expect(
+ validatePercentageAllocation([
+ percent('Income-HSA', 100, true),
+ percent('Interest-HSA', 100),
+ ]),
+ ).toBeNull();
+ });
+
+ it('treats this-month and last-month income as different sources', () => {
+ expect(
+ validatePercentageAllocation([
+ percent('Salary', 100, false),
+ percent('Salary', 100, true),
+ ]),
+ ).toBeNull();
+ });
+
+ it('ignores templates with a missing source', () => {
+ const orphan = {
+ ...percent('Salary', 100),
+ category: null as unknown as string,
+ };
+ expect(validatePercentageAllocation([orphan])).toBeNull();
+ });
+
+ it('matches sources case-insensitively', () => {
+ expect(
+ validatePercentageAllocation([
+ percent('Salary', 60),
+ percent('salary', 50),
+ ]),
+ ).toEqual({ kind: 'percent-over-100', total: 110 });
+ });
+});
diff --git a/packages/desktop-client/src/components/budget/goals/validateAutomation.ts b/packages/desktop-client/src/components/budget/goals/validateAutomation.ts
index 781d46bf69..5d8239f8e0 100644
--- a/packages/desktop-client/src/components/budget/goals/validateAutomation.ts
+++ b/packages/desktop-client/src/components/budget/goals/validateAutomation.ts
@@ -93,3 +93,18 @@ export function validateAutomation(
return null;
}
}
+
+export function validatePercentageAllocation(
+ templates: readonly Template[],
+): GlobalConflictKind | null {
+ const percentBySource = new Map<string, number>();
+ for (const t of templates) {
+ if (t.type !== 'percentage' || !t.category) continue;
+ const key = `${t.previous}|${t.category.toLocaleLowerCase()}`;
+ percentBySource.set(key, (percentBySource.get(key) ?? 0) + t.percent);
+ }
+ const maxPercent = Math.max(0, ...percentBySource.values());
+ return maxPercent > 100
+ ? { kind: 'percent-over-100', total: maxPercent }
+ : null;
+}
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx
index fa6116377e..ed7c5494e1 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx
@@ -19,8 +19,10 @@ import {
} from '#components/budget/goals/automationExamples';
import type { AutomationEntry } from '#components/budget/goals/automationExamples';
import { formatMonthLabel } from '#components/budget/goals/formatMonthLabel';
-import { validateAutomation } from '#components/budget/goals/validateAutomation';
-import type { GlobalConflictKind } from '#components/budget/goals/validateAutomation';
+import {
+ validateAutomation,
+ validatePercentageAllocation,
+} from '#components/budget/goals/validateAutomation';
import { Link } from '#components/common/Link';
import { useFormat } from '#hooks/useFormat';
import { useLocale } from '#hooks/useLocale';
@@ -212,12 +214,7 @@ export function BudgetAutomationsBody({
dryRun?.perTemplate?.[i] != null ? dryRun.perTemplate[i] : null,
);
const hasErrors = automationErrors.some(error => error !== null);
- const percentSum = templates.reduce<number>((sum, t) => {
- if (t.type === 'percentage') return sum + t.percent;
- return sum;
- }, 0);
- const conflict: GlobalConflictKind | null =
- percentSum > 100 ? { kind: 'percent-over-100', total: percentSum } : null;
+ const conflict = validatePercentageAllocation(templates);
const categoryNameMap: Record<string, string> = {};
for (const group of categories) {
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.tsx
index 22cd3d5163..5172179fa5 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.tsx
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.tsx
@@ -65,9 +65,20 @@ export function BudgetAutomationsModal({
hasSpendTemplate ||
hasCleanupDirective;
+ const incomeNameToId = new Map<string, string>();
+ for (const group of categories) {
+ for (const cat of group.categories ?? []) {
+ if (cat.name) incomeNameToId.set(cat.name.toLowerCase(), cat.id);
+ }
+ }
+ const resolved = parsedTemplates?.map(t => {
+ if (t.type !== 'percentage' || !t.category) return t;
+ const id = incomeNameToId.get(t.category.toLowerCase());
+ return id ? { ...t, category: id } : t;
+ });
const initialEntries =
- parsedTemplates && !hasUnsupportedDirective
- ? migrateTemplatesToAutomations(parsedTemplates)
+ resolved && !hasUnsupportedDirective
+ ? migrateTemplatesToAutomations(resolved)
: null;
return (
diff --git a/upcoming-release-notes/7694.md b/upcoming-release-notes/7694.md
new file mode 100644
index 0000000000..126d91ac0a
--- /dev/null
+++ b/upcoming-release-notes/7694.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [matt-fidd]
+---
+
+Fix percentage calculation in automation UI error message