Skip to content

A local-first personal finance app

[Goals] fix not budgeting enough when schedule amount from previous month was less than this month (#6268)

youngcw962fe4477271
diff --git a/packages/loot-core/src/server/budget/schedule-template.ts b/packages/loot-core/src/server/budget/schedule-template.ts
index 0c5487fc62..9b3ec30dfe 100644
--- a/packages/loot-core/src/server/budget/schedule-template.ts
+++ b/packages/loot-core/src/server/budget/schedule-template.ts
@@ -10,7 +10,7 @@ import { ScheduleTemplate, Template } from '../../types/models/templates';
import * as db from '../db';
import { getRuleForSchedule } from '../schedules/app';
-import { isReflectBudget } from './actions';
+import { getSheetValue, isReflectBudget } from './actions';
type ScheduleTemplateTarget = {
name: string;
@@ -285,8 +285,21 @@ export async function runSchedule(
const totalSinking = getSinkingTotal(t_sinking);
const totalSinkingBaseContribution =
getSinkingBaseContributionTotal(t_sinking);
+ const lastMonthGoal = await getSheetValue(
+ monthUtils.sheetForMonth(monthUtils.subMonths(current_month, 1)),
+ `goal-${category.id}`,
+ );
- if (balance >= totalSinking + totalPayMonthOf) {
+ // check and see if we should budget the full amount becaue the previous schedules
+ // haven't been paid yet, or if we can use the leftover balance for this month
+ // First option: check if the previous month doesn't have its monthly schedules paid yet
+ // Second option: check if the previous month needed less than this month and hasn't paid yet
+ if (
+ balance >= totalSinking + totalPayMonthOf ||
+ (lastMonthGoal < totalSinking + totalPayMonthOf &&
+ lastMonthGoal !== 0 &&
+ balance >= lastMonthGoal)
+ ) {
to_budget += Math.round(totalPayMonthOf + totalSinkingBaseContribution);
} else {
const totalSinkingContribution = await getSinkingContributionTotal(
diff --git a/upcoming-release-notes/6268.md b/upcoming-release-notes/6268.md
new file mode 100644
index 0000000000..f586a81c27
--- /dev/null
+++ b/upcoming-release-notes/6268.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [youngcw]
+---
+
+Fix schedule templates not budgeting enough under certain conditions