diff --git a/src/app/(dashboard)/grid/page.tsx b/src/app/(dashboard)/grid/page.tsx index c20dd6f..5e2cc64 100644 --- a/src/app/(dashboard)/grid/page.tsx +++ b/src/app/(dashboard)/grid/page.tsx @@ -26,7 +26,7 @@ export default async function GridPage({ searchParams }: GridPageProps) { const [fournisseurs, magasins, nomenclature] = await Promise.all([ getFournisseurs(), getMagasins(), - getAvailableNomenclature() + getAvailableNomenclature(codeFournisseur, magasin) ]); // 2. If no supplier selected, show compact selection UI diff --git a/src/features/grid/actions.ts b/src/features/grid/actions.ts index 59ea8dc..3a16d27 100644 --- a/src/features/grid/actions.ts +++ b/src/features/grid/actions.ts @@ -2,7 +2,7 @@ import { db } from "@/db"; import { ventesProduits } from "@/db/schema"; -import { sql } from "drizzle-orm"; +import { sql, and, eq } from "drizzle-orm"; import { getProductRows } from "./api/get-product-rows"; import type { ProductRow, GridFilters } from "@/types/grid"; @@ -75,15 +75,28 @@ const NOMENCLATURE_LABELS: Record = { /** * Get the list of all unique nomenclature levels available in the database. */ -export async function getAvailableNomenclature() { +export async function getAvailableNomenclature( + codeFournisseur?: string, + magasin?: string +) { try { + const conditions = [sql`${ventesProduits.code3} IS NOT NULL`]; + + if (codeFournisseur) { + conditions.push(eq(ventesProduits.codeFournisseur, codeFournisseur)); + } + + if (magasin && magasin !== "TOTAL") { + conditions.push(eq(ventesProduits.magasin, magasin)); + } + const results = await db .select({ code3: ventesProduits.code3, libelle3: ventesProduits.libelle3, }) .from(ventesProduits) - .where(sql`${ventesProduits.code3} IS NOT NULL`) + .where(and(...conditions)) .groupBy(ventesProduits.code3, ventesProduits.libelle3); const hierarchy: Record }> = {};