From a3e3c78c5cd4c40ae7fe2d108f7adfbe35b6ca84 Mon Sep 17 00:00:00 2001
From: shall0pass <20625555+shall0pass@users.noreply.github.com>
Date: Tue, 11 Jul 2023 14:17:05 -0500
Subject: [PATCH] Goals: Improve remainder calculation when combined with other
 keywords (#1288)

---
 .../loot-core/src/server/budget/goaltemplates.ts    | 13 ++++++++++---
 upcoming-release-notes/1288.md                      |  6 ++++++
 2 files changed, 16 insertions(+), 3 deletions(-)
 create mode 100644 upcoming-release-notes/1288.md

diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts
index e6d900a12..12a78d706 100644
--- a/packages/loot-core/src/server/budget/goaltemplates.ts
+++ b/packages/loot-core/src/server/budget/goaltemplates.ts
@@ -603,9 +603,16 @@ async function applyCategoryTemplate(
         break;
       }
       case 'remainder': {
-        to_budget = Math.round(remainder_scale * template.weight);
-        // can over budget with the rounding, so checking that
-        if (to_budget > budgetAvailable) to_budget = budgetAvailable;
+        if (remainder_scale >= 0) {
+          to_budget +=
+            remainder_scale === 0
+              ? Math.round(template.weight)
+              : Math.round(remainder_scale * template.weight);
+          // can over budget with the rounding, so checking that
+          if (to_budget >= budgetAvailable + budgeted) {
+            to_budget = budgetAvailable + budgeted;
+          }
+        }
         break;
       }
       case 'error':
diff --git a/upcoming-release-notes/1288.md b/upcoming-release-notes/1288.md
new file mode 100644
index 000000000..496d975cc
--- /dev/null
+++ b/upcoming-release-notes/1288.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [shall0pass]
+---
+
+Goals: Improved calculation when 'remainder' keyword is used with other keywords in the same category.
\ No newline at end of file
-- 
GitLab