Skip to content

Commit

Permalink
fix yearly schedule templates (#3511)
Browse files Browse the repository at this point in the history
Co-authored-by: Austin Pearce <[email protected]>
  • Loading branch information
JukeboxRhino and Austin Pearce authored Oct 3, 2024
1 parent 3f85aed commit d0caf9f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
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

0 comments on commit d0caf9f

Please sign in to comment.