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 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Sonnet 4.6 committed 2026-03-31 10:44:50 +02:00
1 parent b420dc2a2b
commit 5436513e30
1 file changed
+3 -2
+3 -2
View File
@@ -646,10 +646,11 @@ export async function pgGetCaByFournisseur(
): Promise<CaByFournisseurRow[]> {
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,