feat: show actual transactions in budget previous year column

Replace planned budget data with actual transaction totals for the
previous year column in the budget table. Add getActualTotalsForYear
helper to budgetService.

Ref #34

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
medic-bot 2026-03-10 23:03:26 -04:00
parent 52faa017f3
commit 4e70eee0a8
5 changed files with 23 additions and 6 deletions

View file

@ -2,6 +2,9 @@
## [Non publié]
### Modifié
- Tableau de budget : la colonne année précédente affiche maintenant le réel (transactions) au lieu du budget planifié (#34)
## [0.6.5]
### Ajouté

View file

@ -2,6 +2,9 @@
## [Unreleased]
### Changed
- Budget table: previous year column now shows actual transactions instead of planned budget (#34)
## [0.6.5]
### Added

View file

@ -3,6 +3,7 @@ import type { BudgetYearRow, BudgetTemplate } from "../shared/types";
import {
getAllActiveCategories,
getBudgetEntriesForYear,
getActualTotalsForYear,
upsertBudgetEntry,
upsertBudgetEntriesForYear,
getAllTemplates,
@ -72,10 +73,10 @@ export function useBudget() {
dispatch({ type: "SET_ERROR", payload: null });
try {
const [allCategories, entries, prevYearEntries, templates] = await Promise.all([
const [allCategories, entries, prevYearActuals, templates] = await Promise.all([
getAllActiveCategories(),
getBudgetEntriesForYear(year),
getBudgetEntriesForYear(year - 1),
getActualTotalsForYear(year - 1),
getAllTemplates(),
]);
@ -88,10 +89,10 @@ export function useBudget() {
entryMap.get(e.category_id)!.set(e.month, e.amount);
}
// Build a map for previous year totals: categoryId -> annual total
// Build a map for previous year actuals: categoryId -> annual actual total
const prevYearTotalMap = new Map<number, number>();
for (const e of prevYearEntries) {
prevYearTotalMap.set(e.category_id, (prevYearTotalMap.get(e.category_id) ?? 0) + e.amount);
for (const a of prevYearActuals) {
if (a.category_id != null) prevYearTotalMap.set(a.category_id, a.actual);
}
// Helper: build months array from entryMap

View file

@ -178,6 +178,16 @@ export async function deleteTemplate(templateId: number): Promise<void> {
await db.execute("DELETE FROM budget_templates WHERE id = $1", [templateId]);
}
// --- Actuals helpers ---
export async function getActualTotalsForYear(
year: number
): Promise<Array<{ category_id: number | null; actual: number }>> {
const dateFrom = `${year}-01-01`;
const dateTo = `${year}-12-31`;
return getActualsByCategoryRange(dateFrom, dateTo);
}
// --- Budget vs Actual ---
async function getActualsByCategoryRange(

View file

@ -142,7 +142,7 @@ export interface BudgetYearRow {
depth?: number;
months: number[]; // index 0-11 = Jan-Dec planned amounts
annual: number; // computed sum
previousYearTotal: number; // total budget from the previous year
previousYearTotal: number; // actual (transactions) total from the previous year
}
export interface ImportConfigTemplate {