A local-first personal finance app
[Goals] Add copy template (#3617)
youngcwc25e97b0f666
diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjsindex dbe2dd3f16..e8805c7c5b 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.tsindex 44151a59d1..2dd7472ad0 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' paramlet 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.tsnew file mode 100644index 0000000000..7932cd7447--- /dev/null+++ b/packages/loot-core/src/server/budget/goals/goalsCopy.ts@@ -0,0 +1,38 @@+// @ts-strict-ignore+import * as monthUtils from '../../../shared/months';+import { amountToInteger } from '../../../shared/util';+import { getSheetValue } from '../actions';++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.tsindex d26485be44..516218496e 100644--- a/packages/loot-core/src/server/budget/goaltemplates.ts+++ b/packages/loot-core/src/server/budget/goaltemplates.ts@@ -8,6 +8,7 @@ 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';@@ -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,diff --git a/upcoming-release-notes/3617.md b/upcoming-release-notes/3617.mdnew file mode 100644index 0000000000..6e686f2154--- /dev/null+++ b/upcoming-release-notes/3617.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [youngcw]+---++Add goal template to copy budget from X months prior