Skip to content
Snippets Groups Projects
Unverified Commit 4ed9f4ff authored by youngcw's avatar youngcw Committed by GitHub
Browse files

[Goals]add template to set x month average (#2688)

* add template to set x month average

* error on 0

* note

* lint
parent 176c8466
No related branches found
No related tags found
No related merge requests found
......@@ -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'
......
// @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:
......
---
category: Enhancements
authors: [youngcw]
---
Goals: Add template to budget X months average spending. Matches the funcion of the existing budget page button.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment