fix: query ventesProduits by codein array instead of code_fournisseur

The old fournisseur code stored in ventesProduits may differ from the
new API codefou (e.g. "AUXENCE" vs "A025"), causing zero results.
Querying by codein = ANY(...) finds the classification data regardless
of how the supplier was previously keyed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Sonnet 4.6 committed 2026-03-18 19:05:58 +01:00
1 parent 518838c01e
commit 09be89b557
1 file changed
+20 -17
+20 -17
View File
@@ -9,7 +9,7 @@ import {
buildLast12MonthsRange, buildLast12MonthsRange,
} from "@/lib/api-ff-client"; } from "@/lib/api-ff-client";
import { db } from "@/db"; import { db } from "@/db";
import { sessionSnapshots, ventesProduits } from "@/db/schema"; import { sessionSnapshots } from "@/db/schema";
import { eq, desc, sql } from "drizzle-orm"; import { eq, desc, sql } from "drizzle-orm";
interface GetProductRowsInput { interface GetProductRowsInput {
@@ -152,25 +152,28 @@ export async function getProductRows(input: GetProductRowsInput): Promise<Produc
} }
// ─── Phase 6 : Classification depuis ventesProduits (lecture seule) ── // ─── Phase 6 : Classification depuis ventesProduits (lecture seule) ──
// Source de vérité pour : codeGamme initial, codeGammeInit, code3, libelle3 // Requête par codein (pas par code_fournisseur, car l'ancien code peut différer)
try { try {
const classifRows = await db.execute(sql` const codeins = Array.from(productMap.keys());
SELECT DISTINCT ON (codein) if (codeins.length > 0) {
codein, code_gamme, code_gamme_init, code3, libelle3 const classifRows = await db.execute(sql`
FROM ventes_produits SELECT DISTINCT ON (codein)
WHERE code_fournisseur = ${codeFournisseur} codein, code_gamme, code_gamme_init, code3, libelle3
ORDER BY codein, updated_at DESC FROM ventes_produits
`); WHERE codein = ANY(${codeins})
for (const row of classifRows.rows) { ORDER BY codein, updated_at DESC
const product = productMap.get(row.codein as string); `);
if (product) { for (const row of classifRows.rows) {
product.codeGamme = (row.code_gamme as string | null) ?? null; const product = productMap.get(row.codein as string);
product.codeGammeInit = (row.code_gamme_init as string | null) ?? null; if (product) {
product.code3 = (row.code3 as string | null) ?? ""; product.codeGamme = (row.code_gamme as string | null) ?? null;
product.libelle3 = (row.libelle3 as string | null) ?? ""; product.codeGammeInit = (row.code_gamme_init as string | null) ?? null;
product.code3 = (row.code3 as string | null) ?? "";
product.libelle3 = (row.libelle3 as string | null) ?? "";
}
} }
console.log(`[getProductRows] Classification: ${classifRows.rows.length} lignes trouvées pour ${codeins.length} articles`);
} }
console.log(`[getProductRows] Classification chargée pour ${classifRows.rows.length} lignes`);
} catch (classifErr) { } catch (classifErr) {
console.warn("[getProductRows] ventesProduits classification non disponible:", classifErr); console.warn("[getProductRows] ventesProduits classification non disponible:", classifErr);
} }