Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Goals] Add copy template #3617

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/loot-core/src/server/budget/goal-template.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}


Expand Down
1 change: 0 additions & 1 deletion packages/loot-core/src/server/budget/goals/goalsAverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions packages/loot-core/src/server/budget/goals/goalsCopy.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
19 changes: 19 additions & 0 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3617.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [youngcw]
---

Add goal template to copy budget from X months prior