Skip to content

A local-first personal finance app

[AI] Fix schedule template underfunding for long day/week intervals (#8824)

Arbelc58e6c5dc814
diff --git a/packages/loot-core/src/server/budget/schedule-template.test.ts b/packages/loot-core/src/server/budget/schedule-template.test.ts
index 0bca932015..343924a828 100644
--- a/packages/loot-core/src/server/budget/schedule-template.test.ts
+++ b/packages/loot-core/src/server/budget/schedule-template.test.ts
@@ -3,8 +3,9 @@ import { Rule } from '#server/rules';
import { getRuleForSchedule } from '#server/schedules/app';
import type { Currency } from '#shared/currencies';
import type { CategoryEntity } from '#types/models';
+import type { ScheduleTemplate } from '#types/models/templates';
-import { isTrackingBudget } from './actions';
+import { getSheetValue, isTrackingBudget } from './actions';
import { runSchedule } from './schedule-template';
vi.mock('#server/db');
@@ -626,6 +627,45 @@ describe('runSchedule', () => {
expect(result.to_budget).toBe(3000);
});
+ it('keeps meeting the deadline for a schedule repeating every 360 days', async () => {
+ // Every 360 days is daily in shape but yearly in effect, so it sinks:
+ // $500 due 2026-05-15 spread over the five months from January is $100
+ // a month. Once January is funded, February used to drop to $41.67 --
+ // subDays(2026-05-15, 360) lands in May 2025, and that 12-month span
+ // became the divisor. The deadline and the $100 already saved were both
+ // ignored, so the category came up short in May.
+ const template_lines = [
+ {
+ type: 'schedule',
+ name: 'EveryThreeSixtyDays',
+ directive: 'template',
+ priority: 0,
+ } satisfies ScheduleTemplate,
+ ];
+ mockSingleSchedule({
+ start: '2026-05-15',
+ amount: -50000,
+ frequency: 'daily',
+ interval: 360,
+ });
+ // January budgeted $100 and nothing was spent, so it carries forward and
+ // is also last month's goal.
+ vi.mocked(getSheetValue).mockResolvedValue(10000);
+
+ const result = await runSchedule(
+ template_lines,
+ '2026-02-01',
+ 10000,
+ 0,
+ 10000,
+ 0,
+ [],
+ defaultCategory,
+ defaultCurrency,
+ );
+ expect(result.to_budget).toBe(10000);
+ });
+
it('absorbs surplus when last-month balance exceeds a sinking schedule target', async () => {
// Last-month balance ($150) > yearly target ($120). The sink rolls
// the surplus forward and contributes nothing this month.
diff --git a/packages/loot-core/src/server/budget/schedule-template.ts b/packages/loot-core/src/server/budget/schedule-template.ts
index 4faf40a9b1..5b9f1917b2 100644
--- a/packages/loot-core/src/server/budget/schedule-template.ts
+++ b/packages/loot-core/src/server/budget/schedule-template.ts
@@ -342,8 +342,18 @@ export async function runSchedule(
(c.target_frequency === 'daily' && c.target_interval <= 31) ||
isTrackingBudget();
- const isSubMonthly = c =>
- c.target_frequency === 'weekly' || c.target_frequency === 'daily';
+ // A schedule only behaves "sub-monthly" when it recurs more often than
+ // monthly — the same weekly/daily caps `isPayMonthOf` uses. A "repeat every
+ // 360 days" schedule is daily in shape but yearly in effect: letting it
+ // count here wrongly switches the category to the steady-accrual shortcut,
+ // which ignores both the deadline and what has already been saved (#6644).
+ function isSubMonthly(schedule: ScheduleTemplateTarget) {
+ return (
+ (schedule.target_frequency === 'weekly' &&
+ schedule.target_interval <= 4) ||
+ (schedule.target_frequency === 'daily' && schedule.target_interval <= 31)
+ );
+ }
const t_payMonthOf = t.t.filter(isPayMonthOf);
const t_sinking = t.t
diff --git a/upcoming-release-notes/fix-schedule-template-long-day-interval.md b/upcoming-release-notes/fix-schedule-template-long-day-interval.md
new file mode 100644
index 0000000000..787cca004f
--- /dev/null
+++ b/upcoming-release-notes/fix-schedule-template-long-day-interval.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [arbelonson-source]
+---
+
+Fix `#template schedule` underfunding a category when the schedule repeats every N days or weeks and N spans more than a month