From f68cb4ae13ea21b39a471ade7dac8a87c526ba8c Mon Sep 17 00:00:00 2001 From: youngcw Date: Fri, 28 Jul 2023 17:10:44 -0700 Subject: [PATCH] Goals: fix remaining funds calculation (#1410) Fixes #1409. This makes the template processing not include previously budgeted funds in the return value. --- .../loot-core/src/server/budget/goaltemplates.ts | 15 ++++++++++----- upcoming-release-notes/1410.md | 6 ++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 upcoming-release-notes/1410.md diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index bf394c4c5f2..28f8c1a2c3b 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -191,6 +191,10 @@ async function processTemplate(month, force, category_templates) { ].join('\n'), ), ); + let prev_budgeted = await getSheetValue( + sheetName, + `budget-${category.id}`, + ); let { amount: to_budget, errors: applyErrors } = await applyCategoryTemplate( category, @@ -200,13 +204,14 @@ async function processTemplate(month, force, category_templates) { remainder_scale, available_start, available_remaining, + prev_budgeted, force, ); if (to_budget != null) { num_applied++; templateBudget.push({ category: category.id, - amount: to_budget, + amount: to_budget + prev_budgeted, }); available_remaining -= to_budget; } @@ -307,6 +312,7 @@ async function applyCategoryTemplate( remainder_scale, available_start, budgetAvailable, + budgeted, force, ) { let current_month = `${month}-01`; @@ -377,10 +383,9 @@ async function applyCategoryTemplate( }); } let sheetName = monthUtils.sheetForMonth(month); - let budgeted = await getSheetValue(sheetName, `budget-${category.id}`); let spent = await getSheetValue(sheetName, `sum-amount-${category.id}`); let balance = await getSheetValue(sheetName, `leftover-${category.id}`); - let to_budget = budgeted; + let to_budget = 0; let limit; let hold; let last_month_balance = balance - spent - budgeted; @@ -669,8 +674,8 @@ async function applyCategoryTemplate( ? 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; + if (to_budget >= budgetAvailable) { + to_budget = budgetAvailable; // check if there is 1 cent leftover from rounding } else if (budgetAvailable - to_budget === 1) { to_budget = to_budget + 1; diff --git a/upcoming-release-notes/1410.md b/upcoming-release-notes/1410.md new file mode 100644 index 00000000000..1b10d4fa120 --- /dev/null +++ b/upcoming-release-notes/1410.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [youngcw] +--- + +Goals: Fix tracking of remaining funds when using priorities