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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix cases of negative previous balance
youngcw committed Nov 13, 2024
commit 0a6f162041894346c5d8d66f2e985810e76c1e41
9 changes: 9 additions & 0 deletions packages/loot-core/src/server/budget/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-strict-ignore

import * as monthUtils from '../../shared/months';
import { integerToCurrency, safeNumber } from '../../shared/util';
import * as db from '../db';
@@ -13,6 +14,14 @@ export async function getSheetValue(
return safeNumber(typeof node.value === 'number' ? node.value : 0);
}

export async function getSheetBoolean(
sheetName: string,
cell: string,
): Promise<boolean> {
const node = await sheet.getCell(sheetName, cell);
return typeof node.value === 'boolean' ? node.value : false;
}

// We want to only allow the positive movement of money back and
// forth. buffered should never be allowed to go into the negative,
// and you shouldn't be allowed to pull non-existent money from
28 changes: 23 additions & 5 deletions packages/loot-core/src/server/budget/categoryTemplate.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import * as monthUtils from '../../shared/months';
import { amountToInteger } from '../../shared/util';
import * as db from '../db';

import { getSheetValue } from './actions';
import { getSheetValue, getSheetBoolean } from './actions';
import { goalsSchedule } from './goalsSchedule';
import { getActiveSchedules } from './statements';
import { Template } from './types/templates';
@@ -31,10 +31,23 @@ export class CategoryTemplate {
// set up the class and check all templates
static async init(templates: Template[], categoryID: string, month) {
// get all the needed setup values
const fromLastMonth = await getSheetValue(
monthUtils.sheetForMonth(monthUtils.subMonths(month, 1)),
const lastMonthSheet = monthUtils.sheetForMonth(
monthUtils.subMonths(month, 1),
);
const lastMonthBalance = await getSheetValue(
lastMonthSheet,
`leftover-${categoryID}`,
);
const carryover = await getSheetBoolean(
lastMonthSheet,
`carryover-${categoryID}`,
);
let fromLastMonth;
if (lastMonthBalance < 0 && !carryover) {
fromLastMonth = 0;
} else {
fromLastMonth = lastMonthBalance;
}
// run all checks
await CategoryTemplate.checkByAndScheduleAndSpend(templates, month);
await CategoryTemplate.checkPercentage(templates);
@@ -142,9 +155,14 @@ export class CategoryTemplate {

//check limit
if (this.limitCheck) {
if (toBudget + this.fromLastMonth >= this.limitAmount) {
toBudget = this.limitAmount - this.fromLastMonth;
if (
toBudget + this.toBudgetAmount + this.fromLastMonth >=
this.limitAmount
) {
const orig = toBudget;
toBudget = this.limitAmount - this.toBudgetAmount - this.fromLastMonth;
this.limitMet = true;
available = available + orig - toBudget;
}
}
// don't overbudget when using a priority