Skip to content

Commit

Permalink
Goals: fix remaining funds calculation (#1410)
Browse files Browse the repository at this point in the history
Fixes #1409. This makes the template processing not include previously
budgeted funds in the return value.
  • Loading branch information
youngcw authored Jul 29, 2023
1 parent e7d8fdf commit f68cb4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -307,6 +312,7 @@ async function applyCategoryTemplate(
remainder_scale,
available_start,
budgetAvailable,
budgeted,
force,
) {
let current_month = `${month}-01`;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1410.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [youngcw]
---

Goals: Fix tracking of remaining funds when using priorities

0 comments on commit f68cb4a

Please sign in to comment.