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

Fix yearly schedule templates #3511

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
64 changes: 64 additions & 0 deletions packages/loot-core/src/server/budget/goals/goalsSchedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,68 @@ describe('goalsSchedule', () => {
expect(result.remainder).toBe(0);
expect(result.scheduleFlag).toBe(true);
});

it('should return correct budget when yearly recurring schedule set and balance is greater than target', async () => {
// Given
const scheduleFlag = false;
const template_lines = [{ type: 'schedule', name: 'Test Schedule' }];
const current_month = '2024-09-01';
const balance = 12000;
const remainder = 0;
const last_month_balance = 12000;
const to_budget = 0;
const errors: string[] = [];
const category = { id: 1, name: 'Test Category' };

(db.first as jest.Mock).mockResolvedValue({ id: 1, completed: 0 });
(getRuleForSchedule as jest.Mock).mockResolvedValue({
serialize: () => ({
conditions: [
{
op: 'is',
field: 'date',
value: {
start: '2024-08-01',
interval: 1,
frequency: 'yearly',
patterns: [],
skipWeekend: false,
weekendSolveMode: 'before',
endMode: 'never',
endOccurrences: 1,
endDate: '2024-08-04',
},
type: 'date',
},
{
op: 'is',
field: 'amount',
value: -12000,
type: 'number',
},
],
}),
execActions: () => ({ amount: -12000, subtransactions: [] }),
});
(isReflectBudget as jest.Mock).mockReturnValue(false);

// When
const result = await goalsSchedule(
scheduleFlag,
template_lines,
current_month,
balance,
remainder,
last_month_balance,
to_budget,
errors,
category,
);

// Then
expect(result.to_budget).toBe(1000);
expect(result.errors).toHaveLength(0);
expect(result.remainder).toBe(0);
expect(result.scheduleFlag).toBe(true);
});
});
6 changes: 5 additions & 1 deletion packages/loot-core/src/server/budget/goals/goalsSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ async function getSinkingBaseContributionTotal(t) {
//return only the base contribution of each schedule
let total = 0;
for (let ll = 0; ll < t.length; ll++) {
total += t[ll].target / t[ll].target_interval;
let monthlyAmount = t[ll].target / t[ll].target_interval;
if (t[ll].target_frequency === 'yearly') {
monthlyAmount /= 12;
}
total += monthlyAmount;
}
return total;
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3511.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [JukeboxRhino]
---

Fix yearly schedule templates not behaving correctly when budgeting ahead of the transaction date
Loading