From 90a83873c04c8af1dbfd4891fedfc499efe7aad8 Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:13:15 -0700 Subject: [PATCH 1/7] start of new prev flag --- .../src/server/budget/goal-template.pegjs | 6 +++-- .../src/server/budget/goaltemplates.ts | 22 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjs index e0b6d97c83b..d4c8de41e71 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 _ 'prev'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 cdfe1b48c5a..fbca70b0c9b 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -500,8 +500,14 @@ 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 { @@ -514,11 +520,17 @@ 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, 'total-income'); + } else { + monthlyIncome = await getSheetValue( + sheetName, + `sum-amount-${income_category.id}`, + ); + } } + let increment = Math.max( 0, Math.round(monthlyIncome * (percent / 100)), From d5daecd7fe6e5be9749395cc792e299fe9d55f7c Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:42:48 -0700 Subject: [PATCH 2/7] cleanup and lint --- .../src/server/budget/goaltemplates.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index 03ca95a3cea..0badcfbd504 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -552,10 +552,18 @@ async function applyCategoryTemplate( if (template.category.toLowerCase() === 'all income') { if (template.previous) { - let sheetname_lastmonth = monthUtils.sheetForMonth(monthUtils.addMonths(month, -1)); - monthlyIncome = await getSheetValue(sheetname_lastmonth, 'total-income'); + let sheetName_lastmonth = monthUtils.sheetForMonth( + monthUtils.addMonths(month, -1), + ); + monthlyIncome = await getSheetValue( + sheetName_lastmonth, + 'total-income', + ); } else { - monthlyIncome = await getSheetValue(sheetName, `total-income`); + monthlyIncome = await getSheetValue( + sheetName, + `total-income` + ); } } else if (template.category.toLowerCase() === 'available funds') { monthlyIncome = available_start; @@ -570,8 +578,13 @@ async function applyCategoryTemplate( return { errors }; } if (template.previous) { - let sheetname_lastmonth = monthUtils.sheetForMonth(monthUtils.addMonths(month, -1)); - monthlyIncome = await getSheetValue(sheetname_lastmonth, 'total-income'); + let sheetName_lastmonth = monthUtils.sheetForMonth( + monthUtils.addMonths(month, -1), + ); + monthlyIncome = await getSheetValue( + sheetName_lastmonth, + `sum-amount-${income_category.id}`, + ); } else { monthlyIncome = await getSheetValue( sheetName, From 606af7c290c9f8346ce54064ecaa0b4b9e1eb5eb Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:49:24 -0700 Subject: [PATCH 3/7] revert unnecessary change --- packages/loot-core/src/server/budget/goal-template.pegjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjs index d4c8de41e71..1286a15ba75 100644 --- a/packages/loot-core/src/server/budget/goal-template.pegjs +++ b/packages/loot-core/src/server/budget/goal-template.pegjs @@ -1,7 +1,7 @@ // https://peggyjs.org expr - = priority: priority? _? percentOf:percentOf category: name + = 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 }} From 62d7eda51ec9bbb6d83dbf8f1b5c550da8e56658 Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:52:27 -0700 Subject: [PATCH 4/7] release note --- upcoming-release-notes/1403.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 upcoming-release-notes/1403.md diff --git a/upcoming-release-notes/1403.md b/upcoming-release-notes/1403.md new file mode 100644 index 00000000000..963800ffd5f --- /dev/null +++ b/upcoming-release-notes/1403.md @@ -0,0 +1,6 @@ +--- +category: Enhancement +authors: [youngcw] +--- + +Goals: add "prev" flag to percent goal to use previous month income. From ba460ae1ddbb80a909fdd72e945374b2a5462ee7 Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:54:06 -0700 Subject: [PATCH 5/7] fix --- upcoming-release-notes/1403.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upcoming-release-notes/1403.md b/upcoming-release-notes/1403.md index 963800ffd5f..f2af7faa735 100644 --- a/upcoming-release-notes/1403.md +++ b/upcoming-release-notes/1403.md @@ -1,5 +1,5 @@ --- -category: Enhancement +category: Enhancements authors: [youngcw] --- From a8454eca53c9e40061eff0474f7c2f559b9f8d2c Mon Sep 17 00:00:00 2001 From: youngcw Date: Thu, 27 Jul 2023 14:58:17 -0700 Subject: [PATCH 6/7] i dissagree with the lint this time --- packages/loot-core/src/server/budget/goaltemplates.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index 0badcfbd504..5a7f1a832a8 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -560,10 +560,7 @@ async function applyCategoryTemplate( 'total-income', ); } else { - monthlyIncome = await getSheetValue( - sheetName, - `total-income` - ); + monthlyIncome = await getSheetValue(sheetName, `total-income`); } } else if (template.category.toLowerCase() === 'available funds') { monthlyIncome = available_start; From f7a4c25a791bc9fb3297b899b01b10822dd89a21 Mon Sep 17 00:00:00 2001 From: youngcw Date: Fri, 28 Jul 2023 12:34:19 -0700 Subject: [PATCH 7/7] previous --- packages/loot-core/src/server/budget/goal-template.pegjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjs index 1286a15ba75..094ece260a4 100644 --- a/packages/loot-core/src/server/budget/goal-template.pegjs +++ b/packages/loot-core/src/server/budget/goal-template.pegjs @@ -33,7 +33,7 @@ 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 _ 'prev'i _ { return { percent: percent, prev: true} } +percentOf = percent:percent _ of _ 'previous'i _ { return { percent: percent, prev: true} } / percent:percent _ of _ { return { percent: percent, prev: false} } weekCount