A local-first personal finance app
[Goals] limits for remainders (#5301)
youngcwdbcfb6385792
diff --git a/packages/loot-core/src/server/budget/category-template-context.ts b/packages/loot-core/src/server/budget/category-template-context.tsindex 75b7e8aa0c..07fdcd01c5 100644--- a/packages/loot-core/src/server/budget/category-template-context.ts+++ b/packages/loot-core/src/server/budget/category-template-context.ts@@ -102,6 +102,9 @@ export class CategoryTemplateContext {getPriorities(): number[] {return this.priorities;}+ hasRemainder(): boolean {+ return this.remainderWeight > 0 && !this.limitMet;+ }getRemainderWeight(): number {return this.remainderWeight;}@@ -236,23 +239,34 @@ export class CategoryTemplateContext {return toBudget;}- // run all of the 'remainder' type templatesrunRemainder(budgetAvail: number, perWeight: number) {if (this.remainder.length === 0) return 0;let toBudget = Math.round(this.remainderWeight * perWeight);++ let smallest = 1;+ if (this.hideDecimal) {+ // handle hideDecimal+ toBudget = this.removeFraction(toBudget);+ smallest = 100;+ }+//check possible overbudget from rounding, 1cent leftover- if (toBudget > budgetAvail) {+ if (toBudget > budgetAvail || budgetAvail - toBudget <= smallest) {toBudget = budgetAvail;- this.toBudgetAmount += toBudget;- return toBudget;- } else if (budgetAvail - toBudget === 1) {- toBudget += 1;- this.toBudgetAmount += toBudget;- return toBudget;- } else {- this.toBudgetAmount += toBudget;- return toBudget;}++ if (this.limitCheck) {+ if (+ toBudget + this.toBudgetAmount + this.fromLastMonth >=+ this.limitAmount+ ) {+ toBudget = this.limitAmount - this.toBudgetAmount - this.fromLastMonth;+ this.limitMet = true;+ }+ }++ this.toBudgetAmount += toBudget;+ return toBudget;}getValues() {@@ -315,8 +329,8 @@ export class CategoryTemplateContext {if (t.directive === 'goal' && t.type === 'goal') this.goals.push(t);});}- // check limits here since it needs to save states inside the object- this.checkLimit();++ this.checkLimit(templates);this.checkSpend();this.checkGoal();@@ -429,8 +443,8 @@ export class CategoryTemplateContext {});}- private checkLimit() {- for (const template of this.templates+ private checkLimit(templates: Template[]) {+ for (const template of templates.filter(t =>t.type === 'simple' ||diff --git a/packages/loot-core/src/server/budget/goal-template.ts b/packages/loot-core/src/server/budget/goal-template.tsindex b3cc4a1c0f..a547dc9499 100644--- a/packages/loot-core/src/server/budget/goal-template.ts+++ b/packages/loot-core/src/server/budget/goal-template.ts@@ -170,7 +170,6 @@ async function processTemplate(`to-budget`,);let priorities: number[] = [];- let remainderWeight = 0;const errors: string[] = [];const budgetList: TemplateBudget[] = [];const goalList: TemplateGoal[] = [];@@ -199,7 +198,6 @@ async function processTemplate(}availBudget += templateContext.getLimitExcess();priorities = [...priorities, ...templateContext.getPriorities()];- remainderWeight += templateContext.getRemainderWeight();templateContexts.push(templateContext);} catch (e) {errors.push(`${category.name}: ${e.message}`);@@ -252,13 +250,21 @@ async function processTemplate(availBudget -= budget;}}+// run remainder- if (availBudget > 0 && remainderWeight) {+ let remainderContexts = templateContexts.filter(c => c.hasRemainder());+ while (availBudget > 0 && remainderContexts.length > 0) {+ let remainderWeight = 0;+ remainderContexts.forEach(+ context => (remainderWeight += context.getRemainderWeight()),+ );const perWeight = availBudget / remainderWeight;- templateContexts.forEach(context => {+ remainderContexts.forEach(context => {availBudget -= context.runRemainder(availBudget, perWeight);});+ remainderContexts = templateContexts.filter(c => c.hasRemainder());}+// finishtemplateContexts.forEach(context => {const values = context.getValues();diff --git a/upcoming-release-notes/5301.md b/upcoming-release-notes/5301.mdnew file mode 100644index 0000000000..e0b13e77db--- /dev/null+++ b/upcoming-release-notes/5301.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [youngcw]+---++Make remainder templates respect up to limits