A local-first personal finance app
Goals: add flag to percent goals to use previous month income instead of this months (#1403)
youngcw9273a0abcfec
diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjsindex e0b6d97c83..094ece260a 100644--- a/packages/loot-core/src/server/budget/goal-template.pegjs+++ b/packages/loot-core/src/server/budget/goal-template.pegjs@@ -1,8 +1,8 @@// https://peggyjs.orgexpr- = 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)?@@ -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 }diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.tsindex 28f8c1a2c3..6ed7cd47c8 100644--- a/packages/loot-core/src/server/budget/goaltemplates.ts+++ b/packages/loot-core/src/server/budget/goaltemplates.ts@@ -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 {@@ -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)),diff --git a/upcoming-release-notes/1403.md b/upcoming-release-notes/1403.mdnew file mode 100644index 0000000000..f2af7faa73--- /dev/null+++ b/upcoming-release-notes/1403.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [youngcw]+---++Goals: add "prev" flag to percent goal to use previous month income.