From 4e70eee0a844be7d876370095baf0cc4ec3c7033 Mon Sep 17 00:00:00 2001 From: medic-bot Date: Tue, 10 Mar 2026 23:03:26 -0400 Subject: [PATCH] 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 --- CHANGELOG.fr.md | 3 +++ CHANGELOG.md | 3 +++ src/hooks/useBudget.ts | 11 ++++++----- src/services/budgetService.ts | 10 ++++++++++ src/shared/types/index.ts | 2 +- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.fr.md b/CHANGELOG.fr.md index 4fb3f87..202dffd 100644 --- a/CHANGELOG.fr.md +++ b/CHANGELOG.fr.md @@ -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é diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b384a4..81f294b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Changed +- Budget table: previous year column now shows actual transactions instead of planned budget (#34) + ## [0.6.5] ### Added diff --git a/src/hooks/useBudget.ts b/src/hooks/useBudget.ts index 9ee0899..efa99c4 100644 --- a/src/hooks/useBudget.ts +++ b/src/hooks/useBudget.ts @@ -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(); - 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 diff --git a/src/services/budgetService.ts b/src/services/budgetService.ts index 8e4d625..8b2bdb3 100644 --- a/src/services/budgetService.ts +++ b/src/services/budgetService.ts @@ -178,6 +178,16 @@ export async function deleteTemplate(templateId: number): Promise { await db.execute("DELETE FROM budget_templates WHERE id = $1", [templateId]); } +// --- Actuals helpers --- + +export async function getActualTotalsForYear( + year: number +): Promise> { + const dateFrom = `${year}-01-01`; + const dateTo = `${year}-12-31`; + return getActualsByCategoryRange(dateFrom, dateTo); +} + // --- Budget vs Actual --- async function getActualsByCategoryRange( diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index 3c96ae7..93f0019 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -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 {