From 4097e16de55f7bda4d58e6a410326a7dfbab70b2 Mon Sep 17 00:00:00 2001 From: youngcw Date: Fri, 28 Jul 2023 17:11:19 -0700 Subject: [PATCH] Goals: add flag to percent goals to use previous month income instead of this months (#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`. --- .../src/server/budget/goal-template.pegjs | 6 ++-- .../src/server/budget/goaltemplates.ts | 32 ++++++++++++++++--- upcoming-release-notes/1403.md | 6 ++++ 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 upcoming-release-notes/1403.md diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjs index e0b6d97c83b..094ece260a4 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.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)? @@ -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.ts index 28f8c1a2c3b..6ed7cd47c84 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.md new file mode 100644 index 00000000000..f2af7faa735 --- /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.