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] fix limits #3829

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
95 changes: 50 additions & 45 deletions packages/loot-core/src/server/budget/categoryTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ export class CategoryTemplate {
* templates: all templates for this category (including templates and goals)
* categoryID: the ID of the category that this Class will be for
* month: the month string of the month for templates being applied
* 2. gather needed data for external use. ex: remainder weights, priorities
* 2. gather needed data for external use. ex: remainder weights, priorities, limitExcess
* 3. run each priority level that is needed via runTemplatesForPriority
* 4. run applyLimits to apply any existing limit to the category
* 5. run the remainder templates via runRemainder()
* 6. finish processing by running getValues() and saving values for batch processing.
* 4. run the remainder templates via runRemainder()
* 5. finish processing by running getValues() and saving values for batch processing.
* Alternate:
* If the situation calls for it you can run all templates in a catagory in one go using the
* method runAll which will run all templates and goals for reference, and can optionally be saved
Expand Down Expand Up @@ -49,6 +48,9 @@ export class CategoryTemplate {
getRemainderWeight(): number {
return this.remainderWeight;
}
getLimitExcess(): number {
return this.limitExcess;
}

// what is the full requested amount this month
async runAll(available: number) {
Expand All @@ -69,6 +71,7 @@ export class CategoryTemplate {
availStart: number,
): Promise<number> {
if (!this.priorities.includes(priority)) return 0;
if (this.limitMet) return 0;

const t = this.templates.filter(t => t.priority === priority);
let available = budgetAvail || 0;
Expand Down Expand Up @@ -137,6 +140,13 @@ export class CategoryTemplate {
available = available - toBudget;
}

//check limit
if (this.limitCheck) {
if (toBudget + this.fromLastMonth >= this.limitAmount) {
toBudget = this.limitAmount - this.fromLastMonth;
this.limitMet = true;
}
}
// don't overbudget when using a priority
if (priority > 0 && available < 0) {
this.fullAmount += toBudget;
Expand All @@ -149,25 +159,6 @@ export class CategoryTemplate {
return toBudget;
}

applyLimit(): number {
if (this.limitCheck === false) {
return 0;
}
if (this.limitHold && this.fromLastMonth >= this.limitAmount) {
const orig = this.toBudgetAmount;
this.fullAmount = 0;
this.toBudgetAmount = 0;
return orig;
}
if (this.toBudgetAmount + this.fromLastMonth > this.limitAmount) {
const orig = this.toBudgetAmount;
this.toBudgetAmount = this.limitAmount - this.fromLastMonth;
this.fullAmount = this.toBudgetAmount;
return orig - this.toBudgetAmount;
}
return 0;
}

// run all of the 'remainder' type templates
runRemainder(budgetAvail: number, perWeight: number) {
if (this.remainder.length === 0) return 0;
Expand Down Expand Up @@ -206,6 +197,8 @@ export class CategoryTemplate {
private isLongGoal: boolean = null; //defaulting the goals to null so templates can be unset
private goalAmount: number = null;
private fromLastMonth = 0; // leftover from last month
private limitMet = false;
private limitExcess: number = 0;
private limitAmount = 0;
private limitCheck = false;
private limitHold = false;
Expand Down Expand Up @@ -344,31 +337,43 @@ export class CategoryTemplate {
if (!t.limit) continue;
if (this.limitCheck) {
throw new Error('Only one `up to` allowed per category');
} else if (t.limit) {
if (t.limit.period === 'daily') {
const numDays = monthUtils.differenceInCalendarDays(
monthUtils.addMonths(this.month, 1),
this.month,
);
this.limitAmount += amountToInteger(t.limit.amount) * numDays;
} else if (t.limit.period === 'weekly') {
const nextMonth = monthUtils.nextMonth(this.month);
let week = t.limit.start;
const baseLimit = amountToInteger(t.limit.amount);
while (week < nextMonth) {
if (week >= this.month) {
this.limitAmount += baseLimit;
}
week = monthUtils.addWeeks(week, 1);
}
if (t.limit.period === 'daily') {
const numDays = monthUtils.differenceInCalendarDays(
monthUtils.addMonths(this.month, 1),
this.month,
);
this.limitAmount += amountToInteger(t.limit.amount) * numDays;
} else if (t.limit.period === 'weekly') {
const nextMonth = monthUtils.nextMonth(this.month);
let week = t.limit.start;
const baseLimit = amountToInteger(t.limit.amount);
while (week < nextMonth) {
if (week >= this.month) {
this.limitAmount += baseLimit;
}
} else if (t.limit.period === 'monthly') {
this.limitAmount = amountToInteger(t.limit.amount);
week = monthUtils.addWeeks(week, 1);
}
} else if (t.limit.period === 'monthly') {
this.limitAmount = amountToInteger(t.limit.amount);
} else {
throw new Error('Invalid limit period. Check template syntax');
}
//amount is good save the rest
this.limitCheck = true;
this.limitHold = t.limit.hold ? true : false;
// check if the limit is already met and save the excess
if (this.fromLastMonth >= this.limitAmount) {
this.limitMet = true;
if (this.limitHold) {
this.limitExcess = 0;
this.toBudgetAmount = 0;
this.fullAmount = 0;
} else {
throw new Error('Invalid limit period. Check template syntax');
this.limitExcess = this.fromLastMonth - this.limitAmount;
this.toBudgetAmount = -this.limitExcess;
this.fullAmount = -this.limitExcess;
}
//amount is good save the rest
this.limitCheck = true;
this.limitHold = t.limit.hold ? true : false;
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-strict-ignore
import { t } from 'i18next';
//import { t } from 'i18next';

import { Notification } from '../../client/state-types/notifications';
import * as monthUtils from '../../shared/months';
Expand Down Expand Up @@ -166,6 +166,7 @@ async function processTemplate(
try {
const obj = await CategoryTemplate.init(templates, id, month);
availBudget += budgeted;
availBudget += obj.getLimitExcess();
const p = obj.getPriorities();
p.forEach(pr => priorities.push(pr));
remainderWeight += obj.getRemainderWeight();
Expand All @@ -190,13 +191,15 @@ async function processTemplate(
if (catObjects.length === 0 && errors.length === 0) {
return {
type: 'message',
message: t('Everything is up to date'),
//message: t('Everything is up to date'),
message: 'Everything is up to date',
};
}
if (errors.length > 0) {
return {
sticky: true,
message: t('There were errors interpreting some templates:'),
//message: t('There were errors interpreting some templates:'),
message: 'There were errors interpreting some templates:',
pre: errors.join(`\n\n`),
};
}
Expand All @@ -221,10 +224,6 @@ async function processTemplate(
availBudget -= ret;
}
}
// run limits
catObjects.forEach(o => {
availBudget += o.applyLimit();
});
// run remainder
if (availBudget > 0 && remainderWeight) {
const perWeight = availBudget / remainderWeight;
Expand All @@ -247,8 +246,9 @@ async function processTemplate(

return {
type: 'message',
message: t('Successfully applied templates to {length} categories', {
length: catObjects.length,
}),
//message: t('Successfully applied templates to {length} categories', {
// length: catObjects.length,
//}),
message: `Successfully applied templates to ${catObjects.length} categories`,
};
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/3829.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [youngcw]
---

Fix template limits