From 5436513e309cdf72559e85df1f855f2532414828 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Tue, 31 Mar 2026 10:44:50 +0200 Subject: [PATCH] fix: use most recent artfou1 entry (no_id DESC) for supplier attribution DISTINCT ON with ORDER BY no_id ASC was picking the oldest supplier entry (BAZAR 5000 historically), attributing CA to a supplier the store no longer uses. Changing to DESC picks the most recently inserted artfou1 row, which represents the current active supplier for each article. Co-Authored-By: Claude Sonnet 4.6 --- src/lib/pg-ff-client.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/pg-ff-client.ts b/src/lib/pg-ff-client.ts index cf71352..8feb148 100644 --- a/src/lib/pg-ff-client.ts +++ b/src/lib/pg-ff-client.ts @@ -646,10 +646,11 @@ export async function pgGetCaByFournisseur( ): Promise { const result = await pgNoParallel(sql` WITH art_fou AS ( - -- Un seul fournisseur par article (le plus ancien no_id = fournisseur principal) + -- Un seul fournisseur par article : le plus récent (no_id DESC = entrée courante) + -- no_id est une séquence croissante → DESC sélectionne le fournisseur actif actuel SELECT DISTINCT ON (art_no_id) art_no_id, code AS codefou FROM artfou1 - ORDER BY art_no_id, no_id + ORDER BY art_no_id, no_id DESC ) SELECT af.codefou AS code,