diff --git a/src/app/(dashboard)/stock-negatif/client.tsx b/src/app/(dashboard)/stock-negatif/client.tsx index 673a1f3..86f3ef7 100644 --- a/src/app/(dashboard)/stock-negatif/client.tsx +++ b/src/app/(dashboard)/stock-negatif/client.tsx @@ -14,6 +14,13 @@ function fmtDate(raw: string | null) { return d.toLocaleDateString("fr-FR", { day: "2-digit", month: "2-digit", year: "numeric" }); } +function getYear(raw: string | null): string { + if (!raw) return ""; + const d = new Date(raw); + if (isNaN(d.getTime())) return ""; + return String(d.getFullYear()); +} + function SortIcon({ col, sortKey, sortDir }: { col: T; sortKey: T; sortDir: SortDir }) { if (col !== sortKey) return ; return sortDir === "asc" @@ -21,6 +28,27 @@ function SortIcon({ col, sortKey, sortDir }: { col: T; sortKey : ; } +function FilterSelect({ label, value, onChange, options }: { + label: string; + value: string; + onChange: (v: string) => void; + options: string[]; +}) { + return ( +
+ + +
+ ); +} + function useSortedRows>(rows: T[], numericKeys: string[]) { const [sortKey, setSortKey] = useState(Object.keys(rows[0] ?? { codein: "" })[0] as keyof T); const [sortDir, setSortDir] = useState("asc"); @@ -50,8 +78,26 @@ function useSortedRows>(rows: T[], numericKeys function TabStockNegatif({ rows, magasin }: { rows: PgStockNegatifRow[]; magasin: string }) { type SK = keyof PgStockNegatifRow; + + const [filterFournisseur, setFilterFournisseur] = useState(""); + const [filterAnnee, setFilterAnnee] = useState(""); + + const fournisseurs = useMemo(() => + [...new Set(rows.map(r => r.fournisseur).filter(Boolean))].sort((a, b) => a.localeCompare(b, "fr")), + [rows] + ); + const annees = useMemo(() => + [...new Set(rows.map(r => getYear(r.derniereentree)).filter(Boolean))].sort((a, b) => b.localeCompare(a)), + [rows] + ); + + const filtered = useMemo(() => rows.filter(r => + (!filterFournisseur || r.fournisseur === filterFournisseur) && + (!filterAnnee || getYear(r.derniereentree) === filterAnnee) + ), [rows, filterFournisseur, filterAnnee]); + const { sorted, sortKey, sortDir, handleSort } = useSortedRows( - rows.length ? rows : [] as PgStockNegatifRow[], + filtered, ["stockdispo"] ); @@ -87,11 +133,25 @@ function TabStockNegatif({ rows, magasin }: { rows: PgStockNegatifRow[]; magasin return (
-
- {sorted.length} article{sorted.length !== 1 ? "s" : ""} - +
+
+ + + {(filterFournisseur || filterAnnee) && ( + + )} +
+
+ {sorted.length} article{sorted.length !== 1 ? "s" : ""} + +
@@ -136,9 +196,28 @@ function TabStockNegatif({ rows, magasin }: { rows: PgStockNegatifRow[]; magasin function TabEntreesSansVente({ rows, magasin }: { rows: PgStockSansVenteRow[]; magasin: string }) { type SK = keyof PgStockSansVenteRow; - const filteredRows = useMemo(() => rows.filter(r => r.stock_actuel !== 0), [rows]); + + const [filterFournisseur, setFilterFournisseur] = useState(""); + const [filterAnnee, setFilterAnnee] = useState(""); + + const baseRows = useMemo(() => rows.filter(r => r.stock_actuel !== 0), [rows]); + + const fournisseurs = useMemo(() => + [...new Set(baseRows.map(r => r.fournisseur).filter(Boolean))].sort((a, b) => a.localeCompare(b, "fr")), + [baseRows] + ); + const annees = useMemo(() => + [...new Set(baseRows.map(r => getYear(r.derniere_entree)).filter(Boolean))].sort((a, b) => b.localeCompare(a)), + [baseRows] + ); + + const filtered = useMemo(() => baseRows.filter(r => + (!filterFournisseur || r.fournisseur === filterFournisseur) && + (!filterAnnee || getYear(r.derniere_entree) === filterAnnee) + ), [baseRows, filterFournisseur, filterAnnee]); + const { sorted, sortKey, sortDir, handleSort } = useSortedRows( - filteredRows, + filtered, ["stock_actuel"] ); @@ -174,11 +253,25 @@ function TabEntreesSansVente({ rows, magasin }: { rows: PgStockSansVenteRow[]; m return (
-
- {sorted.length} article{sorted.length !== 1 ? "s" : ""} - +
+
+ + + {(filterFournisseur || filterAnnee) && ( + + )} +
+
+ {sorted.length} article{sorted.length !== 1 ? "s" : ""} + +