From 1dd1c188d62479d37cd73d6e4ea69bead91d4e60 Mon Sep 17 00:00:00 2001
From: shall0pass <20625555+shall0pass@users.noreply.github.com>
Date: Tue, 2 Apr 2024 10:35:55 -0500
Subject: [PATCH] Goals: Up To Template calculation with negative category
 balance (#2535)

* 'up to' calculation

* note

* update to last months balance
---
 packages/loot-core/src/server/budget/goals/goalsSimple.ts | 3 ++-
 packages/loot-core/src/server/budget/goaltemplates.ts     | 1 +
 upcoming-release-notes/2535.md                            | 6 ++++++
 3 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 upcoming-release-notes/2535.md

diff --git a/packages/loot-core/src/server/budget/goals/goalsSimple.ts b/packages/loot-core/src/server/budget/goals/goalsSimple.ts
index ae3befaab..eec6e9624 100644
--- a/packages/loot-core/src/server/budget/goals/goalsSimple.ts
+++ b/packages/loot-core/src/server/budget/goals/goalsSimple.ts
@@ -8,6 +8,7 @@ export async function goalsSimple(
   limit,
   hold,
   to_budget,
+  last_month_balance,
 ) {
   // simple has 'monthly' and/or 'limit' params
   if (template.limit != null) {
@@ -25,7 +26,7 @@ export async function goalsSimple(
     const monthly = amountToInteger(template.monthly);
     increment = monthly;
   } else {
-    increment = limit;
+    increment = limit - last_month_balance;
   }
   to_budget += increment;
   return { to_budget, errors, limit, limitCheck, hold };
diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts
index 0d94aa777..110a14870 100644
--- a/packages/loot-core/src/server/budget/goaltemplates.ts
+++ b/packages/loot-core/src/server/budget/goaltemplates.ts
@@ -513,6 +513,7 @@ async function applyCategoryTemplate(
           limit,
           hold,
           to_budget,
+          last_month_balance,
         );
         to_budget = goalsReturn.to_budget;
         errors = goalsReturn.errors;
diff --git a/upcoming-release-notes/2535.md b/upcoming-release-notes/2535.md
new file mode 100644
index 000000000..204a85df8
--- /dev/null
+++ b/upcoming-release-notes/2535.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [shall0pass]
+---
+
+Goal templates: Allow budgeting to a full category balance when using 'up to' and a negative category rollover balance.
-- 
GitLab