diff --git a/src/features/grid/actions.ts b/src/features/grid/actions.ts index 4b4bc96..59ea8dc 100644 --- a/src/features/grid/actions.ts +++ b/src/features/grid/actions.ts @@ -52,6 +52,26 @@ export async function getMagasins() { } } +/** + * Mapping global des libellés de nomenclature (Secteurs & Rayons) + */ +const NOMENCLATURE_LABELS: Record = { + // Secteurs (Niveau 1 - 2 digits) + "20": "LIQUIDES", + "30": "ÉPICERIE", + "40": "DPH", + "50": "BAZAR", + "60": "TEXTILE", + "70": "FRAIS", + + // Rayons (Niveau 2 - 4 digits) + "2001": "EAUX", "2002": "JUS DE FRUITS", "2003": "SODAS / BOUILLONS", "2004": "BIERES", "2005": "VINS", "2006": "CIDRES", "2007": "APERITIFS", "2008": "ALCOOLS", + "3001": "ÉPICERIE SALÉE", "3002": "ÉPICERIE SUCRÉE", "3003": "PETIT DÉJEUNER", "3004": "CONSERVES", "3005": "PLAT CUISINES", + "4001": "HYGIÈNE", "4002": "ENTRETIEN", "4003": "PARFUMERIE", + "5001": "BRICOLAGE", "5002": "MAISON", "5003": "LOISIRS", "5004": "JARDIN", + "7001": "FRUITS ET LEGUMES", "7002": "CREMERIE", "7003": "VOLAILLE", "7004": "CHARCUTERIE", "7005": "TRAITEUR", "7006": "BOULANGERIE", +}; + /** * Get the list of all unique nomenclature levels available in the database. */ @@ -68,29 +88,19 @@ export async function getAvailableNomenclature() { const hierarchy: Record }> = {}; - // Mapping basique des libellés Niv 1 et 2 - const L1_LABELS: Record = { - "30": "ÉPICERIE", - "31": "CRÉMERIE / FRAIS", - "32": "LIQUIDES", - "33": "DPH", - "34": "BAZAR", - "35": "TEXTILE", - }; - results.forEach(r => { const c3 = r.code3 || ""; const l3 = r.libelle3 || ""; - if (c3.length < 2) return; + if (c3.length < 6) return; const c1 = c3.slice(0, 2); const c2 = c3.slice(0, 4); if (!hierarchy[c1]) { - hierarchy[c1] = { label: L1_LABELS[c1] || `Niveau 1 (${c1})`, children: {} }; + hierarchy[c1] = { label: NOMENCLATURE_LABELS[c1] || `Secteur ${c1}`, children: {} }; } if (!hierarchy[c1].children[c2]) { - hierarchy[c1].children[c2] = { label: `Niveau 2 (${c2})`, children: [] }; + hierarchy[c1].children[c2] = { label: NOMENCLATURE_LABELS[c2] || `Rayon ${c2}`, children: [] }; } hierarchy[c1].children[c2].children.push({ code: c3, label: l3 }); }); diff --git a/src/features/grid/api/get-product-rows.ts b/src/features/grid/api/get-product-rows.ts index d7bdbc7..2e0a533 100644 --- a/src/features/grid/api/get-product-rows.ts +++ b/src/features/grid/api/get-product-rows.ts @@ -77,13 +77,19 @@ export async function getProductRows(input: GetProductRowsInput): Promise = { + const NOMENCLATURE_LABELS: Record = { + "20": "LIQUIDES", "30": "ÉPICERIE", - "31": "CRÉMERIE / FRAIS", - "32": "LIQUIDES", - "33": "DPH", - "34": "BAZAR", - "35": "TEXTILE", + "40": "DPH", + "50": "BAZAR", + "60": "TEXTILE", + "70": "FRAIS", + // Rayons + "2001": "EAUX", "2002": "JUS DE FRUITS", "2003": "SODAS / BOUILLONS", "2004": "BIERES", "2005": "VINS", "2006": "CIDRES", "2007": "APERITIFS", "2008": "ALCOOLS", + "3001": "ÉPICERIE SALÉE", "3002": "ÉPICERIE SUCRÉE", "3003": "PETIT DÉJEUNER", "3004": "CONSERVES", "3005": "PLAT CUISINES", + "4001": "HYGIÈNE", "4002": "ENTRETIEN", "4003": "PARFUMERIE", + "5001": "BRICOLAGE", "5002": "MAISON", "5003": "LOISIRS", "5004": "JARDIN", + "7001": "FRUITS ET LEGUMES", "7002": "CREMERIE", "7003": "VOLAILLE", "7004": "CHARCUTERIE", "7005": "TRAITEUR", "7006": "BOULANGERIE", }; productMap.set(codein, { @@ -94,9 +100,9 @@ export async function getProductRows(input: GetProductRowsInput): Promise) => { - const nextParams = new URLSearchParams(searchParams.toString()); - Object.entries(params).forEach(([key, value]) => { - if (value) nextParams.set(key, value); - else nextParams.delete(key); - }); + // Valeur composite pour le select : "type:code" + const currentValue = selected3 ? `code3:${selected3}` : + selected2 ? `code2:${selected2}` : + selected1 ? `code1:${selected1}` : ""; - // Reset children if parent changes - if (params.code1 !== undefined) { - nextParams.delete("code2"); - nextParams.delete("code3"); - } - if (params.code2 !== undefined) { - nextParams.delete("code3"); + const updateFilter = (value: string) => { + const params = new URLSearchParams(searchParams.toString()); + + // Reset all nomenclature filters + params.delete("code1"); + params.delete("code2"); + params.delete("code3"); + + if (value) { + const [type, code] = value.split(":"); + params.set(type, code); } - router.push(`/grid?${nextParams.toString()}`); + router.push(`/grid?${params.toString()}`); }; - const l1Options = Object.entries(hierarchy).map(([code, data]: [string, any]) => ({ - code, - label: data.label - })).sort((a, b) => a.code.localeCompare(b.code)); + // Aplatir la hiérarchie pour le menu unique et trier par code + const options: { label: string; value: string; level: number; code: string }[] = []; - const l2Options = selected1 && hierarchy[selected1] - ? Object.entries(hierarchy[selected1].children).map(([code, data]: [string, any]) => ({ - code, - label: data.label - })).sort((a, b) => a.code.localeCompare(b.code)) - : []; + // Sort Secteurs + const sortedSecteurs = Object.entries(hierarchy).sort((a, b) => a[0].localeCompare(b[0], undefined, { numeric: true })); - const l3Options = selected1 && selected2 && hierarchy[selected1]?.children[selected2] - ? hierarchy[selected1].children[selected2].children - .sort((a: any, b: any) => a.code.localeCompare(b.code)) - : []; + sortedSecteurs.forEach(([c1, s]: [string, any]) => { + options.push({ label: s.label, value: `code1:${c1}`, level: 0, code: c1 }); + + // Sort Rayons + const sortedRayons = Object.entries(s.children).sort((a, b) => a[0].localeCompare(b[0], undefined, { numeric: true })); + + sortedRayons.forEach(([c2, r]: [string, any]) => { + options.push({ label: r.label, value: `code2:${c2}`, level: 1, code: c2 }); + + // Sort Familles + const sortedFamilles = [...r.children].sort((a, b) => a.code.localeCompare(b.code, undefined, { numeric: true })); + + sortedFamilles.forEach((f: any) => { + options.push({ label: f.label, value: `code3:${f.code}`, level: 2, code: f.code }); + }); + }); + }); return ( -
- {/* Level 1 */} +
+
+ +
+ - - - {/* Level 2 */} - - - - - {/* Level 3 */} - - - {(selected1 || selected2 || selected3) && ( - - )} +
+ +
); }