mirror of
https://github.com/actualbudget/actual.git
synced 2026-03-09 03:32:54 -05:00
[Goals]add template to set x month average (#2688)
* add template to set x month average * error on 0 * note * lint
This commit is contained in:
@@ -22,6 +22,8 @@ expr
|
||||
{ return { type: 'schedule', name, priority: +priority, full } }
|
||||
/ priority: priority? _? remainder: remainder
|
||||
{ return { type: 'remainder', priority: null, weight: remainder } }
|
||||
/ priority: priority? _? 'average'i _ amount: positive _ 'months'i?
|
||||
{ return { type: 'average', amount: +amount, priority: +priority }}
|
||||
|
||||
|
||||
repeat 'repeat interval'
|
||||
|
||||
31
packages/loot-core/src/server/budget/goals/goalsAverage.ts
Normal file
31
packages/loot-core/src/server/budget/goals/goalsAverage.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// @ts-strict-ignore
|
||||
|
||||
import * as monthUtils from '../../../shared/months';
|
||||
import { getSheetValue } from '../actions';
|
||||
|
||||
export async function goalsAverage(
|
||||
template,
|
||||
month,
|
||||
category,
|
||||
errors,
|
||||
to_budget,
|
||||
) {
|
||||
// simple has an 'amount' param
|
||||
let increment = 0;
|
||||
if (template.amount) {
|
||||
let sum = 0;
|
||||
for (let i = 1; i <= template.amount; i++) {
|
||||
// add up other months
|
||||
const sheetName = monthUtils.sheetForMonth(
|
||||
monthUtils.subMonths(month, i),
|
||||
);
|
||||
sum += await getSheetValue(sheetName, `sum-amount-${category.id}`);
|
||||
}
|
||||
increment = sum / template.amount;
|
||||
} else {
|
||||
errors.push('Number of months to average is not valid');
|
||||
return { to_budget, errors };
|
||||
}
|
||||
to_budget += -Math.round(increment);
|
||||
return { to_budget, errors };
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { batchMessages } from '../sync';
|
||||
|
||||
import { setBudget, getSheetValue, isReflectBudget, setGoal } from './actions';
|
||||
import { parse } from './goal-template.pegjs';
|
||||
import { goalsAverage } from './goals/goalsAverage';
|
||||
import { goalsBy } from './goals/goalsBy';
|
||||
import { goalsPercentage } from './goals/goalsPercentage';
|
||||
import { findRemainder, goalsRemainder } from './goals/goalsRemainder';
|
||||
@@ -609,6 +610,18 @@ async function applyCategoryTemplate(
|
||||
to_budget = goalsReturn.to_budget;
|
||||
break;
|
||||
}
|
||||
case 'average': {
|
||||
const goalsReturn = await goalsAverage(
|
||||
template,
|
||||
current_month,
|
||||
category,
|
||||
errors,
|
||||
to_budget,
|
||||
);
|
||||
to_budget = goalsReturn.to_budget;
|
||||
errors = goalsReturn.errors;
|
||||
break;
|
||||
}
|
||||
case 'error':
|
||||
return { errors };
|
||||
default:
|
||||
|
||||
6
upcoming-release-notes/2688.md
Normal file
6
upcoming-release-notes/2688.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
category: Enhancements
|
||||
authors: [youngcw]
|
||||
---
|
||||
|
||||
Goals: Add template to budget X months average spending. Matches the funcion of the existing budget page button.
|
||||
Reference in New Issue
Block a user