diff --git a/src/features/grid/api/get-product-rows.ts b/src/features/grid/api/get-product-rows.ts index 5b69047..e06e137 100644 --- a/src/features/grid/api/get-product-rows.ts +++ b/src/features/grid/api/get-product-rows.ts @@ -14,6 +14,14 @@ interface GetProductRowsInput { export async function getProductRows(input: GetProductRowsInput): Promise { const { codeFournisseur, magasin = "TOTAL" } = input; + // Calculer les 12 derniers mois COMPLETS (excluant le mois en cours) + const allowedPeriods = new Set(); + const now = new Date(); + for (let i = 12; i >= 1; i--) { + const d = new Date(now.getFullYear(), now.getMonth() - i, 1); + allowedPeriods.add(`${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}`); + } + // Get all rows for this supplier (excluding the synthetic TOTAL period) const rows = await db .select() @@ -30,6 +38,9 @@ export async function getProductRows(input: GetProductRowsInput): Promise(); for (const row of rows) { + // Ignorer les données qui ne font pas partie des 12 mois complets + if (!allowedPeriods.has(row.periode)) continue; + const existing = productMap.get(row.codein); const monthKey = row.periode; const qty = Math.abs(parseFloat(row.quantite?.toString() ?? "0")); diff --git a/src/features/grid/components/heatmap-grid.tsx b/src/features/grid/components/heatmap-grid.tsx index 48df866..bda8237 100644 --- a/src/features/grid/components/heatmap-grid.tsx +++ b/src/features/grid/components/heatmap-grid.tsx @@ -24,7 +24,9 @@ import { AiInsightBlock } from "@/features/ai-copilot/components/ai-insight-bloc function getLast12Months(): string[] { const months: string[] = []; const now = new Date(); - for (let i = 11; i >= 0; i--) { + // On commence à i=12 (il y a 12 mois) et on finit à i=1 (le mois dernier) + // pour exclure le mois en cours (i=0) + for (let i = 12; i >= 1; i--) { const d = new Date(now.getFullYear(), now.getMonth() - i, 1); months.push(`${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}`); }