Skip to content

Commit

Permalink
Goals: add flag to percent goals to use previous month income instead…
Browse files Browse the repository at this point in the history
… of this months (actualbudget#1403)

This is a more elegant way of implementing a month ahead version of the
percent goals. To use it add the `previous` flag to the percent goal, ex
`#template 10% of previous Paycheck`.
  • Loading branch information
youngcw authored Jul 29, 2023
1 parent 49223ce commit 4097e16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/loot-core/src/server/budget/goal-template.pegjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// https://peggyjs.org

expr
= priority: priority? _? percent: percent _ of _ category: name
{ return { type: 'percentage', percent: +percent, category, priority: +priority }}
= priority: priority? _? percentOf:percentOf category: name
{ return { type: 'percentage', percent: +percentOf.percent, previous: percentOf.prev, category, priority: +priority }}
/ priority: priority? _? amount: amount _ repeatEvery _ weeks: weekCount _ starting _ starting: date limit: limit?
{ return { type: 'week', amount, weeks, starting, limit, priority: +priority }}
/ priority: priority? _? amount: amount _ by _ month: month from: spendFrom? repeat: (_ repeatEvery _ repeat)?
Expand Down Expand Up @@ -33,6 +33,8 @@ repeat 'repeat interval'
limit = _? upTo _ amount: amount _ 'hold'i { return {amount: amount, hold: true } }
/ _? upTo _ amount: amount { return {amount: amount, hold: false } }

percentOf = percent:percent _ of _ 'previous'i _ { return { percent: percent, prev: true} }
/ percent:percent _ of _ { return { percent: percent, prev: false} }

weekCount
= week { return null }
Expand Down
32 changes: 27 additions & 5 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,19 @@ async function applyCategoryTemplate(
case 'percentage': {
let percent = template.percent;
let monthlyIncome = 0;

if (template.category.toLowerCase() === 'all income') {
monthlyIncome = await getSheetValue(sheetName, `total-income`);
if (template.previous) {
let sheetName_lastmonth = monthUtils.sheetForMonth(
monthUtils.addMonths(month, -1),
);
monthlyIncome = await getSheetValue(
sheetName_lastmonth,
'total-income',
);
} else {
monthlyIncome = await getSheetValue(sheetName, `total-income`);
}
} else if (template.category.toLowerCase() === 'available funds') {
monthlyIncome = available_start;
} else {
Expand All @@ -568,11 +579,22 @@ async function applyCategoryTemplate(
errors.push(`Could not find category “${template.category}”`);
return { errors };
}
monthlyIncome = await getSheetValue(
sheetName,
`sum-amount-${income_category.id}`,
);
if (template.previous) {
let sheetName_lastmonth = monthUtils.sheetForMonth(
monthUtils.addMonths(month, -1),
);
monthlyIncome = await getSheetValue(
sheetName_lastmonth,
`sum-amount-${income_category.id}`,
);
} else {
monthlyIncome = await getSheetValue(
sheetName,
`sum-amount-${income_category.id}`,
);
}
}

let increment = Math.max(
0,
Math.round(monthlyIncome * (percent / 100)),
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1403.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [youngcw]
---

Goals: add "prev" flag to percent goal to use previous month income.

0 comments on commit 4097e16

Please sign in to comment.