From 292a8d0e273114ce454615dc9a842a4e20cc3449 Mon Sep 17 00:00:00 2001 From: youngcw Date: Wed, 9 Oct 2024 10:33:56 -0700 Subject: [PATCH 1/3] add copy template --- .../src/server/budget/goal-template.pegjs | 2 + .../src/server/budget/goals/goalsAverage.ts | 1 - .../src/server/budget/goals/goalsCopy.ts | 39 +++++++++++++++++++ .../src/server/budget/goaltemplates.ts | 19 +++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 packages/loot-core/src/server/budget/goals/goalsCopy.ts diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjs index dbe2dd3f16e..e8805c7c5b1 100644 --- a/packages/loot-core/src/server/budget/goal-template.pegjs +++ b/packages/loot-core/src/server/budget/goal-template.pegjs @@ -24,6 +24,8 @@ expr { return { type: 'remainder', priority: null, directive: template.directive, weight: remainder, limit } } / template: template _ 'average'i _ amount: positive _ 'months'i? { return { type: 'average', amount: +amount, priority: template.priority, directive: template.directive }} + / template: template _ 'copy from'i _ lookBack: positive _ 'months ago'i limit:limit? + { return { type: 'copy', priority: template.priority, directive: template.directive, lookBack: +lookBack, limit }} / goal: goal amount: amount { return {type: 'simple', amount: amount, priority: null, directive: 'goal' }} diff --git a/packages/loot-core/src/server/budget/goals/goalsAverage.ts b/packages/loot-core/src/server/budget/goals/goalsAverage.ts index 44151a59d1d..2dd7472ad06 100644 --- a/packages/loot-core/src/server/budget/goals/goalsAverage.ts +++ b/packages/loot-core/src/server/budget/goals/goalsAverage.ts @@ -10,7 +10,6 @@ export async function goalsAverage( errors, to_budget, ) { - // simple has an 'amount' param let increment = 0; if (template.amount) { let sum = 0; diff --git a/packages/loot-core/src/server/budget/goals/goalsCopy.ts b/packages/loot-core/src/server/budget/goals/goalsCopy.ts new file mode 100644 index 00000000000..8578d9cb401 --- /dev/null +++ b/packages/loot-core/src/server/budget/goals/goalsCopy.ts @@ -0,0 +1,39 @@ +// @ts-strict-ignore +import * as monthUtils from '../../../shared/months'; +import { getSheetValue } from '../actions'; + +import { amountToInteger } from '../../../shared/util'; + +export async function goalsCopy( + template, + month, + category, + limitCheck, + errors, + limit, + hold, + to_budget, +) { + // simple has 'monthly' and/or 'limit' params + if (template.limit != null) { + if (limitCheck) { + errors.push(`More than one “up to” limit found.`); + return { to_budget, errors, limit, limitCheck, hold }; + } else { + limitCheck = true; + limit = amountToInteger(template.limit.amount); + hold = template.limit.hold; + } + } + let increment = 0; + if (template.lookBack) { + const sheetName = monthUtils.sheetForMonth( + monthUtils.subMonths(month,template.lookBack) + ); + increment = await getSheetValue(sheetName, `budget-${category.id}`); + } else { + errors.push('Missing number of months to look back'); + } + to_budget += increment; + return { to_budget, errors, limit, limitCheck, hold }; +} diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index d26485be44a..e2bd1437430 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -14,6 +14,7 @@ import { goalsSchedule } from './goals/goalsSchedule'; import { goalsSimple } from './goals/goalsSimple'; import { goalsSpend } from './goals/goalsSpend'; import { goalsWeek } from './goals/goalsWeek'; +import { goalsCopy } from './goals/goalsCopy'; import { checkTemplates, storeTemplates } from './template-notes'; const TEMPLATE_PREFIX = '#template'; @@ -528,6 +529,24 @@ async function applyCategoryTemplate( hold = goalsReturn.hold; break; } + case 'copy': { + const goalsReturn = await goalsCopy( + template, + month, + category, + limitCheck, + errors, + limit, + hold, + to_budget, + ); + to_budget = goalsReturn.to_budget; + errors = goalsReturn.errors; + limit = goalsReturn.limit; + limitCheck = goalsReturn.limitCheck; + hold = goalsReturn.hold; + break; + } case 'by': { const goalsReturn = await goalsBy( template_lines, From 16b807c397078b277ebd6c89da2ac081c9b7c5c9 Mon Sep 17 00:00:00 2001 From: youngcw Date: Wed, 9 Oct 2024 10:38:44 -0700 Subject: [PATCH 2/3] note, lint --- packages/loot-core/src/server/budget/goals/goalsCopy.ts | 5 ++--- packages/loot-core/src/server/budget/goaltemplates.ts | 2 +- upcoming-release-notes/3617.md | 6 ++++++ 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 upcoming-release-notes/3617.md diff --git a/packages/loot-core/src/server/budget/goals/goalsCopy.ts b/packages/loot-core/src/server/budget/goals/goalsCopy.ts index 8578d9cb401..7932cd7447d 100644 --- a/packages/loot-core/src/server/budget/goals/goalsCopy.ts +++ b/packages/loot-core/src/server/budget/goals/goalsCopy.ts @@ -1,8 +1,7 @@ // @ts-strict-ignore import * as monthUtils from '../../../shared/months'; -import { getSheetValue } from '../actions'; - import { amountToInteger } from '../../../shared/util'; +import { getSheetValue } from '../actions'; export async function goalsCopy( template, @@ -28,7 +27,7 @@ export async function goalsCopy( let increment = 0; if (template.lookBack) { const sheetName = monthUtils.sheetForMonth( - monthUtils.subMonths(month,template.lookBack) + monthUtils.subMonths(month, template.lookBack), ); increment = await getSheetValue(sheetName, `budget-${category.id}`); } else { diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index e2bd1437430..516218496e8 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -8,13 +8,13 @@ import { batchMessages } from '../sync'; import { getSheetValue, isReflectBudget, setBudget, setGoal } from './actions'; import { goalsAverage } from './goals/goalsAverage'; import { goalsBy } from './goals/goalsBy'; +import { goalsCopy } from './goals/goalsCopy'; import { goalsPercentage } from './goals/goalsPercentage'; import { findRemainder, goalsRemainder } from './goals/goalsRemainder'; import { goalsSchedule } from './goals/goalsSchedule'; import { goalsSimple } from './goals/goalsSimple'; import { goalsSpend } from './goals/goalsSpend'; import { goalsWeek } from './goals/goalsWeek'; -import { goalsCopy } from './goals/goalsCopy'; import { checkTemplates, storeTemplates } from './template-notes'; const TEMPLATE_PREFIX = '#template'; diff --git a/upcoming-release-notes/3617.md b/upcoming-release-notes/3617.md new file mode 100644 index 00000000000..cb7b894e3dd --- /dev/null +++ b/upcoming-release-notes/3617.md @@ -0,0 +1,6 @@ +--- +category: Enhancement +authors: [youngcw] +--- + +Add goal template to copy budget from X months prior From 86e3e19ba668799371b90dc701381dc979f3d524 Mon Sep 17 00:00:00 2001 From: youngcw Date: Wed, 9 Oct 2024 10:39:46 -0700 Subject: [PATCH 3/3] note --- upcoming-release-notes/3617.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upcoming-release-notes/3617.md b/upcoming-release-notes/3617.md index cb7b894e3dd..6e686f2154a 100644 --- a/upcoming-release-notes/3617.md +++ b/upcoming-release-notes/3617.md @@ -1,5 +1,5 @@ --- -category: Enhancement +category: Enhancements authors: [youngcw] ---