Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goals: add flag to percent goals to use previous month income instead of this months #1403

Merged
merged 9 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -549,8 +549,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 @@ -563,11 +574,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.
Loading